Class: EventMachine::HttpClient
- Inherits:
-
Connection
- Object
- Connection
- EventMachine::HttpClient
- Includes:
- Deferrable, HttpEncoding
- Defined in:
- lib/em-http/client.rb
Constant Summary collapse
- TRANSFER_ENCODING =
"TRANSFER_ENCODING"- CONTENT_ENCODING =
"CONTENT_ENCODING"- CONTENT_LENGTH =
"CONTENT_LENGTH"- KEEP_ALIVE =
"CONNECTION"- SET_COOKIE =
"SET_COOKIE"- LOCATION =
"LOCATION"- HOST =
"HOST"- CRLF =
"\r\n"
Constants included from HttpEncoding
EventMachine::HttpEncoding::BASIC_AUTH_ENCODING, EventMachine::HttpEncoding::FIELD_ENCODING, EventMachine::HttpEncoding::HTTP_REQUEST_HEADER
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#method ⇒ Object
Returns the value of attribute method.
-
#options ⇒ Object
Returns the value of attribute options.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#response_header ⇒ Object
readonly
Returns the value of attribute response_header.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#connection_completed ⇒ Object
start HTTP request once we establish connection to host.
-
#dispatch ⇒ Object
Response processing.
-
#on_body_data(data) ⇒ Object
Called when part of the body has been read.
-
#on_error(msg) ⇒ Object
request failed, invoke errback.
-
#on_request_complete ⇒ Object
request is done, invoke the callback.
- #parse_chunk_header ⇒ Object
- #parse_header(header) ⇒ Object
- #parse_response_header ⇒ Object
- #post_init ⇒ Object
- #process_body ⇒ Object
- #process_chunk_body ⇒ Object
- #process_chunk_footer ⇒ Object
- #process_response_footer ⇒ Object
- #receive_data(data) ⇒ Object
- #send_request_body ⇒ Object
- #send_request_header ⇒ Object
- #unbind ⇒ Object
Methods included from HttpEncoding
#encode_basic_auth, #encode_cookies, #encode_field, #encode_headers, #encode_host, #encode_param, #encode_query, #encode_request, #escape, #munge_header_keys, #unescape
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
151 152 153 |
# File 'lib/em-http/client.rb', line 151 def errors @errors end |
#method ⇒ Object
Returns the value of attribute method.
150 151 152 |
# File 'lib/em-http/client.rb', line 150 def method @method end |
#options ⇒ Object
Returns the value of attribute options.
150 151 152 |
# File 'lib/em-http/client.rb', line 150 def end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
151 152 153 |
# File 'lib/em-http/client.rb', line 151 def response @response end |
#response_header ⇒ Object (readonly)
Returns the value of attribute response_header.
151 152 153 |
# File 'lib/em-http/client.rb', line 151 def response_header @response_header end |
#uri ⇒ Object
Returns the value of attribute uri.
150 151 152 |
# File 'lib/em-http/client.rb', line 150 def uri @uri end |
Instance Method Details
#connection_completed ⇒ Object
start HTTP request once we establish connection to host
169 170 171 172 |
# File 'lib/em-http/client.rb', line 169 def connection_completed send_request_header send_request_body end |
#dispatch ⇒ Object
Response processing
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/em-http/client.rb', line 245 def dispatch while case @state when :response_header parse_response_header when :chunk_header parse_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
232 233 234 |
# File 'lib/em-http/client.rb', line 232 def on_body_data(data) @response << data end |
#on_error(msg) ⇒ Object
request failed, invoke errback
190 191 192 193 |
# File 'lib/em-http/client.rb', line 190 def on_error(msg) @errors = msg unbind end |
#on_request_complete ⇒ Object
request is done, invoke the callback
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/em-http/client.rb', line 175 def on_request_complete if @response_header.compressed? and @inflate.include?(response_header[CONTENT_ENCODING]) case response_header[CONTENT_ENCODING] when 'deflate' then @response = Zlib::Inflate.inflate(@response) when 'gzip', 'compressed' then @response = Zlib::GzipReader.new(StringIO.new(@response)).read end end unbind end |
#parse_chunk_header ⇒ Object
305 306 307 308 309 310 311 312 313 |
# File 'lib/em-http/client.rb', line 305 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
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/em-http/client.rb', line 266 def parse_header(header) return false if @data.empty? begin @parser_nbytes = @parser.execute(header, @data.to_str, @parser_nbytes) rescue EventMachine::HttpClientParserError @state = :invalid on_error "invalid HTTP format, parsing fails" 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_header ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/em-http/client.rb', line 286 def parse_response_header return false unless parse_header(@response_header) unless @response_header.http_status and @response_header.http_reason @state = :invalid on_error "no HTTP response" return false end if @response_header.chunked_encoding? @state = :chunk_header else @state = :body @bytes_remaining = @response_header.content_length end true end |
#post_init ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/em-http/client.rb', line 153 def post_init self.comm_inactivity_timeout = 5 @parser = HttpClientParser.new @data = EventMachine::Buffer.new @response_header = HttpResponseHeader.new @chunk_header = HttpChunkHeader.new @state = :response_header @parser_nbytes = 0 @inflate = [] @response = '' @errors = '' end |
#process_body ⇒ Object
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/em-http/client.rb', line 361 def process_body if @bytes_remaining.nil? on_body_data @data.read return false end if @bytes_remaining.zero? @state = :finished on_request_complete 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 Keep-Alive is enabled, the server may be pushing more data to us # after the first request is complete. Hence, finish first request, and # reset state. if @response_header.keep_alive? @data.clear # hard reset, TODO: add support for keep-alive connections! @state = :finished on_request_complete else if @data.empty? @state = :finished on_request_complete else @state = :invalid on_error "garbage at end of body" end end false end |
#process_chunk_body ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/em-http/client.rb', line 315 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 |
#process_chunk_footer ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/em-http/client.rb', line 329 def return false if @data.size < 2 if @data.read(2) == CRLF @state = :chunk_header else @state = :invalid on_error "non-CRLF chunk footer" end true end |
#process_response_footer ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/em-http/client.rb', line 342 def return false if @data.size < 2 if @data.read(2) == CRLF if @data.empty? @state = :finished on_request_complete else @state = :invalid on_error "garbage at end of chunked response" end else @state = :invalid on_error "non-CRLF response footer" end false end |
#receive_data(data) ⇒ Object
226 227 228 229 |
# File 'lib/em-http/client.rb', line 226 def receive_data(data) @data << data dispatch end |
#send_request_body ⇒ Object
222 223 224 |
# File 'lib/em-http/client.rb', line 222 def send_request_body send_data [:body] if [:body] end |
#send_request_header ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/em-http/client.rb', line 195 def send_request_header query = [:query] head = [:head] ? munge_header_keys([:head]) : {} body = [:body] # Set the Host header if it hasn't been specified already head['host'] ||= encode_host # Set the Content-Length if body is given head['content-length'] = body.length if body # Set the User-Agent if it hasn't been specified head['user-agent'] ||= "EventMachine HttpClient" # Set auto-inflate flags if head['accept-encoding'] @inflate = head['accept-encoding'].split(',').map {|t| t.strip} end # Build the request request_header = encode_request(@method, @uri.path, query) request_header << encode_headers(head) request_header << CRLF send_data request_header end |
#unbind ⇒ Object
236 237 238 239 |
# File 'lib/em-http/client.rb', line 236 def unbind (@state == :finished) ? succeed : fail close_connection end |