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