Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1 | /** |
| 2 | * uri.c: set of generic URI related routines |
| 3 | * |
| 4 | * Reference: RFCs 3986, 2732 and 2373 |
| 5 | * |
| 6 | * Copyright (C) 1998-2003 Daniel Veillard. All Rights Reserved. |
| 7 | * |
| 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: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 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. |
| 24 | * |
| 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. |
| 28 | * |
| 29 | * daniel@veillard.com |
| 30 | * |
| 31 | ** |
| 32 | * |
| 33 | * Copyright (C) 2007, 2009-2010 Red Hat, Inc. |
| 34 | * |
| 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. |
| 39 | * |
| 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. |
| 44 | * |
| 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 |
| 48 | * |
| 49 | * Authors: |
| 50 | * Richard W.M. Jones <rjones@redhat.com> |
| 51 | * |
| 52 | */ |
| 53 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 54 | #include "qemu/osdep.h" |
Keno Fischer | 5c99fa3 | 2018-06-29 12:32:10 +0200 | [diff] [blame] | 55 | #include "qemu/cutils.h" |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 56 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 57 | #include "qemu/uri.h" |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 58 | |
| 59 | static void uri_clean(URI *uri); |
| 60 | |
| 61 | /* |
| 62 | * Old rule from 2396 used in legacy handling code |
| 63 | * alpha = lowalpha | upalpha |
| 64 | */ |
| 65 | #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x)) |
| 66 | |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 67 | /* |
| 68 | * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | |
| 69 | * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | |
| 70 | * "u" | "v" | "w" | "x" | "y" | "z" |
| 71 | */ |
| 72 | |
| 73 | #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z')) |
| 74 | |
| 75 | /* |
| 76 | * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | |
| 77 | * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | |
| 78 | * "U" | "V" | "W" | "X" | "Y" | "Z" |
| 79 | */ |
| 80 | #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z')) |
| 81 | |
| 82 | #ifdef IS_DIGIT |
| 83 | #undef IS_DIGIT |
| 84 | #endif |
| 85 | /* |
| 86 | * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
| 87 | */ |
| 88 | #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9')) |
| 89 | |
| 90 | /* |
| 91 | * alphanum = alpha | digit |
| 92 | */ |
| 93 | |
| 94 | #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x)) |
| 95 | |
| 96 | /* |
| 97 | * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" |
| 98 | */ |
| 99 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 100 | #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \ |
| 101 | ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \ |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 102 | ((x) == '(') || ((x) == ')')) |
| 103 | |
| 104 | /* |
| 105 | * unwise = "{" | "}" | "|" | "\" | "^" | "`" |
| 106 | */ |
| 107 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 108 | #define IS_UNWISE(p) \ |
| 109 | (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \ |
| 110 | ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \ |
| 111 | ((*(p) == ']')) || ((*(p) == '`'))) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 112 | /* |
| 113 | * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," | |
| 114 | * "[" | "]" |
| 115 | */ |
| 116 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 117 | #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \ |
| 118 | ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \ |
| 119 | ((x) == '+') || ((x) == '$') || ((x) == ',') || ((x) == '[') || \ |
| 120 | ((x) == ']')) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * unreserved = alphanum | mark |
| 124 | */ |
| 125 | |
| 126 | #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x)) |
| 127 | |
| 128 | /* |
| 129 | * Skip to next pointer char, handle escaped sequences |
| 130 | */ |
| 131 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 132 | #define NEXT(p) ((*p == '%') ? p += 3 : p++) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * Productions from the spec. |
| 136 | * |
| 137 | * authority = server | reg_name |
| 138 | * reg_name = 1*( unreserved | escaped | "$" | "," | |
| 139 | * ";" | ":" | "@" | "&" | "=" | "+" ) |
| 140 | * |
| 141 | * path = [ abs_path | opaque_part ] |
| 142 | */ |
| 143 | |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 144 | /************************************************************************ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 145 | * * |
| 146 | * RFC 3986 parser * |
| 147 | * * |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 148 | ************************************************************************/ |
| 149 | |
| 150 | #define ISA_DIGIT(p) ((*(p) >= '0') && (*(p) <= '9')) |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 151 | #define ISA_ALPHA(p) (((*(p) >= 'a') && (*(p) <= 'z')) || \ |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 152 | ((*(p) >= 'A') && (*(p) <= 'Z'))) |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 153 | #define ISA_HEXDIG(p) \ |
| 154 | (ISA_DIGIT(p) || ((*(p) >= 'a') && (*(p) <= 'f')) || \ |
| 155 | ((*(p) >= 'A') && (*(p) <= 'F'))) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" |
| 159 | * / "*" / "+" / "," / ";" / "=" |
| 160 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 161 | #define ISA_SUB_DELIM(p) \ |
| 162 | (((*(p) == '!')) || ((*(p) == '$')) || ((*(p) == '&')) || \ |
| 163 | ((*(p) == '(')) || ((*(p) == ')')) || ((*(p) == '*')) || \ |
| 164 | ((*(p) == '+')) || ((*(p) == ',')) || ((*(p) == ';')) || \ |
| 165 | ((*(p) == '=')) || ((*(p) == '\''))) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" |
| 169 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 170 | #define ISA_GEN_DELIM(p) \ |
| 171 | (((*(p) == ':')) || ((*(p) == '/')) || ((*(p) == '?')) || \ |
| 172 | ((*(p) == '#')) || ((*(p) == '[')) || ((*(p) == ']')) || \ |
| 173 | ((*(p) == '@'))) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * reserved = gen-delims / sub-delims |
| 177 | */ |
| 178 | #define ISA_RESERVED(p) (ISA_GEN_DELIM(p) || (ISA_SUB_DELIM(p))) |
| 179 | |
| 180 | /* |
| 181 | * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
| 182 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 183 | #define ISA_UNRESERVED(p) \ |
| 184 | ((ISA_ALPHA(p)) || (ISA_DIGIT(p)) || ((*(p) == '-')) || \ |
| 185 | ((*(p) == '.')) || ((*(p) == '_')) || ((*(p) == '~'))) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * pct-encoded = "%" HEXDIG HEXDIG |
| 189 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 190 | #define ISA_PCT_ENCODED(p) \ |
| 191 | ((*(p) == '%') && (ISA_HEXDIG(p + 1)) && (ISA_HEXDIG(p + 2))) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * pchar = unreserved / pct-encoded / sub-delims / ":" / "@" |
| 195 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 196 | #define ISA_PCHAR(p) \ |
| 197 | (ISA_UNRESERVED(p) || ISA_PCT_ENCODED(p) || ISA_SUB_DELIM(p) || \ |
| 198 | ((*(p) == ':')) || ((*(p) == '@'))) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * rfc3986_parse_scheme: |
| 202 | * @uri: pointer to an URI structure |
| 203 | * @str: pointer to the string to analyze |
| 204 | * |
| 205 | * Parse an URI scheme |
| 206 | * |
| 207 | * ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) |
| 208 | * |
| 209 | * Returns 0 or the error code |
| 210 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 211 | static int rfc3986_parse_scheme(URI *uri, const char **str) |
| 212 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 213 | const char *cur; |
| 214 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 215 | if (str == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 216 | return -1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 217 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 218 | |
| 219 | cur = *str; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 220 | if (!ISA_ALPHA(cur)) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 221 | return 2; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 222 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 223 | cur++; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 224 | while (ISA_ALPHA(cur) || ISA_DIGIT(cur) || (*cur == '+') || (*cur == '-') || |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 225 | (*cur == '.')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 226 | cur++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 227 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 228 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 229 | g_free(uri->scheme); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 230 | uri->scheme = g_strndup(*str, cur - *str); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 231 | } |
| 232 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 233 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * rfc3986_parse_fragment: |
| 238 | * @uri: pointer to an URI structure |
| 239 | * @str: pointer to the string to analyze |
| 240 | * |
| 241 | * Parse the query part of an URI |
| 242 | * |
| 243 | * fragment = *( pchar / "/" / "?" ) |
| 244 | * NOTE: the strict syntax as defined by 3986 does not allow '[' and ']' |
| 245 | * in the fragment identifier but this is used very broadly for |
| 246 | * xpointer scheme selection, so we are allowing it here to not break |
| 247 | * for example all the DocBook processing chains. |
| 248 | * |
| 249 | * Returns 0 or the error code |
| 250 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 251 | static int rfc3986_parse_fragment(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 252 | { |
| 253 | const char *cur; |
| 254 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 255 | if (str == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 256 | return -1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 257 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 258 | |
| 259 | cur = *str; |
| 260 | |
| 261 | while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') || |
| 262 | (*cur == '[') || (*cur == ']') || |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 263 | ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 264 | NEXT(cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 265 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 266 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 267 | g_free(uri->fragment); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 268 | if (uri->cleanup & 2) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 269 | uri->fragment = g_strndup(*str, cur - *str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 270 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 271 | uri->fragment = uri_string_unescape(*str, cur - *str, NULL); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 272 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 273 | } |
| 274 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 275 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
| 279 | * rfc3986_parse_query: |
| 280 | * @uri: pointer to an URI structure |
| 281 | * @str: pointer to the string to analyze |
| 282 | * |
| 283 | * Parse the query part of an URI |
| 284 | * |
| 285 | * query = *uric |
| 286 | * |
| 287 | * Returns 0 or the error code |
| 288 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 289 | static int rfc3986_parse_query(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 290 | { |
| 291 | const char *cur; |
| 292 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 293 | if (str == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 294 | return -1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 295 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 296 | |
| 297 | cur = *str; |
| 298 | |
| 299 | while ((ISA_PCHAR(cur)) || (*cur == '/') || (*cur == '?') || |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 300 | ((uri != NULL) && (uri->cleanup & 1) && (IS_UNWISE(cur)))) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 301 | NEXT(cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 302 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 303 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 304 | g_free(uri->query); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 305 | uri->query = g_strndup(*str, cur - *str); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 306 | } |
| 307 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 308 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
| 312 | * rfc3986_parse_port: |
| 313 | * @uri: pointer to an URI structure |
| 314 | * @str: the string to analyze |
| 315 | * |
| 316 | * Parse a port part and fills in the appropriate fields |
| 317 | * of the @uri structure |
| 318 | * |
| 319 | * port = *DIGIT |
| 320 | * |
| 321 | * Returns 0 or the error code |
| 322 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 323 | static int rfc3986_parse_port(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 324 | { |
| 325 | const char *cur = *str; |
Max Reitz | 2b21233 | 2015-02-25 13:08:14 -0500 | [diff] [blame] | 326 | int port = 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 327 | |
| 328 | if (ISA_DIGIT(cur)) { |
Max Reitz | 2b21233 | 2015-02-25 13:08:14 -0500 | [diff] [blame] | 329 | while (ISA_DIGIT(cur)) { |
| 330 | port = port * 10 + (*cur - '0'); |
| 331 | if (port > 65535) { |
| 332 | return 1; |
| 333 | } |
| 334 | cur++; |
| 335 | } |
| 336 | if (uri) { |
| 337 | uri->port = port; |
| 338 | } |
| 339 | *str = cur; |
| 340 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 341 | } |
Max Reitz | 2b21233 | 2015-02-25 13:08:14 -0500 | [diff] [blame] | 342 | return 1; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
| 346 | * rfc3986_parse_user_info: |
| 347 | * @uri: pointer to an URI structure |
| 348 | * @str: the string to analyze |
| 349 | * |
Stefan Weil | 736a83f | 2016-11-19 20:22:07 +0100 | [diff] [blame] | 350 | * Parse a user information part and fill in the appropriate fields |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 351 | * of the @uri structure |
| 352 | * |
| 353 | * userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) |
| 354 | * |
| 355 | * Returns 0 or the error code |
| 356 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 357 | static int rfc3986_parse_user_info(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 358 | { |
| 359 | const char *cur; |
| 360 | |
| 361 | cur = *str; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 362 | while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur) || |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 363 | (*cur == ':')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 364 | NEXT(cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 365 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 366 | if (*cur == '@') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 367 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 368 | g_free(uri->user); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 369 | if (uri->cleanup & 2) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 370 | uri->user = g_strndup(*str, cur - *str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 371 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 372 | uri->user = uri_string_unescape(*str, cur - *str, NULL); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 373 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 374 | } |
| 375 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 376 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 377 | } |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 378 | return 1; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
| 382 | * rfc3986_parse_dec_octet: |
| 383 | * @str: the string to analyze |
| 384 | * |
| 385 | * dec-octet = DIGIT ; 0-9 |
| 386 | * / %x31-39 DIGIT ; 10-99 |
| 387 | * / "1" 2DIGIT ; 100-199 |
| 388 | * / "2" %x30-34 DIGIT ; 200-249 |
| 389 | * / "25" %x30-35 ; 250-255 |
| 390 | * |
| 391 | * Skip a dec-octet. |
| 392 | * |
| 393 | * Returns 0 if found and skipped, 1 otherwise |
| 394 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 395 | static int rfc3986_parse_dec_octet(const char **str) |
| 396 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 397 | const char *cur = *str; |
| 398 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 399 | if (!(ISA_DIGIT(cur))) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 400 | return 1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 401 | } |
| 402 | if (!ISA_DIGIT(cur + 1)) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 403 | cur++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 404 | } else if ((*cur != '0') && (ISA_DIGIT(cur + 1)) && (!ISA_DIGIT(cur + 2))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 405 | cur += 2; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 406 | } else if ((*cur == '1') && (ISA_DIGIT(cur + 1)) && (ISA_DIGIT(cur + 2))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 407 | cur += 3; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 408 | } else if ((*cur == '2') && (*(cur + 1) >= '0') && (*(cur + 1) <= '4') && |
| 409 | (ISA_DIGIT(cur + 2))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 410 | cur += 3; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 411 | } else if ((*cur == '2') && (*(cur + 1) == '5') && (*(cur + 2) >= '0') && |
| 412 | (*(cur + 1) <= '5')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 413 | cur += 3; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 414 | } else { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 415 | return 1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 416 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 417 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 418 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 419 | } |
| 420 | /** |
| 421 | * rfc3986_parse_host: |
| 422 | * @uri: pointer to an URI structure |
| 423 | * @str: the string to analyze |
| 424 | * |
| 425 | * Parse an host part and fills in the appropriate fields |
| 426 | * of the @uri structure |
| 427 | * |
| 428 | * host = IP-literal / IPv4address / reg-name |
| 429 | * IP-literal = "[" ( IPv6address / IPvFuture ) "]" |
| 430 | * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet |
| 431 | * reg-name = *( unreserved / pct-encoded / sub-delims ) |
| 432 | * |
| 433 | * Returns 0 or the error code |
| 434 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 435 | static int rfc3986_parse_host(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 436 | { |
| 437 | const char *cur = *str; |
| 438 | const char *host; |
| 439 | |
| 440 | host = cur; |
| 441 | /* |
Stefan Weil | a93cf9d | 2012-11-02 08:29:53 +0100 | [diff] [blame] | 442 | * IPv6 and future addressing scheme are enclosed between brackets |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 443 | */ |
| 444 | if (*cur == '[') { |
| 445 | cur++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 446 | while ((*cur != ']') && (*cur != 0)) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 447 | cur++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 448 | } |
| 449 | if (*cur != ']') { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 450 | return 1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 451 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 452 | cur++; |
| 453 | goto found; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 454 | } |
| 455 | /* |
| 456 | * try to parse an IPv4 |
| 457 | */ |
| 458 | if (ISA_DIGIT(cur)) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 459 | if (rfc3986_parse_dec_octet(&cur) != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 460 | goto not_ipv4; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 461 | } |
| 462 | if (*cur != '.') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 463 | goto not_ipv4; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 464 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 465 | cur++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 466 | if (rfc3986_parse_dec_octet(&cur) != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 467 | goto not_ipv4; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 468 | } |
| 469 | if (*cur != '.') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 470 | goto not_ipv4; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 471 | } |
| 472 | if (rfc3986_parse_dec_octet(&cur) != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 473 | goto not_ipv4; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 474 | } |
| 475 | if (*cur != '.') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 476 | goto not_ipv4; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 477 | } |
| 478 | if (rfc3986_parse_dec_octet(&cur) != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 479 | goto not_ipv4; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 480 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 481 | goto found; |
| 482 | not_ipv4: |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 483 | cur = *str; |
| 484 | } |
| 485 | /* |
| 486 | * then this should be a hostname which can be empty |
| 487 | */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 488 | while (ISA_UNRESERVED(cur) || ISA_PCT_ENCODED(cur) || ISA_SUB_DELIM(cur)) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 489 | NEXT(cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 490 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 491 | found: |
| 492 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 493 | g_free(uri->authority); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 494 | uri->authority = NULL; |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 495 | g_free(uri->server); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 496 | if (cur != host) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 497 | if (uri->cleanup & 2) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 498 | uri->server = g_strndup(host, cur - host); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 499 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 500 | uri->server = uri_string_unescape(host, cur - host, NULL); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 501 | } |
| 502 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 503 | uri->server = NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 504 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 505 | } |
| 506 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 507 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
| 511 | * rfc3986_parse_authority: |
| 512 | * @uri: pointer to an URI structure |
| 513 | * @str: the string to analyze |
| 514 | * |
| 515 | * Parse an authority part and fills in the appropriate fields |
| 516 | * of the @uri structure |
| 517 | * |
| 518 | * authority = [ userinfo "@" ] host [ ":" port ] |
| 519 | * |
| 520 | * Returns 0 or the error code |
| 521 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 522 | static int rfc3986_parse_authority(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 523 | { |
| 524 | const char *cur; |
| 525 | int ret; |
| 526 | |
| 527 | cur = *str; |
| 528 | /* |
Stefan Weil | 736a83f | 2016-11-19 20:22:07 +0100 | [diff] [blame] | 529 | * try to parse a userinfo and check for the trailing @ |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 530 | */ |
| 531 | ret = rfc3986_parse_user_info(uri, &cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 532 | if ((ret != 0) || (*cur != '@')) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 533 | cur = *str; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 534 | } else { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 535 | cur++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 536 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 537 | ret = rfc3986_parse_host(uri, &cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 538 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 539 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 540 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 541 | if (*cur == ':') { |
| 542 | cur++; |
| 543 | ret = rfc3986_parse_port(uri, &cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 544 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 545 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 546 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 547 | } |
| 548 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 549 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /** |
| 553 | * rfc3986_parse_segment: |
| 554 | * @str: the string to analyze |
| 555 | * @forbid: an optional forbidden character |
| 556 | * @empty: allow an empty segment |
| 557 | * |
| 558 | * Parse a segment and fills in the appropriate fields |
| 559 | * of the @uri structure |
| 560 | * |
| 561 | * segment = *pchar |
| 562 | * segment-nz = 1*pchar |
| 563 | * segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" ) |
| 564 | * ; non-zero-length segment without any colon ":" |
| 565 | * |
| 566 | * Returns 0 or the error code |
| 567 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 568 | static int rfc3986_parse_segment(const char **str, char forbid, int empty) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 569 | { |
| 570 | const char *cur; |
| 571 | |
| 572 | cur = *str; |
| 573 | if (!ISA_PCHAR(cur)) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 574 | if (empty) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 575 | return 0; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 576 | } |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 577 | return 1; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 578 | } |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 579 | while (ISA_PCHAR(cur) && (*cur != forbid)) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 580 | NEXT(cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 581 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 582 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 583 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /** |
| 587 | * rfc3986_parse_path_ab_empty: |
| 588 | * @uri: pointer to an URI structure |
| 589 | * @str: the string to analyze |
| 590 | * |
| 591 | * Parse an path absolute or empty and fills in the appropriate fields |
| 592 | * of the @uri structure |
| 593 | * |
| 594 | * path-abempty = *( "/" segment ) |
| 595 | * |
| 596 | * Returns 0 or the error code |
| 597 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 598 | static int rfc3986_parse_path_ab_empty(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 599 | { |
| 600 | const char *cur; |
| 601 | int ret; |
| 602 | |
| 603 | cur = *str; |
| 604 | |
| 605 | while (*cur == '/') { |
| 606 | cur++; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 607 | ret = rfc3986_parse_segment(&cur, 0, 1); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 608 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 609 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 610 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 611 | } |
| 612 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 613 | g_free(uri->path); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 614 | if (*str != cur) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 615 | if (uri->cleanup & 2) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 616 | uri->path = g_strndup(*str, cur - *str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 617 | } else { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 618 | uri->path = uri_string_unescape(*str, cur - *str, NULL); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 619 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 620 | } else { |
| 621 | uri->path = NULL; |
| 622 | } |
| 623 | } |
| 624 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 625 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /** |
| 629 | * rfc3986_parse_path_absolute: |
| 630 | * @uri: pointer to an URI structure |
| 631 | * @str: the string to analyze |
| 632 | * |
| 633 | * Parse an path absolute and fills in the appropriate fields |
| 634 | * of the @uri structure |
| 635 | * |
| 636 | * path-absolute = "/" [ segment-nz *( "/" segment ) ] |
| 637 | * |
| 638 | * Returns 0 or the error code |
| 639 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 640 | static int rfc3986_parse_path_absolute(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 641 | { |
| 642 | const char *cur; |
| 643 | int ret; |
| 644 | |
| 645 | cur = *str; |
| 646 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 647 | if (*cur != '/') { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 648 | return 1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 649 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 650 | cur++; |
| 651 | ret = rfc3986_parse_segment(&cur, 0, 0); |
| 652 | if (ret == 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 653 | while (*cur == '/') { |
| 654 | cur++; |
| 655 | ret = rfc3986_parse_segment(&cur, 0, 1); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 656 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 657 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 658 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 659 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 660 | } |
| 661 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 662 | g_free(uri->path); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 663 | if (cur != *str) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 664 | if (uri->cleanup & 2) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 665 | uri->path = g_strndup(*str, cur - *str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 666 | } else { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 667 | uri->path = uri_string_unescape(*str, cur - *str, NULL); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 668 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 669 | } else { |
| 670 | uri->path = NULL; |
| 671 | } |
| 672 | } |
| 673 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 674 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /** |
| 678 | * rfc3986_parse_path_rootless: |
| 679 | * @uri: pointer to an URI structure |
| 680 | * @str: the string to analyze |
| 681 | * |
| 682 | * Parse an path without root and fills in the appropriate fields |
| 683 | * of the @uri structure |
| 684 | * |
| 685 | * path-rootless = segment-nz *( "/" segment ) |
| 686 | * |
| 687 | * Returns 0 or the error code |
| 688 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 689 | static int rfc3986_parse_path_rootless(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 690 | { |
| 691 | const char *cur; |
| 692 | int ret; |
| 693 | |
| 694 | cur = *str; |
| 695 | |
| 696 | ret = rfc3986_parse_segment(&cur, 0, 0); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 697 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 698 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 699 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 700 | while (*cur == '/') { |
| 701 | cur++; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 702 | ret = rfc3986_parse_segment(&cur, 0, 1); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 703 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 704 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 705 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 706 | } |
| 707 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 708 | g_free(uri->path); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 709 | if (cur != *str) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 710 | if (uri->cleanup & 2) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 711 | uri->path = g_strndup(*str, cur - *str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 712 | } else { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 713 | uri->path = uri_string_unescape(*str, cur - *str, NULL); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 714 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 715 | } else { |
| 716 | uri->path = NULL; |
| 717 | } |
| 718 | } |
| 719 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 720 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /** |
| 724 | * rfc3986_parse_path_no_scheme: |
| 725 | * @uri: pointer to an URI structure |
| 726 | * @str: the string to analyze |
| 727 | * |
| 728 | * Parse an path which is not a scheme and fills in the appropriate fields |
| 729 | * of the @uri structure |
| 730 | * |
| 731 | * path-noscheme = segment-nz-nc *( "/" segment ) |
| 732 | * |
| 733 | * Returns 0 or the error code |
| 734 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 735 | static int rfc3986_parse_path_no_scheme(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 736 | { |
| 737 | const char *cur; |
| 738 | int ret; |
| 739 | |
| 740 | cur = *str; |
| 741 | |
| 742 | ret = rfc3986_parse_segment(&cur, ':', 0); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 743 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 744 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 745 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 746 | while (*cur == '/') { |
| 747 | cur++; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 748 | ret = rfc3986_parse_segment(&cur, 0, 1); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 749 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 750 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 751 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 752 | } |
| 753 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 754 | g_free(uri->path); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 755 | if (cur != *str) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 756 | if (uri->cleanup & 2) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 757 | uri->path = g_strndup(*str, cur - *str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 758 | } else { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 759 | uri->path = uri_string_unescape(*str, cur - *str, NULL); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 760 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 761 | } else { |
| 762 | uri->path = NULL; |
| 763 | } |
| 764 | } |
| 765 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 766 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /** |
| 770 | * rfc3986_parse_hier_part: |
| 771 | * @uri: pointer to an URI structure |
| 772 | * @str: the string to analyze |
| 773 | * |
| 774 | * Parse an hierarchical part and fills in the appropriate fields |
| 775 | * of the @uri structure |
| 776 | * |
| 777 | * hier-part = "//" authority path-abempty |
| 778 | * / path-absolute |
| 779 | * / path-rootless |
| 780 | * / path-empty |
| 781 | * |
| 782 | * Returns 0 or the error code |
| 783 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 784 | static int rfc3986_parse_hier_part(URI *uri, const char **str) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 785 | { |
| 786 | const char *cur; |
| 787 | int ret; |
| 788 | |
| 789 | cur = *str; |
| 790 | |
| 791 | if ((*cur == '/') && (*(cur + 1) == '/')) { |
| 792 | cur += 2; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 793 | ret = rfc3986_parse_authority(uri, &cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 794 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 795 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 796 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 797 | ret = rfc3986_parse_path_ab_empty(uri, &cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 798 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 799 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 800 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 801 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 802 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 803 | } else if (*cur == '/') { |
| 804 | ret = rfc3986_parse_path_absolute(uri, &cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 805 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 806 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 807 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 808 | } else if (ISA_PCHAR(cur)) { |
| 809 | ret = rfc3986_parse_path_rootless(uri, &cur); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 810 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 811 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 812 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 813 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 814 | /* path-empty is effectively empty */ |
| 815 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 816 | g_free(uri->path); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 817 | uri->path = NULL; |
| 818 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 819 | } |
| 820 | *str = cur; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 821 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /** |
| 825 | * rfc3986_parse_relative_ref: |
| 826 | * @uri: pointer to an URI structure |
| 827 | * @str: the string to analyze |
| 828 | * |
| 829 | * Parse an URI string and fills in the appropriate fields |
| 830 | * of the @uri structure |
| 831 | * |
| 832 | * relative-ref = relative-part [ "?" query ] [ "#" fragment ] |
| 833 | * relative-part = "//" authority path-abempty |
| 834 | * / path-absolute |
| 835 | * / path-noscheme |
| 836 | * / path-empty |
| 837 | * |
| 838 | * Returns 0 or the error code |
| 839 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 840 | static int rfc3986_parse_relative_ref(URI *uri, const char *str) |
| 841 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 842 | int ret; |
| 843 | |
| 844 | if ((*str == '/') && (*(str + 1) == '/')) { |
| 845 | str += 2; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 846 | ret = rfc3986_parse_authority(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 847 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 848 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 849 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 850 | ret = rfc3986_parse_path_ab_empty(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 851 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 852 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 853 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 854 | } else if (*str == '/') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 855 | ret = rfc3986_parse_path_absolute(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 856 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 857 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 858 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 859 | } else if (ISA_PCHAR(str)) { |
| 860 | ret = rfc3986_parse_path_no_scheme(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 861 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 862 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 863 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 864 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 865 | /* path-empty is effectively empty */ |
| 866 | if (uri != NULL) { |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 867 | g_free(uri->path); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 868 | uri->path = NULL; |
| 869 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | if (*str == '?') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 873 | str++; |
| 874 | ret = rfc3986_parse_query(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 875 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 876 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 877 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 878 | } |
| 879 | if (*str == '#') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 880 | str++; |
| 881 | ret = rfc3986_parse_fragment(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 882 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 883 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 884 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 885 | } |
| 886 | if (*str != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 887 | uri_clean(uri); |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 888 | return 1; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 889 | } |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 890 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 891 | } |
| 892 | |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 893 | /** |
| 894 | * rfc3986_parse: |
| 895 | * @uri: pointer to an URI structure |
| 896 | * @str: the string to analyze |
| 897 | * |
| 898 | * Parse an URI string and fills in the appropriate fields |
| 899 | * of the @uri structure |
| 900 | * |
| 901 | * scheme ":" hier-part [ "?" query ] [ "#" fragment ] |
| 902 | * |
| 903 | * Returns 0 or the error code |
| 904 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 905 | static int rfc3986_parse(URI *uri, const char *str) |
| 906 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 907 | int ret; |
| 908 | |
| 909 | ret = rfc3986_parse_scheme(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 910 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 911 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 912 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 913 | if (*str != ':') { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 914 | return 1; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 915 | } |
| 916 | str++; |
| 917 | ret = rfc3986_parse_hier_part(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 918 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 919 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 920 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 921 | if (*str == '?') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 922 | str++; |
| 923 | ret = rfc3986_parse_query(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 924 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 925 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 926 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 927 | } |
| 928 | if (*str == '#') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 929 | str++; |
| 930 | ret = rfc3986_parse_fragment(uri, &str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 931 | if (ret != 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 932 | return ret; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 933 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 934 | } |
| 935 | if (*str != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 936 | uri_clean(uri); |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 937 | return 1; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 938 | } |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 939 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /** |
| 943 | * rfc3986_parse_uri_reference: |
| 944 | * @uri: pointer to an URI structure |
| 945 | * @str: the string to analyze |
| 946 | * |
| 947 | * Parse an URI reference string and fills in the appropriate fields |
| 948 | * of the @uri structure |
| 949 | * |
| 950 | * URI-reference = URI / relative-ref |
| 951 | * |
| 952 | * Returns 0 or the error code |
| 953 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 954 | static int rfc3986_parse_uri_reference(URI *uri, const char *str) |
| 955 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 956 | int ret; |
| 957 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 958 | if (str == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 959 | return -1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 960 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 961 | uri_clean(uri); |
| 962 | |
| 963 | /* |
| 964 | * Try first to parse absolute refs, then fallback to relative if |
| 965 | * it fails. |
| 966 | */ |
| 967 | ret = rfc3986_parse(uri, str); |
| 968 | if (ret != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 969 | uri_clean(uri); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 970 | ret = rfc3986_parse_relative_ref(uri, str); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 971 | if (ret != 0) { |
| 972 | uri_clean(uri); |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 973 | return ret; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 974 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 975 | } |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 976 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /** |
| 980 | * uri_parse: |
| 981 | * @str: the URI string to analyze |
| 982 | * |
| 983 | * Parse an URI based on RFC 3986 |
| 984 | * |
| 985 | * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ] |
| 986 | * |
| 987 | * Returns a newly built URI or NULL in case of error |
| 988 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 989 | URI *uri_parse(const char *str) |
| 990 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 991 | URI *uri; |
| 992 | int ret; |
| 993 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 994 | if (str == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 995 | return NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 996 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 997 | uri = uri_new(); |
Markus Armbruster | c89c6e8 | 2015-01-27 17:13:50 +0100 | [diff] [blame] | 998 | ret = rfc3986_parse_uri_reference(uri, str); |
| 999 | if (ret) { |
| 1000 | uri_free(uri); |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1001 | return NULL; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1002 | } |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1003 | return uri; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * uri_parse_into: |
| 1008 | * @uri: pointer to an URI structure |
| 1009 | * @str: the string to analyze |
| 1010 | * |
| 1011 | * Parse an URI reference string based on RFC 3986 and fills in the |
| 1012 | * appropriate fields of the @uri structure |
| 1013 | * |
| 1014 | * URI-reference = URI / relative-ref |
| 1015 | * |
| 1016 | * Returns 0 or the error code |
| 1017 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1018 | int uri_parse_into(URI *uri, const char *str) |
| 1019 | { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1020 | return rfc3986_parse_uri_reference(uri, str); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * uri_parse_raw: |
| 1025 | * @str: the URI string to analyze |
| 1026 | * @raw: if 1 unescaping of URI pieces are disabled |
| 1027 | * |
| 1028 | * Parse an URI but allows to keep intact the original fragments. |
| 1029 | * |
| 1030 | * URI-reference = URI / relative-ref |
| 1031 | * |
| 1032 | * Returns a newly built URI or NULL in case of error |
| 1033 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1034 | URI *uri_parse_raw(const char *str, int raw) |
| 1035 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1036 | URI *uri; |
| 1037 | int ret; |
| 1038 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1039 | if (str == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1040 | return NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1041 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1042 | uri = uri_new(); |
Markus Armbruster | c89c6e8 | 2015-01-27 17:13:50 +0100 | [diff] [blame] | 1043 | if (raw) { |
| 1044 | uri->cleanup |= 2; |
| 1045 | } |
| 1046 | ret = uri_parse_into(uri, str); |
| 1047 | if (ret) { |
| 1048 | uri_free(uri); |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1049 | return NULL; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1050 | } |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1051 | return uri; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /************************************************************************ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1055 | * * |
| 1056 | * Generic URI structure functions * |
| 1057 | * * |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1058 | ************************************************************************/ |
| 1059 | |
| 1060 | /** |
| 1061 | * uri_new: |
| 1062 | * |
| 1063 | * Simply creates an empty URI |
| 1064 | * |
| 1065 | * Returns the new structure or NULL in case of error |
| 1066 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1067 | URI *uri_new(void) |
| 1068 | { |
Laurent Vivier | 4a4ff4c | 2018-03-23 15:32:02 +0100 | [diff] [blame] | 1069 | return g_new0(URI, 1); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * realloc2n: |
| 1074 | * |
| 1075 | * Function to handle properly a reallocation when saving an URI |
| 1076 | * Also imposes some limit on the length of an URI string output |
| 1077 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1078 | static char *realloc2n(char *ret, int *max) |
| 1079 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1080 | char *temp; |
| 1081 | int tmp; |
| 1082 | |
| 1083 | tmp = *max * 2; |
| 1084 | temp = g_realloc(ret, (tmp + 1)); |
| 1085 | *max = tmp; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1086 | return temp; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /** |
| 1090 | * uri_to_string: |
| 1091 | * @uri: pointer to an URI |
| 1092 | * |
| 1093 | * Save the URI as an escaped string |
| 1094 | * |
| 1095 | * Returns a new string (to be deallocated by caller) |
| 1096 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1097 | char *uri_to_string(URI *uri) |
| 1098 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1099 | char *ret = NULL; |
| 1100 | char *temp; |
| 1101 | const char *p; |
| 1102 | int len; |
| 1103 | int max; |
| 1104 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1105 | if (uri == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1106 | return NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1107 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1108 | |
| 1109 | max = 80; |
| 1110 | ret = g_malloc(max + 1); |
| 1111 | len = 0; |
| 1112 | |
| 1113 | if (uri->scheme != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1114 | p = uri->scheme; |
| 1115 | while (*p != 0) { |
| 1116 | if (len >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1117 | temp = realloc2n(ret, &max); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1118 | ret = temp; |
| 1119 | } |
| 1120 | ret[len++] = *p++; |
| 1121 | } |
| 1122 | if (len >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1123 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1124 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1125 | } |
| 1126 | ret[len++] = ':'; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1127 | } |
| 1128 | if (uri->opaque != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1129 | p = uri->opaque; |
| 1130 | while (*p != 0) { |
| 1131 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1132 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1133 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1134 | } |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1135 | if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1136 | ret[len++] = *p++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1137 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1138 | int val = *(unsigned char *)p++; |
| 1139 | int hi = val / 0x10, lo = val % 0x10; |
| 1140 | ret[len++] = '%'; |
| 1141 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); |
| 1142 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); |
| 1143 | } |
| 1144 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1145 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1146 | if (uri->server != NULL) { |
| 1147 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1148 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1149 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1150 | } |
| 1151 | ret[len++] = '/'; |
| 1152 | ret[len++] = '/'; |
| 1153 | if (uri->user != NULL) { |
| 1154 | p = uri->user; |
| 1155 | while (*p != 0) { |
| 1156 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1157 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1158 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1159 | } |
| 1160 | if ((IS_UNRESERVED(*(p))) || ((*(p) == ';')) || |
| 1161 | ((*(p) == ':')) || ((*(p) == '&')) || ((*(p) == '=')) || |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1162 | ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ','))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1163 | ret[len++] = *p++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1164 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1165 | int val = *(unsigned char *)p++; |
| 1166 | int hi = val / 0x10, lo = val % 0x10; |
| 1167 | ret[len++] = '%'; |
| 1168 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); |
| 1169 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); |
| 1170 | } |
| 1171 | } |
| 1172 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1173 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1174 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1175 | } |
| 1176 | ret[len++] = '@'; |
| 1177 | } |
| 1178 | p = uri->server; |
| 1179 | while (*p != 0) { |
| 1180 | if (len >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1181 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1182 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1183 | } |
| 1184 | ret[len++] = *p++; |
| 1185 | } |
| 1186 | if (uri->port > 0) { |
| 1187 | if (len + 10 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1188 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1189 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1190 | } |
| 1191 | len += snprintf(&ret[len], max - len, ":%d", uri->port); |
| 1192 | } |
| 1193 | } else if (uri->authority != NULL) { |
| 1194 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1195 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1196 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1197 | } |
| 1198 | ret[len++] = '/'; |
| 1199 | ret[len++] = '/'; |
| 1200 | p = uri->authority; |
| 1201 | while (*p != 0) { |
| 1202 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1203 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1204 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1205 | } |
| 1206 | if ((IS_UNRESERVED(*(p))) || ((*(p) == '$')) || |
| 1207 | ((*(p) == ',')) || ((*(p) == ';')) || ((*(p) == ':')) || |
| 1208 | ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) || |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1209 | ((*(p) == '+'))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1210 | ret[len++] = *p++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1211 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1212 | int val = *(unsigned char *)p++; |
| 1213 | int hi = val / 0x10, lo = val % 0x10; |
| 1214 | ret[len++] = '%'; |
| 1215 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); |
| 1216 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); |
| 1217 | } |
| 1218 | } |
| 1219 | } else if (uri->scheme != NULL) { |
| 1220 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1221 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1222 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1223 | } |
| 1224 | ret[len++] = '/'; |
| 1225 | ret[len++] = '/'; |
| 1226 | } |
| 1227 | if (uri->path != NULL) { |
| 1228 | p = uri->path; |
| 1229 | /* |
| 1230 | * the colon in file:///d: should not be escaped or |
| 1231 | * Windows accesses fail later. |
| 1232 | */ |
| 1233 | if ((uri->scheme != NULL) && (p[0] == '/') && |
| 1234 | (((p[1] >= 'a') && (p[1] <= 'z')) || |
| 1235 | ((p[1] >= 'A') && (p[1] <= 'Z'))) && |
| 1236 | (p[2] == ':') && (!strcmp(uri->scheme, "file"))) { |
| 1237 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1238 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1239 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1240 | } |
| 1241 | ret[len++] = *p++; |
| 1242 | ret[len++] = *p++; |
| 1243 | ret[len++] = *p++; |
| 1244 | } |
| 1245 | while (*p != 0) { |
| 1246 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1247 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1248 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1249 | } |
| 1250 | if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) || |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1251 | ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1252 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1253 | ((*(p) == ','))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1254 | ret[len++] = *p++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1255 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1256 | int val = *(unsigned char *)p++; |
| 1257 | int hi = val / 0x10, lo = val % 0x10; |
| 1258 | ret[len++] = '%'; |
| 1259 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); |
| 1260 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | if (uri->query != NULL) { |
| 1265 | if (len + 1 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1266 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1267 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1268 | } |
| 1269 | ret[len++] = '?'; |
| 1270 | p = uri->query; |
| 1271 | while (*p != 0) { |
| 1272 | if (len + 1 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1273 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1274 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1275 | } |
| 1276 | ret[len++] = *p++; |
| 1277 | } |
| 1278 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1279 | } |
| 1280 | if (uri->fragment != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1281 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1282 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1283 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1284 | } |
| 1285 | ret[len++] = '#'; |
| 1286 | p = uri->fragment; |
| 1287 | while (*p != 0) { |
| 1288 | if (len + 3 >= max) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1289 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1290 | ret = temp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1291 | } |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1292 | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1293 | ret[len++] = *p++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1294 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1295 | int val = *(unsigned char *)p++; |
| 1296 | int hi = val / 0x10, lo = val % 0x10; |
| 1297 | ret[len++] = '%'; |
| 1298 | ret[len++] = hi + (hi > 9 ? 'A' - 10 : '0'); |
| 1299 | ret[len++] = lo + (lo > 9 ? 'A' - 10 : '0'); |
| 1300 | } |
| 1301 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1302 | } |
| 1303 | if (len >= max) { |
| 1304 | temp = realloc2n(ret, &max); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1305 | ret = temp; |
| 1306 | } |
| 1307 | ret[len] = 0; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1308 | return ret; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /** |
| 1312 | * uri_clean: |
| 1313 | * @uri: pointer to an URI |
| 1314 | * |
| 1315 | * Make sure the URI struct is free of content |
| 1316 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1317 | static void uri_clean(URI *uri) |
| 1318 | { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1319 | if (uri == NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1320 | return; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1321 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1322 | |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 1323 | g_free(uri->scheme); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1324 | uri->scheme = NULL; |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 1325 | g_free(uri->server); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1326 | uri->server = NULL; |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 1327 | g_free(uri->user); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1328 | uri->user = NULL; |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 1329 | g_free(uri->path); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1330 | uri->path = NULL; |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 1331 | g_free(uri->fragment); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1332 | uri->fragment = NULL; |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 1333 | g_free(uri->opaque); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1334 | uri->opaque = NULL; |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 1335 | g_free(uri->authority); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1336 | uri->authority = NULL; |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 1337 | g_free(uri->query); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1338 | uri->query = NULL; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * uri_free: |
Heinrich Schuchardt | c2615bd | 2021-06-29 08:36:02 +0200 | [diff] [blame] | 1343 | * @uri: pointer to an URI, NULL is ignored |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1344 | * |
| 1345 | * Free up the URI struct |
| 1346 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1347 | void uri_free(URI *uri) |
| 1348 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1349 | uri_clean(uri); |
| 1350 | g_free(uri); |
| 1351 | } |
| 1352 | |
| 1353 | /************************************************************************ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1354 | * * |
| 1355 | * Helper functions * |
| 1356 | * * |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1357 | ************************************************************************/ |
| 1358 | |
| 1359 | /** |
| 1360 | * normalize_uri_path: |
| 1361 | * @path: pointer to the path string |
| 1362 | * |
| 1363 | * Applies the 5 normalization steps to a path string--that is, RFC 2396 |
| 1364 | * Section 5.2, steps 6.c through 6.g. |
| 1365 | * |
| 1366 | * Normalization occurs directly on the string, no new allocation is done |
| 1367 | * |
| 1368 | * Returns 0 or an error code |
| 1369 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1370 | static int normalize_uri_path(char *path) |
| 1371 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1372 | char *cur, *out; |
| 1373 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1374 | if (path == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1375 | return -1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1376 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1377 | |
| 1378 | /* Skip all initial "/" chars. We want to get to the beginning of the |
| 1379 | * first non-empty segment. |
| 1380 | */ |
| 1381 | cur = path; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1382 | while (cur[0] == '/') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1383 | ++cur; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1384 | } |
| 1385 | if (cur[0] == '\0') { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1386 | return 0; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1387 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1388 | |
| 1389 | /* Keep everything we've seen so far. */ |
| 1390 | out = cur; |
| 1391 | |
| 1392 | /* |
| 1393 | * Analyze each segment in sequence for cases (c) and (d). |
| 1394 | */ |
| 1395 | while (cur[0] != '\0') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1396 | /* |
| 1397 | * c) All occurrences of "./", where "." is a complete path segment, |
| 1398 | * are removed from the buffer string. |
| 1399 | */ |
| 1400 | if ((cur[0] == '.') && (cur[1] == '/')) { |
| 1401 | cur += 2; |
| 1402 | /* '//' normalization should be done at this point too */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1403 | while (cur[0] == '/') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1404 | cur++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1405 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1406 | continue; |
| 1407 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1408 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1409 | /* |
| 1410 | * d) If the buffer string ends with "." as a complete path segment, |
| 1411 | * that "." is removed. |
| 1412 | */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1413 | if ((cur[0] == '.') && (cur[1] == '\0')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1414 | break; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1415 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1416 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1417 | /* Otherwise keep the segment. */ |
| 1418 | while (cur[0] != '/') { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1419 | if (cur[0] == '\0') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1420 | goto done_cd; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1421 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1422 | (out++)[0] = (cur++)[0]; |
| 1423 | } |
| 1424 | /* nomalize // */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1425 | while ((cur[0] == '/') && (cur[1] == '/')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1426 | cur++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1427 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1428 | |
| 1429 | (out++)[0] = (cur++)[0]; |
| 1430 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1431 | done_cd: |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1432 | out[0] = '\0'; |
| 1433 | |
| 1434 | /* Reset to the beginning of the first segment for the next sequence. */ |
| 1435 | cur = path; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1436 | while (cur[0] == '/') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1437 | ++cur; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1438 | } |
| 1439 | if (cur[0] == '\0') { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1440 | return 0; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1441 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1442 | |
| 1443 | /* |
| 1444 | * Analyze each segment in sequence for cases (e) and (f). |
| 1445 | * |
| 1446 | * e) All occurrences of "<segment>/../", where <segment> is a |
| 1447 | * complete path segment not equal to "..", are removed from the |
| 1448 | * buffer string. Removal of these path segments is performed |
| 1449 | * iteratively, removing the leftmost matching pattern on each |
| 1450 | * iteration, until no matching pattern remains. |
| 1451 | * |
| 1452 | * f) If the buffer string ends with "<segment>/..", where <segment> |
| 1453 | * is a complete path segment not equal to "..", that |
| 1454 | * "<segment>/.." is removed. |
| 1455 | * |
| 1456 | * To satisfy the "iterative" clause in (e), we need to collapse the |
| 1457 | * string every time we find something that needs to be removed. Thus, |
| 1458 | * we don't need to keep two pointers into the string: we only need a |
| 1459 | * "current position" pointer. |
| 1460 | */ |
| 1461 | while (1) { |
| 1462 | char *segp, *tmp; |
| 1463 | |
| 1464 | /* At the beginning of each iteration of this loop, "cur" points to |
| 1465 | * the first character of the segment we want to examine. |
| 1466 | */ |
| 1467 | |
| 1468 | /* Find the end of the current segment. */ |
| 1469 | segp = cur; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1470 | while ((segp[0] != '/') && (segp[0] != '\0')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1471 | ++segp; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1472 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1473 | |
| 1474 | /* If this is the last segment, we're done (we need at least two |
| 1475 | * segments to meet the criteria for the (e) and (f) cases). |
| 1476 | */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1477 | if (segp[0] == '\0') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1478 | break; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1479 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1480 | |
| 1481 | /* If the first segment is "..", or if the next segment _isn't_ "..", |
| 1482 | * keep this segment and try the next one. |
| 1483 | */ |
| 1484 | ++segp; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1485 | if (((cur[0] == '.') && (cur[1] == '.') && (segp == cur + 3)) || |
| 1486 | ((segp[0] != '.') || (segp[1] != '.') || |
| 1487 | ((segp[2] != '/') && (segp[2] != '\0')))) { |
| 1488 | cur = segp; |
| 1489 | continue; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* If we get here, remove this segment and the next one and back up |
| 1493 | * to the previous segment (if there is one), to implement the |
| 1494 | * "iteratively" clause. It's pretty much impossible to back up |
| 1495 | * while maintaining two pointers into the buffer, so just compact |
| 1496 | * the whole buffer now. |
| 1497 | */ |
| 1498 | |
| 1499 | /* If this is the end of the buffer, we're done. */ |
| 1500 | if (segp[2] == '\0') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1501 | cur[0] = '\0'; |
| 1502 | break; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1503 | } |
| 1504 | /* Valgrind complained, strcpy(cur, segp + 3); */ |
| 1505 | /* string will overlap, do not use strcpy */ |
| 1506 | tmp = cur; |
| 1507 | segp += 3; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1508 | while ((*tmp++ = *segp++) != 0) { |
| 1509 | /* No further work */ |
| 1510 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1511 | |
| 1512 | /* If there are no previous segments, then keep going from here. */ |
| 1513 | segp = cur; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1514 | while ((segp > path) && ((--segp)[0] == '/')) { |
| 1515 | /* No further work */ |
| 1516 | } |
| 1517 | if (segp == path) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1518 | continue; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1519 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1520 | |
| 1521 | /* "segp" is pointing to the end of a previous segment; find it's |
| 1522 | * start. We need to back up to the previous segment and start |
| 1523 | * over with that to handle things like "foo/bar/../..". If we |
| 1524 | * don't do this, then on the first pass we'll remove the "bar/..", |
| 1525 | * but be pointing at the second ".." so we won't realize we can also |
| 1526 | * remove the "foo/..". |
| 1527 | */ |
| 1528 | cur = segp; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1529 | while ((cur > path) && (cur[-1] != '/')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1530 | --cur; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1531 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1532 | } |
| 1533 | out[0] = '\0'; |
| 1534 | |
| 1535 | /* |
| 1536 | * g) If the resulting buffer string still begins with one or more |
| 1537 | * complete path segments of "..", then the reference is |
| 1538 | * considered to be in error. Implementations may handle this |
| 1539 | * error by retaining these components in the resolved path (i.e., |
| 1540 | * treating them as part of the final URI), by removing them from |
| 1541 | * the resolved path (i.e., discarding relative levels above the |
| 1542 | * root), or by avoiding traversal of the reference. |
| 1543 | * |
| 1544 | * We discard them from the final path. |
| 1545 | */ |
| 1546 | if (path[0] == '/') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1547 | cur = path; |
| 1548 | while ((cur[0] == '/') && (cur[1] == '.') && (cur[2] == '.') && |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1549 | ((cur[3] == '/') || (cur[3] == '\0'))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1550 | cur += 3; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1551 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1552 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1553 | if (cur != path) { |
| 1554 | out = path; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1555 | while (cur[0] != '\0') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1556 | (out++)[0] = (cur++)[0]; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1557 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1558 | out[0] = 0; |
| 1559 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1560 | } |
| 1561 | |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1562 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1563 | } |
| 1564 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1565 | static int is_hex(char c) |
| 1566 | { |
| 1567 | if (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1568 | ((c >= 'A') && (c <= 'F'))) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1569 | return 1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1570 | } |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1571 | return 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1572 | } |
| 1573 | |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1574 | /** |
| 1575 | * uri_string_unescape: |
| 1576 | * @str: the string to unescape |
| 1577 | * @len: the length in bytes to unescape (or <= 0 to indicate full string) |
| 1578 | * @target: optional destination buffer |
| 1579 | * |
| 1580 | * Unescaping routine, but does not check that the string is an URI. The |
| 1581 | * output is a direct unsigned char translation of %XX values (no encoding) |
| 1582 | * Note that the length of the result can only be smaller or same size as |
| 1583 | * the input string. |
| 1584 | * |
| 1585 | * Returns a copy of the string, but unescaped, will return NULL only in case |
| 1586 | * of error |
| 1587 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1588 | char *uri_string_unescape(const char *str, int len, char *target) |
| 1589 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1590 | char *ret, *out; |
| 1591 | const char *in; |
| 1592 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1593 | if (str == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1594 | return NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1595 | } |
| 1596 | if (len <= 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1597 | len = strlen(str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1598 | } |
| 1599 | if (len < 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1600 | return NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1601 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1602 | |
| 1603 | if (target == NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1604 | ret = g_malloc(len + 1); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1605 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1606 | ret = target; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1607 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1608 | in = str; |
| 1609 | out = ret; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1610 | while (len > 0) { |
| 1611 | if ((len > 2) && (*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) { |
| 1612 | in++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1613 | if ((*in >= '0') && (*in <= '9')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1614 | *out = (*in - '0'); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1615 | } else if ((*in >= 'a') && (*in <= 'f')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1616 | *out = (*in - 'a') + 10; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1617 | } else if ((*in >= 'A') && (*in <= 'F')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1618 | *out = (*in - 'A') + 10; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1619 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1620 | in++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1621 | if ((*in >= '0') && (*in <= '9')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1622 | *out = *out * 16 + (*in - '0'); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1623 | } else if ((*in >= 'a') && (*in <= 'f')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1624 | *out = *out * 16 + (*in - 'a') + 10; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1625 | } else if ((*in >= 'A') && (*in <= 'F')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1626 | *out = *out * 16 + (*in - 'A') + 10; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1627 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1628 | in++; |
| 1629 | len -= 3; |
| 1630 | out++; |
| 1631 | } else { |
| 1632 | *out++ = *in++; |
| 1633 | len--; |
| 1634 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1635 | } |
| 1636 | *out = 0; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1637 | return ret; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | /** |
| 1641 | * uri_string_escape: |
| 1642 | * @str: string to escape |
| 1643 | * @list: exception list string of chars not to escape |
| 1644 | * |
| 1645 | * This routine escapes a string to hex, ignoring reserved characters (a-z) |
| 1646 | * and the characters in the exception list. |
| 1647 | * |
| 1648 | * Returns a new escaped string or NULL in case of error. |
| 1649 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1650 | char *uri_string_escape(const char *str, const char *list) |
| 1651 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1652 | char *ret, ch; |
| 1653 | char *temp; |
| 1654 | const char *in; |
| 1655 | int len, out; |
| 1656 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1657 | if (str == NULL) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1658 | return NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1659 | } |
| 1660 | if (str[0] == 0) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1661 | return g_strdup(str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1662 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1663 | len = strlen(str); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1664 | if (!(len > 0)) { |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1665 | return NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1666 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1667 | |
| 1668 | len += 20; |
| 1669 | ret = g_malloc(len); |
| 1670 | in = str; |
| 1671 | out = 0; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1672 | while (*in != 0) { |
| 1673 | if (len - out <= 3) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1674 | temp = realloc2n(ret, &len); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1675 | ret = temp; |
| 1676 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1677 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1678 | ch = *in; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1679 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1680 | if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!strchr(list, ch))) { |
| 1681 | unsigned char val; |
| 1682 | ret[out++] = '%'; |
| 1683 | val = ch >> 4; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1684 | if (val <= 9) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1685 | ret[out++] = '0' + val; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1686 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1687 | ret[out++] = 'A' + val - 0xA; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1688 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1689 | val = ch & 0xF; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1690 | if (val <= 9) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1691 | ret[out++] = '0' + val; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1692 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1693 | ret[out++] = 'A' + val - 0xA; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1694 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1695 | in++; |
| 1696 | } else { |
| 1697 | ret[out++] = *in++; |
| 1698 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1699 | } |
| 1700 | ret[out] = 0; |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1701 | return ret; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | /************************************************************************ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1705 | * * |
| 1706 | * Public functions * |
| 1707 | * * |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1708 | ************************************************************************/ |
| 1709 | |
| 1710 | /** |
| 1711 | * uri_resolve: |
| 1712 | * @URI: the URI instance found in the document |
| 1713 | * @base: the base value |
| 1714 | * |
| 1715 | * Computes he final URI of the reference done by checking that |
| 1716 | * the given URI is valid, and building the final URI using the |
| 1717 | * base URI. This is processed according to section 5.2 of the |
| 1718 | * RFC 2396 |
| 1719 | * |
| 1720 | * 5.2. Resolving Relative References to Absolute Form |
| 1721 | * |
| 1722 | * Returns a new URI string (to be freed by the caller) or NULL in case |
| 1723 | * of error. |
| 1724 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1725 | char *uri_resolve(const char *uri, const char *base) |
| 1726 | { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1727 | char *val = NULL; |
| 1728 | int ret, len, indx, cur, out; |
| 1729 | URI *ref = NULL; |
| 1730 | URI *bas = NULL; |
| 1731 | URI *res = NULL; |
| 1732 | |
| 1733 | /* |
| 1734 | * 1) The URI reference is parsed into the potential four components and |
| 1735 | * fragment identifier, as described in Section 4.3. |
| 1736 | * |
| 1737 | * NOTE that a completely empty URI is treated by modern browsers |
| 1738 | * as a reference to "." rather than as a synonym for the current |
| 1739 | * URI. Should we do that here? |
| 1740 | */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1741 | if (uri == NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1742 | ret = -1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1743 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1744 | if (*uri) { |
| 1745 | ref = uri_new(); |
| 1746 | ret = uri_parse_into(ref, uri); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1747 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1748 | ret = 0; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1749 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1750 | } |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1751 | if (ret != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1752 | goto done; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1753 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1754 | if ((ref != NULL) && (ref->scheme != NULL)) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1755 | /* |
| 1756 | * The URI is absolute don't modify. |
| 1757 | */ |
| 1758 | val = g_strdup(uri); |
| 1759 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1760 | } |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1761 | if (base == NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1762 | ret = -1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1763 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1764 | bas = uri_new(); |
| 1765 | ret = uri_parse_into(bas, base); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1766 | } |
| 1767 | if (ret != 0) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1768 | if (ref) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1769 | val = uri_to_string(ref); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1770 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1771 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1772 | } |
| 1773 | if (ref == NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1774 | /* |
| 1775 | * the base fragment must be ignored |
| 1776 | */ |
Markus Armbruster | 44c2286 | 2014-12-04 15:00:01 +0100 | [diff] [blame] | 1777 | g_free(bas->fragment); |
| 1778 | bas->fragment = NULL; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1779 | val = uri_to_string(bas); |
| 1780 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | /* |
| 1784 | * 2) If the path component is empty and the scheme, authority, and |
| 1785 | * query components are undefined, then it is a reference to the |
| 1786 | * current document and we are done. Otherwise, the reference URI's |
| 1787 | * query and fragment components are defined as found (or not found) |
| 1788 | * within the URI reference and not inherited from the base URI. |
| 1789 | * |
| 1790 | * NOTE that in modern browsers, the parsing differs from the above |
| 1791 | * in the following aspect: the query component is allowed to be |
| 1792 | * defined while still treating this as a reference to the current |
| 1793 | * document. |
| 1794 | */ |
| 1795 | res = uri_new(); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1796 | if ((ref->scheme == NULL) && (ref->path == NULL) && |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1797 | ((ref->authority == NULL) && (ref->server == NULL))) { |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 1798 | res->scheme = g_strdup(bas->scheme); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1799 | if (bas->authority != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1800 | res->authority = g_strdup(bas->authority); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1801 | } else if (bas->server != NULL) { |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 1802 | res->server = g_strdup(bas->server); |
| 1803 | res->user = g_strdup(bas->user); |
| 1804 | res->port = bas->port; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1805 | } |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 1806 | res->path = g_strdup(bas->path); |
| 1807 | if (ref->query != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1808 | res->query = g_strdup(ref->query); |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 1809 | } else { |
| 1810 | res->query = g_strdup(bas->query); |
| 1811 | } |
| 1812 | res->fragment = g_strdup(ref->fragment); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1813 | goto step_7; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * 3) If the scheme component is defined, indicating that the reference |
| 1818 | * starts with a scheme name, then the reference is interpreted as an |
| 1819 | * absolute URI and we are done. Otherwise, the reference URI's |
| 1820 | * scheme is inherited from the base URI's scheme component. |
| 1821 | */ |
| 1822 | if (ref->scheme != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1823 | val = uri_to_string(ref); |
| 1824 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1825 | } |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 1826 | res->scheme = g_strdup(bas->scheme); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1827 | |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 1828 | res->query = g_strdup(ref->query); |
| 1829 | res->fragment = g_strdup(ref->fragment); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1830 | |
| 1831 | /* |
| 1832 | * 4) If the authority component is defined, then the reference is a |
| 1833 | * network-path and we skip to step 7. Otherwise, the reference |
| 1834 | * URI's authority is inherited from the base URI's authority |
| 1835 | * component, which will also be undefined if the URI scheme does not |
| 1836 | * use an authority component. |
| 1837 | */ |
| 1838 | if ((ref->authority != NULL) || (ref->server != NULL)) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1839 | if (ref->authority != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1840 | res->authority = g_strdup(ref->authority); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1841 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1842 | res->server = g_strdup(ref->server); |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 1843 | res->user = g_strdup(ref->user); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1844 | res->port = ref->port; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1845 | } |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 1846 | res->path = g_strdup(ref->path); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1847 | goto step_7; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1848 | } |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1849 | if (bas->authority != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1850 | res->authority = g_strdup(bas->authority); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1851 | } else if (bas->server != NULL) { |
Markus Armbruster | 2458810 | 2014-12-04 10:26:55 +0100 | [diff] [blame] | 1852 | res->server = g_strdup(bas->server); |
| 1853 | res->user = g_strdup(bas->user); |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1854 | res->port = bas->port; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * 5) If the path component begins with a slash character ("/"), then |
| 1859 | * the reference is an absolute-path and we skip to step 7. |
| 1860 | */ |
| 1861 | if ((ref->path != NULL) && (ref->path[0] == '/')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1862 | res->path = g_strdup(ref->path); |
| 1863 | goto step_7; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1864 | } |
| 1865 | |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1866 | /* |
| 1867 | * 6) If this step is reached, then we are resolving a relative-path |
| 1868 | * reference. The relative path needs to be merged with the base |
| 1869 | * URI's path. Although there are many ways to do this, we will |
| 1870 | * describe a simple method using a separate string buffer. |
| 1871 | * |
| 1872 | * Allocate a buffer large enough for the result string. |
| 1873 | */ |
| 1874 | len = 2; /* extra / and 0 */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1875 | if (ref->path != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1876 | len += strlen(ref->path); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1877 | } |
| 1878 | if (bas->path != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1879 | len += strlen(bas->path); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1880 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1881 | res->path = g_malloc(len); |
| 1882 | res->path[0] = 0; |
| 1883 | |
| 1884 | /* |
| 1885 | * a) All but the last segment of the base URI's path component is |
| 1886 | * copied to the buffer. In other words, any characters after the |
| 1887 | * last (right-most) slash character, if any, are excluded. |
| 1888 | */ |
| 1889 | cur = 0; |
| 1890 | out = 0; |
| 1891 | if (bas->path != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1892 | while (bas->path[cur] != 0) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1893 | while ((bas->path[cur] != 0) && (bas->path[cur] != '/')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1894 | cur++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1895 | } |
| 1896 | if (bas->path[cur] == 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1897 | break; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1898 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1899 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1900 | cur++; |
| 1901 | while (out < cur) { |
| 1902 | res->path[out] = bas->path[out]; |
| 1903 | out++; |
| 1904 | } |
| 1905 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1906 | } |
| 1907 | res->path[out] = 0; |
| 1908 | |
| 1909 | /* |
| 1910 | * b) The reference's path component is appended to the buffer |
| 1911 | * string. |
| 1912 | */ |
| 1913 | if (ref->path != NULL && ref->path[0] != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1914 | indx = 0; |
| 1915 | /* |
| 1916 | * Ensure the path includes a '/' |
| 1917 | */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1918 | if ((out == 0) && (bas->server != NULL)) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1919 | res->path[out++] = '/'; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1920 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1921 | while (ref->path[indx] != 0) { |
| 1922 | res->path[out++] = ref->path[indx++]; |
| 1923 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1924 | } |
| 1925 | res->path[out] = 0; |
| 1926 | |
| 1927 | /* |
| 1928 | * Steps c) to h) are really path normalization steps |
| 1929 | */ |
| 1930 | normalize_uri_path(res->path); |
| 1931 | |
| 1932 | step_7: |
| 1933 | |
| 1934 | /* |
| 1935 | * 7) The resulting URI components, including any inherited from the |
| 1936 | * base URI, are recombined to give the absolute form of the URI |
| 1937 | * reference. |
| 1938 | */ |
| 1939 | val = uri_to_string(res); |
| 1940 | |
| 1941 | done: |
Heinrich Schuchardt | c2615bd | 2021-06-29 08:36:02 +0200 | [diff] [blame] | 1942 | uri_free(ref); |
| 1943 | uri_free(bas); |
| 1944 | uri_free(res); |
Su Hang | 42fa272 | 2018-02-25 12:35:57 +0800 | [diff] [blame] | 1945 | return val; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | /** |
| 1949 | * uri_resolve_relative: |
| 1950 | * @URI: the URI reference under consideration |
| 1951 | * @base: the base value |
| 1952 | * |
| 1953 | * Expresses the URI of the reference in terms relative to the |
| 1954 | * base. Some examples of this operation include: |
| 1955 | * base = "http://site1.com/docs/book1.html" |
| 1956 | * URI input URI returned |
| 1957 | * docs/pic1.gif pic1.gif |
| 1958 | * docs/img/pic1.gif img/pic1.gif |
| 1959 | * img/pic1.gif ../img/pic1.gif |
| 1960 | * http://site1.com/docs/pic1.gif pic1.gif |
| 1961 | * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif |
| 1962 | * |
| 1963 | * base = "docs/book1.html" |
| 1964 | * URI input URI returned |
| 1965 | * docs/pic1.gif pic1.gif |
| 1966 | * docs/img/pic1.gif img/pic1.gif |
| 1967 | * img/pic1.gif ../img/pic1.gif |
| 1968 | * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif |
| 1969 | * |
| 1970 | * |
Stefan Weil | a93cf9d | 2012-11-02 08:29:53 +0100 | [diff] [blame] | 1971 | * Note: if the URI reference is really weird or complicated, it may be |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1972 | * worthwhile to first convert it into a "nice" one by calling |
| 1973 | * uri_resolve (using 'base') before calling this routine, |
| 1974 | * since this routine (for reasonable efficiency) assumes URI has |
| 1975 | * already been through some validation. |
| 1976 | * |
| 1977 | * Returns a new URI string (to be freed by the caller) or NULL in case |
| 1978 | * error. |
| 1979 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1980 | char *uri_resolve_relative(const char *uri, const char *base) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1981 | { |
| 1982 | char *val = NULL; |
| 1983 | int ret; |
| 1984 | int ix; |
| 1985 | int pos = 0; |
| 1986 | int nbslash = 0; |
| 1987 | int len; |
| 1988 | URI *ref = NULL; |
| 1989 | URI *bas = NULL; |
| 1990 | char *bptr, *uptr, *vptr; |
| 1991 | int remove_path = 0; |
| 1992 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1993 | if ((uri == NULL) || (*uri == 0)) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 1994 | return NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 1995 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 1996 | |
| 1997 | /* |
| 1998 | * First parse URI into a standard form |
| 1999 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2000 | ref = uri_new(); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2001 | /* If URI not already in "relative" form */ |
| 2002 | if (uri[0] != '.') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2003 | ret = uri_parse_into(ref, uri); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2004 | if (ret != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2005 | goto done; /* Error in URI, return NULL */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2006 | } |
| 2007 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2008 | ref->path = g_strdup(uri); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2009 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2010 | |
| 2011 | /* |
| 2012 | * Next parse base into the same standard form |
| 2013 | */ |
| 2014 | if ((base == NULL) || (*base == 0)) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2015 | val = g_strdup(uri); |
| 2016 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2017 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2018 | bas = uri_new(); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2019 | if (base[0] != '.') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2020 | ret = uri_parse_into(bas, base); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2021 | if (ret != 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2022 | goto done; /* Error in base, return NULL */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2023 | } |
| 2024 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2025 | bas->path = g_strdup(base); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2026 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2027 | |
| 2028 | /* |
| 2029 | * If the scheme / server on the URI differs from the base, |
| 2030 | * just return the URI |
| 2031 | */ |
| 2032 | if ((ref->scheme != NULL) && |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2033 | ((bas->scheme == NULL) || (strcmp(bas->scheme, ref->scheme)) || |
| 2034 | (strcmp(bas->server, ref->server)))) { |
| 2035 | val = g_strdup(uri); |
| 2036 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2037 | } |
Markus Armbruster | afb30dd | 2015-01-27 17:13:52 +0100 | [diff] [blame] | 2038 | if (bas->path == ref->path || |
| 2039 | (bas->path && ref->path && !strcmp(bas->path, ref->path))) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2040 | val = g_strdup(""); |
| 2041 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2042 | } |
| 2043 | if (bas->path == NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2044 | val = g_strdup(ref->path); |
| 2045 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2046 | } |
| 2047 | if (ref->path == NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2048 | ref->path = (char *)"/"; |
| 2049 | remove_path = 1; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /* |
| 2053 | * At this point (at last!) we can compare the two paths |
| 2054 | * |
| 2055 | * First we take care of the special case where either of the |
| 2056 | * two path components may be missing (bug 316224) |
| 2057 | */ |
| 2058 | if (bas->path == NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2059 | if (ref->path != NULL) { |
| 2060 | uptr = ref->path; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2061 | if (*uptr == '/') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2062 | uptr++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2063 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2064 | /* exception characters from uri_to_string */ |
| 2065 | val = uri_string_escape(uptr, "/;&=+$,"); |
| 2066 | } |
| 2067 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2068 | } |
| 2069 | bptr = bas->path; |
| 2070 | if (ref->path == NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2071 | for (ix = 0; bptr[ix] != 0; ix++) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2072 | if (bptr[ix] == '/') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2073 | nbslash++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2074 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2075 | } |
| 2076 | uptr = NULL; |
| 2077 | len = 1; /* this is for a string terminator only */ |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2078 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2079 | /* |
| 2080 | * Next we compare the two strings and find where they first differ |
| 2081 | */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2082 | if ((ref->path[pos] == '.') && (ref->path[pos + 1] == '/')) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2083 | pos += 2; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2084 | } |
| 2085 | if ((*bptr == '.') && (bptr[1] == '/')) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2086 | bptr += 2; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2087 | } else if ((*bptr == '/') && (ref->path[pos] != '/')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2088 | bptr++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2089 | } |
| 2090 | while ((bptr[pos] == ref->path[pos]) && (bptr[pos] != 0)) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2091 | pos++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2092 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2093 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2094 | if (bptr[pos] == ref->path[pos]) { |
| 2095 | val = g_strdup(""); |
| 2096 | goto done; /* (I can't imagine why anyone would do this) */ |
| 2097 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2098 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2099 | /* |
| 2100 | * In URI, "back up" to the last '/' encountered. This will be the |
| 2101 | * beginning of the "unique" suffix of URI |
| 2102 | */ |
| 2103 | ix = pos; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2104 | if ((ref->path[ix] == '/') && (ix > 0)) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2105 | ix--; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2106 | } else if ((ref->path[ix] == 0) && (ix > 1) |
| 2107 | && (ref->path[ix - 1] == '/')) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2108 | ix -= 2; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2109 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2110 | for (; ix > 0; ix--) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2111 | if (ref->path[ix] == '/') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2112 | break; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2113 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2114 | } |
| 2115 | if (ix == 0) { |
| 2116 | uptr = ref->path; |
| 2117 | } else { |
| 2118 | ix++; |
| 2119 | uptr = &ref->path[ix]; |
| 2120 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2121 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2122 | /* |
| 2123 | * In base, count the number of '/' from the differing point |
| 2124 | */ |
| 2125 | if (bptr[pos] != ref->path[pos]) { /* check for trivial URI == base */ |
| 2126 | for (; bptr[ix] != 0; ix++) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2127 | if (bptr[ix] == '/') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2128 | nbslash++; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2129 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | len = strlen(uptr) + 1; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | if (nbslash == 0) { |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2136 | if (uptr != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2137 | /* exception characters from uri_to_string */ |
| 2138 | val = uri_string_escape(uptr, "/;&=+$,"); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2139 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2140 | goto done; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | /* |
| 2144 | * Allocate just enough space for the returned string - |
| 2145 | * length of the remainder of the URI, plus enough space |
| 2146 | * for the "../" groups, plus one for the terminator |
| 2147 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2148 | val = g_malloc(len + 3 * nbslash); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2149 | vptr = val; |
| 2150 | /* |
| 2151 | * Put in as many "../" as needed |
| 2152 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2153 | for (; nbslash > 0; nbslash--) { |
| 2154 | *vptr++ = '.'; |
| 2155 | *vptr++ = '.'; |
| 2156 | *vptr++ = '/'; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2157 | } |
| 2158 | /* |
| 2159 | * Finish up with the end of the URI |
| 2160 | */ |
| 2161 | if (uptr != NULL) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2162 | if ((vptr > val) && (len > 0) && (uptr[0] == '/') && |
| 2163 | (vptr[-1] == '/')) { |
| 2164 | memcpy(vptr, uptr + 1, len - 1); |
| 2165 | vptr[len - 2] = 0; |
| 2166 | } else { |
| 2167 | memcpy(vptr, uptr, len); |
| 2168 | vptr[len - 1] = 0; |
| 2169 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2170 | } else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2171 | vptr[len - 1] = 0; |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | /* escape the freshly-built path */ |
| 2175 | vptr = val; |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2176 | /* exception characters from uri_to_string */ |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2177 | val = uri_string_escape(vptr, "/;&=+$,"); |
| 2178 | g_free(vptr); |
| 2179 | |
| 2180 | done: |
| 2181 | /* |
| 2182 | * Free the working variables |
| 2183 | */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2184 | if (remove_path != 0) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2185 | ref->path = NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2186 | } |
Heinrich Schuchardt | c2615bd | 2021-06-29 08:36:02 +0200 | [diff] [blame] | 2187 | uri_free(ref); |
| 2188 | uri_free(bas); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2189 | |
| 2190 | return val; |
| 2191 | } |
| 2192 | |
| 2193 | /* |
| 2194 | * Utility functions to help parse and assemble query strings. |
| 2195 | */ |
| 2196 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2197 | struct QueryParams *query_params_new(int init_alloc) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2198 | { |
| 2199 | struct QueryParams *ps; |
| 2200 | |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2201 | if (init_alloc <= 0) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2202 | init_alloc = 1; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2203 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2204 | |
| 2205 | ps = g_new(QueryParams, 1); |
| 2206 | ps->n = 0; |
| 2207 | ps->alloc = init_alloc; |
| 2208 | ps->p = g_new(QueryParam, ps->alloc); |
| 2209 | |
| 2210 | return ps; |
| 2211 | } |
| 2212 | |
| 2213 | /* Ensure there is space to store at least one more parameter |
| 2214 | * at the end of the set. |
| 2215 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2216 | static int query_params_append(struct QueryParams *ps, const char *name, |
| 2217 | const char *value) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2218 | { |
| 2219 | if (ps->n >= ps->alloc) { |
| 2220 | ps->p = g_renew(QueryParam, ps->p, ps->alloc * 2); |
| 2221 | ps->alloc *= 2; |
| 2222 | } |
| 2223 | |
| 2224 | ps->p[ps->n].name = g_strdup(name); |
Dong Xu Wang | 7f303ad | 2013-05-09 15:53:49 +0800 | [diff] [blame] | 2225 | ps->p[ps->n].value = g_strdup(value); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2226 | ps->p[ps->n].ignore = 0; |
| 2227 | ps->n++; |
| 2228 | |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2232 | void query_params_free(struct QueryParams *ps) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2233 | { |
| 2234 | int i; |
| 2235 | |
| 2236 | for (i = 0; i < ps->n; ++i) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2237 | g_free(ps->p[i].name); |
| 2238 | g_free(ps->p[i].value); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2239 | } |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2240 | g_free(ps->p); |
| 2241 | g_free(ps); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2242 | } |
| 2243 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2244 | struct QueryParams *query_params_parse(const char *query) |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2245 | { |
| 2246 | struct QueryParams *ps; |
| 2247 | const char *end, *eq; |
| 2248 | |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2249 | ps = query_params_new(0); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2250 | if (!query || query[0] == '\0') { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2251 | return ps; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2252 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2253 | |
| 2254 | while (*query) { |
| 2255 | char *name = NULL, *value = NULL; |
| 2256 | |
| 2257 | /* Find the next separator, or end of the string. */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2258 | end = strchr(query, '&'); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2259 | if (!end) { |
Keno Fischer | 5c99fa3 | 2018-06-29 12:32:10 +0200 | [diff] [blame] | 2260 | end = qemu_strchrnul(query, ';'); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2261 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2262 | |
| 2263 | /* Find the first '=' character between here and end. */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2264 | eq = strchr(query, '='); |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2265 | if (eq && eq >= end) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2266 | eq = NULL; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2267 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2268 | |
| 2269 | /* Empty section (eg. "&&"). */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2270 | if (end == query) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2271 | goto next; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2272 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2273 | |
| 2274 | /* If there is no '=' character, then we have just "name" |
| 2275 | * and consistent with CGI.pm we assume value is "". |
| 2276 | */ |
| 2277 | else if (!eq) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2278 | name = uri_string_unescape(query, end - query, NULL); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2279 | value = NULL; |
| 2280 | } |
| 2281 | /* Or if we have "name=" here (works around annoying |
| 2282 | * problem when calling uri_string_unescape with len = 0). |
| 2283 | */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2284 | else if (eq + 1 == end) { |
| 2285 | name = uri_string_unescape(query, eq - query, NULL); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2286 | value = g_new0(char, 1); |
| 2287 | } |
| 2288 | /* If the '=' character is at the beginning then we have |
| 2289 | * "=value" and consistent with CGI.pm we _ignore_ this. |
| 2290 | */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2291 | else if (query == eq) { |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2292 | goto next; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2293 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2294 | |
| 2295 | /* Otherwise it's "name=value". */ |
| 2296 | else { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2297 | name = uri_string_unescape(query, eq - query, NULL); |
| 2298 | value = uri_string_unescape(eq + 1, end - (eq + 1), NULL); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | /* Append to the parameter set. */ |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2302 | query_params_append(ps, name, value); |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2303 | g_free(name); |
| 2304 | g_free(value); |
| 2305 | |
| 2306 | next: |
| 2307 | query = end; |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2308 | if (*query) { |
Su Hang | be95ada | 2018-02-25 12:35:56 +0800 | [diff] [blame] | 2309 | query++; /* skip '&' separator */ |
Su Hang | a151516 | 2018-02-25 12:35:58 +0800 | [diff] [blame] | 2310 | } |
Paolo Bonzini | ca0defb | 2012-09-24 14:42:02 +0530 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | return ps; |
| 2314 | } |