Class: HTTPClient::Session
- Inherits:
-
Object
- Object
- HTTPClient::Session
- Defined in:
- lib/httpclient.rb,
lib/httpclient/session.rb
Overview
Manages a HTTP session with a Site.
Direct Known Subclasses
Defined Under Namespace
Classes: LenientInflater
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
- #connect_ssl_proxy(socket, uri) ⇒ Object
- #create_loopback_socket(host, port, str) ⇒ Object
- #create_socket(host, port) ⇒ Object
- #eof? ⇒ Boolean
- #get_body(&block) ⇒ Object
- #get_header ⇒ Object
-
#initialize(client, dest, agent_name, from) ⇒ Session
constructor
A new instance of Session.
-
#query(req) ⇒ Object
Send a request to the server.
Methods included from Util
#argument_to_hash, hash_find_value, #http?, #https?, #keyword_argument, try_require, uri_dirname, uri_part_of, urify
Methods included from Timeout
Constructor Details
#initialize(client, dest, agent_name, from) ⇒ Session
Returns a new instance of Session.
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/httpclient/session.rb', line 455 def initialize(client, dest, agent_name, from) @client = client @dest = dest @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.
440 441 442 |
# File 'lib/httpclient/session.rb', line 440 def connect_retry @connect_retry end |
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
439 440 441 |
# File 'lib/httpclient/session.rb', line 439 def connect_timeout @connect_timeout end |
#debug_dev ⇒ Object
Device for dumping log for debugging
437 438 439 |
# File 'lib/httpclient/session.rb', line 437 def debug_dev @debug_dev end |
#dest ⇒ Object (readonly)
Destination site
429 430 431 |
# File 'lib/httpclient/session.rb', line 429 def dest @dest end |
#last_used ⇒ Object (readonly)
Returns the value of attribute last_used.
453 454 455 |
# File 'lib/httpclient/session.rb', line 453 def last_used @last_used end |
#protocol_retry_count ⇒ Object
Returns the value of attribute protocol_retry_count.
444 445 446 |
# File 'lib/httpclient/session.rb', line 444 def protocol_retry_count @protocol_retry_count end |
#proxy ⇒ Object
Proxy site
431 432 433 |
# File 'lib/httpclient/session.rb', line 431 def proxy @proxy end |
#read_block_size ⇒ Object
Returns the value of attribute read_block_size.
443 444 445 |
# File 'lib/httpclient/session.rb', line 443 def read_block_size @read_block_size end |
#receive_timeout ⇒ Object
Returns the value of attribute receive_timeout.
442 443 444 |
# File 'lib/httpclient/session.rb', line 442 def receive_timeout @receive_timeout end |
#requested_version ⇒ Object
Requested protocol version
435 436 437 |
# File 'lib/httpclient/session.rb', line 435 def requested_version @requested_version end |
#send_timeout ⇒ Object
Returns the value of attribute send_timeout.
441 442 443 |
# File 'lib/httpclient/session.rb', line 441 def send_timeout @send_timeout end |
#socket_local ⇒ Object
Returns the value of attribute socket_local.
446 447 448 |
# File 'lib/httpclient/session.rb', line 446 def socket_local @socket_local end |
#socket_sync ⇒ Object
Boolean value for Socket#sync
433 434 435 |
# File 'lib/httpclient/session.rb', line 433 def socket_sync @socket_sync end |
#ssl_config ⇒ Object
Returns the value of attribute ssl_config.
448 449 450 |
# File 'lib/httpclient/session.rb', line 448 def ssl_config @ssl_config end |
#ssl_peer_cert ⇒ Object (readonly)
Returns the value of attribute ssl_peer_cert.
449 450 451 |
# File 'lib/httpclient/session.rb', line 449 def ssl_peer_cert @ssl_peer_cert end |
#test_loopback_http_response ⇒ Object
Returns the value of attribute test_loopback_http_response.
450 451 452 |
# File 'lib/httpclient/session.rb', line 450 def test_loopback_http_response @test_loopback_http_response end |
#transparent_gzip_decompression ⇒ Object
Returns the value of attribute transparent_gzip_decompression.
452 453 454 |
# File 'lib/httpclient/session.rb', line 452 def transparent_gzip_decompression @transparent_gzip_decompression end |
Instance Method Details
#close ⇒ Object
528 529 530 531 532 533 534 535 |
# File 'lib/httpclient/session.rb', line 528 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
537 538 539 |
# File 'lib/httpclient/session.rb', line 537 def closed? @state == :INIT end |
#connect_ssl_proxy(socket, uri) ⇒ Object
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
# File 'lib/httpclient/session.rb', line 630 def connect_ssl_proxy(socket, uri) req = HTTP::Message.new_connect_request(uri) @client.request_filter.each do |filter| filter.filter_request(req) end set_header(req) req.dump(socket) socket.flush unless @socket_sync res = HTTP::Message.new_response('') parse_header(socket) res.http_version, res.status, res.reason = @version, @status, @reason @headers.each do |key, value| res.header.set(key.to_s, value) end commands = @client.request_filter.collect { |filter| filter.filter_response(req, res) } if commands.find { |command| command == :retry } raise RetryableResponse.new(res) end unless @status == 200 raise BadResponseError.new("connect to ssl proxy failed with status #{@status} #{@reason}", res) end end |
#create_loopback_socket(host, port, str) ⇒ Object
616 617 618 619 620 621 622 623 624 625 626 627 628 |
# File 'lib/httpclient/session.rb', line 616 def create_loopback_socket(host, port, str) @debug_dev << "! CONNECT TO #{host}:#{port}\n" if @debug_dev socket = LoopBackSocket.new(host, port, str) if @debug_dev @debug_dev << "! CONNECTION ESTABLISHED\n" socket.extend(DebugSocket) socket.debug_dev = @debug_dev end if https?(@dest) && @proxy connect_ssl_proxy(socket, Util.urify(@dest.to_s)) end socket end |
#create_socket(host, port) ⇒ Object
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 |
# File 'lib/httpclient/session.rb', line 590 def create_socket(host, port) socket = nil begin @debug_dev << "! CONNECT TO #{host}:#{port}\n" if @debug_dev clean_host = host.delete("[]") if @socket_local == Site::EMPTY socket = TCPSocket.new(clean_host, port) else clean_local = @socket_local.host.delete("[]") socket = TCPSocket.new(clean_host, port, clean_local, @socket_local.port) end if @debug_dev @debug_dev << "! CONNECTION ESTABLISHED\n" socket.extend(DebugSocket) socket.debug_dev = @debug_dev end rescue SystemCallError => e e. << " (#{host}:#{port})" raise rescue SocketError => e e. << " (#{host}:#{port})" raise end socket end |
#eof? ⇒ Boolean
554 555 556 557 558 559 560 |
# File 'lib/httpclient/session.rb', line 554 def eof? if !@content_length.nil? @content_length == 0 else @socket.closed? or @socket.eof? end end |
#get_body(&block) ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
# File 'lib/httpclient/session.rb', line 562 def get_body(&block) begin read_header if @state == :META return nil if @state != :DATA if @transparent_gzip_decompression block = content_inflater_block(@content_encoding, block) end if @chunked read_body_chunked(&block) 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
541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/httpclient/session.rb', line 541 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 |
#query(req) ⇒ Object
Send a request to the server
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/httpclient/session.rb', line 495 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? && !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 => e close if SSLEnabled and e.is_a?(OpenSSL::SSL::SSLError) raise KeepAliveDisconnected.new(self, e) else raise end end @state = :META if @state == :WAIT @next_connection = nil @requests.push(req) @last_used = Time.now end |