Class: HTTPClient::Session

Inherits:
Object
  • Object
show all
Includes:
Timeout
Defined in:
lib/httpclient.rb,
lib/httpclient/session.rb

Overview

Manages a HTTP session with a Site.

Direct Known Subclasses

HTTPAccess2::Session

Constant Summary collapse

BadResponse =
::HTTPClient::BadResponseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Timeout

#timeout

Constructor Details

#initialize(client, dest, agent_name, from) ⇒ Session

Returns a new instance of Session.



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/httpclient/session.rb', line 485

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

  @agent_name = agent_name
  @from = from
  @state = :INIT

  @requests = []

  @status = nil
  @reason = nil
  @headers = []

  @socket = nil
  @readbuf = nil
end

Instance Attribute Details

#connect_retryObject

Returns the value of attribute connect_retry.



475
476
477
# File 'lib/httpclient/session.rb', line 475

def connect_retry
  @connect_retry
end

#connect_timeoutObject

Returns the value of attribute connect_timeout.



474
475
476
# File 'lib/httpclient/session.rb', line 474

def connect_timeout
  @connect_timeout
end

#debug_devObject

Device for dumping log for debugging



472
473
474
# File 'lib/httpclient/session.rb', line 472

def debug_dev
  @debug_dev
end

#destObject (readonly)

Destination site



464
465
466
# File 'lib/httpclient/session.rb', line 464

def dest
  @dest
end

#protocol_retry_countObject

Returns the value of attribute protocol_retry_count.



479
480
481
# File 'lib/httpclient/session.rb', line 479

def protocol_retry_count
  @protocol_retry_count
end

#proxyObject

Proxy site



466
467
468
# File 'lib/httpclient/session.rb', line 466

def proxy
  @proxy
end

#read_block_sizeObject

Returns the value of attribute read_block_size.



478
479
480
# File 'lib/httpclient/session.rb', line 478

def read_block_size
  @read_block_size
end

#receive_timeoutObject

Returns the value of attribute receive_timeout.



477
478
479
# File 'lib/httpclient/session.rb', line 477

def receive_timeout
  @receive_timeout
end

#requested_versionObject

Requested protocol version



470
471
472
# File 'lib/httpclient/session.rb', line 470

def requested_version
  @requested_version
end

#send_timeoutObject

Returns the value of attribute send_timeout.



476
477
478
# File 'lib/httpclient/session.rb', line 476

def send_timeout
  @send_timeout
end

#socket_syncObject

Boolean value for Socket#sync



468
469
470
# File 'lib/httpclient/session.rb', line 468

def socket_sync
  @socket_sync
end

#ssl_configObject

Returns the value of attribute ssl_config.



481
482
483
# File 'lib/httpclient/session.rb', line 481

def ssl_config
  @ssl_config
end

#ssl_peer_certObject (readonly)

Returns the value of attribute ssl_peer_cert.



482
483
484
# File 'lib/httpclient/session.rb', line 482

def ssl_peer_cert
  @ssl_peer_cert
end

#test_loopback_http_responseObject

Returns the value of attribute test_loopback_http_response.



483
484
485
# File 'lib/httpclient/session.rb', line 483

def test_loopback_http_response
  @test_loopback_http_response
end

Instance Method Details

#closeObject



550
551
552
553
554
555
556
557
# File 'lib/httpclient/session.rb', line 550

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

Returns:

  • (Boolean)


559
560
561
# File 'lib/httpclient/session.rb', line 559

def closed?
  @state == :INIT
end

#eof?Boolean

Returns:

  • (Boolean)


576
577
578
579
580
581
582
# File 'lib/httpclient/session.rb', line 576

def eof?
  if !@content_length.nil?
    @content_length == 0
  else
    @socket.closed? or @socket.eof?
  end
end

#get_body(&block) ⇒ Object



584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/httpclient/session.rb', line 584

def get_body(&block)
  begin
    read_header if @state == :META
    return nil if @state != :DATA
    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_headerObject



563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/httpclient/session.rb', line 563

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



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/httpclient/session.rb', line 521

def query(req)
  connect if @state == :INIT
  req.header.request_via_proxy = !@proxy.nil?
  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
    close
    raise KeepAliveDisconnected.new
  rescue HTTPClient::TimeoutError
    close
    raise
  rescue
    if SSLEnabled and $!.is_a?(OpenSSL::SSL::SSLError)
      raise KeepAliveDisconnected.new
    else
      raise
    end
  end

  @state = :META if @state == :WAIT
  @next_connection = nil
  @requests.push(req)
end