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 * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
169 #define ISA_GEN_DELIM(p) \
170 (((*(p) == ':')) || ((*(p) == '/')) || ((*(p) == '?')) || \
171 ((*(p) == '#')) || ((*(p) == '[')) || ((*(p) == ']')) || \
175 * reserved = gen-delims / sub-delims
177 #define ISA_RESERVED(p) (ISA_GEN_DELIM(p) || (ISA_SUB_DELIM(p)))
180 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
182 #define ISA_UNRESERVED(p) \
183 ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \
184 ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
187 * pct-encoded = "%" HEXDIG HEXDIG
189 #define ISA_PCT_ENCODED(p) \
190 ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2)))
193 * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
195 #define ISA_PCHAR(p) \
196 (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \
197 ((*(p) == ':')) || ((*(p) == '@')))
200 * rfc3986_parse_scheme:
201 * @uri: pointer to an URI structure
202 * @str: pointer to the string to analyze
204 * Parse an URI scheme
206 * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
208 * Returns 0 or the error code
210 static int rfc3986_parse_scheme(URI
*uri
, const char **str
)
219 if (!ISA_ALPHA(cur
)) {
223 while (ISA_ALPHA(cur
) || ISA_DIGIT(cur
) || (*cur
== '+') || (*cur
== '-') ||
229 uri
->scheme
= g_strndup(*str
, cur
- *str
);
236 * rfc3986_parse_fragment:
237 * @uri: pointer to an URI structure
238 * @str: pointer to the string to analyze
240 * Parse the query part of an URI
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.
248 * Returns 0 or the error code
250 static int rfc3986_parse_fragment(URI
*uri
, const char **str
)
260 while ((ISA_PCHAR(cur
)) || (*cur
== '/') || (*cur
== '?') ||
261 (*cur
== '[') || (*cur
== ']') ||
262 ((uri
!= NULL
) && (uri
->cleanup
& 1) && (IS_UNWISE(cur
)))) {
266 g_free(uri
->fragment
);
267 if (uri
->cleanup
& 2) {
268 uri
->fragment
= g_strndup(*str
, cur
- *str
);
270 uri
->fragment
= g_uri_unescape_segment(*str
, cur
, NULL
);
278 * rfc3986_parse_query:
279 * @uri: pointer to an URI structure
280 * @str: pointer to the string to analyze
282 * Parse the query part of an URI
286 * Returns 0 or the error code
288 static int rfc3986_parse_query(URI
*uri
, const char **str
)
298 while ((ISA_PCHAR(cur
)) || (*cur
== '/') || (*cur
== '?') ||
299 ((uri
!= NULL
) && (uri
->cleanup
& 1) && (IS_UNWISE(cur
)))) {
304 uri
->query
= g_strndup(*str
, cur
- *str
);
311 * rfc3986_parse_port:
312 * @uri: pointer to an URI structure
313 * @str: the string to analyze
315 * Parse a port part and fills in the appropriate fields
316 * of the @uri structure
320 * Returns 0 or the error code
322 static int rfc3986_parse_port(URI
*uri
, const char **str
)
324 const char *cur
= *str
;
327 if (ISA_DIGIT(cur
)) {
328 while (ISA_DIGIT(cur
)) {
329 port
= port
* 10 + (*cur
- '0');
345 * rfc3986_parse_user_info:
346 * @uri: pointer to an URI structure
347 * @str: the string to analyze
349 * Parse a user information part and fill in the appropriate fields
350 * of the @uri structure
352 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
354 * Returns 0 or the error code
356 static int rfc3986_parse_user_info(URI
*uri
, const char **str
)
361 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) || ISA_SUB_DELIM(cur
) ||
368 if (uri
->cleanup
& 2) {
369 uri
->user
= g_strndup(*str
, cur
- *str
);
371 uri
->user
= g_uri_unescape_segment(*str
, cur
, NULL
);
381 * rfc3986_parse_dec_octet:
382 * @str: the string to analyze
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
392 * Returns 0 if found and skipped, 1 otherwise
394 static int rfc3986_parse_dec_octet(const char **str
)
396 const char *cur
= *str
;
398 if (!(ISA_DIGIT(cur
))) {
401 if (!ISA_DIGIT(cur
+ 1)) {
403 } else if ((*cur
!= '0') && (ISA_DIGIT(cur
+ 1)) && (!ISA_DIGIT(cur
+ 2))) {
405 } else if ((*cur
== '1') && (ISA_DIGIT(cur
+ 1)) && (ISA_DIGIT(cur
+ 2))) {
407 } else if ((*cur
== '2') && (*(cur
+ 1) >= '0') && (*(cur
+ 1) <= '4') &&
408 (ISA_DIGIT(cur
+ 2))) {
410 } else if ((*cur
== '2') && (*(cur
+ 1) == '5') && (*(cur
+ 2) >= '0') &&
411 (*(cur
+ 1) <= '5')) {
420 * rfc3986_parse_host:
421 * @uri: pointer to an URI structure
422 * @str: the string to analyze
424 * Parse an host part and fills in the appropriate fields
425 * of the @uri structure
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 )
432 * Returns 0 or the error code
434 static int rfc3986_parse_host(URI
*uri
, const char **str
)
436 const char *cur
= *str
;
441 * IPv6 and future addressing scheme are enclosed between brackets
445 while ((*cur
!= ']') && (*cur
!= 0)) {
455 * try to parse an IPv4
457 if (ISA_DIGIT(cur
)) {
458 if (rfc3986_parse_dec_octet(&cur
) != 0) {
465 if (rfc3986_parse_dec_octet(&cur
) != 0) {
471 if (rfc3986_parse_dec_octet(&cur
) != 0) {
477 if (rfc3986_parse_dec_octet(&cur
) != 0) {
485 * then this should be a hostname which can be empty
487 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) || ISA_SUB_DELIM(cur
)) {
492 g_free(uri
->authority
);
493 uri
->authority
= NULL
;
496 if (uri
->cleanup
& 2) {
497 uri
->server
= g_strndup(host
, cur
- host
);
499 uri
->server
= g_uri_unescape_segment(host
, cur
, NULL
);
510 * rfc3986_parse_authority:
511 * @uri: pointer to an URI structure
512 * @str: the string to analyze
514 * Parse an authority part and fills in the appropriate fields
515 * of the @uri structure
517 * authority = [ userinfo "@" ] host [ ":" port ]
519 * Returns 0 or the error code
521 static int rfc3986_parse_authority(URI
*uri
, const char **str
)
528 * try to parse a userinfo and check for the trailing @
530 ret
= rfc3986_parse_user_info(uri
, &cur
);
531 if ((ret
!= 0) || (*cur
!= '@')) {
536 ret
= rfc3986_parse_host(uri
, &cur
);
542 ret
= rfc3986_parse_port(uri
, &cur
);
552 * rfc3986_parse_segment:
553 * @str: the string to analyze
554 * @forbid: an optional forbidden character
555 * @empty: allow an empty segment
557 * Parse a segment and fills in the appropriate fields
558 * of the @uri structure
561 * segment-nz = 1*pchar
562 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
563 * ; non-zero-length segment without any colon ":"
565 * Returns 0 or the error code
567 static int rfc3986_parse_segment(const char **str
, char forbid
, int empty
)
572 if (!ISA_PCHAR(cur
)) {
578 while (ISA_PCHAR(cur
) && (*cur
!= forbid
)) {
586 * rfc3986_parse_path_ab_empty:
587 * @uri: pointer to an URI structure
588 * @str: the string to analyze
590 * Parse an path absolute or empty and fills in the appropriate fields
591 * of the @uri structure
593 * path-abempty = *( "/" segment )
595 * Returns 0 or the error code
597 static int rfc3986_parse_path_ab_empty(URI
*uri
, const char **str
)
604 while (*cur
== '/') {
606 ret
= rfc3986_parse_segment(&cur
, 0, 1);
614 if (uri
->cleanup
& 2) {
615 uri
->path
= g_strndup(*str
, cur
- *str
);
617 uri
->path
= g_uri_unescape_segment(*str
, cur
, NULL
);
628 * rfc3986_parse_path_absolute:
629 * @uri: pointer to an URI structure
630 * @str: the string to analyze
632 * Parse an path absolute and fills in the appropriate fields
633 * of the @uri structure
635 * path-absolute = "/" [ segment-nz *( "/" segment ) ]
637 * Returns 0 or the error code
639 static int rfc3986_parse_path_absolute(URI
*uri
, const char **str
)
650 ret
= rfc3986_parse_segment(&cur
, 0, 0);
652 while (*cur
== '/') {
654 ret
= rfc3986_parse_segment(&cur
, 0, 1);
663 if (uri
->cleanup
& 2) {
664 uri
->path
= g_strndup(*str
, cur
- *str
);
666 uri
->path
= g_uri_unescape_segment(*str
, cur
, NULL
);
677 * rfc3986_parse_path_rootless:
678 * @uri: pointer to an URI structure
679 * @str: the string to analyze
681 * Parse an path without root and fills in the appropriate fields
682 * of the @uri structure
684 * path-rootless = segment-nz *( "/" segment )
686 * Returns 0 or the error code
688 static int rfc3986_parse_path_rootless(URI
*uri
, const char **str
)
695 ret
= rfc3986_parse_segment(&cur
, 0, 0);
699 while (*cur
== '/') {
701 ret
= rfc3986_parse_segment(&cur
, 0, 1);
709 if (uri
->cleanup
& 2) {
710 uri
->path
= g_strndup(*str
, cur
- *str
);
712 uri
->path
= g_uri_unescape_segment(*str
, cur
, NULL
);
723 * rfc3986_parse_path_no_scheme:
724 * @uri: pointer to an URI structure
725 * @str: the string to analyze
727 * Parse an path which is not a scheme and fills in the appropriate fields
728 * of the @uri structure
730 * path-noscheme = segment-nz-nc *( "/" segment )
732 * Returns 0 or the error code
734 static int rfc3986_parse_path_no_scheme(URI
*uri
, const char **str
)
741 ret
= rfc3986_parse_segment(&cur
, ':', 0);
745 while (*cur
== '/') {
747 ret
= rfc3986_parse_segment(&cur
, 0, 1);
755 if (uri
->cleanup
& 2) {
756 uri
->path
= g_strndup(*str
, cur
- *str
);
758 uri
->path
= g_uri_unescape_segment(*str
, cur
, NULL
);
769 * rfc3986_parse_hier_part:
770 * @uri: pointer to an URI structure
771 * @str: the string to analyze
773 * Parse an hierarchical part and fills in the appropriate fields
774 * of the @uri structure
776 * hier-part = "//" authority path-abempty
781 * Returns 0 or the error code
783 static int rfc3986_parse_hier_part(URI
*uri
, const char **str
)
790 if ((*cur
== '/') && (*(cur
+ 1) == '/')) {
792 ret
= rfc3986_parse_authority(uri
, &cur
);
796 ret
= rfc3986_parse_path_ab_empty(uri
, &cur
);
802 } else if (*cur
== '/') {
803 ret
= rfc3986_parse_path_absolute(uri
, &cur
);
807 } else if (ISA_PCHAR(cur
)) {
808 ret
= rfc3986_parse_path_rootless(uri
, &cur
);
813 /* path-empty is effectively empty */
824 * rfc3986_parse_relative_ref:
825 * @uri: pointer to an URI structure
826 * @str: the string to analyze
828 * Parse an URI string and fills in the appropriate fields
829 * of the @uri structure
831 * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
832 * relative-part = "//" authority path-abempty
837 * Returns 0 or the error code
839 static int rfc3986_parse_relative_ref(URI
*uri
, const char *str
)
843 if ((*str
== '/') && (*(str
+ 1) == '/')) {
845 ret
= rfc3986_parse_authority(uri
, &str
);
849 ret
= rfc3986_parse_path_ab_empty(uri
, &str
);
853 } else if (*str
== '/') {
854 ret
= rfc3986_parse_path_absolute(uri
, &str
);
858 } else if (ISA_PCHAR(str
)) {
859 ret
= rfc3986_parse_path_no_scheme(uri
, &str
);
864 /* path-empty is effectively empty */
873 ret
= rfc3986_parse_query(uri
, &str
);
880 ret
= rfc3986_parse_fragment(uri
, &str
);
894 * @uri: pointer to an URI structure
895 * @str: the string to analyze
897 * Parse an URI string and fills in the appropriate fields
898 * of the @uri structure
900 * scheme ":" hier-part [ "?" query ] [ "#" fragment ]
902 * Returns 0 or the error code
904 static int rfc3986_parse(URI
*uri
, const char *str
)
908 ret
= rfc3986_parse_scheme(uri
, &str
);
916 ret
= rfc3986_parse_hier_part(uri
, &str
);
922 ret
= rfc3986_parse_query(uri
, &str
);
929 ret
= rfc3986_parse_fragment(uri
, &str
);
942 * rfc3986_parse_uri_reference:
943 * @uri: pointer to an URI structure
944 * @str: the string to analyze
946 * Parse an URI reference string and fills in the appropriate fields
947 * of the @uri structure
949 * URI-reference = URI / relative-ref
951 * Returns 0 or the error code
953 static int rfc3986_parse_uri_reference(URI
*uri
, const char *str
)
963 * Try first to parse absolute refs, then fallback to relative if
966 ret
= rfc3986_parse(uri
, str
);
969 ret
= rfc3986_parse_relative_ref(uri
, str
);
980 * @str: the URI string to analyze
982 * Parse an URI based on RFC 3986
984 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
986 * Returns a newly built URI or NULL in case of error
988 URI
*uri_parse(const char *str
)
997 ret
= rfc3986_parse_uri_reference(uri
, str
);
1007 * @uri: pointer to an URI structure
1008 * @str: the string to analyze
1010 * Parse an URI reference string based on RFC 3986 and fills in the
1011 * appropriate fields of the @uri structure
1013 * URI-reference = URI / relative-ref
1015 * Returns 0 or the error code
1017 int uri_parse_into(URI
*uri
, const char *str
)
1019 return rfc3986_parse_uri_reference(uri
, str
);
1024 * @str: the URI string to analyze
1025 * @raw: if 1 unescaping of URI pieces are disabled
1027 * Parse an URI but allows to keep intact the original fragments.
1029 * URI-reference = URI / relative-ref
1031 * Returns a newly built URI or NULL in case of error
1033 URI
*uri_parse_raw(const char *str
, int raw
)
1045 ret
= uri_parse_into(uri
, str
);
1053 /************************************************************************
1055 * Generic URI structure functions *
1057 ************************************************************************/
1062 * Simply creates an empty URI
1064 * Returns the new structure or NULL in case of error
1068 return g_new0(URI
, 1);
1074 * Function to handle properly a reallocation when saving an URI
1075 * Also imposes some limit on the length of an URI string output
1077 static char *realloc2n(char *ret
, int *max
)
1083 temp
= g_realloc(ret
, (tmp
+ 1));
1090 * @uri: pointer to an URI
1092 * Save the URI as an escaped string
1094 * Returns a new string (to be deallocated by caller)
1096 char *uri_to_string(URI
*uri
)
1109 ret
= g_malloc(max
+ 1);
1112 if (uri
->scheme
!= NULL
) {
1116 temp
= realloc2n(ret
, &max
);
1122 temp
= realloc2n(ret
, &max
);
1127 if (uri
->opaque
!= NULL
) {
1130 if (len
+ 3 >= max
) {
1131 temp
= realloc2n(ret
, &max
);
1134 if (IS_RESERVED(*(p
)) || IS_UNRESERVED(*(p
))) {
1137 int val
= *(unsigned char *)p
++;
1138 int hi
= val
/ 0x10, lo
= val
% 0x10;
1140 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1141 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1145 if (uri
->server
!= NULL
) {
1146 if (len
+ 3 >= max
) {
1147 temp
= realloc2n(ret
, &max
);
1152 if (uri
->user
!= NULL
) {
1155 if (len
+ 3 >= max
) {
1156 temp
= realloc2n(ret
, &max
);
1159 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == ';')) ||
1160 ((*(p
) == ':')) || ((*(p
) == '&')) || ((*(p
) == '=')) ||
1161 ((*(p
) == '+')) || ((*(p
) == '$')) || ((*(p
) == ','))) {
1164 int val
= *(unsigned char *)p
++;
1165 int hi
= val
/ 0x10, lo
= val
% 0x10;
1167 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1168 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1171 if (len
+ 3 >= max
) {
1172 temp
= realloc2n(ret
, &max
);
1180 temp
= realloc2n(ret
, &max
);
1185 if (uri
->port
> 0) {
1186 if (len
+ 10 >= max
) {
1187 temp
= realloc2n(ret
, &max
);
1190 len
+= snprintf(&ret
[len
], max
- len
, ":%d", uri
->port
);
1192 } else if (uri
->authority
!= NULL
) {
1193 if (len
+ 3 >= max
) {
1194 temp
= realloc2n(ret
, &max
);
1201 if (len
+ 3 >= max
) {
1202 temp
= realloc2n(ret
, &max
);
1205 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == '$')) ||
1206 ((*(p
) == ',')) || ((*(p
) == ';')) || ((*(p
) == ':')) ||
1207 ((*(p
) == '@')) || ((*(p
) == '&')) || ((*(p
) == '=')) ||
1211 int val
= *(unsigned char *)p
++;
1212 int hi
= val
/ 0x10, lo
= val
% 0x10;
1214 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1215 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1218 } else if (uri
->scheme
!= NULL
) {
1219 if (len
+ 3 >= max
) {
1220 temp
= realloc2n(ret
, &max
);
1226 if (uri
->path
!= NULL
) {
1229 * the colon in file:///d: should not be escaped or
1230 * Windows accesses fail later.
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
) {
1237 temp
= realloc2n(ret
, &max
);
1245 if (len
+ 3 >= max
) {
1246 temp
= realloc2n(ret
, &max
);
1249 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == '/')) ||
1250 ((*(p
) == ';')) || ((*(p
) == '@')) || ((*(p
) == '&')) ||
1251 ((*(p
) == '=')) || ((*(p
) == '+')) || ((*(p
) == '$')) ||
1255 int val
= *(unsigned char *)p
++;
1256 int hi
= val
/ 0x10, lo
= val
% 0x10;
1258 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1259 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1263 if (uri
->query
!= NULL
) {
1264 if (len
+ 1 >= max
) {
1265 temp
= realloc2n(ret
, &max
);
1271 if (len
+ 1 >= max
) {
1272 temp
= realloc2n(ret
, &max
);
1279 if (uri
->fragment
!= NULL
) {
1280 if (len
+ 3 >= max
) {
1281 temp
= realloc2n(ret
, &max
);
1287 if (len
+ 3 >= max
) {
1288 temp
= realloc2n(ret
, &max
);
1291 if ((IS_UNRESERVED(*(p
))) || (IS_RESERVED(*(p
)))) {
1294 int val
= *(unsigned char *)p
++;
1295 int hi
= val
/ 0x10, lo
= val
% 0x10;
1297 ret
[len
++] = hi
+ (hi
> 9 ? 'A' - 10 : '0');
1298 ret
[len
++] = lo
+ (lo
> 9 ? 'A' - 10 : '0');
1303 temp
= realloc2n(ret
, &max
);
1312 * @uri: pointer to an URI
1314 * Make sure the URI struct is free of content
1316 static void uri_clean(URI
*uri
)
1322 g_free(uri
->scheme
);
1324 g_free(uri
->server
);
1330 g_free(uri
->fragment
);
1331 uri
->fragment
= NULL
;
1332 g_free(uri
->opaque
);
1334 g_free(uri
->authority
);
1335 uri
->authority
= NULL
;
1342 * @uri: pointer to an URI, NULL is ignored
1344 * Free up the URI struct
1346 void uri_free(URI
*uri
)
1352 /************************************************************************
1354 * Helper functions *
1356 ************************************************************************/
1359 * uri_string_escape:
1360 * @str: string to escape
1361 * @list: exception list string of chars not to escape
1363 * This routine escapes a string to hex, ignoring reserved characters (a-z)
1364 * and the characters in the exception list.
1366 * Returns a new escaped string or NULL in case of error.
1368 char *uri_string_escape(const char *str
, const char *list
)
1379 return g_strdup(str
);
1387 ret
= g_malloc(len
);
1391 if (len
- out
<= 3) {
1392 temp
= realloc2n(ret
, &len
);
1398 if ((ch
!= '@') && (!IS_UNRESERVED(ch
)) && (!strchr(list
, ch
))) {
1403 ret
[out
++] = '0' + val
;
1405 ret
[out
++] = 'A' + val
- 0xA;
1409 ret
[out
++] = '0' + val
;
1411 ret
[out
++] = 'A' + val
- 0xA;
1422 /************************************************************************
1424 * Public functions *
1426 ************************************************************************/
1429 * Utility functions to help parse and assemble query strings.
1432 struct QueryParams
*query_params_new(int init_alloc
)
1434 struct QueryParams
*ps
;
1436 if (init_alloc
<= 0) {
1440 ps
= g_new(QueryParams
, 1);
1442 ps
->alloc
= init_alloc
;
1443 ps
->p
= g_new(QueryParam
, ps
->alloc
);
1448 /* Ensure there is space to store at least one more parameter
1449 * at the end of the set.
1451 static int query_params_append(struct QueryParams
*ps
, const char *name
,
1454 if (ps
->n
>= ps
->alloc
) {
1455 ps
->p
= g_renew(QueryParam
, ps
->p
, ps
->alloc
* 2);
1459 ps
->p
[ps
->n
].name
= g_strdup(name
);
1460 ps
->p
[ps
->n
].value
= g_strdup(value
);
1461 ps
->p
[ps
->n
].ignore
= 0;
1467 void query_params_free(struct QueryParams
*ps
)
1471 for (i
= 0; i
< ps
->n
; ++i
) {
1472 g_free(ps
->p
[i
].name
);
1473 g_free(ps
->p
[i
].value
);
1479 struct QueryParams
*query_params_parse(const char *query
)
1481 struct QueryParams
*ps
;
1482 const char *end
, *eq
;
1484 ps
= query_params_new(0);
1485 if (!query
|| query
[0] == '\0') {
1490 char *name
= NULL
, *value
= NULL
;
1492 /* Find the next separator, or end of the string. */
1493 end
= strchr(query
, '&');
1495 end
= qemu_strchrnul(query
, ';');
1498 /* Find the first '=' character between here and end. */
1499 eq
= strchr(query
, '=');
1500 if (eq
&& eq
>= end
) {
1504 /* Empty section (eg. "&&"). */
1509 /* If there is no '=' character, then we have just "name"
1510 * and consistent with CGI.pm we assume value is "".
1513 name
= g_uri_unescape_segment(query
, end
, NULL
);
1516 /* Or if we have "name=" here (works around annoying
1517 * problem when calling uri_string_unescape with len = 0).
1519 else if (eq
+ 1 == end
) {
1520 name
= g_uri_unescape_segment(query
, eq
, NULL
);
1521 value
= g_new0(char, 1);
1523 /* If the '=' character is at the beginning then we have
1524 * "=value" and consistent with CGI.pm we _ignore_ this.
1526 else if (query
== eq
) {
1530 /* Otherwise it's "name=value". */
1532 name
= g_uri_unescape_segment(query
, eq
, NULL
);
1533 value
= g_uri_unescape_segment(eq
+ 1, end
, NULL
);
1536 /* Append to the parameter set. */
1537 query_params_append(ps
, name
, value
);
1544 query
++; /* skip '&' separator */