Class: HTTPX::Request
- Inherits:
-
Object
- Object
- HTTPX::Request
- Extended by:
- Forwardable
- Defined in:
- lib/httpx/request.rb
Overview
Defines how an HTTP request is handled internally, both in terms of making attributes accessible, as well as maintaining the state machine which manages streaming the request onto the wire.
Direct Known Subclasses
Defined Under Namespace
Classes: Body
Constant Summary collapse
- ALLOWED_URI_SCHEMES =
%w[https http].freeze
Constants included from Loggable
Loggable::COLORS, Loggable::USE_DEBUG_LOG
Instance Attribute Summary collapse
-
#active_timeouts ⇒ Object
readonly
Returns the value of attribute active_timeouts.
-
#body ⇒ Object
readonly
an HTTPX::Request::Body object containing the request body payload (or
nil, whenn there is none). -
#drain_error ⇒ Object
readonly
Exception raised during enumerable body writes.
-
#headers ⇒ Object
readonly
an HTTPX::Headers object containing the request HTTP headers.
-
#options ⇒ Object
readonly
an HTTPX::Options object containing request options.
-
#peer_address ⇒ Object
The IP address from the peer server.
-
#persistent ⇒ Object
writeonly
Sets the attribute persistent.
-
#response ⇒ Object
the corresponding HTTPX::Response object, when there is one.
-
#state ⇒ Object
readonly
a symbol describing which frame is currently being flushed.
-
#uri ⇒ Object
readonly
the absolute URI object for this request.
-
#verb ⇒ Object
readonly
the upcased string HTTP verb for this request.
Instance Method Summary collapse
-
#authority ⇒ Object
returs the URI authority of the request.
- #can_buffer? ⇒ Boolean
- #complete!(response = @response) ⇒ Object
-
#drain_body ⇒ Object
consumes and returns the next available chunk of request body that can be sent.
-
#expects? ⇒ Boolean
whether the request supports the 100-continue handshake and already processed the 100 response.
-
#initialize(verb, uri, options, params = EMPTY_HASH) ⇒ Request
constructor
initializes the instance with the given
verb(an upppercase String, ex. ‘GEt’), an absolute or relativeuri(either as String or URI::HTTP object), the requestoptions(instance of HTTPX::Options) and an optional Hash ofparams. -
#inspect ⇒ Object
:nocov:.
-
#interests ⇒ Object
returns
:ror:w, depending on whether the request is waiting for a response or flushing. -
#merge_headers(h) ⇒ Object
merges
hinto the instance of HTTPX::Headers of the request. -
#origin ⇒ Object
returs the URI origin of the request.
-
#path ⇒ Object
returnns the URI path of the request
uri. - #persistent? ⇒ Boolean
-
#ping! ⇒ Object
marks the request as having been buffered with a ping.
-
#ping? ⇒ Boolean
whether request has been buffered with a ping.
-
#query ⇒ Object
returs the URI query string of the request (when available).
-
#read_timeout ⇒ Object
the read timeout defined for this request.
-
#request_timeout ⇒ Object
the request timeout defined for this request.
-
#scheme ⇒ Object
the URI scheme of the request
uri. - #set_timeout_callback(event, &callback) ⇒ Object
-
#trailers ⇒ Object
returns an instance of HTTPX::Headers containing the trailer headers.
-
#trailers? ⇒ Boolean
if the request contains trailer headers.
-
#transition(nextstate) ⇒ Object
moves on to the
nextstateof the request state machine (when all preconditions are met). -
#write_timeout ⇒ Object
the write timeout defined for this request.
Methods included from Callbacks
#callbacks_for?, #emit, #on, #once
Methods included from Loggable
#log, #log_exception, #log_redact, #log_redact_body, #log_redact_headers
Constructor Details
#initialize(verb, uri, options, params = EMPTY_HASH) ⇒ Request
initializes the instance with the given verb (an upppercase String, ex. ‘GEt’), an absolute or relative uri (either as String or URI::HTTP object), the request options (instance of HTTPX::Options) and an optional Hash of params.
Besides any of the options documented in HTTPX::Options (which would override or merge with what options sets), it accepts also the following:
- :params
-
hash or array of key-values which will be encoded and set in the query string of request uris.
- :body
-
to be encoded in the request body payload. can be a String, an IO object (i.e. a File), or an Enumerable.
- :form
-
hash of array of key-values which will be form-urlencoded- or multipart-encoded in requests body payload.
- :json
-
hash of array of key-values which will be JSON-encoded in requests body payload.
- :xml
-
Nokogiri XML nodes which will be encoded in requests body payload.
:body, :form, :json and :xml are all mutually exclusive, i.e. only one of them gets picked up.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/httpx/request.rb', line 68 def initialize(verb, uri, , params = EMPTY_HASH) @verb = verb.to_s.upcase @uri = Utils.to_uri(uri) @headers = .headers.dup merge_headers(params.delete(:headers)) if params.key?(:headers) @query_params = params.delete(:params) if params.key?(:params) @body = .request_body_class.new(@headers, , **params) = @body. if @uri.relative? || @uri.host.nil? origin = .origin raise(Error, "invalid URI: #{@uri}") unless origin base_path = .base_path @uri = origin.merge("#{base_path}#{@uri}") end raise UnsupportedSchemeError, "#{@uri}: #{@uri.scheme}: unsupported URI scheme" unless ALLOWED_URI_SCHEMES.include?(@uri.scheme) @state = :idle @response = @peer_address = @context = @informational_status = nil @ping = false @persistent = .persistent @active_timeouts = [] end |
Instance Attribute Details
#active_timeouts ⇒ Object (readonly)
Returns the value of attribute active_timeouts.
46 47 48 |
# File 'lib/httpx/request.rb', line 46 def active_timeouts @active_timeouts end |
#body ⇒ Object (readonly)
an HTTPX::Request::Body object containing the request body payload (or nil, whenn there is none).
27 28 29 |
# File 'lib/httpx/request.rb', line 27 def body @body end |
#drain_error ⇒ Object (readonly)
Exception raised during enumerable body writes.
39 40 41 |
# File 'lib/httpx/request.rb', line 39 def drain_error @drain_error end |
#headers ⇒ Object (readonly)
an HTTPX::Headers object containing the request HTTP headers.
24 25 26 |
# File 'lib/httpx/request.rb', line 24 def headers @headers end |
#options ⇒ Object (readonly)
an HTTPX::Options object containing request options.
33 34 35 |
# File 'lib/httpx/request.rb', line 33 def end |
#peer_address ⇒ Object
The IP address from the peer server.
42 43 44 |
# File 'lib/httpx/request.rb', line 42 def peer_address @peer_address end |
#persistent=(value) ⇒ Object (writeonly)
Sets the attribute persistent
44 45 46 |
# File 'lib/httpx/request.rb', line 44 def persistent=(value) @persistent = value end |
#response ⇒ Object
the corresponding HTTPX::Response object, when there is one.
36 37 38 |
# File 'lib/httpx/request.rb', line 36 def response @response end |
#state ⇒ Object (readonly)
a symbol describing which frame is currently being flushed.
30 31 32 |
# File 'lib/httpx/request.rb', line 30 def state @state end |
#uri ⇒ Object (readonly)
the absolute URI object for this request.
21 22 23 |
# File 'lib/httpx/request.rb', line 21 def uri @uri end |
#verb ⇒ Object (readonly)
the upcased string HTTP verb for this request.
18 19 20 |
# File 'lib/httpx/request.rb', line 18 def verb @verb end |
Instance Method Details
#authority ⇒ Object
returs the URI authority of the request.
session.build_request("GET", "https://google.com/query"). #=> "google.com"
session.build_request("GET", "http://internal:3182/a"). #=> "internal:3182"
207 208 209 |
# File 'lib/httpx/request.rb', line 207 def @uri. end |
#can_buffer? ⇒ Boolean
149 150 151 |
# File 'lib/httpx/request.rb', line 149 def can_buffer? @state != :done end |
#complete!(response = @response) ⇒ Object
99 100 101 |
# File 'lib/httpx/request.rb', line 99 def complete!(response = @response) emit(:complete, response) end |
#drain_body ⇒ Object
consumes and returns the next available chunk of request body that can be sent
237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/httpx/request.rb', line 237 def drain_body return nil if @body.nil? @drainer ||= @body.each chunk = @drainer.next.dup emit(:body_chunk, chunk) chunk rescue StopIteration nil rescue StandardError => e @drain_error = e nil end |
#expects? ⇒ Boolean
whether the request supports the 100-continue handshake and already processed the 100 response.
303 304 305 |
# File 'lib/httpx/request.rb', line 303 def expects? @headers["expect"] == "100-continue" && @informational_status == 100 && !@response end |
#inspect ⇒ Object
:nocov:
253 254 255 256 257 258 259 |
# File 'lib/httpx/request.rb', line 253 def inspect "#<#{self.class}:#{object_id} " \ "#{@verb} " \ "#{uri} " \ "@headers=#{@headers} " \ "@body=#{@body}>" end |
#interests ⇒ Object
returns :r or :w, depending on whether the request is waiting for a response or flushing.
143 144 145 146 147 |
# File 'lib/httpx/request.rb', line 143 def interests return :r if @state == :done || @state == :expect :w end |
#merge_headers(h) ⇒ Object
merges h into the instance of HTTPX::Headers of the request.
154 155 156 157 158 159 |
# File 'lib/httpx/request.rb', line 154 def merge_headers(h) @headers = @headers.merge(h) return unless @headers.key?("range") @headers.delete("accept-encoding") end |
#origin ⇒ Object
returs the URI origin of the request.
session.build_request("GET", "https://google.com/query"). #=> "https://google.com"
session.build_request("GET", "http://internal:3182/a"). #=> "http://internal:3182"
215 216 217 |
# File 'lib/httpx/request.rb', line 215 def origin @uri.origin end |
#path ⇒ Object
returnns the URI path of the request uri.
195 196 197 198 199 200 201 |
# File 'lib/httpx/request.rb', line 195 def path path = uri.path.dup path = +"" if path.nil? path << "/" if path.empty? path << "?#{query}" unless query.empty? path end |
#persistent? ⇒ Boolean
128 129 130 |
# File 'lib/httpx/request.rb', line 128 def persistent? @persistent end |
#ping! ⇒ Object
marks the request as having been buffered with a ping
109 110 111 |
# File 'lib/httpx/request.rb', line 109 def ping! @ping = true end |
#ping? ⇒ Boolean
whether request has been buffered with a ping
104 105 106 |
# File 'lib/httpx/request.rb', line 104 def ping? @ping end |
#query ⇒ Object
returs the URI query string of the request (when available).
session.build_request("GET", "https://search.com").query #=> ""
session.build_request("GET", "https://search.com?q=a").query #=> "q=a"
session.build_request("GET", "https://search.com", params: { q: "a"}).query #=> "q=a"
session.build_request("GET", "https://search.com?q=a", params: { foo: "bar"}).query #=> "q=a&foo&bar"
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/httpx/request.rb', line 225 def query return @query if defined?(@query) query = [] if (q = @query_params) && !q.empty? query << Transcoder::Form.encode(q) end query << @uri.query if @uri.query @query = query.join("&") end |
#read_timeout ⇒ Object
the read timeout defined for this request.
114 115 116 |
# File 'lib/httpx/request.rb', line 114 def read_timeout .timeout[:read_timeout] end |
#request_timeout ⇒ Object
the request timeout defined for this request.
124 125 126 |
# File 'lib/httpx/request.rb', line 124 def request_timeout .timeout[:request_timeout] end |
#scheme ⇒ Object
the URI scheme of the request uri.
162 163 164 |
# File 'lib/httpx/request.rb', line 162 def scheme @uri.scheme end |
#set_timeout_callback(event, &callback) ⇒ Object
307 308 309 310 311 312 313 314 |
# File 'lib/httpx/request.rb', line 307 def set_timeout_callback(event, &callback) clb = once(event, &callback) # reset timeout callbacks when requests get rerouted to a different connection once(:idle) do callbacks(event).delete(clb) end end |
#trailers ⇒ Object
returns an instance of HTTPX::Headers containing the trailer headers
138 139 140 |
# File 'lib/httpx/request.rb', line 138 def trailers @trailers ||= .headers_class.new end |
#trailers? ⇒ Boolean
if the request contains trailer headers
133 134 135 |
# File 'lib/httpx/request.rb', line 133 def trailers? defined?(@trailers) end |
#transition(nextstate) ⇒ Object
moves on to the nextstate of the request state machine (when all preconditions are met)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/httpx/request.rb', line 263 def transition(nextstate) case nextstate when :idle @body.rewind @ping = false @response = nil @drainer = nil @active_timeouts.clear when :headers return unless @state == :idle when :body return unless @state == :headers || @state == :expect if @headers.key?("expect") if @informational_status && @informational_status == 100 # check for 100 Continue response, and deallocate the var # if @informational_status == 100 # @response = nil # end else return if @state == :expect # do not re-set it nextstate = :expect end end when :trailers return unless @state == :body when :done return if @state == :expect end log(level: 3) { "#{@state}] -> #{nextstate}" } @state = nextstate emit(@state, self) nil end |
#write_timeout ⇒ Object
the write timeout defined for this request.
119 120 121 |
# File 'lib/httpx/request.rb', line 119 def write_timeout .timeout[:write_timeout] end |