Class: HTTPX::Connection::HTTP1

Inherits:
Object
  • Object
show all
Includes:
HTTPX::Callbacks, Loggable
Defined in:
lib/httpx/connection/http1.rb

Constant Summary collapse

MAX_REQUESTS =
200
CRLF =
"\r\n"
UPCASED =
{
  "www-authenticate" => "WWW-Authenticate",
  "http2-settings" => "HTTP2-Settings",
  "content-md5" => "Content-MD5",
  "last-event-id" => "Last-Event-ID",
}.freeze

Constants included from Loggable

Loggable::COLORS, Loggable::USE_DEBUG_LOG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log, #log_exception, #log_redact, #log_redact_body, #log_redact_headers

Methods included from HTTPX::Callbacks

#callbacks_for?, #emit, #on, #once

Constructor Details

#initialize(buffer, options) ⇒ HTTP1

Returns a new instance of HTTP1.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/httpx/connection/http1.rb', line 23

def initialize(buffer, options)
  @options = options
  @max_concurrent_requests = @options.max_concurrent_requests || MAX_REQUESTS
  @max_requests = @options.max_requests
  @parser = Parser::HTTP1.new(self, options.max_response_headers, options.max_response_header_value_size)
  @buffer = buffer
  @version = [1, 1]
  @pending = []
  @requests = []
  @request = nil
  @handshake_completed = @pipelining = false
end

Instance Attribute Details

#max_concurrent_requestsObject

Returns the value of attribute max_concurrent_requests.



21
22
23
# File 'lib/httpx/connection/http1.rb', line 21

def max_concurrent_requests
  @max_concurrent_requests
end

#pendingObject (readonly)

Returns the value of attribute pending.



19
20
21
# File 'lib/httpx/connection/http1.rb', line 19

def pending
  @pending
end

#requestsObject (readonly)

Returns the value of attribute requests.



19
20
21
# File 'lib/httpx/connection/http1.rb', line 19

def requests
  @requests
end

Instance Method Details

#<<(data) ⇒ Object



91
92
93
# File 'lib/httpx/connection/http1.rb', line 91

def <<(data)
  @parser << data
end

#closeObject



71
72
73
74
# File 'lib/httpx/connection/http1.rb', line 71

def close
  reset
  emit(:close)
end

#consumeObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/httpx/connection/http1.rb', line 107

def consume
  requests_limit = [@max_requests, @requests.size].min
  concurrent_requests_limit = [@max_concurrent_requests, requests_limit].min
  @requests.each_with_index do |request, idx|
    break if idx >= concurrent_requests_limit
    next unless request.can_buffer?

    handle(request)
  end
end

#dispatch(request) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/httpx/connection/http1.rb', line 188

def dispatch(request)
  if request.expects?
    @parser.reset!
    return handle(request)
  end

  @request = nil
  @requests.shift
  response = request.response
  emit(:response, request, response)

  if @parser.upgrade?
    response << @parser.upgrade_data
    @parser.reset!
    throw(:called)
  end

  @parser.reset!
  @max_requests -= 1
  if response.is_a?(ErrorResponse)
    disable
  else
    manage_connection(request, response)
  end

  if exhausted?
    @pending.unshift(*@requests)
    @requests.clear

    emit(:exhausted)
  else
    send(@pending.shift) unless @pending.empty?
  end
end

#empty?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
# File 'lib/httpx/connection/http1.rb', line 80

def empty?
  # this means that for every request there's an available
  # partial response, so there are no in-flight requests waiting.
  @requests.empty? || (
    # checking all responses can be time-consuming. Alas, as in HTTP/1, responses
    # do not come out of order, we can get away with checking first and last.
    !@requests.first.response.nil? &&
    (@requests.size == 1 || !@requests.last.response.nil?)
  )
end

#exhausted?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/httpx/connection/http1.rb', line 76

def exhausted?
  !@max_requests.positive?
end

#handle_error(ex, request = nil) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/httpx/connection/http1.rb', line 223

