#— Copyright (C)2008 Ilya Grigorik
Includes portion originally Copyright (C)2007 Tony Arcieri Includes portion originally Copyright (C)2005 Zed Shaw You can redistribute this under the terms of the Ruby license See file LICENSE for details #—
# File lib/em-http/client.rb, line 527 527: def parse_chunk_header 528: return false unless parse_header(@chunk_header) 529: 530: @bytes_remaining = @chunk_header.chunk_size 531: @chunk_header = HttpChunkHeader.new 532: 533: @state = @bytes_remaining > 0 ? :chunk_body : :response_footer 534: true 535: end
# File lib/em-http/client.rb, line 431 431: def parse_header(header) 432: return false if @data.empty? 433: 434: begin 435: @parser_nbytes = @parser.execute(header, @data.to_str, @parser_nbytes) 436: rescue EventMachine::HttpClientParserError 437: @state = :invalid 438: on_error "invalid HTTP format, parsing fails" 439: end 440: 441: return false unless @parser.finished? 442: 443: # Clear parsed data from the buffer 444: @data.read(@parser_nbytes) 445: @parser.reset 446: @parser_nbytes = 0 447: 448: true 449: end
# File lib/em-http/client.rb, line 451 451: def parse_response_header 452: return false unless parse_header(@response_header) 453: 454: unless @response_header.http_status and @response_header.http_reason 455: @state = :invalid 456: on_error "no HTTP response" 457: return false 458: end 459: 460: if @state == :response_proxy 461: # when a successfull tunnel is established, the proxy responds with a 462: # 200 response code. from here, the tunnel is transparent. 463: if @response_header.http_status.to_i == 200 464: @response_header = HttpResponseHeader.new 465: connection_completed 466: return true 467: else 468: @state = :invalid 469: on_error "proxy not accessible" 470: return false 471: end 472: end 473: 474: # correct location header - some servers will incorrectly give a relative URI 475: if @response_header.location 476: begin 477: location = Addressable::URI.parse(@response_header.location) 478: if location.relative? 479: location = @uri.join(location) 480: @response_header[LOCATION] = location.to_s 481: end 482: 483: # store last url on any sign of redirect 484: @last_effective_url = location 485: 486: rescue 487: on_error "Location header format error" 488: return false 489: end 490: end 491: 492: # shortcircuit on HEAD requests 493: if @method == "HEAD" 494: @state = :finished 495: unbind 496: end 497: 498: if websocket? 499: if @response_header.status == 101 500: @state = :websocket 501: succeed 502: else 503: fail "websocket handshake failed" 504: end 505: 506: elsif @response_header.chunked_encoding? 507: @state = :chunk_header 508: elsif @response_header.content_length 509: @state = :body 510: @bytes_remaining = @response_header.content_length 511: else 512: @state = :body 513: @bytes_remaining = nil 514: end 515: 516: if decoder_class = HttpDecoders.decoder_for_encoding(response_header[CONTENT_ENCODING]) 517: begin 518: @content_decoder = decoder_class.new do |s| on_decoded_body_data(s) end 519: rescue HttpDecoders::DecoderError 520: on_error "Content-decoder error" 521: end 522: end 523: 524: true 525: end
# File lib/em-http/client.rb, line 583 583: def process_body 584: if @bytes_remaining.nil? 585: on_body_data @data.read 586: return false 587: end 588: 589: if @bytes_remaining.zero? 590: @state = :finished 591: on_request_complete 592: return false 593: end 594: 595: if @data.size < @bytes_remaining 596: @bytes_remaining -= @data.size 597: on_body_data @data.read 598: return false 599: end 600: 601: on_body_data @data.read(@bytes_remaining) 602: @bytes_remaining = 0 603: 604: # If Keep-Alive is enabled, the server may be pushing more data to us 605: # after the first request is complete. Hence, finish first request, and 606: # reset state. 607: if @response_header.keep_alive? 608: @data.clear # hard reset, TODO: add support for keep-alive connections! 609: @state = :finished 610: on_request_complete 611: 612: else 613: if @data.empty? 614: @state = :finished 615: on_request_complete 616: else 617: @state = :invalid 618: on_error "garbage at end of body" 619: end 620: end 621: 622: false 623: end
# File lib/em-http/client.rb, line 537 537: def process_chunk_body 538: if @data.size < @bytes_remaining 539: @bytes_remaining -= @data.size 540: on_body_data @data.read 541: return false 542: end 543: 544: on_body_data @data.read(@bytes_remaining) 545: @bytes_remaining = 0 546: 547: @state = :chunk_footer 548: true 549: end
# File lib/em-http/client.rb, line 625 625: def process_websocket 626: return false if @data.empty? 627: 628: # slice the message out of the buffer and pass in 629: # for processing, and buffer data otherwise 630: buffer = @data.read 631: while msg = buffer.slice!(/\0000([^\3377]*)\3377/) 632: msg.gsub!(/^\x00|\xff$/, '') 633: @stream.call(msg) 634: end 635: 636: # store remainder if message boundary has not yet 637: # been received 638: @data << buffer if not buffer.empty? 639: 640: false 641: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.