Debian Codegraph @

net/uri/query.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use io;
5 use memio;
6 use strings;
7
8 export type query_decoder = struct {
9 tokenizer: strings::tokenizer,
10 bufs: (memio::stream, memio::stream),
11 };
12
13 // Initializes a decoder for a query string. Use [[query_next]] to walk it. The
14 // caller must call [[query_finish]] once they're done using it.
15 export fn decodequery(q: const str) query_decoder = query_decoder {
16 tokenizer = strings::tokenize(q, "&"),
17 bufs = (memio::dynamic(), memio::dynamic()),
18 };
19
20 // Frees resources associated with the [[query_decoder]].
21 export fn query_finish(dec: *query_decoder) void = {
22 io::close(&dec.bufs.0)!;
23 io::close(&dec.bufs.1)!;
24 };
25
26 // Retrieves the next (key, value) pair from the query. The return value is
27 // borrowed from the decoder and will be replaced on the next call, use
28 // [[strings::dup]] to extend its lifetime.
29 export fn query_next(dec: *query_decoder) ((str, str) | invalid | nomem | void) = {
30 const tok = match (strings::next_token(&dec.tokenizer)) {
31 case let s: str =>
32 yield s;
33 case => return;
34 };
35
36 const raw = strings::cut(tok, "=");
37 memio::reset(&dec.bufs.0);
38 percent_decode_static(&dec.bufs.0, raw.0)?;
39 memio::reset(&dec.bufs.1);
40 percent_decode_static(&dec.bufs.1, raw.1)?;
41 return (
42 memio::string(&dec.bufs.0)!,
43 memio::string(&dec.bufs.1)!,
44 );
45 };
46
47 // Encodes (key, value) pairs into a URI query string. The result must be
48 // freed by the caller.
49 export fn encodequery(pairs: [](str, str)) (str | nomem) = {
50 const buf = memio::dynamic();
51 for (let i = 0z; i < len(pairs); i += 1) {
52 const pair = pairs[i];
53 if (i > 0 && memio::appendrune(&buf, '&') is io::error) {
54 return nomem;
55 };
56
57 assert(len(pair.0) > 0);
58 if (percent_encode(&buf, pair.0, unres_query_frag) is io::error) {
59 return nomem;
60 };
61 if (len(pair.1) > 0) {
62 if (memio::appendrune(&buf, '=') is io::error) {
63 return nomem;
64 };
65 if (percent_encode(&buf, pair.1, unres_query_frag)
66 is io::error) {
67 return nomem;
68 };
69 };
70 };
71
72 return memio::string(&buf)!;
73 };
74
75 @test fn decodequery() void = {
76 const u = parse("https://sr.ht/projects?search=%23risc-v&sort=longest-active&quantity=100%25")!;
77 defer finish(&u);
78
79 const query = decodequery(u.query);
80 defer query_finish(&query);
81 const pair = query_next(&query)! as (str, str);
82 assert(pair.0 == "search");
83 assert(pair.1 == "#risc-v");
84
85 const pair = query_next(&query)! as (str, str);
86 assert(pair.0 == "sort");
87 assert(pair.1 == "longest-active");
88
89 const pair = query_next(&query)! as (str, str);
90 assert(pair.0 == "quantity");
91 assert(pair.1 == "100%");
92 };
93
94 @test fn encodequery() void = {
95 const pairs = [
96 ("search", "#risc-v"),
97 ("sort", "longest-active"),
98 ("quantity", "100%")
99 ];
100 const encoded = encodequery(pairs)!;
101 defer free(encoded);
102
103 assert(encoded == "search=%23risc-v&sort=longest-active&quantity=100%25");
104 };