class Protocol::HTTP::Request
~~~
Protocol::HTTP::Request[“GET”, “/index.html”, {“accept” => “text/html”}]
# Short form:
Protocol::HTTP::Request.new(“http”, “example.com”, “GET”, “/index.html”, “HTTP/1.1”, Protocol::HTTP::Headers[[“accept”, “text/html”]])
# Long form:
require ‘protocol/http’
~~~ ruby
Represents an HTTP request which can be used both server and client-side.
def self.[](method, path, _headers = nil, _body = nil, scheme: nil, authority: nil, headers: _headers, body: _body, protocol: nil, interim_response: nil)
@parameter headers [Hash] The headers, e.g. `{"accept" => "text/html"}`, etc.
@parameter path [String] The path, e.g. `"/index.html"`, `"/search?q=hello"`, etc.
@parameter method [String] The HTTP method, e.g. `"GET"`, `"POST"`, etc.
A short-cut method which exposes the main request variables that you'd typically care about.
def self.[](method, path, _headers = nil, _body = nil, scheme: nil, authority: nil, headers: _headers, body: _body, protocol: nil, interim_response: nil) body = Body::Buffered.wrap(body) headers = Headers[headers] self.new(scheme, authority, method, path, nil, headers, body, protocol, interim_response) end
def as_json(...)
def as_json(...) { scheme: @scheme, authority: @authority, method: @method, path: @path, version: @version, headers: @headers&.as_json, body: @body&.as_json, protocol: @protocol } end
def call(connection)
def call(connection) connection.call(self) end
def connect?
def connect? @method == Methods::CONNECT end
def head?
def head? @method == Methods::HEAD end
def idempotent?
def idempotent? @method != Methods::POST && (@body.nil? || @body.empty?) end
def initialize(scheme = nil, authority = nil, method = nil, path = nil, version = nil, headers = Headers.new, body = nil, protocol = nil, interim_response = nil)
def initialize(scheme = nil, authority = nil, method = nil, path = nil, version = nil, headers = Headers.new, body = nil, protocol = nil, interim_response = nil) @scheme = scheme @authority = authority @method = method @path = path @version = version @headers = headers @body = body @protocol = protocol @interim_response = interim_response end
def on_interim_response(&block)
def on_interim_response(&block) if interim_response = @interim_response @interim_response = ->(status, headers) do block.call(status, headers) interim_response.call(status, headers) end else @interim_response = block end end
def peer
A request that is generated by a server, may choose to include the peer (address) associated with the request. It should be implemented by a sub-class.
def peer nil end
def send_interim_response(status, headers)
def send_interim_response(status, headers) @interim_response&.call(status, headers) end
def to_json(...)
def to_json(...) as_json.to_json(...) end
def to_s
def to_s "#{@scheme}://#{@authority}: #{@method} #{@path} #{@version}" end