]>
git.proxmox.com Git - mirror_qemu.git/blob - util/uri.c
2 * uri.c: set of generic URI related routines
4 * Reference: RFCs 3986, 2732 and 2373
6 * Copyright (C) 1998-2003 Daniel Veillard. All Rights Reserved.
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
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.
33 * Copyright (C) 2007, 2009-2010 Red Hat, Inc.
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.
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.
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
50 * Richard W.M. Jones <rjones@redhat.com>
54 #include "qemu/osdep.h"
55 #include "qemu/cutils.h"
59 static void uri_clean(URI
*uri
);
62 * Old rule from 2396 used in legacy handling code
63 * alpha = lowalpha | upalpha
65 #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
68 * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
69 * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
70 * "u" | "v" | "w" | "x" | "y" | "z"
73 #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z'))
76 * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" |
77 * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" |
78 * "U" | "V" | "W" | "X" | "Y" | "Z"
80 #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z'))
86 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
88 #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
91 * alphanum = alpha | digit
94 #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
97 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
100 #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
101 ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
102 ((x) == '(') || ((x) == ')'))
105 * unwise = "{" | "}" | "|" | "\" | "^" | "`"
108 #define IS_UNWISE(p) \
109 (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \
110 ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \
111 ((*(p) == ']')) || ((*(p) == '`')))
113 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
117 #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
118 ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
119 ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \
123 * unreserved = alphanum | mark
126 #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
129 * Skip to next pointer char, handle escaped sequences
132 #define NEXT(p) ((*p == '%') ? p += 3 : p++)
135 * Productions from the spec.
137 * authority = server | reg_name
138 * reg_name = 1*( unreserved | escaped | "$" | "," |
139 * ";" | ":" | "@" | "&" | "=" | "+" )
141 * path = [ abs_path | opaque_part ]
144 /************************************************************************
148 ************************************************************************/
150 #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9'))
151 #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \
152 ((*(p) >= 'A') && (*(p) <= 'Z')))
153 #define ISA_HEXDIG(p) \
154 (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \
155 ((*(p) >= 'A') && (*(p) <= 'F')))
158 * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
159 * / "*" / "+" / "," / ";" / "="
161 #define ISA_SUB_DELIM(p) \
162 (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \
163 ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \
164 ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \
165 ((*(p) == '=')) || ((*(p) == '\'')))
168 * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
170 #define ISA_GEN_DELIM(p) \
171 (((*(p) == ':')) || ((*(p) == '/')) || ((*(p) == '?')) || \
172 ((*(p) == '#')) || ((*(p) == '[')) || ((*(p) == ']')) || \
176 * reserved = gen-delims / sub-delims
178 #define ISA_RESERVED(p) (ISA_GEN_DELIM(p) || (ISA_SUB_DELIM(p)))
181 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
183 #define ISA_UNRESERVED(p) \
184 ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \
185 ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
188 * pct-encoded = "%" HEXDIG HEXDIG
190 #define ISA_PCT_ENCODED(p) \
191 ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2)))
194 * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
196 #define ISA_PCHAR(p) \
197 (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \
198 ((*(p) == ':')) || ((*(p) == '@')))
201 * rfc3986_parse_scheme:
202 * @uri: pointer to an URI structure
203 * @str: pointer to the string to analyze
205 * Parse an URI scheme
207 * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
209 * Returns 0 or the error code
211 static int rfc3986_parse_scheme(URI
*uri
, const char **str
)
220 if (!ISA_ALPHA(cur
)) {
224 while (ISA_ALPHA(cur
) || ISA_DIGIT(cur
) || (*cur
== '+') || (*cur
== '-') ||
230 uri
->scheme
= g_strndup(*str
, cur
- *str
);
237 * rfc3986_parse_fragment:
238 * @uri: pointer to an URI structure
239 * @str: pointer to the string to analyze
241 * Parse the query part of an URI
243 * fragment = *( pchar / "/" / "?" )
244 * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']'
245 * in the fragment identifier but this is used very broadly for
246 * xpointer scheme selection, so we are allowing it here to not break
247 * for example all the DocBook processing chains.
249 * Returns 0 or the error code
251 static int rfc3986_parse_fragment(URI
*uri
, const char **str
)
261 while ((ISA_PCHAR(cur
)) || (*cur
== '/') || (*cur
== '?') ||
262 (*cur
== '[') || (*cur
== ']') ||
263 ((uri
!= NULL
) && (uri
->cleanup
& 1) && (IS_UNWISE(cur
)))) {
267 g_free(uri
->fragment
);
268 if (uri
->cleanup
& 2) {
269 uri
->fragment
= g_strndup(*str
, cur
- *str
);
271 uri
->fragment
= uri_string_unescape(*str
, cur
- *str
, NULL
);
279 * rfc3986_parse_query:
280 * @uri: pointer to an URI structure
281 * @str: pointer to the string to analyze
283 * Parse the query part of an URI
287 * Returns 0 or the error code
289 static int rfc3986_parse_query(URI
*uri
, const char **str
)
299 while ((ISA_PCHAR(cur
)) || (*cur
== '/') || (*cur
== '?') ||
300 ((uri
!= NULL
) && (uri
->cleanup
& 1) && (IS_UNWISE(cur
)))) {
305 uri
->query
= g_strndup(*str
, cur
- *str
);
312 * rfc3986_parse_port:
313 * @uri: pointer to an URI structure
314 * @str: the string to analyze
316 * Parse a port part and fills in the appropriate fields
317 * of the @uri structure
321 * Returns 0 or the error code
323 static int rfc3986_parse_port(URI
*uri
, const char **str
)
325 const char *cur
= *str
;
328 if (ISA_DIGIT(cur
)) {
329 while (ISA_DIGIT(cur
)) {
330 port
= port
* 10 + (*cur
- '0');
346 * rfc3986_parse_user_info:
347 * @uri: pointer to an URI structure
348 * @str: the string to analyze
350 * Parse a user information part and fill in the appropriate fields
351 * of the @uri structure
353 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
355 * Returns 0 or the error code
357 static int rfc3986_parse_user_info(URI
*uri
, const char **str
)
362 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) || ISA_SUB_DELIM(cur
) ||
369 if (uri
->cleanup
& 2) {
370 uri
->user
= g_strndup(*str
, cur
- *str
);
372 uri
->user
= uri_string_unescape(*str
, cur
- *str
, NULL
);
382 * rfc3986_parse_dec_octet:
383 * @str: the string to analyze
385 * dec-octet = DIGIT ; 0-9
386 * / %x31-39 DIGIT ; 10-99
387 * / "1" 2DIGIT ; 100-199
388 * / "2" %x30-34 DIGIT ; 200-249
389 * / "25" %x30-35 ; 250-255
393 * Returns 0 if found and skipped, 1 otherwise
395 static int rfc3986_parse_dec_octet(const char **str
)
397 const char *cur
= *str
;
399 if (!(ISA_DIGIT(cur
))) {
402 if (!ISA_DIGIT(cur
+ 1)) {
404 } else if ((*cur
!= '0') && (ISA_DIGIT(cur
+ 1)) && (!ISA_DIGIT(cur
+ 2))) {
406 } else if ((*cur
== '1') && (ISA_DIGIT(cur
+ 1)) && (ISA_DIGIT(cur
+ 2))) {
408 } else if ((*cur
== '2') && (*(cur
+ 1) >= '0') && (*(cur
+ 1) <= '4') &&
409 (ISA_DIGIT(cur
+ 2))) {
411 } else if ((*cur
== '2') && (*(cur
+ 1) == '5') && (*(cur
+ 2) >= '0') &&
412 (*(cur
+ 1) <= '5')) {
421 * rfc3986_parse_host:
422 * @uri: pointer to an URI structure
423 * @str: the string to analyze
425 * Parse an host part and fills in the appropriate fields
426 * of the @uri structure
428 * host = IP-literal / IPv4address / reg-name
429 * IP-literal = "[" ( IPv6address / IPvFuture ) "]"
430 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
431 * reg-name = *( unreserved / pct-encoded / sub-delims )
433 * Returns 0 or the error code
435 static int rfc3986_parse_host(URI
*uri
, const char **str
)
437 const char *cur
= *str
;
442 * IPv6 and future addressing scheme are enclosed between brackets
446 while ((*cur
!= ']') && (*cur
!= 0)) {
456 * try to parse an IPv4
458 if (ISA_DIGIT(cur
)) {
459 if (rfc3986_parse_dec_octet(&cur
) != 0) {
466 if (rfc3986_parse_dec_octet(&cur
) != 0) {
472 if (rfc3986_parse_dec_octet(&cur
) != 0) {
478 if (rfc3986_parse_dec_octet(&cur
) != 0) {
486 * then this should be a hostname which can be empty
488 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) || ISA_SUB_DELIM(cur
)) {
493 g_free(uri
->authority
);
494 uri
->authority
= NULL
;
497 if (uri
->cleanup
& 2) {
498 uri
->server
= g_strndup(host
, cur
- host
);
500 uri
->server
= uri_string_unescape(host
, cur
- host
, NULL
);
511 * rfc3986_parse_authority:
512 * @uri: pointer to an URI structure
513 * @str: the string to analyze
515 * Parse an authority part and fills in the appropriate fields
516 * of the @uri structure
518 * authority = [ userinfo "@" ] host [ ":" port ]
520 * Returns 0 or the error code
522 static int rfc3986_parse_authority(URI
*uri
, const char **str
)
529 * try to parse a userinfo and check for the trailing @
531 ret
= rfc3986_parse_user_info(uri
, &cur
);
532 if ((ret
!= 0) || (*cur
!= '@')) {
537 ret
= rfc3986_parse_host(uri
, &cur
);
543 ret
= rfc3986_parse_port(uri
, &cur
);
553 * rfc3986_parse_segment:
554 * @str: the string to analyze
555 * @forbid: an optional forbidden character
556 * @empty: allow an empty segment
558 * Parse a segment and fills in the appropriate fields
559 * of the @uri structure
562 * segment-nz = 1*pchar
563 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
564 * ; non-zero-length segment without any colon ":"
566 * Returns 0 or the error code
568 static int rfc3986_parse_segment(const char **str
, char forbid
, int empty
)
573 if (!ISA_PCHAR(cur
)) {
579 while (ISA_PCHAR(cur
) && (*cur
!= forbid
)) {
587 * rfc3986_parse_path_ab_empty:
588 * @uri: pointer to an URI structure
589 * @str: the string to analyze
591 * Parse an path absolute or empty and fills in the appropriate fields
592 * of the @uri structure
594 * path-abempty = *( "/" segment )
596 * Returns 0 or the error code
598 static int rfc3986_parse_path_ab_empty(URI
*uri
, const char **str
)
605 while (*cur
== '/') {
607 ret
= rfc3986_parse_segment(&cur
, 0, 1);
615 if (uri
->cleanup
& 2) {
616 uri
->path
= g_strndup(*str
, cur
- *str
);
618 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
629 * rfc3986_parse_path_absolute:
630 * @uri: pointer to an URI structure
631 * @str: the string to analyze
633 * Parse an path absolute and fills in the appropriate fields
634 * of the @uri structure
636 * path-absolute = "/" [ segment-nz *( "/" segment ) ]
638 * Returns 0 or the error code
640 static int rfc3986_parse_path_absolute(URI
*uri
, const char **str
)
651 ret
= rfc3986_parse_segment(&cur
, 0, 0);
653 while (*cur
== '/') {
655 ret
= rfc3986_parse_segment(&cur
, 0, 1);
664 if (uri
->cleanup
& 2) {
665 uri
->path
= g_strndup(*str
, cur
- *str
);
667 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
678 * rfc3986_parse_path_rootless:
679 * @uri: pointer to an URI structure
680 * @str: the string to analyze
682 * Parse an path without root and fills in the appropriate fields
683 * of the @uri structure
685 * path-rootless = segment-nz *( "/" segment )
687 * Returns 0 or the error code
689 static int rfc3986_parse_path_rootless(URI
*uri
, const char **str
)
696 ret
= rfc3986_parse_segment(&cur
, 0, 0);
700 while (*cur
== '/') {
702 ret
= rfc3986_parse_segment(&cur
, 0, 1);
710 if (uri
->cleanup
& 2) {
711 uri
->path
= g_strndup(*str
, cur
- *str
);
713 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
724 * rfc3986_parse_path_no_scheme:
725 * @uri: pointer to an URI structure
726 * @str: the string to analyze
728 * Parse an path which is not a scheme and fills in the appropriate fields
729 * of the @uri structure
731 * path-noscheme = segment-nz-nc *( "/" segment )
733 * Returns 0 or the error code
735 static int rfc3986_parse_path_no_scheme(URI
*uri
, const char **str
)
742 ret
= rfc3986_parse_segment(&cur
, ':', 0);
746 while (*cur
== '/') {
748 ret
= rfc3986_parse_segment(&cur
, 0, 1);
756 if (uri
->cleanup
& 2) {
757 uri
->path
= g_strndup(*str
, cur
- *str
);
759 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
770 * rfc3986_parse_hier_part:
771 * @uri: pointer to an URI structure
772 * @str: the string to analyze
774 * Parse an hierarchical part and fills in the appropriate fields
775 * of the @uri structure
777 * hier-part = "//" authority path-abempty
782 * Returns 0 or the error code
784 static int rfc3986_parse_hier_part(URI
*uri
, const char **str
)
791 if ((*cur
== '/') && (*(cur
+ 1) == '/')) {
793 ret
= rfc3986_parse_authority(uri
, &cur
);
797 ret
= rfc3986_parse_path_ab_empty(uri
, &cur
);
803 } else if (*cur
== '/') {
804 ret
= rfc3986_parse_path_absolute(uri
, &cur
);
808 } else if (ISA_PCHAR(cur
)) {
809 ret
= rfc3986_parse_path_rootless(uri
, &cur
);
814 /* path-empty is effectively empty */
825 * rfc3986_parse_relative_ref:
826 * @uri: pointer to an URI structure
827 * @str: the string to analyze
829 * Parse an URI string and fills in the appropriate fields
830 * of the @uri structure
832 * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
833 * relative-part = "//" authority path-abempty
838 * Returns 0 or the error code
840 static int rfc3986_parse_relative_ref(URI
*uri
, const char *str
)
844 if ((*str
== '/') && (*(str
+ 1) == '/')) {
846 ret
= rfc3986_parse_authority(uri
, &str
);
850 ret
= rfc3986_parse_path_ab_empty(uri
, &str
);
854 } else if (*str
== '/') {
855 ret
= rfc3986_parse_path_absolute(uri
, &str
);
859 } else if (ISA_PCHAR(str
)) {
860 ret
= rfc3986_parse_path_no_scheme(uri
, &str
);
865 /* path-empty is effectively empty */
874 ret
= rfc3986_parse_query(uri
, &str
);
881 ret
= rfc3986_parse_fragment(uri
, &str
);
895 * @uri: pointer to an URI structure
896 * @str: the string to analyze
898 * Parse an URI string and fills in the appropriate fields
899 * of the @uri structure
901 * scheme ":" hier-part [ "?" query ] [ "#" fragment ]
903 * Returns 0 or the error code
905 static int rfc3986_parse(URI
*uri
, const char *str
)
909 ret
= rfc3986_parse_scheme(uri
, &str
);
917 ret
= rfc3986_parse_hier_part(uri
, &str
);
923 ret
= rfc3986_parse_query(uri
, &str
);
930 ret
= rfc3986_parse_fragment(uri
, &str
);
943 * rfc3986_parse_uri_reference:
944 * @uri: pointer to an URI structure
945 * @str: the string to analyze
947 * Parse an URI reference string and fills in the appropriate fields
948 * of the @uri structure
950 * URI-reference = URI / relative-ref
952 * Returns 0 or the error code
954 static int rfc3986_parse_uri_reference(URI
*uri
, const char *str
)
964 * Try first to parse absolute refs, then fallback to relative if
967 ret
= rfc3986_parse(uri
, str
);
970 ret
= rfc3986_parse_relative_ref(uri
, str
);
981 * @str: the URI string to analyze
983 * Parse an URI based on RFC 3986
985 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
987 * Returns a newly built URI or NULL in case of error
989 URI
*uri_parse(const char *str
)
998 ret
= rfc3986_parse_uri_reference(uri
, str
);
1008 * @uri: pointer to an URI structure
1009 * @str: the string to analyze
1011 * Parse an URI reference string based on RFC 3986 and fills in the
1012 * appropriate fields of the @uri structure
1014 * URI-reference = URI / relative-ref
1016 * Returns 0 or the error code
1018 int uri_parse_into(URI
*uri
, const char *str
)
1020 return rfc3986_parse_uri_reference(uri
, str
);
1025 * @str: the URI string to analyze
1026 * @raw: if 1 unescaping of URI pieces are disabled
1028 * Parse an URI but allows to keep intact the original fragments.
1030 * URI-reference = URI / relative-ref
1032 * Returns a newly built URI or NULL in case of error
1034 URI
*uri_parse_raw(const char *str
, int raw
)
1046 ret
= uri_parse_into(uri
, str
);
1054 /************************************************************************
1056 * Generic URI structure functions *
1058 ************************************************************************/
1063 * Simply creates an empty URI
1065 * Returns the new structure or NULL in case of error
1069 return g_new0(URI
, 1);
1075 * Function to handle properly a reallocation when saving an URI
1076 * Also imposes some limit on the length of an URI string output
1078 static char *realloc2n(char *ret
, int *max
)
1084 temp
= g_realloc(ret
, (tmp
+ 1));
1091 * @uri: pointer to an URI
1093 * Save the URI as an escaped string
1095 * Returns a new string (to be deallocated by caller)
1097 char *uri_to_string(URI
*uri
)
1110 ret
= g_malloc(max
+ 1);
1113 if (uri
->scheme
!= NULL
) {
1117 temp
= realloc2n(ret
, &max
);
1123 temp
= realloc2n(ret
, &max
);
1128 if (uri
->opaque
!= NULL
) {
1131 if (len
+ 3 >= max
) {
1132 temp
= realloc2n(ret
, &max
);
1135 if (IS_RESERVED(*(p
)) || IS_UNRESERVED(*(p
))) {
1138 int val
= *(unsigned char *)p
++;
1139 int hi
= val
/ 0x10, lo
= val
% 0x10;
1141 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1142 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1146 if (uri
->server
!= NULL
) {
1147 if (len
+ 3 >= max
) {
1148 temp
= realloc2n(ret
, &max
);
1153 if (uri
->user
!= NULL
) {
1156 if (len
+ 3 >= max
) {
1157 temp
= realloc2n(ret
, &max
);
1160 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == ';')) ||
1161 ((*(p
) == ':')) || ((*(p
) == '&')) || ((*(p
) == '=')) ||
1162 ((*(p
) == '+')) || ((*(p
) == '$')) || ((*(p
) == ','))) {
1165 int val
= *(unsigned char *)p
++;
1166 int hi
= val
/ 0x10, lo
= val
% 0x10;
1168 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1169 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1172 if (len
+ 3 >= max
) {
1173 temp
= realloc2n(ret
, &max
);
1181 temp
= realloc2n(ret
, &max
);
1186 if (uri
->port
> 0) {
1187 if (len
+ 10 >= max
) {
1188 temp
= realloc2n(ret
, &max
);
1191 len
+= snprintf(&ret
[len
], max
- len
, ":%d", uri
->port
);
1193 } else if (uri
->authority
!= NULL
) {
1194 if (len
+ 3 >= max
) {
1195 temp
= realloc2n(ret
, &max
);
1202 if (len
+ 3 >= max
) {
1203 temp
= realloc2n(ret
, &max
);
1206 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == '$')) ||
1207 ((*(p
) == ',')) || ((*(p
) == ';')) || ((*(p
) == ':')) ||
1208 ((*(p
) == '@')) || ((*(p
) == '&')) || ((*(p
) == '=')) ||
1212 int val
= *(unsigned char *)p
++;
1213 int hi
= val
/ 0x10, lo
= val
% 0x10;
1215 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1216 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1219 } else if (uri
->scheme
!= NULL
) {
1220 if (len
+ 3 >= max
) {
1221 temp
= realloc2n(ret
, &max
);
1227 if (uri
->path
!= NULL
) {
1230 * the colon in file:///d: should not be escaped or
1231 * Windows accesses fail later.
1233 if ((uri
->scheme
!= NULL
) && (p
[0] == '/') &&
1234 (((p
[1] >= 'a') && (p
[1] <= 'z')) ||
1235 ((p
[1] >= 'A') && (p
[1] <= 'Z'))) &&
1236 (p
[2] == ':') && (!strcmp(uri
->scheme
, "file"))) {
1237 if (len
+ 3 >= max
) {
1238 temp
= realloc2n(ret
, &max
);
1246 if (len
+ 3 >= max
) {
1247 temp
= realloc2n(ret
, &max
);
1250 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == '/')) ||
1251 ((*(p
) == ';')) || ((*(p
) == '@')) || ((*(p
) == '&')) ||
1252 ((*(p
) == '=')) || ((*(p
) == '+')) || ((*(p
) == '$')) ||
1256 int val
= *(unsigned char *)p
++;
1257 int hi
= val
/ 0x10, lo
= val
% 0x10;
1259 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1260 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1264 if (uri
->query
!= NULL
) {
1265 if (len
+ 1 >= max
) {
1266 temp
= realloc2n(ret
, &max
);
1272 if (len
+ 1 >= max
) {
1273 temp
= realloc2n(ret
, &max
);
1280 if (uri
->fragment
!= NULL
) {
1281 if (len
+ 3 >= max
) {
1282 temp
= realloc2n(ret
, &max
);
1288 if (len
+ 3 >= max
) {
1289 temp
= realloc2n(ret
, &max
);
1292 if ((IS_UNRESERVED(*(p
))) || (IS_RESERVED(*(p
)))) {
1295 int val
= *(unsigned char *)p
++;
1296 int hi
= val
/ 0x10, lo
= val
% 0x10;
1298 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1299 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1304 temp
= realloc2n(ret
, &max
);
1313 * @uri: pointer to an URI
1315 * Make sure the URI struct is free of content
1317 static void uri_clean(URI
*uri
)
1323 g_free(uri
->scheme
);
1325 g_free(uri
->server
);
1331 g_free(uri
->fragment
);
1332 uri
->fragment
= NULL
;
1333 g_free(uri
->opaque
);
1335 g_free(uri
->authority
);
1336 uri
->authority
= NULL
;
1343 * @uri: pointer to an URI
1345 * Free up the URI struct
1347 void uri_free(URI
*uri
)
1353 /************************************************************************
1355 * Helper functions *
1357 ************************************************************************/
1360 * normalize_uri_path:
1361 * @path: pointer to the path string
1363 * Applies the 5 normalization steps to a path string--that is, RFC 2396
1364 * Section 5.2, steps 6.c through 6.g.
1366 * Normalization occurs directly on the string, no new allocation is done
1368 * Returns 0 or an error code
1370 static int normalize_uri_path(char *path
)
1378 /* Skip all initial "/" chars. We want to get to the beginning of the
1379 * first non-empty segment.
1382 while (cur
[0] == '/') {
1385 if (cur
[0] == '\0') {
1389 /* Keep everything we've seen so far. */
1393 * Analyze each segment in sequence for cases (c) and (d).
1395 while (cur
[0] != '\0') {
1397 * c) All occurrences of "./", where "." is a complete path segment,
1398 * are removed from the buffer string.
1400 if ((cur
[0] == '.') && (cur
[1] == '/')) {
1402 /* '//' normalization should be done at this point too */
1403 while (cur
[0] == '/') {
1410 * d) If the buffer string ends with "." as a complete path segment,
1411 * that "." is removed.
1413 if ((cur
[0] == '.') && (cur
[1] == '\0')) {
1417 /* Otherwise keep the segment. */
1418 while (cur
[0] != '/') {
1419 if (cur
[0] == '\0') {
1422 (out
++)[0] = (cur
++)[0];
1425 while ((cur
[0] == '/') && (cur
[1] == '/')) {
1429 (out
++)[0] = (cur
++)[0];
1434 /* Reset to the beginning of the first segment for the next sequence. */
1436 while (cur
[0] == '/') {
1439 if (cur
[0] == '\0') {
1444 * Analyze each segment in sequence for cases (e) and (f).
1446 * e) All occurrences of "<segment>/../", where <segment> is a
1447 * complete path segment not equal to "..", are removed from the
1448 * buffer string. Removal of these path segments is performed
1449 * iteratively, removing the leftmost matching pattern on each
1450 * iteration, until no matching pattern remains.
1452 * f) If the buffer string ends with "<segment>/..", where <segment>
1453 * is a complete path segment not equal to "..", that
1454 * "<segment>/.." is removed.
1456 * To satisfy the "iterative" clause in (e), we need to collapse the
1457 * string every time we find something that needs to be removed. Thus,
1458 * we don't need to keep two pointers into the string: we only need a
1459 * "current position" pointer.
1464 /* At the beginning of each iteration of this loop, "cur" points to
1465 * the first character of the segment we want to examine.
1468 /* Find the end of the current segment. */
1470 while ((segp
[0] != '/') && (segp
[0] != '\0')) {
1474 /* If this is the last segment, we're done (we need at least two
1475 * segments to meet the criteria for the (e) and (f) cases).
1477 if (segp
[0] == '\0') {
1481 /* If the first segment is "..", or if the next segment _isn't_ "..",
1482 * keep this segment and try the next one.
1485 if (((cur
[0] == '.') && (cur
[1] == '.') && (segp
== cur
+ 3)) ||
1486 ((segp
[0] != '.') || (segp
[1] != '.') ||
1487 ((segp
[2] != '/') && (segp
[2] != '\0')))) {
1492 /* If we get here, remove this segment and the next one and back up
1493 * to the previous segment (if there is one), to implement the
1494 * "iteratively" clause. It's pretty much impossible to back up
1495 * while maintaining two pointers into the buffer, so just compact
1496 * the whole buffer now.
1499 /* If this is the end of the buffer, we're done. */
1500 if (segp
[2] == '\0') {
1504 /* Valgrind complained, strcpy(cur, segp + 3); */
1505 /* string will overlap, do not use strcpy */
1508 while ((*tmp
++ = *segp
++) != 0) {
1509 /* No further work */
1512 /* If there are no previous segments, then keep going from here. */
1514 while ((segp
> path
) && ((--segp
)[0] == '/')) {
1515 /* No further work */
1521 /* "segp" is pointing to the end of a previous segment; find it's
1522 * start. We need to back up to the previous segment and start
1523 * over with that to handle things like "foo/bar/../..". If we
1524 * don't do this, then on the first pass we'll remove the "bar/..",
1525 * but be pointing at the second ".." so we won't realize we can also
1526 * remove the "foo/..".
1529 while ((cur
> path
) && (cur
[-1] != '/')) {
1536 * g) If the resulting buffer string still begins with one or more
1537 * complete path segments of "..", then the reference is
1538 * considered to be in error. Implementations may handle this
1539 * error by retaining these components in the resolved path (i.e.,
1540 * treating them as part of the final URI), by removing them from
1541 * the resolved path (i.e., discarding relative levels above the
1542 * root), or by avoiding traversal of the reference.
1544 * We discard them from the final path.
1546 if (path
[0] == '/') {
1548 while ((cur
[0] == '/') && (cur
[1] == '.') && (cur
[2] == '.') &&
1549 ((cur
[3] == '/') || (cur
[3] == '\0'))) {
1555 while (cur
[0] != '\0') {
1556 (out
++)[0] = (cur
++)[0];
1565 static int is_hex(char c
)
1567 if (((c
>= '0') && (c
<= '9')) || ((c
>= 'a') && (c
<= 'f')) ||
1568 ((c
>= 'A') && (c
<= 'F'))) {
1575 * uri_string_unescape:
1576 * @str: the string to unescape
1577 * @len: the length in bytes to unescape (or <= 0 to indicate full string)
1578 * @target: optional destination buffer
1580 * Unescaping routine, but does not check that the string is an URI. The
1581 * output is a direct unsigned char translation of %XX values (no encoding)
1582 * Note that the length of the result can only be smaller or same size as
1585 * Returns a copy of the string, but unescaped, will return NULL only in case
1588 char *uri_string_unescape(const char *str
, int len
, char *target
)
1603 if (target
== NULL
) {
1604 ret
= g_malloc(len
+ 1);
1611 if ((len
> 2) && (*in
== '%') && (is_hex(in
[1])) && (is_hex(in
[2]))) {
1613 if ((*in
>= '0') && (*in
<= '9')) {
1615 } else if ((*in
>= 'a') && (*in
<= 'f')) {
1616 *out
= (*in
- 'a') + 10;
1617 } else if ((*in
>= 'A') && (*in
<= 'F')) {
1618 *out
= (*in
- 'A') + 10;
1621 if ((*in
>= '0') && (*in
<= '9')) {
1622 *out
= *out
* 16 + (*in
- '0');
1623 } else if ((*in
>= 'a') && (*in
<= 'f')) {
1624 *out
= *out
* 16 + (*in
- 'a') + 10;
1625 } else if ((*in
>= 'A') && (*in
<= 'F')) {
1626 *out
= *out
* 16 + (*in
- 'A') + 10;
1641 * uri_string_escape:
1642 * @str: string to escape
1643 * @list: exception list string of chars not to escape
1645 * This routine escapes a string to hex, ignoring reserved characters (a-z)
1646 * and the characters in the exception list.
1648 * Returns a new escaped string or NULL in case of error.
1650 char *uri_string_escape(const char *str
, const char *list
)
1661 return g_strdup(str
);
1669 ret
= g_malloc(len
);
1673 if (len
- out
<= 3) {
1674 temp
= realloc2n(ret
, &len
);
1680 if ((ch
!= '@') && (!IS_UNRESERVED(ch
)) && (!strchr(list
, ch
))) {
1685 ret
[out
++] = '0' + val
;
1687 ret
[out
++] = 'A' + val
- 0xA;
1691 ret
[out
++] = '0' + val
;
1693 ret
[out
++] = 'A' + val
- 0xA;
1704 /************************************************************************
1706 * Public functions *
1708 ************************************************************************/
1712 * @URI: the URI instance found in the document
1713 * @base: the base value
1715 * Computes he final URI of the reference done by checking that
1716 * the given URI is valid, and building the final URI using the
1717 * base URI. This is processed according to section 5.2 of the
1720 * 5.2. Resolving Relative References to Absolute Form
1722 * Returns a new URI string (to be freed by the caller) or NULL in case
1725 char *uri_resolve(const char *uri
, const char *base
)
1728 int ret
, len
, indx
, cur
, out
;
1734 * 1) The URI reference is parsed into the potential four components and
1735 * fragment identifier, as described in Section 4.3.
1737 * NOTE that a completely empty URI is treated by modern browsers
1738 * as a reference to "." rather than as a synonym for the current
1739 * URI. Should we do that here?
1746 ret
= uri_parse_into(ref
, uri
);
1754 if ((ref
!= NULL
) && (ref
->scheme
!= NULL
)) {
1756 * The URI is absolute don't modify.
1758 val
= g_strdup(uri
);
1765 ret
= uri_parse_into(bas
, base
);
1769 val
= uri_to_string(ref
);
1775 * the base fragment must be ignored
1777 g_free(bas
->fragment
);
1778 bas
->fragment
= NULL
;
1779 val
= uri_to_string(bas
);
1784 * 2) If the path component is empty and the scheme, authority, and
1785 * query components are undefined, then it is a reference to the
1786 * current document and we are done. Otherwise, the reference URI's
1787 * query and fragment components are defined as found (or not found)
1788 * within the URI reference and not inherited from the base URI.
1790 * NOTE that in modern browsers, the parsing differs from the above
1791 * in the following aspect: the query component is allowed to be
1792 * defined while still treating this as a reference to the current
1796 if ((ref
->scheme
== NULL
) && (ref
->path
== NULL
) &&
1797 ((ref
->authority
== NULL
) && (ref
->server
== NULL
))) {
1798 res
->scheme
= g_strdup(bas
->scheme
);
1799 if (bas
->authority
!= NULL
) {
1800 res
->authority
= g_strdup(bas
->authority
);
1801 } else if (bas
->server
!= NULL
) {
1802 res
->server
= g_strdup(bas
->server
);
1803 res
->user
= g_strdup(bas
->user
);
1804 res
->port
= bas
->port
;
1806 res
->path
= g_strdup(bas
->path
);
1807 if (ref
->query
!= NULL
) {
1808 res
->query
= g_strdup(ref
->query
);
1810 res
->query
= g_strdup(bas
->query
);
1812 res
->fragment
= g_strdup(ref
->fragment
);
1817 * 3) If the scheme component is defined, indicating that the reference
1818 * starts with a scheme name, then the reference is interpreted as an
1819 * absolute URI and we are done. Otherwise, the reference URI's
1820 * scheme is inherited from the base URI's scheme component.
1822 if (ref
->scheme
!= NULL
) {
1823 val
= uri_to_string(ref
);
1826 res
->scheme
= g_strdup(bas
->scheme
);
1828 res
->query
= g_strdup(ref
->query
);
1829 res
->fragment
= g_strdup(ref
->fragment
);
1832 * 4) If the authority component is defined, then the reference is a
1833 * network-path and we skip to step 7. Otherwise, the reference
1834 * URI's authority is inherited from the base URI's authority
1835 * component, which will also be undefined if the URI scheme does not
1836 * use an authority component.
1838 if ((ref
->authority
!= NULL
) || (ref
->server
!= NULL
)) {
1839 if (ref
->authority
!= NULL
) {
1840 res
->authority
= g_strdup(ref
->authority
);
1842 res
->server
= g_strdup(ref
->server
);
1843 res
->user
= g_strdup(ref
->user
);
1844 res
->port
= ref
->port
;
1846 res
->path
= g_strdup(ref
->path
);
1849 if (bas
->authority
!= NULL
) {
1850 res
->authority
= g_strdup(bas
->authority
);
1851 } else if (bas
->server
!= NULL
) {
1852 res
->server
= g_strdup(bas
->server
);
1853 res
->user
= g_strdup(bas
->user
);
1854 res
->port
= bas
->port
;
1858 * 5) If the path component begins with a slash character ("/"), then
1859 * the reference is an absolute-path and we skip to step 7.
1861 if ((ref
->path
!= NULL
) && (ref
->path
[0] == '/')) {
1862 res
->path
= g_strdup(ref
->path
);
1867 * 6) If this step is reached, then we are resolving a relative-path
1868 * reference. The relative path needs to be merged with the base
1869 * URI's path. Although there are many ways to do this, we will
1870 * describe a simple method using a separate string buffer.
1872 * Allocate a buffer large enough for the result string.
1874 len
= 2; /* extra / and 0 */
1875 if (ref
->path
!= NULL
) {
1876 len
+= strlen(ref
->path
);
1878 if (bas
->path
!= NULL
) {
1879 len
+= strlen(bas
->path
);
1881 res
->path
= g_malloc(len
);
1885 * a) All but the last segment of the base URI's path component is
1886 * copied to the buffer. In other words, any characters after the
1887 * last (right-most) slash character, if any, are excluded.
1891 if (bas
->path
!= NULL
) {
1892 while (bas
->path
[cur
] != 0) {
1893 while ((bas
->path
[cur
] != 0) && (bas
->path
[cur
] != '/')) {
1896 if (bas
->path
[cur
] == 0) {
1902 res
->path
[out
] = bas
->path
[out
];
1910 * b) The reference's path component is appended to the buffer
1913 if (ref
->path
!= NULL
&& ref
->path
[0] != 0) {
1916 * Ensure the path includes a '/'
1918 if ((out
== 0) && (bas
->server
!= NULL
)) {
1919 res
->path
[out
++] = '/';
1921 while (ref
->path
[indx
] != 0) {
1922 res
->path
[out
++] = ref
->path
[indx
++];
1928 * Steps c) to h) are really path normalization steps
1930 normalize_uri_path(res
->path
);
1935 * 7) The resulting URI components, including any inherited from the
1936 * base URI, are recombined to give the absolute form of the URI
1939 val
= uri_to_string(res
);
1955 * uri_resolve_relative:
1956 * @URI: the URI reference under consideration
1957 * @base: the base value
1959 * Expresses the URI of the reference in terms relative to the
1960 * base. Some examples of this operation include:
1961 * base = "http://site1.com/docs/book1.html"
1962 * URI input URI returned
1963 * docs/pic1.gif pic1.gif
1964 * docs/img/pic1.gif img/pic1.gif
1965 * img/pic1.gif ../img/pic1.gif
1966 * http://site1.com/docs/pic1.gif pic1.gif
1967 * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif
1969 * base = "docs/book1.html"
1970 * URI input URI returned
1971 * docs/pic1.gif pic1.gif
1972 * docs/img/pic1.gif img/pic1.gif
1973 * img/pic1.gif ../img/pic1.gif
1974 * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif
1977 * Note: if the URI reference is really weird or complicated, it may be
1978 * worthwhile to first convert it into a "nice" one by calling
1979 * uri_resolve (using 'base') before calling this routine,
1980 * since this routine (for reasonable efficiency) assumes URI has
1981 * already been through some validation.
1983 * Returns a new URI string (to be freed by the caller) or NULL in case
1986 char *uri_resolve_relative(const char *uri
, const char *base
)
1996 char *bptr
, *uptr
, *vptr
;
1997 int remove_path
= 0;
1999 if ((uri
== NULL
) || (*uri
== 0)) {
2004 * First parse URI into a standard form
2007 /* If URI not already in "relative" form */
2008 if (uri
[0] != '.') {
2009 ret
= uri_parse_into(ref
, uri
);
2011 goto done
; /* Error in URI, return NULL */
2014 ref
->path
= g_strdup(uri
);
2018 * Next parse base into the same standard form
2020 if ((base
== NULL
) || (*base
== 0)) {
2021 val
= g_strdup(uri
);
2025 if (base
[0] != '.') {
2026 ret
= uri_parse_into(bas
, base
);
2028 goto done
; /* Error in base, return NULL */
2031 bas
->path
= g_strdup(base
);
2035 * If the scheme / server on the URI differs from the base,
2036 * just return the URI
2038 if ((ref
->scheme
!= NULL
) &&
2039 ((bas
->scheme
== NULL
) || (strcmp(bas
->scheme
, ref
->scheme
)) ||
2040 (strcmp(bas
->server
, ref
->server
)))) {
2041 val
= g_strdup(uri
);
2044 if (bas
->path
== ref
->path
||
2045 (bas
->path
&& ref
->path
&& !strcmp(bas
->path
, ref
->path
))) {
2049 if (bas
->path
== NULL
) {
2050 val
= g_strdup(ref
->path
);
2053 if (ref
->path
== NULL
) {
2054 ref
->path
= (char *)"/";
2059 * At this point (at last!) we can compare the two paths
2061 * First we take care of the special case where either of the
2062 * two path components may be missing (bug 316224)
2064 if (bas
->path
== NULL
) {
2065 if (ref
->path
!= NULL
) {
2070 /* exception characters from uri_to_string */
2071 val
= uri_string_escape(uptr
, "/;&=+$,");
2076 if (ref
->path
== NULL
) {
2077 for (ix
= 0; bptr
[ix
] != 0; ix
++) {
2078 if (bptr
[ix
] == '/') {
2083 len
= 1; /* this is for a string terminator only */
2086 * Next we compare the two strings and find where they first differ
2088 if ((ref
->path
[pos
] == '.') && (ref
->path
[pos
+ 1] == '/')) {
2091 if ((*bptr
== '.') && (bptr
[1] == '/')) {
2093 } else if ((*bptr
== '/') && (ref
->path
[pos
] != '/')) {
2096 while ((bptr
[pos
] == ref
->path
[pos
]) && (bptr
[pos
] != 0)) {
2100 if (bptr
[pos
] == ref
->path
[pos
]) {
2102 goto done
; /* (I can't imagine why anyone would do this) */
2106 * In URI, "back up" to the last '/' encountered. This will be the
2107 * beginning of the "unique" suffix of URI
2110 if ((ref
->path
[ix
] == '/') && (ix
> 0)) {
2112 } else if ((ref
->path
[ix
] == 0) && (ix
> 1)
2113 && (ref
->path
[ix
- 1] == '/')) {
2116 for (; ix
> 0; ix
--) {
2117 if (ref
->path
[ix
] == '/') {
2125 uptr
= &ref
->path
[ix
];
2129 * In base, count the number of '/' from the differing point
2131 if (bptr
[pos
] != ref
->path
[pos
]) { /* check for trivial URI == base */
2132 for (; bptr
[ix
] != 0; ix
++) {
2133 if (bptr
[ix
] == '/') {
2138 len
= strlen(uptr
) + 1;
2143 /* exception characters from uri_to_string */
2144 val
= uri_string_escape(uptr
, "/;&=+$,");
2150 * Allocate just enough space for the returned string -
2151 * length of the remainder of the URI, plus enough space
2152 * for the "../" groups, plus one for the terminator
2154 val
= g_malloc(len
+ 3 * nbslash
);
2157 * Put in as many "../" as needed
2159 for (; nbslash
> 0; nbslash
--) {
2165 * Finish up with the end of the URI
2168 if ((vptr
> val
) && (len
> 0) && (uptr
[0] == '/') &&
2169 (vptr
[-1] == '/')) {
2170 memcpy(vptr
, uptr
+ 1, len
- 1);
2173 memcpy(vptr
, uptr
, len
);
2180 /* escape the freshly-built path */
2182 /* exception characters from uri_to_string */
2183 val
= uri_string_escape(vptr
, "/;&=+$,");
2188 * Free the working variables
2190 if (remove_path
!= 0) {
2204 * Utility functions to help parse and assemble query strings.
2207 struct QueryParams
*query_params_new(int init_alloc
)
2209 struct QueryParams
*ps
;
2211 if (init_alloc
<= 0) {
2215 ps
= g_new(QueryParams
, 1);
2217 ps
->alloc
= init_alloc
;
2218 ps
->p
= g_new(QueryParam
, ps
->alloc
);
2223 /* Ensure there is space to store at least one more parameter
2224 * at the end of the set.
2226 static int query_params_append(struct QueryParams
*ps
, const char *name
,
2229 if (ps
->n
>= ps
->alloc
) {
2230 ps
->p
= g_renew(QueryParam
, ps
->p
, ps
->alloc
* 2);
2234 ps
->p
[ps
->n
].name
= g_strdup(name
);
2235 ps
->p
[ps
->n
].value
= g_strdup(value
);
2236 ps
->p
[ps
->n
].ignore
= 0;
2242 void query_params_free(struct QueryParams
*ps
)
2246 for (i
= 0; i
< ps
->n
; ++i
) {
2247 g_free(ps
->p
[i
].name
);
2248 g_free(ps
->p
[i
].value
);
2254 struct QueryParams
*query_params_parse(const char *query
)
2256 struct QueryParams
*ps
;
2257 const char *end
, *eq
;
2259 ps
= query_params_new(0);
2260 if (!query
|| query
[0] == '\0') {
2265 char *name
= NULL
, *value
= NULL
;
2267 /* Find the next separator, or end of the string. */
2268 end
= strchr(query
, '&');
2270 end
= qemu_strchrnul(query
, ';');
2273 /* Find the first '=' character between here and end. */
2274 eq
= strchr(query
, '=');
2275 if (eq
&& eq
>= end
) {
2279 /* Empty section (eg. "&&"). */
2284 /* If there is no '=' character, then we have just "name"
2285 * and consistent with CGI.pm we assume value is "".
2288 name
= uri_string_unescape(query
, end
- query
, NULL
);
2291 /* Or if we have "name=" here (works around annoying
2292 * problem when calling uri_string_unescape with len = 0).
2294 else if (eq
+ 1 == end
) {
2295 name
= uri_string_unescape(query
, eq
- query
, NULL
);
2296 value
= g_new0(char, 1);
2298 /* If the '=' character is at the beginning then we have
2299 * "=value" and consistent with CGI.pm we _ignore_ this.
2301 else if (query
== eq
) {
2305 /* Otherwise it's "name=value". */
2307 name
= uri_string_unescape(query
, eq
- query
, NULL
);
2308 value
= uri_string_unescape(eq
+ 1, end
- (eq
+ 1), NULL
);
2311 /* Append to the parameter set. */
2312 query_params_append(ps
, name
, value
);
2319 query
++; /* skip '&' separator */