module HTTP
HTTP: require "http". An HTTP/1.1 server for use behind a reverse
proxy (docs/11-http.md): a handler takes a Request and returns a
Response; the server runs a task per connection.
require "http"
HTTP.serve(8080) { |req| HTTP::Response.text("hello #{req.path}") }
<std:http>:12
Classes and modules
HTTP::BadRequestA malformed request or body: answered 400.HTTP::BodyA request body, read from the connection as the handler asks for it.HTTP::BodyWriterGiven to a Stream body's procedure: each write is a chunk.HTTP::ErrorAnswered with its status (and its message as a text body) when a handler raises it; the parser raises the subclasses.HTTP::FieldOne header field, its name lowercased.HTTP::HandlerWhat serves requests: a Router, a procedure (HTTP.handler) or a class or data of one's own. Handlers are Shareable, since every connection's task calls them: state shared between requests lives in an Atom or a Channel. Middleware is a handler holding the next.HTTP::HeadersHeader fields in order, names lowercased; a name may repeat. Names are case-insensitive, and written out in the conventional form (Content-Type). Immutable: put, add and delete return new headers.HTTP::HeadersTooLargeA request line and headers over the server's max_header_bytes: answered 431.HTTP::OptionsA server's settings, as Server.new takes them.HTTP::PayloadTooLargeA body over the server's max_body_bytes (Body#read): answered 413.HTTP::ProxiesTrusted proxies: addresses or networks (10.0.0.0/8, fd00::/8).HTTP::RequestA request, as the server read it or as a test builds it (HTTP::Request.get). A data value: with makes a changed copy. It is not Shareable, since its Body is not: copy the fields a task needs out of it before spawning one.HTTP::ResponseWhat a handler returns. A data value: #header and with make changed copies. When it sends one, the server adds the framing (Content-Length or Transfer-Encoding) and a Date header.HTTP::ResponseBodyThe body of a Response. The server adds the framing it implies: Content-Length for all but ResponseBody::Stream, which is sent chunked.HTTP::RouterRoutes by method and path pattern (/users/{id}, /files/{path...}): the most specific match wins; a path that only other methods have is answered 405.HTTP::RouterBuilderCollects routes for Router.build. A pattern is a path whose segments may be {name} (one segment) or, last, {name...} (the rest of the path, possibly empty, slashes included); a trailing slash is significant.HTTP::ServerAn HTTP/1.1 server: a task per connection, requests on one connection served in order. Shareable, so that a Signal.trap block or another task can shut it down.
Class methods
def self.reason(status: Int) -> String
The reason phrase of a status ("" for one without).
<std:http>:80
def self.split_target(target: String) -> [String, String]
A request target split at its first ? into the path, percent-decoded,
and the query, left as it is ("" when there is none).
A path whose decoding is not UTF-8 is left undecoded.
HTTP.split_target("/a%20b?x=1") # => ["/a b", "x=1"]
HTTP.split_target("/a") # => ["/a", ""]
<std:http>:699
def self.percent_decode(s: String, plus: Bool = …) -> String?
Percent-decoding, nil when the result is not UTF-8. plus decodes
+ as a space (forms and queries).
<std:http>:706
def self.parse_query(q: String) -> Hash[String, Array[String]]
The parameters of the query string q, each name with its values in
order.
Names and values are percent-decoded, + as a space (left as they
are when the decoding is not UTF-8); a pair without = has the value
"", and empty pairs are skipped.
HTTP.parse_query("a=1&b=2&a=3") # => {"a" => ["1", "3"], "b" => ["2"]}
HTTP.parse_query("a+b=c%20d&&e") # => {"a b" => ["c d"], "e" => [""]}
<std:http>:748
def self.handler(&f: shareable (HTTP::Request) -> HTTP::Response) -> HTTP::Handler
A handler calling the block (which captures by value).
hello = HTTP.handler { |req| HTTP::Response.text("hello #{req.path}") }
hello.call(HTTP::Request.get("/world")).body
# => HTTP::ResponseBody::Text(text: "hello /world")
<std:http>:1131
def self.serve(port: Int = …, host: String = …, &handler: shareable (HTTP::Request) -> HTTP::Response) -> Void
Serves HTTP on host:port, answering each request with the
block's response, until the calling task is cancelled.
A shorthand for HTTP::Server.new with a handler from the block
(HTTP.handler) and HTTP::Server#serve.
<std:http>:1773