Module
whiplash/http1
src/whiplash/http1.pookaReading HTTP/1.1 a few bytes at a time, never trusting more than it should (RFC 9112).
A Parser is fed bytes as they arrive, in any pieces, and returns
events: a request's head, chunks of its body, its end, and the next
request when they are pipelined. It is sans-IO, strict where RFC 9112
says ambiguity leads to request smuggling, and limited where an endless
header could exhaust memory.
The serialiser writes response heads, chunked bodies and trailers.
API reference
- enumVersion
- classLimits
- classEvent
- classRequestHead
- classResponseHead
- classBodyChunk
- classTrailers
- classMessageEnd
- classHttp1Error
- enumSide
- classState
- classHead
- classBody
- classChunks
- classUntilClose
- classUpgraded
- classParser
- functiondefault_limits
- functionrequests
- functionresponses
- functionexpect
- functionfeed
- functionfinish
- functionresponse_head
- functionchunk
- functionchunk_head
- functionlast_chunk
- functionchecked_response_head
- functionserve_head
- functionserve_head_dated
enum Version
The HTTP version of a message.
Version.Http10
Version.Http11
class Limits(
max_request_line: Int,
max_header_bytes: Int,
max_headers: Int,
max_chunk_extension: Int
)
How much a peer may send before it is refused.
max_request_line: Int
Intmax_header_bytes: Int
Intmax_headers: Int
Intmax_chunk_extension: Int
Int
fn(Int, Int, Int, Int) -> whiplash/http1.Limitsclass Event
Something that happened in the bytes.
class RequestHead(
method: String,
target: String,
version: Version,
headers: List(#(String, String)),
content_length: Option(Int),
chunked: Bool,
keep_alive: Bool,
expects_continue: Bool
) is Event
A request's line and headers. content_length is known for fixed-size
bodies; chunked for Transfer-Encoding: chunked.
method: String
Stringtarget: String
Stringversion: Version
whiplash/http1.Versionheaders: List(#(String, String))
List(#(String, String))content_length: Option(Int)
Option(Int)chunked: Bool
Boolkeep_alive: Bool
Boolexpects_continue: Bool
Bool
fn(String, String, whiplash/http1.Version, List(#(String, String)), Option(Int), Bool, Bool, Bool) -> whiplash/http1.RequestHeadclass ResponseHead(
version: Version,
status: Int,
reason: String,
headers: List(#(String, String))
) is Event
A response's status line and headers, for clients.
version: Version
whiplash/http1.Versionstatus: Int
Intreason: String
Stringheaders: List(#(String, String))
List(#(String, String))
fn(whiplash/http1.Version, Int, String, List(#(String, String))) -> whiplash/http1.ResponseHeadclass BodyChunk(data: BitArray) is Event
Part of a body, as it arrived.
data: BitArray
BitArray
fn(BitArray) -> whiplash/http1.BodyChunkclass Trailers(fields: List(#(String, String))) is Event
Trailer fields after a chunked body.
fields: List(#(String, String))
List(#(String, String))
fn(List(#(String, String))) -> whiplash/http1.Trailersclass MessageEnd is Event
The end of one message. Another may follow on the same connection.
class Http1Error(status: Int, message: String)
Why the bytes were refused, and the status to answer with: 400, 413, 414, 431, 501 or 505.
status: Int
Intmessage: String
String
fn(Int, String) -> whiplash/http1.Http1Errorenum Side
Whether a parser reads requests (servers) or responses (clients).
Side.Requests
Side.Responses
class State
The parser's progress through a message.
class Head(seen: BitArray, cursor: Int, line_end: Int) is State
seen: BitArray
BitArraycursor: Int
Intline_end: Int
Int
fn(BitArray, Int, Int) -> whiplash/http1.Headclass Body(remaining: Int) is State
remaining: Int
Int
fn(Int) -> whiplash/http1.Bodyclass Chunks(phase: String, remaining: Int) is State
phase: String
Stringremaining: Int
Int
fn(String, Int) -> whiplash/http1.Chunksclass UntilClose is State
class Upgraded is State
Parsing paused at a protocol upgrade. Parser.buffer belongs to the new
protocol, even when its first frame arrived in the same TCP delivery.
class Parser(
side: Side,
limits: Limits,
state: State,
buffer: BitArray,
methods: List(String)
)
A parser and what it has not yet consumed. A response parser also keeps the methods of the requests it is answering, oldest first: whether a response has a body depends on its request (RFC 9112 section 6.3).
side: Side
whiplash/http1.Sidelimits: Limits
whiplash/http1.Limitsstate: State
whiplash/http1.Statebuffer: BitArray
BitArraymethods: List(String)
List(String)
fn(whiplash/http1.Side, whiplash/http1.Limits, whiplash/http1.State, BitArray, List(String)) -> whiplash/http1.Parserdefault_limits() -> Limits
fn() -> whiplash/http1.LimitsLimits like those of common servers: an 8 KiB request line, 64 KiB of headers, at most 100 of them.
requests(limits: Limits) -> Parser
fn(whiplash/http1.Limits) -> whiplash/http1.ParserA parser for requests, as a server reads them.
responses(limits: Limits) -> Parser
fn(whiplash/http1.Limits) -> whiplash/http1.ParserA parser for responses, as a client reads them.
expect(parser: Parser, method: String) -> Parser
fn(whiplash/http1.Parser, String) -> whiplash/http1.ParserTell a response parser the method of a request sent on its connection, in order. Without one, a response is read as the answer to a GET.
feed(parser: Parser, bytes: BitArray) -> Result(
#(List(Event), Parser),
Http1Error
)
fn(whiplash/http1.Parser, BitArray) -> Result(#(List(whiplash/http1.Event), whiplash/http1.Parser), whiplash/http1.Http1Error)Feed bytes, and get every event they complete, with the parser to use for the next bytes.
finish(parser: Parser) -> Result(List(Event), Http1Error)
fn(whiplash/http1.Parser) -> Result(List(whiplash/http1.Event), whiplash/http1.Http1Error)The end of the input: a body delimited by the connection closing ends here; anything else unfinished is an error.
response_head(
version: Version,
status: Int,
headers: List(#(String, String))
) -> BitArray
fn(whiplash/http1.Version, Int, List(#(String, String))) -> BitArrayA response's status line and headers.
chunk(data: BitArray) -> BitArray
fn(BitArray) -> BitArrayOne chunk of a chunked body. An empty chunk would end the body, so it writes nothing.
chunk_head(size: Int) -> BitArray
fn(Int) -> BitArrayThe size line that opens a chunk of size bytes: the size in hex, then
CRLF. With the chunk's bytes and a closing CRLF it frames the chunk, as
separate parts, so the bytes need not be copied into one binary.
last_chunk(trailers: List(#(String, String))) -> BitArray
fn(List(#(String, String))) -> BitArrayThe last chunk, with any trailer fields.
checked_response_head(
version: Version,
status: Int,
headers: List(#(String, String))
) -> Result(BitArray, Http1Error)
fn(whiplash/http1.Version, Int, List(#(String, String))) -> Result(BitArray, whiplash/http1.Http1Error)Validate application-supplied fields before they can become wire metadata. response_head is a convenience for trusted constants and asserts this result; a server should use this checked entry point for handler-supplied values. https://www.rfc-editor.org/rfc/rfc9112.html#section-4
serve_head(
version: Version,
status: Int,
headers: List(#(String, String)),
content_length: Option(Int),
connection: Option(String)
) -> Result(BitArray, Http1Error)
fn(whiplash/http1.Version, Int, List(#(String, String)), Option(Int), Option(String)) -> Result(BitArray, whiplash/http1.Http1Error)The head a server writes around an application's fields. The server owns
framing: application Content-Length, Transfer-Encoding and Connection
fields are dropped, and content_length and connection written instead.
Names and values are still checked, so a handler cannot inject a line.
Statuses below 200 are refused; 1xx responses are the server's business.
serve_head_dated(
version: Version,
status: Int,
headers: List(#(String, String)),
content_length: Option(Int),
connection: Option(String),
date: Option(String)
) -> Result(BitArray, Http1Error)
fn(whiplash/http1.Version, Int, List(#(String, String)), Option(Int), Option(String), Option(String)) -> Result(BitArray, whiplash/http1.Http1Error)serve_head, with the server's own Date field written as given. The
date is trusted, so it is not validated like an application's fields.