Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +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 | * |
| 8 | * Daniel.Veillard@w3.org |
| 9 | */ |
| 10 | |
| 11 | #ifdef WIN32 |
| 12 | #define INCLUDE_WINSOCK |
| 13 | #include "win32config.h" |
| 14 | #else |
| 15 | #include "config.h" |
| 16 | #endif |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "xmlmemory.h" |
| 22 | #include "uri.h" |
| 23 | |
| 24 | /** |
| 25 | * alpha = lowalpha | upalpha |
| 26 | */ |
| 27 | #define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x)) |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | |
| 32 | * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | |
| 33 | * "u" | "v" | "w" | "x" | "y" | "z" |
| 34 | */ |
| 35 | |
| 36 | #define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z')) |
| 37 | |
| 38 | /** |
| 39 | * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | |
| 40 | * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | |
| 41 | * "U" | "V" | "W" | "X" | "Y" | "Z" |
| 42 | */ |
| 43 | #define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z')) |
| 44 | |
| 45 | /** |
| 46 | * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
| 47 | */ |
| 48 | |
| 49 | #define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9')) |
| 50 | |
| 51 | /** |
| 52 | * alphanum = alpha | digit |
| 53 | */ |
| 54 | |
| 55 | #define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x)) |
| 56 | |
| 57 | /** |
| 58 | * he(x) = digit | "A" | "B" | "C" | "D" | "E" | "F" | |
| 59 | * "a" | "b" | "c" | "d" | "e" | "f" |
| 60 | */ |
| 61 | |
| 62 | #define IS_HEX(x) ((IS_DIGIT(x)) || (((x) >= 'a') && ((x) <= 'f')) || \ |
| 63 | (((x) >= 'A') && ((x) <= 'F'))) |
| 64 | |
| 65 | /** |
| 66 | * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" |
| 67 | */ |
| 68 | |
| 69 | #define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \ |
| 70 | ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \ |
| 71 | ((x) == '(') || ((x) == ')')) |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," |
| 76 | */ |
| 77 | |
| 78 | #define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \ |
| 79 | ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \ |
| 80 | ((x) == '+') || ((x) == '$') || ((x) == ',')) |
| 81 | |
| 82 | /** |
| 83 | * unreserved = alphanum | mark |
| 84 | */ |
| 85 | |
| 86 | #define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x)) |
| 87 | |
| 88 | /** |
| 89 | * escaped = "%" hex hex |
| 90 | */ |
| 91 | |
| 92 | #define IS_ESCAPED(p) ((*(p) == '%') && (IS_HEX((p)[1])) && \ |
| 93 | (IS_HEX((p)[2]))) |
| 94 | |
| 95 | /** |
| 96 | * uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" | |
| 97 | * "&" | "=" | "+" | "$" | "," |
| 98 | */ |
| 99 | #define IS_URIC_NO_SLASH(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) ||\ |
| 100 | ((*(p) == ';')) || ((*(p) == '?')) || ((*(p) == ':')) ||\ |
| 101 | ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) ||\ |
| 102 | ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ','))) |
| 103 | |
| 104 | /** |
| 105 | * pchar = unreserved | escaped | ":" | "@" | "&" | "=" | "+" | "$" | "," |
| 106 | */ |
| 107 | #define IS_PCHAR(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 108 | ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) ||\ |
| 109 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||\ |
| 110 | ((*(p) == ','))) |
| 111 | |
| 112 | /** |
| 113 | * rel_segment = 1*( unreserved | escaped | |
| 114 | * ";" | "@" | "&" | "=" | "+" | "$" | "," ) |
| 115 | */ |
| 116 | |
| 117 | #define IS_SEGMENT(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 118 | ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || \ |
| 119 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \ |
| 120 | ((*(p) == ','))) |
| 121 | |
| 122 | /** |
| 123 | * scheme = alpha *( alpha | digit | "+" | "-" | "." ) |
| 124 | */ |
| 125 | |
| 126 | #define IS_SCHEME(x) ((IS_ALPHA(x)) || (IS_DIGIT(x)) || \ |
| 127 | ((x) == '+') || ((x) == '-') || ((x) == '.')) |
| 128 | |
| 129 | /** |
| 130 | * reg_name = 1*( unreserved | escaped | "$" | "," | |
| 131 | * ";" | ":" | "@" | "&" | "=" | "+" ) |
| 132 | */ |
| 133 | |
| 134 | #define IS_REG_NAME(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 135 | ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || \ |
| 136 | ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) || \ |
| 137 | ((*(p) == '=')) || ((*(p) == '+'))) |
| 138 | |
| 139 | /** |
| 140 | * userinfo = *( unreserved | escaped | ";" | ":" | "&" | "=" | |
| 141 | * "+" | "$" | "," ) |
| 142 | */ |
| 143 | #define IS_USERINFO(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 144 | ((*(p) == ';')) || ((*(p) == ':')) || ((*(p) == '&')) || \ |
| 145 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \ |
| 146 | ((*(p) == ','))) |
| 147 | |
| 148 | /** |
| 149 | * uric = reserved | unreserved | escaped |
| 150 | */ |
| 151 | |
| 152 | #define IS_URIC(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \ |
| 153 | (IS_RESERVED(*(p)))) |
| 154 | |
| 155 | /** |
| 156 | * Skip to next pointer char, handle escaped sequences |
| 157 | */ |
| 158 | |
| 159 | #define NEXT(p) ((*p == '%')? p += 3 : p++) |
| 160 | |
| 161 | /** |
| 162 | * |
| 163 | |
| 164 | * |
| 165 | authority = server | reg_name |
| 166 | server = [ [ userinfo "@" ] hostport ] |
| 167 | |
| 168 | * reg_name = 1*( unreserved | escaped | "$" | "," | |
| 169 | * ";" | ":" | "@" | "&" | "=" | "+" ) |
| 170 | |
| 171 | * userinfo = *( unreserved | escaped | |
| 172 | * ";" | ":" | "&" | "=" | "+" | "$" | "," ) |
| 173 | |
| 174 | hostport = host [ ":" port ] |
| 175 | host = hostname | IPv4address |
| 176 | hostname = *( domainlabel "." ) toplabel [ "." ] |
| 177 | domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum |
| 178 | toplabel = alpha | alpha *( alphanum | "-" ) alphanum |
| 179 | IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit |
| 180 | port = *digit |
| 181 | |
| 182 | path = [ abs_path | opaque_part ] |
| 183 | |
| 184 | |
| 185 | */ |
| 186 | |
| 187 | /** |
| 188 | * xmlCreateURI: |
| 189 | * |
| 190 | * Simply creates an empty xmlURI |
| 191 | * |
| 192 | * Returns the new structure or NULL in case of error |
| 193 | */ |
| 194 | xmlURIPtr |
| 195 | xmlCreateURI(void) { |
| 196 | xmlURIPtr ret; |
| 197 | |
| 198 | ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI)); |
| 199 | if (ret == NULL) { |
| 200 | fprintf(stderr, "xmlCreateURI: out of memory\n"); |
| 201 | return(NULL); |
| 202 | } |
| 203 | memset(ret, 0, sizeof(xmlURI)); |
| 204 | return(ret); |
| 205 | } |
| 206 | |
| 207 | /** |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 208 | * xmlSaveUri: |
| 209 | * @uri: pointer to an xmlURI |
| 210 | * |
| 211 | * Save the URI as an escaped string |
| 212 | * |
| 213 | * Returns a new string (to be deallocated by caller) |
| 214 | */ |
| 215 | xmlChar * |
| 216 | xmlSaveUri(xmlURIPtr uri) { |
| 217 | xmlChar *ret = NULL; |
| 218 | const char *p; |
| 219 | int len; |
| 220 | int max; |
| 221 | |
| 222 | if (uri == NULL) return(NULL); |
| 223 | |
| 224 | |
| 225 | max = 80; |
| 226 | ret = xmlMalloc((max + 1) * sizeof(xmlChar)); |
| 227 | if (ret == NULL) { |
| 228 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 229 | return(NULL); |
| 230 | } |
| 231 | len = 0; |
| 232 | |
| 233 | if (uri->scheme != NULL) { |
| 234 | p = uri->scheme; |
| 235 | while (*p != 0) { |
| 236 | if (len >= max) { |
| 237 | max *= 2; |
| 238 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 239 | if (ret == NULL) { |
| 240 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 241 | return(NULL); |
| 242 | } |
| 243 | } |
| 244 | ret[len++] = *p++; |
| 245 | } |
| 246 | if (len >= max) { |
| 247 | max *= 2; |
| 248 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 249 | if (ret == NULL) { |
| 250 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 251 | return(NULL); |
| 252 | } |
| 253 | } |
| 254 | ret[len++] = ':'; |
| 255 | } |
| 256 | if (uri->opaque != NULL) { |
| 257 | p = uri->opaque; |
| 258 | while (*p != 0) { |
| 259 | if (len + 3 >= max) { |
| 260 | max *= 2; |
| 261 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 262 | if (ret == NULL) { |
| 263 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 264 | return(NULL); |
| 265 | } |
| 266 | } |
| 267 | if ((IS_UNRESERVED(*(p))) || |
| 268 | ((*(p) == ';')) || ((*(p) == '?')) || ((*(p) == ':')) || |
| 269 | ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) || |
| 270 | ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ','))) |
| 271 | ret[len++] = *p++; |
| 272 | else { |
| 273 | int val = *p++; |
| 274 | ret[len++] = '%'; |
| 275 | switch (val / 0x10) { |
| 276 | case 0xF: ret[len++] = 'F'; break; |
| 277 | case 0xE: ret[len++] = 'E'; break; |
| 278 | case 0xD: ret[len++] = 'D'; break; |
| 279 | case 0xC: ret[len++] = 'C'; break; |
| 280 | case 0xB: ret[len++] = 'B'; break; |
| 281 | case 0xA: ret[len++] = 'A'; break; |
| 282 | default: ret[len++] = '0' + (val / 0x10); |
| 283 | } |
| 284 | switch (val % 0x10) { |
| 285 | case 0xF: ret[len++] = 'F'; break; |
| 286 | case 0xE: ret[len++] = 'E'; break; |
| 287 | case 0xD: ret[len++] = 'D'; break; |
| 288 | case 0xC: ret[len++] = 'C'; break; |
| 289 | case 0xB: ret[len++] = 'B'; break; |
| 290 | case 0xA: ret[len++] = 'A'; break; |
| 291 | default: ret[len++] = '0' + (val % 0x10); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | if (len >= max) { |
| 296 | max *= 2; |
| 297 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 298 | if (ret == NULL) { |
| 299 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 300 | return(NULL); |
| 301 | } |
| 302 | } |
| 303 | ret[len++] = 0; |
| 304 | } else { |
| 305 | if (uri->authority != NULL) { |
| 306 | if (len + 3 >= max) { |
| 307 | max *= 2; |
| 308 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 309 | if (ret == NULL) { |
| 310 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 311 | return(NULL); |
| 312 | } |
| 313 | } |
| 314 | ret[len++] = '/'; |
| 315 | ret[len++] = '/'; |
| 316 | p = uri->authority; |
| 317 | while (*p != 0) { |
| 318 | if (len + 3 >= max) { |
| 319 | max *= 2; |
| 320 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 321 | if (ret == NULL) { |
| 322 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 323 | return(NULL); |
| 324 | } |
| 325 | } |
| 326 | if ((IS_UNRESERVED(*(p))) || |
| 327 | ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || |
| 328 | ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) || |
| 329 | ((*(p) == '=')) || ((*(p) == '+'))) |
| 330 | ret[len++] = *p++; |
| 331 | else { |
| 332 | int val = *p++; |
| 333 | ret[len++] = '%'; |
| 334 | switch (val / 0x10) { |
| 335 | case 0xF: ret[len++] = 'F'; break; |
| 336 | case 0xE: ret[len++] = 'E'; break; |
| 337 | case 0xD: ret[len++] = 'D'; break; |
| 338 | case 0xC: ret[len++] = 'C'; break; |
| 339 | case 0xB: ret[len++] = 'B'; break; |
| 340 | case 0xA: ret[len++] = 'A'; break; |
| 341 | default: ret[len++] = '0' + (val / 0x10); |
| 342 | } |
| 343 | switch (val % 0x10) { |
| 344 | case 0xF: ret[len++] = 'F'; break; |
| 345 | case 0xE: ret[len++] = 'E'; break; |
| 346 | case 0xD: ret[len++] = 'D'; break; |
| 347 | case 0xC: ret[len++] = 'C'; break; |
| 348 | case 0xB: ret[len++] = 'B'; break; |
| 349 | case 0xA: ret[len++] = 'A'; break; |
| 350 | default: ret[len++] = '0' + (val % 0x10); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | if (uri->path != NULL) { |
| 356 | p = uri->path; |
| 357 | while (*p != 0) { |
| 358 | if (len + 3 >= max) { |
| 359 | max *= 2; |
| 360 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 361 | if (ret == NULL) { |
| 362 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 363 | return(NULL); |
| 364 | } |
| 365 | } |
| 366 | if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) || |
| 367 | ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || |
| 368 | ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || |
| 369 | ((*(p) == ','))) |
| 370 | ret[len++] = *p++; |
| 371 | else { |
| 372 | int val = *p++; |
| 373 | ret[len++] = '%'; |
| 374 | switch (val / 0x10) { |
| 375 | case 0xF: ret[len++] = 'F'; break; |
| 376 | case 0xE: ret[len++] = 'E'; break; |
| 377 | case 0xD: ret[len++] = 'D'; break; |
| 378 | case 0xC: ret[len++] = 'C'; break; |
| 379 | case 0xB: ret[len++] = 'B'; break; |
| 380 | case 0xA: ret[len++] = 'A'; break; |
| 381 | default: ret[len++] = '0' + (val / 0x10); |
| 382 | } |
| 383 | switch (val % 0x10) { |
| 384 | case 0xF: ret[len++] = 'F'; break; |
| 385 | case 0xE: ret[len++] = 'E'; break; |
| 386 | case 0xD: ret[len++] = 'D'; break; |
| 387 | case 0xC: ret[len++] = 'C'; break; |
| 388 | case 0xB: ret[len++] = 'B'; break; |
| 389 | case 0xA: ret[len++] = 'A'; break; |
| 390 | default: ret[len++] = '0' + (val % 0x10); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | if (uri->query != NULL) { |
| 396 | if (len + 3 >= max) { |
| 397 | max *= 2; |
| 398 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 399 | if (ret == NULL) { |
| 400 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 401 | return(NULL); |
| 402 | } |
| 403 | } |
| 404 | ret[len++] = '?'; |
| 405 | p = uri->query; |
| 406 | while (*p != 0) { |
| 407 | if (len + 3 >= max) { |
| 408 | max *= 2; |
| 409 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 410 | if (ret == NULL) { |
| 411 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 412 | return(NULL); |
| 413 | } |
| 414 | } |
| 415 | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) |
| 416 | ret[len++] = *p++; |
| 417 | else { |
| 418 | int val = *p++; |
| 419 | ret[len++] = '%'; |
| 420 | switch (val / 0x10) { |
| 421 | case 0xF: ret[len++] = 'F'; break; |
| 422 | case 0xE: ret[len++] = 'E'; break; |
| 423 | case 0xD: ret[len++] = 'D'; break; |
| 424 | case 0xC: ret[len++] = 'C'; break; |
| 425 | case 0xB: ret[len++] = 'B'; break; |
| 426 | case 0xA: ret[len++] = 'A'; break; |
| 427 | default: ret[len++] = '0' + (val / 0x10); |
| 428 | } |
| 429 | switch (val % 0x10) { |
| 430 | case 0xF: ret[len++] = 'F'; break; |
| 431 | case 0xE: ret[len++] = 'E'; break; |
| 432 | case 0xD: ret[len++] = 'D'; break; |
| 433 | case 0xC: ret[len++] = 'C'; break; |
| 434 | case 0xB: ret[len++] = 'B'; break; |
| 435 | case 0xA: ret[len++] = 'A'; break; |
| 436 | default: ret[len++] = '0' + (val % 0x10); |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | if (uri->fragment != NULL) { |
| 442 | if (len + 3 >= max) { |
| 443 | max *= 2; |
| 444 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 445 | if (ret == NULL) { |
| 446 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 447 | return(NULL); |
| 448 | } |
| 449 | } |
| 450 | ret[len++] = '#'; |
| 451 | p = uri->fragment; |
| 452 | while (*p != 0) { |
| 453 | if (len + 3 >= max) { |
| 454 | max *= 2; |
| 455 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 456 | if (ret == NULL) { |
| 457 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 458 | return(NULL); |
| 459 | } |
| 460 | } |
| 461 | if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) |
| 462 | ret[len++] = *p++; |
| 463 | else { |
| 464 | int val = *p++; |
| 465 | ret[len++] = '%'; |
| 466 | switch (val / 0x10) { |
| 467 | case 0xF: ret[len++] = 'F'; break; |
| 468 | case 0xE: ret[len++] = 'E'; break; |
| 469 | case 0xD: ret[len++] = 'D'; break; |
| 470 | case 0xC: ret[len++] = 'C'; break; |
| 471 | case 0xB: ret[len++] = 'B'; break; |
| 472 | case 0xA: ret[len++] = 'A'; break; |
| 473 | default: ret[len++] = '0' + (val / 0x10); |
| 474 | } |
| 475 | switch (val % 0x10) { |
| 476 | case 0xF: ret[len++] = 'F'; break; |
| 477 | case 0xE: ret[len++] = 'E'; break; |
| 478 | case 0xD: ret[len++] = 'D'; break; |
| 479 | case 0xC: ret[len++] = 'C'; break; |
| 480 | case 0xB: ret[len++] = 'B'; break; |
| 481 | case 0xA: ret[len++] = 'A'; break; |
| 482 | default: ret[len++] = '0' + (val % 0x10); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | if (len >= max) { |
| 488 | max *= 2; |
| 489 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); |
| 490 | if (ret == NULL) { |
| 491 | fprintf(stderr, "xmlSaveUri: out of memory\n"); |
| 492 | return(NULL); |
| 493 | } |
| 494 | } |
| 495 | ret[len++] = 0; |
| 496 | } |
| 497 | return(ret); |
| 498 | } |
| 499 | |
| 500 | /** |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 501 | * xmlPrintURI: |
| 502 | * @stream: a FILE* for the output |
| 503 | * @uri: pointer to an xmlURI |
| 504 | * |
| 505 | * Prints the URI in the stream @steam. |
| 506 | */ |
| 507 | void |
| 508 | xmlPrintURI(FILE *stream, xmlURIPtr uri) { |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 509 | xmlChar *out; |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 510 | |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 511 | out = xmlSaveUri(uri); |
| 512 | if (out != NULL) { |
| 513 | fprintf(stream, "%s", out); |
| 514 | xmlFree(out); |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * xmlCleanURI: |
| 520 | * @uri: pointer to an xmlURI |
| 521 | * |
| 522 | * Make sure the xmlURI struct is free of content |
| 523 | */ |
| 524 | void |
| 525 | xmlCleanURI(xmlURIPtr uri) { |
| 526 | if (uri == NULL) return; |
| 527 | |
| 528 | if (uri->scheme != NULL) xmlFree(uri->scheme); |
| 529 | uri->scheme = NULL; |
| 530 | if (uri->server != NULL) xmlFree(uri->server); |
| 531 | uri->server = NULL; |
| 532 | if (uri->path != NULL) xmlFree(uri->path); |
| 533 | uri->path = NULL; |
| 534 | if (uri->fragment != NULL) xmlFree(uri->fragment); |
| 535 | uri->fragment = NULL; |
| 536 | if (uri->opaque != NULL) xmlFree(uri->opaque); |
| 537 | uri->opaque = NULL; |
| 538 | if (uri->authority != NULL) xmlFree(uri->authority); |
| 539 | uri->authority = NULL; |
| 540 | if (uri->query != NULL) xmlFree(uri->query); |
| 541 | uri->query = NULL; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * xmlFreeURI: |
| 546 | * @uri: pointer to an xmlURI |
| 547 | * |
| 548 | * Free up the xmlURI struct |
| 549 | */ |
| 550 | void |
| 551 | xmlFreeURI(xmlURIPtr uri) { |
| 552 | if (uri == NULL) return; |
| 553 | |
| 554 | if (uri->scheme != NULL) xmlFree(uri->scheme); |
| 555 | if (uri->server != NULL) xmlFree(uri->server); |
| 556 | if (uri->path != NULL) xmlFree(uri->path); |
| 557 | if (uri->fragment != NULL) xmlFree(uri->fragment); |
| 558 | if (uri->opaque != NULL) xmlFree(uri->opaque); |
| 559 | if (uri->authority != NULL) xmlFree(uri->authority); |
| 560 | if (uri->query != NULL) xmlFree(uri->query); |
| 561 | memset(uri, -1, sizeof(xmlURI)); |
| 562 | xmlFree(uri); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * xmlURIUnescape: |
| 567 | * @str: the string to unescape |
| 568 | * @len: the lenght in bytes to unescape (or <= 0 to indicate full string) |
| 569 | * @target: optionnal destination buffer |
| 570 | * |
| 571 | * Unescaping routine, does not do validity checks ! |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 572 | * Output is direct unsigned char translation of %XX values (no encoding) |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 573 | * |
| 574 | * Returns an copy of the string, but unescaped |
| 575 | */ |
| 576 | char * |
| 577 | xmlURIUnescape(const char *str, int len, char *target) { |
| 578 | char *ret, *out; |
| 579 | const char *in; |
| 580 | |
| 581 | if (str == NULL) |
| 582 | return(NULL); |
| 583 | if (len <= 0) len = strlen(str); |
| 584 | if (len <= 0) return(NULL); |
| 585 | |
| 586 | if (target == NULL) { |
| 587 | ret = (char *) xmlMalloc(len + 1); |
| 588 | if (ret == NULL) { |
| 589 | fprintf(stderr, "xmlURIUnescape: out of memory\n"); |
| 590 | return(NULL); |
| 591 | } |
| 592 | } else |
| 593 | ret = target; |
| 594 | in = str; |
| 595 | out = ret; |
| 596 | while(len > 0) { |
| 597 | if (*in == '%') { |
| 598 | in++; |
| 599 | if ((*in >= '0') && (*in <= '9')) |
| 600 | *out = (*in - '0'); |
| 601 | else if ((*in >= 'a') && (*in <= 'f')) |
| 602 | *out = (*in - 'a') + 10; |
| 603 | else if ((*in >= 'A') && (*in <= 'F')) |
| 604 | *out = (*in - 'A') + 10; |
| 605 | in++; |
| 606 | if ((*in >= '0') && (*in <= '9')) |
| 607 | *out = *out * 16 + (*in - '0'); |
| 608 | else if ((*in >= 'a') && (*in <= 'f')) |
| 609 | *out = *out * 16 + (*in - 'a') + 10; |
| 610 | else if ((*in >= 'A') && (*in <= 'F')) |
| 611 | *out = *out * 16 + (*in - 'A') + 10; |
| 612 | in++; |
| 613 | len -= 3; |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 614 | out++; |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 615 | } else { |
| 616 | *out++ = *in++; |
| 617 | len--; |
| 618 | } |
| 619 | } |
| 620 | *out = 0; |
| 621 | return(ret); |
| 622 | } |
| 623 | |
| 624 | |
| 625 | /** |
| 626 | * xmlParseURIFragment: |
| 627 | * @uri: pointer to an URI structure |
| 628 | * @str: pointer to the string to analyze |
| 629 | * |
| 630 | * Parse an URI fragment string and fills in the appropriate fields |
| 631 | * of the @uri structure. |
| 632 | * |
| 633 | * fragment = *uric |
| 634 | * |
| 635 | * Returns 0 or the error code |
| 636 | */ |
| 637 | int |
| 638 | xmlParseURIFragment(xmlURIPtr uri, const char **str) { |
| 639 | const char *cur = *str; |
| 640 | |
| 641 | if (str == NULL) return(-1); |
| 642 | |
| 643 | while (IS_URIC(cur)) NEXT(cur); |
| 644 | if (uri != NULL) { |
| 645 | if (uri->fragment != NULL) xmlFree(uri->fragment); |
| 646 | uri->fragment = xmlURIUnescape(*str, cur - *str, NULL); |
| 647 | } |
| 648 | *str = cur; |
| 649 | return(0); |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * xmlParseURIQuery: |
| 654 | * @uri: pointer to an URI structure |
| 655 | * @str: pointer to the string to analyze |
| 656 | * |
| 657 | * Parse the query part of an URI |
| 658 | * |
| 659 | * query = *uric |
| 660 | * |
| 661 | * Returns 0 or the error code |
| 662 | */ |
| 663 | int |
| 664 | xmlParseURIQuery(xmlURIPtr uri, const char **str) { |
| 665 | const char *cur = *str; |
| 666 | |
| 667 | if (str == NULL) return(-1); |
| 668 | |
| 669 | while (IS_URIC(cur)) NEXT(cur); |
| 670 | if (uri != NULL) { |
| 671 | if (uri->query != NULL) xmlFree(uri->query); |
| 672 | uri->query = xmlURIUnescape(*str, cur - *str, NULL); |
| 673 | } |
| 674 | *str = cur; |
| 675 | return(0); |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * xmlParseURIScheme: |
| 680 | * @uri: pointer to an URI structure |
| 681 | * @str: pointer to the string to analyze |
| 682 | * |
| 683 | * Parse an URI scheme |
| 684 | * |
| 685 | * scheme = alpha *( alpha | digit | "+" | "-" | "." ) |
| 686 | * |
| 687 | * Returns 0 or the error code |
| 688 | */ |
| 689 | int |
| 690 | xmlParseURIScheme(xmlURIPtr uri, const char **str) { |
| 691 | const char *cur; |
| 692 | |
| 693 | if (str == NULL) |
| 694 | return(-1); |
| 695 | |
| 696 | cur = *str; |
| 697 | if (!IS_ALPHA(*cur)) |
| 698 | return(2); |
| 699 | cur++; |
| 700 | while (IS_SCHEME(*cur)) cur++; |
| 701 | if (uri != NULL) { |
| 702 | if (uri->scheme != NULL) xmlFree(uri->scheme); |
| 703 | uri->scheme = xmlURIUnescape(*str, cur - *str, NULL); /* !!! strndup */ |
| 704 | } |
| 705 | *str = cur; |
| 706 | return(0); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * xmlParseURIOpaquePart: |
| 711 | * @uri: pointer to an URI structure |
| 712 | * @str: pointer to the string to analyze |
| 713 | * |
| 714 | * Parse an URI opaque part |
| 715 | * |
| 716 | * opaque_part = uric_no_slash *uric |
| 717 | * |
| 718 | * Returns 0 or the error code |
| 719 | */ |
| 720 | int |
| 721 | xmlParseURIOpaquePart(xmlURIPtr uri, const char **str) { |
| 722 | const char *cur; |
| 723 | |
| 724 | if (str == NULL) |
| 725 | return(-1); |
| 726 | |
| 727 | cur = *str; |
| 728 | if (!IS_URIC_NO_SLASH(cur)) { |
| 729 | return(3); |
| 730 | } |
| 731 | NEXT(cur); |
| 732 | while (IS_URIC(cur)) NEXT(cur); |
| 733 | if (uri != NULL) { |
| 734 | if (uri->opaque != NULL) xmlFree(uri->opaque); |
| 735 | uri->opaque = xmlURIUnescape(*str, cur - *str, NULL); |
| 736 | } |
| 737 | *str = cur; |
| 738 | return(0); |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * xmlParseURIRelSegment: |
| 743 | * @uri: pointer to an URI structure |
| 744 | * @str: pointer to the string to analyze |
| 745 | * |
| 746 | * Parse an URI relative segment |
| 747 | * |
| 748 | * rel_segment = 1*( unreserved | escaped | ";" | "@" | "&" | "=" | |
| 749 | * "+" | "$" | "," ) |
| 750 | * |
| 751 | * Returns 0 or the error code |
| 752 | */ |
| 753 | int |
| 754 | xmlParseURIRelSegment(xmlURIPtr uri, const char **str) { |
| 755 | const char *cur; |
| 756 | |
| 757 | if (str == NULL) |
| 758 | return(-1); |
| 759 | |
| 760 | cur = *str; |
| 761 | if (!IS_SEGMENT(cur)) { |
| 762 | return(3); |
| 763 | } |
| 764 | NEXT(cur); |
| 765 | while (IS_SEGMENT(cur)) NEXT(cur); |
| 766 | if (uri != NULL) { |
| 767 | if (uri->path != NULL) xmlFree(uri->path); |
| 768 | uri->path = xmlURIUnescape(*str, cur - *str, NULL); |
| 769 | } |
| 770 | *str = cur; |
| 771 | return(0); |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * xmlParseURIPathSegments: |
| 776 | * @uri: pointer to an URI structure |
| 777 | * @str: pointer to the string to analyze |
| 778 | * @slash: should we add a leading slash |
| 779 | * |
| 780 | * Parse an URI set of path segments |
| 781 | * |
| 782 | * path_segments = segment *( "/" segment ) |
| 783 | * segment = *pchar *( ";" param ) |
| 784 | * param = *pchar |
| 785 | * |
| 786 | * Returns 0 or the error code |
| 787 | */ |
| 788 | int |
| 789 | xmlParseURIPathSegments(xmlURIPtr uri, const char **str, int slash) { |
| 790 | const char *cur; |
| 791 | |
| 792 | if (str == NULL) |
| 793 | return(-1); |
| 794 | |
| 795 | cur = *str; |
| 796 | |
| 797 | do { |
| 798 | while (IS_PCHAR(cur)) NEXT(cur); |
| 799 | if (*cur == ';') { |
| 800 | cur++; |
| 801 | while (IS_PCHAR(cur)) NEXT(cur); |
| 802 | } |
| 803 | if (*cur != '/') break; |
| 804 | cur++; |
| 805 | } while (1); |
| 806 | if (uri != NULL) { |
| 807 | int len, len2 = 0; |
| 808 | char *path; |
| 809 | |
| 810 | /* |
| 811 | * Concat the set of path segments to the current path |
| 812 | */ |
| 813 | len = cur - *str; |
| 814 | if (slash) |
| 815 | len++; |
| 816 | |
| 817 | if (uri->path != NULL) { |
| 818 | len2 = strlen(uri->path); |
| 819 | len += len2; |
| 820 | } |
| 821 | path = (char *) xmlMalloc(len + 1); |
| 822 | if (path == NULL) { |
| 823 | fprintf(stderr, "xmlParseURIPathSegments: out of memory\n"); |
| 824 | *str = cur; |
| 825 | return(-1); |
| 826 | } |
| 827 | if (uri->path != NULL) |
| 828 | memcpy(path, uri->path, len2); |
| 829 | if (slash) { |
| 830 | path[len2] = '/'; |
| 831 | len2++; |
| 832 | } |
| 833 | xmlURIUnescape(*str, cur - *str, &path[len2]); |
| 834 | if (uri->path != NULL) |
| 835 | xmlFree(uri->path); |
| 836 | uri->path = path; |
| 837 | } |
| 838 | *str = cur; |
| 839 | return(0); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * xmlParseURIAuthority: |
| 844 | * @uri: pointer to an URI structure |
| 845 | * @str: pointer to the string to analyze |
| 846 | * |
| 847 | * Parse the authority part of an URI. |
| 848 | * |
| 849 | * authority = server | reg_name |
| 850 | * server = [ [ userinfo "@" ] hostport ] |
| 851 | * reg_name = 1*( unreserved | escaped | "$" | "," | ";" | ":" | |
| 852 | * "@" | "&" | "=" | "+" ) |
| 853 | * |
| 854 | * Note : this is completely ambiguous since reg_name is allowed to |
| 855 | * use the full set of chars in use by server: |
| 856 | * |
| 857 | * 3.2.1. Registry-based Naming Authority |
| 858 | * |
| 859 | * The structure of a registry-based naming authority is specific |
| 860 | * to the URI scheme, but constrained to the allowed characters |
| 861 | * for an authority component. |
| 862 | * |
| 863 | * Returns 0 or the error code |
| 864 | */ |
| 865 | int |
| 866 | xmlParseURIAuthority(xmlURIPtr uri, const char **str) { |
| 867 | const char *cur; |
| 868 | |
| 869 | if (str == NULL) |
| 870 | return(-1); |
| 871 | |
| 872 | cur = *str; |
| 873 | if (!IS_REG_NAME(cur)) { |
| 874 | return(5); |
| 875 | } |
| 876 | NEXT(cur); |
| 877 | while (IS_REG_NAME(cur)) NEXT(cur); |
| 878 | if (uri != NULL) { |
| 879 | if (uri->authority != NULL) xmlFree(uri->authority); |
| 880 | uri->authority = xmlURIUnescape(*str, cur - *str, NULL); |
| 881 | |
| 882 | /* @@ Parse the authority to try to extract server infos !!! */ |
| 883 | } |
| 884 | *str = cur; |
| 885 | return(0); |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * xmlParseURIHierPart: |
| 890 | * @uri: pointer to an URI structure |
| 891 | * @str: pointer to the string to analyze |
| 892 | * |
| 893 | * Parse an URI hirarchical part |
| 894 | * |
| 895 | * hier_part = ( net_path | abs_path ) [ "?" query ] |
| 896 | * abs_path = "/" path_segments |
| 897 | * net_path = "//" authority [ abs_path ] |
| 898 | * |
| 899 | * Returns 0 or the error code |
| 900 | */ |
| 901 | int |
| 902 | xmlParseURIHierPart(xmlURIPtr uri, const char **str) { |
| 903 | int ret; |
| 904 | const char *cur; |
| 905 | |
| 906 | if (str == NULL) |
| 907 | return(-1); |
| 908 | |
| 909 | cur = *str; |
| 910 | |
| 911 | if ((cur[0] == '/') && (cur[1] == '/')) { |
| 912 | cur += 2; |
| 913 | ret = xmlParseURIAuthority(uri, &cur); |
| 914 | if (ret != 0) |
| 915 | return(ret); |
| 916 | if (cur[0] == '/') { |
| 917 | cur++; |
| 918 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 919 | } |
| 920 | } else if (cur[0] == '/') { |
| 921 | cur++; |
| 922 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 923 | } else { |
| 924 | return(4); |
| 925 | } |
| 926 | if (ret != 0) |
| 927 | return(ret); |
| 928 | if (*cur == '?') { |
| 929 | cur++; |
| 930 | ret = xmlParseURIQuery(uri, &cur); |
| 931 | if (ret != 0) |
| 932 | return(ret); |
| 933 | } |
| 934 | *str = cur; |
| 935 | return(0); |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * xmlParseAbsoluteURI: |
| 940 | * @uri: pointer to an URI structure |
| 941 | * @str: pointer to the string to analyze |
| 942 | * |
| 943 | * Parse an URI reference string and fills in the appropriate fields |
| 944 | * of the @uri structure |
| 945 | * |
| 946 | * absoluteURI = scheme ":" ( hier_part | opaque_part ) |
| 947 | * |
| 948 | * Returns 0 or the error code |
| 949 | */ |
| 950 | int |
| 951 | xmlParseAbsoluteURI(xmlURIPtr uri, const char **str) { |
| 952 | int ret; |
| 953 | |
| 954 | if (str == NULL) |
| 955 | return(-1); |
| 956 | |
| 957 | ret = xmlParseURIScheme(uri, str); |
| 958 | if (ret != 0) return(ret); |
| 959 | if (**str != ':') |
| 960 | return(1); |
| 961 | (*str)++; |
| 962 | if (**str == '/') |
| 963 | return(xmlParseURIHierPart(uri, str)); |
| 964 | return(xmlParseURIOpaquePart(uri, str)); |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * xmlParseRelativeURI: |
| 969 | * @uri: pointer to an URI structure |
| 970 | * @str: pointer to the string to analyze |
| 971 | * |
| 972 | * Parse an relative URI string and fills in the appropriate fields |
| 973 | * of the @uri structure |
| 974 | * |
| 975 | * relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ] |
| 976 | * abs_path = "/" path_segments |
| 977 | * net_path = "//" authority [ abs_path ] |
| 978 | * rel_path = rel_segment [ abs_path ] |
| 979 | * |
| 980 | * Returns 0 or the error code |
| 981 | */ |
| 982 | int |
| 983 | xmlParseRelativeURI(xmlURIPtr uri, const char **str) { |
| 984 | int ret = 0; |
| 985 | const char *cur; |
| 986 | |
| 987 | if (str == NULL) |
| 988 | return(-1); |
| 989 | |
| 990 | cur = *str; |
| 991 | if ((cur[0] == '/') && (cur[1] == '/')) { |
| 992 | cur += 2; |
| 993 | ret = xmlParseURIAuthority(uri, &cur); |
| 994 | if (ret != 0) |
| 995 | return(ret); |
| 996 | if (cur[0] == '/') { |
| 997 | cur++; |
| 998 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 999 | } |
| 1000 | } else if (cur[0] == '/') { |
| 1001 | cur++; |
| 1002 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 1003 | } else { |
| 1004 | ret = xmlParseURIRelSegment(uri, &cur); |
| 1005 | if (ret != 0) |
| 1006 | return(ret); |
| 1007 | if (cur[0] == '/') { |
| 1008 | cur++; |
| 1009 | ret = xmlParseURIPathSegments(uri, &cur, 1); |
| 1010 | } |
| 1011 | } |
| 1012 | if (ret != 0) |
| 1013 | return(ret); |
| 1014 | if (*cur == '?') { |
| 1015 | cur++; |
| 1016 | ret = xmlParseURIQuery(uri, &cur); |
| 1017 | if (ret != 0) |
| 1018 | return(ret); |
| 1019 | } |
| 1020 | *str = cur; |
| 1021 | return(ret); |
| 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * xmlParseURIReference: |
| 1026 | * @uri: pointer to an URI structure |
| 1027 | * @str: the string to analyze |
| 1028 | * |
| 1029 | * Parse an URI reference string and fills in the appropriate fields |
| 1030 | * of the @uri structure |
| 1031 | * |
| 1032 | * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ] |
| 1033 | * |
| 1034 | * Returns 0 or the error code |
| 1035 | */ |
| 1036 | int |
| 1037 | xmlParseURIReference(xmlURIPtr uri, const char *str) { |
| 1038 | int ret; |
| 1039 | const char *tmp = str; |
| 1040 | |
| 1041 | if (str == NULL) |
| 1042 | return(-1); |
| 1043 | xmlCleanURI(uri); |
| 1044 | |
| 1045 | /* |
| 1046 | * Try first to parse aboslute refs, then fallback to relative if |
| 1047 | * it fails. |
| 1048 | */ |
| 1049 | ret = xmlParseAbsoluteURI(uri, &str); |
| 1050 | if (ret != 0) { |
| 1051 | xmlCleanURI(uri); |
| 1052 | str = tmp; |
| 1053 | ret = xmlParseRelativeURI(uri, &str); |
| 1054 | } |
| 1055 | if (ret != 0) { |
| 1056 | xmlCleanURI(uri); |
| 1057 | return(ret); |
| 1058 | } |
| 1059 | |
| 1060 | if (*str == '#') { |
| 1061 | str++; |
| 1062 | ret = xmlParseURIFragment(uri, &str); |
| 1063 | if (ret != 0) return(ret); |
| 1064 | } |
| 1065 | if (*str != 0) { |
| 1066 | xmlCleanURI(uri); |
| 1067 | return(1); |
| 1068 | } |
| 1069 | return(0); |
| 1070 | } |
| 1071 | |
| 1072 | /** |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1073 | * xmlNormalizeURIPath: |
| 1074 | * @path: pointer to the path string |
| 1075 | * |
| 1076 | * applies the 5 normalization steps to a path string |
| 1077 | * Normalization occurs directly on the string, no new allocation is done |
| 1078 | * |
| 1079 | * Returns 0 or an error code |
| 1080 | */ |
| 1081 | int |
| 1082 | xmlNormalizeURIPath(char *path) { |
| 1083 | int cur, out; |
| 1084 | |
| 1085 | if (path == NULL) |
| 1086 | return(-1); |
| 1087 | cur = 0; |
| 1088 | out = 0; |
| 1089 | while ((path[cur] != 0) && (path[cur] != '/')) cur++; |
| 1090 | if (path[cur] == 0) |
| 1091 | return(0); |
| 1092 | |
| 1093 | /* we are positionned at the beginning of the first segment */ |
| 1094 | cur++; |
| 1095 | out = cur; |
| 1096 | |
| 1097 | /* |
| 1098 | * Analyze each segment in sequence. |
| 1099 | */ |
| 1100 | while (path[cur] != 0) { |
| 1101 | /* |
| 1102 | * c) All occurrences of "./", where "." is a complete path segment, |
| 1103 | * are removed from the buffer string. |
| 1104 | */ |
| 1105 | if ((path[cur] == '.') && (path[cur + 1] == '/')) { |
| 1106 | cur += 2; |
| 1107 | continue; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * d) If the buffer string ends with "." as a complete path segment, |
| 1112 | * that "." is removed. |
| 1113 | */ |
| 1114 | if ((path[cur] == '.') && (path[cur + 1] == 0)) { |
| 1115 | path[out] = 0; |
| 1116 | break; |
| 1117 | } |
| 1118 | |
| 1119 | /* read the segment */ |
| 1120 | while ((path[cur] != 0) && (path[cur] != '/')) { |
| 1121 | path[out++] = path[cur++]; |
| 1122 | } |
| 1123 | path[out++] = path[cur]; |
| 1124 | if (path[cur] != 0) { |
| 1125 | cur++; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | cur = 0; |
| 1130 | out = 0; |
| 1131 | while ((path[cur] != 0) && (path[cur] != '/')) cur++; |
| 1132 | if (path[cur] == 0) |
| 1133 | return(0); |
| 1134 | /* we are positionned at the beginning of the first segment */ |
| 1135 | cur++; |
| 1136 | out = cur; |
| 1137 | /* |
| 1138 | * Analyze each segment in sequence. |
| 1139 | */ |
| 1140 | while (path[cur] != 0) { |
| 1141 | /* |
| 1142 | * e) All occurrences of "<segment>/../", where <segment> is a |
| 1143 | * complete path segment not equal to "..", are removed from the |
| 1144 | * buffer string. Removal of these path segments is performed |
| 1145 | * iteratively, removing the leftmost matching pattern on each |
| 1146 | * iteration, until no matching pattern remains. |
| 1147 | */ |
| 1148 | if ((cur > 1) && (out > 1) && |
| 1149 | (path[cur] == '/') && (path[cur + 1] == '.') && |
| 1150 | (path[cur + 2] == '.') && (path[cur + 3] == '/') && |
| 1151 | ((path[out] != '.') || (path[out - 1] != '.') || |
| 1152 | (path[out - 2] != '/'))) { |
| 1153 | cur += 3; |
| 1154 | out --; |
| 1155 | while ((out > 0) && (path[out] != '/')) { out --; } |
| 1156 | path[out] = 0; |
| 1157 | continue; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * f) If the buffer string ends with "<segment>/..", where <segment> |
| 1162 | * is a complete path segment not equal to "..", that |
| 1163 | * "<segment>/.." is removed. |
| 1164 | */ |
| 1165 | if ((path[cur] == '/') && (path[cur + 1] == '.') && |
| 1166 | (path[cur + 2] == '.') && (path[cur + 3] == 0) && |
| 1167 | ((path[out] != '.') || (path[out - 1] != '.') || |
| 1168 | (path[out - 2] != '/'))) { |
| 1169 | cur += 4; |
| 1170 | out --; |
| 1171 | while ((out > 0) && (path[out - 1] != '/')) { out --; } |
| 1172 | path[out] = 0; |
| 1173 | continue; |
| 1174 | } |
| 1175 | |
| 1176 | path[out++] = path[cur++]; /* / or 0 */ |
| 1177 | } |
| 1178 | path[out] = 0; |
| 1179 | |
| 1180 | /* |
| 1181 | * g) If the resulting buffer string still begins with one or more |
| 1182 | * complete path segments of "..", then the reference is |
| 1183 | * considered to be in error. Implementations may handle this |
| 1184 | * error by retaining these components in the resolved path (i.e., |
| 1185 | * treating them as part of the final URI), by removing them from |
| 1186 | * the resolved path (i.e., discarding relative levels above the |
| 1187 | * root), or by avoiding traversal of the reference. |
| 1188 | * |
| 1189 | * We discard them from the final path. |
| 1190 | */ |
| 1191 | cur = 0; |
| 1192 | while ((path[cur] == '/') && (path[cur + 1] == '.') && |
| 1193 | (path[cur + 2] == '.')) |
| 1194 | cur += 3; |
| 1195 | if (cur != 0) { |
| 1196 | out = 0; |
| 1197 | while (path[cur] != 0) path[out++] = path[cur++]; |
| 1198 | path[out] = 0; |
| 1199 | } |
| 1200 | return(0); |
| 1201 | } |
| 1202 | |
| 1203 | /** |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1204 | * xmlBuildURI: |
| 1205 | * @URI: the URI instance found in the document |
| 1206 | * @base: the base value |
| 1207 | * |
| 1208 | * Computes he final URI of the reference done by checking that |
| 1209 | * the given URI is valid, and building the final URI using the |
| 1210 | * base URI. This is processed according to section 5.2 of the |
| 1211 | * RFC 2396 |
| 1212 | * |
| 1213 | * 5.2. Resolving Relative References to Absolute Form |
| 1214 | * |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1215 | * Returns a new URI string (to be freed by the caller) or NULL in case |
| 1216 | * of error. |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1217 | */ |
| 1218 | xmlChar * |
| 1219 | xmlBuildURI(const xmlChar *URI, const xmlChar *base) { |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1220 | xmlChar *val = NULL; |
| 1221 | int ret, len, index, cur, out; |
| 1222 | xmlURIPtr ref = NULL; |
| 1223 | xmlURIPtr bas = NULL; |
| 1224 | xmlURIPtr res = NULL; |
| 1225 | |
| 1226 | |
| 1227 | /* |
| 1228 | * 1) The URI reference is parsed into the potential four components and |
| 1229 | * fragment identifier, as described in Section 4.3. |
| 1230 | */ |
| 1231 | ref = xmlCreateURI(); |
| 1232 | if (ref == NULL) |
| 1233 | goto done; |
| 1234 | ret = xmlParseURIReference(ref, (const char *) URI); |
| 1235 | if (ret != 0) |
| 1236 | goto done; |
| 1237 | bas = xmlCreateURI(); |
| 1238 | if (bas == NULL) |
| 1239 | goto done; |
| 1240 | ret = xmlParseURIReference(bas, (const char *) base); |
| 1241 | if (ret != 0) |
| 1242 | goto done; |
| 1243 | |
| 1244 | /* |
| 1245 | * 2) If the path component is empty and the scheme, authority, and |
| 1246 | * query components are undefined, then it is a reference to the |
| 1247 | * current document and we are done. Otherwise, the reference URI's |
| 1248 | * query and fragment components are defined as found (or not found) |
| 1249 | * within the URI reference and not inherited from the base URI. |
| 1250 | */ |
| 1251 | res = xmlCreateURI(); |
| 1252 | if (res == NULL) |
| 1253 | goto done; |
| 1254 | if ((ref->scheme == NULL) && (ref->path == NULL) && |
| 1255 | (ref->authority == NULL) && (ref->query == NULL)) { |
| 1256 | if (ref->fragment == NULL) |
| 1257 | goto done; |
| 1258 | res->fragment = xmlMemStrdup(ref->fragment); |
| 1259 | val = xmlSaveUri(res); |
| 1260 | goto done; |
| 1261 | } |
| 1262 | |
| 1263 | /* |
| 1264 | * 3) If the scheme component is defined, indicating that the reference |
| 1265 | * starts with a scheme name, then the reference is interpreted as an |
| 1266 | * absolute URI and we are done. Otherwise, the reference URI's |
| 1267 | * scheme is inherited from the base URI's scheme component. |
| 1268 | */ |
| 1269 | if (ref->scheme != NULL) { |
| 1270 | val = xmlSaveUri(ref); |
| 1271 | goto done; |
| 1272 | } |
| 1273 | res->scheme = xmlMemStrdup(bas->scheme); |
| 1274 | |
| 1275 | /* |
| 1276 | * 4) If the authority component is defined, then the reference is a |
| 1277 | * network-path and we skip to step 7. Otherwise, the reference |
| 1278 | * URI's authority is inherited from the base URI's authority |
| 1279 | * component, which will also be undefined if the URI scheme does not |
| 1280 | * use an authority component. |
| 1281 | */ |
| 1282 | if (ref->authority != NULL) { |
| 1283 | res->authority = xmlMemStrdup(ref->authority); |
| 1284 | if (ref->path != NULL) |
| 1285 | res->path = xmlMemStrdup(ref->path); |
| 1286 | if (ref->query != NULL) |
| 1287 | res->query = xmlMemStrdup(ref->query); |
| 1288 | if (ref->fragment != NULL) |
| 1289 | res->fragment = xmlMemStrdup(ref->fragment); |
| 1290 | goto step_7; |
| 1291 | } |
| 1292 | if (bas->authority != NULL) |
| 1293 | res->authority = xmlMemStrdup(bas->authority); |
| 1294 | |
| 1295 | /* |
| 1296 | * 5) If the path component begins with a slash character ("/"), then |
| 1297 | * the reference is an absolute-path and we skip to step 7. |
| 1298 | */ |
| 1299 | if ((ref->path != NULL) && (ref->path[0] == '/')) { |
| 1300 | res->path = xmlMemStrdup(ref->path); |
| 1301 | if (ref->query != NULL) |
| 1302 | res->query = xmlMemStrdup(ref->query); |
| 1303 | if (ref->fragment != NULL) |
| 1304 | res->fragment = xmlMemStrdup(ref->fragment); |
| 1305 | goto step_7; |
| 1306 | } |
| 1307 | |
| 1308 | |
| 1309 | /* |
| 1310 | * 6) If this step is reached, then we are resolving a relative-path |
| 1311 | * reference. The relative path needs to be merged with the base |
| 1312 | * URI's path. Although there are many ways to do this, we will |
| 1313 | * describe a simple method using a separate string buffer. |
| 1314 | * |
| 1315 | * Allocate a buffer large enough for the result string. |
| 1316 | */ |
| 1317 | len = 2; /* extra / and 0 */ |
| 1318 | if (ref->path != NULL) |
| 1319 | len += strlen(ref->path); |
| 1320 | if (bas->path != NULL) |
| 1321 | len += strlen(bas->path); |
| 1322 | res->path = (char *) xmlMalloc(len); |
| 1323 | if (res->path == NULL) { |
| 1324 | fprintf(stderr, "xmlBuildURI: out of memory\n"); |
| 1325 | goto done; |
| 1326 | } |
| 1327 | res->path[0] = 0; |
| 1328 | |
| 1329 | /* |
| 1330 | * a) All but the last segment of the base URI's path component is |
| 1331 | * copied to the buffer. In other words, any characters after the |
| 1332 | * last (right-most) slash character, if any, are excluded. |
| 1333 | */ |
| 1334 | cur = 0; |
| 1335 | out = 0; |
| 1336 | if (bas->path != NULL) { |
| 1337 | while (bas->path[cur] != 0) { |
| 1338 | while ((bas->path[cur] != 0) && (bas->path[cur] != '/')) |
| 1339 | cur++; |
| 1340 | if (bas->path[cur] == 0) |
| 1341 | break; |
| 1342 | |
| 1343 | cur++; |
| 1344 | while (out < cur) { |
| 1345 | res->path[out] = bas->path[out]; |
| 1346 | out++; |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | res->path[out] = 0; |
| 1351 | |
| 1352 | /* |
| 1353 | * b) The reference's path component is appended to the buffer |
| 1354 | * string. |
| 1355 | */ |
| 1356 | if (ref->path != NULL) { |
| 1357 | index = 0; |
| 1358 | while (ref->path[index] != 0) { |
| 1359 | res->path[out++] = ref->path[index++]; |
| 1360 | } |
| 1361 | } |
| 1362 | res->path[out] = 0; |
| 1363 | |
| 1364 | /* |
| 1365 | * Steps c) to h) are really path normalization steps |
| 1366 | */ |
| 1367 | xmlNormalizeURIPath(res->path); |
| 1368 | |
| 1369 | step_7: |
| 1370 | |
| 1371 | /* |
| 1372 | * 7) The resulting URI components, including any inherited from the |
| 1373 | * base URI, are recombined to give the absolute form of the URI |
| 1374 | * reference. |
| 1375 | */ |
| 1376 | val = xmlSaveUri(res); |
| 1377 | |
| 1378 | done: |
| 1379 | if (ref != NULL) |
| 1380 | xmlFreeURI(ref); |
| 1381 | if (base != NULL) |
| 1382 | xmlFreeURI(bas); |
| 1383 | if (res != NULL) |
| 1384 | xmlFreeURI(res); |
| 1385 | return(val); |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | |
| 1389 | #ifdef STANDALONE |
| 1390 | int main(int argc, char **argv) { |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1391 | int i, ret, arg = 1; |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1392 | xmlURIPtr uri; |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1393 | const char *base = NULL; |
| 1394 | xmlChar *composite; |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1395 | |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1396 | if ((!strcmp(argv[arg], "-base")) || (!strcmp(argv[arg], "--base"))) { |
| 1397 | arg++; |
| 1398 | base = argv[arg]; |
| 1399 | if (base != NULL) |
| 1400 | arg++; |
| 1401 | } |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1402 | uri = xmlCreateURI(); |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1403 | if (argv[arg] == NULL) { |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1404 | char str[1024]; |
| 1405 | |
| 1406 | while (1) { |
| 1407 | /* |
| 1408 | * read one line in string buffer. |
| 1409 | */ |
| 1410 | if (fgets (&str[0], sizeof (str) - 1, stdin) == NULL) |
| 1411 | break; |
| 1412 | |
| 1413 | /* |
| 1414 | * remove the ending spaces |
| 1415 | */ |
| 1416 | i = strlen(str); |
| 1417 | while ((i > 0) && |
| 1418 | ((str[i - 1] == '\n') || (str[i - 1] == '\r') || |
| 1419 | (str[i - 1] == ' ') || (str[i - 1] == '\t'))) { |
| 1420 | i--; |
| 1421 | str[i] = 0; |
| 1422 | } |
| 1423 | if (i <= 0) |
| 1424 | continue; |
| 1425 | |
| 1426 | ret = xmlParseURIReference(uri, str); |
| 1427 | if (ret != 0) |
| 1428 | printf("%s : error %d\n", str, ret); |
| 1429 | else { |
| 1430 | xmlPrintURI(stdout, uri); |
| 1431 | printf("\n"); |
| 1432 | } |
| 1433 | |
| 1434 | } |
| 1435 | } else { |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1436 | while (argv[arg] != NULL) { |
| 1437 | if (base == NULL) { |
| 1438 | ret = xmlParseURIReference(uri, argv[arg]); |
| 1439 | if (ret != 0) |
| 1440 | printf("%s : error %d\n", argv[arg], ret); |
| 1441 | else { |
| 1442 | xmlPrintURI(stdout, uri); |
| 1443 | printf("\n"); |
| 1444 | } |
| 1445 | } else { |
| 1446 | composite = xmlBuildURI((xmlChar *)argv[arg], (xmlChar *) base); |
| 1447 | if (base == NULL) { |
| 1448 | } else { |
| 1449 | printf("%s\n", composite); |
| 1450 | xmlFree(composite); |
| 1451 | } |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1452 | } |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1453 | arg++; |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | xmlFreeURI(uri); |
Daniel Veillard | ec30341 | 2000-03-24 13:41:54 +0000 | [diff] [blame^] | 1457 | xmlMemoryDump(); |
Daniel Veillard | 3dd82e7 | 2000-03-20 11:48:04 +0000 | [diff] [blame] | 1458 | exit(0); |
| 1459 | } |
| 1460 | #endif |