tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

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

69 lignes · 2254 octetsBrut · Historique
1// Pure helpers of std/http (issue #179 §1e): reason phrases and the
2// path/query split of a request target.
3
4import std/bytes
5import std/http
6import std/io
7
8fn req :: target: String -> http.Request
9{
10 http.Request("GET", target, [], bytes.empty ())
11}
12
13pub fn main :: -> () / <io>
14{
15 // Reason phrases: the ones toki needs, and the fallback.
16 io.println (http.reason 101)
17 io.println (http.reason 405)
18 io.println (http.reason 422)
19 io.println (http.reason 599)
20 // The target splits at its first `?`; later `?`s stay in the query.
21 io.println (show (http.target_split "/notes"))
22 io.println (show (http.target_split "/notes?since=3"))
23 io.println (show (http.target_split "/notes?"))
24 io.println (show (http.target_split "/notes?a=b?c=d"))
25 io.println
26 ( http.path (req "/notes?since=3") <> "|"
27 <> http.query (req "/notes?since=3")
28 )
29 // Segments: empty ones are dropped; decoding happens after splitting,
30 // so %2F stays inside its segment.
31 io.println (show (http.segments (req "/a//b/c%20d/e%2Ff?x=1")))
32 // Query parameters: the first `=` splits; a bare key has ""; decoding.
33 io.println (show (http.query_params (req "/n?a=1&b=two+words&bare&c=x%3Dy")))
34 io.println (show (http.query_params (req "/n")))
35 // Header lookup is case-insensitive (ASCII), however the case is mixed.
36 let headers =
37 [("Host", "h"), ("content-TYPE", "t"), ("X-Request-Id", "r"), ("café", "c")]
38 let r = http.Request("GET", "/", headers, bytes.empty ())
39 io.println (show (http.header r "host"))
40 io.println (show (http.header r "Content-Type"))
41 io.println (show (http.header r "CONTENT-TYPE"))
42 io.println (show (http.header r "x-request-id"))
43 io.println (show (http.header r "X-REQUEST-ID"))
44 io.println (show (http.header r "missing"))
45 io.println (show (http.header r ""))
46 io.println (show (http.header r "cafÉ"))
47 io.println (show (http.header r "café"))
48}
49//> Switching Protocols
50//> Method Not Allowed
51//> Unprocessable Content
52//> Status
53//> (/notes, )
54//> (/notes, since=3)
55//> (/notes, )
56//> (/notes, a=b?c=d)
57//> /notes|since=3
58//> [a, b, c d, e/f]
59//> [(a, 1), (b, two words), (bare, ), (c, x=y)]
60//> []
61//> Some(h)
62//> Some(t)
63//> Some(t)
64//> Some(r)
65//> Some(r)
66//> None
67//> None
68//> None
69//> Some(c)