diff options
Diffstat (limited to 'src/include/gnunet_uri_lib.h')
-rw-r--r-- | src/include/gnunet_uri_lib.h | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/include/gnunet_uri_lib.h b/src/include/gnunet_uri_lib.h new file mode 100644 index 000000000..d428bdd9a --- /dev/null +++ b/src/include/gnunet_uri_lib.h | |||
@@ -0,0 +1,122 @@ | |||
1 | /** | ||
2 | * Copyright (C) 2016 Jack Engqvist Johansson | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
5 | * of this software and associated documentation files (the "Software"), to deal | ||
6 | * in the Software without restriction, including without limitation the rights | ||
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
8 | * copies of the Software, and to permit persons to whom the Software is | ||
9 | * furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in all | ||
12 | * copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
20 | * SOFTWARE. | ||
21 | */ | ||
22 | #ifndef GNUNET_URI_LIB_H | ||
23 | #define GNUNET_URI_LIB_H | ||
24 | |||
25 | |||
26 | /** | ||
27 | * The struct where the parsed values will be stored: | ||
28 | * | ||
29 | * scheme ":" [ "//" ] [ username ":" password "@" ] host [ ":" port ] [ "/" ] [ path ] [ "?" query ] | ||
30 | * | ||
31 | * Note: to make sure that no strings are copied, the first slash "/" in the | ||
32 | * path will be used to null terminate the hostname if no port is supplied. | ||
33 | */ | ||
34 | struct GNUNET_Uri { | ||
35 | char *scheme; /* scheme, without ":" and "//" */ | ||
36 | char *username; /* username, default: NULL */ | ||
37 | char *password; /* password, default: NULL */ | ||
38 | char *host; /* hostname or IP address */ | ||
39 | int port; /* port, default: 0 */ | ||
40 | char *path; /* path, without leading "/", default: NULL */ | ||
41 | char *query; /* query, default: NULL */ | ||
42 | char *fragment; /* fragment, default: NULL */ | ||
43 | }; | ||
44 | |||
45 | |||
46 | /* A struct to hold the query string parameter values. */ | ||
47 | struct GNUNET_UriParam { | ||
48 | char *key; | ||
49 | char *val; | ||
50 | }; | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Parse a URL to a struct. | ||
55 | * | ||
56 | * The URL string should be in one of the following formats: | ||
57 | * | ||
58 | * Absolute URL: | ||
59 | * scheme ":" [ "//" ] [ username ":" password "@" ] host [ ":" port ] [ "/" ] [ path ] [ "?" query ] [ "#" fragment ] | ||
60 | * | ||
61 | * Relative URL: | ||
62 | * path [ "?" query ] [ "#" fragment ] | ||
63 | * | ||
64 | * The following parts will be parsed to the corresponding struct member. | ||
65 | * | ||
66 | * *url: a pointer to the struct where to store the parsed values. | ||
67 | * *url_str: a pointer to the url to be parsed (null terminated). The string | ||
68 | * will be modified. | ||
69 | * | ||
70 | * Returns 0 on success, otherwise -1. | ||
71 | */ | ||
72 | int | ||
73 | GNUNET_uri_parse (struct GNUNET_Uri *url, | ||
74 | char *url_str); | ||
75 | |||
76 | |||
77 | /** | ||
78 | * Split a path into several strings. | ||
79 | * | ||
80 | * No data is copied, the slashed are used as null terminators and then | ||
81 | * pointers to each path part will be stored in **parts. Double slashes will be | ||
82 | * treated as one. | ||
83 | * | ||
84 | * *path: the path to split. The string will be modified. | ||
85 | * **parts: a pointer to an array of (char *) where to store the result. | ||
86 | * max_parts: max number of parts to parse. | ||
87 | * | ||
88 | * Returns the number of parsed items. -1 on error. | ||
89 | */ | ||
90 | int | ||
91 | GNUNET_uri_split_path (char *path, | ||
92 | char **parts, | ||
93 | int max_parts); | ||
94 | |||
95 | |||
96 | /** | ||
97 | * Parse a query string into a key/value struct. | ||
98 | * | ||
99 | * The query string should be a null terminated string of parameters separated by | ||
100 | * a delimiter. Each parameter are checked for the equal sign character. If it | ||
101 | * appears in the parameter, it will be used as a null terminator and the part | ||
102 | * that comes after it will be the value of the parameter. | ||
103 | * | ||
104 | * No data are copied, the equal sign and delimiters are used as null | ||
105 | * terminators and then pointers to each parameter key and value will be stored | ||
106 | * in the yuarel_param struct. | ||
107 | * | ||
108 | * *query: the query string to parse. The string will be modified. | ||
109 | * delimiter: the character that separates the key/value pairs from eachother. | ||
110 | * *params: an array of (struct yuarel_param) where to store the result. | ||
111 | * max_values: max number of parameters to parse. | ||
112 | * | ||
113 | * Returns the number of parsed items. -1 on error. | ||
114 | */ | ||
115 | int | ||
116 | GNUNET_uri_parse_query (char *query, | ||
117 | char delimiter, | ||
118 | struct GNUNET_UriParam *params, | ||
119 | int max_params); | ||
120 | |||
121 | |||
122 | #endif /* GNUNET_URI_LIB_H */ | ||