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
;
324 if (ISA_DIGIT(cur
)) {
327 while (ISA_DIGIT(cur
)) {
329 uri
->port
= uri
->port
* 10 + (*cur
- '0');
339 * rfc3986_parse_user_info:
340 * @uri: pointer to an URI structure
341 * @str: the string to analyze
343 * Parse an user informations part and fills in the appropriate fields
344 * of the @uri structure
346 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
348 * Returns 0 or the error code
351 rfc3986_parse_user_info(URI
*uri
, const char **str
)
356 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) ||
357 ISA_SUB_DELIM(cur
) || (*cur
== ':'))
362 if (uri
->cleanup
& 2)
363 uri
->user
= g_strndup(*str
, cur
- *str
);
365 uri
->user
= uri_string_unescape(*str
, cur
- *str
, NULL
);
374 * rfc3986_parse_dec_octet:
375 * @str: the string to analyze
377 * dec-octet = DIGIT ; 0-9
378 * / %x31-39 DIGIT ; 10-99
379 * / "1" 2DIGIT ; 100-199
380 * / "2" %x30-34 DIGIT ; 200-249
381 * / "25" %x30-35 ; 250-255
385 * Returns 0 if found and skipped, 1 otherwise
388 rfc3986_parse_dec_octet(const char **str
) {
389 const char *cur
= *str
;
391 if (!(ISA_DIGIT(cur
)))
393 if (!ISA_DIGIT(cur
+1))
395 else if ((*cur
!= '0') && (ISA_DIGIT(cur
+ 1)) && (!ISA_DIGIT(cur
+2)))
397 else if ((*cur
== '1') && (ISA_DIGIT(cur
+ 1)) && (ISA_DIGIT(cur
+ 2)))
399 else if ((*cur
== '2') && (*(cur
+ 1) >= '0') &&
400 (*(cur
+ 1) <= '4') && (ISA_DIGIT(cur
+ 2)))
402 else if ((*cur
== '2') && (*(cur
+ 1) == '5') &&
403 (*(cur
+ 2) >= '0') && (*(cur
+ 1) <= '5'))
411 * rfc3986_parse_host:
412 * @uri: pointer to an URI structure
413 * @str: the string to analyze
415 * Parse an host part and fills in the appropriate fields
416 * of the @uri structure
418 * host = IP-literal / IPv4address / reg-name
419 * IP-literal = "[" ( IPv6address / IPvFuture ) "]"
420 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
421 * reg-name = *( unreserved / pct-encoded / sub-delims )
423 * Returns 0 or the error code
426 rfc3986_parse_host(URI
*uri
, const char **str
)
428 const char *cur
= *str
;
433 * IPv6 and future addressing scheme are enclosed between brackets
437 while ((*cur
!= ']') && (*cur
!= 0))
445 * try to parse an IPv4
447 if (ISA_DIGIT(cur
)) {
448 if (rfc3986_parse_dec_octet(&cur
) != 0)
453 if (rfc3986_parse_dec_octet(&cur
) != 0)
457 if (rfc3986_parse_dec_octet(&cur
) != 0)
461 if (rfc3986_parse_dec_octet(&cur
) != 0)
468 * then this should be a hostname which can be empty
470 while (ISA_UNRESERVED(cur
) || ISA_PCT_ENCODED(cur
) || ISA_SUB_DELIM(cur
))
474 g_free(uri
->authority
);
475 uri
->authority
= NULL
;
478 if (uri
->cleanup
& 2)
479 uri
->server
= g_strndup(host
, cur
- host
);
481 uri
->server
= uri_string_unescape(host
, cur
- host
, NULL
);
490 * rfc3986_parse_authority:
491 * @uri: pointer to an URI structure
492 * @str: the string to analyze
494 * Parse an authority part and fills in the appropriate fields
495 * of the @uri structure
497 * authority = [ userinfo "@" ] host [ ":" port ]
499 * Returns 0 or the error code
502 rfc3986_parse_authority(URI
*uri
, const char **str
)
509 * try to parse an userinfo and check for the trailing @
511 ret
= rfc3986_parse_user_info(uri
, &cur
);
512 if ((ret
!= 0) || (*cur
!= '@'))
516 ret
= rfc3986_parse_host(uri
, &cur
);
517 if (ret
!= 0) return(ret
);
520 ret
= rfc3986_parse_port(uri
, &cur
);
521 if (ret
!= 0) return(ret
);
528 * rfc3986_parse_segment:
529 * @str: the string to analyze
530 * @forbid: an optional forbidden character
531 * @empty: allow an empty segment
533 * Parse a segment and fills in the appropriate fields
534 * of the @uri structure
537 * segment-nz = 1*pchar
538 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
539 * ; non-zero-length segment without any colon ":"
541 * Returns 0 or the error code
544 rfc3986_parse_segment(const char **str
, char forbid
, int empty
)
549 if (!ISA_PCHAR(cur
)) {
554 while (ISA_PCHAR(cur
) && (*cur
!= forbid
))
561 * rfc3986_parse_path_ab_empty:
562 * @uri: pointer to an URI structure
563 * @str: the string to analyze
565 * Parse an path absolute or empty and fills in the appropriate fields
566 * of the @uri structure
568 * path-abempty = *( "/" segment )
570 * Returns 0 or the error code
573 rfc3986_parse_path_ab_empty(URI
*uri
, const char **str
)
580 while (*cur
== '/') {
582 ret
= rfc3986_parse_segment(&cur
, 0, 1);
583 if (ret
!= 0) return(ret
);
588 if (uri
->cleanup
& 2)
589 uri
->path
= g_strndup(*str
, cur
- *str
);
591 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
601 * rfc3986_parse_path_absolute:
602 * @uri: pointer to an URI structure
603 * @str: the string to analyze
605 * Parse an path absolute and fills in the appropriate fields
606 * of the @uri structure
608 * path-absolute = "/" [ segment-nz *( "/" segment ) ]
610 * Returns 0 or the error code
613 rfc3986_parse_path_absolute(URI
*uri
, const char **str
)
623 ret
= rfc3986_parse_segment(&cur
, 0, 0);
625 while (*cur
== '/') {
627 ret
= rfc3986_parse_segment(&cur
, 0, 1);
628 if (ret
!= 0) return(ret
);
634 if (uri
->cleanup
& 2)
635 uri
->path
= g_strndup(*str
, cur
- *str
);
637 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
647 * rfc3986_parse_path_rootless:
648 * @uri: pointer to an URI structure
649 * @str: the string to analyze
651 * Parse an path without root and fills in the appropriate fields
652 * of the @uri structure
654 * path-rootless = segment-nz *( "/" segment )
656 * Returns 0 or the error code
659 rfc3986_parse_path_rootless(URI
*uri
, const char **str
)
666 ret
= rfc3986_parse_segment(&cur
, 0, 0);
667 if (ret
!= 0) return(ret
);
668 while (*cur
== '/') {
670 ret
= rfc3986_parse_segment(&cur
, 0, 1);
671 if (ret
!= 0) return(ret
);
676 if (uri
->cleanup
& 2)
677 uri
->path
= g_strndup(*str
, cur
- *str
);
679 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
689 * rfc3986_parse_path_no_scheme:
690 * @uri: pointer to an URI structure
691 * @str: the string to analyze
693 * Parse an path which is not a scheme and fills in the appropriate fields
694 * of the @uri structure
696 * path-noscheme = segment-nz-nc *( "/" segment )
698 * Returns 0 or the error code
701 rfc3986_parse_path_no_scheme(URI
*uri
, const char **str
)
708 ret
= rfc3986_parse_segment(&cur
, ':', 0);
709 if (ret
!= 0) return(ret
);
710 while (*cur
== '/') {
712 ret
= rfc3986_parse_segment(&cur
, 0, 1);
713 if (ret
!= 0) return(ret
);
718 if (uri
->cleanup
& 2)
719 uri
->path
= g_strndup(*str
, cur
- *str
);
721 uri
->path
= uri_string_unescape(*str
, cur
- *str
, NULL
);
731 * rfc3986_parse_hier_part:
732 * @uri: pointer to an URI structure
733 * @str: the string to analyze
735 * Parse an hierarchical part and fills in the appropriate fields
736 * of the @uri structure
738 * hier-part = "//" authority path-abempty
743 * Returns 0 or the error code
746 rfc3986_parse_hier_part(URI
*uri
, const char **str
)
753 if ((*cur
== '/') && (*(cur
+ 1) == '/')) {
755 ret
= rfc3986_parse_authority(uri
, &cur
);
756 if (ret
!= 0) return(ret
);
757 ret
= rfc3986_parse_path_ab_empty(uri
, &cur
);
758 if (ret
!= 0) return(ret
);
761 } else if (*cur
== '/') {
762 ret
= rfc3986_parse_path_absolute(uri
, &cur
);
763 if (ret
!= 0) return(ret
);
764 } else if (ISA_PCHAR(cur
)) {
765 ret
= rfc3986_parse_path_rootless(uri
, &cur
);
766 if (ret
!= 0) return(ret
);
768 /* path-empty is effectively empty */
779 * rfc3986_parse_relative_ref:
780 * @uri: pointer to an URI structure
781 * @str: the string to analyze
783 * Parse an URI string and fills in the appropriate fields
784 * of the @uri structure
786 * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
787 * relative-part = "//" authority path-abempty
792 * Returns 0 or the error code
795 rfc3986_parse_relative_ref(URI
*uri
, const char *str
) {
798 if ((*str
== '/') && (*(str
+ 1) == '/')) {
800 ret
= rfc3986_parse_authority(uri
, &str
);
801 if (ret
!= 0) return(ret
);
802 ret
= rfc3986_parse_path_ab_empty(uri
, &str
);
803 if (ret
!= 0) return(ret
);
804 } else if (*str
== '/') {
805 ret
= rfc3986_parse_path_absolute(uri
, &str
);
806 if (ret
!= 0) return(ret
);
807 } else if (ISA_PCHAR(str
)) {
808 ret
= rfc3986_parse_path_no_scheme(uri
, &str
);
809 if (ret
!= 0) return(ret
);
811 /* path-empty is effectively empty */
820 ret
= rfc3986_parse_query(uri
, &str
);
821 if (ret
!= 0) return(ret
);
825 ret
= rfc3986_parse_fragment(uri
, &str
);
826 if (ret
!= 0) return(ret
);
838 * @uri: pointer to an URI structure
839 * @str: the string to analyze
841 * Parse an URI string and fills in the appropriate fields
842 * of the @uri structure
844 * scheme ":" hier-part [ "?" query ] [ "#" fragment ]
846 * Returns 0 or the error code
849 rfc3986_parse(URI
*uri
, const char *str
) {
852 ret
= rfc3986_parse_scheme(uri
, &str
);
853 if (ret
!= 0) return(ret
);
858 ret
= rfc3986_parse_hier_part(uri
, &str
);
859 if (ret
!= 0) return(ret
);
862 ret
= rfc3986_parse_query(uri
, &str
);
863 if (ret
!= 0) return(ret
);
867 ret
= rfc3986_parse_fragment(uri
, &str
);
868 if (ret
!= 0) return(ret
);
878 * rfc3986_parse_uri_reference:
879 * @uri: pointer to an URI structure
880 * @str: the string to analyze
882 * Parse an URI reference string and fills in the appropriate fields
883 * of the @uri structure
885 * URI-reference = URI / relative-ref
887 * Returns 0 or the error code
890 rfc3986_parse_uri_reference(URI
*uri
, const char *str
) {
898 * Try first to parse absolute refs, then fallback to relative if
901 ret
= rfc3986_parse(uri
, str
);
904 ret
= rfc3986_parse_relative_ref(uri
, str
);
915 * @str: the URI string to analyze
917 * Parse an URI based on RFC 3986
919 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
921 * Returns a newly built URI or NULL in case of error
924 uri_parse(const char *str
) {
932 ret
= rfc3986_parse_uri_reference(uri
, str
);
943 * @uri: pointer to an URI structure
944 * @str: the string to analyze
946 * Parse an URI reference string based on RFC 3986 and fills in the
947 * appropriate fields of the @uri structure
949 * URI-reference = URI / relative-ref
951 * Returns 0 or the error code
954 uri_parse_into(URI
*uri
, const char *str
) {
955 return(rfc3986_parse_uri_reference(uri
, str
));
960 * @str: the URI string to analyze
961 * @raw: if 1 unescaping of URI pieces are disabled
963 * Parse an URI but allows to keep intact the original fragments.
965 * URI-reference = URI / relative-ref
967 * Returns a newly built URI or NULL in case of error
970 uri_parse_raw(const char *str
, int raw
) {
981 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
);
1056 if (temp
== NULL
) goto mem_error
;
1062 temp
= realloc2n(ret
, &max
);
1063 if (temp
== NULL
) goto mem_error
;
1068 if (uri
->opaque
!= NULL
) {
1071 if (len
+ 3 >= max
) {
1072 temp
= realloc2n(ret
, &max
);
1073 if (temp
== NULL
) goto mem_error
;
1076 if (IS_RESERVED(*(p
)) || IS_UNRESERVED(*(p
)))
1079 int val
= *(unsigned char *)p
++;
1080 int hi
= val
/ 0x10, lo
= val
% 0x10;
1082 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1083 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1087 if (uri
->server
!= NULL
) {
1088 if (len
+ 3 >= max
) {
1089 temp
= realloc2n(ret
, &max
);
1090 if (temp
== NULL
) goto mem_error
;
1095 if (uri
->user
!= NULL
) {
1098 if (len
+ 3 >= max
) {
1099 temp
= realloc2n(ret
, &max
);
1100 if (temp
== NULL
) goto mem_error
;
1103 if ((IS_UNRESERVED(*(p
))) ||
1104 ((*(p
) == ';')) || ((*(p
) == ':')) ||
1105 ((*(p
) == '&')) || ((*(p
) == '=')) ||
1106 ((*(p
) == '+')) || ((*(p
) == '$')) ||
1110 int val
= *(unsigned char *)p
++;
1111 int hi
= val
/ 0x10, lo
= val
% 0x10;
1113 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1114 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1117 if (len
+ 3 >= max
) {
1118 temp
= realloc2n(ret
, &max
);
1119 if (temp
== NULL
) goto mem_error
;
1127 temp
= realloc2n(ret
, &max
);
1128 if (temp
== NULL
) goto mem_error
;
1133 if (uri
->port
> 0) {
1134 if (len
+ 10 >= max
) {
1135 temp
= realloc2n(ret
, &max
);
1136 if (temp
== NULL
) goto mem_error
;
1139 len
+= snprintf(&ret
[len
], max
- len
, ":%d", uri
->port
);
1141 } else if (uri
->authority
!= NULL
) {
1142 if (len
+ 3 >= max
) {
1143 temp
= realloc2n(ret
, &max
);
1144 if (temp
== NULL
) goto mem_error
;
1151 if (len
+ 3 >= max
) {
1152 temp
= realloc2n(ret
, &max
);
1153 if (temp
== NULL
) goto mem_error
;
1156 if ((IS_UNRESERVED(*(p
))) ||
1157 ((*(p
) == '$')) || ((*(p
) == ',')) || ((*(p
) == ';')) ||
1158 ((*(p
) == ':')) || ((*(p
) == '@')) || ((*(p
) == '&')) ||
1159 ((*(p
) == '=')) || ((*(p
) == '+')))
1162 int val
= *(unsigned char *)p
++;
1163 int hi
= val
/ 0x10, lo
= val
% 0x10;
1165 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1166 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1169 } else if (uri
->scheme
!= NULL
) {
1170 if (len
+ 3 >= max
) {
1171 temp
= realloc2n(ret
, &max
);
1172 if (temp
== NULL
) goto mem_error
;
1178 if (uri
->path
!= NULL
) {
1181 * the colon in file:///d: should not be escaped or
1182 * Windows accesses fail later.
1184 if ((uri
->scheme
!= NULL
) &&
1186 (((p
[1] >= 'a') && (p
[1] <= 'z')) ||
1187 ((p
[1] >= 'A') && (p
[1] <= 'Z'))) &&
1189 (!strcmp(uri
->scheme
, "file"))) {
1190 if (len
+ 3 >= max
) {
1191 temp
= realloc2n(ret
, &max
);
1192 if (temp
== NULL
) goto mem_error
;
1200 if (len
+ 3 >= max
) {
1201 temp
= realloc2n(ret
, &max
);
1202 if (temp
== NULL
) goto mem_error
;
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');
1219 if (uri
->query
!= NULL
) {
1220 if (len
+ 1 >= max
) {
1221 temp
= realloc2n(ret
, &max
);
1222 if (temp
== NULL
) goto mem_error
;
1228 if (len
+ 1 >= max
) {
1229 temp
= realloc2n(ret
, &max
);
1230 if (temp
== NULL
) goto mem_error
;
1237 if (uri
->fragment
!= NULL
) {
1238 if (len
+ 3 >= max
) {
1239 temp
= realloc2n(ret
, &max
);
1240 if (temp
== NULL
) goto mem_error
;
1246 if (len
+ 3 >= max
) {
1247 temp
= realloc2n(ret
, &max
);
1248 if (temp
== NULL
) goto mem_error
;
1251 if ((IS_UNRESERVED(*(p
))) || (IS_RESERVED(*(p
))))
1254 int val
= *(unsigned char *)p
++;
1255 int hi
= val
/ 0x10, lo
= val
% 0x10;
1257 ret
[len
++] = hi
+ (hi
> 9? 'A'-10 : '0');
1258 ret
[len
++] = lo
+ (lo
> 9? 'A'-10 : '0');
1263 temp
= realloc2n(ret
, &max
);
1264 if (temp
== NULL
) goto mem_error
;
1277 * @uri: pointer to an URI
1279 * Make sure the URI struct is free of content
1282 uri_clean(URI
*uri
) {
1283 if (uri
== NULL
) return;
1285 g_free(uri
->scheme
);
1287 g_free(uri
->server
);
1293 g_free(uri
->fragment
);
1294 uri
->fragment
= NULL
;
1295 g_free(uri
->opaque
);
1297 g_free(uri
->authority
);
1298 uri
->authority
= NULL
;
1305 * @uri: pointer to an URI
1307 * Free up the URI struct
1310 uri_free(URI
*uri
) {
1315 /************************************************************************
1317 * Helper functions *
1319 ************************************************************************/
1322 * normalize_uri_path:
1323 * @path: pointer to the path string
1325 * Applies the 5 normalization steps to a path string--that is, RFC 2396
1326 * Section 5.2, steps 6.c through 6.g.
1328 * Normalization occurs directly on the string, no new allocation is done
1330 * Returns 0 or an error code
1333 normalize_uri_path(char *path
) {
1339 /* Skip all initial "/" chars. We want to get to the beginning of the
1340 * first non-empty segment.
1343 while (cur
[0] == '/')
1348 /* Keep everything we've seen so far. */
1352 * Analyze each segment in sequence for cases (c) and (d).
1354 while (cur
[0] != '\0') {
1356 * c) All occurrences of "./", where "." is a complete path segment,
1357 * are removed from the buffer string.
1359 if ((cur
[0] == '.') && (cur
[1] == '/')) {
1361 /* '//' normalization should be done at this point too */
1362 while (cur
[0] == '/')
1368 * d) If the buffer string ends with "." as a complete path segment,
1369 * that "." is removed.
1371 if ((cur
[0] == '.') && (cur
[1] == '\0'))
1374 /* Otherwise keep the segment. */
1375 while (cur
[0] != '/') {
1378 (out
++)[0] = (cur
++)[0];
1381 while ((cur
[0] == '/') && (cur
[1] == '/'))
1384 (out
++)[0] = (cur
++)[0];
1389 /* Reset to the beginning of the first segment for the next sequence. */
1391 while (cur
[0] == '/')
1397 * Analyze each segment in sequence for cases (e) and (f).
1399 * e) All occurrences of "<segment>/../", where <segment> is a
1400 * complete path segment not equal to "..", are removed from the
1401 * buffer string. Removal of these path segments is performed
1402 * iteratively, removing the leftmost matching pattern on each
1403 * iteration, until no matching pattern remains.
1405 * f) If the buffer string ends with "<segment>/..", where <segment>
1406 * is a complete path segment not equal to "..", that
1407 * "<segment>/.." is removed.
1409 * To satisfy the "iterative" clause in (e), we need to collapse the
1410 * string every time we find something that needs to be removed. Thus,
1411 * we don't need to keep two pointers into the string: we only need a
1412 * "current position" pointer.
1417 /* At the beginning of each iteration of this loop, "cur" points to
1418 * the first character of the segment we want to examine.
1421 /* Find the end of the current segment. */
1423 while ((segp
[0] != '/') && (segp
[0] != '\0'))
1426 /* If this is the last segment, we're done (we need at least two
1427 * segments to meet the criteria for the (e) and (f) cases).
1429 if (segp
[0] == '\0')
1432 /* If the first segment is "..", or if the next segment _isn't_ "..",
1433 * keep this segment and try the next one.
1436 if (((cur
[0] == '.') && (cur
[1] == '.') && (segp
== cur
+3))
1437 || ((segp
[0] != '.') || (segp
[1] != '.')
1438 || ((segp
[2] != '/') && (segp
[2] != '\0')))) {
1443 /* If we get here, remove this segment and the next one and back up
1444 * to the previous segment (if there is one), to implement the
1445 * "iteratively" clause. It's pretty much impossible to back up
1446 * while maintaining two pointers into the buffer, so just compact
1447 * the whole buffer now.
1450 /* If this is the end of the buffer, we're done. */
1451 if (segp
[2] == '\0') {
1455 /* Valgrind complained, strcpy(cur, segp + 3); */
1456 /* string will overlap, do not use strcpy */
1459 while ((*tmp
++ = *segp
++) != 0)
1462 /* If there are no previous segments, then keep going from here. */
1464 while ((segp
> path
) && ((--segp
)[0] == '/'))
1469 /* "segp" is pointing to the end of a previous segment; find it's
1470 * start. We need to back up to the previous segment and start
1471 * over with that to handle things like "foo/bar/../..". If we
1472 * don't do this, then on the first pass we'll remove the "bar/..",
1473 * but be pointing at the second ".." so we won't realize we can also
1474 * remove the "foo/..".
1477 while ((cur
> path
) && (cur
[-1] != '/'))
1483 * g) If the resulting buffer string still begins with one or more
1484 * complete path segments of "..", then the reference is
1485 * considered to be in error. Implementations may handle this
1486 * error by retaining these components in the resolved path (i.e.,
1487 * treating them as part of the final URI), by removing them from
1488 * the resolved path (i.e., discarding relative levels above the
1489 * root), or by avoiding traversal of the reference.
1491 * We discard them from the final path.
1493 if (path
[0] == '/') {
1495 while ((cur
[0] == '/') && (cur
[1] == '.') && (cur
[2] == '.')
1496 && ((cur
[3] == '/') || (cur
[3] == '\0')))
1501 while (cur
[0] != '\0')
1502 (out
++)[0] = (cur
++)[0];
1510 static int is_hex(char c
) {
1511 if (((c
>= '0') && (c
<= '9')) ||
1512 ((c
>= 'a') && (c
<= 'f')) ||
1513 ((c
>= 'A') && (c
<= 'F')))
1520 * uri_string_unescape:
1521 * @str: the string to unescape
1522 * @len: the length in bytes to unescape (or <= 0 to indicate full string)
1523 * @target: optional destination buffer
1525 * Unescaping routine, but does not check that the string is an URI. The
1526 * output is a direct unsigned char translation of %XX values (no encoding)
1527 * Note that the length of the result can only be smaller or same size as
1530 * Returns a copy of the string, but unescaped, will return NULL only in case
1534 uri_string_unescape(const char *str
, int len
, char *target
) {
1540 if (len
<= 0) len
= strlen(str
);
1541 if (len
< 0) return(NULL
);
1543 if (target
== NULL
) {
1544 ret
= g_malloc(len
+ 1);
1550 if ((len
> 2) && (*in
== '%') && (is_hex(in
[1])) && (is_hex(in
[2]))) {
1552 if ((*in
>= '0') && (*in
<= '9'))
1554 else if ((*in
>= 'a') && (*in
<= 'f'))
1555 *out
= (*in
- 'a') + 10;
1556 else if ((*in
>= 'A') && (*in
<= 'F'))
1557 *out
= (*in
- 'A') + 10;
1559 if ((*in
>= '0') && (*in
<= '9'))
1560 *out
= *out
* 16 + (*in
- '0');
1561 else if ((*in
>= 'a') && (*in
<= 'f'))
1562 *out
= *out
* 16 + (*in
- 'a') + 10;
1563 else if ((*in
>= 'A') && (*in
<= 'F'))
1564 *out
= *out
* 16 + (*in
- 'A') + 10;
1578 * uri_string_escape:
1579 * @str: string to escape
1580 * @list: exception list string of chars not to escape
1582 * This routine escapes a string to hex, ignoring reserved characters (a-z)
1583 * and the characters in the exception list.
1585 * Returns a new escaped string or NULL in case of error.
1588 uri_string_escape(const char *str
, const char *list
) {
1597 return(g_strdup(str
));
1599 if (!(len
> 0)) return(NULL
);
1602 ret
= g_malloc(len
);
1606 if (len
- out
<= 3) {
1607 temp
= realloc2n(ret
, &len
);
1613 if ((ch
!= '@') && (!IS_UNRESERVED(ch
)) && (!strchr(list
, ch
))) {
1618 ret
[out
++] = '0' + val
;
1620 ret
[out
++] = 'A' + val
- 0xA;
1623 ret
[out
++] = '0' + val
;
1625 ret
[out
++] = 'A' + val
- 0xA;
1636 /************************************************************************
1638 * Public functions *
1640 ************************************************************************/
1644 * @URI: the URI instance found in the document
1645 * @base: the base value
1647 * Computes he final URI of the reference done by checking that
1648 * the given URI is valid, and building the final URI using the
1649 * base URI. This is processed according to section 5.2 of the
1652 * 5.2. Resolving Relative References to Absolute Form
1654 * Returns a new URI string (to be freed by the caller) or NULL in case
1658 uri_resolve(const char *uri
, const char *base
) {
1660 int ret
, len
, indx
, cur
, out
;
1666 * 1) The URI reference is parsed into the potential four components and
1667 * fragment identifier, as described in Section 4.3.
1669 * NOTE that a completely empty URI is treated by modern browsers
1670 * as a reference to "." rather than as a synonym for the current
1671 * URI. Should we do that here?
1680 ret
= uri_parse_into(ref
, uri
);
1687 if ((ref
!= NULL
) && (ref
->scheme
!= NULL
)) {
1689 * The URI is absolute don't modify.
1691 val
= g_strdup(uri
);
1700 ret
= uri_parse_into(bas
, base
);
1704 val
= uri_to_string(ref
);
1709 * the base fragment must be ignored
1711 g_free(bas
->fragment
);
1712 bas
->fragment
= NULL
;
1713 val
= uri_to_string(bas
);
1718 * 2) If the path component is empty and the scheme, authority, and
1719 * query components are undefined, then it is a reference to the
1720 * current document and we are done. Otherwise, the reference URI's
1721 * query and fragment components are defined as found (or not found)
1722 * within the URI reference and not inherited from the base URI.
1724 * NOTE that in modern browsers, the parsing differs from the above
1725 * in the following aspect: the query component is allowed to be
1726 * defined while still treating this as a reference to the current
1732 if ((ref
->scheme
== NULL
) && (ref
->path
== NULL
) &&
1733 ((ref
->authority
== NULL
) && (ref
->server
== NULL
))) {
1734 res
->scheme
= g_strdup(bas
->scheme
);
1735 if (bas
->authority
!= NULL
)
1736 res
->authority
= g_strdup(bas
->authority
);
1737 else if (bas
->server
!= NULL
) {
1738 res
->server
= g_strdup(bas
->server
);
1739 res
->user
= g_strdup(bas
->user
);
1740 res
->port
= bas
->port
;
1742 res
->path
= g_strdup(bas
->path
);
1743 if (ref
->query
!= NULL
) {
1744 res
->query
= g_strdup (ref
->query
);
1746 res
->query
= g_strdup(bas
->query
);
1748 res
->fragment
= g_strdup(ref
->fragment
);
1753 * 3) If the scheme component is defined, indicating that the reference
1754 * starts with a scheme name, then the reference is interpreted as an
1755 * absolute URI and we are done. Otherwise, the reference URI's
1756 * scheme is inherited from the base URI's scheme component.
1758 if (ref
->scheme
!= NULL
) {
1759 val
= uri_to_string(ref
);
1762 res
->scheme
= g_strdup(bas
->scheme
);
1764 res
->query
= g_strdup(ref
->query
);
1765 res
->fragment
= g_strdup(ref
->fragment
);
1768 * 4) If the authority component is defined, then the reference is a
1769 * network-path and we skip to step 7. Otherwise, the reference
1770 * URI's authority is inherited from the base URI's authority
1771 * component, which will also be undefined if the URI scheme does not
1772 * use an authority component.
1774 if ((ref
->authority
!= NULL
) || (ref
->server
!= NULL
)) {
1775 if (ref
->authority
!= NULL
)
1776 res
->authority
= g_strdup(ref
->authority
);
1778 res
->server
= g_strdup(ref
->server
);
1779 res
->user
= g_strdup(ref
->user
);
1780 res
->port
= ref
->port
;
1782 res
->path
= g_strdup(ref
->path
);
1785 if (bas
->authority
!= NULL
)
1786 res
->authority
= g_strdup(bas
->authority
);
1787 else if (bas
->server
!= NULL
) {
1788 res
->server
= g_strdup(bas
->server
);
1789 res
->user
= g_strdup(bas
->user
);
1790 res
->port
= bas
->port
;
1794 * 5) If the path component begins with a slash character ("/"), then
1795 * the reference is an absolute-path and we skip to step 7.
1797 if ((ref
->path
!= NULL
) && (ref
->path
[0] == '/')) {
1798 res
->path
= g_strdup(ref
->path
);
1804 * 6) If this step is reached, then we are resolving a relative-path
1805 * reference. The relative path needs to be merged with the base
1806 * URI's path. Although there are many ways to do this, we will
1807 * describe a simple method using a separate string buffer.
1809 * Allocate a buffer large enough for the result string.
1811 len
= 2; /* extra / and 0 */
1812 if (ref
->path
!= NULL
)
1813 len
+= strlen(ref
->path
);
1814 if (bas
->path
!= NULL
)
1815 len
+= strlen(bas
->path
);
1816 res
->path
= g_malloc(len
);
1820 * a) All but the last segment of the base URI's path component is
1821 * copied to the buffer. In other words, any characters after the
1822 * last (right-most) slash character, if any, are excluded.
1826 if (bas
->path
!= NULL
) {
1827 while (bas
->path
[cur
] != 0) {
1828 while ((bas
->path
[cur
] != 0) && (bas
->path
[cur
] != '/'))
1830 if (bas
->path
[cur
] == 0)
1835 res
->path
[out
] = bas
->path
[out
];
1843 * b) The reference's path component is appended to the buffer
1846 if (ref
->path
!= NULL
&& ref
->path
[0] != 0) {
1849 * Ensure the path includes a '/'
1851 if ((out
== 0) && (bas
->server
!= NULL
))
1852 res
->path
[out
++] = '/';
1853 while (ref
->path
[indx
] != 0) {
1854 res
->path
[out
++] = ref
->path
[indx
++];
1860 * Steps c) to h) are really path normalization steps
1862 normalize_uri_path(res
->path
);
1867 * 7) The resulting URI components, including any inherited from the
1868 * base URI, are recombined to give the absolute form of the URI
1871 val
= uri_to_string(res
);
1884 * uri_resolve_relative:
1885 * @URI: the URI reference under consideration
1886 * @base: the base value
1888 * Expresses the URI of the reference in terms relative to the
1889 * base. Some examples of this operation include:
1890 * base = "http://site1.com/docs/book1.html"
1891 * URI input URI returned
1892 * docs/pic1.gif pic1.gif
1893 * docs/img/pic1.gif img/pic1.gif
1894 * img/pic1.gif ../img/pic1.gif
1895 * http://site1.com/docs/pic1.gif pic1.gif
1896 * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif
1898 * base = "docs/book1.html"
1899 * URI input URI returned
1900 * docs/pic1.gif pic1.gif
1901 * docs/img/pic1.gif img/pic1.gif
1902 * img/pic1.gif ../img/pic1.gif
1903 * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif
1906 * Note: if the URI reference is really weird or complicated, it may be
1907 * worthwhile to first convert it into a "nice" one by calling
1908 * uri_resolve (using 'base') before calling this routine,
1909 * since this routine (for reasonable efficiency) assumes URI has
1910 * already been through some validation.
1912 * Returns a new URI string (to be freed by the caller) or NULL in case
1916 uri_resolve_relative (const char *uri
, const char * base
)
1926 char *bptr
, *uptr
, *vptr
;
1927 int remove_path
= 0;
1929 if ((uri
== NULL
) || (*uri
== 0))
1933 * First parse URI into a standard form
1938 /* If URI not already in "relative" form */
1939 if (uri
[0] != '.') {
1940 ret
= uri_parse_into (ref
, uri
);
1942 goto done
; /* Error in URI, return NULL */
1944 ref
->path
= g_strdup(uri
);
1947 * Next parse base into the same standard form
1949 if ((base
== NULL
) || (*base
== 0)) {
1950 val
= g_strdup (uri
);
1956 if (base
[0] != '.') {
1957 ret
= uri_parse_into (bas
, base
);
1959 goto done
; /* Error in base, return NULL */
1961 bas
->path
= g_strdup(base
);
1964 * If the scheme / server on the URI differs from the base,
1965 * just return the URI
1967 if ((ref
->scheme
!= NULL
) &&
1968 ((bas
->scheme
== NULL
) ||
1969 (strcmp (bas
->scheme
, ref
->scheme
)) ||
1970 (strcmp (bas
->server
, ref
->server
)))) {
1971 val
= g_strdup (uri
);
1974 if (!strcmp(bas
->path
, ref
->path
)) {
1978 if (bas
->path
== NULL
) {
1979 val
= g_strdup(ref
->path
);
1982 if (ref
->path
== NULL
) {
1983 ref
->path
= (char *) "/";
1988 * At this point (at last!) we can compare the two paths
1990 * First we take care of the special case where either of the
1991 * two path components may be missing (bug 316224)
1993 if (bas
->path
== NULL
) {
1994 if (ref
->path
!= NULL
) {
1998 /* exception characters from uri_to_string */
1999 val
= uri_string_escape(uptr
, "/;&=+$,");
2004 if (ref
->path
== NULL
) {
2005 for (ix
= 0; bptr
[ix
] != 0; ix
++) {
2006 if (bptr
[ix
] == '/')
2010 len
= 1; /* this is for a string terminator only */
2013 * Next we compare the two strings and find where they first differ
2015 if ((ref
->path
[pos
] == '.') && (ref
->path
[pos
+1] == '/'))
2017 if ((*bptr
== '.') && (bptr
[1] == '/'))
2019 else if ((*bptr
== '/') && (ref
->path
[pos
] != '/'))
2021 while ((bptr
[pos
] == ref
->path
[pos
]) && (bptr
[pos
] != 0))
2024 if (bptr
[pos
] == ref
->path
[pos
]) {
2026 goto done
; /* (I can't imagine why anyone would do this) */
2030 * In URI, "back up" to the last '/' encountered. This will be the
2031 * beginning of the "unique" suffix of URI
2034 if ((ref
->path
[ix
] == '/') && (ix
> 0))
2036 else if ((ref
->path
[ix
] == 0) && (ix
> 1) && (ref
->path
[ix
- 1] == '/'))
2038 for (; ix
> 0; ix
--) {
2039 if (ref
->path
[ix
] == '/')
2046 uptr
= &ref
->path
[ix
];
2050 * In base, count the number of '/' from the differing point
2052 if (bptr
[pos
] != ref
->path
[pos
]) {/* check for trivial URI == base */
2053 for (; bptr
[ix
] != 0; ix
++) {
2054 if (bptr
[ix
] == '/')
2058 len
= strlen (uptr
) + 1;
2063 /* exception characters from uri_to_string */
2064 val
= uri_string_escape(uptr
, "/;&=+$,");
2069 * Allocate just enough space for the returned string -
2070 * length of the remainder of the URI, plus enough space
2071 * for the "../" groups, plus one for the terminator
2073 val
= g_malloc (len
+ 3 * nbslash
);
2076 * Put in as many "../" as needed
2078 for (; nbslash
>0; nbslash
--) {
2084 * Finish up with the end of the URI
2087 if ((vptr
> val
) && (len
> 0) &&
2088 (uptr
[0] == '/') && (vptr
[-1] == '/')) {
2089 memcpy (vptr
, uptr
+ 1, len
- 1);
2092 memcpy (vptr
, uptr
, len
);
2099 /* escape the freshly-built path */
2101 /* exception characters from uri_to_string */
2102 val
= uri_string_escape(vptr
, "/;&=+$,");
2107 * Free the working variables
2109 if (remove_path
!= 0)
2120 * Utility functions to help parse and assemble query strings.
2123 struct QueryParams
*
2124 query_params_new (int init_alloc
)
2126 struct QueryParams
*ps
;
2128 if (init_alloc
<= 0) init_alloc
= 1;
2130 ps
= g_new(QueryParams
, 1);
2132 ps
->alloc
= init_alloc
;
2133 ps
->p
= g_new(QueryParam
, ps
->alloc
);
2138 /* Ensure there is space to store at least one more parameter
2139 * at the end of the set.
2142 query_params_append (struct QueryParams
*ps
,
2143 const char *name
, const char *value
)
2145 if (ps
->n
>= ps
->alloc
) {
2146 ps
->p
= g_renew(QueryParam
, ps
->p
, ps
->alloc
* 2);
2150 ps
->p
[ps
->n
].name
= g_strdup(name
);
2151 ps
->p
[ps
->n
].value
= g_strdup(value
);
2152 ps
->p
[ps
->n
].ignore
= 0;
2159 query_params_free (struct QueryParams
*ps
)
2163 for (i
= 0; i
< ps
->n
; ++i
) {
2164 g_free (ps
->p
[i
].name
);
2165 g_free (ps
->p
[i
].value
);
2171 struct QueryParams
*
2172 query_params_parse (const char *query
)
2174 struct QueryParams
*ps
;
2175 const char *end
, *eq
;
2177 ps
= query_params_new (0);
2178 if (!query
|| query
[0] == '\0') return ps
;
2181 char *name
= NULL
, *value
= NULL
;
2183 /* Find the next separator, or end of the string. */
2184 end
= strchr (query
, '&');
2186 end
= strchr (query
, ';');
2188 end
= query
+ strlen (query
);
2190 /* Find the first '=' character between here and end. */
2191 eq
= strchr (query
, '=');
2192 if (eq
&& eq
>= end
) eq
= NULL
;
2194 /* Empty section (eg. "&&"). */
2198 /* If there is no '=' character, then we have just "name"
2199 * and consistent with CGI.pm we assume value is "".
2202 name
= uri_string_unescape (query
, end
- query
, NULL
);
2205 /* Or if we have "name=" here (works around annoying
2206 * problem when calling uri_string_unescape with len = 0).
2208 else if (eq
+1 == end
) {
2209 name
= uri_string_unescape (query
, eq
- query
, NULL
);
2210 value
= g_new0(char, 1);
2212 /* If the '=' character is at the beginning then we have
2213 * "=value" and consistent with CGI.pm we _ignore_ this.
2215 else if (query
== eq
)
2218 /* Otherwise it's "name=value". */
2220 name
= uri_string_unescape (query
, eq
- query
, NULL
);
2221 value
= uri_string_unescape (eq
+1, end
- (eq
+1), NULL
);
2224 /* Append to the parameter set. */
2225 query_params_append (ps
, name
, value
);
2231 if (*query
) query
++; /* skip '&' separator */