Module

whiplash/http1

src/whiplash/http1.pooka

Reading 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

enum

Version

src/whiplash/http1.pooka:22
enum Version

The HTTP version of a message.

variant

Http10

src/whiplash/http1.pooka:23
Version.Http10
variant

Http11

src/whiplash/http1.pooka:24
Version.Http11
class

Limits

src/whiplash/http1.pooka:28
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.

field

max_request_line

src/whiplash/http1.pooka:29
max_request_line: Int
INFERREDInt
field

max_header_bytes

src/whiplash/http1.pooka:30
max_header_bytes: Int
INFERREDInt
field

max_headers

src/whiplash/http1.pooka:31
max_headers: Int
INFERREDInt
field

max_chunk_extension

src/whiplash/http1.pooka:32
max_chunk_extension: Int
INFERREDInt
constructor

new

src/whiplash/http1.pooka:29
INFERREDfn(Int, Int, Int, Int) -> whiplash/http1.Limits
class

Event

src/whiplash/http1.pooka:36
class Event

Something that happened in the bytes.

class

RequestHead

src/whiplash/http1.pooka:40
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.

field

method

src/whiplash/http1.pooka:41
method: String
INFERREDString
field

target

src/whiplash/http1.pooka:42
target: String
INFERREDString
field

version

src/whiplash/http1.pooka:43
version: Version
field

headers

