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