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