Class: EventedNet::HTTP::Connection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- EventedNet::HTTP::Connection
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 =
"SET_COOKIE"
- LOCATION =
"LOCATION"
- HOST =
"HOST"
- CRLF =
"\r\n"
HttpEncoding::FIELD_ENCODING, HttpEncoding::HTTP_REQUEST_HEADER
Class Method Summary
collapse
Instance Method Summary
collapse
#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
EventMachine.connect(args[:host], args[:port], self) do |c|
c.instance_eval { @args = args }
end
end
|
Instance Method Details
#connection_completed ⇒ Object
145
146
147
148
|
# File 'lib/http/connection.rb', line 145
def connection_completed
@connected = true
send_request(@args)
end
|
#dispatch ⇒ Object
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
when :chunk_header
when :chunk_body
process_chunk_body
when :chunk_footer
when :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
212
213
214
215
|
# File 'lib/http/connection.rb', line 212
def on_error(reason)
close_connection
raise RuntimeError, reason
end
|
#on_request_complete ⇒ Object
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
@state, @connected = :response_header, false
set_deferred_status :succeeded, {
:content => @response_body,
:headers => @response_header,
:status => @response_header.status
}
close_connection
end
|
Called when response header has been received
191
192
|
# File 'lib/http/connection.rb', line 191
def ()
end
|
279
280
281
282
283
284
285
286
287
|
# File 'lib/http/connection.rb', line 279
def
return false unless (@chunk_header)
@bytes_remaining = @chunk_header.chunk_size
@chunk_header = HttpChunkHeader.new
@state = @bytes_remaining > 0 ? :chunk_body : :response_footer
true
end
|
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 ()
return false if @data.empty?
begin
@parser_nbytes = @parser.execute(, @data.to_str, @parser_nbytes)
rescue Rev::HttpClientParserError
on_error "Invalid HTTP format, parsing fails"
@state = :invalid
end
return false unless @parser.finished?
@data.read(@parser_nbytes)
@parser.reset
@parser_nbytes = 0
true
end
|
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
return false unless (@response_header)
unless @response_header.http_status and @response_header.http_reason
on_error "No HTTP response"
@state = :invalid
return false
end
(@response_header)
if @response_header.chunked_encoding?
@state = :chunk_header
else
@state = :body
@bytes_remaining = @response_header.content_length
end
true
end
|
#process_body ⇒ Object
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_body ⇒ Object
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
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
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_host ⇒ Object
127
128
129
|
# File 'lib/http/connection.rb', line 127
def remote_host
@args[:host]
end
|
#remote_port ⇒ Object
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)
(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
|
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 (args)
query = args[:query]
head = args[:head] ? (args[:head]) : {}
cookies = args[:cookies]
body = args[:body]
path = args[:request]
path = "/#{path}" if path[0,1] != '/'
head['host'] ||= encode_host
head['content-length'] ||= body ? body.length : 0
head['user-agent'] ||= "EventedNet::HTTP::Connection"
head['connection'] ||= 'close'
= encode_request(args[:method] || 'GET', path, query)
<< (head)
<< encode_cookies(cookies) if cookies
<< CRLF
send_data()
end
|