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.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | // Heads and chunked bodies as RFC 9112 (§4, §5, §6.3, §7.1) writes them; |
| 2 | // the chunked encoder below is the test's own, written from §7.1. |
| 3 | |
| 4 | import gen |
| 5 | import gen/check |
| 6 | import response. |
| 7 | {type Dechunker, type Head, Chunked, Head, Length, NoBody, UntilClose} |
| 8 | import std/bytes |
| 9 | import std/list |
| 10 | import std/random.{type random} |
| 11 | import std/result |
| 12 | import std/string |
| 13 | import std/test.{assert, assert_equal} |
| 14 | |
| 15 | fn head_of :: text: String -> Result<Option<Head>, String> |
| 16 | { |
| 17 | response.head (bytes.from_string text) 65536 |
| 18 | } |
| 19 | |
| 20 | // ---- heads --------------------------------------------------------------------- |
| 21 | |
| 22 | @test |
| 23 | fn a_head_and_its_size :: |
| 24 | { |
| 25 | assert_equal |
| 26 | ( head_of |
| 27 | "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nX-A: b c \r\n\r\nbody" |
| 28 | ) |
| 29 | Ok(Some(Head(200, [("Content-Type", "text/html"), ("X-A", "b c")], 56))) |
| 30 | assert_equal (head_of "HTTP/1.0 404 \r\n\r\n") Ok(Some(Head(404, [], 17))) |
| 31 | assert_equal (head_of "HTTP/1.1 204\r\n\r\n") Ok(Some(Head(204, [], 16))) |
| 32 | } |
| 33 | |
| 34 | @test |
| 35 | fn an_incomplete_head_asks_for_more :: |
| 36 | { |
| 37 | assert_equal (head_of "") Ok(None) |
| 38 | assert_equal (head_of "HTTP/1.1 200 OK\r\n") Ok(None) |
| 39 | assert_equal (head_of "HTTP/1.1 200 OK\r\nA: b\r\n\r") Ok(None) |
| 40 | } |
| 41 | |
| 42 | @test |
| 43 | fn a_head_over_the_limit :: |
| 44 | { |
| 45 | let long = "HTTP/1.1 200 OK\r\nX: " <> string.repeat "a" 100 |
| 46 | assert |
| 47 | (result.is_error (response.head (bytes.from_string long) 64)) |
| 48 | "result.is_error (response.head (bytes.from_string long) 64)" |
| 49 | assert |
| 50 | ( result.is_error |
| 51 | (response.head (bytes.from_string (long <> "\r\n\r\n")) 64) |
| 52 | ) |
| 53 | "result.is_error (response.head (bytes.from_string (long <> rnrn)) 64)" |
| 54 | assert |
| 55 | (result.is_ok (response.head (bytes.from_string (long <> "\r\n\r\n")) 1000)) |
| 56 | "result.is_ok (response.head (bytes.from_string (long <> rnrn)) 1000)" |
| 57 | } |
| 58 | |
| 59 | @test |
| 60 | fn malformed_heads :: |
| 61 | { |
| 62 | let bad = fn text { result.is_error (head_of (text <> "\r\n\r\n")) } |
| 63 | assert (bad "HTTP/2 200 OK") "bad HTTP/2 200 OK" |
| 64 | assert (bad "HTTP/1.1 20 OK") "bad HTTP/1.1 20 OK" |
| 65 | assert (bad "HTTP/1.1 2000 OK") "bad HTTP/1.1 2000 OK" |
| 66 | assert (bad "HTTP/1.1 099 Low") "bad HTTP/1.1 099 Low" |
| 67 | assert (bad "HTTP/1.1 600 High") "bad HTTP/1.1 600 High" |
| 68 | assert (bad "HTTP/1.1 abc OK") "bad HTTP/1.1 abc OK" |
| 69 | assert (bad "ICY 200 OK") "bad ICY 200 OK" |
| 70 | assert (bad "HTTP/1.1 200 OK\r\nNo-Colon") "bad HTTP/1.1 200 OKrnNo-Colon" |
| 71 | assert |
| 72 | (bad "HTTP/1.1 200 OK\r\nSpace : before") |
| 73 | "bad HTTP/1.1 200 OKrnSpace : before" |
| 74 | assert |
| 75 | (bad "HTTP/1.1 200 OK\r\n: empty name") |
| 76 | "bad HTTP/1.1 200 OKrn: empty name" |
| 77 | assert |
| 78 | (bad "HTTP/1.1 200 OK\r\nA: b\r\n folded") |
| 79 | "bad HTTP/1.1 200 OKrnA: brn folded" |
| 80 | assert (bad "HTTP/1.1 200 OK\r\nA: b\rc") "bad HTTP/1.1 200 OKrnA: brc" |
| 81 | assert (bad "HTTP/1.1 200 OK\r\nA: b\u{0}") "bad HTTP/1.1 200 OKrnA: bu0" |
| 82 | } |
| 83 | |
| 84 | @test |
| 85 | fn a_header_by_any_case :: |
| 86 | { |
| 87 | let headers = [("Location", "/a"), ("location", "/b")] |
| 88 | assert_equal (response.header headers "LOCATION") Some("/a") |
| 89 | assert_equal (response.header headers "etag") None |
| 90 | } |
| 91 | |
| 92 | // ---- framing ------------------------------------------------------------------- |
| 93 | |
| 94 | fn framed :: |
| 95 | status: Int |
| 96 | headers: List<(String, String)> |
| 97 | -> Result<response.Framing, String> |
| 98 | { |
| 99 | response.framing (Head(status, headers, 0)) |
| 100 | } |
| 101 | |
| 102 | @test |
| 103 | fn framing :: |
| 104 | { |
| 105 | assert_equal (framed 200 []) Ok(UntilClose) |
| 106 | assert_equal (framed 200 [("Content-Length", "12")]) Ok(Length(12)) |
| 107 | assert_equal (framed 200 [("content-length", "0")]) Ok(Length(0)) |
| 108 | assert_equal |
| 109 | (framed 200 [("Content-Length", "5, 5"), ("Content-Length", "5")]) |
| 110 | Ok(Length(5)) |
| 111 | assert_equal (framed 200 [("Transfer-Encoding", "Chunked")]) Ok(Chunked) |
| 112 | assert_equal |
| 113 | (framed 200 [("Transfer-Encoding", "chunked"), ("Content-Length", "3")]) |
| 114 | Ok(Chunked) |
| 115 | assert_equal (framed 204 [("Content-Length", "3")]) Ok(NoBody) |
| 116 | assert_equal (framed 304 []) Ok(NoBody) |
| 117 | assert_equal (framed 103 []) Ok(NoBody) |
| 118 | assert_equal (framed 302 [("Content-Length", "3")]) Ok(Length(3)) |
| 119 | } |
| 120 | |
| 121 | @test |
| 122 | fn framing_that_is_refused :: |
| 123 | { |
| 124 | let refused = fn headers { result.is_error (framed 200 headers) } |
| 125 | assert |
| 126 | (refused [("Content-Length", "5"), ("Content-Length", "6")]) |
| 127 | "refused [(Content-Length, 5), (Content-Length, 6)]" |
| 128 | assert |
| 129 | (refused [("Content-Length", "5, 6")]) |
| 130 | "refused [(Content-Length, 5, 6)]" |
| 131 | assert (refused [("Content-Length", "-1")]) "refused [(Content-Length, -1)]" |
| 132 | assert (refused [("Content-Length", "+5")]) "refused [(Content-Length, +5)]" |
| 133 | assert (refused [("Content-Length", "")]) "refused [(Content-Length, )]" |
| 134 | assert |
| 135 | (refused [("Content-Length", "0x10")]) |
| 136 | "refused [(Content-Length, 0x10)]" |
| 137 | assert |
| 138 | (refused [("Content-Length", "99999999999999999999")]) |
| 139 | "refused [(Content-Length, 99999999999999999999)]" |
| 140 | assert |
| 141 | (refused [("Transfer-Encoding", "gzip, chunked")]) |
| 142 | "refused [(Transfer-Encoding, gzip, chunked)]" |
| 143 | assert |
| 144 | (refused [("Transfer-Encoding", "gzip")]) |
| 145 | "refused [(Transfer-Encoding, gzip)]" |
| 146 | assert |
| 147 | ( refused |
| 148 | [("Transfer-Encoding", "chunked"), ("Transfer-Encoding", "chunked")] |
| 149 | ) |
| 150 | "refused [(Transfer-Encoding, chunked), (Transfer-Encoding, chunked)]" |
| 151 | } |
| 152 | |
| 153 | // ---- chunked bodies ------------------------------------------------------------ |
| 154 | |
| 155 | fn decoded :: text: String max: Int -> Result<(String, Bool, Bool), String> |
| 156 | { |
| 157 | use d <- |
| 158 | result.try (response.feed (response.dechunker max) (bytes.from_string text)) |
| 159 | let (body, cut) = response.body d |
| 160 | Ok((bytes.to_string_lossy body, cut, response.is_done d)) |
| 161 | } |
| 162 | |
| 163 | @test |
| 164 | fn a_chunked_body :: |
| 165 | { |
| 166 | assert_equal |
| 167 | ( decoded |
| 168 | "4\r\nWiki\r\n5;ext=1\r\npedia\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n" |
| 169 | 100 |
| 170 | ) |
| 171 | Ok(("Wikipedia in\r\n\r\nchunks.", False, True)) |
| 172 | assert_equal |
| 173 | (decoded "3\r\nabc\r\n0\r\nExpires: never\r\nX: y\r\n\r\n" 100) |
| 174 | Ok(("abc", False, True)) |
| 175 | assert_equal (decoded "0\r\n\r\n" 100) Ok(("", False, True)) |
| 176 | } |
| 177 | |
| 178 | @test |
| 179 | fn an_unfinished_chunked_body_is_not_done :: |
| 180 | { |
| 181 | assert_equal (decoded "3\r\nab" 100) Ok(("ab", False, False)) |
| 182 | assert_equal (decoded "3\r\nabc\r\n0\r\n" 100) Ok(("abc", False, False)) |
| 183 | assert_equal (decoded "" 100) Ok(("", False, False)) |
| 184 | } |
| 185 | |
| 186 | @test |
| 187 | fn a_chunked_body_over_the_limit_is_cut :: |
| 188 | { |
| 189 | assert_equal |
| 190 | (decoded "4\r\nWiki\r\n5\r\npedia\r\n" 6) |
| 191 | Ok(("Wikipe", True, True)) |
| 192 | assert_equal (decoded "4\r\nWiki\r\n0\r\n\r\n" 4) Ok(("Wiki", False, True)) |
| 193 | assert_equal (decoded "4\r\nWiki\r\n1\r\n!" 4) Ok(("Wiki", True, True)) |
| 194 | } |
| 195 | |
| 196 | @test |
| 197 | fn taking_hands_out_data_as_it_is_decoded_under_one_limit :: |
| 198 | { |
| 199 | let feed = fn d text |
| 200 | { |
| 201 | result.expect (response.feed d (bytes.from_string text)) text |
| 202 | } |
| 203 | let d = feed (response.dechunker 6) "4\r\nWi" |
| 204 | let (first, d) = response.take d |
| 205 | assert_equal (bytes.to_string_lossy first) "Wi" |
| 206 | let d = feed d "ki\r\n5\r\npedia\r\n" |
| 207 | let (second, d) = response.take d |
| 208 | // The limit counts what was taken: 6 bytes in all. |
| 209 | assert_equal (bytes.to_string_lossy second) "kipe" |
| 210 | assert (response.is_done d) "cut at the limit" |
| 211 | let (rest, cut) = response.body d |
| 212 | assert_equal (bytes.length rest) 0 |
| 213 | assert cut "cut" |
| 214 | } |
| 215 | |
| 216 | @test |
| 217 | fn malformed_chunked_bodies :: |
| 218 | { |
| 219 | let bad = fn text { result.is_error (decoded text 100) } |
| 220 | assert (bad "x\r\n") "bad xrn" |
| 221 | assert (bad "\r\n") "bad rn" |
| 222 | assert (bad "-1\r\n") "bad -1rn" |
| 223 | assert (bad "1000000000000000\r\n") "bad 1000000000000000rn" |
| 224 | assert (bad "3\r\nabcd\r\n") "bad 3rnabcdrn" |
| 225 | assert (bad "3\r\nabc0\r\n\r\n") "bad 3rnabc0rnrn" |
| 226 | assert (bad (string.repeat "1" 5000)) "bad (string.repeat 1 5000)" |
| 227 | assert |
| 228 | (bad ("0\r\n" <> string.repeat "X" 5000)) |
| 229 | "bad (0rn <> string.repeat X 5000)" |
| 230 | } |
| 231 | |
| 232 | // ---- properties ---------------------------------------------------------------- |
| 233 | |
| 234 | fn hex :: n: Int -> String |
| 235 | { |
| 236 | let digits = "0123456789abcdef" |
| 237 | let last = string.slice digits (n % 16) (n % 16 + 1) |
| 238 | if n < 16 |
| 239 | { |
| 240 | last |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | hex (n / 16) <> last |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /// `body` in chunks of the given sizes (the rest in one more chunk), |
| 249 | /// then the last chunk, and maybe a trailer. |
| 250 | fn chunked :: body: Bytes sizes: List<Int> trailer: Bool -> Bytes |
| 251 | { |
| 252 | let frames = |
| 253 | list.flat_map |
| 254 | (cut body 0 sizes) |
| 255 | fn p |
| 256 | { |
| 257 | [ bytes.from_string (hex (bytes.length p) <> "\r\n") |
| 258 | , p |
| 259 | , bytes.from_string "\r\n" |
| 260 | ] |
| 261 | } |
| 262 | let last = if trailer { "0\r\nX-T: 1\r\n\r\n" } else { "0\r\n\r\n" } |
| 263 | bytes.concat_all (list.append frames [bytes.from_string last]) |
| 264 | } |
| 265 | |
| 266 | fn cut :: body: Bytes at: Int sizes: List<Int> -> List<Bytes> |
| 267 | { |
| 268 | let n = bytes.length body |
| 269 | if at >= n |
| 270 | { |
| 271 | [] |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | match sizes |
| 276 | { [size, ..rest] -> [ bytes.slice body at (at + size) |
| 277 | , ..cut body (at + size) rest |
| 278 | ] |
| 279 | , [] -> [bytes.slice body at n] |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /// `data` fed in pieces ending at `splits`. |
| 285 | fn fed :: |
| 286 | d: Dechunker |
| 287 | data: Bytes |
| 288 | at: Int |
| 289 | splits: List<Int> |
| 290 | -> Result<Dechunker, String> |
| 291 | { |
| 292 | match splits |
| 293 | { [] -> response.feed d (bytes.slice data at (bytes.length data)) |
| 294 | , [end, ..rest] -> |
| 295 | { |
| 296 | use d <- result.try (response.feed d (bytes.slice data at end)) |
| 297 | fed d data end rest |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | @derive Debug |
| 303 | type Case |
| 304 | { Case( |
| 305 | body: Bytes |
| 306 | , sizes: List<Int> |
| 307 | , trailer: Bool |
| 308 | , splits: List<Int> |
| 309 | , max: Int |
| 310 | ) |
| 311 | } |
| 312 | |
| 313 | fn any_case :: -> Case / <random> |
| 314 | { |
| 315 | let body = |
| 316 | bytes.from_list |
| 317 | ( gen.list |
| 318 | ~min: 0 |
| 319 | ~max: 200 |
| 320 | fn { gen.pick [13, 10, 48, 59, 0, 255, gen.int ~low: 0 ~high: 255] } |
| 321 | ) |
| 322 | let sizes = gen.list ~min: 0 ~max: 6 fn { gen.int ~low: 1 ~high: 40 } |
| 323 | let trailer = gen.bool () |
| 324 | let encoded = bytes.length (chunked body sizes trailer) |
| 325 | let splits = |
| 326 | list.sort (gen.list ~min: 0 ~max: 8 fn { gen.int ~low: 0 ~high: encoded }) |
| 327 | Case(body, sizes, trailer, splits, gen.int ~low: 0 ~high: 250) |
| 328 | } |
| 329 | |
| 330 | @test |
| 331 | fn decoding_undoes_encoding_at_any_split :: -> () / <random> |
| 332 | { |
| 333 | check.property |
| 334 | ~runs: 400 |
| 335 | any_case |
| 336 | fn c |
| 337 | { |
| 338 | let encoded = chunked c.body c.sizes c.trailer |
| 339 | match fed (response.dechunker 1000) encoded 0 c.splits |
| 340 | { Ok(d) -> response.is_done d && response.body d == (c.body, False) |
| 341 | , Error(_) -> False |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | @test |
| 347 | fn a_body_is_cut_at_the_limit :: -> () / <random> |
| 348 | { |
| 349 | check.property |
| 350 | ~runs: 400 |
| 351 | any_case |
| 352 | fn c |
| 353 | { |
| 354 | let encoded = chunked c.body c.sizes c.trailer |
| 355 | let n = bytes.length c.body |
| 356 | match fed (response.dechunker c.max) encoded 0 c.splits |
| 357 | { Ok(d) -> |
| 358 | response.is_done d |
| 359 | && if n > c.max |
| 360 | { |
| 361 | response.body d == (bytes.slice c.body 0 c.max, True) |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | response.body d == (c.body, False) |
| 366 | } |
| 367 | , Error(_) -> False |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | @test |
| 373 | fn a_prefix_is_never_done_and_never_wrong :: -> () / <random> |
| 374 | { |
| 375 | check.property |
| 376 | ~runs: 300 |
| 377 | any_case |
| 378 | fn c |
| 379 | { |
| 380 | let encoded = chunked c.body c.sizes c.trailer |
| 381 | let end = match c.splits { [s, ..] -> s, [] -> 0 } |
| 382 | let short = bytes.length encoded - 1 |
| 383 | let prefix = bytes.slice encoded 0 (if end < short { end } else { short }) |
| 384 | match response.feed (response.dechunker 1000) prefix |
| 385 | { Ok(d) -> |
| 386 | { |
| 387 | let (body, _) = response.body d |
| 388 | !response.is_done d |
| 389 | && bytes.slice c.body 0 (bytes.length body) == body |
| 390 | } |
| 391 | , Error(_) -> False |
| 392 | } |
| 393 | } |
| 394 | } |