]>
Commit | Line | Data |
---|---|---|
ca0defb9 PB |
1 | /** |
2 | * uri.c: set of generic URI related routines | |
3 | * | |
4 | * Reference: RFCs 3986, 2732 and 2373 | |
5 | * | |
6 | * Copyright (C) 1998-2003 Daniel Veillard. All Rights Reserved. | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * DANIEL VEILLARD BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
22 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
24 | * | |
25 | * Except as contained in this notice, the name of Daniel Veillard shall not | |
26 | * be used in advertising or otherwise to promote the sale, use or other | |
27 | * dealings in this Software without prior written authorization from him. | |
28 | * | |
29 | * [email protected] | |
30 | * | |
31 | ** | |
32 | * | |
33 | * Copyright (C) 2007, 2009-2010 Red Hat, Inc. | |
34 | * | |
35 | * This library is free software; you can redistribute it and/or | |
36 | * modify it under the terms of the GNU Lesser General Public | |
37 | * License as published by the Free Software Foundation; either | |
38 | * version 2.1 of the License, or (at your option) any later version. | |
39 | * | |
40 | * This library is distributed in the hope that it will be useful, | |
41 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
42 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
43 | * Lesser General Public License for more details. | |
44 | * | |
45 | * You should have received a copy of the GNU Lesser General Public | |
46 | * License along with this library; if not, write to the Free Software | |
47 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
48 | * | |
49 | * Authors: | |
50 | * Richard W.M. Jones <[email protected]> | |
51 | * | |
52 | */ | |
53 | ||
aafd7584 | 54 | #include "qemu/osdep.h" |
ca0defb9 | 55 | |
1de7afc9 | 56 | #include "qemu/uri.h" |
ca0defb9 PB |
57 | |
58 | static void uri_clean(URI *uri); | |
59 | ||
60 | /* | |
61 | * Old rule from 2396 used in legacy handling code | |
62 | * alpha = lowalpha | upalpha | |
63 | */ | |
64 | #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x)) | |
65 | ||
ca0defb9 PB |
66 | /* |
67 | * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | | |
68 | * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | | |
69 | * "u" | "v" | "w" | "x" | "y" | "z" | |
70 | */ | |
71 | ||
72 | #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z')) | |
73 | ||
74 | /* | |
75 | * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | | |
76 | * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | | |
77 | * "U" | "V" | "W" | "X" | "Y" | "Z" | |
78 | */ | |
79 | #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z')) | |
80 | ||
81 | #ifdef IS_DIGIT | |
82 | #undef IS_DIGIT | |
83 | #endif | |
84 | /* | |
85 | * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | |
86 | */ | |
87 | #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9')) | |
88 | ||
89 | /* | |
90 | * alphanum = alpha | digit | |
91 | */ | |
92 | ||
93 | #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x)) | |
94 | ||
95 | /* | |
96 | * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" | |
97 | */ | |
98 | ||
be95adaf SH |
99 | #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \ |
100 | ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \ | |
ca0defb9 PB |
101 | ((x) == '(') || ((x) == ')')) |
102 | ||
103 | /* | |
104 | * unwise = "{" | "}" | "|" | "\" | "^" | "`" | |
105 | */ | |
106 | ||
be95adaf SH |
107 | #define IS_UNWISE(p) \ |
108 | (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \ | |
109 | ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \ | |
110 | ((*(p) == ']')) || ((*(p) == '`'))) | |
ca0defb9 PB |
111 | /* |
112 | * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," | | |
113 | * "[" | "]" | |
114 | */ | |
115 | ||
be95adaf SH |
116 | #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \ |
117 | ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \ | |
118 | ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \ | |
119 | ((x) == ']')) | |
ca0defb9 PB |
120 | |
121 | /* | |
122 | * unreserved = alphanum | mark | |
123 | */ | |
124 | ||
125 | #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x)) | |
126 | ||
127 | /* | |
128 | * Skip to next pointer char, handle escaped sequences | |
129 | */ | |
130 | ||
be95adaf | 131 | #define NEXT(p) ((*p == '%') ? p += 3 : p++) |
ca0defb9 PB |
132 | |
133 | /* | |
134 | * Productions from the spec. | |
135 | * | |
136 | * authority = server | reg_name | |
137 | * reg_name = 1*( unreserved | escaped | "$" | "," | | |
138 | * ";" | ":" | "@" | "&" | "=" | "+" ) | |
139 | * | |
140 | * path = [ abs_path | opaque_part ] | |
141 | */ | |
142 | ||
ca0defb9 | 143 | /************************************************************************ |
be95adaf SH |
144 | * * |
145 | * RFC 3986 parser * | |
146 | * * | |
ca0defb9 PB |
147 | ************************************************************************/ |
148 | ||
149 | #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9')) | |
be95adaf | 150 | #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \ |
ca0defb9 | 151 | ((*(p) >= 'A') && (*(p) <= 'Z'))) |
be95adaf SH |
152 | #define ISA_HEXDIG(p) \ |
153 | (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \ | |
154 | ((*(p) >= 'A') && (*(p) <= 'F'))) | |
ca0defb9 PB |
155 | |
156 | /* | |
157 | * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" | |
158 | * / "*" / "+" / "," / ";" / "=" | |
159 | */ | |
be95adaf SH |
160 | #define ISA_SUB_DELIM(p) \ |
161 | (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \ | |
162 | ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \ | |
163 | ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \ | |
164 | ((*(p) == '=')) || ((*(p) == '\''))) | |
ca0defb9 PB |
165 | |
166 | /* | |
167 | * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" | |
168 | */ | |
be95adaf SH |
169 | #define ISA_GEN_DELIM(p) \ |
170 | (((*(p) == ':')) || ((*(p) == '/')) || ((*(p) == '?')) || \ | |
171 | ((*(p) == '#')) || ((*(p) == '[')) || ((*(p) == ']')) || \ | |
172 | ((*(p) == '@'))) | |
ca0defb9 PB |
173 | |
174 | /* | |
175 | * reserved = gen-delims / sub-delims | |
176 | */ | |
177 | #define ISA_RESERVED(p) (ISA_GEN_DELIM(p) || (ISA_SUB_DELIM(p))) | |
178 | ||
179 | /* | |
180 | * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" | |
181 | */ | |
be95adaf SH |
182 | #define ISA_UNRESERVED(p) \ |
183 | ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \ | |
184 | ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~'))) | |
ca0defb9 PB |
185 | |
186 | /* | |
187 | * pct-encoded = "%" HEXDIG HEXDIG | |
188 | */ | |
be95adaf SH |
189 | #define ISA_PCT_ENCODED(p) \ |
190 | ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2))) | |
ca0defb9 PB |
191 | |
192 | /* | |
193 | * pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | |
194 | */ | |
be95adaf SH |
195 | #define ISA_PCHAR(p) \ |
196 | (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \ | |
197 | ((*(p) == ':')) || ((*(p) == '@'))) | |
ca0defb9 PB |
198 | |
199 | /** | |
200 | * rfc3986_parse_scheme: | |
201 | * @uri: pointer to an URI structure | |
202 | * @str: pointer to the string to analyze | |
203 | * | |
204 | * Parse an URI scheme | |
205 | * | |
206 | * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) | |
207 | * | |
208 | * Returns 0 or the error code | |
209 | */ | |
be95adaf SH |
210 | static int rfc3986_parse_scheme(URI *uri, const char **str) |
211 | { | |
ca0defb9 PB |
212 | const char *cur; |
213 | ||
a1515161 | 214 | if (str == NULL) { |
42fa2725 | 215 | return -1; |
a1515161 | 216 | } |
ca0defb9 PB |
217 | |
218 | cur = *str; | |
a1515161 | 219 | if (!ISA_ALPHA(cur)) { |
42fa2725 | 220 | return 2; |
a1515161 | 221 | } |
ca0defb9 | 222 | cur++; |
be95adaf | 223 | while (ISA_ALPHA(cur) || ISA_DIGIT(cur) || (*cur == '+') || (*cur == '-') || |
a1515161 | 224 | (*cur == '.')) { |
be95adaf | 225 | cur++; |
a1515161 | 226 | } |
ca0defb9 | 227 | if (uri != NULL) { |
44c2286b | 228 | g_free(uri->scheme); |
be95adaf | 229 | uri->scheme = g_strndup(*str, cur - *str); |
ca0defb9 PB |
230 | } |
231 | *str = cur; | |
42fa2725 | 232 | return 0; |
ca0defb9 PB |
233 | } |
234 | ||
235 | /** | |
236 | * rfc3986_parse_fragment: | |
237 | * @uri: pointer to an URI structure | |
238 | * @str: pointer to the string to analyze | |
239 | * | |
240 | * Parse the query part of an URI | |
241 | * | |
242 | * fragment = *( pchar / "/" / "?" ) | |
243 | * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']' | |
244 | * in the fragment identifier but this is used very broadly for | |
245 | * xpointer scheme selection, so we are allowing it here to not break | |
246 | * for example all the DocBook processing chains. | |
247 | * | |
248 | * Returns 0 or the error code | |
249 | */ | |
be95adaf | 250 | static int rfc3986_parse_fragment(URI *uri, const char **str) |
ca0defb9 PB |
251 | { |
252 | const char *cur; | |
253 | ||
a1515161 | 254 | if (str == NULL) { |
42fa2725 | 255 | return -1; |
a1515161 | 256 | } |
ca0defb9 PB |
257 | |
258 | cur = *str; | |
259 | ||
260 | while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') || | |
261 | (*cur == '[') || (*cur == ']') || | |
a1515161 | 262 | ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) { |
ca0defb9 | 263 | NEXT(cur); |
a1515161 | 264 | } |
ca0defb9 | 265 | if (uri != NULL) { |
44c2286b | 266 | g_free(uri->fragment); |
a1515161 | 267 | if (uri->cleanup & 2) { |
be95adaf | 268 | uri->fragment = g_strndup(*str, cur - *str); |
a1515161 | 269 | } else { |
be95adaf | 270 | uri->fragment = uri_string_unescape(*str, cur - *str, NULL); |
a1515161 | 271 | } |
ca0defb9 PB |
272 | } |
273 | *str = cur; | |
42fa2725 | 274 | return 0; |
ca0defb9 PB |
275 | } |
276 | ||
277 | /** | |
278 | * rfc3986_parse_query: | |
279 | * @uri: pointer to an URI structure | |
280 | * @str: pointer to the string to analyze | |
281 | * | |
282 | * Parse the query part of an URI | |
283 | * | |
284 | * query = *uric | |
285 | * | |
286 | * Returns 0 or the error code | |
287 | */ | |
be95adaf | 288 | static int rfc3986_parse_query(URI *uri, const char **str) |
ca0defb9 PB |
289 | { |
290 | const char *cur; | |
291 | ||
a1515161 | 292 | if (str == NULL) { |
42fa2725 | 293 | return -1; |
a1515161 | 294 | } |
ca0defb9 PB |
295 | |
296 | cur = *str; | |
297 | ||
298 | while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') || | |
a1515161 | 299 | ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) { |
ca0defb9 | 300 | NEXT(cur); |
a1515161 | 301 | } |
ca0defb9 | 302 | if (uri != NULL) { |
44c2286b | 303 | g_free(uri->query); |
be95adaf | 304 | uri->query = g_strndup(*str, cur - *str); |
ca0defb9 PB |
305 | } |
306 | *str = cur; | |
42fa2725 | 307 | return 0; |
ca0defb9 PB |
308 | } |
309 | ||
310 | /** | |
311 | * rfc3986_parse_port: | |
312 | * @uri: pointer to an URI structure | |
313 | * @str: the string to analyze | |
314 | * | |
315 | * Parse a port part and fills in the appropriate fields | |
316 | * of the @uri structure | |
317 | * | |
318 | * port = *DIGIT | |
319 | * | |
320 | * Returns 0 or the error code | |
321 | */ | |
be95adaf | 322 | static int rfc3986_parse_port(URI *uri, const char **str) |
ca0defb9 PB |
323 | { |
324 | const char *cur = *str; | |
2b212330 | 325 | int port = 0; |
ca0defb9 PB |
326 | |
327 | if (ISA_DIGIT(cur)) { | |
2b212330 HR |
328 | while (ISA_DIGIT(cur)) { |
329 | port = port * 10 + (*cur - '0'); | |
330 | if (port > 65535) { | |
331 | return 1; | |
332 | } | |
333 | cur++; | |
334 | } | |
335 | if (uri) { | |
336 | uri->port = port; | |
337 | } | |
338 | *str = cur; | |
339 | return 0; | |
ca0defb9 | 340 | } |
2b212330 | 341 | return 1; |
ca0defb9 PB |
342 | } |
343 | ||
344 | /** | |
345 | * rfc3986_parse_user_info: | |
346 | * @uri: pointer to an URI structure | |
347 | * @str: the string to analyze | |
348 | * | |
736a83fa | 349 | * Parse a user information part and fill in the appropriate fields |
ca0defb9 PB |
350 | * of the @uri structure |
351 | * | |
352 | * userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) | |
353 | * | |
354 | * Returns 0 or the error code | |
355 | */ | |
be95adaf | 356 | static int rfc3986_parse_user_info(URI *uri, const char **str) |
ca0defb9 PB |
357 | { |
358 | const char *cur; | |
359 | ||
360 | cur = *str; | |
be95adaf | 361 | while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur) || |
a1515161 | 362 | (*cur == ':')) { |
be95adaf | 363 | NEXT(cur); |
a1515161 | 364 | } |
ca0defb9 | 365 | if (*cur == '@') { |
be95adaf | 366 | if (uri != NULL) { |
44c2286b | 367 | g_free(uri->user); |
a1515161 | 368 | if (uri->cleanup & 2) { |
be95adaf | 369 | uri->user = g_strndup(*str, cur - *str); |
a1515161 | 370 | } else { |
be95adaf | 371 | uri->user = uri_string_unescape(*str, cur - *str, NULL); |
a1515161 | 372 | } |
be95adaf SH |
373 | } |
374 | *str = cur; | |
42fa2725 | 375 | return 0; |
be95adaf | 376 | } |
42fa2725 | 377 | return 1; |
ca0defb9 PB |
378 | } |
379 | ||
380 | /** | |
381 | * rfc3986_parse_dec_octet: | |
382 | * @str: the string to analyze | |
383 | * | |
384 | * dec-octet = DIGIT ; 0-9 | |
385 | * / %x31-39 DIGIT ; 10-99 | |
386 | * / "1" 2DIGIT ; 100-199 | |
387 | * / "2" %x30-34 DIGIT ; 200-249 | |
388 | * / "25" %x30-35 ; 250-255 | |
389 | * | |
390 | * Skip a dec-octet. | |
391 | * | |
392 | * Returns 0 if found and skipped, 1 otherwise | |
393 | */ | |
be95adaf SH |
394 | static int rfc3986_parse_dec_octet(const char **str) |
395 | { | |
ca0defb9 PB |
396 | const char *cur = *str; |
397 | ||
a1515161 | 398 | if (!(ISA_DIGIT(cur))) { |
42fa2725 | 399 | return 1; |
a1515161 SH |
400 | } |
401 | if (!ISA_DIGIT(cur + 1)) { | |
be95adaf | 402 | cur++; |
a1515161 | 403 | } else if ((*cur != '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur + 2))) { |
be95adaf | 404 | cur += 2; |
a1515161 | 405 | } else if ((*cur == '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur + 2))) { |
be95adaf | 406 | cur += 3; |
a1515161 SH |
407 | } else if ((*cur == '2') && (*(cur + 1) >= '0') && (*(cur + 1) <= '4') && |
408 | (ISA_DIGIT(cur + 2))) { | |
be95adaf | 409 | cur += 3; |
a1515161 SH |
410 | } else if ((*cur == '2') && (*(cur + 1) == '5') && (*(cur + 2) >= '0') && |
411 | (*(cur + 1) <= '5')) { | |
be95adaf | 412 | cur += 3; |
a1515161 | 413 | } else { |
42fa2725 | 414 | return 1; |
a1515161 | 415 | } |
ca0defb9 | 416 | *str = cur; |
42fa2725 | 417 | return 0; |
ca0defb9 PB |
418 | } |
419 | /** | |
420 | * rfc3986_parse_host: | |
421 | * @uri: pointer to an URI structure | |
422 | * @str: the string to analyze | |
423 | * | |
424 | * Parse an host part and fills in the appropriate fields | |
425 | * of the @uri structure | |
426 | * | |
427 | * host = IP-literal / IPv4address / reg-name | |
428 | * IP-literal = "[" ( IPv6address / IPvFuture ) "]" | |
429 | * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet | |
430 | * reg-name = *( unreserved / pct-encoded / sub-delims ) | |
431 | * | |
432 | * Returns 0 or the error code | |
433 | */ | |
be95adaf | 434 | static int rfc3986_parse_host(URI *uri, const char **str) |
ca0defb9 PB |
435 | { |
436 | const char *cur = *str; | |
437 | const char *host; | |
438 | ||
439 | host = cur; | |
440 | /* | |
a93cf9df | 441 | * IPv6 and future addressing scheme are enclosed between brackets |
ca0defb9 PB |
442 | */ |
443 | if (*cur == '[') { | |
444 | cur++; | |
a1515161 | 445 | while ((*cur != ']') && (*cur != 0)) { |
be95adaf | 446 | cur++; |
a1515161 SH |
447 | } |
448 | if (*cur != ']') { | |
42fa2725 | 449 | return 1; |
a1515161 | 450 | } |
be95adaf SH |
451 | cur++; |
452 | goto found; | |
ca0defb9 PB |
453 | } |
454 | /* | |
455 | * try to parse an IPv4 | |
456 | */ | |
457 | if (ISA_DIGIT(cur)) { | |
a1515161 | 458 | if (rfc3986_parse_dec_octet(&cur) != 0) { |
be95adaf | 459 | goto not_ipv4; |
a1515161 SH |
460 | } |
461 | if (*cur != '.') { | |
be95adaf | 462 | goto not_ipv4; |
a1515161 | 463 | } |
be95adaf | 464 | cur++; |
a1515161 | 465 | if (rfc3986_parse_dec_octet(&cur) != 0) { |
be95adaf | 466 | goto not_ipv4; |
a1515161 SH |
467 | } |
468 | if (*cur != '.') { | |
be95adaf | 469 | goto not_ipv4; |
a1515161 SH |
470 | } |
471 | if (rfc3986_parse_dec_octet(&cur) != 0) { | |
be95adaf | 472 | goto not_ipv4; |
a1515161 SH |
473 | } |
474 | if (*cur != '.') { | |
be95adaf | 475 | goto not_ipv4; |
a1515161 SH |
476 | } |
477 | if (rfc3986_parse_dec_octet(&cur) != 0) { | |
be95adaf | 478 | goto not_ipv4; |
a1515161 | 479 | } |
be95adaf SH |
480 | goto found; |
481 | not_ipv4: | |
ca0defb9 PB |
482 | cur = *str; |
483 | } | |
484 | /* | |
485 | * then this should be a hostname which can be empty | |
486 | */ | |
a1515161 | 487 | while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur)) { |
ca0defb9 | 488 | NEXT(cur); |
a1515161 | 489 | } |
ca0defb9 PB |
490 | found: |
491 | if (uri != NULL) { | |
44c2286b | 492 | g_free(uri->authority); |
be95adaf | 493 | uri->authority = NULL; |
44c2286b | 494 | g_free(uri->server); |
be95adaf | 495 | if (cur != host) { |
a1515161 | 496 | if (uri->cleanup & 2) { |
be95adaf | 497 | uri->server = g_strndup(host, cur - host); |
a1515161 | 498 | } else { |
be95adaf | 499 | uri->server = uri_string_unescape(host, cur - host, NULL); |
a1515161 SH |
500 | } |
501 | } else { | |
be95adaf | 502 | uri->server = NULL; |
a1515161 | 503 | } |
ca0defb9 PB |
504 | } |
505 | *str = cur; | |
42fa2725 | 506 | return 0; |
ca0defb9 PB |
507 | } |
508 | ||
509 | /** | |
510 | * rfc3986_parse_authority: | |
511 | * @uri: pointer to an URI structure | |
512 | * @str: the string to analyze | |
513 | * | |
514 | * Parse an authority part and fills in the appropriate fields | |
515 | * of the @uri structure | |
516 | * | |
517 | * authority = [ userinfo "@" ] host [ ":" port ] | |
518 | * | |
519 | * Returns 0 or the error code | |
520 | */ | |
be95adaf | 521 | static int rfc3986_parse_authority(URI *uri, const char **str) |
ca0defb9 PB |
522 | { |
523 | const char *cur; | |
524 | int ret; | |
525 | ||
526 | cur = *str; | |
527 | /* | |
736a83fa | 528 | * try to parse a userinfo and check for the trailing @ |
ca0defb9 PB |
529 | */ |
530 | ret = rfc3986_parse_user_info(uri, &cur); | |
a1515161 | 531 | if ((ret != 0) || (*cur != '@')) { |
ca0defb9 | 532 | cur = *str; |
a1515161 | 533 | } else { |
ca0defb9 | 534 | cur++; |
a1515161 | 535 | } |
ca0defb9 | 536 | ret = rfc3986_parse_host(uri, &cur); |
a1515161 | 537 | if (ret != 0) { |
42fa2725 | 538 | return ret; |
a1515161 | 539 | } |
ca0defb9 PB |
540 | if (*cur == ':') { |
541 | cur++; | |
542 | ret = rfc3986_parse_port(uri, &cur); | |
a1515161 | 543 | if (ret != 0) { |
42fa2725 | 544 | return ret; |
a1515161 | 545 | } |
ca0defb9 PB |
546 | } |
547 | *str = cur; | |
42fa2725 | 548 | return 0; |
ca0defb9 PB |
549 | } |
550 | ||
551 | /** | |
552 | * rfc3986_parse_segment: | |
553 | * @str: the string to analyze | |
554 | * @forbid: an optional forbidden character | |
555 | * @empty: allow an empty segment | |
556 | * | |
557 | * Parse a segment and fills in the appropriate fields | |
558 | * of the @uri structure | |
559 | * | |
560 | * segment = *pchar | |
561 | * segment-nz = 1*pchar | |
562 | * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" ) | |
563 | * ; non-zero-length segment without any colon ":" | |
564 | * | |
565 | * Returns 0 or the error code | |
566 | */ | |
be95adaf | 567 | static int rfc3986_parse_segment(const char **str, char forbid, int empty) |
ca0defb9 PB |
568 | { |
569 | const char *cur; | |
570 | ||
571 | cur = *str; | |
572 | if (!ISA_PCHAR(cur)) { | |
a1515161 | 573 | if (empty) { |
42fa2725 | 574 | return 0; |
a1515161 | 575 | } |
42fa2725 | 576 | return 1; |
ca0defb9 | 577 | } |
a1515161 | 578 | while (ISA_PCHAR(cur) && (*cur != forbid)) { |
ca0defb9 | 579 | NEXT(cur); |
a1515161 | 580 | } |
ca0defb9 | 581 | *str = cur; |
42fa2725 | 582 | return 0; |
ca0defb9 PB |
583 | } |
584 | ||
585 | /** | |
586 | * rfc3986_parse_path_ab_empty: | |
587 | * @uri: pointer to an URI structure | |
588 | * @str: the string to analyze | |
589 | * | |
590 | * Parse an path absolute or empty and fills in the appropriate fields | |
591 | * of the @uri structure | |
592 | * | |
593 | * path-abempty = *( "/" segment ) | |
594 | * | |
595 | * Returns 0 or the error code | |
596 | */ | |
be95adaf | 597 | static int rfc3986_parse_path_ab_empty(URI *uri, const char **str) |
ca0defb9 PB |
598 | { |
599 | const char *cur; | |
600 | int ret; | |
601 | ||
602 | cur = *str; | |
603 | ||
604 | while (*cur == '/') { | |
605 | cur++; | |
be95adaf | 606 | ret = rfc3986_parse_segment(&cur, 0, 1); |
a1515161 | 607 | if (ret != 0) { |
42fa2725 | 608 | return ret; |
a1515161 | 609 | } |
ca0defb9 PB |
610 | } |
611 | if (uri != NULL) { | |
44c2286b | 612 | g_free(uri->path); |
ca0defb9 | 613 | if (*str != cur) { |
a1515161 | 614 | if (uri->cleanup & 2) { |
ca0defb9 | 615 | uri->path = g_strndup(*str, cur - *str); |
a1515161 | 616 | } else { |
ca0defb9 | 617 | uri->path = uri_string_unescape(*str, cur - *str, NULL); |
a1515161 | 618 | } |
ca0defb9 PB |
619 | } else { |
620 | uri->path = NULL; | |
621 | } | |
622 | } | |
623 | *str = cur; | |
42fa2725 | 624 | return 0; |
ca0defb9 PB |
625 | } |
626 | ||
627 | /** | |
628 | * rfc3986_parse_path_absolute: | |
629 | * @uri: pointer to an URI structure | |
630 | * @str: the string to analyze | |
631 | * | |
632 | * Parse an path absolute and fills in the appropriate fields | |
633 | * of the @uri structure | |
634 | * | |
635 | * path-absolute = "/" [ segment-nz *( "/" segment ) ] | |
636 | * | |
637 | * Returns 0 or the error code | |
638 | */ | |
be95adaf | 639 | static int rfc3986_parse_path_absolute(URI *uri, const char **str) |
ca0defb9 PB |
640 | { |
641 | const char *cur; | |
642 | int ret; | |
643 | ||
644 | cur = *str; | |
645 | ||
a1515161 | 646 | if (*cur != '/') { |
42fa2725 | 647 | return 1; |
a1515161 | 648 | } |
ca0defb9 PB |
649 | cur++; |
650 | ret = rfc3986_parse_segment(&cur, 0, 0); | |
651 | if (ret == 0) { | |
be95adaf SH |
652 | while (*cur == '/') { |
653 | cur++; | |
654 | ret = rfc3986_parse_segment(&cur, 0, 1); | |
a1515161 | 655 | if (ret != 0) { |
42fa2725 | 656 | return ret; |
a1515161 | 657 | } |
be95adaf | 658 | } |
ca0defb9 PB |
659 | } |
660 | if (uri != NULL) { | |
44c2286b | 661 | g_free(uri->path); |
ca0defb9 | 662 | if (cur != *str) { |
a1515161 | 663 | if (uri->cleanup & 2) { |
ca0defb9 | 664 | uri->path = g_strndup(*str, cur - *str); |
a1515161 | 665 | } else { |
ca0defb9 | 666 | uri->path = uri_string_unescape(*str, cur - *str, NULL); |
a1515161 | 667 | } |
ca0defb9 PB |
668 | } else { |
669 | uri->path = NULL; | |
670 | } | |
671 | } | |
672 | *str = cur; | |
42fa2725 | 673 | return 0; |
ca0defb9 PB |
674 | } |
675 | ||
676 | /** | |
677 | * rfc3986_parse_path_rootless: | |
678 | * @uri: pointer to an URI structure | |
679 | * @str: the string to analyze | |
680 | * | |
681 | * Parse an path without root and fills in the appropriate fields | |
682 | * of the @uri structure | |
683 | * | |
684 | * path-rootless = segment-nz *( "/" segment ) | |
685 | * | |
686 | * Returns 0 or the error code | |
687 | */ | |
be95adaf | 688 | static int rfc3986_parse_path_rootless(URI *uri, const char **str) |
ca0defb9 PB |
689 | { |
690 | const char *cur; | |
691 | int ret; | |
692 | ||
693 | cur = *str; | |
694 | ||
695 | ret = rfc3986_parse_segment(&cur, 0, 0); | |
a1515161 | 696 | if (ret != 0) { |
42fa2725 | 697 | return ret; |
a1515161 | 698 | } |
ca0defb9 PB |
699 | while (*cur == '/') { |
700 | cur++; | |
be95adaf | 701 | ret = rfc3986_parse_segment(&cur, 0, 1); |
a1515161 | 702 | if (ret != 0) { |
42fa2725 | 703 | return ret; |
a1515161 | 704 | } |
ca0defb9 PB |
705 | } |
706 | if (uri != NULL) { | |
44c2286b | 707 | g_free(uri->path); |
ca0defb9 | 708 | if (cur != *str) { |
a1515161 | 709 | if (uri->cleanup & 2) { |
ca0defb9 | 710 | uri->path = g_strndup(*str, cur - *str); |
a1515161 | 711 | } else { |
ca0defb9 | 712 | uri->path = uri_string_unescape(*str, cur - *str, NULL); |
a1515161 | 713 | } |
ca0defb9 PB |
714 | } else { |
715 | uri->path = NULL; | |
716 | } | |
717 | } | |
718 | *str = cur; | |
42fa2725 | 719 | return 0; |
ca0defb9 PB |
720 | } |
721 | ||
722 | /** | |
723 | * rfc3986_parse_path_no_scheme: | |
724 | * @uri: pointer to an URI structure | |
725 | * @str: the string to analyze | |
726 | * | |
727 | * Parse an path which is not a scheme and fills in the appropriate fields | |
728 | * of the @uri structure | |
729 | * | |
730 | * path-noscheme = segment-nz-nc *( "/" segment ) | |
731 | * | |
732 | * Returns 0 or the error code | |
733 | */ | |
be95adaf | 734 | static int rfc3986_parse_path_no_scheme(URI *uri, const char **str) |
ca0defb9 PB |
735 | { |
736 | const char *cur; | |
737 | int ret; | |
738 | ||
739 | cur = *str; | |
740 | ||
741 | ret = rfc3986_parse_segment(&cur, ':', 0); | |
a1515161 | 742 | if (ret != 0) { |
42fa2725 | 743 | return ret; |
a1515161 | 744 | } |
ca0defb9 PB |
745 | while (*cur == '/') { |
746 | cur++; | |
be95adaf | 747 | ret = rfc3986_parse_segment(&cur, 0, 1); |
a1515161 | 748 | if (ret != 0) { |
42fa2725 | 749 | return ret; |
a1515161 | 750 | } |
ca0defb9 PB |
751 | } |
752 | if (uri != NULL) { | |
44c2286b | 753 | g_free(uri->path); |
ca0defb9 | 754 | if (cur != *str) { |
a1515161 | 755 | if (uri->cleanup & 2) { |
ca0defb9 | 756 | uri->path = g_strndup(*str, cur - *str); |
a1515161 | 757 | } else { |
ca0defb9 | 758 | uri->path = uri_string_unescape(*str, cur - *str, NULL); |
a1515161 | 759 | } |
ca0defb9 PB |
760 | } else { |
761 | uri->path = NULL; | |
762 | } | |
763 | } | |
764 | *str = cur; | |
42fa2725 | 765 | return 0; |
ca0defb9 PB |
766 | } |
767 | ||
768 | /** | |
769 | * rfc3986_parse_hier_part: | |
770 | * @uri: pointer to an URI structure | |
771 | * @str: the string to analyze | |
772 | * | |
773 | * Parse an hierarchical part and fills in the appropriate fields | |
774 | * of the @uri structure | |
775 | * | |
776 | * hier-part = "//" authority path-abempty | |
777 | * / path-absolute | |
778 | * / path-rootless | |
779 | * / path-empty | |
780 | * | |
781 | * Returns 0 or the error code | |
782 | */ | |
be95adaf | 783 | static int rfc3986_parse_hier_part(URI *uri, const char **str) |
ca0defb9 PB |
784 | { |
785 | const char *cur; | |
786 | int ret; | |
787 | ||
788 | cur = *str; | |
789 | ||
790 | if ((*cur == '/') && (*(cur + 1) == '/')) { | |
791 | cur += 2; | |
be95adaf | 792 | ret = rfc3986_parse_authority(uri, &cur); |
a1515161 | 793 | if (ret != 0) { |
42fa2725 | 794 | return ret; |
a1515161 | 795 | } |
be95adaf | 796 | ret = rfc3986_parse_path_ab_empty(uri, &cur); |
a1515161 | 797 | if (ret != 0) { |
42fa2725 | 798 | return ret; |
a1515161 | 799 | } |
be95adaf | 800 | *str = cur; |
42fa2725 | 801 | return 0; |
ca0defb9 PB |
802 | } else if (*cur == '/') { |
803 | ret = rfc3986_parse_path_absolute(uri, &cur); | |
a1515161 | 804 | if (ret != 0) { |
42fa2725 | 805 | return ret; |
a1515161 | 806 | } |
ca0defb9 PB |
807 | } else if (ISA_PCHAR(cur)) { |
808 | ret = rfc3986_parse_path_rootless(uri, &cur); | |
a1515161 | 809 | if (ret != 0) { |
42fa2725 | 810 | return ret; |
a1515161 | 811 | } |
ca0defb9 | 812 | } else { |
be95adaf SH |
813 | /* path-empty is effectively empty */ |
814 | if (uri != NULL) { | |
44c2286b | 815 | g_free(uri->path); |
be95adaf SH |
816 | uri->path = NULL; |
817 | } | |
ca0defb9 PB |
818 | } |
819 | *str = cur; | |
42fa2725 | 820 | return 0; |
ca0defb9 PB |
821 | } |
822 | ||
823 | /** | |
824 | * rfc3986_parse_relative_ref: | |
825 | * @uri: pointer to an URI structure | |
826 | * @str: the string to analyze | |
827 | * | |
828 | * Parse an URI string and fills in the appropriate fields | |
829 | * of the @uri structure | |
830 | * | |
831 | * relative-ref = relative-part [ "?" query ] [ "#" fragment ] | |
832 | * relative-part = "//" authority path-abempty | |
833 | * / path-absolute | |
834 | * / path-noscheme | |
835 | * / path-empty | |
836 | * | |
837 | * Returns 0 or the error code | |
838 | */ | |
be95adaf SH |
839 | static int rfc3986_parse_relative_ref(URI *uri, const char *str) |
840 | { | |
ca0defb9 PB |
841 | int ret; |
842 | ||
843 | if ((*str == '/') && (*(str + 1) == '/')) { | |
844 | str += 2; | |
be95adaf | 845 | ret = rfc3986_parse_authority(uri, &str); |
a1515161 | 846 | if (ret != 0) { |
42fa2725 | 847 | return ret; |
a1515161 | 848 | } |
be95adaf | 849 | ret = rfc3986_parse_path_ab_empty(uri, &str); |
a1515161 | 850 | if (ret != 0) { |
42fa2725 | 851 | return ret; |
a1515161 | 852 | } |
ca0defb9 | 853 | } else if (*str == '/') { |
be95adaf | 854 | ret = rfc3986_parse_path_absolute(uri, &str); |
a1515161 | 855 | if (ret != 0) { |
42fa2725 | 856 | return ret; |
a1515161 | 857 | } |
ca0defb9 PB |
858 | } else if (ISA_PCHAR(str)) { |
859 | ret = rfc3986_parse_path_no_scheme(uri, &str); | |
a1515161 | 860 | if (ret != 0) { |
42fa2725 | 861 | return ret; |
a1515161 | 862 | } |
ca0defb9 | 863 | } else { |
be95adaf SH |
864 | /* path-empty is effectively empty */ |
865 | if (uri != NULL) { | |
44c2286b | 866 | g_free(uri->path); |
be95adaf SH |
867 | uri->path = NULL; |
868 | } | |
ca0defb9 PB |
869 | } |
870 | ||
871 | if (*str == '?') { | |
be95adaf SH |
872 | str++; |
873 | ret = rfc3986_parse_query(uri, &str); | |
a1515161 | 874 | if (ret != 0) { |
42fa2725 | 875 | return ret; |
a1515161 | 876 | } |
ca0defb9 PB |
877 | } |
878 | if (*str == '#') { | |
be95adaf SH |
879 | str++; |
880 | ret = rfc3986_parse_fragment(uri, &str); | |
a1515161 | 881 | if (ret != 0) { |
42fa2725 | 882 | return ret; |
a1515161 | 883 | } |
ca0defb9 PB |
884 | } |
885 | if (*str != 0) { | |
be95adaf | 886 | uri_clean(uri); |
42fa2725 | 887 | return 1; |
ca0defb9 | 888 | } |
42fa2725 | 889 | return 0; |
ca0defb9 PB |
890 | } |
891 | ||
ca0defb9 PB |
892 | /** |
893 | * rfc3986_parse: | |
894 | * @uri: pointer to an URI structure | |
895 | * @str: the string to analyze | |
896 | * | |
897 | * Parse an URI string and fills in the appropriate fields | |
898 | * of the @uri structure | |
899 | * | |
900 | * scheme ":" hier-part [ "?" query ] [ "#" fragment ] | |
901 | * | |
902 | * Returns 0 or the error code | |
903 | */ | |
be95adaf SH |
904 | static int rfc3986_parse(URI *uri, const char *str) |
905 | { | |
ca0defb9 PB |
906 | int ret; |
907 | ||
908 | ret = rfc3986_parse_scheme(uri, &str); | |
a1515161 | 909 | if (ret != 0) { |
42fa2725 | 910 | return ret; |
a1515161 | 911 | } |
ca0defb9 | 912 | if (*str != ':') { |
42fa2725 | 913 | return 1; |
ca0defb9 PB |
914 | } |
915 | str++; | |
916 | ret = rfc3986_parse_hier_part(uri, &str); | |
a1515161 | 917 | if (ret != 0) { |
42fa2725 | 918 | return ret; |
a1515161 | 919 | } |
ca0defb9 | 920 | if (*str == '?') { |
be95adaf SH |
921 | str++; |
922 | ret = rfc3986_parse_query(uri, &str); | |
a1515161 | 923 | if (ret != 0) { |
42fa2725 | 924 | return ret; |
a1515161 | 925 | } |
ca0defb9 PB |
926 | } |
927 | if (*str == '#') { | |
be95adaf SH |
928 | str++; |
929 | ret = rfc3986_parse_fragment(uri, &str); | |
a1515161 | 930 | if (ret != 0) { |
42fa2725 | 931 | return ret; |
a1515161 | 932 | } |
ca0defb9 PB |
933 | } |
934 | if (*str != 0) { | |
be95adaf | 935 | uri_clean(uri); |
42fa2725 | 936 | return 1; |
ca0defb9 | 937 | } |
42fa2725 | 938 | return 0; |
ca0defb9 PB |
939 | } |
940 | ||
941 | /** | |
942 | * rfc3986_parse_uri_reference: | |
943 | * @uri: pointer to an URI structure | |
944 | * @str: the string to analyze | |
945 | * | |
946 | * Parse an URI reference string and fills in the appropriate fields | |
947 | * of the @uri structure | |
948 | * | |
949 | * URI-reference = URI / relative-ref | |
950 | * | |
951 | * Returns 0 or the error code | |
952 | */ | |
be95adaf SH |
953 | static int rfc3986_parse_uri_reference(URI *uri, const char *str) |
954 | { | |
ca0defb9 PB |
955 | int ret; |
956 | ||
a1515161 | 957 | if (str == NULL) { |
42fa2725 | 958 | return -1; |
a1515161 | 959 | } |
ca0defb9 PB |
960 | uri_clean(uri); |
961 | ||
962 | /* | |
963 | * Try first to parse absolute refs, then fallback to relative if | |
964 | * it fails. | |
965 | */ | |
966 | ret = rfc3986_parse(uri, str); | |
967 | if (ret != 0) { | |
be95adaf | 968 | uri_clean(uri); |
ca0defb9 | 969 | ret = rfc3986_parse_relative_ref(uri, str); |
be95adaf SH |
970 | if (ret != 0) { |
971 | uri_clean(uri); | |
42fa2725 | 972 | return ret; |
be95adaf | 973 | } |
ca0defb9 | 974 | } |
42fa2725 | 975 | return 0; |
ca0defb9 PB |
976 | } |
977 | ||
978 | /** | |
979 | * uri_parse: | |
980 | * @str: the URI string to analyze | |
981 | * | |
982 | * Parse an URI based on RFC 3986 | |
983 | * | |
984 | * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ] | |
985 | * | |
986 | * Returns a newly built URI or NULL in case of error | |
987 | */ | |
be95adaf SH |
988 | URI *uri_parse(const char *str) |
989 | { | |
ca0defb9 PB |
990 | URI *uri; |
991 | int ret; | |
992 | ||
a1515161 | 993 | if (str == NULL) { |
42fa2725 | 994 | return NULL; |
a1515161 | 995 | } |
ca0defb9 | 996 | uri = uri_new(); |
c89c6e80 MA |
997 | ret = rfc3986_parse_uri_reference(uri, str); |
998 | if (ret) { | |
999 | uri_free(uri); | |
42fa2725 | 1000 | return NULL; |
ca0defb9 | 1001 | } |
42fa2725 | 1002 | return uri; |
ca0defb9 PB |
1003 | } |
1004 | ||
1005 | /** | |
1006 | * uri_parse_into: | |
1007 | * @uri: pointer to an URI structure | |
1008 | * @str: the string to analyze | |
1009 | * | |
1010 | * Parse an URI reference string based on RFC 3986 and fills in the | |
1011 | * appropriate fields of the @uri structure | |
1012 | * | |
1013 | * URI-reference = URI / relative-ref | |
1014 | * | |
1015 | * Returns 0 or the error code | |
1016 | */ | |
be95adaf SH |
1017 | int uri_parse_into(URI *uri, const char *str) |
1018 | { | |
42fa2725 | 1019 | return rfc3986_parse_uri_reference(uri, str); |
ca0defb9 PB |
1020 | } |
1021 | ||
1022 | /** | |
1023 | * uri_parse_raw: | |
1024 | * @str: the URI string to analyze | |
1025 | * @raw: if 1 unescaping of URI pieces are disabled | |
1026 | * | |
1027 | * Parse an URI but allows to keep intact the original fragments. | |
1028 | * | |
1029 | * URI-reference = URI / relative-ref | |
1030 | * | |
1031 | * Returns a newly built URI or NULL in case of error | |
1032 | */ | |
be95adaf SH |
1033 | URI *uri_parse_raw(const char *str, int raw) |
1034 | { | |
ca0defb9 PB |
1035 | URI *uri; |
1036 | int ret; | |
1037 | ||
a1515161 | 1038 | if (str == NULL) { |
42fa2725 | 1039 | return NULL; |
a1515161 | 1040 | } |
ca0defb9 | 1041 | uri = uri_new(); |
c89c6e80 MA |
1042 | if (raw) { |
1043 | uri->cleanup |= 2; | |
1044 | } | |
1045 | ret = uri_parse_into(uri, str); | |
1046 | if (ret) { | |
1047 | uri_free(uri); | |
42fa2725 | 1048 | return NULL; |
ca0defb9 | 1049 | } |
42fa2725 | 1050 | return uri; |
ca0defb9 PB |
1051 | } |
1052 | ||
1053 | /************************************************************************ | |
be95adaf SH |
1054 | * * |
1055 | * Generic URI structure functions * | |
1056 | * * | |
ca0defb9 PB |
1057 | ************************************************************************/ |
1058 | ||
1059 | /** | |
1060 | * uri_new: | |
1061 | * | |
1062 | * Simply creates an empty URI | |
1063 | * | |
1064 | * Returns the new structure or NULL in case of error | |
1065 | */ | |
be95adaf SH |
1066 | URI *uri_new(void) |
1067 | { | |
4a4ff4c5 | 1068 | return g_new0(URI, 1); |
ca0defb9 PB |
1069 | } |
1070 | ||
1071 | /** | |
1072 | * realloc2n: | |
1073 | * | |
1074 | * Function to handle properly a reallocation when saving an URI | |
1075 | * Also imposes some limit on the length of an URI string output | |
1076 | */ | |
be95adaf SH |
1077 | static char *realloc2n(char *ret, int *max) |
1078 | { | |
ca0defb9 PB |
1079 | char *temp; |
1080 | int tmp; | |
1081 | ||
1082 | tmp = *max * 2; | |
1083 | temp = g_realloc(ret, (tmp + 1)); | |
1084 | *max = tmp; | |
42fa2725 | 1085 | return temp; |
ca0defb9 PB |
1086 | } |
1087 | ||
1088 | /** | |
1089 | * uri_to_string: | |
1090 | * @uri: pointer to an URI | |
1091 | * | |
1092 | * Save the URI as an escaped string | |
1093 | * | |
1094 | * Returns a new string (to be deallocated by caller) | |
1095 | */ | |
be95adaf SH |
1096 | char *uri_to_string(URI *uri) |
1097 | { | |
ca0defb9 PB |
1098 | char *ret = NULL; |
1099 | char *temp; | |
1100 | const char *p; | |
1101 | int len; | |
1102 | int max; | |
1103 | ||
a1515161 | 1104 | if (uri == NULL) { |
42fa2725 | 1105 | return NULL; |
a1515161 | 1106 | } |
ca0defb9 PB |
1107 | |
1108 | max = 80; | |
1109 | ret = g_malloc(max + 1); | |
1110 | len = 0; | |
1111 | ||
1112 | if (uri->scheme != NULL) { | |
be95adaf SH |
1113 | p = uri->scheme; |
1114 | while (*p != 0) { | |
1115 | if (len >= max) { | |
ca0defb9 | 1116 | temp = realloc2n(ret, &max); |
be95adaf SH |
1117 | ret = temp; |
1118 | } | |
1119 | ret[len++] = *p++; | |
1120 | } | |
1121 | if (len >= max) { | |
ca0defb9 | 1122 | temp = realloc2n(ret, &max); |
ca0defb9 | 1123 | ret = temp; |
be95adaf SH |
1124 | } |
1125 | ret[len++] = ':'; | |
ca0defb9 PB |
1126 | } |
1127 | if (uri->opaque != NULL) { | |
be95adaf SH |
1128 | p = uri->opaque; |
1129 | while (*p != 0) { | |
1130 | if (len + 3 >= max) { | |
ca0defb9 | 1131 | temp = realloc2n(ret, &max); |
ca0defb9 | 1132 | ret = temp; |
be95adaf | 1133 | } |
a1515161 | 1134 | if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p))) { |
be95adaf | 1135 | ret[len++] = *p++; |
a1515161 | 1136 | } else { |
be95adaf SH |
1137 | int val = *(unsigned char *)p++; |
1138 | int hi = val / 0x10, lo = val % 0x10; | |
1139 | ret[len++] = '%'; | |
1140 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); | |
1141 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); | |
1142 | } | |
1143 | } | |
ca0defb9 | 1144 | } else { |
be95adaf SH |
1145 | if (uri->server != NULL) { |
1146 | if (len + 3 >= max) { | |
ca0defb9 | 1147 | temp = realloc2n(ret, &max); |
ca0defb9 | 1148 | ret = temp; |
be95adaf SH |
1149 | } |
1150 | ret[len++] = '/'; | |
1151 | ret[len++] = '/'; | |
1152 | if (uri->user != NULL) { | |
1153 | p = uri->user; | |
1154 | while (*p != 0) { | |
1155 | if (len + 3 >= max) { | |
ca0defb9 | 1156 | temp = realloc2n(ret, &max); |
ca0defb9 | 1157 | ret = temp; |
be95adaf SH |
1158 | } |
1159 | if ((IS_UNRESERVED(*(p))) || ((*(p) == ';')) || | |
1160 | ((*(p) == ':')) || ((*(p) == '&')) || ((*(p) == '=')) || | |
a1515161 | 1161 | ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ','))) { |
be95adaf | 1162 | ret[len++] = *p++; |
a1515161 | 1163 | } else { |
be95adaf SH |
1164 | int val = *(unsigned char *)p++; |
1165 | int hi = val / 0x10, lo = val % 0x10; | |
1166 | ret[len++] = '%'; | |
1167 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); | |
1168 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); | |
1169 | } | |
1170 | } | |
1171 | if (len + 3 >= max) { | |
ca0defb9 | 1172 | temp = realloc2n(ret, &max); |
ca0defb9 | 1173 | ret = temp; |
be95adaf SH |
1174 | } |
1175 | ret[len++] = '@'; | |
1176 | } | |
1177 | p = uri->server; | |
1178 | while (*p != 0) { | |
1179 | if (len >= max) { | |
ca0defb9 | 1180 | temp = realloc2n(ret, &max); |
ca0defb9 | 1181 | ret = temp; |
be95adaf SH |
1182 | } |
1183 | ret[len++] = *p++; | |
1184 | } | |
1185 | if (uri->port > 0) { | |
1186 | if (len + 10 >= max) { | |
ca0defb9 | 1187 | temp = realloc2n(ret, &max); |
ca0defb9 | 1188 | ret = temp; |
be95adaf SH |
1189 | } |
1190 | len += snprintf(&ret[len], max - len, ":%d", uri->port); | |
1191 | } | |
1192 | } else if (uri->authority != NULL) { | |
1193 | if (len + 3 >= max) { | |
ca0defb9 | 1194 | temp = realloc2n(ret, &max); |
ca0defb9 | 1195 | ret = temp; |
be95adaf SH |
1196 | } |
1197 | ret[len++] = '/'; | |
1198 | ret[len++] = '/'; | |
1199 | p = uri->authority; | |
1200 | while (*p != 0) { | |
1201 | if (len + 3 >= max) { | |
ca0defb9 | 1202 | temp = realloc2n(ret, &max); |
ca0defb9 | 1203 | ret = temp; |
be95adaf SH |
1204 | } |
1205 | if ((IS_UNRESERVED(*(p))) || ((*(p) == '$')) || | |
1206 | ((*(p) == ',')) || ((*(p) == ';')) || ((*(p) == ':')) || | |
1207 | ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) || | |
a1515161 | 1208 | ((*(p) == '+'))) { |
be95adaf | 1209 | ret[len++] = *p++; |
a1515161 | 1210 | } else { |
be95adaf SH |
1211 | int val = *(unsigned char *)p++; |
1212 | int hi = val / 0x10, lo = val % 0x10; | |
1213 | ret[len++] = '%'; | |
1214 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); | |
1215 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); | |
1216 | } | |
1217 | } | |
1218 | } else if (uri->scheme != NULL) { | |
1219 | if (len + 3 >= max) { | |
ca0defb9 | 1220 | temp = realloc2n(ret, &max); |
ca0defb9 | 1221 | ret = temp; |
be95adaf SH |
1222 | } |
1223 | ret[len++] = '/'; | |
1224 | ret[len++] = '/'; | |
1225 | } | |
1226 | if (uri->path != NULL) { | |
1227 | p = uri->path; | |
1228 | /* | |
1229 | * the colon in file:///d: should not be escaped or | |
1230 | * Windows accesses fail later. | |
1231 | */ | |
1232 | if ((uri->scheme != NULL) && (p[0] == '/') && | |
1233 | (((p[1] >= 'a') && (p[1] <= 'z')) || | |
1234 | ((p[1] >= 'A') && (p[1] <= 'Z'))) && | |
1235 | (p[2] == ':') && (!strcmp(uri->scheme, "file"))) { | |
1236 | if (len + 3 >= max) { | |
ca0defb9 | 1237 | temp = realloc2n(ret, &max); |
ca0defb9 | 1238 | ret = temp; |
be95adaf SH |
1239 | } |
1240 | ret[len++] = *p++; | |
1241 | ret[len++] = *p++; | |
1242 | ret[len++] = *p++; | |
1243 | } | |
1244 | while (*p != 0) { | |
1245 | if (len + 3 >= max) { | |
ca0defb9 | 1246 | temp = realloc2n(ret, &max); |
ca0defb9 | 1247 | ret = temp; |
be95adaf SH |
1248 | } |
1249 | if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) || | |
ca0defb9 | 1250 | ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || |
be95adaf | 1251 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || |
a1515161 | 1252 | ((*(p) == ','))) { |
be95adaf | 1253 | ret[len++] = *p++; |
a1515161 | 1254 | } else { |
be95adaf SH |
1255 | int val = *(unsigned char *)p++; |
1256 | int hi = val / 0x10, lo = val % 0x10; | |
1257 | ret[len++] = '%'; | |
1258 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); | |
1259 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); | |
1260 | } | |
1261 | } | |
1262 | } | |
1263 | if (uri->query != NULL) { | |
1264 | if (len + 1 >= max) { | |
ca0defb9 | 1265 | temp = realloc2n(ret, &max); |
ca0defb9 | 1266 | ret = temp; |
be95adaf SH |
1267 | } |
1268 | ret[len++] = '?'; | |
1269 | p = uri->query; | |
1270 | while (*p != 0) { | |
1271 | if (len + 1 >= max) { | |
ca0defb9 | 1272 | temp = realloc2n(ret, &max); |
ca0defb9 | 1273 | ret = temp; |
be95adaf SH |
1274 | } |
1275 | ret[len++] = *p++; | |
1276 | } | |
1277 | } | |
ca0defb9 PB |
1278 | } |
1279 | if (uri->fragment != NULL) { | |
be95adaf | 1280 | if (len + 3 >= max) { |
ca0defb9 | 1281 | temp = realloc2n(ret, &max); |
ca0defb9 | 1282 | ret = temp; |
be95adaf SH |
1283 | } |
1284 | ret[len++] = '#'; | |
1285 | p = uri->fragment; | |
1286 | while (*p != 0) { | |
1287 | if (len + 3 >= max) { | |
ca0defb9 | 1288 | temp = realloc2n(ret, &max); |
ca0defb9 | 1289 | ret = temp; |
be95adaf | 1290 | } |
a1515161 | 1291 | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) { |
be95adaf | 1292 | ret[len++] = *p++; |
a1515161 | 1293 | } else { |
be95adaf SH |
1294 | int val = *(unsigned char *)p++; |
1295 | int hi = val / 0x10, lo = val % 0x10; | |
1296 | ret[len++] = '%'; | |
1297 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); | |
1298 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); | |
1299 | } | |
1300 | } | |
ca0defb9 PB |
1301 | } |
1302 | if (len >= max) { | |
1303 | temp = realloc2n(ret, &max); | |
ca0defb9 PB |
1304 | ret = temp; |
1305 | } | |
1306 | ret[len] = 0; | |
42fa2725 | 1307 | return ret; |
ca0defb9 PB |
1308 | } |
1309 | ||
1310 | /** | |
1311 | * uri_clean: | |
1312 | * @uri: pointer to an URI | |
1313 | * | |
1314 | * Make sure the URI struct is free of content | |
1315 | */ | |
be95adaf SH |
1316 | static void uri_clean(URI *uri) |
1317 | { | |
a1515161 | 1318 | if (uri == NULL) { |
be95adaf | 1319 | return; |
a1515161 | 1320 | } |
ca0defb9 | 1321 | |
44c2286b | 1322 | g_free(uri->scheme); |
ca0defb9 | 1323 | uri->scheme = NULL; |
44c2286b | 1324 | g_free(uri->server); |
ca0defb9 | 1325 | uri->server = NULL; |
44c2286b | 1326 | g_free(uri->user); |
ca0defb9 | 1327 | uri->user = NULL; |
44c2286b | 1328 | g_free(uri->path); |
ca0defb9 | 1329 | uri->path = NULL; |
44c2286b | 1330 | g_free(uri->fragment); |
ca0defb9 | 1331 | uri->fragment = NULL; |
44c2286b | 1332 | g_free(uri->opaque); |
ca0defb9 | 1333 | uri->opaque = NULL; |
44c2286b | 1334 | g_free(uri->authority); |
ca0defb9 | 1335 | uri->authority = NULL; |
44c2286b | 1336 | g_free(uri->query); |
ca0defb9 PB |
1337 | uri->query = NULL; |
1338 | } | |
1339 | ||
1340 | /** | |
1341 | * uri_free: | |
1342 | * @uri: pointer to an URI | |
1343 | * | |
1344 | * Free up the URI struct | |
1345 | */ | |
be95adaf SH |
1346 | void uri_free(URI *uri) |
1347 | { | |
ca0defb9 PB |
1348 | uri_clean(uri); |
1349 | g_free(uri); | |
1350 | } | |
1351 | ||
1352 | /************************************************************************ | |
be95adaf SH |
1353 | * * |
1354 | * Helper functions * | |
1355 | * * | |
ca0defb9 PB |
1356 | ************************************************************************/ |
1357 | ||
1358 | /** | |
1359 | * normalize_uri_path: | |
1360 | * @path: pointer to the path string | |
1361 | * | |
1362 | * Applies the 5 normalization steps to a path string--that is, RFC 2396 | |
1363 | * Section 5.2, steps 6.c through 6.g. | |
1364 | * | |
1365 | * Normalization occurs directly on the string, no new allocation is done | |
1366 | * | |
1367 | * Returns 0 or an error code | |
1368 | */ | |
be95adaf SH |
1369 | static int normalize_uri_path(char *path) |
1370 | { | |
ca0defb9 PB |
1371 | char *cur, *out; |
1372 | ||
a1515161 | 1373 | if (path == NULL) { |
42fa2725 | 1374 | return -1; |
a1515161 | 1375 | } |
ca0defb9 PB |
1376 | |
1377 | /* Skip all initial "/" chars. We want to get to the beginning of the | |
1378 | * first non-empty segment. | |
1379 | */ | |
1380 | cur = path; | |
a1515161 | 1381 | while (cur[0] == '/') { |
be95adaf | 1382 | ++cur; |
a1515161 SH |
1383 | } |
1384 | if (cur[0] == '\0') { | |
42fa2725 | 1385 | return 0; |
a1515161 | 1386 | } |
ca0defb9 PB |
1387 | |
1388 | /* Keep everything we've seen so far. */ | |
1389 | out = cur; | |
1390 | ||
1391 | /* | |
1392 | * Analyze each segment in sequence for cases (c) and (d). | |
1393 | */ | |
1394 | while (cur[0] != '\0') { | |
be95adaf SH |
1395 | /* |
1396 | * c) All occurrences of "./", where "." is a complete path segment, | |
1397 | * are removed from the buffer string. | |
1398 | */ | |
1399 | if ((cur[0] == '.') && (cur[1] == '/')) { | |
1400 | cur += 2; | |
1401 | /* '//' normalization should be done at this point too */ | |
a1515161 | 1402 | while (cur[0] == '/') { |
be95adaf | 1403 | cur++; |
a1515161 | 1404 | } |
be95adaf SH |
1405 | continue; |
1406 | } | |
1407 | ||
1408 | /* | |
1409 | * d) If the buffer string ends with "." as a complete path segment, | |
1410 | * that "." is removed. | |
1411 | */ | |
a1515161 | 1412 | if ((cur[0] == '.') && (cur[1] == '\0')) { |
be95adaf | 1413 | break; |
a1515161 | 1414 | } |
be95adaf SH |
1415 | |
1416 | /* Otherwise keep the segment. */ | |
1417 | while (cur[0] != '/') { | |
a1515161 | 1418 | if (cur[0] == '\0') { |
be95adaf | 1419 | goto done_cd; |
a1515161 | 1420 | } |
be95adaf SH |
1421 | (out++)[0] = (cur++)[0]; |
1422 | } | |
1423 | /* nomalize // */ | |
a1515161 | 1424 | while ((cur[0] == '/') && (cur[1] == '/')) { |
be95adaf | 1425 | cur++; |
a1515161 | 1426 | } |
ca0defb9 PB |
1427 | |
1428 | (out++)[0] = (cur++)[0]; | |
1429 | } | |
be95adaf | 1430 | done_cd: |
ca0defb9 PB |
1431 | out[0] = '\0'; |
1432 | ||
1433 | /* Reset to the beginning of the first segment for the next sequence. */ | |
1434 | cur = path; | |
a1515161 | 1435 | while (cur[0] == '/') { |
be95adaf | 1436 | ++cur; |
a1515161 SH |
1437 | } |
1438 | if (cur[0] == '\0') { | |
42fa2725 | 1439 | return 0; |
a1515161 | 1440 | } |
ca0defb9 PB |
1441 | |
1442 | /* | |
1443 | * Analyze each segment in sequence for cases (e) and (f). | |
1444 | * | |
1445 | * e) All occurrences of "<segment>/../", where <segment> is a | |
1446 | * complete path segment not equal to "..", are removed from the | |
1447 | * buffer string. Removal of these path segments is performed | |
1448 | * iteratively, removing the leftmost matching pattern on each | |
1449 | * iteration, until no matching pattern remains. | |
1450 | * | |
1451 | * f) If the buffer string ends with "<segment>/..", where <segment> | |
1452 | * is a complete path segment not equal to "..", that | |
1453 | * "<segment>/.." is removed. | |
1454 | * | |
1455 | * To satisfy the "iterative" clause in (e), we need to collapse the | |
1456 | * string every time we find something that needs to be removed. Thus, | |
1457 | * we don't need to keep two pointers into the string: we only need a | |
1458 | * "current position" pointer. | |
1459 | */ | |
1460 | while (1) { | |
1461 | char *segp, *tmp; | |
1462 | ||
1463 | /* At the beginning of each iteration of this loop, "cur" points to | |
1464 | * the first character of the segment we want to examine. | |
1465 | */ | |
1466 | ||
1467 | /* Find the end of the current segment. */ | |
1468 | segp = cur; | |
a1515161 | 1469 | while ((segp[0] != '/') && (segp[0] != '\0')) { |
be95adaf | 1470 | ++segp; |
a1515161 | 1471 | } |
ca0defb9 PB |
1472 | |
1473 | /* If this is the last segment, we're done (we need at least two | |
1474 | * segments to meet the criteria for the (e) and (f) cases). | |
1475 | */ | |
a1515161 | 1476 | if (segp[0] == '\0') { |
be95adaf | 1477 | break; |
a1515161 | 1478 | } |
ca0defb9 PB |
1479 | |
1480 | /* If the first segment is "..", or if the next segment _isn't_ "..", | |
1481 | * keep this segment and try the next one. | |
1482 | */ | |
1483 | ++segp; | |
be95adaf SH |
1484 | if (((cur[0] == '.') && (cur[1] == '.') && (segp == cur + 3)) || |
1485 | ((segp[0] != '.') || (segp[1] != '.') || | |
1486 | ((segp[2] != '/') && (segp[2] != '\0')))) { | |
1487 | cur = segp; | |
1488 | continue; | |
ca0defb9 PB |
1489 | } |
1490 | ||
1491 | /* If we get here, remove this segment and the next one and back up | |
1492 | * to the previous segment (if there is one), to implement the | |
1493 | * "iteratively" clause. It's pretty much impossible to back up | |
1494 | * while maintaining two pointers into the buffer, so just compact | |
1495 | * the whole buffer now. | |
1496 | */ | |
1497 | ||
1498 | /* If this is the end of the buffer, we're done. */ | |
1499 | if (segp[2] == '\0') { | |
be95adaf SH |
1500 | cur[0] = '\0'; |
1501 | break; | |
ca0defb9 PB |
1502 | } |
1503 | /* Valgrind complained, strcpy(cur, segp + 3); */ | |
1504 | /* string will overlap, do not use strcpy */ | |
1505 | tmp = cur; | |
1506 | segp += 3; | |
a1515161 SH |
1507 | while ((*tmp++ = *segp++) != 0) { |
1508 | /* No further work */ | |
1509 | } | |
ca0defb9 PB |
1510 | |
1511 | /* If there are no previous segments, then keep going from here. */ | |
1512 | segp = cur; | |
a1515161 SH |
1513 | while ((segp > path) && ((--segp)[0] == '/')) { |
1514 | /* No further work */ | |
1515 | } | |
1516 | if (segp == path) { | |
be95adaf | 1517 | continue; |
a1515161 | 1518 | } |
ca0defb9 PB |
1519 | |
1520 | /* "segp" is pointing to the end of a previous segment; find it's | |
1521 | * start. We need to back up to the previous segment and start | |
1522 | * over with that to handle things like "foo/bar/../..". If we | |
1523 | * don't do this, then on the first pass we'll remove the "bar/..", | |
1524 | * but be pointing at the second ".." so we won't realize we can also | |
1525 | * remove the "foo/..". | |
1526 | */ | |
1527 | cur = segp; | |
a1515161 | 1528 | while ((cur > path) && (cur[-1] != '/')) { |
be95adaf | 1529 | --cur; |
a1515161 | 1530 | } |
ca0defb9 PB |
1531 | } |
1532 | out[0] = '\0'; | |
1533 | ||
1534 | /* | |
1535 | * g) If the resulting buffer string still begins with one or more | |
1536 | * complete path segments of "..", then the reference is | |
1537 | * considered to be in error. Implementations may handle this | |
1538 | * error by retaining these components in the resolved path (i.e., | |
1539 | * treating them as part of the final URI), by removing them from | |
1540 | * the resolved path (i.e., discarding relative levels above the | |
1541 | * root), or by avoiding traversal of the reference. | |
1542 | * | |
1543 | * We discard them from the final path. | |
1544 | */ | |
1545 | if (path[0] == '/') { | |
be95adaf SH |
1546 | cur = path; |
1547 | while ((cur[0] == '/') && (cur[1] == '.') && (cur[2] == '.') && | |
a1515161 | 1548 | ((cur[3] == '/') || (cur[3] == '\0'))) { |
be95adaf | 1549 | cur += 3; |
a1515161 | 1550 | } |
be95adaf SH |
1551 | |
1552 | if (cur != path) { | |
1553 | out = path; | |
a1515161 | 1554 | while (cur[0] != '\0') { |
be95adaf | 1555 | (out++)[0] = (cur++)[0]; |
a1515161 | 1556 | } |
be95adaf SH |
1557 | out[0] = 0; |
1558 | } | |
ca0defb9 PB |
1559 | } |
1560 | ||
42fa2725 | 1561 | return 0; |
ca0defb9 PB |
1562 | } |
1563 | ||
be95adaf SH |
1564 | static int is_hex(char c) |
1565 | { | |
1566 | if (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || | |
a1515161 | 1567 | ((c >= 'A') && (c <= 'F'))) { |
42fa2725 | 1568 | return 1; |
a1515161 | 1569 | } |
42fa2725 | 1570 | return 0; |
ca0defb9 PB |
1571 | } |
1572 | ||
ca0defb9 PB |
1573 | /** |
1574 | * uri_string_unescape: | |
1575 | * @str: the string to unescape | |
1576 | * @len: the length in bytes to unescape (or <= 0 to indicate full string) | |
1577 | * @target: optional destination buffer | |
1578 | * | |
1579 | * Unescaping routine, but does not check that the string is an URI. The | |
1580 | * output is a direct unsigned char translation of %XX values (no encoding) | |
1581 | * Note that the length of the result can only be smaller or same size as | |
1582 | * the input string. | |
1583 | * | |
1584 | * Returns a copy of the string, but unescaped, will return NULL only in case | |
1585 | * of error | |
1586 | */ | |
be95adaf SH |
1587 | char *uri_string_unescape(const char *str, int len, char *target) |
1588 | { | |
ca0defb9 PB |
1589 | char *ret, *out; |
1590 | const char *in; | |
1591 | ||
a1515161 | 1592 | if (str == NULL) { |
42fa2725 | 1593 | return NULL; |
a1515161 SH |
1594 | } |
1595 | if (len <= 0) { | |
be95adaf | 1596 | len = strlen(str); |
a1515161 SH |
1597 | } |
1598 | if (len < 0) { | |
42fa2725 | 1599 | return NULL; |
a1515161 | 1600 | } |
ca0defb9 PB |
1601 | |
1602 | if (target == NULL) { | |
be95adaf | 1603 | ret = g_malloc(len + 1); |
a1515161 | 1604 | } else { |
be95adaf | 1605 | ret = target; |
a1515161 | 1606 | } |
ca0defb9 PB |
1607 | in = str; |
1608 | out = ret; | |
be95adaf SH |
1609 | while (len > 0) { |
1610 | if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) { | |
1611 | in++; | |
a1515161 | 1612 | if ((*in >= '0') && (*in <= '9')) { |
be95adaf | 1613 | *out = (*in - '0'); |
a1515161 | 1614 | } else if ((*in >= 'a') && (*in <= 'f')) { |
be95adaf | 1615 | *out = (*in - 'a') + 10; |
a1515161 | 1616 | } else if ((*in >= 'A') && (*in <= 'F')) { |
be95adaf | 1617 | *out = (*in - 'A') + 10; |
a1515161 | 1618 | } |
be95adaf | 1619 | in++; |
a1515161 | 1620 | if ((*in >= '0') && (*in <= '9')) { |
be95adaf | 1621 | *out = *out * 16 + (*in - '0'); |
a1515161 | 1622 | } else if ((*in >= 'a') && (*in <= 'f')) { |
be95adaf | 1623 | *out = *out * 16 + (*in - 'a') + 10; |
a1515161 | 1624 | } else if ((*in >= 'A') && (*in <= 'F')) { |
be95adaf | 1625 | *out = *out * 16 + (*in - 'A') + 10; |
a1515161 | 1626 | } |
be95adaf SH |
1627 | in++; |
1628 | len -= 3; | |
1629 | out++; | |
1630 | } else { | |
1631 | *out++ = *in++; | |
1632 | len--; | |
1633 | } | |
ca0defb9 PB |
1634 | } |
1635 | *out = 0; | |
42fa2725 | 1636 | return ret; |
ca0defb9 PB |
1637 | } |
1638 | ||
1639 | /** | |
1640 | * uri_string_escape: | |
1641 | * @str: string to escape | |
1642 | * @list: exception list string of chars not to escape | |
1643 | * | |
1644 | * This routine escapes a string to hex, ignoring reserved characters (a-z) | |
1645 | * and the characters in the exception list. | |
1646 | * | |
1647 | * Returns a new escaped string or NULL in case of error. | |
1648 | */ | |
be95adaf SH |
1649 | char *uri_string_escape(const char *str, const char *list) |
1650 | { | |
ca0defb9 PB |
1651 | char *ret, ch; |
1652 | char *temp; | |
1653 | const char *in; | |
1654 | int len, out; | |
1655 | ||
a1515161 | 1656 | if (str == NULL) { |
42fa2725 | 1657 | return NULL; |
a1515161 SH |
1658 | } |
1659 | if (str[0] == 0) { | |
42fa2725 | 1660 | return g_strdup(str); |
a1515161 | 1661 | } |
ca0defb9 | 1662 | len = strlen(str); |
a1515161 | 1663 | if (!(len > 0)) { |
42fa2725 | 1664 | return NULL; |
a1515161 | 1665 | } |
ca0defb9 PB |
1666 | |
1667 | len += 20; | |
1668 | ret = g_malloc(len); | |
1669 | in = str; | |
1670 | out = 0; | |
be95adaf SH |
1671 | while (*in != 0) { |
1672 | if (len - out <= 3) { | |
ca0defb9 | 1673 | temp = realloc2n(ret, &len); |
be95adaf SH |
1674 | ret = temp; |
1675 | } | |
ca0defb9 | 1676 | |
be95adaf SH |
1677 | ch = *in; |
1678 | ||
1679 | if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!strchr(list, ch))) { | |
1680 | unsigned char val; | |
1681 | ret[out++] = '%'; | |
1682 | val = ch >> 4; | |
a1515161 | 1683 | if (val <= 9) { |
be95adaf | 1684 | ret[out++] = '0' + val; |
a1515161 | 1685 | } else { |
be95adaf | 1686 | ret[out++] = 'A' + val - 0xA; |
a1515161 | 1687 | } |
be95adaf | 1688 | val = ch & 0xF; |
a1515161 | 1689 | if (val <= 9) { |
be95adaf | 1690 | ret[out++] = '0' + val; |
a1515161 | 1691 | } else { |
be95adaf | 1692 | ret[out++] = 'A' + val - 0xA; |
a1515161 | 1693 | } |
be95adaf SH |
1694 | in++; |
1695 | } else { | |
1696 | ret[out++] = *in++; | |
1697 | } | |
ca0defb9 PB |
1698 | } |
1699 | ret[out] = 0; | |
42fa2725 | 1700 | return ret; |
ca0defb9 PB |
1701 | } |
1702 | ||
1703 | /************************************************************************ | |
be95adaf SH |
1704 | * * |
1705 | * Public functions * | |
1706 | * * | |
ca0defb9 PB |
1707 | ************************************************************************/ |
1708 | ||
1709 | /** | |
1710 | * uri_resolve: | |
1711 | * @URI: the URI instance found in the document | |
1712 | * @base: the base value | |
1713 | * | |
1714 | * Computes he final URI of the reference done by checking that | |
1715 | * the given URI is valid, and building the final URI using the | |
1716 | * base URI. This is processed according to section 5.2 of the | |
1717 | * RFC 2396 | |
1718 | * | |
1719 | * 5.2. Resolving Relative References to Absolute Form | |
1720 | * | |
1721 | * Returns a new URI string (to be freed by the caller) or NULL in case | |
1722 | * of error. | |
1723 | */ | |
be95adaf SH |
1724 | char *uri_resolve(const char *uri, const char *base) |
1725 | { | |
ca0defb9 PB |
1726 | char *val = NULL; |
1727 | int ret, len, indx, cur, out; | |
1728 | URI *ref = NULL; | |
1729 | URI *bas = NULL; | |
1730 | URI *res = NULL; | |
1731 | ||
1732 | /* | |
1733 | * 1) The URI reference is parsed into the potential four components and | |
1734 | * fragment identifier, as described in Section 4.3. | |
1735 | * | |
1736 | * NOTE that a completely empty URI is treated by modern browsers | |
1737 | * as a reference to "." rather than as a synonym for the current | |
1738 | * URI. Should we do that here? | |
1739 | */ | |
a1515161 | 1740 | if (uri == NULL) { |
be95adaf | 1741 | ret = -1; |
a1515161 | 1742 | } else { |
be95adaf SH |
1743 | if (*uri) { |
1744 | ref = uri_new(); | |
1745 | ret = uri_parse_into(ref, uri); | |
a1515161 | 1746 | } else { |
be95adaf | 1747 | ret = 0; |
a1515161 | 1748 | } |
ca0defb9 | 1749 | } |
a1515161 | 1750 | if (ret != 0) { |
be95adaf | 1751 | goto done; |
a1515161 | 1752 | } |
ca0defb9 | 1753 | if ((ref != NULL) && (ref->scheme != NULL)) { |
be95adaf SH |
1754 | /* |
1755 | * The URI is absolute don't modify. | |
1756 | */ | |
1757 | val = g_strdup(uri); | |
1758 | goto done; | |
ca0defb9 | 1759 | } |
a1515161 | 1760 | if (base == NULL) { |
be95adaf | 1761 | ret = -1; |
a1515161 | 1762 | } else { |
be95adaf SH |
1763 | bas = uri_new(); |
1764 | ret = uri_parse_into(bas, base); | |
ca0defb9 PB |
1765 | } |
1766 | if (ret != 0) { | |
a1515161 | 1767 | if (ref) { |
be95adaf | 1768 | val = uri_to_string(ref); |
a1515161 | 1769 | } |
be95adaf | 1770 | goto done; |
ca0defb9 PB |
1771 | } |
1772 | if (ref == NULL) { | |
be95adaf SH |
1773 | /* |
1774 | * the base fragment must be ignored | |
1775 | */ | |
44c2286b MA |
1776 | g_free(bas->fragment); |
1777 | bas->fragment = NULL; | |
be95adaf SH |
1778 | val = uri_to_string(bas); |
1779 | goto done; | |
ca0defb9 PB |
1780 | } |
1781 | ||
1782 | /* | |
1783 | * 2) If the path component is empty and the scheme, authority, and | |
1784 | * query components are undefined, then it is a reference to the | |
1785 | * current document and we are done. Otherwise, the reference URI's | |
1786 | * query and fragment components are defined as found (or not found) | |
1787 | * within the URI reference and not inherited from the base URI. | |
1788 | * | |
1789 | * NOTE that in modern browsers, the parsing differs from the above | |
1790 | * in the following aspect: the query component is allowed to be | |
1791 | * defined while still treating this as a reference to the current | |
1792 | * document. | |
1793 | */ | |
1794 | res = uri_new(); | |
ca0defb9 | 1795 | if ((ref->scheme == NULL) && (ref->path == NULL) && |
be95adaf | 1796 | ((ref->authority == NULL) && (ref->server == NULL))) { |
24588100 | 1797 | res->scheme = g_strdup(bas->scheme); |
a1515161 | 1798 | if (bas->authority != NULL) { |
be95adaf | 1799 | res->authority = g_strdup(bas->authority); |
a1515161 | 1800 | } else if (bas->server != NULL) { |
24588100 MA |
1801 | res->server = g_strdup(bas->server); |
1802 | res->user = g_strdup(bas->user); | |
1803 | res->port = bas->port; | |
be95adaf | 1804 | } |
24588100 MA |
1805 | res->path = g_strdup(bas->path); |
1806 | if (ref->query != NULL) { | |
be95adaf | 1807 | res->query = g_strdup(ref->query); |
24588100 MA |
1808 | } else { |
1809 | res->query = g_strdup(bas->query); | |
1810 | } | |
1811 | res->fragment = g_strdup(ref->fragment); | |
be95adaf | 1812 | goto step_7; |
ca0defb9 PB |
1813 | } |
1814 | ||
1815 | /* | |
1816 | * 3) If the scheme component is defined, indicating that the reference | |
1817 | * starts with a scheme name, then the reference is interpreted as an | |
1818 | * absolute URI and we are done. Otherwise, the reference URI's | |
1819 | * scheme is inherited from the base URI's scheme component. | |
1820 | */ | |
1821 | if (ref->scheme != NULL) { | |
be95adaf SH |
1822 | val = uri_to_string(ref); |
1823 | goto done; | |
ca0defb9 | 1824 | } |
24588100 | 1825 | res->scheme = g_strdup(bas->scheme); |
ca0defb9 | 1826 | |
24588100 MA |
1827 | res->query = g_strdup(ref->query); |
1828 | res->fragment = g_strdup(ref->fragment); | |
ca0defb9 PB |
1829 | |
1830 | /* | |
1831 | * 4) If the authority component is defined, then the reference is a | |
1832 | * network-path and we skip to step 7. Otherwise, the reference | |
1833 | * URI's authority is inherited from the base URI's authority | |
1834 | * component, which will also be undefined if the URI scheme does not | |
1835 | * use an authority component. | |
1836 | */ | |
1837 | if ((ref->authority != NULL) || (ref->server != NULL)) { | |
a1515161 | 1838 | if (ref->authority != NULL) { |
be95adaf | 1839 | res->authority = g_strdup(ref->authority); |
a1515161 | 1840 | } else { |
be95adaf | 1841 | res->server = g_strdup(ref->server); |
24588100 | 1842 | res->user = g_strdup(ref->user); |
ca0defb9 | 1843 | res->port = ref->port; |
be95adaf | 1844 | } |
24588100 | 1845 | res->path = g_strdup(ref->path); |
be95adaf | 1846 | goto step_7; |
ca0defb9 | 1847 | } |
a1515161 | 1848 | if (bas->authority != NULL) { |
be95adaf | 1849 | res->authority = g_strdup(bas->authority); |
a1515161 | 1850 | } else if (bas->server != NULL) { |
24588100 MA |
1851 | res->server = g_strdup(bas->server); |
1852 | res->user = g_strdup(bas->user); | |
be95adaf | 1853 | res->port = bas->port; |
ca0defb9 PB |
1854 | } |
1855 | ||
1856 | /* | |
1857 | * 5) If the path component begins with a slash character ("/"), then | |
1858 | * the reference is an absolute-path and we skip to step 7. | |
1859 | */ | |
1860 | if ((ref->path != NULL) && (ref->path[0] == '/')) { | |
be95adaf SH |
1861 | res->path = g_strdup(ref->path); |
1862 | goto step_7; | |
ca0defb9 PB |
1863 | } |
1864 | ||
ca0defb9 PB |
1865 | /* |
1866 | * 6) If this step is reached, then we are resolving a relative-path | |
1867 | * reference. The relative path needs to be merged with the base | |
1868 | * URI's path. Although there are many ways to do this, we will | |
1869 | * describe a simple method using a separate string buffer. | |
1870 | * | |
1871 | * Allocate a buffer large enough for the result string. | |
1872 | */ | |
1873 | len = 2; /* extra / and 0 */ | |
a1515161 | 1874 | if (ref->path != NULL) { |
be95adaf | 1875 | len += strlen(ref->path); |
a1515161 SH |
1876 | } |
1877 | if (bas->path != NULL) { | |
be95adaf | 1878 | len += strlen(bas->path); |
a1515161 | 1879 | } |
ca0defb9 PB |
1880 | res->path = g_malloc(len); |
1881 | res->path[0] = 0; | |
1882 | ||
1883 | /* | |
1884 | * a) All but the last segment of the base URI's path component is | |
1885 | * copied to the buffer. In other words, any characters after the | |
1886 | * last (right-most) slash character, if any, are excluded. | |
1887 | */ | |
1888 | cur = 0; | |
1889 | out = 0; | |
1890 | if (bas->path != NULL) { | |
be95adaf | 1891 | while (bas->path[cur] != 0) { |
a1515161 | 1892 | while ((bas->path[cur] != 0) && (bas->path[cur] != '/')) { |
be95adaf | 1893 | cur++; |
a1515161 SH |
1894 | } |
1895 | if (bas->path[cur] == 0) { | |
be95adaf | 1896 | break; |
a1515161 | 1897 | } |
be95adaf SH |
1898 | |
1899 | cur++; | |
1900 | while (out < cur) { | |
1901 | res->path[out] = bas->path[out]; | |
1902 | out++; | |
1903 | } | |
1904 | } | |
ca0defb9 PB |
1905 | } |
1906 | res->path[out] = 0; | |
1907 | ||
1908 | /* | |
1909 | * b) The reference's path component is appended to the buffer | |
1910 | * string. | |
1911 | */ | |
1912 | if (ref->path != NULL && ref->path[0] != 0) { | |
be95adaf SH |
1913 | indx = 0; |
1914 | /* | |
1915 | * Ensure the path includes a '/' | |
1916 | */ | |
a1515161 | 1917 | if ((out == 0) && (bas->server != NULL)) { |
be95adaf | 1918 | res->path[out++] = '/'; |
a1515161 | 1919 | } |
be95adaf SH |
1920 | while (ref->path[indx] != 0) { |
1921 | res->path[out++] = ref->path[indx++]; | |
1922 | } | |
ca0defb9 PB |
1923 | } |
1924 | res->path[out] = 0; | |
1925 | ||
1926 | /* | |
1927 | * Steps c) to h) are really path normalization steps | |
1928 | */ | |
1929 | normalize_uri_path(res->path); | |
1930 | ||
1931 | step_7: | |
1932 | ||
1933 | /* | |
1934 | * 7) The resulting URI components, including any inherited from the | |
1935 | * base URI, are recombined to give the absolute form of the URI | |
1936 | * reference. | |
1937 | */ | |
1938 | val = uri_to_string(res); | |
1939 | ||
1940 | done: | |
a1515161 | 1941 | if (ref != NULL) { |
be95adaf | 1942 | uri_free(ref); |
a1515161 SH |
1943 | } |
1944 | if (bas != NULL) { | |
be95adaf | 1945 | uri_free(bas); |
a1515161 SH |
1946 | } |
1947 | if (res != NULL) { | |
be95adaf | 1948 | uri_free(res); |
a1515161 | 1949 | } |
42fa2725 | 1950 | return val; |
ca0defb9 PB |
1951 | } |
1952 | ||
1953 | /** | |
1954 | * uri_resolve_relative: | |
1955 | * @URI: the URI reference under consideration | |
1956 | * @base: the base value | |
1957 | * | |
1958 | * Expresses the URI of the reference in terms relative to the | |
1959 | * base. Some examples of this operation include: | |
1960 | * base = "http://site1.com/docs/book1.html" | |
1961 | * URI input URI returned | |
1962 | * docs/pic1.gif pic1.gif | |
1963 | * docs/img/pic1.gif img/pic1.gif | |
1964 | * img/pic1.gif ../img/pic1.gif | |
1965 | * http://site1.com/docs/pic1.gif pic1.gif | |
1966 | * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif | |
1967 | * | |
1968 | * base = "docs/book1.html" | |
1969 | * URI input URI returned | |
1970 | * docs/pic1.gif pic1.gif | |
1971 | * docs/img/pic1.gif img/pic1.gif | |
1972 | * img/pic1.gif ../img/pic1.gif | |
1973 | * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif | |
1974 | * | |
1975 | * | |
a93cf9df | 1976 | * Note: if the URI reference is really weird or complicated, it may be |
ca0defb9 PB |
1977 | * worthwhile to first convert it into a "nice" one by calling |
1978 | * uri_resolve (using 'base') before calling this routine, | |
1979 | * since this routine (for reasonable efficiency) assumes URI has | |
1980 | * already been through some validation. | |
1981 | * | |
1982 | * Returns a new URI string (to be freed by the caller) or NULL in case | |
1983 | * error. | |
1984 | */ | |
be95adaf | 1985 | char *uri_resolve_relative(const char *uri, const char *base) |
ca0defb9 PB |
1986 | { |
1987 | char *val = NULL; | |
1988 | int ret; | |
1989 | int ix; | |
1990 | int pos = 0; | |
1991 | int nbslash = 0; | |
1992 | int len; | |
1993 | URI *ref = NULL; | |
1994 | URI *bas = NULL; | |
1995 | char *bptr, *uptr, *vptr; | |
1996 | int remove_path = 0; | |
1997 | ||
a1515161 | 1998 | if ((uri == NULL) || (*uri == 0)) { |
be95adaf | 1999 | return NULL; |
a1515161 | 2000 | } |
ca0defb9 PB |
2001 | |
2002 | /* | |
2003 | * First parse URI into a standard form | |
2004 | */ | |
be95adaf | 2005 | ref = uri_new(); |
ca0defb9 PB |
2006 | /* If URI not already in "relative" form */ |
2007 | if (uri[0] != '.') { | |
be95adaf | 2008 | ret = uri_parse_into(ref, uri); |
a1515161 | 2009 | if (ret != 0) { |
be95adaf | 2010 | goto done; /* Error in URI, return NULL */ |
a1515161 SH |
2011 | } |
2012 | } else { | |
be95adaf | 2013 | ref->path = g_strdup(uri); |
a1515161 | 2014 | } |
ca0defb9 PB |
2015 | |
2016 | /* | |
2017 | * Next parse base into the same standard form | |
2018 | */ | |
2019 | if ((base == NULL) || (*base == 0)) { | |
be95adaf SH |
2020 | val = g_strdup(uri); |
2021 | goto done; | |
ca0defb9 | 2022 | } |
be95adaf | 2023 | bas = uri_new(); |
ca0defb9 | 2024 | if (base[0] != '.') { |
be95adaf | 2025 | ret = uri_parse_into(bas, base); |
a1515161 | 2026 | if (ret != 0) { |
be95adaf | 2027 | goto done; /* Error in base, return NULL */ |
a1515161 SH |
2028 | } |
2029 | } else { | |
be95adaf | 2030 | bas->path = g_strdup(base); |
a1515161 | 2031 | } |
ca0defb9 PB |
2032 | |
2033 | /* | |
2034 | * If the scheme / server on the URI differs from the base, | |
2035 | * just return the URI | |
2036 | */ | |
2037 | if ((ref->scheme != NULL) && | |
be95adaf SH |
2038 | ((bas->scheme == NULL) || (strcmp(bas->scheme, ref->scheme)) || |
2039 | (strcmp(bas->server, ref->server)))) { | |
2040 | val = g_strdup(uri); | |
2041 | goto done; | |
ca0defb9 | 2042 | } |
afb30dde MA |
2043 | if (bas->path == ref->path || |
2044 | (bas->path && ref->path && !strcmp(bas->path, ref->path))) { | |
be95adaf SH |
2045 | val = g_strdup(""); |
2046 | goto done; | |
ca0defb9 PB |
2047 | } |
2048 | if (bas->path == NULL) { | |
be95adaf SH |
2049 | val = g_strdup(ref->path); |
2050 | goto done; | |
ca0defb9 PB |
2051 | } |
2052 | if (ref->path == NULL) { | |
be95adaf SH |
2053 | ref->path = (char *)"/"; |
2054 | remove_path = 1; | |
ca0defb9 PB |
2055 | } |
2056 | ||
2057 | /* | |
2058 | * At this point (at last!) we can compare the two paths | |
2059 | * | |
2060 | * First we take care of the special case where either of the | |
2061 | * two path components may be missing (bug 316224) | |
2062 | */ | |
2063 | if (bas->path == NULL) { | |
be95adaf SH |
2064 | if (ref->path != NULL) { |
2065 | uptr = ref->path; | |
a1515161 | 2066 | if (*uptr == '/') { |
be95adaf | 2067 | uptr++; |
a1515161 | 2068 | } |
be95adaf SH |
2069 | /* exception characters from uri_to_string */ |
2070 | val = uri_string_escape(uptr, "/;&=+$,"); | |
2071 | } | |
2072 | goto done; | |
ca0defb9 PB |
2073 | } |
2074 | bptr = bas->path; | |
2075 | if (ref->path == NULL) { | |
be95adaf | 2076 | for (ix = 0; bptr[ix] != 0; ix++) { |
a1515161 | 2077 | if (bptr[ix] == '/') { |
be95adaf | 2078 | nbslash++; |
a1515161 | 2079 | } |
be95adaf SH |
2080 | } |
2081 | uptr = NULL; | |
2082 | len = 1; /* this is for a string terminator only */ | |
ca0defb9 | 2083 | } else { |
be95adaf SH |
2084 | /* |
2085 | * Next we compare the two strings and find where they first differ | |
2086 | */ | |
a1515161 | 2087 | if ((ref->path[pos] == '.') && (ref->path[pos + 1] == '/')) { |
ca0defb9 | 2088 | pos += 2; |
a1515161 SH |
2089 | } |
2090 | if ((*bptr == '.') && (bptr[1] == '/')) { | |
ca0defb9 | 2091 | bptr += 2; |
a1515161 | 2092 | } else if ((*bptr == '/') && (ref->path[pos] != '/')) { |
be95adaf | 2093 | bptr++; |
a1515161 SH |
2094 | } |
2095 | while ((bptr[pos] == ref->path[pos]) && (bptr[pos] != 0)) { | |
be95adaf | 2096 | pos++; |
a1515161 | 2097 | } |
be95adaf SH |
2098 | |
2099 | if (bptr[pos] == ref->path[pos]) { | |
2100 | val = g_strdup(""); | |
2101 | goto done; /* (I can't imagine why anyone would do this) */ | |
2102 | } | |
2103 | ||
2104 | /* | |
2105 | * In URI, "back up" to the last '/' encountered. This will be the | |
2106 | * beginning of the "unique" suffix of URI | |
2107 | */ | |
2108 | ix = pos; | |
a1515161 | 2109 | if ((ref->path[ix] == '/') && (ix > 0)) { |
be95adaf | 2110 | ix--; |
a1515161 SH |
2111 | } else if ((ref->path[ix] == 0) && (ix > 1) |
2112 | && (ref->path[ix - 1] == '/')) { | |
be95adaf | 2113 | ix -= 2; |
a1515161 | 2114 | } |
be95adaf | 2115 | for (; ix > 0; ix--) { |
a1515161 | 2116 | if (ref->path[ix] == '/') { |
be95adaf | 2117 | break; |
a1515161 | 2118 | } |
be95adaf SH |
2119 | } |
2120 | if (ix == 0) { | |
2121 | uptr = ref->path; | |
2122 | } else { | |
2123 | ix++; | |
2124 | uptr = &ref->path[ix]; | |
2125 | } | |
2126 | ||
2127 | /* | |
2128 | * In base, count the number of '/' from the differing point | |
2129 | */ | |
2130 | if (bptr[pos] != ref->path[pos]) { /* check for trivial URI == base */ | |
2131 | for (; bptr[ix] != 0; ix++) { | |
a1515161 | 2132 | if (bptr[ix] == '/') { |
be95adaf | 2133 | nbslash++; |
a1515161 | 2134 | } |
be95adaf SH |
2135 | } |
2136 | } | |
2137 | len = strlen(uptr) + 1; | |
ca0defb9 PB |
2138 | } |
2139 | ||
2140 | if (nbslash == 0) { | |
a1515161 | 2141 | if (uptr != NULL) { |
be95adaf SH |
2142 | /* exception characters from uri_to_string */ |
2143 | val = uri_string_escape(uptr, "/;&=+$,"); | |
a1515161 | 2144 | } |
be95adaf | 2145 | goto done; |
ca0defb9 PB |
2146 | } |
2147 | ||
2148 | /* | |
2149 | * Allocate just enough space for the returned string - | |
2150 | * length of the remainder of the URI, plus enough space | |
2151 | * for the "../" groups, plus one for the terminator | |
2152 | */ | |
be95adaf | 2153 | val = g_malloc(len + 3 * nbslash); |
ca0defb9 PB |
2154 | vptr = val; |
2155 | /* | |
2156 | * Put in as many "../" as needed | |
2157 | */ | |
be95adaf SH |
2158 | for (; nbslash > 0; nbslash--) { |
2159 | *vptr++ = '.'; | |
2160 | *vptr++ = '.'; | |
2161 | *vptr++ = '/'; | |
ca0defb9 PB |
2162 | } |
2163 | /* | |
2164 | * Finish up with the end of the URI | |
2165 | */ | |
2166 | if (uptr != NULL) { | |
be95adaf SH |
2167 | if ((vptr > val) && (len > 0) && (uptr[0] == '/') && |
2168 | (vptr[-1] == '/')) { | |
2169 | memcpy(vptr, uptr + 1, len - 1); | |
2170 | vptr[len - 2] = 0; | |
2171 | } else { | |
2172 | memcpy(vptr, uptr, len); | |
2173 | vptr[len - 1] = 0; | |
2174 | } | |
ca0defb9 | 2175 | } else { |
be95adaf | 2176 | vptr[len - 1] = 0; |
ca0defb9 PB |
2177 | } |
2178 | ||
2179 | /* escape the freshly-built path */ | |
2180 | vptr = val; | |
be95adaf | 2181 | /* exception characters from uri_to_string */ |
ca0defb9 PB |
2182 | val = uri_string_escape(vptr, "/;&=+$,"); |
2183 | g_free(vptr); | |
2184 | ||
2185 | done: | |
2186 | /* | |
2187 | * Free the working variables | |
2188 | */ | |
a1515161 | 2189 | if (remove_path != 0) { |
ca0defb9 | 2190 | ref->path = NULL; |
a1515161 SH |
2191 | } |
2192 | if (ref != NULL) { | |
be95adaf | 2193 | uri_free(ref); |
a1515161 SH |
2194 | } |
2195 | if (bas != NULL) { | |
be95adaf | 2196 | uri_free(bas); |
a1515161 | 2197 | } |
ca0defb9 PB |
2198 | |
2199 | return val; | |
2200 | } | |
2201 | ||
2202 | /* | |
2203 | * Utility functions to help parse and assemble query strings. | |
2204 | */ | |
2205 | ||
be95adaf | 2206 | struct QueryParams *query_params_new(int init_alloc) |
ca0defb9 PB |
2207 | { |
2208 | struct QueryParams *ps; | |
2209 | ||
a1515161 | 2210 | if (init_alloc <= 0) { |
be95adaf | 2211 | init_alloc = 1; |
a1515161 | 2212 | } |
ca0defb9 PB |
2213 | |
2214 | ps = g_new(QueryParams, 1); | |
2215 | ps->n = 0; | |
2216 | ps->alloc = init_alloc; | |
2217 | ps->p = g_new(QueryParam, ps->alloc); | |
2218 | ||
2219 | return ps; | |
2220 | } | |
2221 | ||
2222 | /* Ensure there is space to store at least one more parameter | |
2223 | * at the end of the set. | |
2224 | */ | |
be95adaf SH |
2225 | static int query_params_append(struct QueryParams *ps, const char *name, |
2226 | const char *value) | |
ca0defb9 PB |
2227 | { |
2228 | if (ps->n >= ps->alloc) { | |
2229 | ps->p = g_renew(QueryParam, ps->p, ps->alloc * 2); | |
2230 | ps->alloc *= 2; | |
2231 | } | |
2232 | ||
2233 | ps->p[ps->n].name = g_strdup(name); | |
7f303adc | 2234 | ps->p[ps->n].value = g_strdup(value); |
ca0defb9 PB |
2235 | ps->p[ps->n].ignore = 0; |
2236 | ps->n++; | |
2237 | ||
2238 | return 0; | |
2239 | } | |
2240 | ||
be95adaf | 2241 | void query_params_free(struct QueryParams *ps) |
ca0defb9 PB |
2242 | { |
2243 | int i; | |
2244 | ||
2245 | for (i = 0; i < ps->n; ++i) { | |
be95adaf SH |
2246 | g_free(ps->p[i].name); |
2247 | g_free(ps->p[i].value); | |
ca0defb9 | 2248 | } |
be95adaf SH |
2249 | g_free(ps->p); |
2250 | g_free(ps); | |
ca0defb9 PB |
2251 | } |
2252 | ||
be95adaf | 2253 | struct QueryParams *query_params_parse(const char *query) |
ca0defb9 PB |
2254 | { |
2255 | struct QueryParams *ps; | |
2256 | const char *end, *eq; | |
2257 | ||
be95adaf | 2258 | ps = query_params_new(0); |
a1515161 | 2259 | if (!query || query[0] == '\0') { |
be95adaf | 2260 | return ps; |
a1515161 | 2261 | } |
ca0defb9 PB |
2262 | |
2263 | while (*query) { | |
2264 | char *name = NULL, *value = NULL; | |
2265 | ||
2266 | /* Find the next separator, or end of the string. */ | |
be95adaf | 2267 | end = strchr(query, '&'); |
a1515161 | 2268 | if (!end) { |
be95adaf | 2269 | end = strchr(query, ';'); |
a1515161 SH |
2270 | } |
2271 | if (!end) { | |
be95adaf | 2272 | end = query + strlen(query); |
a1515161 | 2273 | } |
ca0defb9 PB |
2274 | |
2275 | /* Find the first '=' character between here and end. */ | |
be95adaf | 2276 | eq = strchr(query, '='); |
a1515161 | 2277 | if (eq && eq >= end) { |
be95adaf | 2278 | eq = NULL; |
a1515161 | 2279 | } |
ca0defb9 PB |
2280 | |
2281 | /* Empty section (eg. "&&"). */ | |
a1515161 | 2282 | if (end == query) { |
ca0defb9 | 2283 | goto next; |
a1515161 | 2284 | } |
ca0defb9 PB |
2285 | |
2286 | /* If there is no '=' character, then we have just "name" | |
2287 | * and consistent with CGI.pm we assume value is "". | |
2288 | */ | |
2289 | else if (!eq) { | |
be95adaf | 2290 | name = uri_string_unescape(query, end - query, NULL); |
ca0defb9 PB |
2291 | value = NULL; |
2292 | } | |
2293 | /* Or if we have "name=" here (works around annoying | |
2294 | * problem when calling uri_string_unescape with len = 0). | |
2295 | */ | |
be95adaf SH |
2296 | else if (eq + 1 == end) { |
2297 | name = uri_string_unescape(query, eq - query, NULL); | |
ca0defb9 PB |
2298 | value = g_new0(char, 1); |
2299 | } | |
2300 | /* If the '=' character is at the beginning then we have | |
2301 | * "=value" and consistent with CGI.pm we _ignore_ this. | |
2302 | */ | |
a1515161 | 2303 | else if (query == eq) { |
ca0defb9 | 2304 | goto next; |
a1515161 | 2305 | } |
ca0defb9 PB |
2306 | |
2307 | /* Otherwise it's "name=value". */ | |
2308 | else { | |
be95adaf SH |
2309 | name = uri_string_unescape(query, eq - query, NULL); |
2310 | value = uri_string_unescape(eq + 1, end - (eq + 1), NULL); | |
ca0defb9 PB |
2311 | } |
2312 | ||
2313 | /* Append to the parameter set. */ | |
be95adaf | 2314 | query_params_append(ps, name, value); |
ca0defb9 PB |
2315 | g_free(name); |
2316 | g_free(value); | |
2317 | ||
2318 | next: | |
2319 | query = end; | |
a1515161 | 2320 | if (*query) { |
be95adaf | 2321 | query++; /* skip '&' separator */ |
a1515161 | 2322 | } |
ca0defb9 PB |
2323 | } |
2324 | ||
2325 | return ps; | |
2326 | } |