Class: HTTP::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/http/connection.rb

Overview

A connection to the HTTP server

Constant Summary collapse

BUFFER_SIZE =

Attempt to read this much data

16_384
HTTP_1_0 =

HTTP/1.0

"1.0".freeze
HTTP_1_1 =

HTTP/1.1

"1.1".freeze

Instance Method Summary collapse

Constructor Details

#initialize(req, options) ⇒ Connection

Returns a new instance of Connection.

Parameters:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/http/connection.rb', line 20

def initialize(req, options)
  @persistent         = options.persistent?
  @keep_alive_timeout = options[:keep_alive_timeout].to_f
  @pending_request    = false
  @pending_response   = false

  @parser = Response::Parser.new

  @socket = options[:timeout_class].new(options[:timeout_options])
  @socket.connect(options[:socket_class], req.socket_host, req.socket_port)

  start_tls(req, options)
  reset_timer
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close the connection



107
108
109
110
111
112
# File 'lib/http/connection.rb', line 107

def close
  @socket.close unless @socket.closed?

  @pending_response = false
  @pending_request  = false
end

#expired?Boolean

Whether our connection has expired

Returns:

  • (Boolean)


122
123
124
# File 'lib/http/connection.rb', line 122

def expired?
  !@conn_expires_at || @conn_expires_at < Time.now
end

#finish_responsevoid

This method returns an undefined value.

Callback for when we've reached the end of a response



96
97
98
99
100
101
102
103
# File 'lib/http/connection.rb', line 96

def finish_response
  close unless keep_alive?

  @parser.reset
  reset_timer

  @pending_response = false
end

#keep_alive?Boolean

Whether we're keeping the conn alive

Returns:

  • (Boolean)


116
117
118
# File 'lib/http/connection.rb', line 116

def keep_alive?
  !!@keep_alive && !@socket.closed?
end

#read_headers!void

This method returns an undefined value.

Reads data from socket up until headers are loaded



86
87
88
89
90
91
92
# File 'lib/http/connection.rb', line 86

def read_headers!
  read_more BUFFER_SIZE until @parser.headers
  set_keep_alive
rescue IOError, Errno::ECONNRESET, Errno::EPIPE => e
  return if e.is_a?(EOFError) && @parser.headers
  raise IOError, "problem making HTTP request: #{e}"
end

#readpartial(size = BUFFER_SIZE) ⇒ String?

Read a chunk of the body

Returns:

  • (String)

    data chunk

  • (nil)

    when no more data left



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/http/connection.rb', line 67

def readpartial(size = BUFFER_SIZE)
  return unless @pending_response

  begin
    read_more size
    finished = @parser.finished?
  rescue EOFError
    finished = true
  end

  chunk = @parser.chunk

  finish_response if finished

  chunk.to_s
end

#send_request(req) ⇒ nil

Send a request to the server

Parameters:

  • Request (Request)

    to send to the server

Returns:

  • (nil)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/http/connection.rb', line 48

def send_request(req)
  if @pending_response
    fail StateError, "Tried to send a request while one is pending already. Make sure you read off the body."
  elsif @pending_request
    fail StateError, "Tried to send a request while a response is pending. Make sure you've fully read the body from the request."
  end

  @pending_request = true

  req.stream @socket

  @pending_response = true
  @pending_request  = false
end