src/whiplash/http1.pooka:44
headers: List(#(String, String))
INFERREDList(#(String, String))
field

content_length

src/whiplash/http1.pooka:45
content_length: Option(Int)
INFERREDOption(Int)
field

chunked

src/whiplash/http1.pooka:46
chunked: Bool
INFERREDBool
field

keep_alive

src/whiplash/http1.pooka:47
keep_alive: Bool
INFERREDBool
field

expects_continue

src/whiplash/http1.pooka:48
expects_continue: Bool
INFERREDBool
constructor

new

src/whiplash/http1.pooka:41
INFERREDfn(String, String, whiplash/http1.Version, List(#(String, String)), Option(Int), Bool, Bool, Bool) -> whiplash/http1.RequestHead
class

ResponseHead

src/whiplash/http1.pooka:52
class ResponseHead(
  version: Version,
  status: Int,
  reason: String,
  headers: List(#(String, String))
) is Event

A response's status line and headers, for clients.

field

version

src/whiplash/http1.pooka:53
version: Version
field

status

src/whiplash/http1.pooka:54
status: Int
INFERREDInt
field

reason

src/whiplash/http1.pooka:55
reason: String
INFERREDString
field

headers

src/whiplash/http1.pooka:56
headers: List(#(String, String))
INFERREDList(#(String, String))
constructor

new

src/whiplash/http1.pooka:53
INFERREDfn(whiplash/http1.Version, Int, String, List(#(String, String))) -> whiplash/http1.ResponseHead
class

BodyChunk

src/whiplash/http1.pooka:60
class BodyChunk(data: BitArray) is Event

Part of a body, as it arrived.

field

data

src/whiplash/http1.pooka:60
data: BitArray
INFERREDBitArray
constructor

new

src/whiplash/http1.pooka:60
INFERREDfn(BitArray) -> whiplash/http1.BodyChunk
class

Trailers

src/whiplash/http1.pooka:63
class Trailers(fields: List(#(String, String))) is Event

Trailer fields after a chunked body.

field

fields

src/whiplash/http1.pooka:63
fields: List(#(String, String))
INFERREDList(#(String, String))
constructor

new

src/whiplash/http1.pooka:63
INFERREDfn(List(#(String, String))) -> whiplash/http1.Trailers
class

MessageEnd

src/whiplash/http1.pooka:66
class MessageEnd is Event

The end of one message. Another may follow on the same connection.

class

Http1Error

src/whiplash/http1.pooka:70
class Http1Error(status: Int, message: String)

Why the bytes were refused, and the status to answer with: 400, 413, 414, 431, 501 or 505.

field

status

src/whiplash/http1.pooka:70
status: Int
INFERREDInt
field

message

src/whiplash/http1.pooka:70
message: String
INFERREDString
constructor

new

src/whiplash/http1.pooka:70
INFERREDfn(Int, String) -> whiplash/http1.Http1Error
enum

Side

src/whiplash/http1.pooka:73
enum Side

Whether a parser reads requests (servers) or responses (clients).

variant

Requests

src/whiplash/http1.pooka:74
Side.Requests
variant

Responses

src/whiplash/http1.pooka:75
Side.Responses
class

State

src/whiplash/http1.pooka:79
class State

The parser's progress through a message.

class

Head

src/whiplash/http1.pooka:83
class Head(seen: BitArray, cursor: Int, line_end: Int) is State
field

seen

src/whiplash/http1.pooka:83
seen: BitArray
INFERREDBitArray
field

cursor

src/whiplash/http1.pooka:83
cursor: Int
INFERREDInt
field

line_end

src/whiplash/http1.pooka:83
line_end: Int
INFERREDInt
constructor

new

src/whiplash/http1.pooka:83
INFERREDfn(BitArray, Int, Int) -> whiplash/http1.Head
class

Body

src/whiplash/http1.pooka:85
class Body(remaining: Int) is State
field

remaining

src/whiplash/http1.pooka:85
remaining: Int
INFERREDInt
constructor

new

src/whiplash/http1.pooka:85
INFERREDfn(Int) -> whiplash/http1.Body
class

Chunks

src/whiplash/http1.pooka:87
class Chunks(phase: String, remaining: Int) is State
field

phase

src/whiplash/http1.pooka:87
phase: String
INFERREDString
field

remaining

src/whiplash/http1.pooka:87
remaining: Int
INFERREDInt
constructor

new

src/whiplash/http1.pooka:87
INFERREDfn(String, Int) -> whiplash/http1.Chunks
class

UntilClose

src/whiplash/http1.pooka:89
class UntilClose is State
class

Upgraded

src/whiplash/http1.pooka:93
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

src/whiplash/http1.pooka:98
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).

field

side

src/whiplash/http1.pooka:99
side: Side
field

limits

src/whiplash/http1.pooka:100
limits: Limits
field

state

src/whiplash/http1.pooka:101
state: State
field

buffer

src/whiplash/http1.pooka:102
buffer: BitArray
INFERREDBitArray
field

methods

src/whiplash/http1.pooka:103
methods: List(String)
INFERREDList(String)
constructor

new

src/whiplash/http1.pooka:99
function

default_limits

src/whiplash/http1.pooka:108
default_limits() -> Limits
INFERREDfn() -> whiplash/http1.Limits

Limits like those of common servers: an 8 KiB request line, 64 KiB of headers, at most 100 of them.

function

requests

src/whiplash/http1.pooka:113
requests(limits: Limits) -> Parser

A parser for requests, as a server reads them.

function

responses

src/whiplash/http1.pooka:118
responses(limits: Limits) -> Parser

A parser for responses, as a client reads them.

function

expect

src/whiplash/http1.pooka:124
expect(parser: Parser, method: String) -> Parser

Tell 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.

function

feed

src/whiplash/http1.pooka:136
feed(parser: Parser, bytes: BitArray) -> Result(
  #(List(Event), Parser),
  Http1Error
)

Feed bytes, and get every event they complete, with the parser to use for the next bytes.

function

finish

src/whiplash/http1.pooka:171
finish(parser: Parser) -> Result(List(Event), Http1Error)

The end of the input: a body delimited by the connection closing ends here; anything else unfinished is an error.

function

response_head

src/whiplash/http1.pooka:185
response_head(
  version: Version,
  status: Int,
  headers: List(#(String, String))
) -> BitArray
INFERREDfn(whiplash/http1.Version, Int, List(#(String, String))) -> BitArray

A response's status line and headers.

function

chunk

src/whiplash/http1.pooka:196
chunk(data: BitArray) -> BitArray
INFERREDfn(BitArray) -> BitArray

One chunk of a chunked body. An empty chunk would end the body, so it writes nothing.

function

chunk_head

src/whiplash/http1.pooka:209
chunk_head(size: Int) -> BitArray
INFERREDfn(Int) -> BitArray

The 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.

function

last_chunk

src/whiplash/http1.pooka:232
last_chunk(trailers: List(#(String, String))) -> BitArray
INFERREDfn(List(#(String, String))) -> BitArray

The last chunk, with any trailer fields.

function

checked_response_head

src/whiplash/http1.pooka:1439
checked_response_head(
  version: Version,
  status: Int,
  headers: List(#(String, String))
) -> Result(BitArray, Http1Error)
INFERREDfn(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

function

serve_head

src/whiplash/http1.pooka:1479
serve_head(
  version: Version,
  status: Int,
  headers: List(#(String, String)),
  content_length: Option(Int),
  connection: Option(String)
) -> Result(BitArray, Http1Error)
INFERREDfn(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.

function

serve_head_dated

src/whiplash/http1.pooka:1491
serve_head_dated(
  version: Version,
  status: Int,
  headers: List(#(String, String)),
  content_length: Option(Int),
  connection: Option(String),
  date: Option(String)
) -> Result(BitArray, Http1Error)
INFERREDfn(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.