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, see <https://www.gnu.org/licenses/>.
49 * Richard W.M. Jones <rjones@redhat.com>
53 #include "qemu/osdep.h"
54 #include "qemu/cutils.h"
58 static void uri_clean(URI
*uri
);
61 * Old rule from 2396 used in legacy handling code
62 * alpha = lowalpha | upalpha
64 #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
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"
72 #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z'))
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"
79 #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z'))
85 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
87 #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
90 * alphanum = alpha | digit
93 #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
96 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
99 #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
100 ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
101 ((x) == '(') || ((x) == ')'))
104 * unwise = "{" | "}" | "|" | "\" | "^" | "`"
107 #define IS_UNWISE(p) \
108 (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \
109 ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \
110 ((*(p) == ']')) || ((*(p) == '`')))
112 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
116 #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
117 ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
118 ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \
122 * unreserved = alphanum | mark
125 #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
128 * Skip to next pointer char, handle escaped sequences
131 #define NEXT(p) ((*p == '%') ? p += 3 : p++)
134 * Productions from the spec.
136 * authority = server | reg_name
137 * reg_name = 1*( unreserved | escaped | "$" | "," |
138 * ";" | ":" | "@" | "&" | "=" | "+" )
140 * path = [ abs_path | opaque_part ]
143 /************************************************************************
147 ************************************************************************/
149 #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9'))
150 #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \
151 ((*(p) >= 'A') && (*(p) <= 'Z')))
152 #define ISA_HEXDIG(p) \
153 (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \
154 ((*(p) >= 'A') && (*(p) <= 'F')))
157 * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
158 * / "*" / "+" / "," / ";" / "="
160 #define ISA_SUB_DELIM(p) \
161 (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \
162 ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \
163 ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \
164 ((*(p) == '=')) || ((*(p) == '\'')))
167 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
169 #define ISA_UNRESERVED(p) \
170 ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \
171 ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
174 * pct-encoded = "%" HEXDIG HEXDIG
176 #define ISA_PCT_ENCODED(p) \
177 ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2)))
180 * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
182 #define ISA_PCHAR(p) \
183 (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \
184 ((*(p) == ':')) || ((*(p) == '@')))
187 * rfc3986_parse_scheme:
188 * @uri: pointer to an URI structure
189 * @str: pointer to the string to analyze
191 * Parse an URI scheme
193 * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
195 * Returns 0 or the error code
197 static int rfc3986_parse_scheme(URI
*uri
, const char **str
)
206 if (!ISA_ALPHA(cur
)) {
210 while (ISA_ALPHA(cur
) || ISA_DIGIT(cur
) || (*cur
== '+') || (*cur
== '-') ||
216 uri
->scheme
= g_strndup(*str
, cur
- *str
);
223 * rfc3986_parse_fragment:
224 * @uri: pointer to an URI structure
225 * @str: pointer to the string to analyze
227 * Parse the query part of an URI
229 * fragment = *( pchar / "/" / "?" )
230 * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']'
231 * in the fragment identifier but this is used very broadly for
232 * xpointer scheme selection, so we are allowing it here to not break
233 * for example all the DocBook processing chains.
235 * Returns 0 or the error code
237 static int rfc3986_parse_fragment(URI
*uri
, const char **str
)
247 while ((ISA_PCHAR(cur
)) || (*cur
== '/') || (*cur
== '?') ||
248 (*cur
== '[') || (*cur
== ']') ||
249 ((uri
!= NULL
) && (uri
->cleanup
& 1) && (IS_UNWISE(cur
)))) {
253 g_free(uri
->fragment
);
254 if (uri
->cleanup
& 2) {
255 uri
->fragment
= g_strndup(*str
, cur
- *str
);
257 uri
->fragment
= g_uri_unescape_segment(*str
, cur
, NULL
);
265 * rfc3986_parse_query:
266 * @uri: pointer to an URI structure
267 * @str: pointer to the string to analyze
269 * Parse the query part of an URI
273 * Returns 0 or the error code
275 static int rfc3986_parse_query(URI
*uri
, const char **str
)
285 while ((ISA_PCHAR(cur
)) || (*cur
== '/') || (*cur
== '?') ||
286 ((uri
!= NULL
) && (uri
->cleanup
& 1) && (IS_UNWISE(cur
)))) {
291 uri
->query
= g_strndup(*str
, cur
- *str
);
298 * rfc3986_parse_port:
299 * @uri: pointer to an URI structure
300 * @str: the string to analyze
302 * Parse a port part and fills in the appropriate fields
303 * of the @uri structure
307 * Returns 0 or the error code
309 static int rfc3986_parse_port(URI
*uri
, const char **str
)
311 const char *cur
= *str
;
314 if (ISA_DIGIT(cur
)) {
315 while (ISA_DIGIT(cur
)) {
316 port
= port
* 10 + (*cur
- '0');
332 * rfc3986_parse_user_info:
333 * @uri: pointer to an URI structure
334 * @str: the string to analyze
336 * Parse a user information part and fill in the appropriate fields
337 * of the @uri structure
339 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
341 * Returns 0 or the error code
343 static int rfc3986_parse_user_info(URI
*uri
, const char **str
)
348 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) || ISA_SUB_DELIM(cur
) ||
355 if (uri
->cleanup
& 2) {
356 uri
->user
= g_strndup(*str
, cur
- *str
);
358 uri
->user
= g_uri_unescape_segment(*str
, cur
, NULL
);
368 * rfc3986_parse_dec_octet:
369 * @str: the string to analyze
371 * dec-octet = DIGIT ; 0-9
372 * / %x31-39 DIGIT ; 10-99
373 * / "1" 2DIGIT ; 100-199
374 * / "2" %x30-34 DIGIT ; 200-249
375 * / "25" %x30-35 ; 250-255
379 * Returns 0 if found and skipped, 1 otherwise
381 static int rfc3986_parse_dec_octet(const char **str
)
383 const char *cur
= *str
;
385 if (!(ISA_DIGIT(cur
))) {
388 if (!ISA_DIGIT(cur
+ 1)) {
390 } else if ((*cur
!= '0') && (ISA_DIGIT(cur
+ 1)) && (!ISA_DIGIT(cur
+ 2))) {
392 } else if ((*cur
== '1') && (ISA_DIGIT(cur
+ 1)) && (ISA_DIGIT(cur
+ 2))) {
394 } else if ((*cur
== '2') && (*(cur
+ 1) >= '0') && (*(cur
+ 1) <= '4') &&
395 (ISA_DIGIT(cur
+ 2))) {
397 } else if ((*cur
== '2') && (*(cur
+ 1) == '5') && (*(cur
+ 2) >= '0') &&
398 (*(cur
+ 1) <= '5')) {
407 * rfc3986_parse_host:
408 * @uri: pointer to an URI structure
409 * @str: the string to analyze
411 * Parse an host part and fills in the appropriate fields
412 * of the @uri structure
414 * host = IP-literal / IPv4address / reg-name
415 * IP-literal = "[" ( IPv6address / IPvFuture ) "]"
416 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
417 * reg-name = *( unreserved / pct-encoded / sub-delims )
419 * Returns 0 or the error code
421 static int rfc3986_parse_host(URI
*uri
, const char **str
)
423 const char *cur
= *str
;
428 * IPv6 and future addressing scheme are enclosed between brackets
432 while ((*cur
!= ']') && (*cur
!= 0)) {
442 * try to parse an IPv4
444 if (ISA_DIGIT(cur
)) {
445 if (rfc3986_parse_dec_octet(&cur
) != 0) {
452 if (rfc3986_parse_dec_octet(&cur
) != 0) {
458 if (rfc3986_parse_dec_octet(&cur
) != 0) {
464 if (rfc3986_parse_dec_octet(&cur
) != 0) {
472 * then this should be a hostname which can be empty
474 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) || ISA_SUB_DELIM(cur
)) {
479 g_free(uri
->authority
);
480 uri
->authority
= NULL
;
483 if (uri
->cleanup
& 2) {
484 uri
->server
= g_strndup(host
, cur
- host
);
486 uri
->server
= g_uri_unescape_segment(host
, cur
, NULL
);
497 * rfc3986_parse_authority:
498 * @uri: pointer to an URI structure
499 * @str: the string to analyze
501 * Parse an authority part and fills in the appropriate fields
502 * of the @uri structure
504 * authority = [ userinfo "@" ] host [ ":" port ]
506 * Returns 0 or the error code
508 static int rfc3986_parse_authority(URI
*uri
, const char **str
)
515 * try to parse a userinfo and check for the trailing @
517 ret
= rfc3986_parse_user_info(uri
, &cur
);
518 if ((ret
!= 0) || (*cur
!= '@')) {
523 ret
= rfc3986_parse_host(uri
, &cur
);
529 ret
= rfc3986_parse_port(uri
, &cur
);
539 * rfc3986_parse_segment:
540 * @str: the string to analyze
541 * @forbid: an optional forbidden character
542 * @empty: allow an empty segment
544 * Parse a segment and fills in the appropriate fields
545 * of the @uri structure
548 * segment-nz = 1*pchar
549 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
550 * ; non-zero-length segment without any colon ":"
552 * Returns 0 or the error code
554 static int rfc3986_parse_segment(const char **str
, char forbid
, int empty
)
559 if (!ISA_PCHAR(cur
)) {
565 while (ISA_PCHAR(cur
) && (*cur
!= forbid
)) {
573 * rfc3986_parse_path_ab_empty:
574 * @uri: pointer to an URI structure
575 * @str: the string to analyze
577 * Parse an path absolute or empty and fills in the appropriate fields
578 * of the @uri structure
580 * path-abempty = *( "/" segment )
582 * Returns 0 or the error code
584 static int rfc3986_parse_path_ab_empty(URI
*uri
, const char **str
)
591 while (*cur
== '/') {
593 ret
= rfc3986_parse_segment(&cur
, 0, 1);
601 if (uri
->cleanup
& 2) {
602 uri
->path
= g_strndup(*str
, cur
- *str
);
604 uri
->path
= g_uri_unescape_segment(*str
, cur
, NULL
);
615 * rfc3986_parse_path_absolute:
616 * @uri: pointer to an URI structure
617 * @str: the string to analyze
619 * Parse an path absolute and fills in the appropriate fields
620 * of the @uri structure
622 * path-absolute = "/" [ segment-nz *( "/" segment ) ]
624 * Returns 0 or the error code
626 static int rfc3986_parse_path_absolute(URI
*uri
, const char **str
)
637 ret
= rfc3986_parse_segment(&cur
, 0, 0);
639 while (*cur
== '/') {
641 ret
= rfc3986_parse_segment(&cur
, 0, 1);
650 if (uri
->cleanup
& 2) {
651 uri
->path
= g_strndup(*str
, cur
- *str
);
653 uri
->path
= g_uri_unescape_segment(*str
, cur
, NULL
);
664 * rfc3986_parse_path_rootless:
665 * @uri: pointer to an URI structure
666 * @str: the string to analyze
668 * Parse an path without root and fills in the appropriate fields
669 * of the @uri structure
671 * path-rootless = segment-nz *( "/" segment )
673 * Returns 0 or the error code
675 static int rfc3986_parse_path_rootless(URI
*uri
, const char **str
)
682 ret
= rfc3986_parse_segment(&cur
, 0, 0);
686 while (*cur
== '/') {
688 ret
= rfc3986_parse_segment(&cur
, 0, 1);
696 if (uri
->cleanup
& 2) {
697 uri
->path
= g_strndup(*str
, cur
- *str
);
699 uri
->path
= g_uri_unescape_segment(*str
, cur
, NULL
);
710 * rfc3986_parse_path_no_scheme:
711 * @uri: pointer to an URI structure
712 * @str: the string to analyze
714 * Parse an path which is not a scheme and fills in the appropriate fields
715 * of the @uri structure
717 * path-noscheme = segment-nz-nc *( "/" segment )
719 * Returns 0 or the error code
721 static int rfc3986_parse_path_no_scheme(URI
*uri
, const char **str
)
728 ret
= rfc3986_parse_segment(&cur
, ':', 0);
732 while (*cur
== '/') {
734 ret
= rfc3986_parse_segment(&cur
, 0, 1);
742 if (uri
->cleanup
& 2) {
743 uri
->path
= g_strndup(*str
, cur
- *str
);
745 uri
->path
= g_uri_unescape_segment(*str
, cur
, NULL
);
756 * rfc3986_parse_hier_part:
757 * @uri: pointer to an URI structure
758 * @str: the string to analyze
760 * Parse an hierarchical part and fills in the appropriate fields
761 * of the @uri structure
763 * hier-part = "//" authority path-abempty
768 * Returns 0 or the error code
770 static int rfc3986_parse_hier_part(URI
*uri
, const char **str
)
777 if ((*cur
== '/') && (*(cur
+ 1) == '/')) {
779 ret
= rfc3986_parse_authority(uri
, &cur
);
783 ret
= rfc3986_parse_path_ab_empty(uri
, &cur
);
789 } else if (*cur
== '/') {
790 ret
= rfc3986_parse_path_absolute(uri
, &cur
);
794 } else if (ISA_PCHAR(cur
)) {
795 ret
= rfc3986_parse_path_rootless(uri
, &cur
);
800 /* path-empty is effectively empty */
811 * rfc3986_parse_relative_ref:
812 * @uri: pointer to an URI structure
813 * @str: the string to analyze
815 * Parse an URI string and fills in the appropriate fields
816 * of the @uri structure
818 * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
819 * relative-part = "//" authority path-abempty
824 * Returns 0 or the error code
826 static int rfc3986_parse_relative_ref(URI
*uri
, const char *str
)
830 if ((*str
== '/') && (*(str
+ 1) == '/')) {
832 ret
= rfc3986_parse_authority(uri
, &str
);
836 ret
= rfc3986_parse_path_ab_empty(uri
, &str
);
840 } else if (*str
== '/') {
841 ret
= rfc3986_parse_path_absolute(uri
, &str
);
845 } else if (ISA_PCHAR(str
)) {
846 ret
= rfc3986_parse_path_no_scheme(uri
, &str
);
851 /* path-empty is effectively empty */
860 ret
= rfc3986_parse_query(uri
, &str
);
867 ret
= rfc3986_parse_fragment(uri
, &str
);
881 * @uri: pointer to an URI structure
882 * @str: the string to analyze
884 * Parse an URI string and fills in the appropriate fields
885 * of the @uri structure
887 * scheme ":" hier-part [ "?" query ] [ "#" fragment ]
889 * Returns 0 or the error code
891 static int rfc3986_parse(URI
*uri
, const char *str
)
895 ret
= rfc3986_parse_scheme(uri
, &str
);
903 ret
= rfc3986_parse_hier_part(uri
, &str
);
909 ret
= rfc3986_parse_query(uri
, &str
);
916 ret
= rfc3986_parse_fragment(uri
, &str
);
929 * rfc3986_parse_uri_reference:
930 * @uri: pointer to an URI structure
931 * @str: the string to analyze
933 * Parse an URI reference string and fills in the appropriate fields
934 * of the @uri structure
936 * URI-reference = URI / relative-ref
938 * Returns 0 or the error code
940 static int rfc3986_parse_uri_reference(URI
*uri
, const char *str
)
950 * Try first to parse absolute refs, then fallback to relative if
953 ret
= rfc3986_parse(uri
, str
);
956 ret
= rfc3986_parse_relative_ref(uri
, str
);
967 * @str: the URI string to analyze
969 * Parse an URI based on RFC 3986
971 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
973 * Returns a newly built URI or NULL in case of error
975 URI
*uri_parse(const char *str
)
984 ret
= rfc3986_parse_uri_reference(uri
, str
);
994 * @uri: pointer to an URI structure
995 * @str: the string to analyze
997 * Parse an URI reference string based on RFC 3986 and fills in the
998 * appropriate fields of the @uri structure
1000 * URI-reference = URI / relative-ref
1002 * Returns 0 or the error code
1004 int uri_parse_into(URI
*uri
, const char *str
)
1006 return rfc3986_parse_uri_reference(uri
, str
);
1011 * @str: the URI string to analyze
1012 * @raw: if 1 unescaping of URI pieces are disabled
1014 * Parse an URI but allows to keep intact the original fragments.
1016 * URI-reference = URI / relative-ref
1018 * Returns a newly built URI or NULL in case of error
1020 URI
*uri_parse_raw(const char *str
, int raw
)
1032 ret
= uri_parse_into(uri
, str
);
1040 /************************************************************************
1042 * Generic URI structure functions *
1044 ************************************************************************/
1049 * Simply creates an empty URI
1051 * Returns the new structure or NULL in case of error
1055 return g_new0(URI
, 1);
1061 * Function to handle properly a reallocation when saving an URI
1062 * Also imposes some limit on the length of an URI string output
1064 static char *realloc2n(char *ret
, int *max
)
1070 temp
= g_realloc(ret
, (tmp
+ 1));
1077 * @uri: pointer to an URI
1079 * Save the URI as an escaped string
1081 * Returns a new string (to be deallocated by caller)
1083 char *uri_to_string(URI
*uri
)
1096 ret
= g_malloc(max
+ 1);
1099 if (uri
->scheme
!= NULL
) {
1103 temp
= realloc2n(ret
, &max
);
1109 temp
= realloc2n(ret
, &max
);
1114 if (uri
->opaque
!= NULL
) {
1117 if (len
+ 3 >= max
) {
1118 temp
= realloc2n(ret
, &max
);
1121 if (IS_RESERVED(*(p
)) || IS_UNRESERVED(*(p
))) {
1124 int val
= *(unsigned char *)p
++;
1125 int hi
= val
/ 0x10, lo
= val
% 0x10;
1127 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1128 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1132 if (uri
->server
!= NULL
) {
1133 if (len
+ 3 >= max
) {
1134 temp
= realloc2n(ret
, &max
);
1139 if (uri
->user
!= NULL
) {
1142 if (len
+ 3 >= max
) {
1143 temp
= realloc2n(ret
, &max
);
1146 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == ';')) ||
1147 ((*(p
) == ':')) || ((*(p
) == '&')) || ((*(p
) == '=')) ||
1148 ((*(p
) == '+')) || ((*(p
) == '$')) || ((*(p
) == ','))) {
1151 int val
= *(unsigned char *)p
++;
1152 int hi
= val
/ 0x10, lo
= val
% 0x10;
1154 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1155 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1158 if (len
+ 3 >= max
) {
1159 temp
= realloc2n(ret
, &max
);
1167 temp
= realloc2n(ret
, &max
);
1172 if (uri
->port
> 0) {
1173 if (len
+ 10 >= max
) {
1174 temp
= realloc2n(ret
, &max
);
1177 len
+= snprintf(&ret
[len
], max
- len
, ":%d", uri
->port
);
1179 } else if (uri
->authority
!= NULL
) {
1180 if (len
+ 3 >= max
) {
1181 temp
= realloc2n(ret
, &max
);
1188 if (len
+ 3 >= max
) {
1189 temp
= realloc2n(ret
, &max
);
1192 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == '$')) ||
1193 ((*(p
) == ',')) || ((*(p
) == ';')) || ((*(p
) == ':')) ||
1194 ((*(p
) == '@')) || ((*(p
) == '&')) || ((*(p
) == '=')) ||
1198 int val
= *(unsigned char *)p
++;
1199 int hi
= val
/ 0x10, lo
= val
% 0x10;
1201 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1202 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1205 } else if (uri
->scheme
!= NULL
) {
1206 if (len
+ 3 >= max
) {
1207 temp
= realloc2n(ret
, &max
);
1213 if (uri
->path
!= NULL
) {
1216 * the colon in file:///d: should not be escaped or
1217 * Windows accesses fail later.
1219 if ((uri
->scheme
!= NULL
) && (p
[0] == '/') &&
1220 (((p
[1] >= 'a') && (p
[1] <= 'z')) ||
1221 ((p
[1] >= 'A') && (p
[1] <= 'Z'))) &&
1222 (p
[2] == ':') && (!strcmp(uri
->scheme
, "file"))) {
1223 if (len
+ 3 >= max
) {
1224 temp
= realloc2n(ret
, &max
);
1232 if (len
+ 3 >= max
) {
1233 temp
= realloc2n(ret
, &max
);
1236 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == '/')) ||
1237 ((*(p
) == ';')) || ((*(p
) == '@')) || ((*(p
) == '&')) ||
1238 ((*(p
) == '=')) || ((*(p
) == '+')) || ((*(p
) == '$')) ||
1242 int val
= *(unsigned char *)p
++;
1243 int hi
= val
/ 0x10, lo
= val
% 0x10;
1245 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1246 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1250 if (uri
->query
!= NULL
) {
1251 if (len
+ 1 >= max
) {
1252 temp
= realloc2n(ret
, &max
);
1258 if (len
+ 1 >= max
) {
1259 temp
= realloc2n(ret
, &max
);
1266 if (uri
->fragment
!= NULL
) {
1267 if (len
+ 3 >= max
) {
1268 temp
= realloc2n(ret
, &max
);
1274 if (len
+ 3 >= max
) {
1275 temp
= realloc2n(ret
, &max
);
1278 if ((IS_UNRESERVED(*(p
))) || (IS_RESERVED(*(p
)))) {
1281 int val
= *(unsigned char *)p
++;
1282 int hi
= val
/ 0x10, lo
= val
% 0x10;
1284 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1285 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1290 temp
= realloc2n(ret
, &max
);
1299 * @uri: pointer to an URI
1301 * Make sure the URI struct is free of content
1303 static void uri_clean(URI
*uri
)
1309 g_free(uri
->scheme
);
1311 g_free(uri
->server
);
1317 g_free(uri
->fragment
);
1318 uri
->fragment
= NULL
;
1319 g_free(uri
->opaque
);
1321 g_free(uri
->authority
);
1322 uri
->authority
= NULL
;
1329 * @uri: pointer to an URI, NULL is ignored
1331 * Free up the URI struct
1333 void uri_free(URI
*uri
)
1339 /************************************************************************
1341 * Public functions *
1343 ************************************************************************/
1346 * Utility functions to help parse and assemble query strings.
1349 struct QueryParams
*query_params_new(int init_alloc
)
1351 struct QueryParams
*ps
;
1353 if (init_alloc
<= 0) {
1357 ps
= g_new(QueryParams
, 1);
1359 ps
->alloc
= init_alloc
;
1360 ps
->p
= g_new(QueryParam
, ps
->alloc
);
1365 /* Ensure there is space to store at least one more parameter
1366 * at the end of the set.
1368 static int query_params_append(struct QueryParams
*ps
, const char *name
,
1371 if (ps
->n
>= ps
->alloc
) {
1372 ps
->p
= g_renew(QueryParam
, ps
->p
, ps
->alloc
* 2);
1376 ps
->p
[ps
->n
].name
= g_strdup(name
);
1377 ps
->p
[ps
->n
].value
= g_strdup(value
);
1378 ps
->p
[ps
->n
].ignore
= 0;
1384 void query_params_free(struct QueryParams
*ps
)
1388 for (i
= 0; i
< ps
->n
; ++i
) {
1389 g_free(ps
->p
[i
].name
);
1390 g_free(ps
->p
[i
].value
);
1396 struct QueryParams
*query_params_parse(const char *query
)
1398 struct QueryParams
*ps
;
1399 const char *end
, *eq
;
1401 ps
= query_params_new(0);
1402 if (!query
|| query
[0] == '\0') {
1407 char *name
= NULL
, *value
= NULL
;
1409 /* Find the next separator, or end of the string. */
1410 end
= strchr(query
, '&');
1412 end
= qemu_strchrnul(query
, ';');
1415 /* Find the first '=' character between here and end. */
1416 eq
= strchr(query
, '=');
1417 if (eq
&& eq
>= end
) {
1421 /* Empty section (eg. "&&"). */
1426 /* If there is no '=' character, then we have just "name"
1427 * and consistent with CGI.pm we assume value is "".
1430 name
= g_uri_unescape_segment(query
, end
, NULL
);
1433 /* Or if we have "name=" here (works around annoying
1434 * problem when calling uri_string_unescape with len = 0).
1436 else if (eq
+ 1 == end
) {
1437 name
= g_uri_unescape_segment(query
, eq
, NULL
);
1438 value
= g_new0(char, 1);
1440 /* If the '=' character is at the beginning then we have
1441 * "=value" and consistent with CGI.pm we _ignore_ this.
1443 else if (query
== eq
) {
1447 /* Otherwise it's "name=value". */
1449 name
= g_uri_unescape_segment(query
, eq
, NULL
);
1450 value
= g_uri_unescape_segment(eq
+ 1, end
, NULL
);
1453 /* Append to the parameter set. */
1454 query_params_append(ps
, name
, value
);
1461 query
++; /* skip '&' separator */