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"
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))
69 * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
70 * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
71 * "u" | "v" | "w" | "x" | "y" | "z"
74 #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z'))
77 * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" |
78 * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" |
79 * "U" | "V" | "W" | "X" | "Y" | "Z"
81 #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z'))
87 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
89 #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
92 * alphanum = alpha | digit
95 #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
98 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
101 #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
102 ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
103 ((x) == '(') || ((x) == ')'))
106 * unwise = "{" | "}" | "|" | "\" | "^" | "`"
109 #define IS_UNWISE(p) \
110 (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \
111 ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \
112 ((*(p) == ']')) || ((*(p) == '`')))
114 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
118 #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
119 ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
120 ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \
124 * unreserved = alphanum | mark
127 #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
130 * Skip to next pointer char, handle escaped sequences
133 #define NEXT(p) ((*p == '%')? p += 3 : p++)
136 * Productions from the spec.
138 * authority = server | reg_name
139 * reg_name = 1*( unreserved | escaped | "$" | "," |
140 * ";" | ":" | "@" | "&" | "=" | "+" )
142 * path = [ abs_path | opaque_part ]
146 /************************************************************************
150 ************************************************************************/
152 #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9'))
153 #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \
154 ((*(p) >= 'A') && (*(p) <= 'Z')))
155 #define ISA_HEXDIG(p) \
156 (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \
157 ((*(p) >= 'A') && (*(p) <= 'F')))
160 * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
161 * / "*" / "+" / "," / ";" / "="
163 #define ISA_SUB_DELIM(p) \
164 (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \
165 ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \
166 ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \
167 ((*(p) == '=')) || ((*(p) == '\'')))
170 * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
172 #define ISA_GEN_DELIM(p) \
173 (((*(p) == ':')) || ((*(p) == '/')) || ((*(p) == '?')) || \
174 ((*(p) == '#')) || ((*(p) == '[')) || ((*(p) == ']')) || \
178 * reserved = gen-delims / sub-delims
180 #define ISA_RESERVED(p) (ISA_GEN_DELIM(p) || (ISA_SUB_DELIM(p)))
183 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
185 #define ISA_UNRESERVED(p) \
186 ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \
187 ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
190 * pct-encoded = "%" HEXDIG HEXDIG
192 #define ISA_PCT_ENCODED(p) \
193 ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2)))
196 * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
198 #define ISA_PCHAR(p) \
199 (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \
200 ((*(p) == ':')) || ((*(p) == '@')))
203 * rfc3986_parse_scheme:
204 * @uri: pointer to an URI structure
205 * @str: pointer to the string to analyze
207 * Parse an URI scheme
209 * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
211 * Returns 0 or the error code
214 rfc3986_parse_scheme(URI
*uri
, const char **str
) {
224 while (ISA_ALPHA(cur
) || ISA_DIGIT(cur
) ||
225 (*cur
== '+') || (*cur
== '-') || (*cur
== '.')) cur
++;
228 uri
->scheme
= g_strndup(*str
, cur
- *str
);
235 * rfc3986_parse_fragment:
236 * @uri: pointer to an URI structure
237 * @str: pointer to the string to analyze
239 * Parse the query part of an URI
241 * fragment = *( pchar / "/" / "?" )
242 * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']'
243 * in the fragment identifier but this is used very broadly for
244 * xpointer scheme selection, so we are allowing it here to not break
245 * for example all the DocBook processing chains.
247 * Returns 0 or the error code
250 rfc3986_parse_fragment(URI
*uri
, const char **str
)
259 while ((ISA_PCHAR(cur
)) || (*cur
== '/') || (*cur
== '?') ||
260 (*cur
== '[') || (*cur
== ']') ||
261 ((uri
!= NULL
) && (uri
->cleanup
& 1) && (IS_UNWISE(cur
))))
264 g_free(uri
->fragment
);
265 if (uri
->cleanup
& 2)
266 uri
->fragment
= g_strndup(*str
, cur
- *str
);
268 uri
->fragment
= uri_string_unescape(*str
, cur
- *str
, NULL
);
275 * rfc3986_parse_query:
276 * @uri: pointer to an URI structure
277 * @str: pointer to the string to analyze
279 * Parse the query part of an URI
283 * Returns 0 or the error code
286 rfc3986_parse_query(URI
*uri
, const char **str
)
295 while ((ISA_PCHAR(cur
)) || (*cur
== '/') || (*cur
== '?') ||
296 ((uri
!= NULL
) && (uri
->cleanup
& 1) && (IS_UNWISE(cur
))))
300 uri
->query
= g_strndup (*str
, cur
- *str
);
307 * rfc3986_parse_port:
308 * @uri: pointer to an URI structure
309 * @str: the string to analyze
311 * Parse a port part and fills in the appropriate fields
312 * of the @uri structure
316 * Returns 0 or the error code
319 rfc3986_parse_port(URI
*uri
, const char **str
)
321 const char *cur
= *str
;
324 if (ISA_DIGIT(cur
)) {
325 while (ISA_DIGIT(cur
)) {
326 port
= port
* 10 + (*cur
- '0');
342 * rfc3986_parse_user_info:
343 * @uri: pointer to an URI structure
344 * @str: the string to analyze
346 * Parse an user informations part and fills in the appropriate fields
347 * of the @uri structure
349 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
351 * Returns 0 or the error code
354 rfc3986_parse_user_info(URI
*uri
, const char **str
)
359 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) ||
360 ISA_SUB_DELIM(cur
) || (*cur
== ':'))
365 if (uri
->cleanup
& 2)
366 uri
->user
= g_strndup(*str
, cur
- *str
);
368 uri
->user
= uri_string_unescape(*str
, cur
- *str
, NULL
);
377 * rfc3986_parse_dec_octet:
378 * @str: the string to analyze
380 * dec-octet = DIGIT ; 0-9
381 * / %x31-39 DIGIT ; 10-99
382 * / "1" 2DIGIT ; 100-199
383 * / "2" %x30-34 DIGIT ; 200-249
384 * / "25" %x30-35 ; 250-255
388 * Returns 0 if found and skipped, 1 otherwise
391 rfc3986_parse_dec_octet(const char **str
) {
392 const char *cur
= *str
;
394 if (!(ISA_DIGIT(cur
)))
396 if (!ISA_DIGIT(cur
+1))
398 else if ((*cur
!= '0') && (ISA_DIGIT(cur
+ 1)) && (!ISA_DIGIT(cur
+2)))
400 else if ((*cur
== '1') && (ISA_DIGIT(cur
+ 1)) && (ISA_DIGIT(cur
+ 2)))
402 else if ((*cur
== '2') && (*(cur
+ 1) >= '0') &&
403 (*(cur
+ 1) <= '4') && (ISA_DIGIT(cur
+ 2)))
405 else if ((*cur
== '2') && (*(cur
+ 1) == '5') &&
406 (*(cur
+ 2) >= '0') && (*(cur
+ 1) <= '5'))
414 * rfc3986_parse_host:
415 * @uri: pointer to an URI structure
416 * @str: the string to analyze
418 * Parse an host part and fills in the appropriate fields
419 * of the @uri structure
421 * host = IP-literal / IPv4address / reg-name
422 * IP-literal = "[" ( IPv6address / IPvFuture ) "]"
423 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
424 * reg-name = *( unreserved / pct-encoded / sub-delims )
426 * Returns 0 or the error code
429 rfc3986_parse_host(URI
*uri
, const char **str
)
431 const char *cur
= *str
;
436 * IPv6 and future addressing scheme are enclosed between brackets
440 while ((*cur
!= ']') && (*cur
!= 0))
448 * try to parse an IPv4
450 if (ISA_DIGIT(cur
)) {
451 if (rfc3986_parse_dec_octet(&cur
) != 0)
456 if (rfc3986_parse_dec_octet(&cur
) != 0)
460 if (rfc3986_parse_dec_octet(&cur
) != 0)
464 if (rfc3986_parse_dec_octet(&cur
) != 0)
471 * then this should be a hostname which can be empty
473 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) || ISA_SUB_DELIM(cur
))
477 g_free(uri
->authority
);
478 uri
->authority
= NULL
;
481 if (uri
->cleanup
& 2)
482 uri
->server
= g_strndup(host
, cur
- host
);
484 uri
->server
= uri_string_unescape(host
, cur
- host
, NULL
);
493 * rfc3986_parse_authority:
494 * @uri: pointer to an URI structure
495 * @str: the string to analyze
497 * Parse an authority part and fills in the appropriate fields
498 * of the @uri structure
500 * authority = [ userinfo "@" ] host [ ":" port ]
502 * Returns 0 or the error code
505 rfc3986_parse_authority(URI
*uri
, const char **str
)
512 * try to parse an userinfo and check for the trailing @
514 ret
= rfc3986_parse_user_info(uri
, &cur
);
515 if ((ret
!= 0) || (*cur
!= '@'))
519 ret
= rfc3986_parse_host(uri
, &cur
);
520 if (ret
!= 0) return(ret
);
523 ret
= rfc3986_parse_port(uri
, &cur
);
524 if (ret
!= 0) return(ret
);
531 * rfc3986_parse_segment:
532 * @str: the string to analyze
533 * @forbid: an optional forbidden character
534 * @empty: allow an empty segment
536 * Parse a segment and fills in the appropriate fields
537 * of the @uri structure
540 * segment-nz = 1*pchar
541 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
542 * ; non-zero-length segment without any colon ":"
544 * Returns 0 or the error code
547 rfc3986_parse_segment(const char **str
, char forbid
, int empty
)
552 if (!ISA_PCHAR(cur
)) {
557 while (ISA_PCHAR(cur
) && (*cur
!= forbid
))
564 * rfc3986_parse_path_ab_empty:
565 * @uri: pointer to an URI structure
566 * @str: the string to analyze
568 * Parse an path absolute or empty and fills in the appropriate fields
569 * of the @uri structure
571 * path-abempty = *( "/" segment )
573 * Returns 0 or the error code
576 rfc3986_parse_path_ab_empty(URI
*uri
, const char **str
)
583 while (*cur
== '/') {
585 ret
= rfc3986_parse_segment(&cur
, 0, 1);
586 if (ret
!= 0) return(ret
);
591 if (uri
->cleanup
& 2)
592 uri
->path
= g_strndup(*str
, cur
- *str
);
594 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
604 * rfc3986_parse_path_absolute:
605 * @uri: pointer to an URI structure
606 * @str: the string to analyze
608 * Parse an path absolute and fills in the appropriate fields
609 * of the @uri structure
611 * path-absolute = "/" [ segment-nz *( "/" segment ) ]
613 * Returns 0 or the error code
616 rfc3986_parse_path_absolute(URI
*uri
, const char **str
)
626 ret
= rfc3986_parse_segment(&cur
, 0, 0);
628 while (*cur
== '/') {
630 ret
= rfc3986_parse_segment(&cur
, 0, 1);
631 if (ret
!= 0) return(ret
);
637 if (uri
->cleanup
& 2)
638 uri
->path
= g_strndup(*str
, cur
- *str
);
640 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
650 * rfc3986_parse_path_rootless:
651 * @uri: pointer to an URI structure
652 * @str: the string to analyze
654 * Parse an path without root and fills in the appropriate fields
655 * of the @uri structure
657 * path-rootless = segment-nz *( "/" segment )
659 * Returns 0 or the error code
662 rfc3986_parse_path_rootless(URI
*uri
, const char **str
)
669 ret
= rfc3986_parse_segment(&cur
, 0, 0);
670 if (ret
!= 0) return(ret
);
671 while (*cur
== '/') {
673 ret
= rfc3986_parse_segment(&cur
, 0, 1);
674 if (ret
!= 0) return(ret
);
679 if (uri
->cleanup
& 2)
680 uri
->path
= g_strndup(*str
, cur
- *str
);
682 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
692 * rfc3986_parse_path_no_scheme:
693 * @uri: pointer to an URI structure
694 * @str: the string to analyze
696 * Parse an path which is not a scheme and fills in the appropriate fields
697 * of the @uri structure
699 * path-noscheme = segment-nz-nc *( "/" segment )
701 * Returns 0 or the error code
704 rfc3986_parse_path_no_scheme(URI
*uri
, const char **str
)
711 ret
= rfc3986_parse_segment(&cur
, ':', 0);
712 if (ret
!= 0) return(ret
);
713 while (*cur
== '/') {
715 ret
= rfc3986_parse_segment(&cur
, 0, 1);
716 if (ret
!= 0) return(ret
);
721 if (uri
->cleanup
& 2)
722 uri
->path
= g_strndup(*str
, cur
- *str
);
724 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
734 * rfc3986_parse_hier_part:
735 * @uri: pointer to an URI structure
736 * @str: the string to analyze
738 * Parse an hierarchical part and fills in the appropriate fields
739 * of the @uri structure
741 * hier-part = "//" authority path-abempty
746 * Returns 0 or the error code
749 rfc3986_parse_hier_part(URI
*uri
, const char **str
)
756 if ((*cur
== '/') && (*(cur
+ 1) == '/')) {
758 ret
= rfc3986_parse_authority(uri
, &cur
);
759 if (ret
!= 0) return(ret
);
760 ret
= rfc3986_parse_path_ab_empty(uri
, &cur
);
761 if (ret
!= 0) return(ret
);
764 } else if (*cur
== '/') {
765 ret
= rfc3986_parse_path_absolute(uri
, &cur
);
766 if (ret
!= 0) return(ret
);
767 } else if (ISA_PCHAR(cur
)) {
768 ret
= rfc3986_parse_path_rootless(uri
, &cur
);
769 if (ret
!= 0) return(ret
);
771 /* path-empty is effectively empty */
782 * rfc3986_parse_relative_ref:
783 * @uri: pointer to an URI structure
784 * @str: the string to analyze
786 * Parse an URI string and fills in the appropriate fields
787 * of the @uri structure
789 * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
790 * relative-part = "//" authority path-abempty
795 * Returns 0 or the error code
798 rfc3986_parse_relative_ref(URI
*uri
, const char *str
) {
801 if ((*str
== '/') && (*(str
+ 1) == '/')) {
803 ret
= rfc3986_parse_authority(uri
, &str
);
804 if (ret
!= 0) return(ret
);
805 ret
= rfc3986_parse_path_ab_empty(uri
, &str
);
806 if (ret
!= 0) return(ret
);
807 } else if (*str
== '/') {
808 ret
= rfc3986_parse_path_absolute(uri
, &str
);
809 if (ret
!= 0) return(ret
);
810 } else if (ISA_PCHAR(str
)) {
811 ret
= rfc3986_parse_path_no_scheme(uri
, &str
);
812 if (ret
!= 0) return(ret
);
814 /* path-empty is effectively empty */
823 ret
= rfc3986_parse_query(uri
, &str
);
824 if (ret
!= 0) return(ret
);
828 ret
= rfc3986_parse_fragment(uri
, &str
);
829 if (ret
!= 0) return(ret
);
841 * @uri: pointer to an URI structure
842 * @str: the string to analyze
844 * Parse an URI string and fills in the appropriate fields
845 * of the @uri structure
847 * scheme ":" hier-part [ "?" query ] [ "#" fragment ]
849 * Returns 0 or the error code
852 rfc3986_parse(URI
*uri
, const char *str
) {
855 ret
= rfc3986_parse_scheme(uri
, &str
);
856 if (ret
!= 0) return(ret
);
861 ret
= rfc3986_parse_hier_part(uri
, &str
);
862 if (ret
!= 0) return(ret
);
865 ret
= rfc3986_parse_query(uri
, &str
);
866 if (ret
!= 0) return(ret
);
870 ret
= rfc3986_parse_fragment(uri
, &str
);
871 if (ret
!= 0) return(ret
);
881 * rfc3986_parse_uri_reference:
882 * @uri: pointer to an URI structure
883 * @str: the string to analyze
885 * Parse an URI reference string and fills in the appropriate fields
886 * of the @uri structure
888 * URI-reference = URI / relative-ref
890 * Returns 0 or the error code
893 rfc3986_parse_uri_reference(URI
*uri
, const char *str
) {
901 * Try first to parse absolute refs, then fallback to relative if
904 ret
= rfc3986_parse(uri
, str
);
907 ret
= rfc3986_parse_relative_ref(uri
, str
);
918 * @str: the URI string to analyze
920 * Parse an URI based on RFC 3986
922 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
924 * Returns a newly built URI or NULL in case of error
927 uri_parse(const char *str
) {
934 ret
= rfc3986_parse_uri_reference(uri
, str
);
944 * @uri: pointer to an URI structure
945 * @str: the string to analyze
947 * Parse an URI reference string based on RFC 3986 and fills in the
948 * appropriate fields of the @uri structure
950 * URI-reference = URI / relative-ref
952 * Returns 0 or the error code
955 uri_parse_into(URI
*uri
, const char *str
) {
956 return(rfc3986_parse_uri_reference(uri
, str
));
961 * @str: the URI string to analyze
962 * @raw: if 1 unescaping of URI pieces are disabled
964 * Parse an URI but allows to keep intact the original fragments.
966 * URI-reference = URI / relative-ref
968 * Returns a newly built URI or NULL in case of error
971 uri_parse_raw(const char *str
, int raw
) {
981 ret
= uri_parse_into(uri
, str
);
989 /************************************************************************
991 * Generic URI structure functions *
993 ************************************************************************/
998 * Simply creates an empty URI
1000 * Returns the new structure or NULL in case of error
1006 ret
= g_new0(URI
, 1);
1013 * Function to handle properly a reallocation when saving an URI
1014 * Also imposes some limit on the length of an URI string output
1017 realloc2n(char *ret
, int *max
) {
1022 temp
= g_realloc(ret
, (tmp
+ 1));
1029 * @uri: pointer to an URI
1031 * Save the URI as an escaped string
1033 * Returns a new string (to be deallocated by caller)
1036 uri_to_string(URI
*uri
) {
1043 if (uri
== NULL
) return(NULL
);
1047 ret
= g_malloc(max
+ 1);
1050 if (uri
->scheme
!= NULL
) {
1054 temp
= realloc2n(ret
, &max
);
1060 temp
= realloc2n(ret
, &max
);
1065 if (uri
->opaque
!= NULL
) {
1068 if (len
+ 3 >= max
) {
1069 temp
= realloc2n(ret
, &max
);
1072 if (IS_RESERVED(*(p
)) || IS_UNRESERVED(*(p
)))
1075 int val
= *(unsigned char *)p
++;
1076 int hi
= val
/ 0x10, lo
= val
% 0x10;
1078 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1079 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1083 if (uri
->server
!= NULL
) {
1084 if (len
+ 3 >= max
) {
1085 temp
= realloc2n(ret
, &max
);
1090 if (uri
->user
!= NULL
) {
1093 if (len
+ 3 >= max
) {
1094 temp
= realloc2n(ret
, &max
);
1097 if ((IS_UNRESERVED(*(p
))) ||
1098 ((*(p
) == ';')) || ((*(p
) == ':')) ||
1099 ((*(p
) == '&')) || ((*(p
) == '=')) ||
1100 ((*(p
) == '+')) || ((*(p
) == '$')) ||
1104 int val
= *(unsigned char *)p
++;
1105 int hi
= val
/ 0x10, lo
= val
% 0x10;
1107 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1108 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1111 if (len
+ 3 >= max
) {
1112 temp
= realloc2n(ret
, &max
);
1120 temp
= realloc2n(ret
, &max
);
1125 if (uri
->port
> 0) {
1126 if (len
+ 10 >= max
) {
1127 temp
= realloc2n(ret
, &max
);
1130 len
+= snprintf(&ret
[len
], max
- len
, ":%d", uri
->port
);
1132 } else if (uri
->authority
!= NULL
) {
1133 if (len
+ 3 >= max
) {
1134 temp
= realloc2n(ret
, &max
);
1141 if (len
+ 3 >= max
) {
1142 temp
= realloc2n(ret
, &max
);
1145 if ((IS_UNRESERVED(*(p
))) ||
1146 ((*(p
) == '$')) || ((*(p
) == ',')) || ((*(p
) == ';')) ||
1147 ((*(p
) == ':')) || ((*(p
) == '@')) || ((*(p
) == '&')) ||
1148 ((*(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 } else if (uri
->scheme
!= NULL
) {
1159 if (len
+ 3 >= max
) {
1160 temp
= realloc2n(ret
, &max
);
1166 if (uri
->path
!= NULL
) {
1169 * the colon in file:///d: should not be escaped or
1170 * Windows accesses fail later.
1172 if ((uri
->scheme
!= NULL
) &&
1174 (((p
[1] >= 'a') && (p
[1] <= 'z')) ||
1175 ((p
[1] >= 'A') && (p
[1] <= 'Z'))) &&
1177 (!strcmp(uri
->scheme
, "file"))) {
1178 if (len
+ 3 >= max
) {
1179 temp
= realloc2n(ret
, &max
);
1187 if (len
+ 3 >= max
) {
1188 temp
= realloc2n(ret
, &max
);
1191 if ((IS_UNRESERVED(*(p
))) || ((*(p
) == '/')) ||
1192 ((*(p
) == ';')) || ((*(p
) == '@')) || ((*(p
) == '&')) ||
1193 ((*(p
) == '=')) || ((*(p
) == '+')) || ((*(p
) == '$')) ||
1197 int val
= *(unsigned char *)p
++;
1198 int hi
= val
/ 0x10, lo
= val
% 0x10;
1200 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1201 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1205 if (uri
->query
!= NULL
) {
1206 if (len
+ 1 >= max
) {
1207 temp
= realloc2n(ret
, &max
);
1213 if (len
+ 1 >= max
) {
1214 temp
= realloc2n(ret
, &max
);
1221 if (uri
->fragment
!= NULL
) {
1222 if (len
+ 3 >= max
) {
1223 temp
= realloc2n(ret
, &max
);
1229 if (len
+ 3 >= max
) {
1230 temp
= realloc2n(ret
, &max
);
1233 if ((IS_UNRESERVED(*(p
))) || (IS_RESERVED(*(p
))))
1236 int val
= *(unsigned char *)p
++;
1237 int hi
= val
/ 0x10, lo
= val
% 0x10;
1239 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1240 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1245 temp
= realloc2n(ret
, &max
);
1254 * @uri: pointer to an URI
1256 * Make sure the URI struct is free of content
1259 uri_clean(URI
*uri
) {
1260 if (uri
== NULL
) return;
1262 g_free(uri
->scheme
);
1264 g_free(uri
->server
);
1270 g_free(uri
->fragment
);
1271 uri
->fragment
= NULL
;
1272 g_free(uri
->opaque
);
1274 g_free(uri
->authority
);
1275 uri
->authority
= NULL
;
1282 * @uri: pointer to an URI
1284 * Free up the URI struct
1287 uri_free(URI
*uri
) {
1292 /************************************************************************
1294 * Helper functions *
1296 ************************************************************************/
1299 * normalize_uri_path:
1300 * @path: pointer to the path string
1302 * Applies the 5 normalization steps to a path string--that is, RFC 2396
1303 * Section 5.2, steps 6.c through 6.g.
1305 * Normalization occurs directly on the string, no new allocation is done
1307 * Returns 0 or an error code
1310 normalize_uri_path(char *path
) {
1316 /* Skip all initial "/" chars. We want to get to the beginning of the
1317 * first non-empty segment.
1320 while (cur
[0] == '/')
1325 /* Keep everything we've seen so far. */
1329 * Analyze each segment in sequence for cases (c) and (d).
1331 while (cur
[0] != '\0') {
1333 * c) All occurrences of "./", where "." is a complete path segment,
1334 * are removed from the buffer string.
1336 if ((cur
[0] == '.') && (cur
[1] == '/')) {
1338 /* '//' normalization should be done at this point too */
1339 while (cur
[0] == '/')
1345 * d) If the buffer string ends with "." as a complete path segment,
1346 * that "." is removed.
1348 if ((cur
[0] == '.') && (cur
[1] == '\0'))
1351 /* Otherwise keep the segment. */
1352 while (cur
[0] != '/') {
1355 (out
++)[0] = (cur
++)[0];
1358 while ((cur
[0] == '/') && (cur
[1] == '/'))
1361 (out
++)[0] = (cur
++)[0];
1366 /* Reset to the beginning of the first segment for the next sequence. */
1368 while (cur
[0] == '/')
1374 * Analyze each segment in sequence for cases (e) and (f).
1376 * e) All occurrences of "<segment>/../", where <segment> is a
1377 * complete path segment not equal to "..", are removed from the
1378 * buffer string. Removal of these path segments is performed
1379 * iteratively, removing the leftmost matching pattern on each
1380 * iteration, until no matching pattern remains.
1382 * f) If the buffer string ends with "<segment>/..", where <segment>
1383 * is a complete path segment not equal to "..", that
1384 * "<segment>/.." is removed.
1386 * To satisfy the "iterative" clause in (e), we need to collapse the
1387 * string every time we find something that needs to be removed. Thus,
1388 * we don't need to keep two pointers into the string: we only need a
1389 * "current position" pointer.
1394 /* At the beginning of each iteration of this loop, "cur" points to
1395 * the first character of the segment we want to examine.
1398 /* Find the end of the current segment. */
1400 while ((segp
[0] != '/') && (segp
[0] != '\0'))
1403 /* If this is the last segment, we're done (we need at least two
1404 * segments to meet the criteria for the (e) and (f) cases).
1406 if (segp
[0] == '\0')
1409 /* If the first segment is "..", or if the next segment _isn't_ "..",
1410 * keep this segment and try the next one.
1413 if (((cur
[0] == '.') && (cur
[1] == '.') && (segp
== cur
+3))
1414 || ((segp
[0] != '.') || (segp
[1] != '.')
1415 || ((segp
[2] != '/') && (segp
[2] != '\0')))) {
1420 /* If we get here, remove this segment and the next one and back up
1421 * to the previous segment (if there is one), to implement the
1422 * "iteratively" clause. It's pretty much impossible to back up
1423 * while maintaining two pointers into the buffer, so just compact
1424 * the whole buffer now.
1427 /* If this is the end of the buffer, we're done. */
1428 if (segp
[2] == '\0') {
1432 /* Valgrind complained, strcpy(cur, segp + 3); */
1433 /* string will overlap, do not use strcpy */
1436 while ((*tmp
++ = *segp
++) != 0)
1439 /* If there are no previous segments, then keep going from here. */
1441 while ((segp
> path
) && ((--segp
)[0] == '/'))
1446 /* "segp" is pointing to the end of a previous segment; find it's
1447 * start. We need to back up to the previous segment and start
1448 * over with that to handle things like "foo/bar/../..". If we
1449 * don't do this, then on the first pass we'll remove the "bar/..",
1450 * but be pointing at the second ".." so we won't realize we can also
1451 * remove the "foo/..".
1454 while ((cur
> path
) && (cur
[-1] != '/'))
1460 * g) If the resulting buffer string still begins with one or more
1461 * complete path segments of "..", then the reference is
1462 * considered to be in error. Implementations may handle this
1463 * error by retaining these components in the resolved path (i.e.,
1464 * treating them as part of the final URI), by removing them from
1465 * the resolved path (i.e., discarding relative levels above the
1466 * root), or by avoiding traversal of the reference.
1468 * We discard them from the final path.
1470 if (path
[0] == '/') {
1472 while ((cur
[0] == '/') && (cur
[1] == '.') && (cur
[2] == '.')
1473 && ((cur
[3] == '/') || (cur
[3] == '\0')))
1478 while (cur
[0] != '\0')
1479 (out
++)[0] = (cur
++)[0];
1487 static int is_hex(char c
) {
1488 if (((c
>= '0') && (c
<= '9')) ||
1489 ((c
>= 'a') && (c
<= 'f')) ||
1490 ((c
>= 'A') && (c
<= 'F')))
1497 * uri_string_unescape:
1498 * @str: the string to unescape
1499 * @len: the length in bytes to unescape (or <= 0 to indicate full string)
1500 * @target: optional destination buffer
1502 * Unescaping routine, but does not check that the string is an URI. The
1503 * output is a direct unsigned char translation of %XX values (no encoding)
1504 * Note that the length of the result can only be smaller or same size as
1507 * Returns a copy of the string, but unescaped, will return NULL only in case
1511 uri_string_unescape(const char *str
, int len
, char *target
) {
1517 if (len
<= 0) len
= strlen(str
);
1518 if (len
< 0) return(NULL
);
1520 if (target
== NULL
) {
1521 ret
= g_malloc(len
+ 1);
1527 if ((len
> 2) && (*in
== '%') && (is_hex(in
[1])) && (is_hex(in
[2]))) {
1529 if ((*in
>= '0') && (*in
<= '9'))
1531 else if ((*in
>= 'a') && (*in
<= 'f'))
1532 *out
= (*in
- 'a') + 10;
1533 else if ((*in
>= 'A') && (*in
<= 'F'))
1534 *out
= (*in
- 'A') + 10;
1536 if ((*in
>= '0') && (*in
<= '9'))
1537 *out
= *out
* 16 + (*in
- '0');
1538 else if ((*in
>= 'a') && (*in
<= 'f'))
1539 *out
= *out
* 16 + (*in
- 'a') + 10;
1540 else if ((*in
>= 'A') && (*in
<= 'F'))
1541 *out
= *out
* 16 + (*in
- 'A') + 10;
1555 * uri_string_escape:
1556 * @str: string to escape
1557 * @list: exception list string of chars not to escape
1559 * This routine escapes a string to hex, ignoring reserved characters (a-z)
1560 * and the characters in the exception list.
1562 * Returns a new escaped string or NULL in case of error.
1565 uri_string_escape(const char *str
, const char *list
) {
1574 return(g_strdup(str
));
1576 if (!(len
> 0)) return(NULL
);
1579 ret
= g_malloc(len
);
1583 if (len
- out
<= 3) {
1584 temp
= realloc2n(ret
, &len
);
1590 if ((ch
!= '@') && (!IS_UNRESERVED(ch
)) && (!strchr(list
, ch
))) {
1595 ret
[out
++] = '0' + val
;
1597 ret
[out
++] = 'A' + val
- 0xA;
1600 ret
[out
++] = '0' + val
;
1602 ret
[out
++] = 'A' + val
- 0xA;
1613 /************************************************************************
1615 * Public functions *
1617 ************************************************************************/
1621 * @URI: the URI instance found in the document
1622 * @base: the base value
1624 * Computes he final URI of the reference done by checking that
1625 * the given URI is valid, and building the final URI using the
1626 * base URI. This is processed according to section 5.2 of the
1629 * 5.2. Resolving Relative References to Absolute Form
1631 * Returns a new URI string (to be freed by the caller) or NULL in case
1635 uri_resolve(const char *uri
, const char *base
) {
1637 int ret
, len
, indx
, cur
, out
;
1643 * 1) The URI reference is parsed into the potential four components and
1644 * fragment identifier, as described in Section 4.3.
1646 * NOTE that a completely empty URI is treated by modern browsers
1647 * as a reference to "." rather than as a synonym for the current
1648 * URI. Should we do that here?
1655 ret
= uri_parse_into(ref
, uri
);
1662 if ((ref
!= NULL
) && (ref
->scheme
!= NULL
)) {
1664 * The URI is absolute don't modify.
1666 val
= g_strdup(uri
);
1673 ret
= uri_parse_into(bas
, base
);
1677 val
= uri_to_string(ref
);
1682 * the base fragment must be ignored
1684 g_free(bas
->fragment
);
1685 bas
->fragment
= NULL
;
1686 val
= uri_to_string(bas
);
1691 * 2) If the path component is empty and the scheme, authority, and
1692 * query components are undefined, then it is a reference to the
1693 * current document and we are done. Otherwise, the reference URI's
1694 * query and fragment components are defined as found (or not found)
1695 * within the URI reference and not inherited from the base URI.
1697 * NOTE that in modern browsers, the parsing differs from the above
1698 * in the following aspect: the query component is allowed to be
1699 * defined while still treating this as a reference to the current
1703 if ((ref
->scheme
== NULL
) && (ref
->path
== NULL
) &&
1704 ((ref
->authority
== NULL
) && (ref
->server
== NULL
))) {
1705 res
->scheme
= g_strdup(bas
->scheme
);
1706 if (bas
->authority
!= NULL
)
1707 res
->authority
= g_strdup(bas
->authority
);
1708 else if (bas
->server
!= NULL
) {
1709 res
->server
= g_strdup(bas
->server
);
1710 res
->user
= g_strdup(bas
->user
);
1711 res
->port
= bas
->port
;
1713 res
->path
= g_strdup(bas
->path
);
1714 if (ref
->query
!= NULL
) {
1715 res
->query
= g_strdup (ref
->query
);
1717 res
->query
= g_strdup(bas
->query
);
1719 res
->fragment
= g_strdup(ref
->fragment
);
1724 * 3) If the scheme component is defined, indicating that the reference
1725 * starts with a scheme name, then the reference is interpreted as an
1726 * absolute URI and we are done. Otherwise, the reference URI's
1727 * scheme is inherited from the base URI's scheme component.
1729 if (ref
->scheme
!= NULL
) {
1730 val
= uri_to_string(ref
);
1733 res
->scheme
= g_strdup(bas
->scheme
);
1735 res
->query
= g_strdup(ref
->query
);
1736 res
->fragment
= g_strdup(ref
->fragment
);
1739 * 4) If the authority component is defined, then the reference is a
1740 * network-path and we skip to step 7. Otherwise, the reference
1741 * URI's authority is inherited from the base URI's authority
1742 * component, which will also be undefined if the URI scheme does not
1743 * use an authority component.
1745 if ((ref
->authority
!= NULL
) || (ref
->server
!= NULL
)) {
1746 if (ref
->authority
!= NULL
)
1747 res
->authority
= g_strdup(ref
->authority
);
1749 res
->server
= g_strdup(ref
->server
);
1750 res
->user
= g_strdup(ref
->user
);
1751 res
->port
= ref
->port
;
1753 res
->path
= g_strdup(ref
->path
);
1756 if (bas
->authority
!= NULL
)
1757 res
->authority
= g_strdup(bas
->authority
);
1758 else if (bas
->server
!= NULL
) {
1759 res
->server
= g_strdup(bas
->server
);
1760 res
->user
= g_strdup(bas
->user
);
1761 res
->port
= bas
->port
;
1765 * 5) If the path component begins with a slash character ("/"), then
1766 * the reference is an absolute-path and we skip to step 7.
1768 if ((ref
->path
!= NULL
) && (ref
->path
[0] == '/')) {
1769 res
->path
= g_strdup(ref
->path
);
1775 * 6) If this step is reached, then we are resolving a relative-path
1776 * reference. The relative path needs to be merged with the base
1777 * URI's path. Although there are many ways to do this, we will
1778 * describe a simple method using a separate string buffer.
1780 * Allocate a buffer large enough for the result string.
1782 len
= 2; /* extra / and 0 */
1783 if (ref
->path
!= NULL
)
1784 len
+= strlen(ref
->path
);
1785 if (bas
->path
!= NULL
)
1786 len
+= strlen(bas
->path
);
1787 res
->path
= g_malloc(len
);
1791 * a) All but the last segment of the base URI's path component is
1792 * copied to the buffer. In other words, any characters after the
1793 * last (right-most) slash character, if any, are excluded.
1797 if (bas
->path
!= NULL
) {
1798 while (bas
->path
[cur
] != 0) {
1799 while ((bas
->path
[cur
] != 0) && (bas
->path
[cur
] != '/'))
1801 if (bas
->path
[cur
] == 0)
1806 res
->path
[out
] = bas
->path
[out
];
1814 * b) The reference's path component is appended to the buffer
1817 if (ref
->path
!= NULL
&& ref
->path
[0] != 0) {
1820 * Ensure the path includes a '/'
1822 if ((out
== 0) && (bas
->server
!= NULL
))
1823 res
->path
[out
++] = '/';
1824 while (ref
->path
[indx
] != 0) {
1825 res
->path
[out
++] = ref
->path
[indx
++];
1831 * Steps c) to h) are really path normalization steps
1833 normalize_uri_path(res
->path
);
1838 * 7) The resulting URI components, including any inherited from the
1839 * base URI, are recombined to give the absolute form of the URI
1842 val
= uri_to_string(res
);
1855 * uri_resolve_relative:
1856 * @URI: the URI reference under consideration
1857 * @base: the base value
1859 * Expresses the URI of the reference in terms relative to the
1860 * base. Some examples of this operation include:
1861 * base = "http://site1.com/docs/book1.html"
1862 * URI input URI returned
1863 * docs/pic1.gif pic1.gif
1864 * docs/img/pic1.gif img/pic1.gif
1865 * img/pic1.gif ../img/pic1.gif
1866 * http://site1.com/docs/pic1.gif pic1.gif
1867 * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif
1869 * base = "docs/book1.html"
1870 * URI input URI returned
1871 * docs/pic1.gif pic1.gif
1872 * docs/img/pic1.gif img/pic1.gif
1873 * img/pic1.gif ../img/pic1.gif
1874 * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif
1877 * Note: if the URI reference is really weird or complicated, it may be
1878 * worthwhile to first convert it into a "nice" one by calling
1879 * uri_resolve (using 'base') before calling this routine,
1880 * since this routine (for reasonable efficiency) assumes URI has
1881 * already been through some validation.
1883 * Returns a new URI string (to be freed by the caller) or NULL in case
1887 uri_resolve_relative (const char *uri
, const char * base
)
1897 char *bptr
, *uptr
, *vptr
;
1898 int remove_path
= 0;
1900 if ((uri
== NULL
) || (*uri
== 0))
1904 * First parse URI into a standard form
1907 /* If URI not already in "relative" form */
1908 if (uri
[0] != '.') {
1909 ret
= uri_parse_into (ref
, uri
);
1911 goto done
; /* Error in URI, return NULL */
1913 ref
->path
= g_strdup(uri
);
1916 * Next parse base into the same standard form
1918 if ((base
== NULL
) || (*base
== 0)) {
1919 val
= g_strdup (uri
);
1923 if (base
[0] != '.') {
1924 ret
= uri_parse_into (bas
, base
);
1926 goto done
; /* Error in base, return NULL */
1928 bas
->path
= g_strdup(base
);
1931 * If the scheme / server on the URI differs from the base,
1932 * just return the URI
1934 if ((ref
->scheme
!= NULL
) &&
1935 ((bas
->scheme
== NULL
) ||
1936 (strcmp (bas
->scheme
, ref
->scheme
)) ||
1937 (strcmp (bas
->server
, ref
->server
)))) {
1938 val
= g_strdup (uri
);
1941 if (bas
->path
== ref
->path
||
1942 (bas
->path
&& ref
->path
&& !strcmp(bas
->path
, ref
->path
))) {
1946 if (bas
->path
== NULL
) {
1947 val
= g_strdup(ref
->path
);
1950 if (ref
->path
== NULL
) {
1951 ref
->path
= (char *) "/";
1956 * At this point (at last!) we can compare the two paths
1958 * First we take care of the special case where either of the
1959 * two path components may be missing (bug 316224)
1961 if (bas
->path
== NULL
) {
1962 if (ref
->path
!= NULL
) {
1966 /* exception characters from uri_to_string */
1967 val
= uri_string_escape(uptr
, "/;&=+$,");
1972 if (ref
->path
== NULL
) {
1973 for (ix
= 0; bptr
[ix
] != 0; ix
++) {
1974 if (bptr
[ix
] == '/')
1978 len
= 1; /* this is for a string terminator only */
1981 * Next we compare the two strings and find where they first differ
1983 if ((ref
->path
[pos
] == '.') && (ref
->path
[pos
+1] == '/'))
1985 if ((*bptr
== '.') && (bptr
[1] == '/'))
1987 else if ((*bptr
== '/') && (ref
->path
[pos
] != '/'))
1989 while ((bptr
[pos
] == ref
->path
[pos
]) && (bptr
[pos
] != 0))
1992 if (bptr
[pos
] == ref
->path
[pos
]) {
1994 goto done
; /* (I can't imagine why anyone would do this) */
1998 * In URI, "back up" to the last '/' encountered. This will be the
1999 * beginning of the "unique" suffix of URI
2002 if ((ref
->path
[ix
] == '/') && (ix
> 0))
2004 else if ((ref
->path
[ix
] == 0) && (ix
> 1) && (ref
->path
[ix
- 1] == '/'))
2006 for (; ix
> 0; ix
--) {
2007 if (ref
->path
[ix
] == '/')
2014 uptr
= &ref
->path
[ix
];
2018 * In base, count the number of '/' from the differing point
2020 if (bptr
[pos
] != ref
->path
[pos
]) {/* check for trivial URI == base */
2021 for (; bptr
[ix
] != 0; ix
++) {
2022 if (bptr
[ix
] == '/')
2026 len
= strlen (uptr
) + 1;
2031 /* exception characters from uri_to_string */
2032 val
= uri_string_escape(uptr
, "/;&=+$,");
2037 * Allocate just enough space for the returned string -
2038 * length of the remainder of the URI, plus enough space
2039 * for the "../" groups, plus one for the terminator
2041 val
= g_malloc (len
+ 3 * nbslash
);
2044 * Put in as many "../" as needed
2046 for (; nbslash
>0; nbslash
--) {
2052 * Finish up with the end of the URI
2055 if ((vptr
> val
) && (len
> 0) &&
2056 (uptr
[0] == '/') && (vptr
[-1] == '/')) {
2057 memcpy (vptr
, uptr
+ 1, len
- 1);
2060 memcpy (vptr
, uptr
, len
);
2067 /* escape the freshly-built path */
2069 /* exception characters from uri_to_string */
2070 val
= uri_string_escape(vptr
, "/;&=+$,");
2075 * Free the working variables
2077 if (remove_path
!= 0)
2088 * Utility functions to help parse and assemble query strings.
2091 struct QueryParams
*
2092 query_params_new (int init_alloc
)
2094 struct QueryParams
*ps
;
2096 if (init_alloc
<= 0) init_alloc
= 1;
2098 ps
= g_new(QueryParams
, 1);
2100 ps
->alloc
= init_alloc
;
2101 ps
->p
= g_new(QueryParam
, ps
->alloc
);
2106 /* Ensure there is space to store at least one more parameter
2107 * at the end of the set.
2110 query_params_append (struct QueryParams
*ps
,
2111 const char *name
, const char *value
)
2113 if (ps
->n
>= ps
->alloc
) {
2114 ps
->p
= g_renew(QueryParam
, ps
->p
, ps
->alloc
* 2);
2118 ps
->p
[ps
->n
].name
= g_strdup(name
);
2119 ps
->p
[ps
->n
].value
= g_strdup(value
);
2120 ps
->p
[ps
->n
].ignore
= 0;
2127 query_params_free (struct QueryParams
*ps
)
2131 for (i
= 0; i
< ps
->n
; ++i
) {
2132 g_free (ps
->p
[i
].name
);
2133 g_free (ps
->p
[i
].value
);
2139 struct QueryParams
*
2140 query_params_parse (const char *query
)
2142 struct QueryParams
*ps
;
2143 const char *end
, *eq
;
2145 ps
= query_params_new (0);
2146 if (!query
|| query
[0] == '\0') return ps
;
2149 char *name
= NULL
, *value
= NULL
;
2151 /* Find the next separator, or end of the string. */
2152 end
= strchr (query
, '&');
2154 end
= strchr (query
, ';');
2156 end
= query
+ strlen (query
);
2158 /* Find the first '=' character between here and end. */
2159 eq
= strchr (query
, '=');
2160 if (eq
&& eq
>= end
) eq
= NULL
;
2162 /* Empty section (eg. "&&"). */
2166 /* If there is no '=' character, then we have just "name"
2167 * and consistent with CGI.pm we assume value is "".
2170 name
= uri_string_unescape (query
, end
- query
, NULL
);
2173 /* Or if we have "name=" here (works around annoying
2174 * problem when calling uri_string_unescape with len = 0).
2176 else if (eq
+1 == end
) {
2177 name
= uri_string_unescape (query
, eq
- query
, NULL
);
2178 value
= g_new0(char, 1);
2180 /* If the '=' character is at the beginning then we have
2181 * "=value" and consistent with CGI.pm we _ignore_ this.
2183 else if (query
== eq
)
2186 /* Otherwise it's "name=value". */
2188 name
= uri_string_unescape (query
, eq
- query
, NULL
);
2189 value
= uri_string_unescape (eq
+1, end
- (eq
+1), NULL
);
2192 /* Append to the parameter set. */
2193 query_params_append (ps
, name
, value
);
2199 if (*query
) query
++; /* skip '&' separator */