Class: HTTPClient::Session
- Inherits:
-
Object
- Object
- HTTPClient::Session
- Includes:
- Util
- Defined in:
- lib/httpclient.rb,
lib/httpclient/session.rb
Overview
Manages a HTTP session with a Site.
Constant Summary collapse
- BadResponse =
::HTTPClient::BadResponseError
Instance Attribute Summary collapse
-
#connect_retry ⇒ Object
Returns the value of attribute connect_retry.
-
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
-
#debug_dev ⇒ Object
Device for dumping log for debugging.
-
#dest ⇒ Object
readonly
Destination site.
-
#last_used ⇒ Object
readonly
Returns the value of attribute last_used.
-
#protocol_retry_count ⇒ Object
Returns the value of attribute protocol_retry_count.
-
#proxy ⇒ Object
Proxy site.
-
#read_block_size ⇒ Object
Returns the value of attribute read_block_size.
-
#receive_timeout ⇒ Object
Returns the value of attribute receive_timeout.
-
#requested_version ⇒ Object
Requested protocol version.
-
#send_timeout ⇒ Object
Returns the value of attribute send_timeout.
-
#socket_local ⇒ Object
Returns the value of attribute socket_local.
-
#socket_sync ⇒ Object
Boolean value for Socket#sync.
-
#ssl_config ⇒ Object
Returns the value of attribute ssl_config.
-
#ssl_peer_cert ⇒ Object
readonly
Returns the value of attribute ssl_peer_cert.
-
#test_loopback_http_response ⇒ Object
Returns the value of attribute test_loopback_http_response.
-
#transparent_gzip_decompression ⇒ Object
Returns the value of attribute transparent_gzip_decompression.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #eof? ⇒ Boolean
- #get_body(&block) ⇒ Object
- #get_header ⇒ Object
-
#initialize(client, dest, agent_name, from) ⇒ Session
constructor
A new instance of Session.
- #invalidate ⇒ Object
- #invalidated? ⇒ Boolean
-
#query(req) ⇒ Object
Send a request to the server.
Methods included from Util
#argument_to_hash, get_buf, hash_find_value, #http?, #https?, #keyword_argument, uri_dirname, uri_part_of, urify
Constructor Details
#initialize(client, dest, agent_name, from) ⇒ Session
Returns a new instance of Session.
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
# File 'lib/httpclient/session.rb', line 578 def initialize(client, dest, agent_name, from) @client = client @dest = dest @invalidated = false @proxy = nil @socket_sync = true @requested_version = nil @debug_dev = nil @connect_timeout = nil @connect_retry = 1 @send_timeout = nil @receive_timeout = nil @read_block_size = nil @protocol_retry_count = 5 @ssl_config = nil @ssl_peer_cert = nil @test_loopback_http_response = nil @socket_local = Site::EMPTY @agent_name = agent_name @from = from @state = :INIT @requests = [] @status = nil @reason = nil @headers = [] @socket = nil @readbuf = nil @transparent_gzip_decompression = false @last_used = nil end |
Instance Attribute Details
#connect_retry ⇒ Object
Returns the value of attribute connect_retry.
563 564 565 |
# File 'lib/httpclient/session.rb', line 563 def connect_retry @connect_retry end |
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
562 563 564 |
# File 'lib/httpclient/session.rb', line 562 def connect_timeout @connect_timeout end |
#debug_dev ⇒ Object
Device for dumping log for debugging
560 561 562 |
# File 'lib/httpclient/session.rb', line 560 def debug_dev @debug_dev end |
#dest ⇒ Object (readonly)
Destination site
552 553 554 |
# File 'lib/httpclient/session.rb', line 552 def dest @dest end |
#last_used ⇒ Object (readonly)
Returns the value of attribute last_used.
576 577 578 |
# File 'lib/httpclient/session.rb', line 576 def last_used @last_used end |
#protocol_retry_count ⇒ Object
Returns the value of attribute protocol_retry_count.
567 568 569 |
# File 'lib/httpclient/session.rb', line 567 def protocol_retry_count @protocol_retry_count end |
#proxy ⇒ Object
Proxy site
554 555 556 |
# File 'lib/httpclient/session.rb', line 554 def proxy @proxy end |
#read_block_size ⇒ Object
Returns the value of attribute read_block_size.
566 567 568 |
# File 'lib/httpclient/session.rb', line 566 def read_block_size @read_block_size end |
#receive_timeout ⇒ Object
Returns the value of attribute receive_timeout.
565 566 567 |
# File 'lib/httpclient/session.rb', line 565 def receive_timeout @receive_timeout end |
#requested_version ⇒ Object
Requested protocol version
558 559 560 |
# File 'lib/httpclient/session.rb', line 558 def requested_version @requested_version end |
#send_timeout ⇒ Object
Returns the value of attribute send_timeout.
564 565 566 |
# File 'lib/httpclient/session.rb', line 564 def send_timeout @send_timeout end |
#socket_local ⇒ Object
Returns the value of attribute socket_local.
569 570 571 |
# File 'lib/httpclient/session.rb', line 569 def socket_local @socket_local end |
#socket_sync ⇒ Object
Boolean value for Socket#sync
556 557 558 |
# File 'lib/httpclient/session.rb', line 556 def socket_sync @socket_sync end |
#ssl_config ⇒ Object
Returns the value of attribute ssl_config.
571 572 573 |
# File 'lib/httpclient/session.rb', line 571 def ssl_config @ssl_config end |
#ssl_peer_cert ⇒ Object (readonly)
Returns the value of attribute ssl_peer_cert.
572 573 574 |
# File 'lib/httpclient/session.rb', line 572 def ssl_peer_cert @ssl_peer_cert end |
#test_loopback_http_response ⇒ Object
Returns the value of attribute test_loopback_http_response.
573 574 575 |
# File 'lib/httpclient/session.rb', line 573 def test_loopback_http_response @test_loopback_http_response end |
#transparent_gzip_decompression ⇒ Object
Returns the value of attribute transparent_gzip_decompression.
575 576 577 |
# File 'lib/httpclient/session.rb', line 575 def transparent_gzip_decompression @transparent_gzip_decompression end |
Instance Method Details
#close ⇒ Object
652 653 654 655 656 657 658 659 |
# File 'lib/httpclient/session.rb', line 652 def close if !@socket.nil? and !@socket.closed? # @socket.flush may block when it the socket is already closed by # foreign host and the client runs under MT-condition. @socket.close end @state = :INIT end |
#closed? ⇒ Boolean
661 662 663 |
# File 'lib/httpclient/session.rb', line 661 def closed? @state == :INIT end |
#eof? ⇒ Boolean
686 687 688 689 690 691 692 |
# File 'lib/httpclient/session.rb', line 686 def eof? if !@content_length.nil? @content_length == 0 else @socket.closed? or @socket.eof? end end |
#get_body(&block) ⇒ Object
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 |
# File 'lib/httpclient/session.rb', line 694 def get_body(&block) begin read_header if @state == :META return nil if @state != :DATA if @gzipped and @transparent_gzip_decompression # zlib itself has a functionality to decompress gzip stream. # - zlib 1.2.5 Manual # http://www.zlib.net/manual.html#Advanced # > windowBits can also be greater than 15 for optional gzip decoding. Add 32 to # > windowBits to enable zlib and gzip decoding with automatic header detection, # > or add 16 to decode only the gzip format inflate_stream = Zlib::Inflate.new(Zlib::MAX_WBITS + 32) original_block = block if @chunked buffer = '' block = Proc.new { |buf| buffer += buf } else block = Proc.new { |buf| original_block.call(inflate_stream.inflate(buf)) } end end if @chunked read_body_chunked(&block) if @gzipped and @transparent_gzip_decompression original_block.call(inflate_stream.inflate(buffer)) end elsif @content_length read_body_length(&block) else read_body_rest(&block) end rescue close raise end if eof? if @next_connection @state = :WAIT else close end end nil end |
#get_header ⇒ Object
673 674 675 676 677 678 679 680 681 682 683 684 |
# File 'lib/httpclient/session.rb', line 673 def get_header begin if @state != :META raise RuntimeError.new("get_status must be called at the beginning of a session") end read_header rescue close raise end [@version, @status, @reason, @headers] end |
#invalidate ⇒ Object
665 666 667 |
# File 'lib/httpclient/session.rb', line 665 def invalidate @invalidated = true end |
#invalidated? ⇒ Boolean
669 670 671 |
# File 'lib/httpclient/session.rb', line 669 def invalidated? @invalidated end |
#query(req) ⇒ Object
Send a request to the server
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 |
# File 'lib/httpclient/session.rb', line 619 def query(req) connect if @state == :INIT # Use absolute URI (not absolute path) iif via proxy AND not HTTPS. req.header.request_absolute_uri = !@proxy.nil? and !https?(@dest) begin timeout(@send_timeout, SendTimeoutError) do set_header(req) req.dump(@socket) # flush the IO stream as IO::sync mode is false @socket.flush unless @socket_sync end rescue Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EPIPE, IOError # JRuby can raise IOError instead of ECONNRESET for now close raise KeepAliveDisconnected.new(self) rescue HTTPClient::TimeoutError close raise rescue close if SSLEnabled and $!.is_a?(OpenSSL::SSL::SSLError) raise KeepAliveDisconnected.new(self) else raise end end @state = :META if @state == :WAIT @next_connection = nil @requests.push(req) @last_used = Time.now end |