/// `multipart/form-data` request bodies (RFC 7578): the form an HTML page /// posts when it has a file input. /// /// match multipart.from_request req (multipart.defaults ()) /// { Ok(Form(fields, files)) -> ... /// , Error(TooManyFiles) | Error(FileTooLarge(_)) -> http.text 413 "too large" /// , Error(_) -> http.text 400 "bad form" /// } /// /// The body is read untrusted: every count and size has a limit /// ([`Limits`]); of a part's headers only `Content-Disposition` and /// `Content-Type` are read; a file name is cut to its last path segment /// (`C:\a\b.png` is `b.png`) and refused with a control character in it; /// a field must be UTF-8; a part that is itself multipart is refused. The /// preamble and the epilogue are ignored. A file input left empty (no /// file name and no data, as browsers send it) is dropped. /// /// What is not supported: `filename*` (RFC 5987), which RFC 7578 says /// not to send, is ignored; `Content-Transfer-Encoding` is ignored, as /// RFC 7578 deprecates it; a quoted parameter ends at the next `"` (a /// backslash is kept as it is, so a Windows path survives). import std/bytes import std/http.{type Request} import std/list import std/option import std/result import std/string /// How much a body may hold: files, the bytes of one file, text fields, /// the bytes of one field, and the bytes of one part's headers. @derive Eq Show Debug pub type Limits { Limits( max_files: Int , max_file_bytes: Int , max_fields: Int , max_field_bytes: Int , max_part_head: Int ) } /// 10 files of up to 10 MiB, 100 fields of up to 1 MiB, 8 KiB of headers /// per part. Change them with a record update: /// `Limits(..multipart.defaults (), max_files: 1)`. The request body /// itself is limited by the server (`http.Settings`' `max_body` and /// `large_bodies`). pub fn defaults :: -> Limits { Limits( max_files: 10 , max_file_bytes: 10485760 , max_fields: 100 , max_field_bytes: 1048576 , max_part_head: 8192 ) } /// A file: its field's name, its file name (the last path segment of /// what the client sent: untrusted, possibly `""` or `..`), its media /// type (`application/octet-stream` when the part has none) and its /// bytes. @derive Eq Debug pub type Upload { Upload(name: String, filename: String, content_type: String, data: Bytes) } /// The text fields and the files, each in the order they were sent. A /// name may appear more than once. @derive Eq Debug pub type Form { Form(fields: List<(String, String)>, files: List) } impl Show { fn show :: upload { debug upload } } impl Show
{ fn show :: form { debug form } } /// Why a body is not a form. @derive Eq Show Debug pub type Error { /// The content type is not `multipart/form-data`. NotMultipart /// The content type has no boundary, or one RFC 2046 does not allow. , BadBoundary /// The body does not follow the format; `at` is a byte offset. , Malformed(at: Int, reason: String) /// More than `max_files` files. , TooManyFiles /// A file over `max_file_bytes`; its file name. , FileTooLarge(String) /// More than `max_fields` fields. , TooManyFields /// A field over `max_field_bytes`; its name. , FieldTooLarge(String) } /// The form a request posted, by its `Content-Type`. pub fn from_request :: req: Request limits: Limits -> Result { match http.header req "content-type" { None -> Error(NotMultipart) , Some(content_type) -> { use b <- result.try (boundary content_type) parse req.body b limits } } } /// The boundary of a `multipart/form-data` content type: 1 to 70 /// characters of RFC 2046's set, quoted or not. pub fn boundary :: content_type: String -> Result { match media content_type { Ok ( ("multipart/form-data", params) ) -> match find_param params "boundary" { Some(b) -> if is_boundary b { Ok(b) } else { Error(BadBoundary) } , None -> Error(BadBoundary) } , _ -> Error(NotMultipart) } } fn is_boundary :: b: String -> Bool { let data = bytes.from_string b let n = bytes.length data n >= 1 && n <= 70 && bytes.read_u8 data (n - 1) != 32 && list.all (bytes.to_list data) is_bchar } /// RFC 2046's `bchars`: digits, letters, `'()+_,-./:=?` and the space. fn is_bchar :: b: Int -> Bool { b >= 48 && b <= 57 || b >= 65 && b <= 90 || b >= 97 && b <= 122 || b >= 39 && b <= 41 || b >= 43 && b <= 47 || b == 32 || b == 58 || b == 61 || b == 63 || b == 95 } // ---- the body ------------------------------------------------------------------ /// The form in `body`, whose parts are separated by `boundary` (see /// [`boundary`]). pub fn parse :: body: Bytes boundary: String limits: Limits -> Result { let dash = bytes.from_string ("--" <> boundary) let delimiter = bytes.from_string ("\r\n--" <> boundary) let scan = Scan(body, delimiter, skips delimiter, limits) let first = if bytes.slice body 0 (bytes.length dash) == dash { bytes.length dash } else { let found = find scan 0 if found < 0 { -1 } else { found + bytes.length delimiter } } if first < 0 { Error(Malformed(0, "no boundary")) } else { parts scan first [] [] } } type Scan { Scan(body: Bytes, delimiter: Bytes, skips: Bytes, limits: Limits) } /// The parts after a delimiter, which ends at `at`. fn parts :: scan: Scan at: Int fields: List<(String, String)> files: List -> Result { let body = scan.body if bytes.slice body at (at + 2) == bytes.from_string "--" { Ok(Form(list.reverse fields, list.reverse files)) } else { let start = padded body at if bytes.slice body start (start + 2) != bytes.from_string "\r\n" { Error(Malformed(at, "a boundary not followed by a line end")) } else { use (part, next) <- result.try (part_at scan (start + 2)) use (fields, files) <- result.try (kept scan.limits part fields files) parts scan next fields files } } } /// Skips the transport padding (spaces and tabs) after a delimiter. fn padded :: body: Bytes at: Int -> Int { match bytes.read_u8 body at { 32 | 9 -> padded body (at + 1), _ -> at } } type Part { Field(name: String, value: String), File(upload: Upload), EmptyFile } /// The part that starts at `at`, and the end of the delimiter after it. fn part_at :: scan: Scan at: Int -> Result<(Part, Int), Error> { let body = scan.body let head_end = if bytes.slice body at (at + 2) == bytes.from_string "\r\n" { at } else { head_end_from body at (at + scan.limits.max_part_head) } if head_end < 0 { Error(Malformed(at, "a part's headers do not end within the limit")) } else { let data_start = if head_end == at { at + 2 } else { head_end + 4 } let data_end = find scan data_start if data_end < 0 { Error(Malformed(data_start, "a part without a boundary after it")) } else { use head <- result.try (head_of body at head_end) use part <- result.try (part_of at head (bytes.slice body data_start data_end)) Ok((part, data_end + bytes.length scan.delimiter)) } } } /// Where the `\r\n\r\n` that ends a part's headers starts, if it does /// before `limit`; else -1. fn head_end_from :: body: Bytes at: Int limit: Int -> Int { if at > limit || at + 4 > bytes.length body { -1 } else if bytes.read_u8 body at == 13 && bytes.slice body at (at + 4) == bytes.from_string "\r\n\r\n" { at } else { head_end_from body (at + 1) limit } } type Head { Head(disposition: Option, content_type: Option) } /// The headers from `at` to `end` that this package reads. fn head_of :: body: Bytes at: Int end: Int -> Result { match bytes.to_string (bytes.slice body at end) { Error(_) -> Error(Malformed(at, "a part's headers are not UTF-8")) , Ok(text) -> { let lines = if text == "" { [] } else { string.split text "\r\n" } list.fold lines Ok(Head(None, None)) fn head line { result.try head fn h { header_into at h line } } } } } fn header_into :: at: Int head: Head line: String -> Result { match string.find line ":" { None -> Error(Malformed(at, "a part header without a colon")) , Some(colon) -> { let data = bytes.from_string line let name = string.to_lower (string.trim (bytes.to_string_lossy (bytes.slice data 0 colon))) let value = string.trim ( bytes.to_string_lossy (bytes.slice data (colon + 1) (bytes.length data)) ) match name { "content-disposition" if option.is_none head.disposition -> Ok(Head(..head, disposition: Some(value))) , "content-type" if option.is_none head.content_type -> Ok(Head(..head, content_type: Some(value))) , _ -> Ok(head) } } } } fn part_of :: at: Int head: Head data: Bytes -> Result { use disposition <- result.try ( option.to_result head.disposition (Malformed(at, "a part without Content-Disposition")) ) use (kind, params) <- result.try ( media disposition |> result.map_error fn reason { Malformed(at, reason) } ) let content_type = option.map head.content_type string.trim if kind != "form-data" { Error(Malformed(at, "a part that is not form-data")) } else if option.map content_type is_multipart == Some(True) { Error(Malformed(at, "a part that is itself multipart")) } else { match (find_param params "name", find_param params "filename") { (None, _) -> Error(Malformed(at, "a part without a name")) , (Some(name), None) -> match bytes.to_string data { Ok(value) -> Ok(Field(name, value)) , Error(_) -> Error(Malformed(at, "the field \"{name}\" is not UTF-8")) } , (Some(name), Some(sent)) -> if has_control sent { Error(Malformed(at, "a file name with a control character")) } else if sent == "" && bytes.length data == 0 { Ok(EmptyFile) } else { Ok( File( Upload( name , last_segment sent , option.unwrap_or content_type "application/octet-stream" , data ) ) ) } } } } fn is_multipart :: content_type: String -> Bool { string.starts_with (string.to_lower content_type) "multipart/" } /// What follows the last `/` or `\`. fn last_segment :: name: String -> String { let slashed = list.flat_map (string.split name "/") fn s { string.split s "\\" } option.unwrap_or (list.find (list.reverse slashed) fn _ { True }) "" } fn has_control :: text: String -> Bool { list.any (bytes.to_list (bytes.from_string text)) fn b { b < 32 || b == 127 } } /// Adds `part` to the fields or the files, within `limits`. fn kept :: limits: Limits part: Part fields: List<(String, String)> files: List -> Result<(List<(String, String)>, List), Error> { match part { EmptyFile -> Ok((fields, files)) , Field ( name , value ) -> if list.length fields >= limits.max_fields { Error(TooManyFields) } else if string.byte_length value > limits.max_field_bytes { Error(FieldTooLarge(name)) } else { Ok(([(name, value), ..fields], files)) } , File (upload) -> if list.length files >= limits.max_files { Error(TooManyFiles) } else if bytes.length upload.data > limits.max_file_bytes { Error(FileTooLarge(upload.filename)) } else { Ok((fields, [upload, ..files])) } } } // ---- headers' parameters ------------------------------------------------------- /// A header value's first item, lowercased, and its `; name=value` /// parameters (names lowercased, values as written, unquoted). fn media :: value: String -> Result<(String, List<(String, String)>), String> { let data = bytes.from_string value let end = until data 0 59 let kind = string.to_lower (string.trim (bytes.to_string_lossy (bytes.slice data 0 end))) params data end [] |> result.map fn found { (kind, found) } } fn params :: data: Bytes at: Int found: List<(String, String)> -> Result, String> { let at = spaces data at let n = bytes.length data if at >= n { Ok(list.reverse found) } else if bytes.read_u8 data at != 59 { Error("expected ; between parameters") } else { let start = spaces data (at + 1) let name_end = until_either data start 61 59 let name = string.to_lower (string.trim (bytes.to_string_lossy (bytes.slice data start name_end))) if bytes.read_u8 data name_end != 61 { params data name_end (if name == "" { found } else { [(name, ""), ..found] }) } else { let value_start = spaces data (name_end + 1) if bytes.read_u8 data value_start == 34 { let close = until data (value_start + 1) 34 if close >= n { Error("an unclosed quoted parameter") } else { let value = bytes.to_string_lossy (bytes.slice data (value_start + 1) close) params data (close + 1) [(name, value), ..found] } } else { let end = until data value_start 59 let value = string.trim (bytes.to_string_lossy (bytes.slice data value_start end)) params data end [(name, value), ..found] } } } } fn find_param :: params: List<(String, String)> name: String -> Option { list.find params fn p { p.0 == name } |> option.map fn p { p.1 } } fn spaces :: data: Bytes at: Int -> Int { match bytes.read_u8 data at { 32 | 9 -> spaces data (at + 1), _ -> at } } /// The first index at or after `at` whose byte is `stop`, else the length. fn until :: data: Bytes at: Int stop: Int -> Int { until_either data at stop stop } fn until_either :: data: Bytes at: Int a: Int b: Int -> Int { let byte = bytes.read_u8 data at if byte < 0 { bytes.length data } else if byte == a || byte == b { at } else { until_either data (at + 1) a b } } // ---- finding the delimiter ----------------------------------------------------- /// Where the delimiter first starts at or after `from` in the body, else /// -1 (Boyer-Moore-Horspool: a mismatch skips ahead by up to the /// delimiter's length). fn find :: scan: Scan from: Int -> Int { let m = bytes.length scan.delimiter let last = bytes.read_u8 scan.body (from + m - 1) if last < 0 { -1 } else if matches scan.body from scan.delimiter (m - 1) { from } else { find scan (from + bytes.read_u8 scan.skips last) } } /// Whether `needle`'s bytes up to `i` are the body's from `at`, compared /// from the end. fn matches :: body: Bytes at: Int needle: Bytes i: Int -> Bool { i < 0 || bytes.read_u8 body (at + i) == bytes.read_u8 needle i && matches body at needle (i - 1) } /// For each byte value, how far the search may move when that byte ends /// the window: the distance from its last place in `needle` (the final /// byte left out) to the end, or the whole length. fn skips :: needle: Bytes -> Bytes { let m = bytes.length needle bytes.from_list (list.map (list.range 0 255) fn b { m - 1 - last_index needle b (m - 2) }) } fn last_index :: needle: Bytes b: Int i: Int -> Int { if i < 0 || bytes.read_u8 needle i == b { i } else { last_index needle b (i - 1) } }