Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /** |
| 2 | * uri.c: set of generic URI related routines |
| 3 | * |
| 4 | * Reference: RFC 2396 |
| 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 | |
| 21 | /************************************************************************ |
| 22 | * * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 23 | * Macros to differentiate various character type * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 24 | * directly extracted from RFC 2396 * |
| 25 | * * |
| 26 | ************************************************************************/ |
| 27 | |
| 28 | /* |
| 29 | * alpha = lowalpha | upalpha |
| 30 | */ |
| 31 | #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x)) |
| 32 | |
| 33 | |
| 34 | /* |
| 35 | * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | |
| 36 | * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | |
| 37 | * "u" | "v" | "w" | "x" | "y" | "z" |
| 38 | */ |
| 39 | |
| 40 | #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z')) |
| 41 | |
| 42 | /* |
| 43 | * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | |
| 44 | * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | |
| 45 | * "U" | "V" | "W" | "X" | "Y" | "Z" |
| 46 | */ |
| 47 | #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z')) |
| 48 | |
| 49 | /* |
| 50 | * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
| 51 | */ |
| 52 | |
| 53 | #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9')) |
| 54 | |
| 55 | /* |
| 56 | * alphanum = alpha | digit |
| 57 | */ |
| 58 | |
| 59 | #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x)) |
| 60 | |
| 61 | /* |
| 62 | * hex = digit | "A" | "B" | "C" | "D" | "E" | "F" | |
| 63 | * "a" | "b" | "c" | "d" | "e" | "f" |
| 64 | */ |
| 65 | |
| 66 | #define IS_HEX(x) ((IS_DIGIT(x)) || (((x) >= 'a') && ((x) <= 'f')) || \ |
| 67 | (((x) >= 'A') && ((x) <= 'F'))) |
| 68 | |
| 69 | /* |
| 70 | * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" |
| 71 | */ |
| 72 | |
| 73 | #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \ |
| 74 | ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \ |
| 75 | ((x) == '(') || ((x) == ')')) |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
| 80 | */ |
| 81 | |
| 82 | #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \ |
| 83 | ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \ |
| 84 | ((x) == '+') || ((x) == '$') || ((x) == ',')) |
| 85 | |
| 86 | /* |
| 87 | * unreserved = alphanum | mark |
| 88 | */ |
| 89 | |
| 90 | #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x)) |
| 91 | |
| 92 | /* |
| 93 | * escaped = "%" hex hex |
| 94 | */ |
| 95 | |
| 96 | #define IS_ESCAPED(p) ((*(p) == '%') && (IS_HEX((p)[1])) && \ |
| 97 | (IS_HEX((p)[2]))) |
| 98 | |
| 99 | /* |
| 100 | * uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" | |
| 101 | * "&" | "=" | "+" | "$" | "," |
| 102 | */ |
| 103 | #define IS_URIC_NO_SLASH(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) ||\ |
| 104 | ((*(p) == ';')) || ((*(p) == '?')) || ((*(p) == ':')) ||\ |
| 105 | ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) ||\ |
| 106 | ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ','))) |
| 107 | |
| 108 | /* |
| 109 | * pchar = unreserved | escaped | ":" | "@" | "&" | "=" | "+" | "$" | "," |
| 110 | */ |
| 111 | #define IS_PCHAR(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 112 | ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) ||\ |
| 113 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||\ |
| 114 | ((*(p) == ','))) |
| 115 | |
| 116 | /* |
| 117 | * rel_segment = 1*( unreserved | escaped | |
| 118 | * ";" | "@" | "&" | "=" | "+" | "$" | "," ) |
| 119 | */ |
| 120 | |
| 121 | #define IS_SEGMENT(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 122 | ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || \ |
| 123 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \ |
| 124 | ((*(p) == ','))) |
| 125 | |
| 126 | /* |
| 127 | * scheme = alpha *( alpha | digit | "+" | "-" | "." ) |
| 128 | */ |
| 129 | |
| 130 | #define IS_SCHEME(x) ((IS_ALPHA(x)) || (IS_DIGIT(x)) || \ |
| 131 | ((x) == '+') || ((x) == '-') || ((x) == '.')) |
| 132 | |
| 133 | /* |
| 134 | * reg_name = 1*( unreserved | escaped | "$" | "," | |
| 135 | * ";" | ":" | "@" | "&" | "=" | "+" ) |
| 136 | */ |
| 137 | |
| 138 | #define IS_REG_NAME(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 139 | ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || \ |
| 140 | ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) || \ |
| 141 | ((*(p) == '=')) || ((*(p) == '+'))) |
| 142 | |
| 143 | /* |
| 144 | * userinfo = *( unreserved | escaped | ";" | ":" | "&" | "=" | |
| 145 | * "+" | "$" | "," ) |
| 146 | */ |
| 147 | #define IS_USERINFO(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 148 | ((*(p) == ';')) || ((*(p) == ':')) || ((*(p) == '&')) || \ |
| 149 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \ |
| 150 | ((*(p) == ','))) |
| 151 | |
| 152 | /* |
| 153 | * uric = reserved | unreserved | escaped |
| 154 | */ |
| 155 | |
| 156 | #define IS_URIC(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 157 | (IS_RESERVED(*(p)))) |
| 158 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 159 | /* |
| 160 | * unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`" |
| 161 | */ |
Daniel Veillard | bb6808e | 2001-10-29 23:59:27 +0000 | [diff] [blame] | 162 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 163 | #define IS_UNWISE(p) \ |
| 164 | (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \ |
| 165 | ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \ |
| 166 | ((*(p) == ']')) || ((*(p) == '`'))) |
Daniel Veillard | bb6808e | 2001-10-29 23:59:27 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 169 | * Skip to next pointer char, handle escaped sequences |
| 170 | */ |
| 171 | |
| 172 | #define NEXT(p) ((*p == '%')? p += 3 : p++) |
| 173 | |
| 174 | /* |
| 175 | * Productions from the spec. |
| 176 | * |
| 177 | * authority = server | reg_name |
| 178 | * reg_name = 1*( unreserved | escaped | "$" | "," | |
| 179 | * ";" | ":" | "@" | "&" | "=" | "+" ) |
| 180 | * |
| 181 | * path = [ abs_path | opaque_part ] |
| 182 | */ |
| 183 | |
| 184 | /************************************************************************ |
| 185 | * * |
| 186 | * Generic URI structure functions * |
| 187 | * * |
| 188 | ************************************************************************/ |
| 189 | |
| 190 | /** |
| 191 | * xmlCreateURI: |
| 192 | * |
| 193 | * Simply creates an empty xmlURI |
| 194 | * |
| 195 | * Returns the new structure or NULL in case of error |
| 196 | */ |
| 197 | xmlURIPtr |
| 198 | xmlCreateURI(void) { |
| 199 | xmlURIPtr ret; |
| 200 | |
| 201 | ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI)); |
| 202 | if (ret == NULL) { |
| 203 | xmlGenericError(xmlGenericErrorContext, |
| 204 | "xmlCreateURI: out of memory\n"); |
| 205 | return(NULL); |
| 206 | } |
| 207 | memset(ret, 0, sizeof(xmlURI)); |
| 208 | return(ret); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * xmlSaveUri: |
| 213 | * @uri: pointer to an xmlURI |
| 214 | * |
| 215 | * Save the URI as an escaped string |
| 216 | * |
| 217 | * Returns a new string (to be deallocated by caller) |
| 218 | */ |
| 219 | xmlChar * |
| 220 | xmlSaveUri(xmlURIPtr uri) { |
| 221 | xmlChar *ret = NULL; |
| 222 | const char *p; |
| 223 | int len; |
| 224 | int max; |
| 225 | |
| 226 | if (uri == NULL) return(NULL); |
| 227 | |
| 228 | |
| 229 | max = 80; |
| 230 | ret = (xmlChar *) xmlMalloc((max + 1) * sizeof(xmlChar)); |
| 231 | if (ret == NULL) { |
| 232 | xmlGenericError(xmlGenericErrorContext, |
| 233 | "xmlSaveUri: out of memory\n"); |
| 234 | return(NULL); |
| 235 | } |
| 236 | len = 0; |
| 237 | |
| 238 | if (uri->scheme != NULL) { |
| 239 | p = uri->scheme; |
| 240 | while (*p != 0) { |
| 241 | if (len >= max) { |
| 242 | max *= 2; |
| 243 | ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 244 | if (ret == NULL) { |
| 245 | xmlGenericError(xmlGenericErrorContext, |
| 246 | "xmlSaveUri: out of memory\n"); |
| 247 | return(NULL); |
| 248 | } |
| 249 | } |
| 250 | ret[len++] = *p++; |
| 251 | } |
| 252 | if (len >= max) { |
| 253 | max *= 2; |
| 254 | ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 255 | if (ret == NULL) { |
| 256 | xmlGenericError(xmlGenericErrorContext, |
| 257 | "xmlSaveUri: out of memory\n"); |
| 258 | return(NULL); |
| 259 | } |
| 260 | } |
| 261 | ret[len++] = ':'; |
| 262 | } |
| 263 | if (uri->opaque != NULL) { |
| 264 | p = uri->opaque; |
| 265 | while (*p != 0) { |
| 266 | if (len + 3 >= max) { |
| 267 | max *= 2; |
| 268 | ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 269 | if (ret == NULL) { |
| 270 | xmlGenericError(xmlGenericErrorContext, |
| 271 | "xmlSaveUri: out of memory\n"); |
| 272 | return(NULL); |
| 273 | } |
| 274 | } |
| 275 | if ((IS_UNRESERVED(*(p))) || |
| 276 | ((*(p) == ';')) || ((*(p) == '?')) || ((*(p) == ':')) || |
| 277 | ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) || |
| 278 | ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ','))) |
| 279 | ret[len++] = *p++; |
| 280 | else { |
| 281 | int val = *(unsigned char *)p++; |
| 282 | int hi = val / 0x10, lo = val % 0x10; |
| 283 | ret[len++] = '%'; |
| 284 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
| 285 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
| 286 | } |
| 287 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 288 | } else { |
| 289 | if (uri->server != NULL) { |
| 290 | if (len + 3 >= max) { |
| 291 | max *= 2; |
| 292 | ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 293 | if (ret == NULL) { |
| 294 | xmlGenericError(xmlGenericErrorContext, |
| 295 | "xmlSaveUri: out of memory\n"); |
| 296 | return(NULL); |
| 297 | } |
| 298 | } |
| 299 | ret[len++] = '/'; |
| 300 | ret[len++] = '/'; |
| 301 | if (uri->user != NULL) { |
| 302 | p = uri->user; |
| 303 | while (*p != 0) { |
| 304 | if (len + 3 >= max) { |
| 305 | max *= 2; |
| 306 | ret = (xmlChar *) xmlRealloc(ret, |
| 307 | (max + 1) * sizeof(xmlChar)); |
| 308 | if (ret == NULL) { |
| 309 | xmlGenericError(xmlGenericErrorContext, |
| 310 | "xmlSaveUri: out of memory\n"); |
| 311 | return(NULL); |
| 312 | } |
| 313 | } |
| 314 | if ((IS_UNRESERVED(*(p))) || |
| 315 | ((*(p) == ';')) || ((*(p) == ':')) || |
| 316 | ((*(p) == '&')) || ((*(p) == '=')) || |
| 317 | ((*(p) == '+')) || ((*(p) == '$')) || |
| 318 | ((*(p) == ','))) |
| 319 | ret[len++] = *p++; |
| 320 | else { |
| 321 | int val = *(unsigned char *)p++; |
| 322 | int hi = val / 0x10, lo = val % 0x10; |
| 323 | ret[len++] = '%'; |
| 324 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
| 325 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
| 326 | } |
| 327 | } |
| 328 | if (len + 3 >= max) { |
| 329 | max *= 2; |
| 330 | ret = (xmlChar *) xmlRealloc(ret, |
| 331 | (max + 1) * sizeof(xmlChar)); |
| 332 | if (ret == NULL) { |
| 333 | xmlGenericError(xmlGenericErrorContext, |
| 334 | "xmlSaveUri: out of memory\n"); |
| 335 | return(NULL); |
| 336 | } |
| 337 | } |
| 338 | ret[len++] = '@'; |
| 339 | } |
| 340 | p = uri->server; |
| 341 | while (*p != 0) { |
| 342 | if (len >= max) { |
| 343 | max *= 2; |
| 344 | ret = (xmlChar *) xmlRealloc(ret, |
| 345 | (max + 1) * sizeof(xmlChar)); |
| 346 | if (ret == NULL) { |
| 347 | xmlGenericError(xmlGenericErrorContext, |
| 348 | "xmlSaveUri: out of memory\n"); |
| 349 | return(NULL); |
| 350 | } |
| 351 | } |
| 352 | ret[len++] = *p++; |
| 353 | } |
| 354 | if (uri->port > 0) { |
| 355 | if (len + 10 >= max) { |
| 356 | max *= 2; |
| 357 | ret = (xmlChar *) xmlRealloc(ret, |
| 358 | (max + 1) * sizeof(xmlChar)); |
| 359 | if (ret == NULL) { |
| 360 | xmlGenericError(xmlGenericErrorContext, |
| 361 | "xmlSaveUri: out of memory\n"); |
| 362 | return(NULL); |
| 363 | } |
| 364 | } |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 365 | len += snprintf((char *) &ret[len], max - len, ":%d", uri->port); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 366 | } |
| 367 | } else if (uri->authority != NULL) { |
| 368 | if (len + 3 >= max) { |
| 369 | max *= 2; |
| 370 | ret = (xmlChar *) xmlRealloc(ret, |
| 371 | (max + 1) * sizeof(xmlChar)); |
| 372 | if (ret == NULL) { |
| 373 | xmlGenericError(xmlGenericErrorContext, |
| 374 | "xmlSaveUri: out of memory\n"); |
| 375 | return(NULL); |
| 376 | } |
| 377 | } |
| 378 | ret[len++] = '/'; |
| 379 | ret[len++] = '/'; |
| 380 | p = uri->authority; |
| 381 | while (*p != 0) { |
| 382 | if (len + 3 >= max) { |
| 383 | max *= 2; |
| 384 | ret = (xmlChar *) xmlRealloc(ret, |
| 385 | (max + 1) * sizeof(xmlChar)); |
| 386 | if (ret == NULL) { |
| 387 | xmlGenericError(xmlGenericErrorContext, |
| 388 | "xmlSaveUri: out of memory\n"); |
| 389 | return(NULL); |
| 390 | } |
| 391 | } |
| 392 | if ((IS_UNRESERVED(*(p))) || |
| 393 | ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || |
| 394 | ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) || |
| 395 | ((*(p) == '=')) || ((*(p) == '+'))) |
| 396 | ret[len++] = *p++; |
| 397 | else { |
| 398 | int val = *(unsigned char *)p++; |
| 399 | int hi = val / 0x10, lo = val % 0x10; |
| 400 | ret[len++] = '%'; |
| 401 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
| 402 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
| 403 | } |
| 404 | } |
| 405 | } else if (uri->scheme != NULL) { |
| 406 | if (len + 3 >= max) { |
| 407 | max *= 2; |
| 408 | ret = (xmlChar *) xmlRealloc(ret, |
| 409 | (max + 1) * sizeof(xmlChar)); |
| 410 | if (ret == NULL) { |
| 411 | xmlGenericError(xmlGenericErrorContext, |
| 412 | "xmlSaveUri: out of memory\n"); |
| 413 | return(NULL); |
| 414 | } |
| 415 | } |
| 416 | ret[len++] = '/'; |
| 417 | ret[len++] = '/'; |
| 418 | } |
| 419 | if (uri->path != NULL) { |
| 420 | p = uri->path; |
| 421 | while (*p != 0) { |
| 422 | if (len + 3 >= max) { |
| 423 | max *= 2; |
| 424 | ret = (xmlChar *) xmlRealloc(ret, |
| 425 | (max + 1) * sizeof(xmlChar)); |
| 426 | if (ret == NULL) { |
| 427 | xmlGenericError(xmlGenericErrorContext, |
| 428 | "xmlSaveUri: out of memory\n"); |
| 429 | return(NULL); |
| 430 | } |
| 431 | } |
| 432 | if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) || |
| 433 | ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || |
| 434 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || |
| 435 | ((*(p) == ','))) |
| 436 | ret[len++] = *p++; |
| 437 | else { |
| 438 | int val = *(unsigned char *)p++; |
| 439 | int hi = val / 0x10, lo = val % 0x10; |
| 440 | ret[len++] = '%'; |
| 441 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
| 442 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | if (uri->query != NULL) { |
| 447 | if (len + 3 >= max) { |
| 448 | max *= 2; |
| 449 | ret = (xmlChar *) xmlRealloc(ret, |
| 450 | (max + 1) * sizeof(xmlChar)); |
| 451 | if (ret == NULL) { |
| 452 | xmlGenericError(xmlGenericErrorContext, |
| 453 | "xmlSaveUri: out of memory\n"); |
| 454 | return(NULL); |
| 455 | } |
| 456 | } |
| 457 | ret[len++] = '?'; |
| 458 | p = uri->query; |
| 459 | while (*p != 0) { |
| 460 | if (len + 3 >= max) { |
| 461 | max *= 2; |
| 462 | ret = (xmlChar *) xmlRealloc(ret, |
| 463 | (max + 1) * sizeof(xmlChar)); |
| 464 | if (ret == NULL) { |
| 465 | xmlGenericError(xmlGenericErrorContext, |
| 466 | "xmlSaveUri: out of memory\n"); |
| 467 | return(NULL); |
| 468 | } |
| 469 | } |
| 470 | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) |
| 471 | ret[len++] = *p++; |
| 472 | else { |
| 473 | int val = *(unsigned char *)p++; |
| 474 | int hi = val / 0x10, lo = val % 0x10; |
| 475 | ret[len++] = '%'; |
| 476 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
| 477 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
| 478 | } |
| 479 | } |
| 480 | } |
Daniel Veillard | fdd27d2 | 2002-11-28 11:55:38 +0000 | [diff] [blame] | 481 | } |
| 482 | if (uri->fragment != NULL) { |
| 483 | if (len + 3 >= max) { |
| 484 | max *= 2; |
| 485 | ret = (xmlChar *) xmlRealloc(ret, |
| 486 | (max + 1) * sizeof(xmlChar)); |
| 487 | if (ret == NULL) { |
| 488 | xmlGenericError(xmlGenericErrorContext, |
| 489 | "xmlSaveUri: out of memory\n"); |
| 490 | return(NULL); |
| 491 | } |
| 492 | } |
| 493 | ret[len++] = '#'; |
| 494 | p = uri->fragment; |
| 495 | while (*p != 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 496 | if (len + 3 >= max) { |
| 497 | max *= 2; |
| 498 | ret = (xmlChar *) xmlRealloc(ret, |
| 499 | (max + 1) * sizeof(xmlChar)); |
| 500 | if (ret == NULL) { |
| 501 | xmlGenericError(xmlGenericErrorContext, |
| 502 | "xmlSaveUri: out of memory\n"); |
| 503 | return(NULL); |
| 504 | } |
| 505 | } |
Daniel Veillard | fdd27d2 | 2002-11-28 11:55:38 +0000 | [diff] [blame] | 506 | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) |
| 507 | ret[len++] = *p++; |
| 508 | else { |
| 509 | int val = *(unsigned char *)p++; |
| 510 | int hi = val / 0x10, lo = val % 0x10; |
| 511 | ret[len++] = '%'; |
| 512 | ret[len++] = hi + (hi > 9? 'A'-10 : '0'); |
| 513 | ret[len++] = lo + (lo > 9? 'A'-10 : '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 516 | } |
Daniel Veillard | fdd27d2 | 2002-11-28 11:55:38 +0000 | [diff] [blame] | 517 | if (len >= max) { |
| 518 | max *= 2; |
| 519 | ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 520 | if (ret == NULL) { |
| 521 | xmlGenericError(xmlGenericErrorContext, |
| 522 | "xmlSaveUri: out of memory\n"); |
| 523 | return(NULL); |
| 524 | } |
| 525 | } |
| 526 | ret[len++] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 527 | return(ret); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * xmlPrintURI: |
| 532 | * @stream: a FILE* for the output |
| 533 | * @uri: pointer to an xmlURI |
| 534 | * |
| 535 | * Prints the URI in the stream @steam. |
| 536 | */ |
| 537 | void |
| 538 | xmlPrintURI(FILE *stream, xmlURIPtr uri) { |
| 539 | xmlChar *out; |
| 540 | |
| 541 | out = xmlSaveUri(uri); |
| 542 | if (out != NULL) { |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 543 | fprintf(stream, "%s", (char *) out); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 544 | xmlFree(out); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * xmlCleanURI: |
| 550 | * @uri: pointer to an xmlURI |
| 551 | * |
| 552 | * Make sure the xmlURI struct is free of content |
| 553 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 554 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 555 | xmlCleanURI(xmlURIPtr uri) { |
| 556 | if (uri == NULL) return; |
| 557 | |
| 558 | if (uri->scheme != NULL) xmlFree(uri->scheme); |
| 559 | uri->scheme = NULL; |
| 560 | if (uri->server != NULL) xmlFree(uri->server); |
| 561 | uri->server = NULL; |
| 562 | if (uri->user != NULL) xmlFree(uri->user); |
| 563 | uri->user = NULL; |
| 564 | if (uri->path != NULL) xmlFree(uri->path); |
| 565 | uri->path = NULL; |
| 566 | if (uri->fragment != NULL) xmlFree(uri->fragment); |
| 567 | uri->fragment = NULL; |
| 568 | if (uri->opaque != NULL) xmlFree(uri->opaque); |
| 569 | uri->opaque = NULL; |
| 570 | if (uri->authority != NULL) xmlFree(uri->authority); |
| 571 | uri->authority = NULL; |
| 572 | if (uri->query != NULL) xmlFree(uri->query); |
| 573 | uri->query = NULL; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * xmlFreeURI: |
| 578 | * @uri: pointer to an xmlURI |
| 579 | * |
| 580 | * Free up the xmlURI struct |
| 581 | */ |
| 582 | void |
| 583 | xmlFreeURI(xmlURIPtr uri) { |
| 584 | if (uri == NULL) return; |
| 585 | |
| 586 | if (uri->scheme != NULL) xmlFree(uri->scheme); |
| 587 | if (uri->server != NULL) xmlFree(uri->server); |
| 588 | if (uri->user != NULL) xmlFree(uri->user); |
| 589 | if (uri->path != NULL) xmlFree(uri->path); |
| 590 | if (uri->fragment != NULL) xmlFree(uri->fragment); |
| 591 | if (uri->opaque != NULL) xmlFree(uri->opaque); |
| 592 | if (uri->authority != NULL) xmlFree(uri->authority); |
| 593 | if (uri->query != NULL) xmlFree(uri->query); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 594 | xmlFree(uri); |
| 595 | } |
| 596 | |
| 597 | /************************************************************************ |
| 598 | * * |
| 599 | * Helper functions * |
| 600 | * * |
| 601 | ************************************************************************/ |
| 602 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 603 | /** |
| 604 | * xmlNormalizeURIPath: |
| 605 | * @path: pointer to the path string |
| 606 | * |
| 607 | * Applies the 5 normalization steps to a path string--that is, RFC 2396 |
| 608 | * Section 5.2, steps 6.c through 6.g. |
| 609 | * |
| 610 | * Normalization occurs directly on the string, no new allocation is done |
| 611 | * |
| 612 | * Returns 0 or an error code |
| 613 | */ |
| 614 | int |
| 615 | xmlNormalizeURIPath(char *path) { |
| 616 | char *cur, *out; |
| 617 | |
| 618 | if (path == NULL) |
| 619 | return(-1); |
| 620 | |
| 621 | /* Skip all initial "/" chars. We want to get to the beginning of the |
| 622 | * first non-empty segment. |
| 623 | */ |
| 624 | cur = path; |
| 625 | while (cur[0] == '/') |
| 626 | ++cur; |
| 627 | if (cur[0] == '\0') |
| 628 | return(0); |
| 629 | |
| 630 | /* Keep everything we've seen so far. */ |
| 631 | out = cur; |
| 632 | |
| 633 | /* |
| 634 | * Analyze each segment in sequence for cases (c) and (d). |
| 635 | */ |
| 636 | while (cur[0] != '\0') { |
| 637 | /* |
| 638 | * c) All occurrences of "./", where "." is a complete path segment, |
| 639 | * are removed from the buffer string. |
| 640 | */ |
| 641 | if ((cur[0] == '.') && (cur[1] == '/')) { |
| 642 | cur += 2; |
Daniel Veillard | fcbd74a | 2001-06-26 07:47:23 +0000 | [diff] [blame] | 643 | /* '//' normalization should be done at this point too */ |
| 644 | while (cur[0] == '/') |
| 645 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 646 | continue; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * d) If the buffer string ends with "." as a complete path segment, |
| 651 | * that "." is removed. |
| 652 | */ |
| 653 | if ((cur[0] == '.') && (cur[1] == '\0')) |
| 654 | break; |
| 655 | |
| 656 | /* Otherwise keep the segment. */ |
| 657 | while (cur[0] != '/') { |
| 658 | if (cur[0] == '\0') |
| 659 | goto done_cd; |
| 660 | (out++)[0] = (cur++)[0]; |
| 661 | } |
Daniel Veillard | fcbd74a | 2001-06-26 07:47:23 +0000 | [diff] [blame] | 662 | /* nomalize // */ |
| 663 | while ((cur[0] == '/') && (cur[1] == '/')) |
| 664 | cur++; |
| 665 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 666 | (out++)[0] = (cur++)[0]; |
| 667 | } |
| 668 | done_cd: |
| 669 | out[0] = '\0'; |
| 670 | |
| 671 | /* Reset to the beginning of the first segment for the next sequence. */ |
| 672 | cur = path; |
| 673 | while (cur[0] == '/') |
| 674 | ++cur; |
| 675 | if (cur[0] == '\0') |
| 676 | return(0); |
| 677 | |
| 678 | /* |
| 679 | * Analyze each segment in sequence for cases (e) and (f). |
| 680 | * |
| 681 | * e) All occurrences of "<segment>/../", where <segment> is a |
| 682 | * complete path segment not equal to "..", are removed from the |
| 683 | * buffer string. Removal of these path segments is performed |
| 684 | * iteratively, removing the leftmost matching pattern on each |
| 685 | * iteration, until no matching pattern remains. |
| 686 | * |
| 687 | * f) If the buffer string ends with "<segment>/..", where <segment> |
| 688 | * is a complete path segment not equal to "..", that |
| 689 | * "<segment>/.." is removed. |
| 690 | * |
| 691 | * To satisfy the "iterative" clause in (e), we need to collapse the |
| 692 | * string every time we find something that needs to be removed. Thus, |
| 693 | * we don't need to keep two pointers into the string: we only need a |
| 694 | * "current position" pointer. |
| 695 | */ |
| 696 | while (1) { |
| 697 | char *segp; |
| 698 | |
| 699 | /* At the beginning of each iteration of this loop, "cur" points to |
| 700 | * the first character of the segment we want to examine. |
| 701 | */ |
| 702 | |
| 703 | /* Find the end of the current segment. */ |
| 704 | segp = cur; |
| 705 | while ((segp[0] != '/') && (segp[0] != '\0')) |
| 706 | ++segp; |
| 707 | |
| 708 | /* If this is the last segment, we're done (we need at least two |
| 709 | * segments to meet the criteria for the (e) and (f) cases). |
| 710 | */ |
| 711 | if (segp[0] == '\0') |
| 712 | break; |
| 713 | |
| 714 | /* If the first segment is "..", or if the next segment _isn't_ "..", |
| 715 | * keep this segment and try the next one. |
| 716 | */ |
| 717 | ++segp; |
| 718 | if (((cur[0] == '.') && (cur[1] == '.') && (segp == cur+3)) |
| 719 | || ((segp[0] != '.') || (segp[1] != '.') |
| 720 | || ((segp[2] != '/') && (segp[2] != '\0')))) { |
| 721 | cur = segp; |
| 722 | continue; |
| 723 | } |
| 724 | |
| 725 | /* If we get here, remove this segment and the next one and back up |
| 726 | * to the previous segment (if there is one), to implement the |
| 727 | * "iteratively" clause. It's pretty much impossible to back up |
| 728 | * while maintaining two pointers into the buffer, so just compact |
| 729 | * the whole buffer now. |
| 730 | */ |
| 731 | |
| 732 | /* If this is the end of the buffer, we're done. */ |
| 733 | if (segp[2] == '\0') { |
| 734 | cur[0] = '\0'; |
| 735 | break; |
| 736 | } |
| 737 | strcpy(cur, segp + 3); |
| 738 | |
| 739 | /* If there are no previous segments, then keep going from here. */ |
| 740 | segp = cur; |
| 741 | while ((segp > path) && ((--segp)[0] == '/')) |
| 742 | ; |
| 743 | if (segp == path) |
| 744 | continue; |
| 745 | |
| 746 | /* "segp" is pointing to the end of a previous segment; find it's |
| 747 | * start. We need to back up to the previous segment and start |
| 748 | * over with that to handle things like "foo/bar/../..". If we |
| 749 | * don't do this, then on the first pass we'll remove the "bar/..", |
| 750 | * but be pointing at the second ".." so we won't realize we can also |
| 751 | * remove the "foo/..". |
| 752 | */ |
| 753 | cur = segp; |
| 754 | while ((cur > path) && (cur[-1] != '/')) |
| 755 | --cur; |
| 756 | } |
| 757 | out[0] = '\0'; |
| 758 | |
| 759 | /* |
| 760 | * g) If the resulting buffer string still begins with one or more |
| 761 | * complete path segments of "..", then the reference is |
| 762 | * considered to be in error. Implementations may handle this |
| 763 | * error by retaining these components in the resolved path (i.e., |
| 764 | * treating them as part of the final URI), by removing them from |
| 765 | * the resolved path (i.e., discarding relative levels above the |
| 766 | * root), or by avoiding traversal of the reference. |
| 767 | * |
| 768 | * We discard them from the final path. |
| 769 | */ |
| 770 | if (path[0] == '/') { |
| 771 | cur = path; |
| 772 | while ((cur[1] == '.') && (cur[2] == '.') |
| 773 | && ((cur[3] == '/') || (cur[3] == '\0'))) |
| 774 | cur += 3; |
| 775 | |
| 776 | if (cur != path) { |
| 777 | out = path; |
| 778 | while (cur[0] != '\0') |
| 779 | (out++)[0] = (cur++)[0]; |
| 780 | out[0] = 0; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return(0); |
| 785 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 786 | |
| 787 | /** |
| 788 | * xmlURIUnescapeString: |
| 789 | * @str: the string to unescape |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 790 | * @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] | 791 | * @target: optional destination buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 792 | * |
| 793 | * Unescaping routine, does not do validity checks ! |
| 794 | * Output is direct unsigned char translation of %XX values (no encoding) |
| 795 | * |
| 796 | * Returns an copy of the string, but unescaped |
| 797 | */ |
| 798 | char * |
| 799 | xmlURIUnescapeString(const char *str, int len, char *target) { |
| 800 | char *ret, *out; |
| 801 | const char *in; |
| 802 | |
| 803 | if (str == NULL) |
| 804 | return(NULL); |
| 805 | if (len <= 0) len = strlen(str); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 806 | if (len < 0) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 807 | |
| 808 | if (target == NULL) { |
| 809 | ret = (char *) xmlMalloc(len + 1); |
| 810 | if (ret == NULL) { |
| 811 | xmlGenericError(xmlGenericErrorContext, |
| 812 | "xmlURIUnescapeString: out of memory\n"); |
| 813 | return(NULL); |
| 814 | } |
| 815 | } else |
| 816 | ret = target; |
| 817 | in = str; |
| 818 | out = ret; |
| 819 | while(len > 0) { |
| 820 | if (*in == '%') { |
| 821 | in++; |
| 822 | if ((*in >= '0') && (*in <= '9')) |
| 823 | *out = (*in - '0'); |
| 824 | else if ((*in >= 'a') && (*in <= 'f')) |
| 825 | *out = (*in - 'a') + 10; |
| 826 | else if ((*in >= 'A') && (*in <= 'F')) |
| 827 | *out = (*in - 'A') + 10; |
| 828 | in++; |
| 829 | if ((*in >= '0') && (*in <= '9')) |
| 830 | *out = *out * 16 + (*in - '0'); |
| 831 | else if ((*in >= 'a') && (*in <= 'f')) |
| 832 | *out = *out * 16 + (*in - 'a') + 10; |
| 833 | else if ((*in >= 'A') && (*in <= 'F')) |
| 834 | *out = *out * 16 + (*in - 'A') + 10; |
| 835 | in++; |
| 836 | len -= 3; |
| 837 | out++; |
| 838 | } else { |
| 839 | *out++ = *in++; |
| 840 | len--; |
| 841 | } |
| 842 | } |
| 843 | *out = 0; |
| 844 | return(ret); |
| 845 | } |
| 846 | |
| 847 | /** |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 848 | * xmlURIEscapeStr: |
| 849 | * @str: string to escape |
| 850 | * @list: exception list string of chars not to escape |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 851 | * |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 852 | * This routine escapes a string to hex, ignoring reserved characters (a-z) |
| 853 | * and the characters in the exception list. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 854 | * |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 855 | * Returns a new escaped string or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 856 | */ |
| 857 | xmlChar * |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 858 | xmlURIEscapeStr(const xmlChar *str, const xmlChar *list) { |
| 859 | xmlChar *ret, ch; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 860 | const xmlChar *in; |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 861 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 862 | unsigned int len, out; |
| 863 | |
| 864 | if (str == NULL) |
| 865 | return(NULL); |
| 866 | len = xmlStrlen(str); |
Daniel Veillard | e645e8c | 2002-10-22 17:35:37 +0000 | [diff] [blame] | 867 | if (!(len > 0)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 868 | |
| 869 | len += 20; |
| 870 | ret = (xmlChar *) xmlMalloc(len); |
| 871 | if (ret == NULL) { |
| 872 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 873 | "xmlURIEscapeStr: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 874 | return(NULL); |
| 875 | } |
| 876 | in = (const xmlChar *) str; |
| 877 | out = 0; |
| 878 | while(*in != 0) { |
| 879 | if (len - out <= 3) { |
| 880 | len += 20; |
| 881 | ret = (xmlChar *) xmlRealloc(ret, len); |
| 882 | if (ret == NULL) { |
| 883 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 884 | "xmlURIEscapeStr: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 885 | return(NULL); |
| 886 | } |
| 887 | } |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 888 | |
| 889 | ch = *in; |
| 890 | |
Daniel Veillard | eb475a3 | 2002-04-14 22:00:22 +0000 | [diff] [blame] | 891 | if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!xmlStrchr(list, ch))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 892 | unsigned char val; |
| 893 | ret[out++] = '%'; |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 894 | val = ch >> 4; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 895 | if (val <= 9) |
| 896 | ret[out++] = '0' + val; |
| 897 | else |
| 898 | ret[out++] = 'A' + val - 0xA; |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 899 | val = ch & 0xF; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 900 | if (val <= 9) |
| 901 | ret[out++] = '0' + val; |
| 902 | else |
| 903 | ret[out++] = 'A' + val - 0xA; |
| 904 | in++; |
| 905 | } else { |
| 906 | ret[out++] = *in++; |
| 907 | } |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 908 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 909 | } |
| 910 | ret[out] = 0; |
| 911 | return(ret); |
| 912 | } |
| 913 | |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 914 | /** |
| 915 | * xmlURIEscape: |
| 916 | * @str: the string of the URI to escape |
| 917 | * |
| 918 | * Escaping routine, does not do validity checks ! |
| 919 | * It will try to escape the chars needing this, but this is heuristic |
| 920 | * based it's impossible to be sure. |
| 921 | * |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 922 | * Returns an copy of the string, but escaped |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 923 | * |
| 924 | * 25 May 2001 |
| 925 | * Uses xmlParseURI and xmlURIEscapeStr to try to escape correctly |
| 926 | * according to RFC2396. |
| 927 | * - Carl Douglas |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 928 | */ |
| 929 | xmlChar * |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 930 | xmlURIEscape(const xmlChar * str) |
| 931 | { |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 932 | xmlChar *ret, *segment = NULL; |
| 933 | xmlURIPtr uri; |
Daniel Veillard | bb6808e | 2001-10-29 23:59:27 +0000 | [diff] [blame] | 934 | int ret2; |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 935 | |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 936 | #define NULLCHK(p) if(!p) { \ |
| 937 | xmlGenericError(xmlGenericErrorContext, \ |
| 938 | "xmlURIEscape: out of memory\n"); \ |
| 939 | return NULL; } |
| 940 | |
Daniel Veillard | bb6808e | 2001-10-29 23:59:27 +0000 | [diff] [blame] | 941 | if (str == NULL) |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 942 | return (NULL); |
Daniel Veillard | bb6808e | 2001-10-29 23:59:27 +0000 | [diff] [blame] | 943 | |
| 944 | uri = xmlCreateURI(); |
| 945 | if (uri != NULL) { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 946 | /* |
| 947 | * Allow escaping errors in the unescaped form |
| 948 | */ |
| 949 | uri->cleanup = 1; |
| 950 | ret2 = xmlParseURIReference(uri, (const char *)str); |
Daniel Veillard | bb6808e | 2001-10-29 23:59:27 +0000 | [diff] [blame] | 951 | if (ret2) { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 952 | xmlFreeURI(uri); |
| 953 | return (NULL); |
| 954 | } |
Daniel Veillard | bb6808e | 2001-10-29 23:59:27 +0000 | [diff] [blame] | 955 | } |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 956 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 957 | if (!uri) |
| 958 | return NULL; |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 959 | |
| 960 | ret = NULL; |
| 961 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 962 | if (uri->scheme) { |
| 963 | segment = xmlURIEscapeStr(BAD_CAST uri->scheme, BAD_CAST "+-."); |
| 964 | NULLCHK(segment) |
| 965 | ret = xmlStrcat(ret, segment); |
| 966 | ret = xmlStrcat(ret, BAD_CAST ":"); |
| 967 | xmlFree(segment); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 970 | if (uri->authority) { |
| 971 | segment = |
| 972 | xmlURIEscapeStr(BAD_CAST uri->authority, BAD_CAST "/?;:@"); |
| 973 | NULLCHK(segment) |
| 974 | ret = xmlStrcat(ret, BAD_CAST "//"); |
| 975 | ret = xmlStrcat(ret, segment); |
| 976 | xmlFree(segment); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 979 | if (uri->user) { |
| 980 | segment = xmlURIEscapeStr(BAD_CAST uri->user, BAD_CAST ";:&=+$,"); |
| 981 | NULLCHK(segment) |
| 982 | ret = xmlStrcat(ret, segment); |
| 983 | ret = xmlStrcat(ret, BAD_CAST "@"); |
| 984 | xmlFree(segment); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 987 | if (uri->server) { |
| 988 | segment = xmlURIEscapeStr(BAD_CAST uri->server, BAD_CAST "/?;:@"); |
| 989 | NULLCHK(segment) |
| 990 | ret = xmlStrcat(ret, BAD_CAST "//"); |
| 991 | ret = xmlStrcat(ret, segment); |
| 992 | xmlFree(segment); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 995 | if (uri->port) { |
| 996 | xmlChar port[10]; |
| 997 | |
Daniel Veillard | 43d3f61 | 2001-11-10 11:57:23 +0000 | [diff] [blame] | 998 | snprintf((char *) port, 10, "%d", uri->port); |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 999 | ret = xmlStrcat(ret, BAD_CAST ":"); |
| 1000 | ret = xmlStrcat(ret, port); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1003 | if (uri->path) { |
| 1004 | segment = |
| 1005 | xmlURIEscapeStr(BAD_CAST uri->path, BAD_CAST ":@&=+$,/?;"); |
| 1006 | NULLCHK(segment) |
| 1007 | ret = xmlStrcat(ret, segment); |
| 1008 | xmlFree(segment); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1011 | if (uri->query) { |
| 1012 | segment = |
| 1013 | xmlURIEscapeStr(BAD_CAST uri->query, BAD_CAST ";/?:@&=+,$"); |
| 1014 | NULLCHK(segment) |
| 1015 | ret = xmlStrcat(ret, BAD_CAST "?"); |
| 1016 | ret = xmlStrcat(ret, segment); |
| 1017 | xmlFree(segment); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1020 | if (uri->opaque) { |
| 1021 | segment = xmlURIEscapeStr(BAD_CAST uri->opaque, BAD_CAST ""); |
| 1022 | NULLCHK(segment) |
| 1023 | ret = xmlStrcat(ret, segment); |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1024 | xmlFree(segment); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1027 | if (uri->fragment) { |
| 1028 | segment = xmlURIEscapeStr(BAD_CAST uri->fragment, BAD_CAST "#"); |
| 1029 | NULLCHK(segment) |
| 1030 | ret = xmlStrcat(ret, BAD_CAST "#"); |
| 1031 | ret = xmlStrcat(ret, segment); |
| 1032 | xmlFree(segment); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 1033 | } |
Daniel Veillard | 43d3f61 | 2001-11-10 11:57:23 +0000 | [diff] [blame] | 1034 | |
| 1035 | xmlFreeURI(uri); |
Daniel Veillard | 6278fb5 | 2001-05-25 07:38:41 +0000 | [diff] [blame] | 1036 | #undef NULLCHK |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 1037 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1038 | return (ret); |
Daniel Veillard | 8514c67 | 2001-05-23 10:29:12 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1041 | /************************************************************************ |
| 1042 | * * |
| 1043 | * Escaped URI parsing * |
| 1044 | * * |
| 1045 | ************************************************************************/ |
| 1046 | |
| 1047 | /** |
| 1048 | * xmlParseURIFragment: |
| 1049 | * @uri: pointer to an URI structure |
| 1050 | * @str: pointer to the string to analyze |
| 1051 | * |
| 1052 | * Parse an URI fragment string and fills in the appropriate fields |
| 1053 | * of the @uri structure. |
| 1054 | * |
| 1055 | * fragment = *uric |
| 1056 | * |
| 1057 | * Returns 0 or the error code |
| 1058 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1059 | static int |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1060 | xmlParseURIFragment(xmlURIPtr uri, const char **str) |
| 1061 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1062 | const char *cur = *str; |
| 1063 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1064 | if (str == NULL) |
| 1065 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1066 | |
Daniel Veillard | fdd27d2 | 2002-11-28 11:55:38 +0000 | [diff] [blame] | 1067 | while (IS_URIC(cur) || IS_UNWISE(cur)) |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1068 | NEXT(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1069 | if (uri != NULL) { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1070 | if (uri->fragment != NULL) |
| 1071 | xmlFree(uri->fragment); |
| 1072 | uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1073 | } |
| 1074 | *str = cur; |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1075 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /** |
| 1079 | * xmlParseURIQuery: |
| 1080 | * @uri: pointer to an URI structure |
| 1081 | * @str: pointer to the string to analyze |
| 1082 | * |
| 1083 | * Parse the query part of an URI |
| 1084 | * |
| 1085 | * query = *uric |
| 1086 | * |
| 1087 | * Returns 0 or the error code |
| 1088 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1089 | static int |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1090 | xmlParseURIQuery(xmlURIPtr uri, const char **str) |
| 1091 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1092 | const char *cur = *str; |
| 1093 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1094 | if (str == NULL) |
| 1095 | return (-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1096 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1097 | while (IS_URIC(cur) || ((uri->cleanup) && (IS_UNWISE(cur)))) |
| 1098 | NEXT(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1099 | if (uri != NULL) { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1100 | if (uri->query != NULL) |
| 1101 | xmlFree(uri->query); |
| 1102 | uri->query = xmlURIUnescapeString(*str, cur - *str, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1103 | } |
| 1104 | *str = cur; |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1105 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /** |
| 1109 | * xmlParseURIScheme: |
| 1110 | * @uri: pointer to an URI structure |
| 1111 | * @str: pointer to the string to analyze |
| 1112 | * |
| 1113 | * Parse an URI scheme |
| 1114 | * |
| 1115 | * scheme = alpha *( alpha | digit | "+" | "-" | "." ) |
| 1116 | * |
| 1117 | * Returns 0 or the error code |
| 1118 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1119 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1120 | xmlParseURIScheme(xmlURIPtr uri, const char **str) { |
| 1121 | const char *cur; |
| 1122 | |
| 1123 | if (str == NULL) |
| 1124 | return(-1); |
| 1125 | |
| 1126 | cur = *str; |
| 1127 | if (!IS_ALPHA(*cur)) |
| 1128 | return(2); |
| 1129 | cur++; |
| 1130 | while (IS_SCHEME(*cur)) cur++; |
| 1131 | if (uri != NULL) { |
| 1132 | if (uri->scheme != NULL) xmlFree(uri->scheme); |
| 1133 | /* !!! strndup */ |
| 1134 | uri->scheme = xmlURIUnescapeString(*str, cur - *str, NULL); |
| 1135 | } |
| 1136 | *str = cur; |
| 1137 | return(0); |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * xmlParseURIOpaquePart: |
| 1142 | * @uri: pointer to an URI structure |
| 1143 | * @str: pointer to the string to analyze |
| 1144 | * |
| 1145 | * Parse an URI opaque part |
| 1146 | * |
| 1147 | * opaque_part = uric_no_slash *uric |
| 1148 | * |
| 1149 | * Returns 0 or the error code |
| 1150 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1151 | static int |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1152 | xmlParseURIOpaquePart(xmlURIPtr uri, const char **str) |
| 1153 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1154 | const char *cur; |
| 1155 | |
| 1156 | if (str == NULL) |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1157 | return (-1); |
| 1158 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1159 | cur = *str; |
Daniel Veillard | bb6808e | 2001-10-29 23:59:27 +0000 | [diff] [blame] | 1160 | if (!(IS_URIC_NO_SLASH(cur) || ((uri->cleanup) && (IS_UNWISE(cur))))) { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1161 | return (3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1162 | } |
| 1163 | NEXT(cur); |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1164 | while (IS_URIC(cur) || ((uri->cleanup) && (IS_UNWISE(cur)))) |
| 1165 | NEXT(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1166 | if (uri != NULL) { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1167 | if (uri->opaque != NULL) |
| 1168 | xmlFree(uri->opaque); |
| 1169 | uri->opaque = xmlURIUnescapeString(*str, cur - *str, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1170 | } |
| 1171 | *str = cur; |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1172 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * xmlParseURIServer: |
| 1177 | * @uri: pointer to an URI structure |
| 1178 | * @str: pointer to the string to analyze |
| 1179 | * |
| 1180 | * Parse a server subpart of an URI, it's a finer grain analysis |
| 1181 | * of the authority part. |
| 1182 | * |
| 1183 | * server = [ [ userinfo "@" ] hostport ] |
| 1184 | * userinfo = *( unreserved | escaped | |
| 1185 | * ";" | ":" | "&" | "=" | "+" | "$" | "," ) |
| 1186 | * hostport = host [ ":" port ] |
| 1187 | * host = hostname | IPv4address |
| 1188 | * hostname = *( domainlabel "." ) toplabel [ "." ] |
| 1189 | * domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum |
| 1190 | * toplabel = alpha | alpha *( alphanum | "-" ) alphanum |
| 1191 | * IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit |
| 1192 | * port = *digit |
| 1193 | * |
| 1194 | * Returns 0 or the error code |
| 1195 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1196 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1197 | xmlParseURIServer(xmlURIPtr uri, const char **str) { |
| 1198 | const char *cur; |
| 1199 | const char *host, *tmp; |
| 1200 | |
| 1201 | if (str == NULL) |
| 1202 | return(-1); |
| 1203 | |
| 1204 | cur = *str; |
| 1205 | |
| 1206 | /* |
| 1207 | * is there an userinfo ? |
| 1208 | */ |
| 1209 | while (IS_USERINFO(cur)) NEXT(cur); |
| 1210 | if (*cur == '@') { |
| 1211 | if (uri != NULL) { |
| 1212 | if (uri->user != NULL) xmlFree(uri->user); |
| 1213 | uri->user = xmlURIUnescapeString(*str, cur - *str, NULL); |
| 1214 | } |
| 1215 | cur++; |
| 1216 | } else { |
| 1217 | if (uri != NULL) { |
| 1218 | if (uri->user != NULL) xmlFree(uri->user); |
| 1219 | uri->user = NULL; |
| 1220 | } |
| 1221 | cur = *str; |
| 1222 | } |
| 1223 | /* |
| 1224 | * This can be empty in the case where there is no server |
| 1225 | */ |
| 1226 | host = cur; |
| 1227 | if (*cur == '/') { |
| 1228 | if (uri != NULL) { |
| 1229 | if (uri->authority != NULL) xmlFree(uri->authority); |
| 1230 | uri->authority = NULL; |
| 1231 | if (uri->server != NULL) xmlFree(uri->server); |
| 1232 | uri->server = NULL; |
| 1233 | uri->port = 0; |
| 1234 | } |
| 1235 | return(0); |
| 1236 | } |
| 1237 | /* |
| 1238 | * host part of hostport can derive either an IPV4 address |
| 1239 | * or an unresolved name. Check the IP first, it easier to detect |
| 1240 | * errors if wrong one |
| 1241 | */ |
| 1242 | if (IS_DIGIT(*cur)) { |
| 1243 | while(IS_DIGIT(*cur)) cur++; |
| 1244 | if (*cur != '.') |
| 1245 | goto host_name; |
| 1246 | cur++; |
| 1247 | if (!IS_DIGIT(*cur)) |
| 1248 | goto host_name; |
| 1249 | while(IS_DIGIT(*cur)) cur++; |
| 1250 | if (*cur != '.') |
| 1251 | goto host_name; |
| 1252 | cur++; |
| 1253 | if (!IS_DIGIT(*cur)) |
| 1254 | goto host_name; |
| 1255 | while(IS_DIGIT(*cur)) cur++; |
| 1256 | if (*cur != '.') |
| 1257 | goto host_name; |
| 1258 | cur++; |
| 1259 | if (!IS_DIGIT(*cur)) |
| 1260 | goto host_name; |
| 1261 | while(IS_DIGIT(*cur)) cur++; |
| 1262 | if (uri != NULL) { |
| 1263 | if (uri->authority != NULL) xmlFree(uri->authority); |
| 1264 | uri->authority = NULL; |
| 1265 | if (uri->server != NULL) xmlFree(uri->server); |
| 1266 | uri->server = xmlURIUnescapeString(host, cur - host, NULL); |
| 1267 | } |
| 1268 | goto host_done; |
| 1269 | } |
| 1270 | host_name: |
| 1271 | /* |
| 1272 | * the hostname production as-is is a parser nightmare. |
| 1273 | * simplify it to |
| 1274 | * hostname = *( domainlabel "." ) domainlabel [ "." ] |
| 1275 | * and just make sure the last label starts with a non numeric char. |
| 1276 | */ |
| 1277 | if (!IS_ALPHANUM(*cur)) |
| 1278 | return(6); |
| 1279 | while (IS_ALPHANUM(*cur)) { |
| 1280 | while ((IS_ALPHANUM(*cur)) || (*cur == '-')) cur++; |
| 1281 | if (*cur == '.') |
| 1282 | cur++; |
| 1283 | } |
| 1284 | tmp = cur; |
| 1285 | tmp--; |
| 1286 | while (IS_ALPHANUM(*tmp) && (*tmp != '.') && (tmp >= host)) tmp--; |
| 1287 | tmp++; |
| 1288 | if (!IS_ALPHA(*tmp)) |
| 1289 | return(7); |
| 1290 | if (uri != NULL) { |
| 1291 | if (uri->authority != NULL) xmlFree(uri->authority); |
| 1292 | uri->authority = NULL; |
| 1293 | if (uri->server != NULL) xmlFree(uri->server); |
| 1294 | uri->server = xmlURIUnescapeString(host, cur - host, NULL); |
| 1295 | } |
| 1296 | |
| 1297 | host_done: |
| 1298 | |
| 1299 | /* |
| 1300 | * finish by checking for a port presence. |
| 1301 | */ |
| 1302 | if (*cur == ':') { |
| 1303 | cur++; |
| 1304 | if (IS_DIGIT(*cur)) { |
| 1305 | if (uri != NULL) |
| 1306 | uri->port = 0; |
| 1307 | while (IS_DIGIT(*cur)) { |
| 1308 | if (uri != NULL) |
| 1309 | uri->port = uri->port * 10 + (*cur - '0'); |
| 1310 | cur++; |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | *str = cur; |
| 1315 | return(0); |
| 1316 | } |
| 1317 | |
| 1318 | /** |
| 1319 | * xmlParseURIRelSegment: |
| 1320 | * @uri: pointer to an URI structure |
| 1321 | * @str: pointer to the string to analyze |
| 1322 | * |
| 1323 | * Parse an URI relative segment |
| 1324 | * |
| 1325 | * rel_segment = 1*( unreserved | escaped | ";" | "@" | "&" | "=" | |
| 1326 | * "+" | "$" | "," ) |
| 1327 | * |
| 1328 | * Returns 0 or the error code |
| 1329 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1330 | static int |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1331 | xmlParseURIRelSegment(xmlURIPtr uri, const char **str) |
| 1332 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1333 | const char *cur; |
| 1334 | |
| 1335 | if (str == NULL) |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1336 | return (-1); |
| 1337 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1338 | cur = *str; |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1339 | if (!(IS_SEGMENT(cur) || ((uri->cleanup) && (IS_UNWISE(cur))))) { |
| 1340 | return (3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1341 | } |
| 1342 | NEXT(cur); |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1343 | while (IS_SEGMENT(cur) || ((uri->cleanup) && (IS_UNWISE(cur)))) |
| 1344 | NEXT(cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1345 | if (uri != NULL) { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1346 | if (uri->path != NULL) |
| 1347 | xmlFree(uri->path); |
| 1348 | uri->path = xmlURIUnescapeString(*str, cur - *str, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1349 | } |
| 1350 | *str = cur; |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1351 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /** |
| 1355 | * xmlParseURIPathSegments: |
| 1356 | * @uri: pointer to an URI structure |
| 1357 | * @str: pointer to the string to analyze |
| 1358 | * @slash: should we add a leading slash |
| 1359 | * |
| 1360 | * Parse an URI set of path segments |
| 1361 | * |
| 1362 | * path_segments = segment *( "/" segment ) |
| 1363 | * segment = *pchar *( ";" param ) |
| 1364 | * param = *pchar |
| 1365 | * |
| 1366 | * Returns 0 or the error code |
| 1367 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1368 | static int |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1369 | xmlParseURIPathSegments(xmlURIPtr uri, const char **str, int slash) |
| 1370 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1371 | const char *cur; |
| 1372 | |
| 1373 | if (str == NULL) |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1374 | return (-1); |
| 1375 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1376 | cur = *str; |
| 1377 | |
| 1378 | do { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1379 | while (IS_PCHAR(cur) || ((uri->cleanup) && (IS_UNWISE(cur)))) |
| 1380 | NEXT(cur); |
Daniel Veillard | 234bc4e | 2002-05-24 11:03:05 +0000 | [diff] [blame] | 1381 | while (*cur == ';') { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1382 | cur++; |
| 1383 | while (IS_PCHAR(cur) || ((uri->cleanup) && (IS_UNWISE(cur)))) |
| 1384 | NEXT(cur); |
| 1385 | } |
| 1386 | if (*cur != '/') |
| 1387 | break; |
| 1388 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1389 | } while (1); |
| 1390 | if (uri != NULL) { |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1391 | int len, len2 = 0; |
| 1392 | char *path; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1393 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1394 | /* |
| 1395 | * Concat the set of path segments to the current path |
| 1396 | */ |
| 1397 | len = cur - *str; |
| 1398 | if (slash) |
| 1399 | len++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1400 | |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1401 | if (uri->path != NULL) { |
| 1402 | len2 = strlen(uri->path); |
| 1403 | len += len2; |
| 1404 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1405 | path = (char *) xmlMalloc(len + 1); |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1406 | if (path == NULL) { |
| 1407 | xmlGenericError(xmlGenericErrorContext, |
| 1408 | "xmlParseURIPathSegments: out of memory\n"); |
| 1409 | *str = cur; |
| 1410 | return (-1); |
| 1411 | } |
| 1412 | if (uri->path != NULL) |
| 1413 | memcpy(path, uri->path, len2); |
| 1414 | if (slash) { |
| 1415 | path[len2] = '/'; |
| 1416 | len2++; |
| 1417 | } |
| 1418 | path[len2] = 0; |
| 1419 | if (cur - *str > 0) |
| 1420 | xmlURIUnescapeString(*str, cur - *str, &path[len2]); |
| 1421 | if (uri->path != NULL) |
| 1422 | xmlFree(uri->path); |
| 1423 | uri->path = path; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1424 | } |
| 1425 | *str = cur; |
Daniel Veillard | 4def3bd | 2001-10-30 09:47:47 +0000 | [diff] [blame] | 1426 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | /** |
| 1430 | * xmlParseURIAuthority: |
| 1431 | * @uri: pointer to an URI structure |
| 1432 | * @str: pointer to the string to analyze |
| 1433 | * |
| 1434 | * Parse the authority part of an URI. |
| 1435 | * |
| 1436 | * authority = server | reg_name |
| 1437 | * server = [ [ userinfo "@" ] hostport ] |
| 1438 | * reg_name = 1*( unreserved | escaped | "$" | "," | ";" | ":" | |
| 1439 | * "@" | "&" | "=" | "+" ) |
| 1440 | * |
| 1441 | * Note : this is completely ambiguous since reg_name is allowed to |
| 1442 | * use the full set of chars in use by server: |
| 1443 | * |
| 1444 | * 3.2.1. Registry-based Naming Authority |
| 1445 | * |
| 1446 | * The structure of a registry-based naming authority is specific |
| 1447 | * to the URI scheme, but constrained to the allowed characters |
| 1448 | * for an authority component. |
| 1449 | * |
| 1450 | * Returns 0 or the error code |
| 1451 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1452 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1453 | xmlParseURIAuthority(xmlURIPtr uri, const char **str) { |
| 1454 | const char *cur; |
| 1455 | int ret; |
| 1456 | |
| 1457 | if (str == NULL) |
| 1458 | return(-1); |
| 1459 | |
| 1460 | cur = *str; |
| 1461 | |
| 1462 | /* |
| 1463 | * try first to parse it as a server string. |
| 1464 | */ |
| 1465 | ret = xmlParseURIServer(uri, str); |
| 1466 | if (ret == 0) |
| 1467 | return(0); |
| 1468 | |
| 1469 | /* |
| 1470 | * failed, fallback to reg_name |
| 1471 | */ |
| 1472 | if (!IS_REG_NAME(cur)) { |
| 1473 | return(5); |
| 1474 | } |
| 1475 | NEXT(cur); |
| 1476 | while (IS_REG_NAME(cur)) NEXT(cur); |
| 1477 | if (uri != NULL) { |
| 1478 | if (uri->server != NULL) xmlFree(uri->server); |
| 1479 | uri->server = NULL; |
| 1480 | if (uri->user != NULL) xmlFree(uri->user); |
| 1481 | uri->user = NULL; |
| 1482 | if (uri->authority != NULL) xmlFree(uri->authority); |
| 1483 | uri->authority = xmlURIUnescapeString(*str, cur - *str, NULL); |
| 1484 | } |
| 1485 | *str = cur; |
| 1486 | return(0); |
| 1487 | } |
| 1488 | |
| 1489 | /** |
| 1490 | * xmlParseURIHierPart: |
| 1491 | * @uri: pointer to an URI structure |
| 1492 | * @str: pointer to the string to analyze |
| 1493 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1494 | * Parse an URI hierarchical part |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1495 | * |
| 1496 | * hier_part = ( net_path | abs_path ) [ "?" query ] |
| 1497 | * abs_path = "/" path_segments |
| 1498 | * net_path = "//" authority [ abs_path ] |
| 1499 | * |
| 1500 | * Returns 0 or the error code |
| 1501 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1502 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1503 | xmlParseURIHierPart(xmlURIPtr uri, const char **str) { |
| 1504 | int ret; |
| 1505 | const char *cur; |
| 1506 | |
| 1507 | if (str == NULL) |
| 1508 | return(-1); |
| 1509 | |
| 1510 | cur = *str; |
| 1511 | |
| 1512 | if ((cur[0] == '/') && (cur[1] == '/')) { |
| 1513 | cur += 2; |
| 1514 | ret = xmlParseURIAuthority(uri, &cur); |
| 1515 | if (ret != 0) |
| 1516 | return(ret); |
| 1517 | if (cur[0] == '/') { |
| 1518 | cur++; |
| 1519 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 1520 | } |
| 1521 | } else if (cur[0] == '/') { |
| 1522 | cur++; |
| 1523 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 1524 | } else { |
| 1525 | return(4); |
| 1526 | } |
| 1527 | if (ret != 0) |
| 1528 | return(ret); |
| 1529 | if (*cur == '?') { |
| 1530 | cur++; |
| 1531 | ret = xmlParseURIQuery(uri, &cur); |
| 1532 | if (ret != 0) |
| 1533 | return(ret); |
| 1534 | } |
| 1535 | *str = cur; |
| 1536 | return(0); |
| 1537 | } |
| 1538 | |
| 1539 | /** |
| 1540 | * xmlParseAbsoluteURI: |
| 1541 | * @uri: pointer to an URI structure |
| 1542 | * @str: pointer to the string to analyze |
| 1543 | * |
| 1544 | * Parse an URI reference string and fills in the appropriate fields |
| 1545 | * of the @uri structure |
| 1546 | * |
| 1547 | * absoluteURI = scheme ":" ( hier_part | opaque_part ) |
| 1548 | * |
| 1549 | * Returns 0 or the error code |
| 1550 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1551 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1552 | xmlParseAbsoluteURI(xmlURIPtr uri, const char **str) { |
| 1553 | int ret; |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 1554 | const char *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1555 | |
| 1556 | if (str == NULL) |
| 1557 | return(-1); |
| 1558 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 1559 | cur = *str; |
| 1560 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1561 | ret = xmlParseURIScheme(uri, str); |
| 1562 | if (ret != 0) return(ret); |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 1563 | if (**str != ':') { |
| 1564 | *str = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1565 | return(1); |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 1566 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1567 | (*str)++; |
| 1568 | if (**str == '/') |
| 1569 | return(xmlParseURIHierPart(uri, str)); |
| 1570 | return(xmlParseURIOpaquePart(uri, str)); |
| 1571 | } |
| 1572 | |
| 1573 | /** |
| 1574 | * xmlParseRelativeURI: |
| 1575 | * @uri: pointer to an URI structure |
| 1576 | * @str: pointer to the string to analyze |
| 1577 | * |
| 1578 | * Parse an relative URI string and fills in the appropriate fields |
| 1579 | * of the @uri structure |
| 1580 | * |
| 1581 | * relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ] |
| 1582 | * abs_path = "/" path_segments |
| 1583 | * net_path = "//" authority [ abs_path ] |
| 1584 | * rel_path = rel_segment [ abs_path ] |
| 1585 | * |
| 1586 | * Returns 0 or the error code |
| 1587 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1588 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1589 | xmlParseRelativeURI(xmlURIPtr uri, const char **str) { |
| 1590 | int ret = 0; |
| 1591 | const char *cur; |
| 1592 | |
| 1593 | if (str == NULL) |
| 1594 | return(-1); |
| 1595 | |
| 1596 | cur = *str; |
| 1597 | if ((cur[0] == '/') && (cur[1] == '/')) { |
| 1598 | cur += 2; |
| 1599 | ret = xmlParseURIAuthority(uri, &cur); |
| 1600 | if (ret != 0) |
| 1601 | return(ret); |
| 1602 | if (cur[0] == '/') { |
| 1603 | cur++; |
| 1604 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 1605 | } |
| 1606 | } else if (cur[0] == '/') { |
| 1607 | cur++; |
| 1608 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 1609 | } else if (cur[0] != '#' && cur[0] != '?') { |
| 1610 | ret = xmlParseURIRelSegment(uri, &cur); |
| 1611 | if (ret != 0) |
| 1612 | return(ret); |
| 1613 | if (cur[0] == '/') { |
| 1614 | cur++; |
| 1615 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 1616 | } |
| 1617 | } |
| 1618 | if (ret != 0) |
| 1619 | return(ret); |
| 1620 | if (*cur == '?') { |
| 1621 | cur++; |
| 1622 | ret = xmlParseURIQuery(uri, &cur); |
| 1623 | if (ret != 0) |
| 1624 | return(ret); |
| 1625 | } |
| 1626 | *str = cur; |
| 1627 | return(ret); |
| 1628 | } |
| 1629 | |
| 1630 | /** |
| 1631 | * xmlParseURIReference: |
| 1632 | * @uri: pointer to an URI structure |
| 1633 | * @str: the string to analyze |
| 1634 | * |
| 1635 | * Parse an URI reference string and fills in the appropriate fields |
| 1636 | * of the @uri structure |
| 1637 | * |
| 1638 | * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ] |
| 1639 | * |
| 1640 | * Returns 0 or the error code |
| 1641 | */ |
| 1642 | int |
| 1643 | xmlParseURIReference(xmlURIPtr uri, const char *str) { |
| 1644 | int ret; |
| 1645 | const char *tmp = str; |
| 1646 | |
| 1647 | if (str == NULL) |
| 1648 | return(-1); |
| 1649 | xmlCleanURI(uri); |
| 1650 | |
| 1651 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1652 | * Try first to parse absolute refs, then fallback to relative if |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1653 | * it fails. |
| 1654 | */ |
| 1655 | ret = xmlParseAbsoluteURI(uri, &str); |
| 1656 | if (ret != 0) { |
| 1657 | xmlCleanURI(uri); |
| 1658 | str = tmp; |
| 1659 | ret = xmlParseRelativeURI(uri, &str); |
| 1660 | } |
| 1661 | if (ret != 0) { |
| 1662 | xmlCleanURI(uri); |
| 1663 | return(ret); |
| 1664 | } |
| 1665 | |
| 1666 | if (*str == '#') { |
| 1667 | str++; |
| 1668 | ret = xmlParseURIFragment(uri, &str); |
| 1669 | if (ret != 0) return(ret); |
| 1670 | } |
| 1671 | if (*str != 0) { |
| 1672 | xmlCleanURI(uri); |
| 1673 | return(1); |
| 1674 | } |
| 1675 | return(0); |
| 1676 | } |
| 1677 | |
| 1678 | /** |
| 1679 | * xmlParseURI: |
| 1680 | * @str: the URI string to analyze |
| 1681 | * |
| 1682 | * Parse an URI |
| 1683 | * |
| 1684 | * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ] |
| 1685 | * |
| 1686 | * Returns a newly build xmlURIPtr or NULL in case of error |
| 1687 | */ |
| 1688 | xmlURIPtr |
| 1689 | xmlParseURI(const char *str) { |
| 1690 | xmlURIPtr uri; |
| 1691 | int ret; |
| 1692 | |
| 1693 | if (str == NULL) |
| 1694 | return(NULL); |
| 1695 | uri = xmlCreateURI(); |
| 1696 | if (uri != NULL) { |
| 1697 | ret = xmlParseURIReference(uri, str); |
| 1698 | if (ret) { |
| 1699 | xmlFreeURI(uri); |
| 1700 | return(NULL); |
| 1701 | } |
| 1702 | } |
| 1703 | return(uri); |
| 1704 | } |
| 1705 | |
| 1706 | /************************************************************************ |
| 1707 | * * |
| 1708 | * Public functions * |
| 1709 | * * |
| 1710 | ************************************************************************/ |
| 1711 | |
| 1712 | /** |
| 1713 | * xmlBuildURI: |
| 1714 | * @URI: the URI instance found in the document |
| 1715 | * @base: the base value |
| 1716 | * |
| 1717 | * Computes he final URI of the reference done by checking that |
| 1718 | * the given URI is valid, and building the final URI using the |
| 1719 | * base URI. This is processed according to section 5.2 of the |
| 1720 | * RFC 2396 |
| 1721 | * |
| 1722 | * 5.2. Resolving Relative References to Absolute Form |
| 1723 | * |
| 1724 | * Returns a new URI string (to be freed by the caller) or NULL in case |
| 1725 | * of error. |
| 1726 | */ |
| 1727 | xmlChar * |
| 1728 | xmlBuildURI(const xmlChar *URI, const xmlChar *base) { |
| 1729 | xmlChar *val = NULL; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1730 | int ret, len, indx, cur, out; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1731 | xmlURIPtr ref = NULL; |
| 1732 | xmlURIPtr bas = NULL; |
| 1733 | xmlURIPtr res = NULL; |
| 1734 | |
| 1735 | /* |
| 1736 | * 1) The URI reference is parsed into the potential four components and |
| 1737 | * fragment identifier, as described in Section 4.3. |
| 1738 | * |
| 1739 | * NOTE that a completely empty URI is treated by modern browsers |
| 1740 | * as a reference to "." rather than as a synonym for the current |
| 1741 | * URI. Should we do that here? |
| 1742 | */ |
| 1743 | if (URI == NULL) |
| 1744 | ret = -1; |
| 1745 | else { |
| 1746 | if (*URI) { |
| 1747 | ref = xmlCreateURI(); |
| 1748 | if (ref == NULL) |
| 1749 | goto done; |
| 1750 | ret = xmlParseURIReference(ref, (const char *) URI); |
| 1751 | } |
| 1752 | else |
| 1753 | ret = 0; |
| 1754 | } |
| 1755 | if (ret != 0) |
| 1756 | goto done; |
Daniel Veillard | 7b4b2f9 | 2003-01-06 13:11:20 +0000 | [diff] [blame] | 1757 | if ((ref != NULL) && (ref->scheme != NULL)) { |
| 1758 | /* |
| 1759 | * The URI is absolute don't modify. |
| 1760 | */ |
| 1761 | val = xmlStrdup(URI); |
| 1762 | goto done; |
| 1763 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1764 | if (base == NULL) |
| 1765 | ret = -1; |
| 1766 | else { |
| 1767 | bas = xmlCreateURI(); |
| 1768 | if (bas == NULL) |
| 1769 | goto done; |
| 1770 | ret = xmlParseURIReference(bas, (const char *) base); |
| 1771 | } |
| 1772 | if (ret != 0) { |
| 1773 | if (ref) |
| 1774 | val = xmlSaveUri(ref); |
| 1775 | goto done; |
| 1776 | } |
| 1777 | if (ref == NULL) { |
| 1778 | /* |
| 1779 | * the base fragment must be ignored |
| 1780 | */ |
| 1781 | if (bas->fragment != NULL) { |
| 1782 | xmlFree(bas->fragment); |
| 1783 | bas->fragment = NULL; |
| 1784 | } |
| 1785 | val = xmlSaveUri(bas); |
| 1786 | goto done; |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * 2) If the path component is empty and the scheme, authority, and |
| 1791 | * query components are undefined, then it is a reference to the |
| 1792 | * current document and we are done. Otherwise, the reference URI's |
| 1793 | * query and fragment components are defined as found (or not found) |
| 1794 | * within the URI reference and not inherited from the base URI. |
| 1795 | * |
| 1796 | * NOTE that in modern browsers, the parsing differs from the above |
| 1797 | * in the following aspect: the query component is allowed to be |
| 1798 | * defined while still treating this as a reference to the current |
| 1799 | * document. |
| 1800 | */ |
| 1801 | res = xmlCreateURI(); |
| 1802 | if (res == NULL) |
| 1803 | goto done; |
| 1804 | if ((ref->scheme == NULL) && (ref->path == NULL) && |
| 1805 | ((ref->authority == NULL) && (ref->server == NULL))) { |
| 1806 | if (bas->scheme != NULL) |
| 1807 | res->scheme = xmlMemStrdup(bas->scheme); |
| 1808 | if (bas->authority != NULL) |
| 1809 | res->authority = xmlMemStrdup(bas->authority); |
| 1810 | else if (bas->server != NULL) { |
| 1811 | res->server = xmlMemStrdup(bas->server); |
| 1812 | if (bas->user != NULL) |
| 1813 | res->user = xmlMemStrdup(bas->user); |
| 1814 | res->port = bas->port; |
| 1815 | } |
| 1816 | if (bas->path != NULL) |
| 1817 | res->path = xmlMemStrdup(bas->path); |
| 1818 | if (ref->query != NULL) |
| 1819 | res->query = xmlMemStrdup(ref->query); |
| 1820 | else if (bas->query != NULL) |
| 1821 | res->query = xmlMemStrdup(bas->query); |
| 1822 | if (ref->fragment != NULL) |
| 1823 | res->fragment = xmlMemStrdup(ref->fragment); |
| 1824 | goto step_7; |
| 1825 | } |
| 1826 | |
| 1827 | if (ref->query != NULL) |
| 1828 | res->query = xmlMemStrdup(ref->query); |
| 1829 | if (ref->fragment != NULL) |
| 1830 | res->fragment = xmlMemStrdup(ref->fragment); |
| 1831 | |
| 1832 | /* |
| 1833 | * 3) If the scheme component is defined, indicating that the reference |
| 1834 | * starts with a scheme name, then the reference is interpreted as an |
| 1835 | * absolute URI and we are done. Otherwise, the reference URI's |
| 1836 | * scheme is inherited from the base URI's scheme component. |
| 1837 | */ |
| 1838 | if (ref->scheme != NULL) { |
| 1839 | val = xmlSaveUri(ref); |
| 1840 | goto done; |
| 1841 | } |
| 1842 | if (bas->scheme != NULL) |
| 1843 | res->scheme = xmlMemStrdup(bas->scheme); |
| 1844 | |
| 1845 | /* |
| 1846 | * 4) If the authority component is defined, then the reference is a |
| 1847 | * network-path and we skip to step 7. Otherwise, the reference |
| 1848 | * URI's authority is inherited from the base URI's authority |
| 1849 | * component, which will also be undefined if the URI scheme does not |
| 1850 | * use an authority component. |
| 1851 | */ |
| 1852 | if ((ref->authority != NULL) || (ref->server != NULL)) { |
| 1853 | if (ref->authority != NULL) |
| 1854 | res->authority = xmlMemStrdup(ref->authority); |
| 1855 | else { |
| 1856 | res->server = xmlMemStrdup(ref->server); |
| 1857 | if (ref->user != NULL) |
| 1858 | res->user = xmlMemStrdup(ref->user); |
| 1859 | res->port = ref->port; |
| 1860 | } |
| 1861 | if (ref->path != NULL) |
| 1862 | res->path = xmlMemStrdup(ref->path); |
| 1863 | goto step_7; |
| 1864 | } |
| 1865 | if (bas->authority != NULL) |
| 1866 | res->authority = xmlMemStrdup(bas->authority); |
| 1867 | else if (bas->server != NULL) { |
| 1868 | res->server = xmlMemStrdup(bas->server); |
| 1869 | if (bas->user != NULL) |
| 1870 | res->user = xmlMemStrdup(bas->user); |
| 1871 | res->port = bas->port; |
| 1872 | } |
| 1873 | |
| 1874 | /* |
| 1875 | * 5) If the path component begins with a slash character ("/"), then |
| 1876 | * the reference is an absolute-path and we skip to step 7. |
| 1877 | */ |
| 1878 | if ((ref->path != NULL) && (ref->path[0] == '/')) { |
| 1879 | res->path = xmlMemStrdup(ref->path); |
| 1880 | goto step_7; |
| 1881 | } |
| 1882 | |
| 1883 | |
| 1884 | /* |
| 1885 | * 6) If this step is reached, then we are resolving a relative-path |
| 1886 | * reference. The relative path needs to be merged with the base |
| 1887 | * URI's path. Although there are many ways to do this, we will |
| 1888 | * describe a simple method using a separate string buffer. |
| 1889 | * |
| 1890 | * Allocate a buffer large enough for the result string. |
| 1891 | */ |
| 1892 | len = 2; /* extra / and 0 */ |
| 1893 | if (ref->path != NULL) |
| 1894 | len += strlen(ref->path); |
| 1895 | if (bas->path != NULL) |
| 1896 | len += strlen(bas->path); |
| 1897 | res->path = (char *) xmlMalloc(len); |
| 1898 | if (res->path == NULL) { |
| 1899 | xmlGenericError(xmlGenericErrorContext, |
| 1900 | "xmlBuildURI: out of memory\n"); |
| 1901 | goto done; |
| 1902 | } |
| 1903 | res->path[0] = 0; |
| 1904 | |
| 1905 | /* |
| 1906 | * a) All but the last segment of the base URI's path component is |
| 1907 | * copied to the buffer. In other words, any characters after the |
| 1908 | * last (right-most) slash character, if any, are excluded. |
| 1909 | */ |
| 1910 | cur = 0; |
| 1911 | out = 0; |
| 1912 | if (bas->path != NULL) { |
| 1913 | while (bas->path[cur] != 0) { |
| 1914 | while ((bas->path[cur] != 0) && (bas->path[cur] != '/')) |
| 1915 | cur++; |
| 1916 | if (bas->path[cur] == 0) |
| 1917 | break; |
| 1918 | |
| 1919 | cur++; |
| 1920 | while (out < cur) { |
| 1921 | res->path[out] = bas->path[out]; |
| 1922 | out++; |
| 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | res->path[out] = 0; |
| 1927 | |
| 1928 | /* |
| 1929 | * b) The reference's path component is appended to the buffer |
| 1930 | * string. |
| 1931 | */ |
| 1932 | if (ref->path != NULL && ref->path[0] != 0) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1933 | indx = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1934 | /* |
| 1935 | * Ensure the path includes a '/' |
| 1936 | */ |
| 1937 | if ((out == 0) && (bas->server != NULL)) |
| 1938 | res->path[out++] = '/'; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1939 | while (ref->path[indx] != 0) { |
| 1940 | res->path[out++] = ref->path[indx++]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | res->path[out] = 0; |
| 1944 | |
| 1945 | /* |
| 1946 | * Steps c) to h) are really path normalization steps |
| 1947 | */ |
| 1948 | xmlNormalizeURIPath(res->path); |
| 1949 | |
| 1950 | step_7: |
| 1951 | |
| 1952 | /* |
| 1953 | * 7) The resulting URI components, including any inherited from the |
| 1954 | * base URI, are recombined to give the absolute form of the URI |
| 1955 | * reference. |
| 1956 | */ |
| 1957 | val = xmlSaveUri(res); |
| 1958 | |
| 1959 | done: |
| 1960 | if (ref != NULL) |
| 1961 | xmlFreeURI(ref); |
| 1962 | if (bas != NULL) |
| 1963 | xmlFreeURI(bas); |
| 1964 | if (res != NULL) |
| 1965 | xmlFreeURI(res); |
| 1966 | return(val); |
| 1967 | } |
| 1968 | |
Igor Zlatkovic | f2238e6 | 2003-02-19 14:50:35 +0000 | [diff] [blame] | 1969 | /** |
| 1970 | * xmlCanonicPath: |
| 1971 | * @path: the resource locator in a filesystem notation |
| 1972 | * |
| 1973 | * Constructs a canonic path from the specified path. |
| 1974 | * |
| 1975 | * Returns a new canonic path, or a duplicate of the path parameter if the |
| 1976 | * construction fails. The caller is responsible for freeing the memory occupied |
| 1977 | * by the returned string. If there is insufficient memory available, or the |
| 1978 | * argument is NULL, the function returns NULL. |
| 1979 | */ |
| 1980 | #define IS_WINDOWS_PATH(p) \ |
| 1981 | ((p != NULL) && \ |
| 1982 | (((p[0] >= 'a') && (p[0] <= 'z')) || \ |
| 1983 | ((p[0] >= 'A') && (p[0] <= 'Z'))) && \ |
| 1984 | (p[1] == ':') && ((p[2] == '/') || (p[2] == '\\'))) |
| 1985 | xmlChar* |
| 1986 | xmlCanonicPath(const xmlChar *path) |
| 1987 | { |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 1988 | #if defined(_WIN32) && !defined(__CYGWIN__) |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 1989 | int len = 0; |
| 1990 | int i = 0; |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 1991 | xmlChar *p = NULL; |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 1992 | #endif |
| 1993 | xmlChar *ret; |
Igor Zlatkovic | f2238e6 | 2003-02-19 14:50:35 +0000 | [diff] [blame] | 1994 | xmlURIPtr uri; |
| 1995 | |
| 1996 | if (path == NULL) |
| 1997 | return(NULL); |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 1998 | if ((uri = xmlParseURI((const char *) path)) != NULL) { |
Igor Zlatkovic | f2238e6 | 2003-02-19 14:50:35 +0000 | [diff] [blame] | 1999 | xmlFreeURI(uri); |
| 2000 | return xmlStrdup(path); |
| 2001 | } |
| 2002 | |
| 2003 | uri = xmlCreateURI(); |
| 2004 | |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 2005 | #if defined(_WIN32) && !defined(__CYGWIN__) |
Igor Zlatkovic | f2238e6 | 2003-02-19 14:50:35 +0000 | [diff] [blame] | 2006 | len = xmlStrlen(path); |
| 2007 | if ((len > 2) && IS_WINDOWS_PATH(path)) { |
| 2008 | uri->scheme = xmlStrdup(BAD_CAST "file"); |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 2009 | uri->path = xmlMalloc(len + 2); |
Igor Zlatkovic | f2238e6 | 2003-02-19 14:50:35 +0000 | [diff] [blame] | 2010 | uri->path[0] = '/'; |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 2011 | p = uri->path + 1; |
| 2012 | strncpy(p, path, len + 1); |
| 2013 | } else { |
Igor Zlatkovic | f2238e6 | 2003-02-19 14:50:35 +0000 | [diff] [blame] | 2014 | uri->path = xmlStrdup(path); |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 2015 | p = uri->path; |
| 2016 | } |
| 2017 | while (*p != '\0') { |
| 2018 | if (*p == '\\') |
| 2019 | *p = '/'; |
| 2020 | p++; |
| 2021 | } |
| 2022 | #else |
Daniel Veillard | c64b8e9 | 2003-02-24 11:47:13 +0000 | [diff] [blame] | 2023 | uri->path = (char *) xmlStrdup((const char *) path); |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 2024 | #endif |
Igor Zlatkovic | f2238e6 | 2003-02-19 14:50:35 +0000 | [diff] [blame] | 2025 | |
| 2026 | ret = xmlSaveUri(uri); |
| 2027 | xmlFreeURI(uri); |
| 2028 | return(ret); |
| 2029 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2030 | |