Debian Codegraph @

net/uri/uri.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use net::ip;
5 use strings;
6
7 // Representation of a parsed URI.
8 export type uri = struct {
9 scheme: str,
10
11 host: (str | ip::addr),
12 port: u16,
13 userinfo: str,
14
15 path: str,
16 query: str,
17 fragment: str,
18 };
19
20 // Duplicates a [[uri]].
21 export fn dup(u: *uri) (uri | nomem) = {
22 let dup = uri {
23 host = "",
24 port = u.port,
25 ...
26 };
27 let success = false;
28 defer if (!success) finish(&dup);
29
30 dup.scheme = strings::dup(u.scheme)?;
31
32 dup.host = match (u.host) {
33 case let host: str =>
34 yield strings::dup(host)?;
35 case let ip: ip::addr =>
36 yield ip;
37 };
38 dup.port = u.port;
39 dup.userinfo = strings::dup(u.userinfo)?;
40
41 dup.path = strings::dup(u.path)?;
42 dup.query = strings::dup(u.query)?;
43 dup.fragment = strings::dup(u.fragment)?;
44
45 success = true;
46 return dup;
47 };
48
49 // Frees resources associated with a [[uri]].
50 export fn finish(u: *uri) void = {
51 free(u.scheme);
52 match (u.host) {
53 case let s: str =>
54 free(s);
55 case => void;
56 };
57 free(u.userinfo);
58 free(u.path);
59 free(u.query);
60 free(u.fragment);
61 };