Something went wrong. Try again.
HTTP types and an I/O-free, zero-copy HTTP/1.1 request parser
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260(* Content codings and Accept-Encoding, RFC 9110 sections 8.4 and 12.5.3: the section's own examples, then the edges of the grammar and of the choice. *)
open Http
let coding = Alcotest.testable Content_coding.pp Content_coding.equallet all = [ `Identity; `Gzip; `Deflate; `Compress; `Br; `Zstd ]
(* Every name reads back as itself, in any case. *)let test_names_roundtrip () = List.iter (fun c -> let s = Content_coding.to_string c in Alcotest.(check (option coding)) s (Some c) (Content_coding.of_string s); Alcotest.(check (option coding)) (String.uppercase_ascii s) (Some c) (Content_coding.of_string (String.uppercase_ascii s))) all
(* RFC 9110 s.8.4.1: names are case-insensitive tokens, x-gzip is gzip and x-compress is compress (s.8.4.1.1, s.8.4.1.3); a name outside the registry is no coding. *)let test_registry_names () = List.iter (fun (s, expected) -> Alcotest.(check (option string)) s expected (Option.map Content_coding.to_string (Content_coding.of_string s))) [ ("gzip", Some "gzip"); ("GZip", Some "gzip"); ("x-gzip", Some "gzip"); ("deflate", Some "deflate"); ("compress", Some "compress"); ("x-compress", Some "compress"); ("br", Some "br"); ("zstd", Some "zstd"); ("identity", Some "identity"); ("lzma", None); ("", None); ]
let weight field c = Option.map (fun a -> Content_coding.quality a c) (Content_coding.accept_of_string field)
(* RFC 9110 s.12.4.2: every qvalue the grammar allows reads exactly, from 0 to 1 by thousandths, whatever the number of decimals written. *)let test_qvalue_grammar () = for n = 0 to 1000 do let digits = if n = 1000 then "1.000" else Fmt.str "0.%03d" n in Alcotest.(check (option int)) digits (Some n) (weight ("gzip;q=" ^ digits) `Gzip) done; List.iter (fun (q, w) -> Alcotest.(check (option int)) q w (weight ("gzip;q=" ^ q) `Gzip)) [ ("0", Some 0); ("0.", Some 0); ("0.5", Some 500); ("0.05", Some 50); ("1", Some 1000); ("1.", Some 1000); ("1.0", Some 1000); ("1.00", Some 1000); ("1.001", None); ("1.1", None); ("2", None); ("0.0000", None); ("-0", None); ("0;5", None); ("0.5x", None); ("", None); (" 0.5", None); ]
(* RFC 9110 s.12.4.2 and s.12.5.3: a weight in thousandths; an unnamed coding takes the weight of "*", or 0 without one, except identity, which is then the least preferred (1) unless the field excludes it. *)let test_weights () = List.iter (fun (field, c, w) -> Alcotest.(check (option int)) (field ^ " / " ^ Content_coding.to_string c) w (weight field c)) [ ("gzip", `Gzip, Some 1000); ("gzip;q=0.5", `Gzip, Some 500); ("GZIP ; Q=0.125", `Gzip, Some 125); ("gzip;q=1.000", `Gzip, Some 1000); ("gzip;q=0", `Gzip, Some 0); ("gzip", `Br, Some 0); ("gzip, *;q=0.2", `Br, Some 200); ("gzip", `Identity, Some 1); ("identity;q=0", `Identity, Some 0); ("*;q=0", `Identity, Some 0); ("*;q=0, identity;q=0.3", `Identity, Some 300); ("x-gzip;q=0.7", `Gzip, Some 700); (* RFC 9110 s.5.6.1: empty elements and unknown codings are ignored. *) (", gzip;q=0.4 ,, lzma", `Gzip, Some 400); ("", `Gzip, Some 0); (* Not the grammar: a weight above 1, four decimals, another parameter, an empty weight, a coding that is no token, no leading digit. *) ("gzip;q=1.5", `Gzip, None); ("gzip;q=0.1234", `Gzip, None); ("gzip;level=9", `Gzip, None); ("gzip;q=", `Gzip, None); ("gz ip", `Gzip, None); ("gzip;q=.5", `Gzip, None); ]
(* Whitespace may surround the semicolon (OWS ";" OWS) but not the "=". *)let test_ows () = Alcotest.(check (option int)) "around ;" (Some 300) (weight "gzip \t; \tq=0.3" `Gzip); Alcotest.(check (option int)) "before =" None (weight "gzip;q =0.3" `Gzip); Alcotest.(check (option int)) "two weights" None (weight "gzip;q=0.3;q=0.4" `Gzip)
(* The first element naming a coding wins over a later one, and over "*". *)let test_first_element_wins () = Alcotest.(check (option int)) "first of two" (Some 200) (weight "gzip;q=0.2, gzip;q=0.9" `Gzip); Alcotest.(check (option int)) "named over star" (Some 0) (weight "*;q=1, gzip;q=0" `Gzip); Alcotest.(check (option int)) "first star" (Some 100) (weight "*;q=0.1, *;q=0.9" `Br)
let negotiate field offered = Content_coding.negotiate (Headers.of_list [ ("Accept-Encoding", field) ]) offered
(* RFC 9110 s.12.5.3: the acceptable offered coding of highest weight, ties to the server's order, identity after every offered coding, and None (the 406 case) when not even identity is acceptable. *)let test_negotiate () = let pick ?field offered = let headers = match field with | None -> Headers.empty | Some v -> Headers.of_list [ ("accept-encoding", v) ] in Option.map Content_coding.to_string (Content_coding.negotiate headers offered) in List.iter (fun (field, offered, expected) -> Alcotest.(check (option string)) (Option.value field ~default:"no field") expected (pick ?field offered)) [ (* No field: any coding is acceptable, but the client may decode none. *) (None, [ `Gzip ], Some "identity"); (Some "", [ `Gzip; `Br ], Some "identity"); (* The section's own examples. *) (Some "compress, gzip", [ `Gzip ], Some "gzip"); (Some "*", [ `Br; `Gzip ], Some "br"); (Some "compress;q=0.5, gzip;q=1.0", [ `Compress; `Gzip ], Some "gzip"); (Some "gzip;q=1.0, identity; q=0.5, *;q=0", [ `Br; `Gzip ], Some "gzip"); (Some "gzip;q=1.0, identity; q=0.5, *;q=0", [ `Br ], Some "identity"); (* A browser's field, and the server's preference among equal weights. *) (Some "gzip, deflate, br, zstd", [ `Br; `Zstd; `Gzip ], Some "br"); (Some "gzip, deflate, br, zstd", [ `Gzip; `Br ], Some "gzip"); (Some "br;q=0.8, gzip", [ `Br; `Gzip ], Some "gzip"); (* Below identity loses to it; unnamed with no "*" is never sent. *) (Some "gzip;q=0.5, identity", [ `Gzip ], Some "identity"); (Some "gzip;q=0.5", [ `Gzip ], Some "gzip"); (Some "br", [ `Gzip ], Some "identity"); (Some "gzip;q=0", [ `Gzip ], Some "identity"); (* Identity excluded and nothing offered acceptable. *) (Some "identity;q=0", [ `Gzip ], None); (Some "*;q=0", [], None); (Some "br, identity;q=0", [ `Gzip ], None); (Some "br, identity;q=0", [ `Gzip; `Br ], Some "br"); (* RFC 9110 s.12.1: a field off the grammar is disregarded. *) (Some "gzip;q=2", [ `Gzip ], Some "identity"); ]
(* RFC 9110 s.5.3: several Accept-Encoding fields are one list. *)let test_several_fields () = Alcotest.(check (option coding)) "two fields" (Some `Br) (Content_coding.negotiate (Headers.of_list [ ("accept-encoding", "identity;q=0"); ("Accept-Encoding", "br") ]) [ `Gzip; `Br ])
(* An offered identity, or a coding offered twice, changes nothing. *)let test_offered_list () = Alcotest.(check (option coding)) "identity offered first" (Some `Gzip) (negotiate "gzip, identity" [ `Identity; `Gzip ]); Alcotest.(check (option coding)) "duplicates keep the first place" (Some `Br) (negotiate "*" [ `Br; `Gzip; `Br ]); Alcotest.(check (option coding)) "nothing offered" (Some `Identity) (negotiate "gzip, br" [])
(* A weight wins over the server's order; only a tie falls back to it. *)let test_weight_over_order () = Alcotest.(check (option coding)) "higher weight" (Some `Zstd) (negotiate "br;q=0.1, zstd;q=0.2, gzip;q=0.15" [ `Br; `Gzip; `Zstd ]); Alcotest.(check (option coding)) "tie" (Some `Gzip) (negotiate "br;q=0.5, gzip;q=0.5" [ `Gzip; `Br ])
(* The lowest acceptable weight still beats an identity the field leaves unnamed, and an excluded identity is never chosen. *)let test_identity_floor () = Alcotest.(check (option coding)) "q=0.001" (Some `Gzip) (negotiate "gzip;q=0.001" [ `Gzip ]); Alcotest.(check (option coding)) "identity named below" (Some `Gzip) (negotiate "gzip;q=0.002, identity;q=0.001" [ `Gzip ]); Alcotest.(check (option coding)) "identity named above" (Some `Identity) (negotiate "gzip;q=0.002, identity;q=0.003" [ `Gzip ]); Alcotest.(check (option coding)) "excluded by star" None (negotiate "*;q=0" [ `Gzip ])
(* A field the grammar refuses is disregarded whole, even when an earlier element of it would have read. *)let test_malformed_field () = Alcotest.(check (option coding)) "late defect" (Some `Identity) (negotiate "gzip, br;q=7" [ `Gzip ]); Alcotest.(check (option coding)) "empty coding with a weight" (Some `Identity) (negotiate ";q=0.5, gzip" [ `Gzip ])
let suite = ( "content_coding", [ Alcotest.test_case "names roundtrip" `Quick test_names_roundtrip; Alcotest.test_case "registry names" `Quick test_registry_names; Alcotest.test_case "weights" `Quick test_weights; Alcotest.test_case "negotiate" `Quick test_negotiate; Alcotest.test_case "several fields are one list" `Quick test_several_fields; Alcotest.test_case "qvalue grammar" `Quick test_qvalue_grammar; Alcotest.test_case "whitespace" `Quick test_ows; Alcotest.test_case "first element wins" `Quick test_first_element_wins; Alcotest.test_case "offered list" `Quick test_offered_list; Alcotest.test_case "weight over order" `Quick test_weight_over_order; Alcotest.test_case "identity floor" `Quick test_identity_floor; Alcotest.test_case "malformed field" `Quick test_malformed_field; ] )