hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

uri.ha (919B)


      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 = {
     22 	return uri {
     23 		scheme = strings::dup(u.scheme),
     24 
     25 		host = match (u.host) {
     26 		case let host: str =>
     27 			yield strings::dup(host);
     28 		case let ip: ip::addr =>
     29 			yield ip;
     30 		},
     31 		port = u.port,
     32 		userinfo = strings::dup(u.userinfo),
     33 
     34 		path = strings::dup(u.path),
     35 		query = strings::dup(u.query),
     36 		fragment = strings::dup(u.fragment),
     37 	};
     38 };
     39 
     40 // Frees resources associated with a [[uri]].
     41 export fn finish(u: *uri) void = {
     42 	free(u.scheme);
     43 	match (u.host) {
     44 	case let s: str =>
     45 		free(s);
     46 	case => void;
     47 	};
     48 	free(u.userinfo);
     49 	free(u.path);
     50 	free(u.query);
     51 	free(u.fragment);
     52 };