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>
60 static void uri_clean(URI
*uri
);
63 * Old rule from 2396 used in legacy handling code
64 * alpha = lowalpha | upalpha
66 #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
70 * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
71 * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
72 * "u" | "v" | "w" | "x" | "y" | "z"
75 #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z'))
78 * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" |
79 * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" |
80 * "U" | "V" | "W" | "X" | "Y" | "Z"
82 #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z'))
88 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
90 #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
93 * alphanum = alpha | digit
96 #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
99 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
102 #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
103 ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
104 ((x) == '(') || ((x) == ')'))
107 * unwise = "{" | "}" | "|" | "\" | "^" | "`"
110 #define IS_UNWISE(p) \
111 (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \
112 ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \
113 ((*(p) == ']')) || ((*(p) == '`')))
115 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
119 #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
120 ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
121 ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \
125 * unreserved = alphanum | mark
128 #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
131 * Skip to next pointer char, handle escaped sequences
134 #define NEXT(p) ((*p == '%')? p += 3 : p++)
137 * Productions from the spec.
139 * authority = server | reg_name
140 * reg_name = 1*( unreserved | escaped | "$" | "," |
141 * ";" | ":" | "@" | "&" | "=" | "+" )
143 * path = [ abs_path | opaque_part ]
147 /************************************************************************
151 ************************************************************************/
153 #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9'))
154 #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \
155 ((*(p) >= 'A') && (*(p) <= 'Z')))
156 #define ISA_HEXDIG(p) \
157 (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \
158 ((*(p) >= 'A') && (*(p) <= 'F')))
161 * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
162 * / "*" / "+" / "," / ";" / "="
164 #define ISA_SUB_DELIM(p) \
165 (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \
166 ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \
167 ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \
168 ((*(p) == '=')) || ((*(p) == '\'')))
171 * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
173 #define ISA_GEN_DELIM(p) \
174 (((*(p) == ':')) || ((*(p) == '/')) || ((*(p) == '?')) || \
175 ((*(p) == '#')) || ((*(p) == '[')) || ((*(p) == ']')) || \
179 * reserved = gen-delims / sub-delims
181 #define ISA_RESERVED(p) (ISA_GEN_DELIM(p) || (ISA_SUB_DELIM(p)))
184 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
186 #define ISA_UNRESERVED(p) \
187 ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \
188 ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
191 * pct-encoded = "%" HEXDIG HEXDIG
193 #define ISA_PCT_ENCODED(p) \
194 ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2)))
197 * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
199 #define ISA_PCHAR(p) \
200 (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \
201 ((*(p) == ':')) || ((*(p) == '@')))
204 * rfc3986_parse_scheme:
205 * @uri: pointer to an URI structure
206 * @str: pointer to the string to analyze
208 * Parse an URI scheme
210 * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
212 * Returns 0 or the error code
215 rfc3986_parse_scheme(URI
*uri
, const char **str
) {
225 while (ISA_ALPHA(cur
) || ISA_DIGIT(cur
) ||
226 (*cur
== '+') || (*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
251 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
))))
265 g_free(uri
->fragment
);
266 if (uri
->cleanup
& 2)
267 uri
->fragment
= g_strndup(*str
, cur
- *str
);
269 uri
->fragment
= uri_string_unescape(*str
, cur
- *str
, NULL
);
276 * rfc3986_parse_query:
277 * @uri: pointer to an URI structure
278 * @str: pointer to the string to analyze
280 * Parse the query part of an URI
284 * Returns 0 or the error code
287 rfc3986_parse_query(URI
*uri
, const char **str
)
296 while ((ISA_PCHAR(cur
)) || (*cur
== '/') || (*cur
== '?') ||
297 ((uri
!= NULL
) && (uri
->cleanup
& 1) && (IS_UNWISE(cur
))))
301 uri
->query
= g_strndup (*str
, cur
- *str
);
308 * rfc3986_parse_port:
309 * @uri: pointer to an URI structure
310 * @str: the string to analyze
312 * Parse a port part and fills in the appropriate fields
313 * of the @uri structure
317 * Returns 0 or the error code
320 rfc3986_parse_port(URI
*uri
, const char **str
)
322 const char *cur
= *str
;
325 if (ISA_DIGIT(cur
)) {
326 while (ISA_DIGIT(cur
)) {
327 port
= port
* 10 + (*cur
- '0');
343 * rfc3986_parse_user_info:
344 * @uri: pointer to an URI structure
345 * @str: the string to analyze
347 * Parse an user informations part and fills in the appropriate fields
348 * of the @uri structure
350 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
352 * Returns 0 or the error code
355 rfc3986_parse_user_info(URI
*uri
, const char **str
)
360 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) ||
361 ISA_SUB_DELIM(cur
) || (*cur
== ':'))
366 if (uri
->cleanup
& 2)
367 uri
->user
= g_strndup(*str
, cur
- *str
);
369 uri
->user
= uri_string_unescape(*str
, cur
- *str
, NULL
);
378 * rfc3986_parse_dec_octet:
379 * @str: the string to analyze
381 * dec-octet = DIGIT ; 0-9
382 * / %x31-39 DIGIT ; 10-99
383 * / "1" 2DIGIT ; 100-199
384 * / "2" %x30-34 DIGIT ; 200-249
385 * / "25" %x30-35 ; 250-255
389 * Returns 0 if found and skipped, 1 otherwise
392 rfc3986_parse_dec_octet(const char **str
) {
393 const char *cur
= *str
;
395 if (!(ISA_DIGIT(cur
)))
397 if (!ISA_DIGIT(cur
+1))
399 else if ((*cur
!= '0') && (ISA_DIGIT(cur
+ 1)) && (!ISA_DIGIT(cur
+2)))
401 else if ((*cur
== '1') && (ISA_DIGIT(cur
+ 1)) && (ISA_DIGIT(cur
+ 2)))
403 else if ((*cur
== '2') && (*(cur
+ 1) >= '0') &&
404 (*(cur
+ 1) <= '4') && (ISA_DIGIT(cur
+ 2)))
406 else if ((*cur
== '2') && (*(cur
+ 1) == '5') &&
407 (*(cur
+ 2) >= '0') && (*(cur
+ 1) <= '5'))
415 * rfc3986_parse_host:
416 * @uri: pointer to an URI structure
417 * @str: the string to analyze
419 * Parse an host part and fills in the appropriate fields
420 * of the @uri structure
422 * host = IP-literal / IPv4address / reg-name
423 * IP-literal = "[" ( IPv6address / IPvFuture ) "]"
424 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
425 * reg-name = *( unreserved / pct-encoded / sub-delims )
427 * Returns 0 or the error code
430 rfc3986_parse_host(URI
*uri
, const char **str
)
432 const char *cur
= *str
;
437 * IPv6 and future addressing scheme are enclosed between brackets
441 while ((*cur
!= ']') && (*cur
!= 0))
449 * try to parse an IPv4
451 if (ISA_DIGIT(cur
)) {
452 if (rfc3986_parse_dec_octet(&cur
) != 0)
457 if (rfc3986_parse_dec_octet(&cur
) != 0)
461 if (rfc3986_parse_dec_octet(&cur
) != 0)
465 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
))
478 g_free(uri
->authority
);
479 uri
->authority
= NULL
;
482 if (uri
->cleanup
& 2)
483 uri
->server
= g_strndup(host
, cur
- host
);
485 uri
->server
= uri_string_unescape(host
, cur
- host
, NULL
);
494 * rfc3986_parse_authority:
495 * @uri: pointer to an URI structure
496 * @str: the string to analyze
498 * Parse an authority part and fills in the appropriate fields
499 * of the @uri structure
501 * authority = [ userinfo "@" ] host [ ":" port ]
503 * Returns 0 or the error code
506 rfc3986_parse_authority(URI
*uri
, const char **str
)
513 * try to parse an userinfo and check for the trailing @
515 ret
= rfc3986_parse_user_info(uri
, &cur
);
516 if ((ret
!= 0) || (*cur
!= '@'))
520 ret
= rfc3986_parse_host(uri
, &cur
);
521 if (ret
!= 0) return(ret
);
524 ret
= rfc3986_parse_port(uri
, &cur
);
525 if (ret
!= 0) return(ret
);
532 * rfc3986_parse_segment:
533 * @str: the string to analyze
534 * @forbid: an optional forbidden character
535 * @empty: allow an empty segment
537 * Parse a segment and fills in the appropriate fields
538 * of the @uri structure
541 * segment-nz = 1*pchar
542 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
543 * ; non-zero-length segment without any colon ":"
545 * Returns 0 or the error code
548 rfc3986_parse_segment(const char **str
, char forbid
, int empty
)
553 if (!ISA_PCHAR(cur
)) {
558 while (ISA_PCHAR(cur
) && (*cur
!= forbid
))
565 * rfc3986_parse_path_ab_empty:
566 * @uri: pointer to an URI structure
567 * @str: the string to analyze
569 * Parse an path absolute or empty and fills in the appropriate fields
570 * of the @uri structure
572 * path-abempty = *( "/" segment )
574 * Returns 0 or the error code
577 rfc3986_parse_path_ab_empty(URI
*uri
, const char **str
)
584 while (*cur
== '/') {
586 ret
= rfc3986_parse_segment(&cur
, 0, 1);
587 if (ret
!= 0) return(ret
);
592 if (uri
->cleanup
& 2)
593 uri
->path
= g_strndup(*str
, cur
- *str
);
595 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
605 * rfc3986_parse_path_absolute:
606 * @uri: pointer to an URI structure
607 * @str: the string to analyze
609 * Parse an path absolute and fills in the appropriate fields
610 * of the @uri structure
612 * path-absolute = "/" [ segment-nz *( "/" segment ) ]
614 * Returns 0 or the error code
617 rfc3986_parse_path_absolute(URI
*uri
, const char **str
)
627 ret
= rfc3986_parse_segment(&cur
, 0, 0);
629 while (*cur
== '/') {
631 ret
= rfc3986_parse_segment(&cur
, 0, 1);
632 if (ret
!= 0) return(ret
);
638 if (uri
->cleanup
& 2)
639 uri
->path
= g_strndup(*str
, cur
- *str
);
641 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
651 * rfc3986_parse_path_rootless:
652 * @uri: pointer to an URI structure
653 * @str: the string to analyze
655 * Parse an path without root and fills in the appropriate fields
656 * of the @uri structure
658 * path-rootless = segment-nz *( "/" segment )
660 * Returns 0 or the error code
663 rfc3986_parse_path_rootless(URI
*uri
, const char **str
)
670 ret
= rfc3986_parse_segment(&cur
, 0, 0);
671 if (ret
!= 0) return(ret
);
672 while (*cur
== '/') {
674 ret
= rfc3986_parse_segment(&cur
, 0, 1);
675 if (ret
!= 0) return(ret
);
680 if (uri
->cleanup
& 2)
681 uri
->path
= g_strndup(*str
, cur
- *str
);
683 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
693 * rfc3986_parse_path_no_scheme:
694 * @uri: pointer to an URI structure
695 * @str: the string to analyze
697 * Parse an path which is not a scheme and fills in the appropriate fields
698 * of the @uri structure
700 * path-noscheme = segment-nz-nc *( "/" segment )
702 * Returns 0 or the error code
705 rfc3986_parse_path_no_scheme(URI
*uri
, const char **str
)
712 ret
= rfc3986_parse_segment(&cur
, ':', 0);
713 if (ret
!= 0) return(ret
);
714 while (*cur
== '/') {
716 ret
= rfc3986_parse_segment(&cur
, 0, 1);
717 if (ret
!= 0) return(ret
);
722 if (uri
->cleanup
& 2)
723 uri
->path
= g_strndup(*str
, cur
- *str
);
725 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
735 * rfc3986_parse_hier_part:
736 * @uri: pointer to an URI structure
737 * @str: the string to analyze
739 * Parse an hierarchical part and fills in the appropriate fields
740 * of the @uri structure
742 * hier-part = "//" authority path-abempty
747 * Returns 0 or the error code
750 rfc3986_parse_hier_part(URI
*uri
, const char **str
)
757 if ((*cur
== '/') && (*(cur
+ 1) == '/')) {
759 ret
= rfc3986_parse_authority(uri
, &cur
);
760 if (ret
!= 0) return(ret
);
761 ret
= rfc3986_parse_path_ab_empty(uri
, &cur
);
762 if (ret
!= 0) return(ret
);
765 } else if (*cur
== '/') {
766 ret
= rfc3986_parse_path_absolute(uri
, &cur
);
767 if (ret
!= 0) return(ret
);
768 } else if (ISA_PCHAR(cur
)) {
769 ret
= rfc3986_parse_path_rootless(uri
, &cur
);
770 if (ret
!= 0) return(ret
);
772 /* path-empty is effectively empty */
783 * rfc3986_parse_relative_ref:
784 * @uri: pointer to an URI structure
785 * @str: the string to analyze
787 * Parse an URI string and fills in the appropriate fields
788 * of the @uri structure
790 * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
791 * relative-part = "//" authority path-abempty
796 * Returns 0 or the error code
799 rfc3986_parse_relative_ref(URI
*uri
, const char *str
) {
802 if ((*str
== '/') && (*(str
+ 1) == '/')) {
804 ret
= rfc3986_parse_authority(uri
, &str
);
805 if (ret
!= 0) return(ret
);
806 ret
= rfc3986_parse_path_ab_empty(uri
, &str
);
807 if (ret
!= 0) return(ret
);
808 } else if (*str
== '/') {
809 ret
= rfc3986_parse_path_absolute(uri
, &str
);
810 if (ret
!= 0) return(ret
);
811 } else if (ISA_PCHAR(str
)) {
812 ret
= rfc3986_parse_path_no_scheme(uri
, &str
);
813 if (ret
!= 0) return(ret
);
815 /* path-empty is effectively empty */
824 ret
= rfc3986_parse_query(uri
, &str
);
825 if (ret
!= 0) return(ret
);
829 ret
= rfc3986_parse_fragment(uri
, &str
);
830 if (ret
!= 0) return(ret
);
842 * @uri: pointer to an URI structure
843 * @str: the string to analyze
845 * Parse an URI string and fills in the appropriate fields
846 * of the @uri structure
848 * scheme ":" hier-part [ "?" query ] [ "#" fragment ]
850 * Returns 0 or the error code
853 rfc3986_parse(URI
*uri
, const char *str
) {
856 ret
= rfc3986_parse_scheme(uri
, &str
);
857 if (ret
!= 0) return(ret
);
862 ret
= rfc3986_parse_hier_part(uri
, &str
);
863 if (ret
!= 0) return(ret
);
866 ret
= rfc3986_parse_query(uri
, &str
);
867 if (ret
!= 0) return(ret
);
871 ret
= rfc3986_parse_fragment(uri
, &str
);
872 if (ret
!= 0) return(ret
);
882 * rfc3986_parse_uri_reference:
883 * @uri: pointer to an URI structure
884 * @str: the string to analyze
886 * Parse an URI reference string and fills in the appropriate fields
887 * of the @uri structure
889 * URI-reference = URI / relative-ref
891 * Returns 0 or the error code
894 rfc3986_parse_uri_reference(URI
*uri
, const char *str
) {
902 * Try first to parse absolute refs, then fallback to relative if
905 ret
= rfc3986_parse(uri
, str
);
908 ret
= rfc3986_parse_relative_ref(uri
, str
);
919 * @str: the URI string to analyze
921 * Parse an URI based on RFC 3986
923 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
925 * Returns a newly built URI or NULL in case of error
928 uri_parse(const char *str
) {
935 ret
= rfc3986_parse_uri_reference(uri
, str
);
945 * @uri: pointer to an URI structure
946 * @str: the string to analyze
948 * Parse an URI reference string based on RFC 3986 and fills in the
949 * appropriate fields of the @uri structure
951 * URI-reference = URI / relative-ref
953 * Returns 0 or the error code
956 uri_parse_into(URI
*uri
, const char *str
) {
957 return(rfc3986_parse_uri_reference(uri
, str
));
962 * @str: the URI string to analyze
963 * @raw: if 1 unescaping of URI pieces are disabled
965 * Parse an URI but allows to keep intact the original fragments.
967 * URI-reference = URI / relative-ref
969 * Returns a newly built URI or NULL in case of error
972 uri_parse_raw(const char *str
, int raw
) {
982 ret
= uri_parse_into(uri
, str
);
990 /************************************************************************
992 * Generic URI structure functions *
994 ************************************************************************/
999 * Simply creates an empty URI
1001 * Returns the new structure or NULL in case of error
1007 ret
= g_new0(URI
, 1);
1014 * Function to handle properly a reallocation when saving an URI
1015 * Also imposes some limit on the length of an URI string output
1018 realloc2n(char *ret
, int *max
) {
1023 temp
= g_realloc(ret
, (tmp
+ 1));
1030 * @uri: pointer to an URI
1032 * Save the URI as an escaped string
1034 * Returns a new string (to be deallocated by caller)
1037 uri_to_string(URI
*uri
) {
1044 if (uri
== NULL
) return(NULL
);
1048 ret
= g_malloc(max
+ 1);
1051 if (uri
->scheme
!= NULL
) {
1055 temp
= realloc2n(ret
, &max
);
1061 temp
= realloc2n(ret
, &max
);
1066 if (uri
->opaque
!= NULL
) {
1069 if (len
+ 3 >= max
) {
1070 temp
= realloc2n(ret
, &max
);
1073 if (IS_RESERVED(*(p
)) || IS_UNRESERVED(*(p
)))
1076 int val
= *(unsigned char *)p
++;
1077 int hi
= val
/ 0x10, lo
= val
% 0x10;
1079 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1080 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1084 if (uri
->server
!= NULL
) {
1085 if (len
+ 3 >= max
) {
1086 temp
= realloc2n(ret
, &max
);
1091 if (uri
->user
!= NULL
) {
1094 if (len
+ 3 >= max
) {
1095 temp
= realloc2n(ret
, &max
);
1098 if ((IS_UNRESERVED(*(p
))) ||
1099 ((*(p
) == ';')) || ((*(p
) == ':')) ||
1100 ((*(p
) == '&')) || ((*(p
) == '=')) ||
1101 ((*(p
) == '+')) || ((*(p
) == '$')) ||
1105 int val
= *(unsigned char *)p
++;
1106 int hi
= val
/ 0x10, lo
= val
% 0x10;
1108 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1109 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1112 if (len
+ 3 >= max
) {
1113 temp
= realloc2n(ret
, &max
);
1121 temp
= realloc2n(ret
, &max
);
1126 if (uri
->port
> 0) {
1127 if (len
+ 10 >= max
) {
1128 temp
= realloc2n(ret
, &max
);
1131 len
+= snprintf(&ret
[len
], max
- len
, ":%d", uri
->port
);
1133 } else if (uri
->authority
!= NULL
) {
1134 if (len
+ 3 >= max
) {
1135 temp
= realloc2n(ret
, &max
);
1142 if (len
+ 3 >= max
) {
1143 temp
= realloc2n(ret
, &max
);
1146 if ((IS_UNRESERVED(*(p
))) ||
1147 ((*(p
) == '$')) || ((*(p
) == ',')) || ((*(p
) == ';')) ||
1148 ((*(p
) == ':')) || ((*(p
) == '@')) || ((*(p
) == '&')) ||
1149 ((*(p
) == '=')) || ((*(p
) == '+')))
1152 int val
= *(unsigned char *)p
++;
1153 int hi
= val
/ 0x10, lo
= val
% 0x10;
1155 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1156 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1159 } else if (uri
->scheme
!= NULL
) {
1160 if (len
+ 3 >= max
) {
1161 temp
= realloc2n(ret
, &max
);
1167 if (uri
->path
!= NULL
) {
1170 * the colon in file:///d: should not be escaped or
1171 * Windows accesses fail later.
1173 if ((uri
->scheme
!= NULL
) &&
1175 (((p
[1] >= 'a') && (p
[1] <= 'z')) ||
1176 ((p
[1] >= 'A') && (p
[1] <= 'Z'))) &&
1178 (!strcmp(uri
->scheme
, "file"))) {
1179 if (len
+ 3 >= max
) {
1180 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');
1206 if (uri
->query
!= NULL
) {
1207 if (len
+ 1 >= max
) {
1208 temp
= realloc2n(ret
, &max
);
1214 if (len
+ 1 >= max
) {
1215 temp
= realloc2n(ret
, &max
);
1222 if (uri
->fragment
!= NULL
) {
1223 if (len
+ 3 >= max
) {
1224 temp
= realloc2n(ret
, &max
);
1230 if (len
+ 3 >= max
) {
1231 temp
= realloc2n(ret
, &max
);
1234 if ((IS_UNRESERVED(*(p
))) || (IS_RESERVED(*(p
))))
1237 int val
= *(unsigned char *)p
++;
1238 int hi
= val
/ 0x10, lo
= val
% 0x10;
1240 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1241 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1246 temp
= realloc2n(ret
, &max
);
1255 * @uri: pointer to an URI
1257 * Make sure the URI struct is free of content
1260 uri_clean(URI
*uri
) {
1261 if (uri
== NULL
) return;
1263 g_free(uri
->scheme
);
1265 g_free(uri
->server
);
1271 g_free(uri
->fragment
);
1272 uri
->fragment
= NULL
;
1273 g_free(uri
->opaque
);
1275 g_free(uri
->authority
);
1276 uri
->authority
= NULL
;
1283 * @uri: pointer to an URI
1285 * Free up the URI struct
1288 uri_free(URI
*uri
) {
1293 /************************************************************************
1295 * Helper functions *
1297 ************************************************************************/
1300 * normalize_uri_path:
1301 * @path: pointer to the path string
1303 * Applies the 5 normalization steps to a path string--that is, RFC 2396
1304 * Section 5.2, steps 6.c through 6.g.
1306 * Normalization occurs directly on the string, no new allocation is done
1308 * Returns 0 or an error code
1311 normalize_uri_path(char *path
) {
1317 /* Skip all initial "/" chars. We want to get to the beginning of the
1318 * first non-empty segment.
1321 while (cur
[0] == '/')
1326 /* Keep everything we've seen so far. */
1330 * Analyze each segment in sequence for cases (c) and (d).
1332 while (cur
[0] != '\0') {
1334 * c) All occurrences of "./", where "." is a complete path segment,
1335 * are removed from the buffer string.
1337 if ((cur
[0] == '.') && (cur
[1] == '/')) {
1339 /* '//' normalization should be done at this point too */
1340 while (cur
[0] == '/')
1346 * d) If the buffer string ends with "." as a complete path segment,
1347 * that "." is removed.
1349 if ((cur
[0] == '.') && (cur
[1] == '\0'))
1352 /* Otherwise keep the segment. */
1353 while (cur
[0] != '/') {
1356 (out
++)[0] = (cur
++)[0];
1359 while ((cur
[0] == '/') && (cur
[1] == '/'))
1362 (out
++)[0] = (cur
++)[0];
1367 /* Reset to the beginning of the first segment for the next sequence. */
1369 while (cur
[0] == '/')
1375 * Analyze each segment in sequence for cases (e) and (f).
1377 * e) All occurrences of "<segment>/../", where <segment> is a
1378 * complete path segment not equal to "..", are removed from the
1379 * buffer string. Removal of these path segments is performed
1380 * iteratively, removing the leftmost matching pattern on each
1381 * iteration, until no matching pattern remains.
1383 * f) If the buffer string ends with "<segment>/..", where <segment>
1384 * is a complete path segment not equal to "..", that
1385 * "<segment>/.." is removed.
1387 * To satisfy the "iterative" clause in (e), we need to collapse the
1388 * string every time we find something that needs to be removed. Thus,
1389 * we don't need to keep two pointers into the string: we only need a
1390 * "current position" pointer.
1395 /* At the beginning of each iteration of this loop, "cur" points to
1396 * the first character of the segment we want to examine.
1399 /* Find the end of the current segment. */
1401 while ((segp
[0] != '/') && (segp
[0] != '\0'))
1404 /* If this is the last segment, we're done (we need at least two
1405 * segments to meet the criteria for the (e) and (f) cases).
1407 if (segp
[0] == '\0')
1410 /* If the first segment is "..", or if the next segment _isn't_ "..",
1411 * keep this segment and try the next one.
1414 if (((cur
[0] == '.') && (cur
[1] == '.') && (segp
== cur
+3))
1415 || ((segp
[0] != '.') || (segp
[1] != '.')
1416 || ((segp
[2] != '/') && (segp
[2] != '\0')))) {
1421 /* If we get here, remove this segment and the next one and back up
1422 * to the previous segment (if there is one), to implement the
1423 * "iteratively" clause. It's pretty much impossible to back up
1424 * while maintaining two pointers into the buffer, so just compact
1425 * the whole buffer now.
1428 /* If this is the end of the buffer, we're done. */
1429 if (segp
[2] == '\0') {
1433 /* Valgrind complained, strcpy(cur, segp + 3); */
1434 /* string will overlap, do not use strcpy */
1437 while ((*tmp
++ = *segp
++) != 0)
1440 /* If there are no previous segments, then keep going from here. */
1442 while ((segp
> path
) && ((--segp
)[0] == '/'))
1447 /* "segp" is pointing to the end of a previous segment; find it's
1448 * start. We need to back up to the previous segment and start
1449 * over with that to handle things like "foo/bar/../..". If we
1450 * don't do this, then on the first pass we'll remove the "bar/..",
1451 * but be pointing at the second ".." so we won't realize we can also
1452 * remove the "foo/..".
1455 while ((cur
> path
) && (cur
[-1] != '/'))
1461 * g) If the resulting buffer string still begins with one or more
1462 * complete path segments of "..", then the reference is
1463 * considered to be in error. Implementations may handle this
1464 * error by retaining these components in the resolved path (i.e.,
1465 * treating them as part of the final URI), by removing them from
1466 * the resolved path (i.e., discarding relative levels above the
1467 * root), or by avoiding traversal of the reference.
1469 * We discard them from the final path.
1471 if (path
[0] == '/') {
1473 while ((cur
[0] == '/') && (cur
[1] == '.') && (cur
[2] == '.')
1474 && ((cur
[3] == '/') || (cur
[3] == '\0')))
1479 while (cur
[0] != '\0')
1480 (out
++)[0] = (cur
++)[0];
1488 static int is_hex(char c
) {
1489 if (((c
>= '0') && (c
<= '9')) ||
1490 ((c
>= 'a') && (c
<= 'f')) ||
1491 ((c
>= 'A') && (c
<= 'F')))
1498 * uri_string_unescape:
1499 * @str: the string to unescape
1500 * @len: the length in bytes to unescape (or <= 0 to indicate full string)
1501 * @target: optional destination buffer
1503 * Unescaping routine, but does not check that the string is an URI. The
1504 * output is a direct unsigned char translation of %XX values (no encoding)
1505 * Note that the length of the result can only be smaller or same size as
1508 * Returns a copy of the string, but unescaped, will return NULL only in case
1512 uri_string_unescape(const char *str
, int len
, char *target
) {
1518 if (len
<= 0) len
= strlen(str
);
1519 if (len
< 0) return(NULL
);
1521 if (target
== NULL
) {
1522 ret
= g_malloc(len
+ 1);
1528 if ((len
> 2) && (*in
== '%') && (is_hex(in
[1])) && (is_hex(in
[2]))) {
1530 if ((*in
>= '0') && (*in
<= '9'))
1532 else if ((*in
>= 'a') && (*in
<= 'f'))
1533 *out
= (*in
- 'a') + 10;
1534 else if ((*in
>= 'A') && (*in
<= 'F'))
1535 *out
= (*in
- 'A') + 10;
1537 if ((*in
>= '0') && (*in
<= '9'))
1538 *out
= *out
* 16 + (*in
- '0');
1539 else if ((*in
>= 'a') && (*in
<= 'f'))
1540 *out
= *out
* 16 + (*in
- 'a') + 10;
1541 else if ((*in
>= 'A') && (*in
<= 'F'))
1542 *out
= *out
* 16 + (*in
- 'A') + 10;
1556 * uri_string_escape:
1557 * @str: string to escape
1558 * @list: exception list string of chars not to escape
1560 * This routine escapes a string to hex, ignoring reserved characters (a-z)
1561 * and the characters in the exception list.
1563 * Returns a new escaped string or NULL in case of error.
1566 uri_string_escape(const char *str
, const char *list
) {
1575 return(g_strdup(str
));
1577 if (!(len
> 0)) return(NULL
);
1580 ret
= g_malloc(len
);
1584 if (len
- out
<= 3) {
1585 temp
= realloc2n(ret
, &len
);
1591 if ((ch
!= '@') && (!IS_UNRESERVED(ch
)) && (!strchr(list
, ch
))) {
1596 ret
[out
++] = '0' + val
;
1598 ret
[out
++] = 'A' + val
- 0xA;
1601 ret
[out
++] = '0' + val
;
1603 ret
[out
++] = 'A' + val
- 0xA;
1614 /************************************************************************
1616 * Public functions *
1618 ************************************************************************/
1622 * @URI: the URI instance found in the document
1623 * @base: the base value
1625 * Computes he final URI of the reference done by checking that
1626 * the given URI is valid, and building the final URI using the
1627 * base URI. This is processed according to section 5.2 of the
1630 * 5.2. Resolving Relative References to Absolute Form
1632 * Returns a new URI string (to be freed by the caller) or NULL in case
1636 uri_resolve(const char *uri
, const char *base
) {
1638 int ret
, len
, indx
, cur
, out
;
1644 * 1) The URI reference is parsed into the potential four components and
1645 * fragment identifier, as described in Section 4.3.
1647 * NOTE that a completely empty URI is treated by modern browsers
1648 * as a reference to "." rather than as a synonym for the current
1649 * URI. Should we do that here?
1656 ret
= uri_parse_into(ref
, uri
);
1663 if ((ref
!= NULL
) && (ref
->scheme
!= NULL
)) {
1665 * The URI is absolute don't modify.
1667 val
= g_strdup(uri
);
1674 ret
= uri_parse_into(bas
, base
);
1678 val
= uri_to_string(ref
);
1683 * the base fragment must be ignored
1685 g_free(bas
->fragment
);
1686 bas
->fragment
= NULL
;
1687 val
= uri_to_string(bas
);
1692 * 2) If the path component is empty and the scheme, authority, and
1693 * query components are undefined, then it is a reference to the
1694 * current document and we are done. Otherwise, the reference URI's
1695 * query and fragment components are defined as found (or not found)
1696 * within the URI reference and not inherited from the base URI.
1698 * NOTE that in modern browsers, the parsing differs from the above
1699 * in the following aspect: the query component is allowed to be
1700 * defined while still treating this as a reference to the current
1704 if ((ref
->scheme
== NULL
) && (ref
->path
== NULL
) &&
1705 ((ref
->authority
== NULL
) && (ref
->server
== NULL
))) {
1706 res
->scheme
= g_strdup(bas
->scheme
);
1707 if (bas
->authority
!= NULL
)
1708 res
->authority
= g_strdup(bas
->authority
);
1709 else if (bas
->server
!= NULL
) {
1710 res
->server
= g_strdup(bas
->server
);
1711 res
->user
= g_strdup(bas
->user
);
1712 res
->port
= bas
->port
;
1714 res
->path
= g_strdup(bas
->path
);
1715 if (ref
->query
!= NULL
) {
1716 res
->query
= g_strdup (ref
->query
);
1718 res
->query
= g_strdup(bas
->query
);
1720 res
->fragment
= g_strdup(ref
->fragment
);
1725 * 3) If the scheme component is defined, indicating that the reference
1726 * starts with a scheme name, then the reference is interpreted as an
1727 * absolute URI and we are done. Otherwise, the reference URI's
1728 * scheme is inherited from the base URI's scheme component.
1730 if (ref
->scheme
!= NULL
) {
1731 val
= uri_to_string(ref
);
1734 res
->scheme
= g_strdup(bas
->scheme
);
1736 res
->query
= g_strdup(ref
->query
);
1737 res
->fragment
= g_strdup(ref
->fragment
);
1740 * 4) If the authority component is defined, then the reference is a
1741 * network-path and we skip to step 7. Otherwise, the reference
1742 * URI's authority is inherited from the base URI's authority
1743 * component, which will also be undefined if the URI scheme does not
1744 * use an authority component.
1746 if ((ref
->authority
!= NULL
) || (ref
->server
!= NULL
)) {
1747 if (ref
->authority
!= NULL
)
1748 res
->authority
= g_strdup(ref
->authority
);
1750 res
->server
= g_strdup(ref
->server
);
1751 res
->user
= g_strdup(ref
->user
);
1752 res
->port
= ref
->port
;
1754 res
->path
= g_strdup(ref
->path
);
1757 if (bas
->authority
!= NULL
)
1758 res
->authority
= g_strdup(bas
->authority
);
1759 else if (bas
->server
!= NULL
) {
1760 res
->server
= g_strdup(bas
->server
);
1761 res
->user
= g_strdup(bas
->user
);
1762 res
->port
= bas
->port
;
1766 * 5) If the path component begins with a slash character ("/"), then
1767 * the reference is an absolute-path and we skip to step 7.
1769 if ((ref
->path
!= NULL
) && (ref
->path
[0] == '/')) {
1770 res
->path
= g_strdup(ref
->path
);
1776 * 6) If this step is reached, then we are resolving a relative-path
1777 * reference. The relative path needs to be merged with the base
1778 * URI's path. Although there are many ways to do this, we will
1779 * describe a simple method using a separate string buffer.
1781 * Allocate a buffer large enough for the result string.
1783 len
= 2; /* extra / and 0 */
1784 if (ref
->path
!= NULL
)
1785 len
+= strlen(ref
->path
);
1786 if (bas
->path
!= NULL
)
1787 len
+= strlen(bas
->path
);
1788 res
->path
= g_malloc(len
);
1792 * a) All but the last segment of the base URI's path component is
1793 * copied to the buffer. In other words, any characters after the
1794 * last (right-most) slash character, if any, are excluded.
1798 if (bas
->path
!= NULL
) {
1799 while (bas
->path
[cur
] != 0) {
1800 while ((bas
->path
[cur
] != 0) && (bas
->path
[cur
] != '/'))
1802 if (bas
->path
[cur
] == 0)
1807 res
->path
[out
] = bas
->path
[out
];
1815 * b) The reference's path component is appended to the buffer
1818 if (ref
->path
!= NULL
&& ref
->path
[0] != 0) {
1821 * Ensure the path includes a '/'
1823 if ((out
== 0) && (bas
->server
!= NULL
))
1824 res
->path
[out
++] = '/';
1825 while (ref
->path
[indx
] != 0) {
1826 res
->path
[out
++] = ref
->path
[indx
++];
1832 * Steps c) to h) are really path normalization steps
1834 normalize_uri_path(res
->path
);
1839 * 7) The resulting URI components, including any inherited from the
1840 * base URI, are recombined to give the absolute form of the URI
1843 val
= uri_to_string(res
);
1856 * uri_resolve_relative:
1857 * @URI: the URI reference under consideration
1858 * @base: the base value
1860 * Expresses the URI of the reference in terms relative to the
1861 * base. Some examples of this operation include:
1862 * base = "http://site1.com/docs/book1.html"
1863 * URI input URI returned
1864 * docs/pic1.gif pic1.gif
1865 * docs/img/pic1.gif img/pic1.gif
1866 * img/pic1.gif ../img/pic1.gif
1867 * http://site1.com/docs/pic1.gif pic1.gif
1868 * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif
1870 * base = "docs/book1.html"
1871 * URI input URI returned
1872 * docs/pic1.gif pic1.gif
1873 * docs/img/pic1.gif img/pic1.gif
1874 * img/pic1.gif ../img/pic1.gif
1875 * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif
1878 * Note: if the URI reference is really weird or complicated, it may be
1879 * worthwhile to first convert it into a "nice" one by calling
1880 * uri_resolve (using 'base') before calling this routine,
1881 * since this routine (for reasonable efficiency) assumes URI has
1882 * already been through some validation.
1884 * Returns a new URI string (to be freed by the caller) or NULL in case
1888 uri_resolve_relative (const char *uri
, const char * base
)
1898 char *bptr
, *uptr
, *vptr
;
1899 int remove_path
= 0;
1901 if ((uri
== NULL
) || (*uri
== 0))
1905 * First parse URI into a standard form
1908 /* If URI not already in "relative" form */
1909 if (uri
[0] != '.') {
1910 ret
= uri_parse_into (ref
, uri
);
1912 goto done
; /* Error in URI, return NULL */
1914 ref
->path
= g_strdup(uri
);
1917 * Next parse base into the same standard form
1919 if ((base
== NULL
) || (*base
== 0)) {
1920 val
= g_strdup (uri
);
1924 if (base
[0] != '.') {
1925 ret
= uri_parse_into (bas
, base
);
1927 goto done
; /* Error in base, return NULL */
1929 bas
->path
= g_strdup(base
);
1932 * If the scheme / server on the URI differs from the base,
1933 * just return the URI
1935 if ((ref
->scheme
!= NULL
) &&
1936 ((bas
->scheme
== NULL
) ||
1937 (strcmp (bas
->scheme
, ref
->scheme
)) ||
1938 (strcmp (bas
->server
, ref
->server
)))) {
1939 val
= g_strdup (uri
);
1942 if (bas
->path
== ref
->path
||
1943 (bas
->path
&& ref
->path
&& !strcmp(bas
->path
, ref
->path
))) {
1947 if (bas
->path
== NULL
) {
1948 val
= g_strdup(ref
->path
);
1951 if (ref
->path
== NULL
) {
1952 ref
->path
= (char *) "/";
1957 * At this point (at last!) we can compare the two paths
1959 * First we take care of the special case where either of the
1960 * two path components may be missing (bug 316224)
1962 if (bas
->path
== NULL
) {
1963 if (ref
->path
!= NULL
) {
1967 /* exception characters from uri_to_string */
1968 val
= uri_string_escape(uptr
, "/;&=+$,");
1973 if (ref
->path
== NULL
) {
1974 for (ix
= 0; bptr
[ix
] != 0; ix
++) {
1975 if (bptr
[ix
] == '/')
1979 len
= 1; /* this is for a string terminator only */
1982 * Next we compare the two strings and find where they first differ
1984 if ((ref
->path
[pos
] == '.') && (ref
->path
[pos
+1] == '/'))
1986 if ((*bptr
== '.') && (bptr
[1] == '/'))
1988 else if ((*bptr
== '/') && (ref
->path
[pos
] != '/'))
1990 while ((bptr
[pos
] == ref
->path
[pos
]) && (bptr
[pos
] != 0))
1993 if (bptr
[pos
] == ref
->path
[pos
]) {
1995 goto done
; /* (I can't imagine why anyone would do this) */
1999 * In URI, "back up" to the last '/' encountered. This will be the
2000 * beginning of the "unique" suffix of URI
2003 if ((ref
->path
[ix
] == '/') && (ix
> 0))
2005 else if ((ref
->path
[ix
] == 0) && (ix
> 1) && (ref
->path
[ix
- 1] == '/'))
2007 for (; ix
> 0; ix
--) {
2008 if (ref
->path
[ix
] == '/')
2015 uptr
= &ref
->path
[ix
];
2019 * In base, count the number of '/' from the differing point
2021 if (bptr
[pos
] != ref
->path
[pos
]) {/* check for trivial URI == base */
2022 for (; bptr
[ix
] != 0; ix
++) {
2023 if (bptr
[ix
] == '/')
2027 len
= strlen (uptr
) + 1;
2032 /* exception characters from uri_to_string */
2033 val
= uri_string_escape(uptr
, "/;&=+$,");
2038 * Allocate just enough space for the returned string -
2039 * length of the remainder of the URI, plus enough space
2040 * for the "../" groups, plus one for the terminator
2042 val
= g_malloc (len
+ 3 * nbslash
);
2045 * Put in as many "../" as needed
2047 for (; nbslash
>0; nbslash
--) {
2053 * Finish up with the end of the URI
2056 if ((vptr
> val
) && (len
> 0) &&
2057 (uptr
[0] == '/') && (vptr
[-1] == '/')) {
2058 memcpy (vptr
, uptr
+ 1, len
- 1);
2061 memcpy (vptr
, uptr
, len
);
2068 /* escape the freshly-built path */
2070 /* exception characters from uri_to_string */
2071 val
= uri_string_escape(vptr
, "/;&=+$,");
2076 * Free the working variables
2078 if (remove_path
!= 0)
2089 * Utility functions to help parse and assemble query strings.
2092 struct QueryParams
*
2093 query_params_new (int init_alloc
)
2095 struct QueryParams
*ps
;
2097 if (init_alloc
<= 0) init_alloc
= 1;
2099 ps
= g_new(QueryParams
, 1);
2101 ps
->alloc
= init_alloc
;
2102 ps
->p
= g_new(QueryParam
, ps
->alloc
);
2107 /* Ensure there is space to store at least one more parameter
2108 * at the end of the set.
2111 query_params_append (struct QueryParams
*ps
,
2112 const char *name
, const char *value
)
2114 if (ps
->n
>= ps
->alloc
) {
2115 ps
->p
= g_renew(QueryParam
, ps
->p
, ps
->alloc
* 2);
2119 ps
->p
[ps
->n
].name
= g_strdup(name
);
2120 ps
->p
[ps
->n
].value
= g_strdup(value
);
2121 ps
->p
[ps
->n
].ignore
= 0;
2128 query_params_free (struct QueryParams
*ps
)
2132 for (i
= 0; i
< ps
->n
; ++i
) {
2133 g_free (ps
->p
[i
].name
);
2134 g_free (ps
->p
[i
].value
);
2140 struct QueryParams
*
2141 query_params_parse (const char *query
)
2143 struct QueryParams
*ps
;
2144 const char *end
, *eq
;
2146 ps
= query_params_new (0);
2147 if (!query
|| query
[0] == '\0') return ps
;
2150 char *name
= NULL
, *value
= NULL
;
2152 /* Find the next separator, or end of the string. */
2153 end
= strchr (query
, '&');
2155 end
= strchr (query
, ';');
2157 end
= query
+ strlen (query
);
2159 /* Find the first '=' character between here and end. */
2160 eq
= strchr (query
, '=');
2161 if (eq
&& eq
>= end
) eq
= NULL
;
2163 /* Empty section (eg. "&&"). */
2167 /* If there is no '=' character, then we have just "name"
2168 * and consistent with CGI.pm we assume value is "".
2171 name
= uri_string_unescape (query
, end
- query
, NULL
);
2174 /* Or if we have "name=" here (works around annoying
2175 * problem when calling uri_string_unescape with len = 0).
2177 else if (eq
+1 == end
) {
2178 name
= uri_string_unescape (query
, eq
- query
, NULL
);
2179 value
= g_new0(char, 1);
2181 /* If the '=' character is at the beginning then we have
2182 * "=value" and consistent with CGI.pm we _ignore_ this.
2184 else if (query
== eq
)
2187 /* Otherwise it's "name=value". */
2189 name
= uri_string_unescape (query
, eq
- query
, NULL
);
2190 value
= uri_string_unescape (eq
+1, end
- (eq
+1), NULL
);
2193 /* Append to the parameter set. */
2194 query_params_append (ps
, name
, value
);
2200 if (*query
) query
++; /* skip '&' separator */