/// The pure half of reading an HTTP/1.1 response (RFC 9112): the head /// (status line and headers), how its body is framed, and a chunked /// body decoded as it arrives. `fetch` feeds them what it reads. /// /// response.head (bytes.from_string "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi") 65536 /// // Ok(Some(Head(200, [("Content-Length", "2")], 38))) /// /// Strict where it is cheap: lines end in CRLF, a header name is a token /// with no space before its colon, a folded header line is refused, and /// `Content-Length` values must agree. import std/bytes import std/list import std/result import std/string /// A response's head: its status, its headers in order (names as sent), /// and its size in bytes, the blank line included. @derive Eq Show Debug pub type Head { Head(status: Int, headers: List<(String, String)>, size: Int) } /// The head at the start of `data`: `Ok(None)` while it is incomplete, /// an error when it is malformed or longer than `max_head` bytes. pub fn head :: data: Bytes max_head: Int -> Result, String> { let end = blank_line data 0 (bytes.length data) if end < 0 { if bytes.length data > max_head { Error("the response head is too large") } else { Ok(None) } } else if end + 4 > max_head { Error("the response head is too large") } else { match bytes.to_string_lossy (bytes.slice data 0 end) |> string.split "\r\n" { [status_line, ..lines] -> { use status <- result.try (status_of status_line) use headers <- result.try (result.all (list.map lines header_of)) Ok(Some(Head(status, headers, end + 4))) } , [] -> Error("an empty response head") } } } /// Where the first `\r\n\r\n` at or after `at` starts, else -1. fn blank_line :: data: Bytes at: Int n: Int -> Int { if at + 4 > n { -1 } else if bytes.read_u8 data at == 13 && bytes.read_u8 data (at + 1) == 10 && bytes.read_u8 data (at + 2) == 13 && bytes.read_u8 data (at + 3) == 10 { at } else { blank_line data (at + 1) n } } /// `HTTP/1.1 200 OK` (or HTTP/1.0; the reason may be empty or absent). fn status_of :: line: String -> Result { let data = bytes.from_string line let version = bytes.to_string_lossy (bytes.slice data 0 9) let code = bytes.to_string_lossy (bytes.slice data 9 12) let after = bytes.read_u8 data 12 if (version == "HTTP/1.1 " || version == "HTTP/1.0 ") && string.byte_length code == 3 && list.all (string.chars code) fn c { string.contains "0123456789" c } && (after < 0 || after == 32) { let status = result.unwrap_or (string.to_int code) 0 if status >= 100 && status <= 599 { Ok(status) } else { Error("a status out of range: {code}") } } else { Error("not an HTTP/1.x status line") } } fn header_of :: line: String -> Result<(String, String), String> { let data = bytes.from_string line match string.find line ":" { Some(colon) if colon > 0 && is_token (bytes.slice data 0 colon) -> { let value = bytes.slice data (colon + 1) (bytes.length data) if list.any (bytes.to_list value) fn b { b == 0 || b == 13 || b == 10 } { Error("a header value with a CR, LF or NUL") } else { Ok( ( bytes.to_string_lossy (bytes.slice data 0 colon) , string.trim (bytes.to_string_lossy value) ) ) } } , _ -> Error("a malformed header line") } } /// RFC 9110's `token`: the characters a header name may have. fn is_token :: name: Bytes -> Bool { list.all (bytes.to_list name) fn b { b >= 48 && b <= 57 || b >= 65 && b <= 90 || b >= 97 && b <= 122 || string.contains "!#$%&'*+-.^_`|~" (string_of_byte b) } } fn string_of_byte :: b: Int -> String { bytes.to_string_lossy (bytes.from_list [b]) } /// The value of header `name` (any case), if present. pub fn header :: headers: List<(String, String)> name: String -> Option { list.find headers fn h { string.eq_ignore_ascii_case h.0 name } |> option_map fn h { h.1 } } fn option_map :: o: Option f: fn(a) -> b -> Option { match o { Some(x) -> Some(f x), None -> None } } // ---- framing ------------------------------------------------------------------- /// How a response's body is delimited (RFC 9112, §6.3). @derive Eq Show Debug pub type Framing { /// 1xx, 204 and 304: no body. NoBody /// `Content-Length` bytes. , Length(Int) /// `Transfer-Encoding: chunked`. , Chunked /// Everything until the server closes the connection. , UntilClose } /// The framing of a response to a GET. `Transfer-Encoding` other than /// `chunked` alone is an error (no compression is asked for), and so are /// `Content-Length` values that are not one same number. pub fn framing :: head: Head -> Result { let named = fn name { list.filter head.headers fn h { string.eq_ignore_ascii_case h.0 name } } if head.status < 200 || head.status == 204 || head.status == 304 { Ok(NoBody) } else { match (named "transfer-encoding", named "content-length") { ([], []) -> Ok(UntilClose) , ([], lengths) -> length_of (list.map lengths fn h { h.1 }) , ([(_, coding)], _) -> if string.to_lower coding == "chunked" { Ok(Chunked) } else { Error("an unsupported Transfer-Encoding: {coding}") } , _ -> Error("more than one Transfer-Encoding") } } } fn length_of :: values: List -> Result { let all = list.flat_map values fn v { list.map (string.split v ",") string.trim } match all { [first, ..rest] if list.all rest fn v { v == first } && first != "" && string.byte_length first <= 18 && list.all (string.chars first) fn c { string.contains "0123456789" c } -> string.to_int first |> result.map Length , _ -> Error("a bad Content-Length") } } // ---- chunked bodies ------------------------------------------------------------ /// A chunked body (RFC 9112, §7.1) being decoded as it arrives, kept to /// at most `max` bytes. Feed it what is read with [`feed`] until /// [`is_done`]; then [`body`] is the data and whether it was cut. pub opaque type Dechunker { Dechunker( phase: Phase , line: Bytes , left: Int , body: bytes.Builder , size: Int , max: Int ) } @derive Eq type Phase { SizeLine, Data, DataEnd, Trailer, Done, Full } /// The longest chunk-size or trailer line accepted. fn max_line :: -> Int { 4096 } /// Nothing decoded yet; the body is kept to `max` bytes. pub fn dechunker :: max: Int -> Dechunker { Dechunker(SizeLine, bytes.empty (), 0, bytes.builder (), 0, max) } /// Whether the last chunk and the trailers were read, or the body /// reached its limit: either way, nothing more needs reading. pub fn is_done :: d: Dechunker -> Bool { d.phase == Done || d.phase == Full } /// The data so far, and whether it was cut at the limit. pub fn body :: d: Dechunker -> (Bytes, Bool) { (bytes.build d.body, d.phase == Full) } /// The data decoded since the last `take` (or the start), and `d` /// without it: for a body read as it arrives. The limit still counts /// every byte decoded, and `body` then gives only what came after. pub fn take :: d: Dechunker -> (Bytes, Dechunker) { (bytes.build d.body, Dechunker(..d, body: bytes.builder ())) } /// `d` after `data`, or why the body is not chunked properly. Bytes /// after the end are ignored. pub fn feed :: d: Dechunker data: Bytes -> Result { step d data 0 } fn step :: d: Dechunker data: Bytes at: Int -> Result { let n = bytes.length data match d.phase { Done | Full -> Ok(d) , Data -> if at >= n { Ok(d) } else { let take = int_min d.left (n - at) let room = d.max - d.size if take > room { let body = bytes.append d.body (bytes.slice data at (at + room)) Ok(Dechunker(..d, phase: Full, body: body, size: d.max)) } else { let body = bytes.append d.body (bytes.slice data at (at + take)) let left = d.left - take let phase = if left == 0 { DataEnd } else { Data } let d = Dechunker( ..d , phase: phase , left: left , body: body , size: d.size + take ) step d data (at + take) } } , _ -> line_step d data at } } /// A size line, the CRLF after a chunk's data, or a trailer line. fn line_step :: d: Dechunker data: Bytes at: Int -> Result { let rest = bytes.slice data at (bytes.length data) let text = if bytes.length d.line == 0 { rest } else { bytes.concat d.line rest } let end = crlf text 0 (bytes.length text) if end < 0 { if bytes.length text > max_line () { Error("a chunk line too long") } else { Ok(Dechunker(..d, line: text)) } } else if end > max_line () { Error("a chunk line too long") } else { let line = bytes.slice text 0 end let d = Dechunker(..d, line: bytes.empty ()) use d <- result.try (after_line d line) step d text (end + 2) } } fn after_line :: d: Dechunker line: Bytes -> Result { match d.phase { SizeLine -> match chunk_size line { Ok(0) -> Ok(Dechunker(..d, phase: Trailer)) , Ok(size) -> Ok(Dechunker(..d, phase: Data, left: size)) , Error(e) -> Error(e) } , DataEnd -> if bytes.length line == 0 { Ok(Dechunker(..d, phase: SizeLine)) } else { Error("chunk data not followed by CRLF") } , _ -> if bytes.length line == 0 { Ok(Dechunker(..d, phase: Done)) } else { Ok(d) } } } /// The hex size before any `;extension`; at most 15 digits. fn chunk_size :: line: Bytes -> Result { let semicolon = until_semicolon line 0 let digits = string.trim (bytes.to_string_lossy (bytes.slice line 0 semicolon)) let chars = string.chars (string.to_lower digits) if chars == [] || list.length chars > 15 { Error("a bad chunk size") } else { list.fold chars Ok(0) fn acc c { use n <- result.try acc match string.find "0123456789abcdef" c { Some(v) -> Ok(n * 16 + v), None -> Error("a bad chunk size") } } } } fn until_semicolon :: line: Bytes at: Int -> Int { let b = bytes.read_u8 line at if b < 0 { bytes.length line } else if b == 59 { at } else { until_semicolon line (at + 1) } } fn crlf :: data: Bytes at: Int n: Int -> Int { if at + 2 > n { -1 } else if bytes.read_u8 data at == 13 && bytes.read_u8 data (at + 1) == 10 { at } else { crlf data (at + 1) n } } fn int_min :: a: Int b: Int -> Int { if a < b { a } else { b } }