qapi: Drop redundant documentation of inherited members
[qemu/kevin.git] / util / uri.c
blob573174bf4756a2eab299b023be208a0c816b7d03
1 /**
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.
29 * daniel@veillard.com
33 * Copyright (C) 2007, 2009-2010 Red Hat, Inc.
35 * This library is free software; you can redistribute it and/or
36 * modify it under the terms of the GNU Lesser General Public
37 * License as published by the Free Software Foundation; either
38 * version 2.1 of the License, or (at your option) any later version.
40 * This library is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43 * Lesser General Public License for more details.
45 * You should have received a copy of the GNU Lesser General Public
46 * License along with this library. If not, see <https://www.gnu.org/licenses/>.
48 * Authors:
49 * Richard W.M. Jones <rjones@redhat.com>
53 #include "qemu/osdep.h"
54 #include "qemu/cutils.h"
56 #include "qemu/uri.h"
58 static void uri_clean(URI *uri);
61 * Old rule from 2396 used in legacy handling code
62 * alpha = lowalpha | upalpha
64 #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
67 * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
68 * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
69 * "u" | "v" | "w" | "x" | "y" | "z"
72 #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z'))
75 * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" |
76 * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" |
77 * "U" | "V" | "W" | "X" | "Y" | "Z"
79 #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z'))
81 #ifdef IS_DIGIT
82 #undef IS_DIGIT
83 #endif
85 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
87 #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
90 * alphanum = alpha | digit
93 #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
96 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
99 #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
100 ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
101 ((x) == '(') || ((x) == ')'))
104 * unwise = "{" | "}" | "|" | "\" | "^" | "`"
107 #define IS_UNWISE(p) \
108 (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \
109 ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \
110 ((*(p) == ']')) || ((*(p) == '`')))
112 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
113 * "[" | "]"
116 #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
117 ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
118 ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \
119 ((x) == ']'))
122 * unreserved = alphanum | mark
125 #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
128 * Skip to next pointer char, handle escaped sequences
131 #define NEXT(p) ((*p == '%') ? p += 3 : p++)
134 * Productions from the spec.
136 * authority = server | reg_name
137 * reg_name = 1*( unreserved | escaped | "$" | "," |
138 * ";" | ":" | "@" | "&" | "=" | "+" )
140 * path = [ abs_path | opaque_part ]
143 /************************************************************************
145 * RFC 3986 parser *
147 ************************************************************************/
149 #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9'))
150 #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \
151 ((*(p) >= 'A') && (*(p) <= 'Z')))
152 #define ISA_HEXDIG(p) \
153 (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \
154 ((*(p) >= 'A') && (*(p) <= 'F')))
157 * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
158 * / "*" / "+" / "," / ";" / "="
160 #define ISA_SUB_DELIM(p) \
161 (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \
162 ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \
163 ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \
164 ((*(p) == '=')) || ((*(p) == '\'')))
167 * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
169 #define ISA_UNRESERVED(p) \
170 ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \
171 ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~')))
174 * pct-encoded = "%" HEXDIG HEXDIG
176 #define ISA_PCT_ENCODED(p) \
177 ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2)))
180 * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
182 #define ISA_PCHAR(p) \
183 (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \
184 ((*(p) == ':')) || ((*(p) == '@')))
187 * rfc3986_parse_scheme:
188 * @uri: pointer to an URI structure
189 * @str: pointer to the string to analyze
191 * Parse an URI scheme
193 * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
195 * Returns 0 or the error code
197 static int rfc3986_parse_scheme(URI *uri, const char **str)
199 const char *cur;
201 if (str == NULL) {
202 return -1;
205 cur = *str;
206 if (!ISA_ALPHA(cur)) {
207 return 2;
209 cur++;
210 while (ISA_ALPHA(cur) || ISA_DIGIT(cur) || (*cur == '+') || (*cur == '-') ||
211 (*cur == '.')) {
212 cur++;
214 if (uri != NULL) {
215 g_free(uri->scheme);
216 uri->scheme = g_strndup(*str, cur - *str);
218 *str = cur;
219 return 0;
223 * rfc3986_parse_fragment:
224 * @uri: pointer to an URI structure
225 * @str: pointer to the string to analyze
227 * Parse the query part of an URI
229 * fragment = *( pchar / "/" / "?" )
230 * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']'
231 * in the fragment identifier but this is used very broadly for
232 * xpointer scheme selection, so we are allowing it here to not break
233 * for example all the DocBook processing chains.
235 * Returns 0 or the error code
237 static int rfc3986_parse_fragment(URI *uri, const char **str)
239 const char *cur;
241 if (str == NULL) {
242 return -1;
245 cur = *str;
247 while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') ||
248 (*cur == '[') || (*cur == ']') ||
249 ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) {
250 NEXT(cur);
252 if (uri != NULL) {
253 g_free(uri->fragment);
254 if (uri->cleanup & 2) {
255 uri->fragment = g_strndup(*str, cur - *str);
256 } else {
257 uri->fragment = g_uri_unescape_segment(*str, cur, NULL);
260 *str = cur;
261 return 0;
265 * rfc3986_parse_query:
266 * @uri: pointer to an URI structure
267 * @str: pointer to the string to analyze
269 * Parse the query part of an URI
271 * query = *uric
273 * Returns 0 or the error code
275 static int rfc3986_parse_query(URI *uri, const char **str)
277 const char *cur;
279 if (str == NULL) {
280 return -1;
283 cur = *str;
285 while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') ||
286 ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) {
287 NEXT(cur);
289 if (uri != NULL) {
290 g_free(uri->query);
291 uri->query = g_strndup(*str, cur - *str);
293 *str = cur;
294 return 0;
298 * rfc3986_parse_port:
299 * @uri: pointer to an URI structure
300 * @str: the string to analyze
302 * Parse a port part and fills in the appropriate fields
303 * of the @uri structure
305 * port = *DIGIT
307 * Returns 0 or the error code
309 static int rfc3986_parse_port(URI *uri, const char **str)
311 const char *cur = *str;
312 int port = 0;
314 if (ISA_DIGIT(cur)) {
315 while (ISA_DIGIT(cur)) {
316 port = port * 10 + (*cur - '0');
317 if (port > 65535) {
318 return 1;
320 cur++;
322 if (uri) {
323 uri->port = port;
325 *str = cur;
326 return 0;
328 return 1;
332 * rfc3986_parse_user_info:
333 * @uri: pointer to an URI structure
334 * @str: the string to analyze
336 * Parse a user information part and fill in the appropriate fields
337 * of the @uri structure
339 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
341 * Returns 0 or the error code
343 static int rfc3986_parse_user_info(URI *uri, const char **str)
345 const char *cur;
347 cur = *str;
348 while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur) ||
349 (*cur == ':')) {
350 NEXT(cur);
352 if (*cur == '@') {
353 if (uri != NULL) {
354 g_free(uri->user);
355 if (uri->cleanup & 2) {
356 uri->user = g_strndup(*str, cur - *str);
357 } else {
358 uri->user = g_uri_unescape_segment(*str, cur, NULL);
361 *str = cur;
362 return 0;
364 return 1;
368 * rfc3986_parse_dec_octet:
369 * @str: the string to analyze
371 * dec-octet = DIGIT ; 0-9
372 * / %x31-39 DIGIT ; 10-99
373 * / "1" 2DIGIT ; 100-199
374 * / "2" %x30-34 DIGIT ; 200-249
375 * / "25" %x30-35 ; 250-255
377 * Skip a dec-octet.
379 * Returns 0 if found and skipped, 1 otherwise
381 static int rfc3986_parse_dec_octet(const char **str)
383 const char *cur = *str;
385 if (!(ISA_DIGIT(cur))) {
386 return 1;
388 if (!ISA_DIGIT(cur + 1)) {
389 cur++;
390 } else if ((*cur != '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur + 2))) {
391 cur += 2;
392 } else if ((*cur == '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur + 2))) {
393 cur += 3;
394 } else if ((*cur == '2') && (*(cur + 1) >= '0') && (*(cur + 1) <= '4') &&
395 (ISA_DIGIT(cur + 2))) {
396 cur += 3;
397 } else if ((*cur == '2') && (*(cur + 1) == '5') && (*(cur + 2) >= '0') &&
398 (*(cur + 1) <= '5')) {
399 cur += 3;
400 } else {
401 return 1;
403 *str = cur;
404 return 0;
407 * rfc3986_parse_host:
408 * @uri: pointer to an URI structure
409 * @str: the string to analyze
411 * Parse an host part and fills in the appropriate fields
412 * of the @uri structure
414 * host = IP-literal / IPv4address / reg-name
415 * IP-literal = "[" ( IPv6address / IPvFuture ) "]"
416 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
417 * reg-name = *( unreserved / pct-encoded / sub-delims )
419 * Returns 0 or the error code
421 static int rfc3986_parse_host(URI *uri, const char **str)
423 const char *cur = *str;
424 const char *host;
426 host = cur;
428 * IPv6 and future addressing scheme are enclosed between brackets
430 if (*cur == '[') {
431 cur++;
432 while ((*cur != ']') && (*cur != 0)) {
433 cur++;
435 if (*cur != ']') {
436 return 1;
438 cur++;
439 goto found;
442 * try to parse an IPv4
444 if (ISA_DIGIT(cur)) {
445 if (rfc3986_parse_dec_octet(&cur) != 0) {
446 goto not_ipv4;
448 if (*cur != '.') {
449 goto not_ipv4;
451 cur++;
452 if (rfc3986_parse_dec_octet(&cur) != 0) {
453 goto not_ipv4;
455 if (*cur != '.') {
456 goto not_ipv4;
458 if (rfc3986_parse_dec_octet(&cur) != 0) {
459 goto not_ipv4;
461 if (*cur != '.') {
462 goto not_ipv4;
464 if (rfc3986_parse_dec_octet(&cur) != 0) {
465 goto not_ipv4;
467 goto found;
468 not_ipv4:
469 cur = *str;
472 * then this should be a hostname which can be empty
474 while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur)) {
475 NEXT(cur);
477 found:
478 if (uri != NULL) {
479 g_free(uri->authority);
480 uri->authority = NULL;
481 g_free(uri->server);
482 if (cur != host) {
483 if (uri->cleanup & 2) {
484 uri->server = g_strndup(host, cur - host);
485 } else {
486 uri->server = g_uri_unescape_segment(host, cur, NULL);
488 } else {
489 uri->server = NULL;
492 *str = cur;
493 return 0;
497 * rfc3986_parse_authority:
498 * @uri: pointer to an URI structure
499 * @str: the string to analyze
501 * Parse an authority part and fills in the appropriate fields
502 * of the @uri structure
504 * authority = [ userinfo "@" ] host [ ":" port ]
506 * Returns 0 or the error code
508 static int rfc3986_parse_authority(URI *uri, const char **str)
510 const char *cur;
511 int ret;
513 cur = *str;
515 * try to parse a userinfo and check for the trailing @
517 ret = rfc3986_parse_user_info(uri, &cur);
518 if ((ret != 0) || (*cur != '@')) {
519 cur = *str;
520 } else {
521 cur++;
523 ret = rfc3986_parse_host(uri, &cur);
524 if (ret != 0) {
525 return ret;
527 if (*cur == ':') {
528 cur++;
529 ret = rfc3986_parse_port(uri, &cur);
530 if (ret != 0) {
531 return ret;
534 *str = cur;
535 return 0;
539 * rfc3986_parse_segment:
540 * @str: the string to analyze
541 * @forbid: an optional forbidden character
542 * @empty: allow an empty segment
544 * Parse a segment and fills in the appropriate fields
545 * of the @uri structure
547 * segment = *pchar
548 * segment-nz = 1*pchar
549 * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
550 * ; non-zero-length segment without any colon ":"
552 * Returns 0 or the error code
554 static int rfc3986_parse_segment(const char **str, char forbid, int empty)
556 const char *cur;
558 cur = *str;
559 if (!ISA_PCHAR(cur)) {
560 if (empty) {
561 return 0;
563 return 1;
565 while (ISA_PCHAR(cur) && (*cur != forbid)) {
566 NEXT(cur);
568 *str = cur;
569 return 0;
573 * rfc3986_parse_path_ab_empty:
574 * @uri: pointer to an URI structure
575 * @str: the string to analyze
577 * Parse an path absolute or empty and fills in the appropriate fields
578 * of the @uri structure
580 * path-abempty = *( "/" segment )
582 * Returns 0 or the error code
584 static int rfc3986_parse_path_ab_empty(URI *uri, const char **str)
586 const char *cur;
587 int ret;
589 cur = *str;
591 while (*cur == '/') {
592 cur++;
593 ret = rfc3986_parse_segment(&cur, 0, 1);
594 if (ret != 0) {
595 return ret;
598 if (uri != NULL) {
599 g_free(uri->path);
600 if (*str != cur) {
601 if (uri->cleanup & 2) {
602 uri->path = g_strndup(*str, cur - *str);
603 } else {
604 uri->path = g_uri_unescape_segment(*str, cur, NULL);
606 } else {
607 uri->path = NULL;
610 *str = cur;
611 return 0;
615 * rfc3986_parse_path_absolute:
616 * @uri: pointer to an URI structure
617 * @str: the string to analyze
619 * Parse an path absolute and fills in the appropriate fields
620 * of the @uri structure
622 * path-absolute = "/" [ segment-nz *( "/" segment ) ]
624 * Returns 0 or the error code
626 static int rfc3986_parse_path_absolute(URI *uri, const char **str)
628 const char *cur;
629 int ret;
631 cur = *str;
633 if (*cur != '/') {
634 return 1;
636 cur++;
637 ret = rfc3986_parse_segment(&cur, 0, 0);
638 if (ret == 0) {
639 while (*cur == '/') {
640 cur++;
641 ret = rfc3986_parse_segment(&cur, 0, 1);
642 if (ret != 0) {
643 return ret;
647 if (uri != NULL) {
648 g_free(uri->path);
649 if (cur != *str) {
650 if (uri->cleanup & 2) {
651 uri->path = g_strndup(*str, cur - *str);
652 } else {
653 uri->path = g_uri_unescape_segment(*str, cur, NULL);
655 } else {
656 uri->path = NULL;
659 *str = cur;
660 return 0;
664 * rfc3986_parse_path_rootless:
665 * @uri: pointer to an URI structure
666 * @str: the string to analyze
668 * Parse an path without root and fills in the appropriate fields
669 * of the @uri structure
671 * path-rootless = segment-nz *( "/" segment )
673 * Returns 0 or the error code
675 static int rfc3986_parse_path_rootless(URI *uri, const char **str)
677 const char *cur;
678 int ret;
680 cur = *str;
682 ret = rfc3986_parse_segment(&cur, 0, 0);
683 if (ret != 0) {
684 return ret;
686 while (*cur == '/') {
687 cur++;
688 ret = rfc3986_parse_segment(&cur, 0, 1);
689 if (ret != 0) {
690 return ret;
693 if (uri != NULL) {
694 g_free(uri->path);
695 if (cur != *str) {
696 if (uri->cleanup & 2) {
697 uri->path = g_strndup(*str, cur - *str);
698 } else {
699 uri->path = g_uri_unescape_segment(*str, cur, NULL);
701 } else {
702 uri->path = NULL;
705 *str = cur;
706 return 0;
710 * rfc3986_parse_path_no_scheme:
711 * @uri: pointer to an URI structure
712 * @str: the string to analyze
714 * Parse an path which is not a scheme and fills in the appropriate fields
715 * of the @uri structure
717 * path-noscheme = segment-nz-nc *( "/" segment )
719 * Returns 0 or the error code
721 static int rfc3986_parse_path_no_scheme(URI *uri, const char **str)
723 const char *cur;
724 int ret;
726 cur = *str;
728 ret = rfc3986_parse_segment(&cur, ':', 0);
729 if (ret != 0) {
730 return ret;
732 while (*cur == '/') {
733 cur++;
734 ret = rfc3986_parse_segment(&cur, 0, 1);
735 if (ret != 0) {
736 return ret;
739 if (uri != NULL) {
740 g_free(uri->path);
741 if (cur != *str) {
742 if (uri->cleanup & 2) {
743 uri->path = g_strndup(*str, cur - *str);
744 } else {
745 uri->path = g_uri_unescape_segment(*str, cur, NULL);
747 } else {
748 uri->path = NULL;
751 *str = cur;
752 return 0;
756 * rfc3986_parse_hier_part:
757 * @uri: pointer to an URI structure
758 * @str: the string to analyze
760 * Parse an hierarchical part and fills in the appropriate fields
761 * of the @uri structure
763 * hier-part = "//" authority path-abempty
764 * / path-absolute
765 * / path-rootless
766 * / path-empty
768 * Returns 0 or the error code
770 static int rfc3986_parse_hier_part(URI *uri, const char **str)
772 const char *cur;
773 int ret;
775 cur = *str;
777 if ((*cur == '/') && (*(cur + 1) == '/')) {
778 cur += 2;
779 ret = rfc3986_parse_authority(uri, &cur);
780 if (ret != 0) {
781 return ret;
783 ret = rfc3986_parse_path_ab_empty(uri, &cur);
784 if (ret != 0) {
785 return ret;
787 *str = cur;
788 return 0;
789 } else if (*cur == '/') {
790 ret = rfc3986_parse_path_absolute(uri, &cur);
791 if (ret != 0) {
792 return ret;
794 } else if (ISA_PCHAR(cur)) {
795 ret = rfc3986_parse_path_rootless(uri, &cur);
796 if (ret != 0) {
797 return ret;
799 } else {
800 /* path-empty is effectively empty */
801 if (uri != NULL) {
802 g_free(uri->path);
803 uri->path = NULL;
806 *str = cur;
807 return 0;
811 * rfc3986_parse_relative_ref:
812 * @uri: pointer to an URI structure
813 * @str: the string to analyze
815 * Parse an URI string and fills in the appropriate fields
816 * of the @uri structure
818 * relative-ref = relative-part [ "?" query ] [ "#" fragment ]
819 * relative-part = "//" authority path-abempty
820 * / path-absolute
821 * / path-noscheme
822 * / path-empty
824 * Returns 0 or the error code
826 static int rfc3986_parse_relative_ref(URI *uri, const char *str)
828 int ret;
830 if ((*str == '/') && (*(str + 1) == '/')) {
831 str += 2;
832 ret = rfc3986_parse_authority(uri, &str);
833 if (ret != 0) {
834 return ret;
836 ret = rfc3986_parse_path_ab_empty(uri, &str);
837 if (ret != 0) {
838 return ret;
840 } else if (*str == '/') {
841 ret = rfc3986_parse_path_absolute(uri, &str);
842 if (ret != 0) {
843 return ret;
845 } else if (ISA_PCHAR(str)) {
846 ret = rfc3986_parse_path_no_scheme(uri, &str);
847 if (ret != 0) {
848 return ret;
850 } else {
851 /* path-empty is effectively empty */
852 if (uri != NULL) {
853 g_free(uri->path);
854 uri->path = NULL;
858 if (*str == '?') {
859 str++;
860 ret = rfc3986_parse_query(uri, &str);
861 if (ret != 0) {
862 return ret;
865 if (*str == '#') {
866 str++;
867 ret = rfc3986_parse_fragment(uri, &str);
868 if (ret != 0) {
869 return ret;
872 if (*str != 0) {
873 uri_clean(uri);
874 return 1;
876 return 0;
880 * rfc3986_parse:
881 * @uri: pointer to an URI structure
882 * @str: the string to analyze
884 * Parse an URI string and fills in the appropriate fields
885 * of the @uri structure
887 * scheme ":" hier-part [ "?" query ] [ "#" fragment ]
889 * Returns 0 or the error code
891 static int rfc3986_parse(URI *uri, const char *str)
893 int ret;
895 ret = rfc3986_parse_scheme(uri, &str);
896 if (ret != 0) {
897 return ret;
899 if (*str != ':') {
900 return 1;
902 str++;
903 ret = rfc3986_parse_hier_part(uri, &str);
904 if (ret != 0) {
905 return ret;
907 if (*str == '?') {
908 str++;
909 ret = rfc3986_parse_query(uri, &str);
910 if (ret != 0) {
911 return ret;
914 if (*str == '#') {
915 str++;
916 ret = rfc3986_parse_fragment(uri, &str);
917 if (ret != 0) {
918 return ret;
921 if (*str != 0) {
922 uri_clean(uri);
923 return 1;
925 return 0;
929 * rfc3986_parse_uri_reference:
930 * @uri: pointer to an URI structure
931 * @str: the string to analyze
933 * Parse an URI reference string and fills in the appropriate fields
934 * of the @uri structure
936 * URI-reference = URI / relative-ref
938 * Returns 0 or the error code
940 static int rfc3986_parse_uri_reference(URI *uri, const char *str)
942 int ret;
944 if (str == NULL) {
945 return -1;
947 uri_clean(uri);
950 * Try first to parse absolute refs, then fallback to relative if
951 * it fails.
953 ret = rfc3986_parse(uri, str);
954 if (ret != 0) {
955 uri_clean(uri);
956 ret = rfc3986_parse_relative_ref(uri, str);
957 if (ret != 0) {
958 uri_clean(uri);
959 return ret;
962 return 0;
966 * uri_parse:
967 * @str: the URI string to analyze
969 * Parse an URI based on RFC 3986
971 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
973 * Returns a newly built URI or NULL in case of error
975 URI *uri_parse(const char *str)
977 URI *uri;
978 int ret;
980 if (str == NULL) {
981 return NULL;
983 uri = uri_new();
984 ret = rfc3986_parse_uri_reference(uri, str);
985 if (ret) {
986 uri_free(uri);
987 return NULL;
989 return uri;
993 * uri_parse_into:
994 * @uri: pointer to an URI structure
995 * @str: the string to analyze
997 * Parse an URI reference string based on RFC 3986 and fills in the
998 * appropriate fields of the @uri structure
1000 * URI-reference = URI / relative-ref
1002 * Returns 0 or the error code
1004 int uri_parse_into(URI *uri, const char *str)
1006 return rfc3986_parse_uri_reference(uri, str);
1010 * uri_parse_raw:
1011 * @str: the URI string to analyze
1012 * @raw: if 1 unescaping of URI pieces are disabled
1014 * Parse an URI but allows to keep intact the original fragments.
1016 * URI-reference = URI / relative-ref
1018 * Returns a newly built URI or NULL in case of error
1020 URI *uri_parse_raw(const char *str, int raw)
1022 URI *uri;
1023 int ret;
1025 if (str == NULL) {
1026 return NULL;
1028 uri = uri_new();
1029 if (raw) {
1030 uri->cleanup |= 2;
1032 ret = uri_parse_into(uri, str);
1033 if (ret) {
1034 uri_free(uri);
1035 return NULL;
1037 return uri;
1040 /************************************************************************
1042 * Generic URI structure functions *
1044 ************************************************************************/
1047 * uri_new:
1049 * Simply creates an empty URI
1051 * Returns the new structure or NULL in case of error
1053 URI *uri_new(void)
1055 return g_new0(URI, 1);
1059 * realloc2n:
1061 * Function to handle properly a reallocation when saving an URI
1062 * Also imposes some limit on the length of an URI string output
1064 static char *realloc2n(char *ret, int *max)
1066 char *temp;
1067 int tmp;
1069 tmp = *max * 2;
1070 temp = g_realloc(ret, (tmp + 1));
1071 *max = tmp;
1072 return temp;
1076 * uri_to_string:
1077 * @uri: pointer to an URI
1079 * Save the URI as an escaped string
1081 * Returns a new string (to be deallocated by caller)
1083 char *uri_to_string(URI *uri)
1085 char *ret = NULL;
1086 char *temp;
1087 const char *p;
1088 int len;
1089 int max;
1091 if (uri == NULL) {
1092 return NULL;
1095 max = 80;
1096 ret = g_malloc(max + 1);
1097 len = 0;
1099 if (uri->scheme != NULL) {
1100 p = uri->scheme;
1101 while (*p != 0) {
1102 if (len >= max) {
1103 temp = realloc2n(ret, &max);
1104 ret = temp;
1106 ret[len++] = *p++;
1108 if (len >= max) {
1109 temp = realloc2n(ret, &max);
1110 ret = temp;
1112 ret[len++] = ':';
1114 if (uri->opaque != NULL) {
1115 p = uri->opaque;
1116 while (*p != 0) {
1117 if (len + 3 >= max) {
1118 temp = realloc2n(ret, &max);
1119 ret = temp;
1121 if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p))) {
1122 ret[len++] = *p++;
1123 } else {
1124 int val = *(unsigned char *)p++;
1125 int hi = val / 0x10, lo = val % 0x10;
1126 ret[len++] = '%';
1127 ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
1128 ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
1131 } else {
1132 if (uri->server != NULL) {
1133 if (len + 3 >= max) {
1134 temp = realloc2n(ret, &max);
1135 ret = temp;
1137 ret[len++] = '/';
1138 ret[len++] = '/';
1139 if (uri->user != NULL) {
1140 p = uri->user;
1141 while (*p != 0) {
1142 if (len + 3 >= max) {
1143 temp = realloc2n(ret, &max);
1144 ret = temp;
1146 if ((IS_UNRESERVED(*(p))) || ((*(p) == ';')) ||
1147 ((*(p) == ':')) || ((*(p) == '&')) || ((*(p) == '=')) ||
1148 ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ','))) {
1149 ret[len++] = *p++;
1150 } else {
1151 int val = *(unsigned char *)p++;
1152 int hi = val / 0x10, lo = val % 0x10;
1153 ret[len++] = '%';
1154 ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
1155 ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
1158 if (len + 3 >= max) {
1159 temp = realloc2n(ret, &max);
1160 ret = temp;
1162 ret[len++] = '@';
1164 p = uri->server;
1165 while (*p != 0) {
1166 if (len >= max) {
1167 temp = realloc2n(ret, &max);
1168 ret = temp;
1170 ret[len++] = *p++;
1172 if (uri->port > 0) {
1173 if (len + 10 >= max) {
1174 temp = realloc2n(ret, &max);
1175 ret = temp;
1177 len += snprintf(&ret[len], max - len, ":%d", uri->port);
1179 } else if (uri->authority != NULL) {
1180 if (len + 3 >= max) {
1181 temp = realloc2n(ret, &max);
1182 ret = temp;
1184 ret[len++] = '/';
1185 ret[len++] = '/';
1186 p = uri->authority;
1187 while (*p != 0) {
1188 if (len + 3 >= max) {
1189 temp = realloc2n(ret, &max);
1190 ret = temp;
1192 if ((IS_UNRESERVED(*(p))) || ((*(p) == '$')) ||
1193 ((*(p) == ',')) || ((*(p) == ';')) || ((*(p) == ':')) ||
1194 ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) ||
1195 ((*(p) == '+'))) {
1196 ret[len++] = *p++;
1197 } else {
1198 int val = *(unsigned char *)p++;
1199 int hi = val / 0x10, lo = val % 0x10;
1200 ret[len++] = '%';
1201 ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
1202 ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
1205 } else if (uri->scheme != NULL) {
1206 if (len + 3 >= max) {
1207 temp = realloc2n(ret, &max);
1208 ret = temp;
1210 ret[len++] = '/';
1211 ret[len++] = '/';
1213 if (uri->path != NULL) {
1214 p = uri->path;
1216 * the colon in file:///d: should not be escaped or
1217 * Windows accesses fail later.
1219 if ((uri->scheme != NULL) && (p[0] == '/') &&
1220 (((p[1] >= 'a') && (p[1] <= 'z')) ||
1221 ((p[1] >= 'A') && (p[1] <= 'Z'))) &&
1222 (p[2] == ':') && (!strcmp(uri->scheme, "file"))) {
1223 if (len + 3 >= max) {
1224 temp = realloc2n(ret, &max);
1225 ret = temp;
1227 ret[len++] = *p++;
1228 ret[len++] = *p++;
1229 ret[len++] = *p++;
1231 while (*p != 0) {
1232 if (len + 3 >= max) {
1233 temp = realloc2n(ret, &max);
1234 ret = temp;
1236 if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) ||
1237 ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) ||
1238 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||
1239 ((*(p) == ','))) {
1240 ret[len++] = *p++;
1241 } else {
1242 int val = *(unsigned char *)p++;
1243 int hi = val / 0x10, lo = val % 0x10;
1244 ret[len++] = '%';
1245 ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
1246 ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
1250 if (uri->query != NULL) {
1251 if (len + 1 >= max) {
1252 temp = realloc2n(ret, &max);
1253 ret = temp;
1255 ret[len++] = '?';
1256 p = uri->query;
1257 while (*p != 0) {
1258 if (len + 1 >= max) {
1259 temp = realloc2n(ret, &max);
1260 ret = temp;
1262 ret[len++] = *p++;
1266 if (uri->fragment != NULL) {
1267 if (len + 3 >= max) {
1268 temp = realloc2n(ret, &max);
1269 ret = temp;
1271 ret[len++] = '#';
1272 p = uri->fragment;
1273 while (*p != 0) {
1274 if (len + 3 >= max) {
1275 temp = realloc2n(ret, &max);
1276 ret = temp;
1278 if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) {
1279 ret[len++] = *p++;
1280 } else {
1281 int val = *(unsigned char *)p++;
1282 int hi = val / 0x10, lo = val % 0x10;
1283 ret[len++] = '%';
1284 ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0');
1285 ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0');
1289 if (len >= max) {
1290 temp = realloc2n(ret, &max);
1291 ret = temp;
1293 ret[len] = 0;
1294 return ret;
1298 * uri_clean:
1299 * @uri: pointer to an URI
1301 * Make sure the URI struct is free of content
1303 static void uri_clean(URI *uri)
1305 if (uri == NULL) {
1306 return;
1309 g_free(uri->scheme);
1310 uri->scheme = NULL;
1311 g_free(uri->server);
1312 uri->server = NULL;
1313 g_free(uri->user);
1314 uri->user = NULL;
1315 g_free(uri->path);
1316 uri->path = NULL;
1317 g_free(uri->fragment);
1318 uri->fragment = NULL;
1319 g_free(uri->opaque);
1320 uri->opaque = NULL;
1321 g_free(uri->authority);
1322 uri->authority = NULL;
1323 g_free(uri->query);
1324 uri->query = NULL;
1328 * uri_free:
1329 * @uri: pointer to an URI, NULL is ignored
1331 * Free up the URI struct
1333 void uri_free(URI *uri)
1335 uri_clean(uri);
1336 g_free(uri);
1339 /************************************************************************
1341 * Public functions *
1343 ************************************************************************/
1346 * Utility functions to help parse and assemble query strings.
1349 struct QueryParams *query_params_new(int init_alloc)
1351 struct QueryParams *ps;
1353 if (init_alloc <= 0) {
1354 init_alloc = 1;
1357 ps = g_new(QueryParams, 1);
1358 ps->n = 0;
1359 ps->alloc = init_alloc;
1360 ps->p = g_new(QueryParam, ps->alloc);
1362 return ps;
1365 /* Ensure there is space to store at least one more parameter
1366 * at the end of the set.
1368 static int query_params_append(struct QueryParams *ps, const char *name,
1369 const char *value)
1371 if (ps->n >= ps->alloc) {
1372 ps->p = g_renew(QueryParam, ps->p, ps->alloc * 2);
1373 ps->alloc *= 2;
1376 ps->p[ps->n].name = g_strdup(name);
1377 ps->p[ps->n].value = g_strdup(value);
1378 ps->p[ps->n].ignore = 0;
1379 ps->n++;
1381 return 0;
1384 void query_params_free(struct QueryParams *ps)
1386 int i;
1388 for (i = 0; i < ps->n; ++i) {
1389 g_free(ps->p[i].name);
1390 g_free(ps->p[i].value);
1392 g_free(ps->p);
1393 g_free(ps);
1396 struct QueryParams *query_params_parse(const char *query)
1398 struct QueryParams *ps;
1399 const char *end, *eq;
1401 ps = query_params_new(0);
1402 if (!query || query[0] == '\0') {
1403 return ps;
1406 while (*query) {
1407 char *name = NULL, *value = NULL;
1409 /* Find the next separator, or end of the string. */
1410 end = strchr(query, '&');
1411 if (!end) {
1412 end = qemu_strchrnul(query, ';');
1415 /* Find the first '=' character between here and end. */
1416 eq = strchr(query, '=');
1417 if (eq && eq >= end) {
1418 eq = NULL;
1421 /* Empty section (eg. "&&"). */
1422 if (end == query) {
1423 goto next;
1426 /* If there is no '=' character, then we have just "name"
1427 * and consistent with CGI.pm we assume value is "".
1429 else if (!eq) {
1430 name = g_uri_unescape_segment(query, end, NULL);
1431 value = NULL;
1433 /* Or if we have "name=" here (works around annoying
1434 * problem when calling uri_string_unescape with len = 0).
1436 else if (eq + 1 == end) {
1437 name = g_uri_unescape_segment(query, eq, NULL);
1438 value = g_new0(char, 1);
1440 /* If the '=' character is at the beginning then we have
1441 * "=value" and consistent with CGI.pm we _ignore_ this.
1443 else if (query == eq) {
1444 goto next;
1447 /* Otherwise it's "name=value". */
1448 else {
1449 name = g_uri_unescape_segment(query, eq, NULL);
1450 value = g_uri_unescape_segment(eq + 1, end, NULL);
1453 /* Append to the parameter set. */
1454 query_params_append(ps, name, value);
1455 g_free(name);
1456 g_free(value);
1458 next:
1459 query = end;
1460 if (*query) {
1461 query++; /* skip '&' separator */
1465 return ps;