blob: b89603cd56647131b413b58381575796df4e6b61 [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
Daniel Veillard34ce8be2002-03-18 19:37:11 +000011#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000012#include "libxml.h"
13
Owen Taylor3473f882001-02-23 17:55:21 +000014#include <string.h>
15
16#include <libxml/xmlmemory.h>
17#include <libxml/uri.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000018#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000019#include <libxml/xmlerror.h>
20
21/************************************************************************
22 * *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000023 * Macros to differentiate various character type *
Owen Taylor3473f882001-02-23 17:55:21 +000024 * directly extracted from RFC 2396 *
25 * *
26 ************************************************************************/
27
28/*
29 * alpha = lowalpha | upalpha
30 */
31#define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
32
33
34/*
35 * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
36 * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
37 * "u" | "v" | "w" | "x" | "y" | "z"
38 */
39
40#define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z'))
41
42/*
43 * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" |
44 * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" |
45 * "U" | "V" | "W" | "X" | "Y" | "Z"
46 */
47#define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z'))
48
49/*
50 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
51 */
52
53#define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
54
55/*
56 * alphanum = alpha | digit
57 */
58
59#define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
60
61/*
62 * hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
63 * "a" | "b" | "c" | "d" | "e" | "f"
64 */
65
66#define IS_HEX(x) ((IS_DIGIT(x)) || (((x) >= 'a') && ((x) <= 'f')) || \
67 (((x) >= 'A') && ((x) <= 'F')))
68
69/*
70 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
71 */
72
73#define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
74 ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
75 ((x) == '(') || ((x) == ')'))
76
77
78/*
79 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
80 */
81
82#define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
83 ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
84 ((x) == '+') || ((x) == '$') || ((x) == ','))
85
86/*
87 * unreserved = alphanum | mark
88 */
89
90#define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
91
92/*
93 * escaped = "%" hex hex
94 */
95
96#define IS_ESCAPED(p) ((*(p) == '%') && (IS_HEX((p)[1])) && \
97 (IS_HEX((p)[2])))
98
99/*
100 * uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
101 * "&" | "=" | "+" | "$" | ","
102 */
103#define IS_URIC_NO_SLASH(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) ||\
104 ((*(p) == ';')) || ((*(p) == '?')) || ((*(p) == ':')) ||\
105 ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) ||\
106 ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ',')))
107
108/*
109 * pchar = unreserved | escaped | ":" | "@" | "&" | "=" | "+" | "$" | ","
110 */
111#define IS_PCHAR(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
112 ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) ||\
113 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||\
114 ((*(p) == ',')))
115
116/*
117 * rel_segment = 1*( unreserved | escaped |
118 * ";" | "@" | "&" | "=" | "+" | "$" | "," )
119 */
120
121#define IS_SEGMENT(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
122 ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || \
123 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \
124 ((*(p) == ',')))
125
126/*
127 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
128 */
129
130#define IS_SCHEME(x) ((IS_ALPHA(x)) || (IS_DIGIT(x)) || \
131 ((x) == '+') || ((x) == '-') || ((x) == '.'))
132
133/*
134 * reg_name = 1*( unreserved | escaped | "$" | "," |
135 * ";" | ":" | "@" | "&" | "=" | "+" )
136 */
137
138#define IS_REG_NAME(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
139 ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || \
140 ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) || \
141 ((*(p) == '=')) || ((*(p) == '+')))
142
143/*
144 * userinfo = *( unreserved | escaped | ";" | ":" | "&" | "=" |
145 * "+" | "$" | "," )
146 */
147#define IS_USERINFO(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
148 ((*(p) == ';')) || ((*(p) == ':')) || ((*(p) == '&')) || \
149 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \
150 ((*(p) == ',')))
151
152/*
153 * uric = reserved | unreserved | escaped
154 */
155
156#define IS_URIC(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
157 (IS_RESERVED(*(p))))
158
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000159/*
160* unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
161*/
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000162
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000163#define IS_UNWISE(p) \
164 (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \
165 ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \
166 ((*(p) == ']')) || ((*(p) == '`')))
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000167
168/*
Owen Taylor3473f882001-02-23 17:55:21 +0000169 * Skip to next pointer char, handle escaped sequences
170 */
171
172#define NEXT(p) ((*p == '%')? p += 3 : p++)
173
174/*
175 * Productions from the spec.
176 *
177 * authority = server | reg_name
178 * reg_name = 1*( unreserved | escaped | "$" | "," |
179 * ";" | ":" | "@" | "&" | "=" | "+" )
180 *
181 * path = [ abs_path | opaque_part ]
182 */
183
184/************************************************************************
185 * *
186 * Generic URI structure functions *
187 * *
188 ************************************************************************/
189
190/**
191 * xmlCreateURI:
192 *
193 * Simply creates an empty xmlURI
194 *
195 * Returns the new structure or NULL in case of error
196 */
197xmlURIPtr
198xmlCreateURI(void) {
199 xmlURIPtr ret;
200
201 ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI));
202 if (ret == NULL) {
203 xmlGenericError(xmlGenericErrorContext,
204 "xmlCreateURI: out of memory\n");
205 return(NULL);
206 }
207 memset(ret, 0, sizeof(xmlURI));
208 return(ret);
209}
210
211/**
212 * xmlSaveUri:
213 * @uri: pointer to an xmlURI
214 *
215 * Save the URI as an escaped string
216 *
217 * Returns a new string (to be deallocated by caller)
218 */
219xmlChar *
220xmlSaveUri(xmlURIPtr uri) {
221 xmlChar *ret = NULL;
222 const char *p;
223 int len;
224 int max;
225
226 if (uri == NULL) return(NULL);
227
228
229 max = 80;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000230 ret = (xmlChar *) xmlMallocAtomic((max + 1) * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +0000231 if (ret == NULL) {
232 xmlGenericError(xmlGenericErrorContext,
233 "xmlSaveUri: out of memory\n");
234 return(NULL);
235 }
236 len = 0;
237
238 if (uri->scheme != NULL) {
239 p = uri->scheme;
240 while (*p != 0) {
241 if (len >= max) {
242 max *= 2;
243 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
244 if (ret == NULL) {
245 xmlGenericError(xmlGenericErrorContext,
246 "xmlSaveUri: out of memory\n");
247 return(NULL);
248 }
249 }
250 ret[len++] = *p++;
251 }
252 if (len >= max) {
253 max *= 2;
254 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
255 if (ret == NULL) {
256 xmlGenericError(xmlGenericErrorContext,
257 "xmlSaveUri: out of memory\n");
258 return(NULL);
259 }
260 }
261 ret[len++] = ':';
262 }
263 if (uri->opaque != NULL) {
264 p = uri->opaque;
265 while (*p != 0) {
266 if (len + 3 >= max) {
267 max *= 2;
268 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
269 if (ret == NULL) {
270 xmlGenericError(xmlGenericErrorContext,
271 "xmlSaveUri: out of memory\n");
272 return(NULL);
273 }
274 }
Daniel Veillard9231ff92003-03-23 22:00:51 +0000275 if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p)))
Owen Taylor3473f882001-02-23 17:55:21 +0000276 ret[len++] = *p++;
277 else {
278 int val = *(unsigned char *)p++;
279 int hi = val / 0x10, lo = val % 0x10;
280 ret[len++] = '%';
281 ret[len++] = hi + (hi > 9? 'A'-10 : '0');
282 ret[len++] = lo + (lo > 9? 'A'-10 : '0');
283 }
284 }
Owen Taylor3473f882001-02-23 17:55:21 +0000285 } else {
286 if (uri->server != NULL) {
287 if (len + 3 >= max) {
288 max *= 2;
289 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
290 if (ret == NULL) {
291 xmlGenericError(xmlGenericErrorContext,
292 "xmlSaveUri: out of memory\n");
293 return(NULL);
294 }
295 }
296 ret[len++] = '/';
297 ret[len++] = '/';
298 if (uri->user != NULL) {
299 p = uri->user;
300 while (*p != 0) {
301 if (len + 3 >= max) {
302 max *= 2;
303 ret = (xmlChar *) xmlRealloc(ret,
304 (max + 1) * sizeof(xmlChar));
305 if (ret == NULL) {
306 xmlGenericError(xmlGenericErrorContext,
307 "xmlSaveUri: out of memory\n");
308 return(NULL);
309 }
310 }
311 if ((IS_UNRESERVED(*(p))) ||
312 ((*(p) == ';')) || ((*(p) == ':')) ||
313 ((*(p) == '&')) || ((*(p) == '=')) ||
314 ((*(p) == '+')) || ((*(p) == '$')) ||
315 ((*(p) == ',')))
316 ret[len++] = *p++;
317 else {
318 int val = *(unsigned char *)p++;
319 int hi = val / 0x10, lo = val % 0x10;
320 ret[len++] = '%';
321 ret[len++] = hi + (hi > 9? 'A'-10 : '0');
322 ret[len++] = lo + (lo > 9? 'A'-10 : '0');
323 }
324 }
325 if (len + 3 >= max) {
326 max *= 2;
327 ret = (xmlChar *) xmlRealloc(ret,
328 (max + 1) * sizeof(xmlChar));
329 if (ret == NULL) {
330 xmlGenericError(xmlGenericErrorContext,
331 "xmlSaveUri: out of memory\n");
332 return(NULL);
333 }
334 }
335 ret[len++] = '@';
336 }
337 p = uri->server;
338 while (*p != 0) {
339 if (len >= max) {
340 max *= 2;
341 ret = (xmlChar *) xmlRealloc(ret,
342 (max + 1) * sizeof(xmlChar));
343 if (ret == NULL) {
344 xmlGenericError(xmlGenericErrorContext,
345 "xmlSaveUri: out of memory\n");
346 return(NULL);
347 }
348 }
349 ret[len++] = *p++;
350 }
351 if (uri->port > 0) {
352 if (len + 10 >= max) {
353 max *= 2;
354 ret = (xmlChar *) xmlRealloc(ret,
355 (max + 1) * sizeof(xmlChar));
356 if (ret == NULL) {
357 xmlGenericError(xmlGenericErrorContext,
358 "xmlSaveUri: out of memory\n");
359 return(NULL);
360 }
361 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000362 len += snprintf((char *) &ret[len], max - len, ":%d", uri->port);
Owen Taylor3473f882001-02-23 17:55:21 +0000363 }
364 } else if (uri->authority != NULL) {
365 if (len + 3 >= max) {
366 max *= 2;
367 ret = (xmlChar *) xmlRealloc(ret,
368 (max + 1) * sizeof(xmlChar));
369 if (ret == NULL) {
370 xmlGenericError(xmlGenericErrorContext,
371 "xmlSaveUri: out of memory\n");
372 return(NULL);
373 }
374 }
375 ret[len++] = '/';
376 ret[len++] = '/';
377 p = uri->authority;
378 while (*p != 0) {
379 if (len + 3 >= max) {
380 max *= 2;
381 ret = (xmlChar *) xmlRealloc(ret,
382 (max + 1) * sizeof(xmlChar));
383 if (ret == NULL) {
384 xmlGenericError(xmlGenericErrorContext,
385 "xmlSaveUri: out of memory\n");
386 return(NULL);
387 }
388 }
389 if ((IS_UNRESERVED(*(p))) ||
390 ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) ||
391 ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) ||
392 ((*(p) == '=')) || ((*(p) == '+')))
393 ret[len++] = *p++;
394 else {
395 int val = *(unsigned char *)p++;
396 int hi = val / 0x10, lo = val % 0x10;
397 ret[len++] = '%';
398 ret[len++] = hi + (hi > 9? 'A'-10 : '0');
399 ret[len++] = lo + (lo > 9? 'A'-10 : '0');
400 }
401 }
402 } else if (uri->scheme != NULL) {
403 if (len + 3 >= max) {
404 max *= 2;
405 ret = (xmlChar *) xmlRealloc(ret,
406 (max + 1) * sizeof(xmlChar));
407 if (ret == NULL) {
408 xmlGenericError(xmlGenericErrorContext,
409 "xmlSaveUri: out of memory\n");
410 return(NULL);
411 }
412 }
413 ret[len++] = '/';
414 ret[len++] = '/';
415 }
416 if (uri->path != NULL) {
417 p = uri->path;
418 while (*p != 0) {
419 if (len + 3 >= max) {
420 max *= 2;
421 ret = (xmlChar *) xmlRealloc(ret,
422 (max + 1) * sizeof(xmlChar));
423 if (ret == NULL) {
424 xmlGenericError(xmlGenericErrorContext,
425 "xmlSaveUri: out of memory\n");
426 return(NULL);
427 }
428 }
429 if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) ||
430 ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) ||
431 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||
432 ((*(p) == ',')))
433 ret[len++] = *p++;
434 else {
435 int val = *(unsigned char *)p++;
436 int hi = val / 0x10, lo = val % 0x10;
437 ret[len++] = '%';
438 ret[len++] = hi + (hi > 9? 'A'-10 : '0');
439 ret[len++] = lo + (lo > 9? 'A'-10 : '0');
440 }
441 }
442 }
443 if (uri->query != NULL) {
444 if (len + 3 >= max) {
445 max *= 2;
446 ret = (xmlChar *) xmlRealloc(ret,
447 (max + 1) * sizeof(xmlChar));
448 if (ret == NULL) {
449 xmlGenericError(xmlGenericErrorContext,
450 "xmlSaveUri: out of memory\n");
451 return(NULL);
452 }
453 }
454 ret[len++] = '?';
455 p = uri->query;
456 while (*p != 0) {
457 if (len + 3 >= max) {
458 max *= 2;
459 ret = (xmlChar *) xmlRealloc(ret,
460 (max + 1) * sizeof(xmlChar));
461 if (ret == NULL) {
462 xmlGenericError(xmlGenericErrorContext,
463 "xmlSaveUri: out of memory\n");
464 return(NULL);
465 }
466 }
467 if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
468 ret[len++] = *p++;
469 else {
470 int val = *(unsigned char *)p++;
471 int hi = val / 0x10, lo = val % 0x10;
472 ret[len++] = '%';
473 ret[len++] = hi + (hi > 9? 'A'-10 : '0');
474 ret[len++] = lo + (lo > 9? 'A'-10 : '0');
475 }
476 }
477 }
Daniel Veillardfdd27d22002-11-28 11:55:38 +0000478 }
479 if (uri->fragment != NULL) {
480 if (len + 3 >= max) {
481 max *= 2;
482 ret = (xmlChar *) xmlRealloc(ret,
483 (max + 1) * sizeof(xmlChar));
484 if (ret == NULL) {
485 xmlGenericError(xmlGenericErrorContext,
486 "xmlSaveUri: out of memory\n");
487 return(NULL);
488 }
489 }
490 ret[len++] = '#';
491 p = uri->fragment;
492 while (*p != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000493 if (len + 3 >= max) {
494 max *= 2;
495 ret = (xmlChar *) xmlRealloc(ret,
496 (max + 1) * sizeof(xmlChar));
497 if (ret == NULL) {
498 xmlGenericError(xmlGenericErrorContext,
499 "xmlSaveUri: out of memory\n");
500 return(NULL);
501 }
502 }
Daniel Veillardfdd27d22002-11-28 11:55:38 +0000503 if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
504 ret[len++] = *p++;
505 else {
506 int val = *(unsigned char *)p++;
507 int hi = val / 0x10, lo = val % 0x10;
508 ret[len++] = '%';
509 ret[len++] = hi + (hi > 9? 'A'-10 : '0');
510 ret[len++] = lo + (lo > 9? 'A'-10 : '0');
Owen Taylor3473f882001-02-23 17:55:21 +0000511 }
512 }
Owen Taylor3473f882001-02-23 17:55:21 +0000513 }
Daniel Veillardfdd27d22002-11-28 11:55:38 +0000514 if (len >= max) {
515 max *= 2;
516 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
517 if (ret == NULL) {
518 xmlGenericError(xmlGenericErrorContext,
519 "xmlSaveUri: out of memory\n");
520 return(NULL);
521 }
522 }
523 ret[len++] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000524 return(ret);
525}
526
527/**
528 * xmlPrintURI:
529 * @stream: a FILE* for the output
530 * @uri: pointer to an xmlURI
531 *
532 * Prints the URI in the stream @steam.
533 */
534void
535xmlPrintURI(FILE *stream, xmlURIPtr uri) {
536 xmlChar *out;
537
538 out = xmlSaveUri(uri);
539 if (out != NULL) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000540 fprintf(stream, "%s", (char *) out);
Owen Taylor3473f882001-02-23 17:55:21 +0000541 xmlFree(out);
542 }
543}
544
545/**
546 * xmlCleanURI:
547 * @uri: pointer to an xmlURI
548 *
549 * Make sure the xmlURI struct is free of content
550 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000551static void
Owen Taylor3473f882001-02-23 17:55:21 +0000552xmlCleanURI(xmlURIPtr uri) {
553 if (uri == NULL) return;
554
555 if (uri->scheme != NULL) xmlFree(uri->scheme);
556 uri->scheme = NULL;
557 if (uri->server != NULL) xmlFree(uri->server);
558 uri->server = NULL;
559 if (uri->user != NULL) xmlFree(uri->user);
560 uri->user = NULL;
561 if (uri->path != NULL) xmlFree(uri->path);
562 uri->path = NULL;
563 if (uri->fragment != NULL) xmlFree(uri->fragment);
564 uri->fragment = NULL;
565 if (uri->opaque != NULL) xmlFree(uri->opaque);
566 uri->opaque = NULL;
567 if (uri->authority != NULL) xmlFree(uri->authority);
568 uri->authority = NULL;
569 if (uri->query != NULL) xmlFree(uri->query);
570 uri->query = NULL;
571}
572
573/**
574 * xmlFreeURI:
575 * @uri: pointer to an xmlURI
576 *
577 * Free up the xmlURI struct
578 */
579void
580xmlFreeURI(xmlURIPtr uri) {
581 if (uri == NULL) return;
582
583 if (uri->scheme != NULL) xmlFree(uri->scheme);
584 if (uri->server != NULL) xmlFree(uri->server);
585 if (uri->user != NULL) xmlFree(uri->user);
586 if (uri->path != NULL) xmlFree(uri->path);
587 if (uri->fragment != NULL) xmlFree(uri->fragment);
588 if (uri->opaque != NULL) xmlFree(uri->opaque);
589 if (uri->authority != NULL) xmlFree(uri->authority);
590 if (uri->query != NULL) xmlFree(uri->query);
Owen Taylor3473f882001-02-23 17:55:21 +0000591 xmlFree(uri);
592}
593
594/************************************************************************
595 * *
596 * Helper functions *
597 * *
598 ************************************************************************/
599
Owen Taylor3473f882001-02-23 17:55:21 +0000600/**
601 * xmlNormalizeURIPath:
602 * @path: pointer to the path string
603 *
604 * Applies the 5 normalization steps to a path string--that is, RFC 2396
605 * Section 5.2, steps 6.c through 6.g.
606 *
607 * Normalization occurs directly on the string, no new allocation is done
608 *
609 * Returns 0 or an error code
610 */
611int
612xmlNormalizeURIPath(char *path) {
613 char *cur, *out;
614
615 if (path == NULL)
616 return(-1);
617
618 /* Skip all initial "/" chars. We want to get to the beginning of the
619 * first non-empty segment.
620 */
621 cur = path;
622 while (cur[0] == '/')
623 ++cur;
624 if (cur[0] == '\0')
625 return(0);
626
627 /* Keep everything we've seen so far. */
628 out = cur;
629
630 /*
631 * Analyze each segment in sequence for cases (c) and (d).
632 */
633 while (cur[0] != '\0') {
634 /*
635 * c) All occurrences of "./", where "." is a complete path segment,
636 * are removed from the buffer string.
637 */
638 if ((cur[0] == '.') && (cur[1] == '/')) {
639 cur += 2;
Daniel Veillardfcbd74a2001-06-26 07:47:23 +0000640 /* '//' normalization should be done at this point too */
641 while (cur[0] == '/')
642 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000643 continue;
644 }
645
646 /*
647 * d) If the buffer string ends with "." as a complete path segment,
648 * that "." is removed.
649 */
650 if ((cur[0] == '.') && (cur[1] == '\0'))
651 break;
652
653 /* Otherwise keep the segment. */
654 while (cur[0] != '/') {
655 if (cur[0] == '\0')
656 goto done_cd;
657 (out++)[0] = (cur++)[0];
658 }
Daniel Veillardfcbd74a2001-06-26 07:47:23 +0000659 /* nomalize // */
660 while ((cur[0] == '/') && (cur[1] == '/'))
661 cur++;
662
Owen Taylor3473f882001-02-23 17:55:21 +0000663 (out++)[0] = (cur++)[0];
664 }
665 done_cd:
666 out[0] = '\0';
667
668 /* Reset to the beginning of the first segment for the next sequence. */
669 cur = path;
670 while (cur[0] == '/')
671 ++cur;
672 if (cur[0] == '\0')
673 return(0);
674
675 /*
676 * Analyze each segment in sequence for cases (e) and (f).
677 *
678 * e) All occurrences of "<segment>/../", where <segment> is a
679 * complete path segment not equal to "..", are removed from the
680 * buffer string. Removal of these path segments is performed
681 * iteratively, removing the leftmost matching pattern on each
682 * iteration, until no matching pattern remains.
683 *
684 * f) If the buffer string ends with "<segment>/..", where <segment>
685 * is a complete path segment not equal to "..", that
686 * "<segment>/.." is removed.
687 *
688 * To satisfy the "iterative" clause in (e), we need to collapse the
689 * string every time we find something that needs to be removed. Thus,
690 * we don't need to keep two pointers into the string: we only need a
691 * "current position" pointer.
692 */
693 while (1) {
Daniel Veillard608d0ac2003-08-14 22:44:25 +0000694 char *segp, *tmp;
Owen Taylor3473f882001-02-23 17:55:21 +0000695
696 /* At the beginning of each iteration of this loop, "cur" points to
697 * the first character of the segment we want to examine.
698 */
699
700 /* Find the end of the current segment. */
701 segp = cur;
702 while ((segp[0] != '/') && (segp[0] != '\0'))
703 ++segp;
704
705 /* If this is the last segment, we're done (we need at least two
706 * segments to meet the criteria for the (e) and (f) cases).
707 */
708 if (segp[0] == '\0')
709 break;
710
711 /* If the first segment is "..", or if the next segment _isn't_ "..",
712 * keep this segment and try the next one.
713 */
714 ++segp;
715 if (((cur[0] == '.') && (cur[1] == '.') && (segp == cur+3))
716 || ((segp[0] != '.') || (segp[1] != '.')
717 || ((segp[2] != '/') && (segp[2] != '\0')))) {
718 cur = segp;
719 continue;
720 }
721
722 /* If we get here, remove this segment and the next one and back up
723 * to the previous segment (if there is one), to implement the
724 * "iteratively" clause. It's pretty much impossible to back up
725 * while maintaining two pointers into the buffer, so just compact
726 * the whole buffer now.
727 */
728
729 /* If this is the end of the buffer, we're done. */
730 if (segp[2] == '\0') {
731 cur[0] = '\0';
732 break;
733 }
Daniel Veillard608d0ac2003-08-14 22:44:25 +0000734 /* Valgrind complained, strcpy(cur, segp + 3); */
735 /* string will overlap, do not use strcpy */
736 tmp = cur;
737 segp += 3;
738 while ((*tmp++ = *segp++) != 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000739
740 /* If there are no previous segments, then keep going from here. */
741 segp = cur;
742 while ((segp > path) && ((--segp)[0] == '/'))
743 ;
744 if (segp == path)
745 continue;
746
747 /* "segp" is pointing to the end of a previous segment; find it's
748 * start. We need to back up to the previous segment and start
749 * over with that to handle things like "foo/bar/../..". If we
750 * don't do this, then on the first pass we'll remove the "bar/..",
751 * but be pointing at the second ".." so we won't realize we can also
752 * remove the "foo/..".
753 */
754 cur = segp;
755 while ((cur > path) && (cur[-1] != '/'))
756 --cur;
757 }
758 out[0] = '\0';
759
760 /*
761 * g) If the resulting buffer string still begins with one or more
762 * complete path segments of "..", then the reference is
763 * considered to be in error. Implementations may handle this
764 * error by retaining these components in the resolved path (i.e.,
765 * treating them as part of the final URI), by removing them from
766 * the resolved path (i.e., discarding relative levels above the
767 * root), or by avoiding traversal of the reference.
768 *
769 * We discard them from the final path.
770 */
771 if (path[0] == '/') {
772 cur = path;
Daniel Veillard9231ff92003-03-23 22:00:51 +0000773 while ((cur[0] == '/') && (cur[1] == '.') && (cur[2] == '.')
Owen Taylor3473f882001-02-23 17:55:21 +0000774 && ((cur[3] == '/') || (cur[3] == '\0')))
775 cur += 3;
776
777 if (cur != path) {
778 out = path;
779 while (cur[0] != '\0')
780 (out++)[0] = (cur++)[0];
781 out[0] = 0;
782 }
783 }
784
785 return(0);
786}
Owen Taylor3473f882001-02-23 17:55:21 +0000787
788/**
789 * xmlURIUnescapeString:
790 * @str: the string to unescape
Daniel Veillard60087f32001-10-10 09:45:09 +0000791 * @len: the length in bytes to unescape (or <= 0 to indicate full string)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000792 * @target: optional destination buffer
Owen Taylor3473f882001-02-23 17:55:21 +0000793 *
794 * Unescaping routine, does not do validity checks !
795 * Output is direct unsigned char translation of %XX values (no encoding)
796 *
797 * Returns an copy of the string, but unescaped
798 */
799char *
800xmlURIUnescapeString(const char *str, int len, char *target) {
801 char *ret, *out;
802 const char *in;
803
804 if (str == NULL)
805 return(NULL);
806 if (len <= 0) len = strlen(str);
Daniel Veillardd2298792003-02-14 16:54:11 +0000807 if (len < 0) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000808
809 if (target == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000810 ret = (char *) xmlMallocAtomic(len + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000811 if (ret == NULL) {
812 xmlGenericError(xmlGenericErrorContext,
813 "xmlURIUnescapeString: out of memory\n");
814 return(NULL);
815 }
816 } else
817 ret = target;
818 in = str;
819 out = ret;
820 while(len > 0) {
821 if (*in == '%') {
822 in++;
823 if ((*in >= '0') && (*in <= '9'))
824 *out = (*in - '0');
825 else if ((*in >= 'a') && (*in <= 'f'))
826 *out = (*in - 'a') + 10;
827 else if ((*in >= 'A') && (*in <= 'F'))
828 *out = (*in - 'A') + 10;
829 in++;
830 if ((*in >= '0') && (*in <= '9'))
831 *out = *out * 16 + (*in - '0');
832 else if ((*in >= 'a') && (*in <= 'f'))
833 *out = *out * 16 + (*in - 'a') + 10;
834 else if ((*in >= 'A') && (*in <= 'F'))
835 *out = *out * 16 + (*in - 'A') + 10;
836 in++;
837 len -= 3;
838 out++;
839 } else {
840 *out++ = *in++;
841 len--;
842 }
843 }
844 *out = 0;
845 return(ret);
846}
847
848/**
Daniel Veillard8514c672001-05-23 10:29:12 +0000849 * xmlURIEscapeStr:
850 * @str: string to escape
851 * @list: exception list string of chars not to escape
Owen Taylor3473f882001-02-23 17:55:21 +0000852 *
Daniel Veillard8514c672001-05-23 10:29:12 +0000853 * This routine escapes a string to hex, ignoring reserved characters (a-z)
854 * and the characters in the exception list.
Owen Taylor3473f882001-02-23 17:55:21 +0000855 *
Daniel Veillard8514c672001-05-23 10:29:12 +0000856 * Returns a new escaped string or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +0000857 */
858xmlChar *
Daniel Veillard8514c672001-05-23 10:29:12 +0000859xmlURIEscapeStr(const xmlChar *str, const xmlChar *list) {
860 xmlChar *ret, ch;
Owen Taylor3473f882001-02-23 17:55:21 +0000861 const xmlChar *in;
Daniel Veillard8514c672001-05-23 10:29:12 +0000862
Owen Taylor3473f882001-02-23 17:55:21 +0000863 unsigned int len, out;
864
865 if (str == NULL)
866 return(NULL);
867 len = xmlStrlen(str);
Daniel Veillarde645e8c2002-10-22 17:35:37 +0000868 if (!(len > 0)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000869
870 len += 20;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000871 ret = (xmlChar *) xmlMallocAtomic(len);
Owen Taylor3473f882001-02-23 17:55:21 +0000872 if (ret == NULL) {
873 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000874 "xmlURIEscapeStr: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000875 return(NULL);
876 }
877 in = (const xmlChar *) str;
878 out = 0;
879 while(*in != 0) {
880 if (len - out <= 3) {
881 len += 20;
882 ret = (xmlChar *) xmlRealloc(ret, len);
883 if (ret == NULL) {
884 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000885 "xmlURIEscapeStr: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000886 return(NULL);
887 }
888 }
Daniel Veillard8514c672001-05-23 10:29:12 +0000889
890 ch = *in;
891
Daniel Veillardeb475a32002-04-14 22:00:22 +0000892 if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!xmlStrchr(list, ch))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000893 unsigned char val;
894 ret[out++] = '%';
Daniel Veillard8514c672001-05-23 10:29:12 +0000895 val = ch >> 4;
Owen Taylor3473f882001-02-23 17:55:21 +0000896 if (val <= 9)
897 ret[out++] = '0' + val;
898 else
899 ret[out++] = 'A' + val - 0xA;
Daniel Veillard8514c672001-05-23 10:29:12 +0000900 val = ch & 0xF;
Owen Taylor3473f882001-02-23 17:55:21 +0000901 if (val <= 9)
902 ret[out++] = '0' + val;
903 else
904 ret[out++] = 'A' + val - 0xA;
905 in++;
906 } else {
907 ret[out++] = *in++;
908 }
Daniel Veillard8514c672001-05-23 10:29:12 +0000909
Owen Taylor3473f882001-02-23 17:55:21 +0000910 }
911 ret[out] = 0;
912 return(ret);
913}
914
Daniel Veillard8514c672001-05-23 10:29:12 +0000915/**
916 * xmlURIEscape:
917 * @str: the string of the URI to escape
918 *
919 * Escaping routine, does not do validity checks !
920 * It will try to escape the chars needing this, but this is heuristic
921 * based it's impossible to be sure.
922 *
Daniel Veillard8514c672001-05-23 10:29:12 +0000923 * Returns an copy of the string, but escaped
Daniel Veillard6278fb52001-05-25 07:38:41 +0000924 *
925 * 25 May 2001
926 * Uses xmlParseURI and xmlURIEscapeStr to try to escape correctly
927 * according to RFC2396.
928 * - Carl Douglas
Daniel Veillard8514c672001-05-23 10:29:12 +0000929 */
930xmlChar *
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000931xmlURIEscape(const xmlChar * str)
932{
Daniel Veillard6278fb52001-05-25 07:38:41 +0000933 xmlChar *ret, *segment = NULL;
934 xmlURIPtr uri;
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000935 int ret2;
Daniel Veillard8514c672001-05-23 10:29:12 +0000936
Daniel Veillard6278fb52001-05-25 07:38:41 +0000937#define NULLCHK(p) if(!p) { \
938 xmlGenericError(xmlGenericErrorContext, \
939 "xmlURIEscape: out of memory\n"); \
940 return NULL; }
941
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000942 if (str == NULL)
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000943 return (NULL);
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000944
945 uri = xmlCreateURI();
946 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000947 /*
948 * Allow escaping errors in the unescaped form
949 */
950 uri->cleanup = 1;
951 ret2 = xmlParseURIReference(uri, (const char *)str);
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000952 if (ret2) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000953 xmlFreeURI(uri);
954 return (NULL);
955 }
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000956 }
Daniel Veillard6278fb52001-05-25 07:38:41 +0000957
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000958 if (!uri)
959 return NULL;
Daniel Veillard6278fb52001-05-25 07:38:41 +0000960
961 ret = NULL;
962
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000963 if (uri->scheme) {
964 segment = xmlURIEscapeStr(BAD_CAST uri->scheme, BAD_CAST "+-.");
965 NULLCHK(segment)
966 ret = xmlStrcat(ret, segment);
967 ret = xmlStrcat(ret, BAD_CAST ":");
968 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +0000969 }
970
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000971 if (uri->authority) {
972 segment =
973 xmlURIEscapeStr(BAD_CAST uri->authority, BAD_CAST "/?;:@");
974 NULLCHK(segment)
975 ret = xmlStrcat(ret, BAD_CAST "//");
976 ret = xmlStrcat(ret, segment);
977 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +0000978 }
979
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000980 if (uri->user) {
981 segment = xmlURIEscapeStr(BAD_CAST uri->user, BAD_CAST ";:&=+$,");
982 NULLCHK(segment)
Daniel Veillard0a194582004-04-01 20:09:22 +0000983 ret = xmlStrcat(ret,BAD_CAST "//");
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000984 ret = xmlStrcat(ret, segment);
985 ret = xmlStrcat(ret, BAD_CAST "@");
986 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +0000987 }
988
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000989 if (uri->server) {
990 segment = xmlURIEscapeStr(BAD_CAST uri->server, BAD_CAST "/?;:@");
991 NULLCHK(segment)
Daniel Veillard0a194582004-04-01 20:09:22 +0000992 if (uri->user == NULL)
993 ret = xmlStrcat(ret, BAD_CAST "//");
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000994 ret = xmlStrcat(ret, segment);
995 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +0000996 }
997
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000998 if (uri->port) {
999 xmlChar port[10];
1000
Daniel Veillard43d3f612001-11-10 11:57:23 +00001001 snprintf((char *) port, 10, "%d", uri->port);
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001002 ret = xmlStrcat(ret, BAD_CAST ":");
1003 ret = xmlStrcat(ret, port);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001004 }
1005
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001006 if (uri->path) {
1007 segment =
1008 xmlURIEscapeStr(BAD_CAST uri->path, BAD_CAST ":@&=+$,/?;");
1009 NULLCHK(segment)
1010 ret = xmlStrcat(ret, segment);
1011 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001012 }
1013
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001014 if (uri->query) {
1015 segment =
1016 xmlURIEscapeStr(BAD_CAST uri->query, BAD_CAST ";/?:@&=+,$");
1017 NULLCHK(segment)
1018 ret = xmlStrcat(ret, BAD_CAST "?");
1019 ret = xmlStrcat(ret, segment);
1020 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001021 }
1022
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001023 if (uri->opaque) {
1024 segment = xmlURIEscapeStr(BAD_CAST uri->opaque, BAD_CAST "");
1025 NULLCHK(segment)
1026 ret = xmlStrcat(ret, segment);
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001027 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001028 }
1029
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001030 if (uri->fragment) {
1031 segment = xmlURIEscapeStr(BAD_CAST uri->fragment, BAD_CAST "#");
1032 NULLCHK(segment)
1033 ret = xmlStrcat(ret, BAD_CAST "#");
1034 ret = xmlStrcat(ret, segment);
1035 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001036 }
Daniel Veillard43d3f612001-11-10 11:57:23 +00001037
1038 xmlFreeURI(uri);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001039#undef NULLCHK
Daniel Veillard8514c672001-05-23 10:29:12 +00001040
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001041 return (ret);
Daniel Veillard8514c672001-05-23 10:29:12 +00001042}
1043
Owen Taylor3473f882001-02-23 17:55:21 +00001044/************************************************************************
1045 * *
1046 * Escaped URI parsing *
1047 * *
1048 ************************************************************************/
1049
1050/**
1051 * xmlParseURIFragment:
1052 * @uri: pointer to an URI structure
1053 * @str: pointer to the string to analyze
1054 *
1055 * Parse an URI fragment string and fills in the appropriate fields
1056 * of the @uri structure.
1057 *
1058 * fragment = *uric
1059 *
1060 * Returns 0 or the error code
1061 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001062static int
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001063xmlParseURIFragment(xmlURIPtr uri, const char **str)
1064{
Owen Taylor3473f882001-02-23 17:55:21 +00001065 const char *cur = *str;
1066
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001067 if (str == NULL)
1068 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001069
Daniel Veillardfdd27d22002-11-28 11:55:38 +00001070 while (IS_URIC(cur) || IS_UNWISE(cur))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001071 NEXT(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001072 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001073 if (uri->fragment != NULL)
1074 xmlFree(uri->fragment);
1075 uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001076 }
1077 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001078 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001079}
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
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001093xmlParseURIQuery(xmlURIPtr uri, const char **str)
1094{
Owen Taylor3473f882001-02-23 17:55:21 +00001095 const char *cur = *str;
1096
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001097 if (str == NULL)
1098 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001099
Daniel Veillard9231ff92003-03-23 22:00:51 +00001100 while (IS_URIC(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001101 NEXT(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001102 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001103 if (uri->query != NULL)
1104 xmlFree(uri->query);
1105 uri->query = xmlURIUnescapeString(*str, cur - *str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001106 }
1107 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001108 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001109}
1110
1111/**
1112 * xmlParseURIScheme:
1113 * @uri: pointer to an URI structure
1114 * @str: pointer to the string to analyze
1115 *
1116 * Parse an URI scheme
1117 *
1118 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
1119 *
1120 * Returns 0 or the error code
1121 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001122static int
Owen Taylor3473f882001-02-23 17:55:21 +00001123xmlParseURIScheme(xmlURIPtr uri, const char **str) {
1124 const char *cur;
1125
1126 if (str == NULL)
1127 return(-1);
1128
1129 cur = *str;
1130 if (!IS_ALPHA(*cur))
1131 return(2);
1132 cur++;
1133 while (IS_SCHEME(*cur)) cur++;
1134 if (uri != NULL) {
1135 if (uri->scheme != NULL) xmlFree(uri->scheme);
1136 /* !!! strndup */
1137 uri->scheme = xmlURIUnescapeString(*str, cur - *str, NULL);
1138 }
1139 *str = cur;
1140 return(0);
1141}
1142
1143/**
1144 * xmlParseURIOpaquePart:
1145 * @uri: pointer to an URI structure
1146 * @str: pointer to the string to analyze
1147 *
1148 * Parse an URI opaque part
1149 *
1150 * opaque_part = uric_no_slash *uric
1151 *
1152 * Returns 0 or the error code
1153 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001154static int
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001155xmlParseURIOpaquePart(xmlURIPtr uri, const char **str)
1156{
Owen Taylor3473f882001-02-23 17:55:21 +00001157 const char *cur;
1158
1159 if (str == NULL)
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001160 return (-1);
1161
Owen Taylor3473f882001-02-23 17:55:21 +00001162 cur = *str;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001163 if (!(IS_URIC_NO_SLASH(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001164 return (3);
Owen Taylor3473f882001-02-23 17:55:21 +00001165 }
1166 NEXT(cur);
Daniel Veillard9231ff92003-03-23 22:00:51 +00001167 while (IS_URIC(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001168 NEXT(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001169 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001170 if (uri->opaque != NULL)
1171 xmlFree(uri->opaque);
1172 uri->opaque = xmlURIUnescapeString(*str, cur - *str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001173 }
1174 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001175 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001176}
1177
1178/**
1179 * xmlParseURIServer:
1180 * @uri: pointer to an URI structure
1181 * @str: pointer to the string to analyze
1182 *
1183 * Parse a server subpart of an URI, it's a finer grain analysis
1184 * of the authority part.
1185 *
1186 * server = [ [ userinfo "@" ] hostport ]
1187 * userinfo = *( unreserved | escaped |
1188 * ";" | ":" | "&" | "=" | "+" | "$" | "," )
1189 * hostport = host [ ":" port ]
1190 * host = hostname | IPv4address
1191 * hostname = *( domainlabel "." ) toplabel [ "." ]
1192 * domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
1193 * toplabel = alpha | alpha *( alphanum | "-" ) alphanum
1194 * IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
1195 * port = *digit
1196 *
1197 * Returns 0 or the error code
1198 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001199static int
Owen Taylor3473f882001-02-23 17:55:21 +00001200xmlParseURIServer(xmlURIPtr uri, const char **str) {
1201 const char *cur;
1202 const char *host, *tmp;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001203 const int IPmax = 4;
1204 int oct;
Owen Taylor3473f882001-02-23 17:55:21 +00001205
1206 if (str == NULL)
1207 return(-1);
1208
1209 cur = *str;
1210
1211 /*
1212 * is there an userinfo ?
1213 */
1214 while (IS_USERINFO(cur)) NEXT(cur);
1215 if (*cur == '@') {
1216 if (uri != NULL) {
1217 if (uri->user != NULL) xmlFree(uri->user);
1218 uri->user = xmlURIUnescapeString(*str, cur - *str, NULL);
1219 }
1220 cur++;
1221 } else {
1222 if (uri != NULL) {
1223 if (uri->user != NULL) xmlFree(uri->user);
1224 uri->user = NULL;
1225 }
1226 cur = *str;
1227 }
1228 /*
1229 * This can be empty in the case where there is no server
1230 */
1231 host = cur;
1232 if (*cur == '/') {
1233 if (uri != NULL) {
1234 if (uri->authority != NULL) xmlFree(uri->authority);
1235 uri->authority = NULL;
1236 if (uri->server != NULL) xmlFree(uri->server);
1237 uri->server = NULL;
1238 uri->port = 0;
1239 }
1240 return(0);
1241 }
1242 /*
1243 * host part of hostport can derive either an IPV4 address
1244 * or an unresolved name. Check the IP first, it easier to detect
1245 * errors if wrong one
1246 */
Daniel Veillard9231ff92003-03-23 22:00:51 +00001247 for (oct = 0; oct < IPmax; ++oct) {
1248 if (*cur == '.')
1249 return(3); /* e.g. http://.xml/ or http://18.29..30/ */
Owen Taylor3473f882001-02-23 17:55:21 +00001250 while(IS_DIGIT(*cur)) cur++;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001251 if (oct == (IPmax-1))
1252 continue;
1253 if (*cur != '.')
1254 break;
1255 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00001256 }
Daniel Veillard9231ff92003-03-23 22:00:51 +00001257 if (oct < IPmax || (*cur == '.' && cur++) || IS_ALPHA(*cur)) {
1258 /* maybe host_name */
1259 if (!IS_ALPHANUM(*cur))
1260 return(4); /* e.g. http://xml.$oft */
1261 do {
1262 do ++cur; while (IS_ALPHANUM(*cur));
1263 if (*cur == '-') {
1264 --cur;
1265 if (*cur == '.')
1266 return(5); /* e.g. http://xml.-soft */
1267 ++cur;
1268 continue;
1269 }
1270 if (*cur == '.') {
1271 --cur;
1272 if (*cur == '-')
1273 return(6); /* e.g. http://xml-.soft */
1274 if (*cur == '.')
1275 return(7); /* e.g. http://xml..soft */
1276 ++cur;
1277 continue;
1278 }
1279 break;
1280 } while (1);
1281 tmp = cur;
1282 if (tmp[-1] == '.')
1283 --tmp; /* e.g. http://xml.$Oft/ */
1284 do --tmp; while (tmp >= host && IS_ALPHANUM(*tmp));
1285 if ((++tmp == host || tmp[-1] == '.') && !IS_ALPHA(*tmp))
1286 return(8); /* e.g. http://xmlsOft.0rg/ */
Owen Taylor3473f882001-02-23 17:55:21 +00001287 }
Owen Taylor3473f882001-02-23 17:55:21 +00001288 if (uri != NULL) {
1289 if (uri->authority != NULL) xmlFree(uri->authority);
1290 uri->authority = NULL;
1291 if (uri->server != NULL) xmlFree(uri->server);
1292 uri->server = xmlURIUnescapeString(host, cur - host, NULL);
1293 }
Owen Taylor3473f882001-02-23 17:55:21 +00001294 /*
1295 * finish by checking for a port presence.
1296 */
1297 if (*cur == ':') {
1298 cur++;
1299 if (IS_DIGIT(*cur)) {
1300 if (uri != NULL)
1301 uri->port = 0;
1302 while (IS_DIGIT(*cur)) {
1303 if (uri != NULL)
1304 uri->port = uri->port * 10 + (*cur - '0');
1305 cur++;
1306 }
1307 }
1308 }
1309 *str = cur;
1310 return(0);
1311}
1312
1313/**
1314 * xmlParseURIRelSegment:
1315 * @uri: pointer to an URI structure
1316 * @str: pointer to the string to analyze
1317 *
1318 * Parse an URI relative segment
1319 *
1320 * rel_segment = 1*( unreserved | escaped | ";" | "@" | "&" | "=" |
1321 * "+" | "$" | "," )
1322 *
1323 * Returns 0 or the error code
1324 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001325static int
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001326xmlParseURIRelSegment(xmlURIPtr uri, const char **str)
1327{
Owen Taylor3473f882001-02-23 17:55:21 +00001328 const char *cur;
1329
1330 if (str == NULL)
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001331 return (-1);
1332
Owen Taylor3473f882001-02-23 17:55:21 +00001333 cur = *str;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001334 if (!(IS_SEGMENT(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001335 return (3);
Owen Taylor3473f882001-02-23 17:55:21 +00001336 }
1337 NEXT(cur);
Daniel Veillard9231ff92003-03-23 22:00:51 +00001338 while (IS_SEGMENT(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001339 NEXT(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001340 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001341 if (uri->path != NULL)
1342 xmlFree(uri->path);
1343 uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001344 }
1345 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001346 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001347}
1348
1349/**
1350 * xmlParseURIPathSegments:
1351 * @uri: pointer to an URI structure
1352 * @str: pointer to the string to analyze
1353 * @slash: should we add a leading slash
1354 *
1355 * Parse an URI set of path segments
1356 *
1357 * path_segments = segment *( "/" segment )
1358 * segment = *pchar *( ";" param )
1359 * param = *pchar
1360 *
1361 * Returns 0 or the error code
1362 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001363static int
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001364xmlParseURIPathSegments(xmlURIPtr uri, const char **str, int slash)
1365{
Owen Taylor3473f882001-02-23 17:55:21 +00001366 const char *cur;
1367
1368 if (str == NULL)
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001369 return (-1);
1370
Owen Taylor3473f882001-02-23 17:55:21 +00001371 cur = *str;
1372
1373 do {
Daniel Veillard9231ff92003-03-23 22:00:51 +00001374 while (IS_PCHAR(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001375 NEXT(cur);
Daniel Veillard234bc4e2002-05-24 11:03:05 +00001376 while (*cur == ';') {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001377 cur++;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001378 while (IS_PCHAR(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001379 NEXT(cur);
1380 }
1381 if (*cur != '/')
1382 break;
1383 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00001384 } while (1);
1385 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001386 int len, len2 = 0;
1387 char *path;
Owen Taylor3473f882001-02-23 17:55:21 +00001388
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001389 /*
1390 * Concat the set of path segments to the current path
1391 */
1392 len = cur - *str;
1393 if (slash)
1394 len++;
Owen Taylor3473f882001-02-23 17:55:21 +00001395
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001396 if (uri->path != NULL) {
1397 len2 = strlen(uri->path);
1398 len += len2;
1399 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001400 path = (char *) xmlMallocAtomic(len + 1);
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001401 if (path == NULL) {
1402 xmlGenericError(xmlGenericErrorContext,
1403 "xmlParseURIPathSegments: out of memory\n");
1404 *str = cur;
1405 return (-1);
1406 }
1407 if (uri->path != NULL)
1408 memcpy(path, uri->path, len2);
1409 if (slash) {
1410 path[len2] = '/';
1411 len2++;
1412 }
1413 path[len2] = 0;
1414 if (cur - *str > 0)
1415 xmlURIUnescapeString(*str, cur - *str, &path[len2]);
1416 if (uri->path != NULL)
1417 xmlFree(uri->path);
1418 uri->path = path;
Owen Taylor3473f882001-02-23 17:55:21 +00001419 }
1420 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001421 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001422}
1423
1424/**
1425 * xmlParseURIAuthority:
1426 * @uri: pointer to an URI structure
1427 * @str: pointer to the string to analyze
1428 *
1429 * Parse the authority part of an URI.
1430 *
1431 * authority = server | reg_name
1432 * server = [ [ userinfo "@" ] hostport ]
1433 * reg_name = 1*( unreserved | escaped | "$" | "," | ";" | ":" |
1434 * "@" | "&" | "=" | "+" )
1435 *
1436 * Note : this is completely ambiguous since reg_name is allowed to
1437 * use the full set of chars in use by server:
1438 *
1439 * 3.2.1. Registry-based Naming Authority
1440 *
1441 * The structure of a registry-based naming authority is specific
1442 * to the URI scheme, but constrained to the allowed characters
1443 * for an authority component.
1444 *
1445 * Returns 0 or the error code
1446 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001447static int
Owen Taylor3473f882001-02-23 17:55:21 +00001448xmlParseURIAuthority(xmlURIPtr uri, const char **str) {
1449 const char *cur;
1450 int ret;
1451
1452 if (str == NULL)
1453 return(-1);
1454
1455 cur = *str;
1456
1457 /*
1458 * try first to parse it as a server string.
1459 */
1460 ret = xmlParseURIServer(uri, str);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001461 if ((ret == 0) && (*str != NULL) &&
1462 ((**str == 0) || (**str == '/') || (**str == '?')))
Owen Taylor3473f882001-02-23 17:55:21 +00001463 return(0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001464 *str = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001465
1466 /*
1467 * failed, fallback to reg_name
1468 */
1469 if (!IS_REG_NAME(cur)) {
1470 return(5);
1471 }
1472 NEXT(cur);
1473 while (IS_REG_NAME(cur)) NEXT(cur);
1474 if (uri != NULL) {
1475 if (uri->server != NULL) xmlFree(uri->server);
1476 uri->server = NULL;
1477 if (uri->user != NULL) xmlFree(uri->user);
1478 uri->user = NULL;
1479 if (uri->authority != NULL) xmlFree(uri->authority);
1480 uri->authority = xmlURIUnescapeString(*str, cur - *str, NULL);
1481 }
1482 *str = cur;
1483 return(0);
1484}
1485
1486/**
1487 * xmlParseURIHierPart:
1488 * @uri: pointer to an URI structure
1489 * @str: pointer to the string to analyze
1490 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001491 * Parse an URI hierarchical part
Owen Taylor3473f882001-02-23 17:55:21 +00001492 *
1493 * hier_part = ( net_path | abs_path ) [ "?" query ]
1494 * abs_path = "/" path_segments
1495 * net_path = "//" authority [ abs_path ]
1496 *
1497 * Returns 0 or the error code
1498 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001499static int
Owen Taylor3473f882001-02-23 17:55:21 +00001500xmlParseURIHierPart(xmlURIPtr uri, const char **str) {
1501 int ret;
1502 const char *cur;
1503
1504 if (str == NULL)
1505 return(-1);
1506
1507 cur = *str;
1508
1509 if ((cur[0] == '/') && (cur[1] == '/')) {
1510 cur += 2;
1511 ret = xmlParseURIAuthority(uri, &cur);
1512 if (ret != 0)
1513 return(ret);
1514 if (cur[0] == '/') {
1515 cur++;
1516 ret = xmlParseURIPathSegments(uri, &cur, 1);
1517 }
1518 } else if (cur[0] == '/') {
1519 cur++;
1520 ret = xmlParseURIPathSegments(uri, &cur, 1);
1521 } else {
1522 return(4);
1523 }
1524 if (ret != 0)
1525 return(ret);
1526 if (*cur == '?') {
1527 cur++;
1528 ret = xmlParseURIQuery(uri, &cur);
1529 if (ret != 0)
1530 return(ret);
1531 }
1532 *str = cur;
1533 return(0);
1534}
1535
1536/**
1537 * xmlParseAbsoluteURI:
1538 * @uri: pointer to an URI structure
1539 * @str: pointer to the string to analyze
1540 *
1541 * Parse an URI reference string and fills in the appropriate fields
1542 * of the @uri structure
1543 *
1544 * absoluteURI = scheme ":" ( hier_part | opaque_part )
1545 *
1546 * Returns 0 or the error code
1547 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001548static int
Owen Taylor3473f882001-02-23 17:55:21 +00001549xmlParseAbsoluteURI(xmlURIPtr uri, const char **str) {
1550 int ret;
Daniel Veillard20ee8c02001-10-05 09:18:14 +00001551 const char *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001552
1553 if (str == NULL)
1554 return(-1);
1555
Daniel Veillard20ee8c02001-10-05 09:18:14 +00001556 cur = *str;
1557
Owen Taylor3473f882001-02-23 17:55:21 +00001558 ret = xmlParseURIScheme(uri, str);
1559 if (ret != 0) return(ret);
Daniel Veillard20ee8c02001-10-05 09:18:14 +00001560 if (**str != ':') {
1561 *str = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001562 return(1);
Daniel Veillard20ee8c02001-10-05 09:18:14 +00001563 }
Owen Taylor3473f882001-02-23 17:55:21 +00001564 (*str)++;
1565 if (**str == '/')
1566 return(xmlParseURIHierPart(uri, str));
1567 return(xmlParseURIOpaquePart(uri, str));
1568}
1569
1570/**
1571 * xmlParseRelativeURI:
1572 * @uri: pointer to an URI structure
1573 * @str: pointer to the string to analyze
1574 *
1575 * Parse an relative URI string and fills in the appropriate fields
1576 * of the @uri structure
1577 *
1578 * relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
1579 * abs_path = "/" path_segments
1580 * net_path = "//" authority [ abs_path ]
1581 * rel_path = rel_segment [ abs_path ]
1582 *
1583 * Returns 0 or the error code
1584 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001585static int
Owen Taylor3473f882001-02-23 17:55:21 +00001586xmlParseRelativeURI(xmlURIPtr uri, const char **str) {
1587 int ret = 0;
1588 const char *cur;
1589
1590 if (str == NULL)
1591 return(-1);
1592
1593 cur = *str;
1594 if ((cur[0] == '/') && (cur[1] == '/')) {
1595 cur += 2;
1596 ret = xmlParseURIAuthority(uri, &cur);
1597 if (ret != 0)
1598 return(ret);
1599 if (cur[0] == '/') {
1600 cur++;
1601 ret = xmlParseURIPathSegments(uri, &cur, 1);
1602 }
1603 } else if (cur[0] == '/') {
1604 cur++;
1605 ret = xmlParseURIPathSegments(uri, &cur, 1);
1606 } else if (cur[0] != '#' && cur[0] != '?') {
1607 ret = xmlParseURIRelSegment(uri, &cur);
1608 if (ret != 0)
1609 return(ret);
1610 if (cur[0] == '/') {
1611 cur++;
1612 ret = xmlParseURIPathSegments(uri, &cur, 1);
1613 }
1614 }
1615 if (ret != 0)
1616 return(ret);
1617 if (*cur == '?') {
1618 cur++;
1619 ret = xmlParseURIQuery(uri, &cur);
1620 if (ret != 0)
1621 return(ret);
1622 }
1623 *str = cur;
1624 return(ret);
1625}
1626
1627/**
1628 * xmlParseURIReference:
1629 * @uri: pointer to an URI structure
1630 * @str: the string to analyze
1631 *
1632 * Parse an URI reference string and fills in the appropriate fields
1633 * of the @uri structure
1634 *
1635 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
1636 *
1637 * Returns 0 or the error code
1638 */
1639int
1640xmlParseURIReference(xmlURIPtr uri, const char *str) {
1641 int ret;
1642 const char *tmp = str;
1643
1644 if (str == NULL)
1645 return(-1);
1646 xmlCleanURI(uri);
1647
1648 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001649 * Try first to parse absolute refs, then fallback to relative if
Owen Taylor3473f882001-02-23 17:55:21 +00001650 * it fails.
1651 */
1652 ret = xmlParseAbsoluteURI(uri, &str);
1653 if (ret != 0) {
1654 xmlCleanURI(uri);
1655 str = tmp;
1656 ret = xmlParseRelativeURI(uri, &str);
1657 }
1658 if (ret != 0) {
1659 xmlCleanURI(uri);
1660 return(ret);
1661 }
1662
1663 if (*str == '#') {
1664 str++;
1665 ret = xmlParseURIFragment(uri, &str);
1666 if (ret != 0) return(ret);
1667 }
1668 if (*str != 0) {
1669 xmlCleanURI(uri);
1670 return(1);
1671 }
1672 return(0);
1673}
1674
1675/**
1676 * xmlParseURI:
1677 * @str: the URI string to analyze
1678 *
1679 * Parse an URI
1680 *
1681 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
1682 *
1683 * Returns a newly build xmlURIPtr or NULL in case of error
1684 */
1685xmlURIPtr
1686xmlParseURI(const char *str) {
1687 xmlURIPtr uri;
1688 int ret;
1689
1690 if (str == NULL)
1691 return(NULL);
1692 uri = xmlCreateURI();
1693 if (uri != NULL) {
1694 ret = xmlParseURIReference(uri, str);
1695 if (ret) {
1696 xmlFreeURI(uri);
1697 return(NULL);
1698 }
1699 }
1700 return(uri);
1701}
1702
1703/************************************************************************
1704 * *
1705 * Public functions *
1706 * *
1707 ************************************************************************/
1708
1709/**
1710 * xmlBuildURI:
1711 * @URI: the URI instance found in the document
1712 * @base: the base value
1713 *
1714 * Computes he final URI of the reference done by checking that
1715 * the given URI is valid, and building the final URI using the
1716 * base URI. This is processed according to section 5.2 of the
1717 * RFC 2396
1718 *
1719 * 5.2. Resolving Relative References to Absolute Form
1720 *
1721 * Returns a new URI string (to be freed by the caller) or NULL in case
1722 * of error.
1723 */
1724xmlChar *
1725xmlBuildURI(const xmlChar *URI, const xmlChar *base) {
1726 xmlChar *val = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001727 int ret, len, indx, cur, out;
Owen Taylor3473f882001-02-23 17:55:21 +00001728 xmlURIPtr ref = NULL;
1729 xmlURIPtr bas = NULL;
1730 xmlURIPtr res = NULL;
1731
1732 /*
1733 * 1) The URI reference is parsed into the potential four components and
1734 * fragment identifier, as described in Section 4.3.
1735 *
1736 * NOTE that a completely empty URI is treated by modern browsers
1737 * as a reference to "." rather than as a synonym for the current
1738 * URI. Should we do that here?
1739 */
1740 if (URI == NULL)
1741 ret = -1;
1742 else {
1743 if (*URI) {
1744 ref = xmlCreateURI();
1745 if (ref == NULL)
1746 goto done;
1747 ret = xmlParseURIReference(ref, (const char *) URI);
1748 }
1749 else
1750 ret = 0;
1751 }
1752 if (ret != 0)
1753 goto done;
Daniel Veillard7b4b2f92003-01-06 13:11:20 +00001754 if ((ref != NULL) && (ref->scheme != NULL)) {
1755 /*
1756 * The URI is absolute don't modify.
1757 */
1758 val = xmlStrdup(URI);
1759 goto done;
1760 }
Owen Taylor3473f882001-02-23 17:55:21 +00001761 if (base == NULL)
1762 ret = -1;
1763 else {
1764 bas = xmlCreateURI();
1765 if (bas == NULL)
1766 goto done;
1767 ret = xmlParseURIReference(bas, (const char *) base);
1768 }
1769 if (ret != 0) {
1770 if (ref)
1771 val = xmlSaveUri(ref);
1772 goto done;
1773 }
1774 if (ref == NULL) {
1775 /*
1776 * the base fragment must be ignored
1777 */
1778 if (bas->fragment != NULL) {
1779 xmlFree(bas->fragment);
1780 bas->fragment = NULL;
1781 }
1782 val = xmlSaveUri(bas);
1783 goto done;
1784 }
1785
1786 /*
1787 * 2) If the path component is empty and the scheme, authority, and
1788 * query components are undefined, then it is a reference to the
1789 * current document and we are done. Otherwise, the reference URI's
1790 * query and fragment components are defined as found (or not found)
1791 * within the URI reference and not inherited from the base URI.
1792 *
1793 * NOTE that in modern browsers, the parsing differs from the above
1794 * in the following aspect: the query component is allowed to be
1795 * defined while still treating this as a reference to the current
1796 * document.
1797 */
1798 res = xmlCreateURI();
1799 if (res == NULL)
1800 goto done;
1801 if ((ref->scheme == NULL) && (ref->path == NULL) &&
1802 ((ref->authority == NULL) && (ref->server == NULL))) {
1803 if (bas->scheme != NULL)
1804 res->scheme = xmlMemStrdup(bas->scheme);
1805 if (bas->authority != NULL)
1806 res->authority = xmlMemStrdup(bas->authority);
1807 else if (bas->server != NULL) {
1808 res->server = xmlMemStrdup(bas->server);
1809 if (bas->user != NULL)
1810 res->user = xmlMemStrdup(bas->user);
1811 res->port = bas->port;
1812 }
1813 if (bas->path != NULL)
1814 res->path = xmlMemStrdup(bas->path);
1815 if (ref->query != NULL)
1816 res->query = xmlMemStrdup(ref->query);
1817 else if (bas->query != NULL)
1818 res->query = xmlMemStrdup(bas->query);
1819 if (ref->fragment != NULL)
1820 res->fragment = xmlMemStrdup(ref->fragment);
1821 goto step_7;
1822 }
Owen Taylor3473f882001-02-23 17:55:21 +00001823
1824 /*
1825 * 3) If the scheme component is defined, indicating that the reference
1826 * starts with a scheme name, then the reference is interpreted as an
1827 * absolute URI and we are done. Otherwise, the reference URI's
1828 * scheme is inherited from the base URI's scheme component.
1829 */
1830 if (ref->scheme != NULL) {
1831 val = xmlSaveUri(ref);
1832 goto done;
1833 }
1834 if (bas->scheme != NULL)
1835 res->scheme = xmlMemStrdup(bas->scheme);
Daniel Veillard9231ff92003-03-23 22:00:51 +00001836
1837 if (ref->query != NULL)
1838 res->query = xmlMemStrdup(ref->query);
1839 if (ref->fragment != NULL)
1840 res->fragment = xmlMemStrdup(ref->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001841
1842 /*
1843 * 4) If the authority component is defined, then the reference is a
1844 * network-path and we skip to step 7. Otherwise, the reference
1845 * URI's authority is inherited from the base URI's authority
1846 * component, which will also be undefined if the URI scheme does not
1847 * use an authority component.
1848 */
1849 if ((ref->authority != NULL) || (ref->server != NULL)) {
1850 if (ref->authority != NULL)
1851 res->authority = xmlMemStrdup(ref->authority);
1852 else {
1853 res->server = xmlMemStrdup(ref->server);
1854 if (ref->user != NULL)
1855 res->user = xmlMemStrdup(ref->user);
1856 res->port = ref->port;
1857 }
1858 if (ref->path != NULL)
1859 res->path = xmlMemStrdup(ref->path);
1860 goto step_7;
1861 }
1862 if (bas->authority != NULL)
1863 res->authority = xmlMemStrdup(bas->authority);
1864 else if (bas->server != NULL) {
1865 res->server = xmlMemStrdup(bas->server);
1866 if (bas->user != NULL)
1867 res->user = xmlMemStrdup(bas->user);
1868 res->port = bas->port;
1869 }
1870
1871 /*
1872 * 5) If the path component begins with a slash character ("/"), then
1873 * the reference is an absolute-path and we skip to step 7.
1874 */
1875 if ((ref->path != NULL) && (ref->path[0] == '/')) {
1876 res->path = xmlMemStrdup(ref->path);
1877 goto step_7;
1878 }
1879
1880
1881 /*
1882 * 6) If this step is reached, then we are resolving a relative-path
1883 * reference. The relative path needs to be merged with the base
1884 * URI's path. Although there are many ways to do this, we will
1885 * describe a simple method using a separate string buffer.
1886 *
1887 * Allocate a buffer large enough for the result string.
1888 */
1889 len = 2; /* extra / and 0 */
1890 if (ref->path != NULL)
1891 len += strlen(ref->path);
1892 if (bas->path != NULL)
1893 len += strlen(bas->path);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001894 res->path = (char *) xmlMallocAtomic(len);
Owen Taylor3473f882001-02-23 17:55:21 +00001895 if (res->path == NULL) {
1896 xmlGenericError(xmlGenericErrorContext,
1897 "xmlBuildURI: out of memory\n");
1898 goto done;
1899 }
1900 res->path[0] = 0;
1901
1902 /*
1903 * a) All but the last segment of the base URI's path component is
1904 * copied to the buffer. In other words, any characters after the
1905 * last (right-most) slash character, if any, are excluded.
1906 */
1907 cur = 0;
1908 out = 0;
1909 if (bas->path != NULL) {
1910 while (bas->path[cur] != 0) {
1911 while ((bas->path[cur] != 0) && (bas->path[cur] != '/'))
1912 cur++;
1913 if (bas->path[cur] == 0)
1914 break;
1915
1916 cur++;
1917 while (out < cur) {
1918 res->path[out] = bas->path[out];
1919 out++;
1920 }
1921 }
1922 }
1923 res->path[out] = 0;
1924
1925 /*
1926 * b) The reference's path component is appended to the buffer
1927 * string.
1928 */
1929 if (ref->path != NULL && ref->path[0] != 0) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001930 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001931 /*
1932 * Ensure the path includes a '/'
1933 */
1934 if ((out == 0) && (bas->server != NULL))
1935 res->path[out++] = '/';
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001936 while (ref->path[indx] != 0) {
1937 res->path[out++] = ref->path[indx++];
Owen Taylor3473f882001-02-23 17:55:21 +00001938 }
1939 }
1940 res->path[out] = 0;
1941
1942 /*
1943 * Steps c) to h) are really path normalization steps
1944 */
1945 xmlNormalizeURIPath(res->path);
1946
1947step_7:
1948
1949 /*
1950 * 7) The resulting URI components, including any inherited from the
1951 * base URI, are recombined to give the absolute form of the URI
1952 * reference.
1953 */
1954 val = xmlSaveUri(res);
1955
1956done:
1957 if (ref != NULL)
1958 xmlFreeURI(ref);
1959 if (bas != NULL)
1960 xmlFreeURI(bas);
1961 if (res != NULL)
1962 xmlFreeURI(res);
1963 return(val);
1964}
1965
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00001966/**
1967 * xmlCanonicPath:
1968 * @path: the resource locator in a filesystem notation
1969 *
1970 * Constructs a canonic path from the specified path.
1971 *
1972 * Returns a new canonic path, or a duplicate of the path parameter if the
1973 * construction fails. The caller is responsible for freeing the memory occupied
1974 * by the returned string. If there is insufficient memory available, or the
1975 * argument is NULL, the function returns NULL.
1976 */
1977#define IS_WINDOWS_PATH(p) \
1978 ((p != NULL) && \
1979 (((p[0] >= 'a') && (p[0] <= 'z')) || \
1980 ((p[0] >= 'A') && (p[0] <= 'Z'))) && \
1981 (p[1] == ':') && ((p[2] == '/') || (p[2] == '\\')))
1982xmlChar*
1983xmlCanonicPath(const xmlChar *path)
1984{
Daniel Veillardc64b8e92003-02-24 11:47:13 +00001985#if defined(_WIN32) && !defined(__CYGWIN__)
Igor Zlatkovicce076162003-02-23 13:39:39 +00001986 int len = 0;
1987 int i = 0;
Igor Zlatkovicce076162003-02-23 13:39:39 +00001988 xmlChar *p = NULL;
Daniel Veillardc64b8e92003-02-24 11:47:13 +00001989#endif
1990 xmlChar *ret;
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00001991 xmlURIPtr uri;
1992
1993 if (path == NULL)
1994 return(NULL);
Daniel Veillardc64b8e92003-02-24 11:47:13 +00001995 if ((uri = xmlParseURI((const char *) path)) != NULL) {
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00001996 xmlFreeURI(uri);
1997 return xmlStrdup(path);
1998 }
1999
2000 uri = xmlCreateURI();
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002001 if (uri == NULL) {
2002 return(NULL);
2003 }
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002004
Igor Zlatkovicce076162003-02-23 13:39:39 +00002005#if defined(_WIN32) && !defined(__CYGWIN__)
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002006 len = xmlStrlen(path);
2007 if ((len > 2) && IS_WINDOWS_PATH(path)) {
2008 uri->scheme = xmlStrdup(BAD_CAST "file");
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002009 uri->path = xmlMallocAtomic(len + 2);
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002010 uri->path[0] = '/';
Igor Zlatkovicce076162003-02-23 13:39:39 +00002011 p = uri->path + 1;
2012 strncpy(p, path, len + 1);
2013 } else {
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002014 uri->path = xmlStrdup(path);
Igor Zlatkovicce076162003-02-23 13:39:39 +00002015 p = uri->path;
2016 }
2017 while (*p != '\0') {
2018 if (*p == '\\')
2019 *p = '/';
2020 p++;
2021 }
2022#else
Daniel Veillard42f12e92003-03-07 18:32:59 +00002023 uri->path = (char *) xmlStrdup((const xmlChar *) path);
Igor Zlatkovicce076162003-02-23 13:39:39 +00002024#endif
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002025
2026 ret = xmlSaveUri(uri);
2027 xmlFreeURI(uri);
2028 return(ret);
2029}
Owen Taylor3473f882001-02-23 17:55:21 +00002030