// Pure helpers of std/http (issue #179 §1e): reason phrases and the // path/query split of a request target. import std/bytes import std/http import std/io fn req :: target: String -> http.Request { http.Request("GET", target, [], bytes.empty ()) } pub fn main :: -> () / { // Reason phrases: the ones toki needs, and the fallback. io.println (http.reason 101) io.println (http.reason 405) io.println (http.reason 422) io.println (http.reason 599) // The target splits at its first `?`; later `?`s stay in the query. io.println (show (http.target_split "/notes")) io.println (show (http.target_split "/notes?since=3")) io.println (show (http.target_split "/notes?")) io.println (show (http.target_split "/notes?a=b?c=d")) io.println ( http.path (req "/notes?since=3") <> "|" <> http.query (req "/notes?since=3") ) // Segments: empty ones are dropped; decoding happens after splitting, // so %2F stays inside its segment. io.println (show (http.segments (req "/a//b/c%20d/e%2Ff?x=1"))) // Query parameters: the first `=` splits; a bare key has ""; decoding. io.println (show (http.query_params (req "/n?a=1&b=two+words&bare&c=x%3Dy"))) io.println (show (http.query_params (req "/n"))) // Header lookup is case-insensitive (ASCII), however the case is mixed. let headers = [("Host", "h"), ("content-TYPE", "t"), ("X-Request-Id", "r"), ("café", "c")] let r = http.Request("GET", "/", headers, bytes.empty ()) io.println (show (http.header r "host")) io.println (show (http.header r "Content-Type")) io.println (show (http.header r "CONTENT-TYPE")) io.println (show (http.header r "x-request-id")) io.println (show (http.header r "X-REQUEST-ID")) io.println (show (http.header r "missing")) io.println (show (http.header r "")) io.println (show (http.header r "cafÉ")) io.println (show (http.header r "café")) } //> Switching Protocols //> Method Not Allowed //> Unprocessable Content //> Status //> (/notes, ) //> (/notes, since=3) //> (/notes, ) //> (/notes, a=b?c=d) //> /notes|since=3 //> [a, b, c d, e/f] //> [(a, 1), (b, two words), (bare, ), (c, x=y)] //> [] //> Some(h) //> Some(t) //> Some(t) //> Some(r) //> Some(r) //> None //> None //> None //> Some(c)