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