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.

444 lignes · 10944 octetsBrut · Historique
1/// The pure half of reading an HTTP/1.1 response (RFC 9112): the head
2/// (status line and headers), how its body is framed, and a chunked
3/// body decoded as it arrives. `fetch` feeds them what it reads.
4///
5/// response.head (bytes.from_string "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi") 65536
6/// // Ok(Some(Head(200, [("Content-Length", "2")], 38)))
7///
8/// Strict where it is cheap: lines end in CRLF, a header name is a token
9/// with no space before its colon, a folded header line is refused, and
10/// `Content-Length` values must agree.
11
12import std/bytes
13import std/list
14import std/result
15import std/string
16
17/// A response's head: its status, its headers in order (names as sent),
18/// and its size in bytes, the blank line included.
19@derive Eq Show Debug
20pub type Head { Head(status: Int, headers: List<(String, String)>, size: Int) }
21
22/// The head at the start of `data`: `Ok(None)` while it is incomplete,
23/// an error when it is malformed or longer than `max_head` bytes.
24pub fn head :: data: Bytes max_head: Int -> Result<Option<Head>, String>
25{
26 let end = blank_line data 0 (bytes.length data)
27 if end < 0
28 {
29 if bytes.length data > max_head
30 {
31 Error("the response head is too large")
32 }
33 else
34 {
35 Ok(None)
36 }
37 }
38 else if end + 4 > max_head
39 {
40 Error("the response head is too large")
41 }
42 else
43 {
44 match bytes.to_string_lossy (bytes.slice data 0 end) |> string.split "\r\n"
45 { [status_line, ..lines] ->
46 {
47 use status <- result.try (status_of status_line)
48 use headers <- result.try (result.all (list.map lines header_of))
49 Ok(Some(Head(status, headers, end + 4)))
50 }
51 , [] -> Error("an empty response head")
52 }
53 }
54}
55
56/// Where the first `\r\n\r\n` at or after `at` starts, else -1.
57fn blank_line :: data: Bytes at: Int n: Int -> Int
58{
59 if at + 4 > n
60 {
61 -1
62 }
63 else if bytes.read_u8 data at == 13 && bytes.read_u8 data (at + 1) == 10
64 && bytes.read_u8 data (at + 2) == 13
65 && bytes.read_u8 data (at + 3) == 10
66 {
67 at
68 }
69 else
70 {
71 blank_line data (at + 1) n
72 }
73}
74
75/// `HTTP/1.1 200 OK` (or HTTP/1.0; the reason may be empty or absent).
76fn status_of :: line: String -> Result<Int, String>
77{
78 let data = bytes.from_string line
79 let version = bytes.to_string_lossy (bytes.slice data 0 9)
80 let code = bytes.to_string_lossy (bytes.slice data 9 12)
81 let after = bytes.read_u8 data 12
82 if (version == "HTTP/1.1 " || version == "HTTP/1.0 ")
83 && string.byte_length code == 3
84 && list.all (string.chars code) fn c { string.contains "0123456789" c }
85 && (after < 0 || after == 32)
86 {
87 let status = result.unwrap_or (string.to_int code) 0
88 if status >= 100 && status <= 599
89 {
90 Ok(status)
91 }
92 else
93 {
94 Error("a status out of range: {code}")
95 }
96 }
97 else
98 {
99 Error("not an HTTP/1.x status line")
100 }
101}
102
103fn header_of :: line: String -> Result<(String, String), String>
104{
105 let data = bytes.from_string line
106 match string.find line ":"
107 { Some(colon) if colon > 0 && is_token (bytes.slice data 0 colon) ->
108 {
109 let value = bytes.slice data (colon + 1) (bytes.length data)
110 if list.any (bytes.to_list value) fn b { b == 0 || b == 13 || b == 10 }
111 {
112 Error("a header value with a CR, LF or NUL")
113 }
114 else
115 {
116 Ok( ( bytes.to_string_lossy (bytes.slice data 0 colon)
117 , string.trim (bytes.to_string_lossy value)
118 )
119 )
120 }
121 }
122 , _ -> Error("a malformed header line")
123 }
124}
125
126/// RFC 9110's `token`: the characters a header name may have.
127fn is_token :: name: Bytes -> Bool
128{
129 list.all
130 (bytes.to_list name)
131 fn b
132 {
133 b >= 48 && b <= 57 || b >= 65 && b <= 90 || b >= 97 && b <= 122
134 || string.contains "!#$%&'*+-.^_`|~" (string_of_byte b)
135 }
136}
137
138fn string_of_byte :: b: Int -> String
139{
140 bytes.to_string_lossy (bytes.from_list [b])
141}
142
143/// The value of header `name` (any case), if present.
144pub fn header :: headers: List<(String, String)> name: String -> Option<String>
145{
146 list.find headers fn h { string.eq_ignore_ascii_case h.0 name }
147 |> option_map fn h { h.1 }
148}
149
150fn option_map :: o: Option<a> f: fn(a) -> b -> Option<b>
151{
152 match o { Some(x) -> Some(f x), None -> None }
153}
154
155// ---- framing -------------------------------------------------------------------
156
157/// How a response's body is delimited (RFC 9112, §6.3).
158@derive Eq Show Debug
159pub type Framing
160{ /// 1xx, 204 and 304: no body.
161 NoBody
162 /// `Content-Length` bytes.
163, Length(Int)
164 /// `Transfer-Encoding: chunked`.
165, Chunked
166 /// Everything until the server closes the connection.
167, UntilClose
168}
169
170/// The framing of a response to a GET. `Transfer-Encoding` other than
171/// `chunked` alone is an error (no compression is asked for), and so are
172/// `Content-Length` values that are not one same number.
173pub fn framing :: head: Head -> Result<Framing, String>
174{
175 let named = fn name
176 {
177 list.filter head.headers fn h { string.eq_ignore_ascii_case h.0 name }
178 }
179 if head.status < 200 || head.status == 204 || head.status == 304
180 {
181 Ok(NoBody)
182 }
183 else
184 {
185 match (named "transfer-encoding", named "content-length")
186 { ([], []) -> Ok(UntilClose)
187 , ([], lengths) -> length_of (list.map lengths fn h { h.1 })
188 , ([(_, coding)], _) -> if string.to_lower coding == "chunked"
189 {
190 Ok(Chunked)
191 }
192 else
193 {
194 Error("an unsupported Transfer-Encoding: {coding}")
195 }
196 , _ -> Error("more than one Transfer-Encoding")
197 }
198 }
199}
200
201fn length_of :: values: List<String> -> Result<Framing, String>
202{
203 let all =
204 list.flat_map values fn v { list.map (string.split v ",") string.trim }
205 match all
206 { [first, ..rest] if list.all rest fn v { v == first } && first != ""
207 && string.byte_length first <= 18
208 && list.all
209 (string.chars first)
210 fn c { string.contains "0123456789" c } ->
211 string.to_int first |> result.map Length
212 , _ -> Error("a bad Content-Length")
213 }
214}
215
216// ---- chunked bodies ------------------------------------------------------------
217
218/// A chunked body (RFC 9112, §7.1) being decoded as it arrives, kept to
219/// at most `max` bytes. Feed it what is read with [`feed`] until
220/// [`is_done`]; then [`body`] is the data and whether it was cut.
221pub opaque type Dechunker
222{ Dechunker(
223 phase: Phase
224 , line: Bytes
225 , left: Int
226 , body: bytes.Builder
227 , size: Int
228 , max: Int
229 )
230}
231
232@derive Eq
233type Phase { SizeLine, Data, DataEnd, Trailer, Done, Full }
234
235/// The longest chunk-size or trailer line accepted.
236fn max_line :: -> Int
237{
238 4096
239}
240
241/// Nothing decoded yet; the body is kept to `max` bytes.
242pub fn dechunker :: max: Int -> Dechunker
243{
244 Dechunker(SizeLine, bytes.empty (), 0, bytes.builder (), 0, max)
245}
246
247/// Whether the last chunk and the trailers were read, or the body
248/// reached its limit: either way, nothing more needs reading.
249pub fn is_done :: d: Dechunker -> Bool
250{
251 d.phase == Done || d.phase == Full
252}
253
254/// The data so far, and whether it was cut at the limit.
255pub fn body :: d: Dechunker -> (Bytes, Bool)
256{
257 (bytes.build d.body, d.phase == Full)
258}
259
260/// The data decoded since the last `take` (or the start), and `d`
261/// without it: for a body read as it arrives. The limit still counts
262/// every byte decoded, and `body` then gives only what came after.
263pub fn take :: d: Dechunker -> (Bytes, Dechunker)
264{
265 (bytes.build d.body, Dechunker(..d, body: bytes.builder ()))
266}
267
268/// `d` after `data`, or why the body is not chunked properly. Bytes
269/// after the end are ignored.
270pub fn feed :: d: Dechunker data: Bytes -> Result<Dechunker, String>
271{
272 step d data 0
273}
274
275fn step :: d: Dechunker data: Bytes at: Int -> Result<Dechunker, String>
276{
277 let n = bytes.length data
278 match d.phase
279 { Done | Full -> Ok(d)
280 , Data -> if at >= n
281 {
282 Ok(d)
283 }
284 else
285 {
286 let take = int_min d.left (n - at)
287 let room = d.max - d.size
288 if take > room
289 {
290 let body = bytes.append d.body (bytes.slice data at (at + room))
291 Ok(Dechunker(..d, phase: Full, body: body, size: d.max))
292 }
293 else
294 {
295 let body = bytes.append d.body (bytes.slice data at (at + take))
296 let left = d.left - take
297 let phase = if left == 0 { DataEnd } else { Data }
298 let d =
299 Dechunker( ..d
300 , phase: phase
301 , left: left
302 , body: body
303 , size: d.size + take
304 )
305 step d data (at + take)
306 }
307 }
308 , _ -> line_step d data at
309 }
310}
311
312/// A size line, the CRLF after a chunk's data, or a trailer line.
313fn line_step :: d: Dechunker data: Bytes at: Int -> Result<Dechunker, String>
314{
315 let rest = bytes.slice data at (bytes.length data)
316 let text = if bytes.length d.line == 0
317 {
318 rest
319 }
320 else
321 {
322 bytes.concat d.line rest
323 }
324 let end = crlf text 0 (bytes.length text)
325 if end < 0
326 {
327 if bytes.length text > max_line ()
328 {
329 Error("a chunk line too long")
330 }
331 else
332 {
333 Ok(Dechunker(..d, line: text))
334 }
335 }
336 else if end > max_line ()
337 {
338 Error("a chunk line too long")
339 }
340 else
341 {
342 let line = bytes.slice text 0 end
343 let d = Dechunker(..d, line: bytes.empty ())
344 use d <- result.try (after_line d line)
345 step d text (end + 2)
346 }
347}
348
349fn after_line :: d: Dechunker line: Bytes -> Result<Dechunker, String>
350{
351 match d.phase
352 { SizeLine -> match chunk_size line
353 { Ok(0) -> Ok(Dechunker(..d, phase: Trailer))
354 , Ok(size) -> Ok(Dechunker(..d, phase: Data, left: size))
355 , Error(e) -> Error(e)
356 }
357 , DataEnd -> if bytes.length line == 0
358 {
359 Ok(Dechunker(..d, phase: SizeLine))
360 }
361 else
362 {
363 Error("chunk data not followed by CRLF")
364 }
365 , _ -> if bytes.length line == 0
366 {
367 Ok(Dechunker(..d, phase: Done))
368 }
369 else
370 {
371 Ok(d)
372 }
373 }
374}
375
376/// The hex size before any `;extension`; at most 15 digits.
377fn chunk_size :: line: Bytes -> Result<Int, String>
378{
379 let semicolon = until_semicolon line 0
380 let digits =
381 string.trim (bytes.to_string_lossy (bytes.slice line 0 semicolon))
382 let chars = string.chars (string.to_lower digits)
383 if chars == [] || list.length chars > 15
384 {
385 Error("a bad chunk size")
386 }
387 else
388 {
389 list.fold
390 chars
391 Ok(0)
392 fn acc c
393 {
394 use n <- result.try acc
395 match string.find "0123456789abcdef" c
396 { Some(v) -> Ok(n * 16 + v), None -> Error("a bad chunk size") }
397 }
398 }
399}
400
401fn until_semicolon :: line: Bytes at: Int -> Int
402{
403 let b = bytes.read_u8 line at
404 if b < 0
405 {
406 bytes.length line
407 }
408 else if b == 59
409 {
410 at
411 }
412 else
413 {
414 until_semicolon line (at + 1)
415 }
416}
417
418fn crlf :: data: Bytes at: Int n: Int -> Int
419{
420 if at + 2 > n
421 {
422 -1
423 }
424 else if bytes.read_u8 data at == 13 && bytes.read_u8 data (at + 1) == 10
425 {
426 at
427 }
428 else
429 {
430 crlf data (at + 1) n
431 }
432}
433
434fn int_min :: a: Int b: Int -> Int
435{
436 if a < b
437 {
438 a
439 }
440 else
441 {
442 b
443 }
444}