tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

599 lignes · 15715 octetsBrut · Historique
1/// `multipart/form-data` request bodies (RFC 7578): the form an HTML page
2/// posts when it has a file input.
3///
4/// match multipart.from_request req (multipart.defaults ())
5/// { Ok(Form(fields, files)) -> ...
6/// , Error(TooManyFiles) | Error(FileTooLarge(_)) -> http.text 413 "too large"
7/// , Error(_) -> http.text 400 "bad form"
8/// }
9///
10/// The body is read untrusted: every count and size has a limit
11/// ([`Limits`]); of a part's headers only `Content-Disposition` and
12/// `Content-Type` are read; a file name is cut to its last path segment
13/// (`C:\a\b.png` is `b.png`) and refused with a control character in it;
14/// a field must be UTF-8; a part that is itself multipart is refused. The
15/// preamble and the epilogue are ignored. A file input left empty (no
16/// file name and no data, as browsers send it) is dropped.
17///
18/// What is not supported: `filename*` (RFC 5987), which RFC 7578 says
19/// not to send, is ignored; `Content-Transfer-Encoding` is ignored, as
20/// RFC 7578 deprecates it; a quoted parameter ends at the next `"` (a
21/// backslash is kept as it is, so a Windows path survives).
22
23import std/bytes
24import std/http.{type Request}
25import std/list
26import std/option
27import std/result
28import std/string
29
30/// How much a body may hold: files, the bytes of one file, text fields,
31/// the bytes of one field, and the bytes of one part's headers.
32@derive Eq Show Debug
33pub type Limits
34{ Limits(
35 max_files: Int
36 , max_file_bytes: Int
37 , max_fields: Int
38 , max_field_bytes: Int
39 , max_part_head: Int
40 )
41}
42
43/// 10 files of up to 10 MiB, 100 fields of up to 1 MiB, 8 KiB of headers
44/// per part. Change them with a record update:
45/// `Limits(..multipart.defaults (), max_files: 1)`. The request body
46/// itself is limited by the server (`http.Settings`' `max_body` and
47/// `large_bodies`).
48pub fn defaults :: -> Limits
49{
50 Limits( max_files: 10
51 , max_file_bytes: 10485760
52 , max_fields: 100
53 , max_field_bytes: 1048576
54 , max_part_head: 8192
55 )
56}
57
58/// A file: its field's name, its file name (the last path segment of
59/// what the client sent: untrusted, possibly `""` or `..`), its media
60/// type (`application/octet-stream` when the part has none) and its
61/// bytes.
62@derive Eq Debug
63pub type Upload
64{ Upload(name: String, filename: String, content_type: String, data: Bytes) }
65
66/// The text fields and the files, each in the order they were sent. A
67/// name may appear more than once.
68@derive Eq Debug
69pub type Form { Form(fields: List<(String, String)>, files: List<Upload>) }
70
71impl Show<Upload>
72{
73 fn show :: upload
74 {
75 debug upload
76 }
77}
78
79impl Show<Form>
80{
81 fn show :: form
82 {
83 debug form
84 }
85}
86
87/// Why a body is not a form.
88@derive Eq Show Debug
89pub type Error
90{ /// The content type is not `multipart/form-data`.
91 NotMultipart
92 /// The content type has no boundary, or one RFC 2046 does not allow.
93, BadBoundary
94 /// The body does not follow the format; `at` is a byte offset.
95, Malformed(at: Int, reason: String)
96 /// More than `max_files` files.
97, TooManyFiles
98 /// A file over `max_file_bytes`; its file name.
99, FileTooLarge(String)
100 /// More than `max_fields` fields.
101, TooManyFields
102 /// A field over `max_field_bytes`; its name.
103, FieldTooLarge(String)
104}
105
106/// The form a request posted, by its `Content-Type`.
107pub fn from_request :: req: Request limits: Limits -> Result<Form, Error>
108{
109 match http.header req "content-type"
110 { None -> Error(NotMultipart)
111 , Some(content_type) ->
112 {
113 use b <- result.try (boundary content_type)
114 parse req.body b limits
115 }
116 }
117}
118
119/// The boundary of a `multipart/form-data` content type: 1 to 70
120/// characters of RFC 2046's set, quoted or not.
121pub fn boundary :: content_type: String -> Result<String, Error>
122{
123 match media content_type
124 { Ok
125 ( ("multipart/form-data", params)
126 ) -> match find_param params "boundary"
127 { Some(b) -> if is_boundary b { Ok(b) } else { Error(BadBoundary) }
128 , None -> Error(BadBoundary)
129 }
130 , _ -> Error(NotMultipart)
131 }
132}
133
134fn is_boundary :: b: String -> Bool
135{
136 let data = bytes.from_string b
137 let n = bytes.length data
138 n >= 1 && n <= 70 && bytes.read_u8 data (n - 1) != 32
139 && list.all (bytes.to_list data) is_bchar
140}
141
142/// RFC 2046's `bchars`: digits, letters, `'()+_,-./:=?` and the space.
143fn is_bchar :: b: Int -> Bool
144{
145 b >= 48 && b <= 57 || b >= 65 && b <= 90 || b >= 97 && b <= 122
146 || b >= 39 && b <= 41
147 || b >= 43 && b <= 47
148 || b == 32
149 || b == 58
150 || b == 61
151 || b == 63
152 || b == 95
153}
154
155// ---- the body ------------------------------------------------------------------
156
157/// The form in `body`, whose parts are separated by `boundary` (see
158/// [`boundary`]).
159pub fn parse ::
160 body: Bytes
161 boundary: String
162 limits: Limits
163 -> Result<Form, Error>
164{
165 let dash = bytes.from_string ("--" <> boundary)
166 let delimiter = bytes.from_string ("\r\n--" <> boundary)
167 let scan = Scan(body, delimiter, skips delimiter, limits)
168 let first = if bytes.slice body 0 (bytes.length dash) == dash
169 {
170 bytes.length dash
171 }
172 else
173 {
174 let found = find scan 0
175 if found < 0
176 {
177 -1
178 }
179 else
180 {
181 found + bytes.length delimiter
182 }
183 }
184 if first < 0
185 {
186 Error(Malformed(0, "no boundary"))
187 }
188 else
189 {
190 parts scan first [] []
191 }
192}
193
194type Scan { Scan(body: Bytes, delimiter: Bytes, skips: Bytes, limits: Limits) }
195
196/// The parts after a delimiter, which ends at `at`.
197fn parts ::
198 scan: Scan
199 at: Int
200 fields: List<(String, String)>
201 files: List<Upload>
202 -> Result<Form, Error>
203{
204 let body = scan.body
205 if bytes.slice body at (at + 2) == bytes.from_string "--"
206 {
207 Ok(Form(list.reverse fields, list.reverse files))
208 }
209 else
210 {
211 let start = padded body at
212 if bytes.slice body start (start + 2) != bytes.from_string "\r\n"
213 {
214 Error(Malformed(at, "a boundary not followed by a line end"))
215 }
216 else
217 {
218 use (part, next) <- result.try (part_at scan (start + 2))
219 use (fields, files) <- result.try (kept scan.limits part fields files)
220 parts scan next fields files
221 }
222 }
223}
224
225/// Skips the transport padding (spaces and tabs) after a delimiter.
226fn padded :: body: Bytes at: Int -> Int
227{
228 match bytes.read_u8 body at { 32 | 9 -> padded body (at + 1), _ -> at }
229}
230
231type Part
232{ Field(name: String, value: String), File(upload: Upload), EmptyFile }
233
234/// The part that starts at `at`, and the end of the delimiter after it.
235fn part_at :: scan: Scan at: Int -> Result<(Part, Int), Error>
236{
237 let body = scan.body
238 let head_end = if bytes.slice body at (at + 2) == bytes.from_string "\r\n"
239 {
240 at
241 }
242 else
243 {
244 head_end_from body at (at + scan.limits.max_part_head)
245 }
246 if head_end < 0
247 {
248 Error(Malformed(at, "a part's headers do not end within the limit"))
249 }
250 else
251 {
252 let data_start = if head_end == at { at + 2 } else { head_end + 4 }
253 let data_end = find scan data_start
254 if data_end < 0
255 {
256 Error(Malformed(data_start, "a part without a boundary after it"))
257 }
258 else
259 {
260 use head <- result.try (head_of body at head_end)
261 use part <-
262 result.try (part_of at head (bytes.slice body data_start data_end))
263 Ok((part, data_end + bytes.length scan.delimiter))
264 }
265 }
266}
267
268/// Where the `\r\n\r\n` that ends a part's headers starts, if it does
269/// before `limit`; else -1.
270fn head_end_from :: body: Bytes at: Int limit: Int -> Int
271{
272 if at > limit || at + 4 > bytes.length body
273 {
274 -1
275 }
276 else if bytes.read_u8 body at == 13
277 && bytes.slice body at (at + 4) == bytes.from_string "\r\n\r\n"
278 {
279 at
280 }
281 else
282 {
283 head_end_from body (at + 1) limit
284 }
285}
286
287type Head { Head(disposition: Option<String>, content_type: Option<String>) }
288
289/// The headers from `at` to `end` that this package reads.
290fn head_of :: body: Bytes at: Int end: Int -> Result<Head, Error>
291{
292 match bytes.to_string (bytes.slice body at end)
293 { Error(_) -> Error(Malformed(at, "a part's headers are not UTF-8"))
294 , Ok(text) ->
295 {
296 let lines = if text == "" { [] } else { string.split text "\r\n" }
297 list.fold
298 lines
299 Ok(Head(None, None))
300 fn head line { result.try head fn h { header_into at h line } }
301 }
302 }
303}
304
305fn header_into :: at: Int head: Head line: String -> Result<Head, Error>
306{
307 match string.find line ":"
308 { None -> Error(Malformed(at, "a part header without a colon"))
309 , Some(colon) ->
310 {
311 let data = bytes.from_string line
312 let name =
313 string.to_lower
314 (string.trim (bytes.to_string_lossy (bytes.slice data 0 colon)))
315 let value =
316 string.trim
317 ( bytes.to_string_lossy
318 (bytes.slice data (colon + 1) (bytes.length data))
319 )
320 match name
321 { "content-disposition" if option.is_none head.disposition ->
322 Ok(Head(..head, disposition: Some(value)))
323 , "content-type" if option.is_none head.content_type ->
324 Ok(Head(..head, content_type: Some(value)))
325 , _ -> Ok(head)
326 }
327 }
328 }
329}
330
331fn part_of :: at: Int head: Head data: Bytes -> Result<Part, Error>
332{
333 use disposition <-
334 result.try
335 ( option.to_result
336 head.disposition
337 (Malformed(at, "a part without Content-Disposition"))
338 )
339 use (kind, params) <-
340 result.try
341 ( media disposition
342 |> result.map_error fn reason { Malformed(at, reason) }
343 )
344 let content_type = option.map head.content_type string.trim
345 if kind != "form-data"
346 {
347 Error(Malformed(at, "a part that is not form-data"))
348 }
349 else if option.map content_type is_multipart == Some(True)
350 {
351 Error(Malformed(at, "a part that is itself multipart"))
352 }
353 else
354 {
355 match (find_param params "name", find_param params "filename")
356 { (None, _) -> Error(Malformed(at, "a part without a name"))
357 , (Some(name), None) -> match bytes.to_string data
358 { Ok(value) -> Ok(Field(name, value))
359 , Error(_) -> Error(Malformed(at, "the field \"{name}\" is not UTF-8"))
360 }
361 , (Some(name), Some(sent)) -> if has_control sent
362 {
363 Error(Malformed(at, "a file name with a control character"))
364 }
365 else if sent == "" && bytes.length data == 0
366 {
367 Ok(EmptyFile)
368 }
369 else
370 {
371 Ok( File( Upload( name
372 , last_segment sent
373 , option.unwrap_or content_type "application/octet-stream"
374 , data
375 )
376 )
377 )
378 }
379 }
380 }
381}
382
383fn is_multipart :: content_type: String -> Bool
384{
385 string.starts_with (string.to_lower content_type) "multipart/"
386}
387
388/// What follows the last `/` or `\`.
389fn last_segment :: name: String -> String
390{
391 let slashed =
392 list.flat_map (string.split name "/") fn s { string.split s "\\" }
393 option.unwrap_or (list.find (list.reverse slashed) fn _ { True }) ""
394}
395
396fn has_control :: text: String -> Bool
397{
398 list.any (bytes.to_list (bytes.from_string text)) fn b { b < 32 || b == 127 }
399}
400
401/// Adds `part` to the fields or the files, within `limits`.
402fn kept ::
403 limits: Limits
404 part: Part
405 fields: List<(String, String)>
406 files: List<Upload>
407 -> Result<(List<(String, String)>, List<Upload>), Error>
408{
409 match part
410 { EmptyFile -> Ok((fields, files))
411 , Field
412 ( name
413 , value
414 ) -> if list.length fields >= limits.max_fields
415 {
416 Error(TooManyFields)
417 }
418 else if string.byte_length value > limits.max_field_bytes
419 {
420 Error(FieldTooLarge(name))
421 }
422 else
423 {
424 Ok(([(name, value), ..fields], files))
425 }
426 , File
427 (upload) -> if list.length files >= limits.max_files
428 {
429 Error(TooManyFiles)
430 }
431 else if bytes.length upload.data > limits.max_file_bytes
432 {
433 Error(FileTooLarge(upload.filename))
434 }
435 else
436 {
437 Ok((fields, [upload, ..files]))
438 }
439 }
440}
441
442// ---- headers' parameters -------------------------------------------------------
443
444/// A header value's first item, lowercased, and its `; name=value`
445/// parameters (names lowercased, values as written, unquoted).
446fn media :: value: String -> Result<(String, List<(String, String)>), String>
447{
448 let data = bytes.from_string value
449 let end = until data 0 59
450 let kind =
451 string.to_lower
452 (string.trim (bytes.to_string_lossy (bytes.slice data 0 end)))
453 params data end [] |> result.map fn found { (kind, found) }
454}
455
456fn params ::
457 data: Bytes
458 at: Int
459 found: List<(String, String)>
460 -> Result<List<(String, String)>, String>
461{
462 let at = spaces data at
463 let n = bytes.length data
464 if at >= n
465 {
466 Ok(list.reverse found)
467 }
468 else if bytes.read_u8 data at != 59
469 {
470 Error("expected ; between parameters")
471 }
472 else
473 {
474 let start = spaces data (at + 1)
475 let name_end = until_either data start 61 59
476 let name =
477 string.to_lower
478 (string.trim (bytes.to_string_lossy (bytes.slice data start name_end)))
479 if bytes.read_u8 data name_end != 61
480 {
481 params
482 data
483 name_end
484 (if name == "" { found } else { [(name, ""), ..found] })
485 }
486 else
487 {
488 let value_start = spaces data (name_end + 1)
489 if bytes.read_u8 data value_start == 34
490 {
491 let close = until data (value_start + 1) 34
492 if close >= n
493 {
494 Error("an unclosed quoted parameter")
495 }
496 else
497 {
498 let value =
499 bytes.to_string_lossy (bytes.slice data (value_start + 1) close)
500 params data (close + 1) [(name, value), ..found]
501 }
502 }
503 else
504 {
505 let end = until data value_start 59
506 let value =
507 string.trim (bytes.to_string_lossy (bytes.slice data value_start end))
508 params data end [(name, value), ..found]
509 }
510 }
511 }
512}
513
514fn find_param :: params: List<(String, String)> name: String -> Option<String>
515{
516 list.find params fn p { p.0 == name } |> option.map fn p { p.1 }
517}
518
519fn spaces :: data: Bytes at: Int -> Int
520{
521 match bytes.read_u8 data at { 32 | 9 -> spaces data (at + 1), _ -> at }
522}
523
524/// The first index at or after `at` whose byte is `stop`, else the length.
525fn until :: data: Bytes at: Int stop: Int -> Int
526{
527 until_either data at stop stop
528}
529
530fn until_either :: data: Bytes at: Int a: Int b: Int -> Int
531{
532 let byte = bytes.read_u8 data at
533 if byte < 0
534 {
535 bytes.length data
536 }
537 else if byte == a || byte == b
538 {
539 at
540 }
541 else
542 {
543 until_either data (at + 1) a b
544 }
545}
546
547// ---- finding the delimiter -----------------------------------------------------
548
549/// Where the delimiter first starts at or after `from` in the body, else
550/// -1 (Boyer-Moore-Horspool: a mismatch skips ahead by up to the
551/// delimiter's length).
552fn find :: scan: Scan from: Int -> Int
553{
554 let m = bytes.length scan.delimiter
555 let last = bytes.read_u8 scan.body (from + m - 1)
556 if last < 0
557 {
558 -1
559 }
560 else if matches scan.body from scan.delimiter (m - 1)
561 {
562 from
563 }
564 else
565 {
566 find scan (from + bytes.read_u8 scan.skips last)
567 }
568}
569
570/// Whether `needle`'s bytes up to `i` are the body's from `at`, compared
571/// from the end.
572fn matches :: body: Bytes at: Int needle: Bytes i: Int -> Bool
573{
574 i < 0
575 || bytes.read_u8 body (at + i) == bytes.read_u8 needle i
576 && matches body at needle (i - 1)
577}
578
579/// For each byte value, how far the search may move when that byte ends
580/// the window: the distance from its last place in `needle` (the final
581/// byte left out) to the end, or the whole length.
582fn skips :: needle: Bytes -> Bytes
583{
584 let m = bytes.length needle
585 bytes.from_list
586 (list.map (list.range 0 255) fn b { m - 1 - last_index needle b (m - 2) })
587}
588
589fn last_index :: needle: Bytes b: Int i: Int -> Int
590{
591 if i < 0 || bytes.read_u8 needle i == b
592 {
593 i
594 }
595 else
596 {
597 last_index needle b (i - 1)
598 }
599}