def handle_error(ex, request = nil)
  if (ex.is_a?(EOFError) || ex.is_a?(TimeoutError)) && @request &&
     (response = @request.response) && response.is_a?(Response) &&
     !response.headers.key?("content-length") &&
     !response.headers.key?("transfer-encoding")
    # if the response does not contain a content-length header, the server closing the
    # connnection is the indicator of response consumed.
    # https://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.4.4
    catch(:called) { on_complete }
    return
  end

  if @pipelining
    catch(:called) { disable }
  else
    while (req = @requests.shift)
      next if request && request == req

      emit(:error, req, ex)
    end
    while (req = @pending.shift)
      next if request && request == req

      emit(:error, req, ex)
    end
  end
end

#interestsObject



40
41
42
43
44
45
46
47
48
# File 'lib/httpx/connection/http1.rb', line 40

def interests
  request = @request || @requests.first

  return unless request

  return :w if request.interests == :w || !@buffer.empty?

  :r
end

#on_completeObject



179
180
181
182
183
184
185
186
# File 'lib/httpx/connection/http1.rb', line 179

def on_complete
  request = @request

  return unless request

  request.log(level: 2) { "parsing complete" }
  dispatch(request)
end

#on_data(chunk) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/httpx/connection/http1.rb', line 160

def on_data(chunk)
  request = @request

  return unless request

  begin
    request.log(color: :green) { "-> DATA: #{chunk.bytesize} bytes..." }
    request.log(level: 2, color: :green) { "-> #{log_redact_body(chunk.inspect)}" }

    response = request.response

    response << chunk
  rescue StandardError => e
    error_response = ErrorResponse.new(request, e)
    request.response = error_response
    dispatch(request)
  end
end

#on_headers(h) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/httpx/connection/http1.rb', line 126

def on_headers(h)
  request = @request = @requests.first

  return if request.response

  request.log(level: 2) { "headers received" }
  headers = request.options.headers_class.new(h)
  response = request.options.response_class.new(request,
                                                @parser.status_code,
                                                @parser.http_version.join("."),
                                                headers)
  request.log(color: :yellow) { "-> HEADLINE: #{response.status} HTTP/#{@parser.http_version.join(".")}" }
  request.log(color: :yellow) { response.headers.each.map { |f, v| "-> HEADER: #{f}: #{log_redact_headers(v)}" }.join("\n") }

  if response.content_length && response.content_length > request.options.max_response_body_size
    raise HTTPX::Error, "maximum response body size exceeded"
  end

  request.response = response
  on_complete if response.finished?
end

#on_startObject

HTTP Parser callbacks

must be public methods, or else they won’t be reachable



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

def on_start
  log(level: 2) { "parsing begins" }
end

#on_trailers(h) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/httpx/connection/http1.rb', line 148

def on_trailers(h)
  request = @request

  return unless request

  response = request.response

  request.log(level: 2) { "trailer headers received" }
  request.log(color: :yellow) { h.each.map { |f, v| "-> HEADER: #{f}: #{log_redact_headers(v.join(", "))}" }.join("\n") }
  response.merge_headers(h)
end

#pingObject



251
252
253
254
255
# File 'lib/httpx/connection/http1.rb', line 251

def ping
  reset
  emit(:reset)
  emit(:exhausted)
end

#resetObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/httpx/connection/http1.rb', line 50

def reset
  if @ping_timer
    @ping_timer.cancel
    @ping_timer = nil
  end
  @max_requests = @options.max_requests || MAX_REQUESTS
  @parser.reset!
  @handshake_completed = false
  reset_requests
end

#reset_requestsObject



61
62
63
64
65
66
67
68
69
# File 'lib/httpx/connection/http1.rb', line 61

def reset_requests
  @requests.reverse_each do |request|
    next if request.response

    request.transition(:idle)
    @pending.unshift(request)
  end
  @requests.clear
end

#send(request) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/httpx/connection/http1.rb', line 95

def send(request)
  unless @max_requests.positive?
    @pending << request
    return
  end

  return if @requests.include?(request)

  @requests << request
  @pipelining = @max_concurrent_requests > 1 && @requests.size > 1
end

#timeoutObject



36
37
38
# File 'lib/httpx/connection/http1.rb', line 36

def timeout
  @options.timeout[:operation_timeout]
end

#waiting_for_ping?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/httpx/connection/http1.rb', line 257

def waiting_for_ping?
  false
end