Class: EventedNet::HTTP::Connection

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::Deferrable, HttpEncoding
Defined in:
lib/http/connection.rb

Constant Summary collapse

ALLOWED_METHODS =
[:put, :get, :post, :delete, :head]
TRANSFER_ENCODING =
"TRANSFER_ENCODING"
CONTENT_LENGTH =
"CONTENT_LENGTH"
"SET_COOKIE"
LOCATION =
"LOCATION"
HOST =
"HOST"
CRLF =
"\r\n"

Constants included from HttpEncoding

HttpEncoding::FIELD_ENCODING, HttpEncoding::HTTP_REQUEST_HEADER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HttpEncoding

#encode_cookies, #encode_field, #encode_headers, #encode_host, #encode_param, #encode_query, #encode_request, #escape, #munge_header_keys, #unescape

Class Method Details

.request(args = {}) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/http/connection.rb', line 118

def request(args = {})
  args[:port] ||= 80
  # According to the docs, we will get here AFTER post_init is called.
  EventMachine.connect(args[:host], args[:port], self) do |c|
    c.instance_eval { @args = args }
  end
end

Instance Method Details

#connection_completedObject



145
146
147
148
# File 'lib/http/connection.rb', line 145

def connection_completed
  @connected = true
  send_request(@args)
end

#dispatchObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/http/connection.rb', line 217

def dispatch
  while @connected and case @state
    when :response_header
      parse_response_header
    when :chunk_header
      parse_chunk_header
    when :chunk_body
      process_chunk_body
    when :chunk_footer
      process_chunk_footer
    when :response_footer
      process_response_footer
    when :body
      process_body
    when :finished, :invalid
      break
    else raise RuntimeError, "Invalid state: #{@state}"
    end
  end
end

#on_body_data(data) ⇒ Object

Called when part of the body has been read



195
196
197
# File 'lib/http/connection.rb', line 195

def on_body_data(data)
  @response_body = data
end

#on_error(reason) ⇒ Object

Called when an error occurs dispatching the request

Raises:

  • (RuntimeError)


212
213
214
215
# File 'lib/http/connection.rb', line 212

def on_error(reason)
  close_connection
  raise RuntimeError, reason
end

#on_request_completeObject

Called when the request has completed



200
201
202
203
204
205
206
207
208
209
# File 'lib/http/connection.rb', line 200

def on_request_complete
  # Reset the state of the client
  @state, @connected = :response_header, false
  set_deferred_status :succeeded, {
    :content => @response_body,
    :headers => @response_header,
    :status => @response_header.status
  }
  close_connection
end

#on_response_header(response_header) ⇒ Object

Called when response header has been received



191
192
# File 'lib/http/connection.rb', line 191

def on_response_header(response_header)
end

#parse_chunk_headerObject



279
280
281
282
283
284
285
286
287
# File 'lib/http/connection.rb', line 279

def parse_chunk_header
  return false unless parse_header(@chunk_header)

  @bytes_remaining = @chunk_header.chunk_size
  @chunk_header = HttpChunkHeader.new

  @state = @bytes_remaining > 0 ? :chunk_body : :response_footer      
  true
end

#parse_header(header) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/http/connection.rb', line 238

def parse_header(header)
  return false if @data.empty?

  begin
    @parser_nbytes = @parser.execute(header, @data.to_str, @parser_nbytes)
  rescue Rev::HttpClientParserError
    on_error "Invalid HTTP format, parsing fails"
    @state = :invalid
  end

  return false unless @parser.finished?

  # Clear parsed data from the buffer
  @data.read(@parser_nbytes)
  @parser.reset
  @parser_nbytes = 0

  true
end

#parse_response_headerObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/http/connection.rb', line 258

def parse_response_header
  return false unless parse_header(@response_header)

  unless @response_header.http_status and @response_header.http_reason
    on_error "No HTTP response"
    @state = :invalid
    return false
  end

  on_response_header(@response_header)

  if @response_header.chunked_encoding?
    @state = :chunk_header
  else
    @state = :body
    @bytes_remaining = @response_header.content_length
  end

  true
