Class: HTTPX::Connection::HTTP1
- Inherits:
-
Object
- Object
- HTTPX::Connection::HTTP1
- Includes:
- HTTPX::Callbacks, Loggable
- Defined in:
- lib/httpx/connection/http1.rb,
sig/connection/http1.rbs
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
-
#max_concurrent_requests ⇒ Integer
Returns the value of attribute max_concurrent_requests.
-
#pending ⇒ Array[Request]
readonly
Returns the value of attribute pending.
-
#requests ⇒ Array[Request]
readonly
Returns the value of attribute requests.
Instance Method Summary collapse
- #<<(data) ⇒ void
- #close ⇒ void
- #consume ⇒ void
- #dispatch(request) ⇒ void
- #empty? ⇒ Boolean
- #exhausted? ⇒ Boolean
- #handle_error(ex, request = nil) ⇒ void
- #interests ⇒ io_interests?
- #no_more_requests? ⇒ Boolean
- #on_complete ⇒ void
- #on_data(chunk) ⇒ void
- #on_headers(h) ⇒ void
-
#on_start ⇒ void
HTTP Parser callbacks.
- #on_trailers(h) ⇒ void
- #ping ⇒ void
- #reset ⇒ void
-
#reset_requests ⇒ void
move incompleted requests back to pending queue.
-
#send(request, head = false) ⇒ Boolean
rubocop:disable Naming/PredicateMethod.
- #timeout ⇒ interval?
- #waiting_for_ping? ⇒ Boolean
Methods included from Loggable
#log, #log_exception, log_identifiers
Methods included from HTTPX::Callbacks
#callbacks, #callbacks_for?, #emit, #on, #once
Instance Attribute Details
#max_concurrent_requests ⇒ Integer
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 |
#pending ⇒ Array[Request] (readonly)
Returns the value of attribute pending.
19 20 21 |
# File 'lib/httpx/connection/http1.rb', line 19 def pending @pending end |
#requests ⇒ Array[Request] (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) ⇒ void
This method returns an undefined value.
94 95 96 |
# File 'lib/httpx/connection/http1.rb', line 94 def <<(data) @parser << data end |
#close ⇒ void
This method returns an undefined value.
73 74 75 76 77 |
# File 'lib/httpx/connection/http1.rb', line 73 def close reset reset_requests emit(:close) end |
#consume ⇒ void
This method returns an undefined value.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/httpx/connection/http1.rb', line 110 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) ⇒ void
This method returns an undefined value.
185 186 187 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 |
# File 'lib/httpx/connection/http1.rb', line 185 def dispatch(request) if request.expects? @parser.reset! return handle(request) end @request = nil @requests.shift response = request.response #: Response emit(:response, request, response) if @parser.upgrade? response << @parser.upgrade_data @parser.reset! throw(:called) end @parser.reset! @max_requests -= 1 manage_connection(request, response) if exhausted? reset_requests emit(:exhausted) elsif @requests.any? consume else send(@pending.shift, true) unless @pending.empty? end end |
#empty? ⇒ Boolean
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/httpx/connection/http1.rb', line 83 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
79 80 81 |
# File 'lib/httpx/connection/http1.rb', line 79 def exhausted? !@max_requests.positive? end |
#handle_error(ex, request = nil) ⇒ void
This method returns an undefined value.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/httpx/connection/http1.rb', line 217 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 |
#interests ⇒ io_interests?
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 |
#no_more_requests? ⇒ Boolean
254 255 256 |
# File 'lib/httpx/connection/http1.rb', line 254 def no_more_requests? @requests.empty? end |
#on_complete ⇒ void
This method returns an undefined value.
176 177 178 179 180 181 182 183 |
# File 'lib/httpx/connection/http1.rb', line 176 def on_complete request = @request return unless request request.log(level: 2) { "parsing complete" } dispatch(request) end |
#on_data(chunk) ⇒ void
This method returns an undefined value.
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/httpx/connection/http1.rb', line 163 def on_data(chunk) request = @request return unless request request.log(color: :green) { "-> DATA: #{chunk.bytesize} bytes..." } request.log(level: 2, color: :green) { "-> #{log_redact_body(chunk.inspect)}" } response = request.response response << chunk end |
#on_headers(h) ⇒ void
This method returns an undefined value.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/httpx/connection/http1.rb', line 129 def on_headers(h) request = @request = @requests.first return if request.response request.log(level: 2) { "headers received" } headers = request..headers_class.new(h) response = request..response_class.new(request, @parser.status_code, @parser.http_version, headers) request.log(color: :yellow) { "-> HEADLINE: #{response.status} HTTP/#{@parser.http_version}" } 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..max_response_body_size raise HTTPX::Error, "maximum response body size exceeded" end request.response = response on_complete if response.finished? end |
#on_start ⇒ void
This method returns an undefined value.
HTTP Parser callbacks
must be public methods, or else they won't be reachable
125 126 127 |
# File 'lib/httpx/connection/http1.rb', line 125 def on_start log(level: 2) { "parsing begins" } end |
#on_trailers(h) ⇒ void
This method returns an undefined value.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/httpx/connection/http1.rb', line 151 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 |
#ping ⇒ void
This method returns an undefined value.
245 246 247 248 |
# File 'lib/httpx/connection/http1.rb', line 245 def ping reset emit(:reset) end |
#reset ⇒ void
This method returns an undefined value.
50 51 52 53 54 |
# File 'lib/httpx/connection/http1.rb', line 50 def reset @max_requests = @options.max_requests || MAX_REQUESTS @parser.reset! @handshake_completed = false end |
#reset_requests ⇒ void
This method returns an undefined value.
move incompleted requests back to pending queue.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/httpx/connection/http1.rb', line 57 def reset_requests completed_idx = 0 @requests.each_with_index do |request, idx| if request.response completed_idx = idx + 1 next end request.transition(:idle) end # drop completed requests @requests.shift(completed_idx) @pending.unshift(*@requests) @requests.clear end |
#send(request, head = false) ⇒ Boolean
rubocop:disable Naming/PredicateMethod
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/httpx/connection/http1.rb', line 98 def send(request, head = false) # rubocop:disable Naming/PredicateMethod unless @max_requests.positive? && @max_requests > @requests.size head ? @pending.unshift(request) : @pending << request return false end @requests << request @pipelining = @max_concurrent_requests > 1 && @requests.size > 1 emit(:request_accepted, request) true end |
#timeout ⇒ interval?
36 37 38 |
# File 'lib/httpx/connection/http1.rb', line 36 def timeout @options.timeout.operation_timeout end |
#waiting_for_ping? ⇒ Boolean
250 251 252 |
# File 'lib/httpx/connection/http1.rb', line 250 def waiting_for_ping? false end |