end

#post_initObject



135
136
137
138
139
140
141
142
143
# File 'lib/http/connection.rb', line 135

def post_init
  @parser = Rev::HttpClientParser.new
  @parser_nbytes = 0
  @state = :response_header
  @data = Rev::Buffer.new
  @response_header = HttpResponseHeader.new
  @response_body = ''
  @chunk_header = HttpChunkHeader.new
end

#process_bodyObject



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/http/connection.rb', line 335

def process_body
  if @bytes_remaining.nil?
    on_body_data(@data.read)
    return false
  end

  if @bytes_remaining.zero?
    on_request_complete
    @state = :finished
    return false
  end

  if @data.size < @bytes_remaining
    @bytes_remaining -= @data.size
    on_body_data(@data.read)
    return false
  end
  
  on_body_data(@data.read(@bytes_remaining))
  @bytes_remaining = 0
  if @data.empty?
    on_request_complete
    @state = :finished
  else
    on_error "Garbage at end of body"
    @state = :invalid
  end

  false
end

#process_chunk_bodyObject



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/http/connection.rb', line 289

def process_chunk_body
  if @data.size < @bytes_remaining
    @bytes_remaining -= @data.size
    on_body_data(@data.read)
    return false
  end

  on_body_data(@data.read(@bytes_remaining))
  @bytes_remaining = 0

  @state = :chunk_footer      
  true
end


303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/http/connection.rb', line 303

def process_chunk_footer
  return false if @data.size < 2

  if @data.read(2) == CRLF
    @state = :chunk_header
  else
    on_error "Non-CRLF chunk footer"
    @state = :invalid
  end

  true
end


316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/http/connection.rb', line 316

def process_response_footer
  return false if @data.size < 2

  if @data.read(2) == CRLF
    if @data.empty?
      on_request_complete
      @state = :finished
    else
      on_error "Garbage at end of chunked response"
      @state = :invalid
    end
  else
    on_error "Non-CRLF response footer"
    @state = :invalid
  end

  false
end

#receive_data(data) ⇒ Object



185
186
187
188
# File 'lib/http/connection.rb', line 185

def receive_data(data)
  @data << data
  dispatch
end

#remote_hostObject



127
128
129
# File 'lib/http/connection.rb', line 127

def remote_host
  @args[:host]
end

#remote_portObject



131
132
133
# File 'lib/http/connection.rb', line 131

def remote_port
  @args[:port]
end

#send_request(args) ⇒ Object



150
151
152
153
# File 'lib/http/connection.rb', line 150

def send_request(args)
  send_request_header(args)
  send_request_body(args)
end

#send_request_body(args) ⇒ Object



181
182
183
# File 'lib/http/connection.rb', line 181

def send_request_body(args)
  send_data(args[:body]) if args[:body]
end

#send_request_header(args) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/http/connection.rb', line 155

def send_request_header(args)
  query   = args[:query]
  head    = args[:head] ? munge_header_keys(args[:head]) : {}
  cookies = args[:cookies]
  body    = args[:body]
  path    = args[:request]
  
  path = "/#{path}" if path[0,1] != '/'
  
  # Set the Host header if it hasn't been specified already
  head['host'] ||= encode_host
  # Set the Content-Length if it hasn't been specified already and a body was given
  head['content-length'] ||= body ? body.length : 0
  # Set the User-Agent if it hasn't been specified
  head['user-agent'] ||= "EventedNet::HTTP::Connection"
  # Default to Connection: close
  head['connection'] ||= 'close'
  # Build the request
  request_header = encode_request(args[:method] || 'GET', path, query)
  request_header << encode_headers(head)
  request_header << encode_cookies(cookies) if cookies
  request_header << CRLF
  # Finally send it
  send_data(request_header)
end