blob: 31e51a3d1b5b4c05aa5a61b0286cc66a036c92ad [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
Daniel Veillardbe3eb202004-07-09 12:05:25 +000049#ifdef IS_DIGIT
50#undef IS_DIGIT
51#endif
Owen Taylor3473f882001-02-23 17:55:21 +000052/*
53 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
54 */
Owen Taylor3473f882001-02-23 17:55:21 +000055#define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
56
57/*
58 * alphanum = alpha | digit
59 */
60
61#define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
62
63/*
64 * hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
65 * "a" | "b" | "c" | "d" | "e" | "f"
66 */
67
68#define IS_HEX(x) ((IS_DIGIT(x)) || (((x) >= 'a') && ((x) <= 'f')) || \
69 (((x) >= 'A') && ((x) <= 'F')))
70
71/*
72 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
73 */
74
75#define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
76 ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
77 ((x) == '(') || ((x) == ')'))
78
79
80/*
81 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
82 */
83
84#define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
85 ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
86 ((x) == '+') || ((x) == '$') || ((x) == ','))
87
88/*
89 * unreserved = alphanum | mark
90 */
91
92#define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
93
94/*
95 * escaped = "%" hex hex
96 */
97
98#define IS_ESCAPED(p) ((*(p) == '%') && (IS_HEX((p)[1])) && \
99 (IS_HEX((p)[2])))
100
101/*
102 * uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
103 * "&" | "=" | "+" | "$" | ","
104 */
105#define IS_URIC_NO_SLASH(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) ||\
106 ((*(p) == ';')) || ((*(p) == '?')) || ((*(p) == ':')) ||\
107 ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) ||\
108 ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ',')))
109
110/*
111 * pchar = unreserved | escaped | ":" | "@" | "&" | "=" | "+" | "$" | ","
112 */
113#define IS_PCHAR(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
114 ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) ||\
115 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||\
116 ((*(p) == ',')))
117
118/*
119 * rel_segment = 1*( unreserved | escaped |
120 * ";" | "@" | "&" | "=" | "+" | "$" | "," )
121 */
122
123#define IS_SEGMENT(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
124 ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || \
125 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \
126 ((*(p) == ',')))
127
128/*
129 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
130 */
131
132#define IS_SCHEME(x) ((IS_ALPHA(x)) || (IS_DIGIT(x)) || \
133 ((x) == '+') || ((x) == '-') || ((x) == '.'))
134
135/*
136 * reg_name = 1*( unreserved | escaped | "$" | "," |
137 * ";" | ":" | "@" | "&" | "=" | "+" )
138 */
139
140#define IS_REG_NAME(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
141 ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || \
142 ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) || \
143 ((*(p) == '=')) || ((*(p) == '+')))
144
145/*
146 * userinfo = *( unreserved | escaped | ";" | ":" | "&" | "=" |
147 * "+" | "$" | "," )
148 */
149#define IS_USERINFO(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
150 ((*(p) == ';')) || ((*(p) == ':')) || ((*(p) == '&')) || \
151 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \
152 ((*(p) == ',')))
153
154/*
155 * uric = reserved | unreserved | escaped
156 */
157
158#define IS_URIC(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
159 (IS_RESERVED(*(p))))
160
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000161/*
162* unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
163*/
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000164
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000165#define IS_UNWISE(p) \
166 (((*(p) == '{')) || ((*(p) == '}')) || ((*(p) == '|')) || \
167 ((*(p) == '\\')) || ((*(p) == '^')) || ((*(p) == '[')) || \
168 ((*(p) == ']')) || ((*(p) == '`')))
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000169
170/*
Owen Taylor3473f882001-02-23 17:55:21 +0000171 * Skip to next pointer char, handle escaped sequences
172 */
173
174#define NEXT(p) ((*p == '%')? p += 3 : p++)
175
176/*
177 * Productions from the spec.
178 *
179 * authority = server | reg_name
180 * reg_name = 1*( unreserved | escaped | "$" | "," |
181 * ";" | ":" | "@" | "&" | "=" | "+" )
182 *
183 * path = [ abs_path | opaque_part ]
184 */
185
186/************************************************************************
187 * *
188 * Generic URI structure functions *
189 * *
190 ************************************************************************/
191
192/**
193 * xmlCreateURI:
194 *
195 * Simply creates an empty xmlURI
196 *
197 * Returns the new structure or NULL in case of error
198 */
199xmlURIPtr
200xmlCreateURI(void) {
201 xmlURIPtr ret;
202
203 ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI));
204 if (ret == NULL) {
205 xmlGenericError(xmlGenericErrorContext,
206 "xmlCreateURI: out of memory\n");
207 return(NULL);
208 }
209 memset(ret, 0, sizeof(xmlURI));
210 return(ret);
211}
212
213/**
214 * xmlSaveUri:
215 * @uri: pointer to an xmlURI
216 *
217 * Save the URI as an escaped string
218 *
219 * Returns a new string (to be deallocated by caller)
220 */
221xmlChar *
222xmlSaveUri(xmlURIPtr uri) {
223 xmlChar *ret = NULL;
224 const char *p;
225 int len;
226 int max;
227
228 if (uri == NULL) return(NULL);
229
230
231 max = 80;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000232 ret = (xmlChar *) xmlMallocAtomic((max + 1) * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +0000233 if (ret == NULL) {
234 xmlGenericError(xmlGenericErrorContext,
235 "xmlSaveUri: out of memory\n");
236 return(NULL);
237 }
238 len = 0;
239
240 if (uri->scheme != NULL) {
241 p = uri->scheme;
242 while (*p != 0) {
243 if (len >= max) {
244 max *= 2;
245 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
246 if (ret == NULL) {
247 xmlGenericError(xmlGenericErrorContext,
248 "xmlSaveUri: out of memory\n");
249 return(NULL);
250 }
251 }
252 ret[len++] = *p++;
253 }
254 if (len >= max) {
255 max *= 2;
256 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
257 if (ret == NULL) {
258 xmlGenericError(xmlGenericErrorContext,
259 "xmlSaveUri: out of memory\n");
260 return(NULL);
261 }
262 }
263 ret[len++] = ':';
264 }
265 if (uri->opaque != NULL) {
266 p = uri->opaque;
267 while (*p != 0) {
268 if (len + 3 >= max) {
269 max *= 2;
270 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
271 if (ret == NULL) {
272 xmlGenericError(xmlGenericErrorContext,
273 "xmlSaveUri: out of memory\n");
274 return(NULL);
275 }
276 }
Daniel Veillard9231ff92003-03-23 22:00:51 +0000277 if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p)))
Owen Taylor3473f882001-02-23 17:55:21 +0000278 ret[len++] = *p++;
279 else {
280 int val = *(unsigned char *)p++;
281 int hi = val / 0x10, lo = val % 0x10;
282 ret[len++] = '%';
283 ret[len++] = hi + (hi > 9? 'A'-10 : '0');
284 ret[len++] = lo + (lo > 9? 'A'-10 : '0');
285 }
286 }
Owen Taylor3473f882001-02-23 17:55:21 +0000287 } 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 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000364 len += snprintf((char *) &ret[len], max - len, ":%d", uri->port);
Owen Taylor3473f882001-02-23 17:55:21 +0000365 }
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 }
Daniel Veillardfdd27d22002-11-28 11:55:38 +0000480 }
481 if (uri->fragment != NULL) {
482 if (len + 3 >= max) {
483 max *= 2;
484 ret = (xmlChar *) xmlRealloc(ret,
485 (max + 1) * sizeof(xmlChar));
486 if (ret == NULL) {
487 xmlGenericError(xmlGenericErrorContext,
488 "xmlSaveUri: out of memory\n");
489 return(NULL);
490 }
491 }
492 ret[len++] = '#';
493 p = uri->fragment;
494 while (*p != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000495 if (len + 3 >= max) {
496 max *= 2;
497 ret = (xmlChar *) xmlRealloc(ret,
498 (max + 1) * sizeof(xmlChar));
499 if (ret == NULL) {
500 xmlGenericError(xmlGenericErrorContext,
501 "xmlSaveUri: out of memory\n");
502 return(NULL);
503 }
504 }
Daniel Veillardfdd27d22002-11-28 11:55:38 +0000505 if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
506 ret[len++] = *p++;
507 else {
508 int val = *(unsigned char *)p++;
509 int hi = val / 0x10, lo = val % 0x10;
510 ret[len++] = '%';
511 ret[len++] = hi + (hi > 9? 'A'-10 : '0');
512 ret[len++] = lo + (lo > 9? 'A'-10 : '0');
Owen Taylor3473f882001-02-23 17:55:21 +0000513 }
514 }
Owen Taylor3473f882001-02-23 17:55:21 +0000515 }
Daniel Veillardfdd27d22002-11-28 11:55:38 +0000516 if (len >= max) {
517 max *= 2;
518 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
519 if (ret == NULL) {
520 xmlGenericError(xmlGenericErrorContext,
521 "xmlSaveUri: out of memory\n");
522 return(NULL);
523 }
524 }
525 ret[len++] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000526 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) {
Daniel Veillardea7751d2002-12-20 00:16:24 +0000542 fprintf(stream, "%s", (char *) out);
Owen Taylor3473f882001-02-23 17:55:21 +0000543 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
Owen Taylor3473f882001-02-23 17:55:21 +0000602/**
603 * xmlNormalizeURIPath:
604 * @path: pointer to the path string
605 *
606 * Applies the 5 normalization steps to a path string--that is, RFC 2396
607 * Section 5.2, steps 6.c through 6.g.
608 *
609 * Normalization occurs directly on the string, no new allocation is done
610 *
611 * Returns 0 or an error code
612 */
613int
614xmlNormalizeURIPath(char *path) {
615 char *cur, *out;
616
617 if (path == NULL)
618 return(-1);
619
620 /* Skip all initial "/" chars. We want to get to the beginning of the
621 * first non-empty segment.
622 */
623 cur = path;
624 while (cur[0] == '/')
625 ++cur;
626 if (cur[0] == '\0')
627 return(0);
628
629 /* Keep everything we've seen so far. */
630 out = cur;
631
632 /*
633 * Analyze each segment in sequence for cases (c) and (d).
634 */
635 while (cur[0] != '\0') {
636 /*
637 * c) All occurrences of "./", where "." is a complete path segment,
638 * are removed from the buffer string.
639 */
640 if ((cur[0] == '.') && (cur[1] == '/')) {
641 cur += 2;
Daniel Veillardfcbd74a2001-06-26 07:47:23 +0000642 /* '//' normalization should be done at this point too */
643 while (cur[0] == '/')
644 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000645 continue;
646 }
647
648 /*
649 * d) If the buffer string ends with "." as a complete path segment,
650 * that "." is removed.
651 */
652 if ((cur[0] == '.') && (cur[1] == '\0'))
653 break;
654
655 /* Otherwise keep the segment. */
656 while (cur[0] != '/') {
657 if (cur[0] == '\0')
658 goto done_cd;
659 (out++)[0] = (cur++)[0];
660 }
Daniel Veillardfcbd74a2001-06-26 07:47:23 +0000661 /* nomalize // */
662 while ((cur[0] == '/') && (cur[1] == '/'))
663 cur++;
664
Owen Taylor3473f882001-02-23 17:55:21 +0000665 (out++)[0] = (cur++)[0];
666 }
667 done_cd:
668 out[0] = '\0';
669
670 /* Reset to the beginning of the first segment for the next sequence. */
671 cur = path;
672 while (cur[0] == '/')
673 ++cur;
674 if (cur[0] == '\0')
675 return(0);
676
677 /*
678 * Analyze each segment in sequence for cases (e) and (f).
679 *
680 * e) All occurrences of "<segment>/../", where <segment> is a
681 * complete path segment not equal to "..", are removed from the
682 * buffer string. Removal of these path segments is performed
683 * iteratively, removing the leftmost matching pattern on each
684 * iteration, until no matching pattern remains.
685 *
686 * f) If the buffer string ends with "<segment>/..", where <segment>
687 * is a complete path segment not equal to "..", that
688 * "<segment>/.." is removed.
689 *
690 * To satisfy the "iterative" clause in (e), we need to collapse the
691 * string every time we find something that needs to be removed. Thus,
692 * we don't need to keep two pointers into the string: we only need a
693 * "current position" pointer.
694 */
695 while (1) {
Daniel Veillard608d0ac2003-08-14 22:44:25 +0000696 char *segp, *tmp;
Owen Taylor3473f882001-02-23 17:55:21 +0000697
698 /* At the beginning of each iteration of this loop, "cur" points to
699 * the first character of the segment we want to examine.
700 */
701
702 /* Find the end of the current segment. */
703 segp = cur;
704 while ((segp[0] != '/') && (segp[0] != '\0'))
705 ++segp;
706
707 /* If this is the last segment, we're done (we need at least two
708 * segments to meet the criteria for the (e) and (f) cases).
709 */
710 if (segp[0] == '\0')
711 break;
712
713 /* If the first segment is "..", or if the next segment _isn't_ "..",
714 * keep this segment and try the next one.
715 */
716 ++segp;
717 if (((cur[0] == '.') && (cur[1] == '.') && (segp == cur+3))
718 || ((segp[0] != '.') || (segp[1] != '.')
719 || ((segp[2] != '/') && (segp[2] != '\0')))) {
720 cur = segp;
721 continue;
722 }
723
724 /* If we get here, remove this segment and the next one and back up
725 * to the previous segment (if there is one), to implement the
726 * "iteratively" clause. It's pretty much impossible to back up
727 * while maintaining two pointers into the buffer, so just compact
728 * the whole buffer now.
729 */
730
731 /* If this is the end of the buffer, we're done. */
732 if (segp[2] == '\0') {
733 cur[0] = '\0';
734 break;
735 }
Daniel Veillard608d0ac2003-08-14 22:44:25 +0000736 /* Valgrind complained, strcpy(cur, segp + 3); */
737 /* string will overlap, do not use strcpy */
738 tmp = cur;
739 segp += 3;
740 while ((*tmp++ = *segp++) != 0);
Owen Taylor3473f882001-02-23 17:55:21 +0000741
742 /* If there are no previous segments, then keep going from here. */
743 segp = cur;
744 while ((segp > path) && ((--segp)[0] == '/'))
745 ;
746 if (segp == path)
747 continue;
748
749 /* "segp" is pointing to the end of a previous segment; find it's
750 * start. We need to back up to the previous segment and start
751 * over with that to handle things like "foo/bar/../..". If we
752 * don't do this, then on the first pass we'll remove the "bar/..",
753 * but be pointing at the second ".." so we won't realize we can also
754 * remove the "foo/..".
755 */
756 cur = segp;
757 while ((cur > path) && (cur[-1] != '/'))
758 --cur;
759 }
760 out[0] = '\0';
761
762 /*
763 * g) If the resulting buffer string still begins with one or more
764 * complete path segments of "..", then the reference is
765 * considered to be in error. Implementations may handle this
766 * error by retaining these components in the resolved path (i.e.,
767 * treating them as part of the final URI), by removing them from
768 * the resolved path (i.e., discarding relative levels above the
769 * root), or by avoiding traversal of the reference.
770 *
771 * We discard them from the final path.
772 */
773 if (path[0] == '/') {
774 cur = path;
Daniel Veillard9231ff92003-03-23 22:00:51 +0000775 while ((cur[0] == '/') && (cur[1] == '.') && (cur[2] == '.')
Owen Taylor3473f882001-02-23 17:55:21 +0000776 && ((cur[3] == '/') || (cur[3] == '\0')))
777 cur += 3;
778
779 if (cur != path) {
780 out = path;
781 while (cur[0] != '\0')
782 (out++)[0] = (cur++)[0];
783 out[0] = 0;
784 }
785 }
786
787 return(0);
788}
Owen Taylor3473f882001-02-23 17:55:21 +0000789
Daniel Veillard966a31e2004-05-09 02:58:44 +0000790static int is_hex(char c) {
791 if (((c >= '0') && (c <= '9')) ||
792 ((c >= 'a') && (c <= 'f')) ||
793 ((c >= 'A') && (c <= 'F')))
794 return(1);
795 return(0);
796}
797
Owen Taylor3473f882001-02-23 17:55:21 +0000798/**
799 * xmlURIUnescapeString:
800 * @str: the string to unescape
Daniel Veillard60087f32001-10-10 09:45:09 +0000801 * @len: the length in bytes to unescape (or <= 0 to indicate full string)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000802 * @target: optional destination buffer
Owen Taylor3473f882001-02-23 17:55:21 +0000803 *
804 * Unescaping routine, does not do validity checks !
805 * Output is direct unsigned char translation of %XX values (no encoding)
806 *
807 * Returns an copy of the string, but unescaped
808 */
809char *
810xmlURIUnescapeString(const char *str, int len, char *target) {
811 char *ret, *out;
812 const char *in;
813
814 if (str == NULL)
815 return(NULL);
816 if (len <= 0) len = strlen(str);
Daniel Veillardd2298792003-02-14 16:54:11 +0000817 if (len < 0) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000818
819 if (target == NULL) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000820 ret = (char *) xmlMallocAtomic(len + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000821 if (ret == NULL) {
822 xmlGenericError(xmlGenericErrorContext,
823 "xmlURIUnescapeString: out of memory\n");
824 return(NULL);
825 }
826 } else
827 ret = target;
828 in = str;
829 out = ret;
830 while(len > 0) {
Daniel Veillard966a31e2004-05-09 02:58:44 +0000831 if ((*in == '%') && (is_hex(in[1])) && (is_hex(in[2]))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000832 in++;
833 if ((*in >= '0') && (*in <= '9'))
834 *out = (*in - '0');
835 else if ((*in >= 'a') && (*in <= 'f'))
836 *out = (*in - 'a') + 10;
837 else if ((*in >= 'A') && (*in <= 'F'))
838 *out = (*in - 'A') + 10;
839 in++;
840 if ((*in >= '0') && (*in <= '9'))
841 *out = *out * 16 + (*in - '0');
842 else if ((*in >= 'a') && (*in <= 'f'))
843 *out = *out * 16 + (*in - 'a') + 10;
844 else if ((*in >= 'A') && (*in <= 'F'))
845 *out = *out * 16 + (*in - 'A') + 10;
846 in++;
847 len -= 3;
848 out++;
849 } else {
850 *out++ = *in++;
851 len--;
852 }
853 }
854 *out = 0;
855 return(ret);
856}
857
858/**
Daniel Veillard8514c672001-05-23 10:29:12 +0000859 * xmlURIEscapeStr:
860 * @str: string to escape
861 * @list: exception list string of chars not to escape
Owen Taylor3473f882001-02-23 17:55:21 +0000862 *
Daniel Veillard8514c672001-05-23 10:29:12 +0000863 * This routine escapes a string to hex, ignoring reserved characters (a-z)
864 * and the characters in the exception list.
Owen Taylor3473f882001-02-23 17:55:21 +0000865 *
Daniel Veillard8514c672001-05-23 10:29:12 +0000866 * Returns a new escaped string or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +0000867 */
868xmlChar *
Daniel Veillard8514c672001-05-23 10:29:12 +0000869xmlURIEscapeStr(const xmlChar *str, const xmlChar *list) {
870 xmlChar *ret, ch;
Owen Taylor3473f882001-02-23 17:55:21 +0000871 const xmlChar *in;
Daniel Veillard8514c672001-05-23 10:29:12 +0000872
Owen Taylor3473f882001-02-23 17:55:21 +0000873 unsigned int len, out;
874
875 if (str == NULL)
876 return(NULL);
877 len = xmlStrlen(str);
Daniel Veillarde645e8c2002-10-22 17:35:37 +0000878 if (!(len > 0)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000879
880 len += 20;
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000881 ret = (xmlChar *) xmlMallocAtomic(len);
Owen Taylor3473f882001-02-23 17:55:21 +0000882 if (ret == NULL) {
883 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000884 "xmlURIEscapeStr: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000885 return(NULL);
886 }
887 in = (const xmlChar *) str;
888 out = 0;
889 while(*in != 0) {
890 if (len - out <= 3) {
891 len += 20;
892 ret = (xmlChar *) xmlRealloc(ret, len);
893 if (ret == NULL) {
894 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000895 "xmlURIEscapeStr: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000896 return(NULL);
897 }
898 }
Daniel Veillard8514c672001-05-23 10:29:12 +0000899
900 ch = *in;
901
Daniel Veillardeb475a32002-04-14 22:00:22 +0000902 if ((ch != '@') && (!IS_UNRESERVED(ch)) && (!xmlStrchr(list, ch))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000903 unsigned char val;
904 ret[out++] = '%';
Daniel Veillard8514c672001-05-23 10:29:12 +0000905 val = ch >> 4;
Owen Taylor3473f882001-02-23 17:55:21 +0000906 if (val <= 9)
907 ret[out++] = '0' + val;
908 else
909 ret[out++] = 'A' + val - 0xA;
Daniel Veillard8514c672001-05-23 10:29:12 +0000910 val = ch & 0xF;
Owen Taylor3473f882001-02-23 17:55:21 +0000911 if (val <= 9)
912 ret[out++] = '0' + val;
913 else
914 ret[out++] = 'A' + val - 0xA;
915 in++;
916 } else {
917 ret[out++] = *in++;
918 }
Daniel Veillard8514c672001-05-23 10:29:12 +0000919
Owen Taylor3473f882001-02-23 17:55:21 +0000920 }
921 ret[out] = 0;
922 return(ret);
923}
924
Daniel Veillard8514c672001-05-23 10:29:12 +0000925/**
926 * xmlURIEscape:
927 * @str: the string of the URI to escape
928 *
929 * Escaping routine, does not do validity checks !
930 * It will try to escape the chars needing this, but this is heuristic
931 * based it's impossible to be sure.
932 *
Daniel Veillard8514c672001-05-23 10:29:12 +0000933 * Returns an copy of the string, but escaped
Daniel Veillard6278fb52001-05-25 07:38:41 +0000934 *
935 * 25 May 2001
936 * Uses xmlParseURI and xmlURIEscapeStr to try to escape correctly
937 * according to RFC2396.
938 * - Carl Douglas
Daniel Veillard8514c672001-05-23 10:29:12 +0000939 */
940xmlChar *
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000941xmlURIEscape(const xmlChar * str)
942{
Daniel Veillard6278fb52001-05-25 07:38:41 +0000943 xmlChar *ret, *segment = NULL;
944 xmlURIPtr uri;
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000945 int ret2;
Daniel Veillard8514c672001-05-23 10:29:12 +0000946
Daniel Veillard6278fb52001-05-25 07:38:41 +0000947#define NULLCHK(p) if(!p) { \
948 xmlGenericError(xmlGenericErrorContext, \
949 "xmlURIEscape: out of memory\n"); \
950 return NULL; }
951
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000952 if (str == NULL)
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000953 return (NULL);
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000954
955 uri = xmlCreateURI();
956 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000957 /*
958 * Allow escaping errors in the unescaped form
959 */
960 uri->cleanup = 1;
961 ret2 = xmlParseURIReference(uri, (const char *)str);
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000962 if (ret2) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000963 xmlFreeURI(uri);
964 return (NULL);
965 }
Daniel Veillardbb6808e2001-10-29 23:59:27 +0000966 }
Daniel Veillard6278fb52001-05-25 07:38:41 +0000967
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000968 if (!uri)
969 return NULL;
Daniel Veillard6278fb52001-05-25 07:38:41 +0000970
971 ret = NULL;
972
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000973 if (uri->scheme) {
974 segment = xmlURIEscapeStr(BAD_CAST uri->scheme, BAD_CAST "+-.");
975 NULLCHK(segment)
976 ret = xmlStrcat(ret, segment);
977 ret = xmlStrcat(ret, BAD_CAST ":");
978 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +0000979 }
980
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000981 if (uri->authority) {
982 segment =
983 xmlURIEscapeStr(BAD_CAST uri->authority, BAD_CAST "/?;:@");
984 NULLCHK(segment)
985 ret = xmlStrcat(ret, BAD_CAST "//");
986 ret = xmlStrcat(ret, segment);
987 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +0000988 }
989
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000990 if (uri->user) {
991 segment = xmlURIEscapeStr(BAD_CAST uri->user, BAD_CAST ";:&=+$,");
992 NULLCHK(segment)
Daniel Veillard0a194582004-04-01 20:09:22 +0000993 ret = xmlStrcat(ret,BAD_CAST "//");
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000994 ret = xmlStrcat(ret, segment);
995 ret = xmlStrcat(ret, BAD_CAST "@");
996 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +0000997 }
998
Daniel Veillard4def3bd2001-10-30 09:47:47 +0000999 if (uri->server) {
1000 segment = xmlURIEscapeStr(BAD_CAST uri->server, BAD_CAST "/?;:@");
1001 NULLCHK(segment)
Daniel Veillard0a194582004-04-01 20:09:22 +00001002 if (uri->user == NULL)
1003 ret = xmlStrcat(ret, BAD_CAST "//");
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001004 ret = xmlStrcat(ret, segment);
1005 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001006 }
1007
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001008 if (uri->port) {
1009 xmlChar port[10];
1010
Daniel Veillard43d3f612001-11-10 11:57:23 +00001011 snprintf((char *) port, 10, "%d", uri->port);
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001012 ret = xmlStrcat(ret, BAD_CAST ":");
1013 ret = xmlStrcat(ret, port);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001014 }
1015
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001016 if (uri->path) {
1017 segment =
1018 xmlURIEscapeStr(BAD_CAST uri->path, BAD_CAST ":@&=+$,/?;");
1019 NULLCHK(segment)
1020 ret = xmlStrcat(ret, segment);
1021 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001022 }
1023
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001024 if (uri->query) {
1025 segment =
1026 xmlURIEscapeStr(BAD_CAST uri->query, BAD_CAST ";/?:@&=+,$");
1027 NULLCHK(segment)
1028 ret = xmlStrcat(ret, BAD_CAST "?");
1029 ret = xmlStrcat(ret, segment);
1030 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001031 }
1032
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001033 if (uri->opaque) {
1034 segment = xmlURIEscapeStr(BAD_CAST uri->opaque, BAD_CAST "");
1035 NULLCHK(segment)
1036 ret = xmlStrcat(ret, segment);
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001037 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001038 }
1039
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001040 if (uri->fragment) {
1041 segment = xmlURIEscapeStr(BAD_CAST uri->fragment, BAD_CAST "#");
1042 NULLCHK(segment)
1043 ret = xmlStrcat(ret, BAD_CAST "#");
1044 ret = xmlStrcat(ret, segment);
1045 xmlFree(segment);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001046 }
Daniel Veillard43d3f612001-11-10 11:57:23 +00001047
1048 xmlFreeURI(uri);
Daniel Veillard6278fb52001-05-25 07:38:41 +00001049#undef NULLCHK
Daniel Veillard8514c672001-05-23 10:29:12 +00001050
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001051 return (ret);
Daniel Veillard8514c672001-05-23 10:29:12 +00001052}
1053
Owen Taylor3473f882001-02-23 17:55:21 +00001054/************************************************************************
1055 * *
1056 * Escaped URI parsing *
1057 * *
1058 ************************************************************************/
1059
1060/**
1061 * xmlParseURIFragment:
1062 * @uri: pointer to an URI structure
1063 * @str: pointer to the string to analyze
1064 *
1065 * Parse an URI fragment string and fills in the appropriate fields
1066 * of the @uri structure.
1067 *
1068 * fragment = *uric
1069 *
1070 * Returns 0 or the error code
1071 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001072static int
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001073xmlParseURIFragment(xmlURIPtr uri, const char **str)
1074{
Owen Taylor3473f882001-02-23 17:55:21 +00001075 const char *cur = *str;
1076
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001077 if (str == NULL)
1078 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001079
Daniel Veillardfdd27d22002-11-28 11:55:38 +00001080 while (IS_URIC(cur) || IS_UNWISE(cur))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001081 NEXT(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001082 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001083 if (uri->fragment != NULL)
1084 xmlFree(uri->fragment);
1085 uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001086 }
1087 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001088 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001089}
1090
1091/**
1092 * xmlParseURIQuery:
1093 * @uri: pointer to an URI structure
1094 * @str: pointer to the string to analyze
1095 *
1096 * Parse the query part of an URI
1097 *
1098 * query = *uric
1099 *
1100 * Returns 0 or the error code
1101 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001102static int
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001103xmlParseURIQuery(xmlURIPtr uri, const char **str)
1104{
Owen Taylor3473f882001-02-23 17:55:21 +00001105 const char *cur = *str;
1106
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001107 if (str == NULL)
1108 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001109
Daniel Veillard9231ff92003-03-23 22:00:51 +00001110 while (IS_URIC(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001111 NEXT(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001112 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001113 if (uri->query != NULL)
1114 xmlFree(uri->query);
1115 uri->query = xmlURIUnescapeString(*str, cur - *str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001116 }
1117 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001118 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001119}
1120
1121/**
1122 * xmlParseURIScheme:
1123 * @uri: pointer to an URI structure
1124 * @str: pointer to the string to analyze
1125 *
1126 * Parse an URI scheme
1127 *
1128 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
1129 *
1130 * Returns 0 or the error code
1131 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001132static int
Owen Taylor3473f882001-02-23 17:55:21 +00001133xmlParseURIScheme(xmlURIPtr uri, const char **str) {
1134 const char *cur;
1135
1136 if (str == NULL)
1137 return(-1);
1138
1139 cur = *str;
1140 if (!IS_ALPHA(*cur))
1141 return(2);
1142 cur++;
1143 while (IS_SCHEME(*cur)) cur++;
1144 if (uri != NULL) {
1145 if (uri->scheme != NULL) xmlFree(uri->scheme);
1146 /* !!! strndup */
1147 uri->scheme = xmlURIUnescapeString(*str, cur - *str, NULL);
1148 }
1149 *str = cur;
1150 return(0);
1151}
1152
1153/**
1154 * xmlParseURIOpaquePart:
1155 * @uri: pointer to an URI structure
1156 * @str: pointer to the string to analyze
1157 *
1158 * Parse an URI opaque part
1159 *
1160 * opaque_part = uric_no_slash *uric
1161 *
1162 * Returns 0 or the error code
1163 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001164static int
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001165xmlParseURIOpaquePart(xmlURIPtr uri, const char **str)
1166{
Owen Taylor3473f882001-02-23 17:55:21 +00001167 const char *cur;
1168
1169 if (str == NULL)
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001170 return (-1);
1171
Owen Taylor3473f882001-02-23 17:55:21 +00001172 cur = *str;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001173 if (!(IS_URIC_NO_SLASH(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001174 return (3);
Owen Taylor3473f882001-02-23 17:55:21 +00001175 }
1176 NEXT(cur);
Daniel Veillard9231ff92003-03-23 22:00:51 +00001177 while (IS_URIC(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001178 NEXT(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001179 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001180 if (uri->opaque != NULL)
1181 xmlFree(uri->opaque);
1182 uri->opaque = xmlURIUnescapeString(*str, cur - *str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001183 }
1184 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001185 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001186}
1187
1188/**
1189 * xmlParseURIServer:
1190 * @uri: pointer to an URI structure
1191 * @str: pointer to the string to analyze
1192 *
1193 * Parse a server subpart of an URI, it's a finer grain analysis
1194 * of the authority part.
1195 *
1196 * server = [ [ userinfo "@" ] hostport ]
1197 * userinfo = *( unreserved | escaped |
1198 * ";" | ":" | "&" | "=" | "+" | "$" | "," )
1199 * hostport = host [ ":" port ]
1200 * host = hostname | IPv4address
1201 * hostname = *( domainlabel "." ) toplabel [ "." ]
1202 * domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
1203 * toplabel = alpha | alpha *( alphanum | "-" ) alphanum
1204 * IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
1205 * port = *digit
1206 *
1207 * Returns 0 or the error code
1208 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001209static int
Owen Taylor3473f882001-02-23 17:55:21 +00001210xmlParseURIServer(xmlURIPtr uri, const char **str) {
1211 const char *cur;
1212 const char *host, *tmp;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001213 const int IPmax = 4;
1214 int oct;
Owen Taylor3473f882001-02-23 17:55:21 +00001215
1216 if (str == NULL)
1217 return(-1);
1218
1219 cur = *str;
1220
1221 /*
1222 * is there an userinfo ?
1223 */
1224 while (IS_USERINFO(cur)) NEXT(cur);
1225 if (*cur == '@') {
1226 if (uri != NULL) {
1227 if (uri->user != NULL) xmlFree(uri->user);
1228 uri->user = xmlURIUnescapeString(*str, cur - *str, NULL);
1229 }
1230 cur++;
1231 } else {
1232 if (uri != NULL) {
1233 if (uri->user != NULL) xmlFree(uri->user);
1234 uri->user = NULL;
1235 }
1236 cur = *str;
1237 }
1238 /*
1239 * This can be empty in the case where there is no server
1240 */
1241 host = cur;
1242 if (*cur == '/') {
1243 if (uri != NULL) {
1244 if (uri->authority != NULL) xmlFree(uri->authority);
1245 uri->authority = NULL;
1246 if (uri->server != NULL) xmlFree(uri->server);
1247 uri->server = NULL;
1248 uri->port = 0;
1249 }
1250 return(0);
1251 }
1252 /*
1253 * host part of hostport can derive either an IPV4 address
1254 * or an unresolved name. Check the IP first, it easier to detect
1255 * errors if wrong one
1256 */
Daniel Veillard9231ff92003-03-23 22:00:51 +00001257 for (oct = 0; oct < IPmax; ++oct) {
1258 if (*cur == '.')
1259 return(3); /* e.g. http://.xml/ or http://18.29..30/ */
Owen Taylor3473f882001-02-23 17:55:21 +00001260 while(IS_DIGIT(*cur)) cur++;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001261 if (oct == (IPmax-1))
1262 continue;
1263 if (*cur != '.')
1264 break;
1265 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00001266 }
Daniel Veillard9231ff92003-03-23 22:00:51 +00001267 if (oct < IPmax || (*cur == '.' && cur++) || IS_ALPHA(*cur)) {
1268 /* maybe host_name */
1269 if (!IS_ALPHANUM(*cur))
1270 return(4); /* e.g. http://xml.$oft */
1271 do {
1272 do ++cur; while (IS_ALPHANUM(*cur));
1273 if (*cur == '-') {
1274 --cur;
1275 if (*cur == '.')
1276 return(5); /* e.g. http://xml.-soft */
1277 ++cur;
1278 continue;
1279 }
1280 if (*cur == '.') {
1281 --cur;
1282 if (*cur == '-')
1283 return(6); /* e.g. http://xml-.soft */
1284 if (*cur == '.')
1285 return(7); /* e.g. http://xml..soft */
1286 ++cur;
1287 continue;
1288 }
1289 break;
1290 } while (1);
1291 tmp = cur;
1292 if (tmp[-1] == '.')
1293 --tmp; /* e.g. http://xml.$Oft/ */
1294 do --tmp; while (tmp >= host && IS_ALPHANUM(*tmp));
1295 if ((++tmp == host || tmp[-1] == '.') && !IS_ALPHA(*tmp))
1296 return(8); /* e.g. http://xmlsOft.0rg/ */
Owen Taylor3473f882001-02-23 17:55:21 +00001297 }
Owen Taylor3473f882001-02-23 17:55:21 +00001298 if (uri != NULL) {
1299 if (uri->authority != NULL) xmlFree(uri->authority);
1300 uri->authority = NULL;
1301 if (uri->server != NULL) xmlFree(uri->server);
1302 uri->server = xmlURIUnescapeString(host, cur - host, NULL);
1303 }
Owen Taylor3473f882001-02-23 17:55:21 +00001304 /*
1305 * finish by checking for a port presence.
1306 */
1307 if (*cur == ':') {
1308 cur++;
1309 if (IS_DIGIT(*cur)) {
1310 if (uri != NULL)
1311 uri->port = 0;
1312 while (IS_DIGIT(*cur)) {
1313 if (uri != NULL)
1314 uri->port = uri->port * 10 + (*cur - '0');
1315 cur++;
1316 }
1317 }
1318 }
1319 *str = cur;
1320 return(0);
1321}
1322
1323/**
1324 * xmlParseURIRelSegment:
1325 * @uri: pointer to an URI structure
1326 * @str: pointer to the string to analyze
1327 *
1328 * Parse an URI relative segment
1329 *
1330 * rel_segment = 1*( unreserved | escaped | ";" | "@" | "&" | "=" |
1331 * "+" | "$" | "," )
1332 *
1333 * Returns 0 or the error code
1334 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001335static int
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001336xmlParseURIRelSegment(xmlURIPtr uri, const char **str)
1337{
Owen Taylor3473f882001-02-23 17:55:21 +00001338 const char *cur;
1339
1340 if (str == NULL)
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001341 return (-1);
1342
Owen Taylor3473f882001-02-23 17:55:21 +00001343 cur = *str;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001344 if (!(IS_SEGMENT(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001345 return (3);
Owen Taylor3473f882001-02-23 17:55:21 +00001346 }
1347 NEXT(cur);
Daniel Veillard9231ff92003-03-23 22:00:51 +00001348 while (IS_SEGMENT(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001349 NEXT(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001350 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001351 if (uri->path != NULL)
1352 xmlFree(uri->path);
1353 uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001354 }
1355 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001356 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001357}
1358
1359/**
1360 * xmlParseURIPathSegments:
1361 * @uri: pointer to an URI structure
1362 * @str: pointer to the string to analyze
1363 * @slash: should we add a leading slash
1364 *
1365 * Parse an URI set of path segments
1366 *
1367 * path_segments = segment *( "/" segment )
1368 * segment = *pchar *( ";" param )
1369 * param = *pchar
1370 *
1371 * Returns 0 or the error code
1372 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001373static int
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001374xmlParseURIPathSegments(xmlURIPtr uri, const char **str, int slash)
1375{
Owen Taylor3473f882001-02-23 17:55:21 +00001376 const char *cur;
1377
1378 if (str == NULL)
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001379 return (-1);
1380
Owen Taylor3473f882001-02-23 17:55:21 +00001381 cur = *str;
1382
1383 do {
Daniel Veillard9231ff92003-03-23 22:00:51 +00001384 while (IS_PCHAR(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001385 NEXT(cur);
Daniel Veillard234bc4e2002-05-24 11:03:05 +00001386 while (*cur == ';') {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001387 cur++;
Daniel Veillard9231ff92003-03-23 22:00:51 +00001388 while (IS_PCHAR(cur) || ((uri != NULL) && (uri->cleanup) && (IS_UNWISE(cur))))
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001389 NEXT(cur);
1390 }
1391 if (*cur != '/')
1392 break;
1393 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00001394 } while (1);
1395 if (uri != NULL) {
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001396 int len, len2 = 0;
1397 char *path;
Owen Taylor3473f882001-02-23 17:55:21 +00001398
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001399 /*
1400 * Concat the set of path segments to the current path
1401 */
1402 len = cur - *str;
1403 if (slash)
1404 len++;
Owen Taylor3473f882001-02-23 17:55:21 +00001405
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001406 if (uri->path != NULL) {
1407 len2 = strlen(uri->path);
1408 len += len2;
1409 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001410 path = (char *) xmlMallocAtomic(len + 1);
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001411 if (path == NULL) {
1412 xmlGenericError(xmlGenericErrorContext,
1413 "xmlParseURIPathSegments: out of memory\n");
1414 *str = cur;
1415 return (-1);
1416 }
1417 if (uri->path != NULL)
1418 memcpy(path, uri->path, len2);
1419 if (slash) {
1420 path[len2] = '/';
1421 len2++;
1422 }
1423 path[len2] = 0;
1424 if (cur - *str > 0)
1425 xmlURIUnescapeString(*str, cur - *str, &path[len2]);
1426 if (uri->path != NULL)
1427 xmlFree(uri->path);
1428 uri->path = path;
Owen Taylor3473f882001-02-23 17:55:21 +00001429 }
1430 *str = cur;
Daniel Veillard4def3bd2001-10-30 09:47:47 +00001431 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001432}
1433
1434/**
1435 * xmlParseURIAuthority:
1436 * @uri: pointer to an URI structure
1437 * @str: pointer to the string to analyze
1438 *
1439 * Parse the authority part of an URI.
1440 *
1441 * authority = server | reg_name
1442 * server = [ [ userinfo "@" ] hostport ]
1443 * reg_name = 1*( unreserved | escaped | "$" | "," | ";" | ":" |
1444 * "@" | "&" | "=" | "+" )
1445 *
1446 * Note : this is completely ambiguous since reg_name is allowed to
1447 * use the full set of chars in use by server:
1448 *
1449 * 3.2.1. Registry-based Naming Authority
1450 *
1451 * The structure of a registry-based naming authority is specific
1452 * to the URI scheme, but constrained to the allowed characters
1453 * for an authority component.
1454 *
1455 * Returns 0 or the error code
1456 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001457static int
Owen Taylor3473f882001-02-23 17:55:21 +00001458xmlParseURIAuthority(xmlURIPtr uri, const char **str) {
1459 const char *cur;
1460 int ret;
1461
1462 if (str == NULL)
1463 return(-1);
1464
1465 cur = *str;
1466
1467 /*
1468 * try first to parse it as a server string.
1469 */
1470 ret = xmlParseURIServer(uri, str);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001471 if ((ret == 0) && (*str != NULL) &&
1472 ((**str == 0) || (**str == '/') || (**str == '?')))
Owen Taylor3473f882001-02-23 17:55:21 +00001473 return(0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001474 *str = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001475
1476 /*
1477 * failed, fallback to reg_name
1478 */
1479 if (!IS_REG_NAME(cur)) {
1480 return(5);
1481 }
1482 NEXT(cur);
1483 while (IS_REG_NAME(cur)) NEXT(cur);
1484 if (uri != NULL) {
1485 if (uri->server != NULL) xmlFree(uri->server);
1486 uri->server = NULL;
1487 if (uri->user != NULL) xmlFree(uri->user);
1488 uri->user = NULL;
1489 if (uri->authority != NULL) xmlFree(uri->authority);
1490 uri->authority = xmlURIUnescapeString(*str, cur - *str, NULL);
1491 }
1492 *str = cur;
1493 return(0);
1494}
1495
1496/**
1497 * xmlParseURIHierPart:
1498 * @uri: pointer to an URI structure
1499 * @str: pointer to the string to analyze
1500 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001501 * Parse an URI hierarchical part
Owen Taylor3473f882001-02-23 17:55:21 +00001502 *
1503 * hier_part = ( net_path | abs_path ) [ "?" query ]
1504 * abs_path = "/" path_segments
1505 * net_path = "//" authority [ abs_path ]
1506 *
1507 * Returns 0 or the error code
1508 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001509static int
Owen Taylor3473f882001-02-23 17:55:21 +00001510xmlParseURIHierPart(xmlURIPtr uri, const char **str) {
1511 int ret;
1512 const char *cur;
1513
1514 if (str == NULL)
1515 return(-1);
1516
1517 cur = *str;
1518
1519 if ((cur[0] == '/') && (cur[1] == '/')) {
1520 cur += 2;
1521 ret = xmlParseURIAuthority(uri, &cur);
1522 if (ret != 0)
1523 return(ret);
1524 if (cur[0] == '/') {
1525 cur++;
1526 ret = xmlParseURIPathSegments(uri, &cur, 1);
1527 }
1528 } else if (cur[0] == '/') {
1529 cur++;
1530 ret = xmlParseURIPathSegments(uri, &cur, 1);
1531 } else {
1532 return(4);
1533 }
1534 if (ret != 0)
1535 return(ret);
1536 if (*cur == '?') {
1537 cur++;
1538 ret = xmlParseURIQuery(uri, &cur);
1539 if (ret != 0)
1540 return(ret);
1541 }
1542 *str = cur;
1543 return(0);
1544}
1545
1546/**
1547 * xmlParseAbsoluteURI:
1548 * @uri: pointer to an URI structure
1549 * @str: pointer to the string to analyze
1550 *
1551 * Parse an URI reference string and fills in the appropriate fields
1552 * of the @uri structure
1553 *
1554 * absoluteURI = scheme ":" ( hier_part | opaque_part )
1555 *
1556 * Returns 0 or the error code
1557 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001558static int
Owen Taylor3473f882001-02-23 17:55:21 +00001559xmlParseAbsoluteURI(xmlURIPtr uri, const char **str) {
1560 int ret;
Daniel Veillard20ee8c02001-10-05 09:18:14 +00001561 const char *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001562
1563 if (str == NULL)
1564 return(-1);
1565
Daniel Veillard20ee8c02001-10-05 09:18:14 +00001566 cur = *str;
1567
Owen Taylor3473f882001-02-23 17:55:21 +00001568 ret = xmlParseURIScheme(uri, str);
1569 if (ret != 0) return(ret);
Daniel Veillard20ee8c02001-10-05 09:18:14 +00001570 if (**str != ':') {
1571 *str = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001572 return(1);
Daniel Veillard20ee8c02001-10-05 09:18:14 +00001573 }
Owen Taylor3473f882001-02-23 17:55:21 +00001574 (*str)++;
1575 if (**str == '/')
1576 return(xmlParseURIHierPart(uri, str));
1577 return(xmlParseURIOpaquePart(uri, str));
1578}
1579
1580/**
1581 * xmlParseRelativeURI:
1582 * @uri: pointer to an URI structure
1583 * @str: pointer to the string to analyze
1584 *
1585 * Parse an relative URI string and fills in the appropriate fields
1586 * of the @uri structure
1587 *
1588 * relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
1589 * abs_path = "/" path_segments
1590 * net_path = "//" authority [ abs_path ]
1591 * rel_path = rel_segment [ abs_path ]
1592 *
1593 * Returns 0 or the error code
1594 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001595static int
Owen Taylor3473f882001-02-23 17:55:21 +00001596xmlParseRelativeURI(xmlURIPtr uri, const char **str) {
1597 int ret = 0;
1598 const char *cur;
1599
1600 if (str == NULL)
1601 return(-1);
1602
1603 cur = *str;
1604 if ((cur[0] == '/') && (cur[1] == '/')) {
1605 cur += 2;
1606 ret = xmlParseURIAuthority(uri, &cur);
1607 if (ret != 0)
1608 return(ret);
1609 if (cur[0] == '/') {
1610 cur++;
1611 ret = xmlParseURIPathSegments(uri, &cur, 1);
1612 }
1613 } else if (cur[0] == '/') {
1614 cur++;
1615 ret = xmlParseURIPathSegments(uri, &cur, 1);
1616 } else if (cur[0] != '#' && cur[0] != '?') {
1617 ret = xmlParseURIRelSegment(uri, &cur);
1618 if (ret != 0)
1619 return(ret);
1620 if (cur[0] == '/') {
1621 cur++;
1622 ret = xmlParseURIPathSegments(uri, &cur, 1);
1623 }
1624 }
1625 if (ret != 0)
1626 return(ret);
1627 if (*cur == '?') {
1628 cur++;
1629 ret = xmlParseURIQuery(uri, &cur);
1630 if (ret != 0)
1631 return(ret);
1632 }
1633 *str = cur;
1634 return(ret);
1635}
1636
1637/**
1638 * xmlParseURIReference:
1639 * @uri: pointer to an URI structure
1640 * @str: the string to analyze
1641 *
1642 * Parse an URI reference string and fills in the appropriate fields
1643 * of the @uri structure
1644 *
1645 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
1646 *
1647 * Returns 0 or the error code
1648 */
1649int
1650xmlParseURIReference(xmlURIPtr uri, const char *str) {
1651 int ret;
1652 const char *tmp = str;
1653
1654 if (str == NULL)
1655 return(-1);
1656 xmlCleanURI(uri);
1657
1658 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001659 * Try first to parse absolute refs, then fallback to relative if
Owen Taylor3473f882001-02-23 17:55:21 +00001660 * it fails.
1661 */
1662 ret = xmlParseAbsoluteURI(uri, &str);
1663 if (ret != 0) {
1664 xmlCleanURI(uri);
1665 str = tmp;
1666 ret = xmlParseRelativeURI(uri, &str);
1667 }
1668 if (ret != 0) {
1669 xmlCleanURI(uri);
1670 return(ret);
1671 }
1672
1673 if (*str == '#') {
1674 str++;
1675 ret = xmlParseURIFragment(uri, &str);
1676 if (ret != 0) return(ret);
1677 }
1678 if (*str != 0) {
1679 xmlCleanURI(uri);
1680 return(1);
1681 }
1682 return(0);
1683}
1684
1685/**
1686 * xmlParseURI:
1687 * @str: the URI string to analyze
1688 *
1689 * Parse an URI
1690 *
1691 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
1692 *
1693 * Returns a newly build xmlURIPtr or NULL in case of error
1694 */
1695xmlURIPtr
1696xmlParseURI(const char *str) {
1697 xmlURIPtr uri;
1698 int ret;
1699
1700 if (str == NULL)
1701 return(NULL);
1702 uri = xmlCreateURI();
1703 if (uri != NULL) {
1704 ret = xmlParseURIReference(uri, str);
1705 if (ret) {
1706 xmlFreeURI(uri);
1707 return(NULL);
1708 }
1709 }
1710 return(uri);
1711}
1712
1713/************************************************************************
1714 * *
1715 * Public functions *
1716 * *
1717 ************************************************************************/
1718
1719/**
1720 * xmlBuildURI:
1721 * @URI: the URI instance found in the document
1722 * @base: the base value
1723 *
1724 * Computes he final URI of the reference done by checking that
1725 * the given URI is valid, and building the final URI using the
1726 * base URI. This is processed according to section 5.2 of the
1727 * RFC 2396
1728 *
1729 * 5.2. Resolving Relative References to Absolute Form
1730 *
1731 * Returns a new URI string (to be freed by the caller) or NULL in case
1732 * of error.
1733 */
1734xmlChar *
1735xmlBuildURI(const xmlChar *URI, const xmlChar *base) {
1736 xmlChar *val = NULL;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001737 int ret, len, indx, cur, out;
Owen Taylor3473f882001-02-23 17:55:21 +00001738 xmlURIPtr ref = NULL;
1739 xmlURIPtr bas = NULL;
1740 xmlURIPtr res = NULL;
1741
1742 /*
1743 * 1) The URI reference is parsed into the potential four components and
1744 * fragment identifier, as described in Section 4.3.
1745 *
1746 * NOTE that a completely empty URI is treated by modern browsers
1747 * as a reference to "." rather than as a synonym for the current
1748 * URI. Should we do that here?
1749 */
1750 if (URI == NULL)
1751 ret = -1;
1752 else {
1753 if (*URI) {
1754 ref = xmlCreateURI();
1755 if (ref == NULL)
1756 goto done;
1757 ret = xmlParseURIReference(ref, (const char *) URI);
1758 }
1759 else
1760 ret = 0;
1761 }
1762 if (ret != 0)
1763 goto done;
Daniel Veillard7b4b2f92003-01-06 13:11:20 +00001764 if ((ref != NULL) && (ref->scheme != NULL)) {
1765 /*
1766 * The URI is absolute don't modify.
1767 */
1768 val = xmlStrdup(URI);
1769 goto done;
1770 }
Owen Taylor3473f882001-02-23 17:55:21 +00001771 if (base == NULL)
1772 ret = -1;
1773 else {
1774 bas = xmlCreateURI();
1775 if (bas == NULL)
1776 goto done;
1777 ret = xmlParseURIReference(bas, (const char *) base);
1778 }
1779 if (ret != 0) {
1780 if (ref)
1781 val = xmlSaveUri(ref);
1782 goto done;
1783 }
1784 if (ref == NULL) {
1785 /*
1786 * the base fragment must be ignored
1787 */
1788 if (bas->fragment != NULL) {
1789 xmlFree(bas->fragment);
1790 bas->fragment = NULL;
1791 }
1792 val = xmlSaveUri(bas);
1793 goto done;
1794 }
1795
1796 /*
1797 * 2) If the path component is empty and the scheme, authority, and
1798 * query components are undefined, then it is a reference to the
1799 * current document and we are done. Otherwise, the reference URI's
1800 * query and fragment components are defined as found (or not found)
1801 * within the URI reference and not inherited from the base URI.
1802 *
1803 * NOTE that in modern browsers, the parsing differs from the above
1804 * in the following aspect: the query component is allowed to be
1805 * defined while still treating this as a reference to the current
1806 * document.
1807 */
1808 res = xmlCreateURI();
1809 if (res == NULL)
1810 goto done;
1811 if ((ref->scheme == NULL) && (ref->path == NULL) &&
1812 ((ref->authority == NULL) && (ref->server == NULL))) {
1813 if (bas->scheme != NULL)
1814 res->scheme = xmlMemStrdup(bas->scheme);
1815 if (bas->authority != NULL)
1816 res->authority = xmlMemStrdup(bas->authority);
1817 else if (bas->server != NULL) {
1818 res->server = xmlMemStrdup(bas->server);
1819 if (bas->user != NULL)
1820 res->user = xmlMemStrdup(bas->user);
1821 res->port = bas->port;
1822 }
1823 if (bas->path != NULL)
1824 res->path = xmlMemStrdup(bas->path);
1825 if (ref->query != NULL)
1826 res->query = xmlMemStrdup(ref->query);
1827 else if (bas->query != NULL)
1828 res->query = xmlMemStrdup(bas->query);
1829 if (ref->fragment != NULL)
1830 res->fragment = xmlMemStrdup(ref->fragment);
1831 goto step_7;
1832 }
Owen Taylor3473f882001-02-23 17:55:21 +00001833
1834 /*
1835 * 3) If the scheme component is defined, indicating that the reference
1836 * starts with a scheme name, then the reference is interpreted as an
1837 * absolute URI and we are done. Otherwise, the reference URI's
1838 * scheme is inherited from the base URI's scheme component.
1839 */
1840 if (ref->scheme != NULL) {
1841 val = xmlSaveUri(ref);
1842 goto done;
1843 }
1844 if (bas->scheme != NULL)
1845 res->scheme = xmlMemStrdup(bas->scheme);
Daniel Veillard9231ff92003-03-23 22:00:51 +00001846
1847 if (ref->query != NULL)
1848 res->query = xmlMemStrdup(ref->query);
1849 if (ref->fragment != NULL)
1850 res->fragment = xmlMemStrdup(ref->fragment);
Owen Taylor3473f882001-02-23 17:55:21 +00001851
1852 /*
1853 * 4) If the authority component is defined, then the reference is a
1854 * network-path and we skip to step 7. Otherwise, the reference
1855 * URI's authority is inherited from the base URI's authority
1856 * component, which will also be undefined if the URI scheme does not
1857 * use an authority component.
1858 */
1859 if ((ref->authority != NULL) || (ref->server != NULL)) {
1860 if (ref->authority != NULL)
1861 res->authority = xmlMemStrdup(ref->authority);
1862 else {
1863 res->server = xmlMemStrdup(ref->server);
1864 if (ref->user != NULL)
1865 res->user = xmlMemStrdup(ref->user);
1866 res->port = ref->port;
1867 }
1868 if (ref->path != NULL)
1869 res->path = xmlMemStrdup(ref->path);
1870 goto step_7;
1871 }
1872 if (bas->authority != NULL)
1873 res->authority = xmlMemStrdup(bas->authority);
1874 else if (bas->server != NULL) {
1875 res->server = xmlMemStrdup(bas->server);
1876 if (bas->user != NULL)
1877 res->user = xmlMemStrdup(bas->user);
1878 res->port = bas->port;
1879 }
1880
1881 /*
1882 * 5) If the path component begins with a slash character ("/"), then
1883 * the reference is an absolute-path and we skip to step 7.
1884 */
1885 if ((ref->path != NULL) && (ref->path[0] == '/')) {
1886 res->path = xmlMemStrdup(ref->path);
1887 goto step_7;
1888 }
1889
1890
1891 /*
1892 * 6) If this step is reached, then we are resolving a relative-path
1893 * reference. The relative path needs to be merged with the base
1894 * URI's path. Although there are many ways to do this, we will
1895 * describe a simple method using a separate string buffer.
1896 *
1897 * Allocate a buffer large enough for the result string.
1898 */
1899 len = 2; /* extra / and 0 */
1900 if (ref->path != NULL)
1901 len += strlen(ref->path);
1902 if (bas->path != NULL)
1903 len += strlen(bas->path);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001904 res->path = (char *) xmlMallocAtomic(len);
Owen Taylor3473f882001-02-23 17:55:21 +00001905 if (res->path == NULL) {
1906 xmlGenericError(xmlGenericErrorContext,
1907 "xmlBuildURI: out of memory\n");
1908 goto done;
1909 }
1910 res->path[0] = 0;
1911
1912 /*
1913 * a) All but the last segment of the base URI's path component is
1914 * copied to the buffer. In other words, any characters after the
1915 * last (right-most) slash character, if any, are excluded.
1916 */
1917 cur = 0;
1918 out = 0;
1919 if (bas->path != NULL) {
1920 while (bas->path[cur] != 0) {
1921 while ((bas->path[cur] != 0) && (bas->path[cur] != '/'))
1922 cur++;
1923 if (bas->path[cur] == 0)
1924 break;
1925
1926 cur++;
1927 while (out < cur) {
1928 res->path[out] = bas->path[out];
1929 out++;
1930 }
1931 }
1932 }
1933 res->path[out] = 0;
1934
1935 /*
1936 * b) The reference's path component is appended to the buffer
1937 * string.
1938 */
1939 if (ref->path != NULL && ref->path[0] != 0) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001940 indx = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001941 /*
1942 * Ensure the path includes a '/'
1943 */
1944 if ((out == 0) && (bas->server != NULL))
1945 res->path[out++] = '/';
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001946 while (ref->path[indx] != 0) {
1947 res->path[out++] = ref->path[indx++];
Owen Taylor3473f882001-02-23 17:55:21 +00001948 }
1949 }
1950 res->path[out] = 0;
1951
1952 /*
1953 * Steps c) to h) are really path normalization steps
1954 */
1955 xmlNormalizeURIPath(res->path);
1956
1957step_7:
1958
1959 /*
1960 * 7) The resulting URI components, including any inherited from the
1961 * base URI, are recombined to give the absolute form of the URI
1962 * reference.
1963 */
1964 val = xmlSaveUri(res);
1965
1966done:
1967 if (ref != NULL)
1968 xmlFreeURI(ref);
1969 if (bas != NULL)
1970 xmlFreeURI(bas);
1971 if (res != NULL)
1972 xmlFreeURI(res);
1973 return(val);
1974}
1975
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00001976/**
William M. Brackf7789b12004-06-07 08:57:27 +00001977 * xmlBuildRelativeURI:
1978 * @URI: the URI reference under consideration
1979 * @base: the base value
1980 *
1981 * Expresses the URI of the reference in terms relative to the
1982 * base. Some examples of this operation include:
1983 * base = "http://site1.com/docs/book1.html"
1984 * URI input URI returned
1985 * docs/pic1.gif pic1.gif
1986 * docs/img/pic1.gif img/pic1.gif
1987 * img/pic1.gif ../img/pic1.gif
1988 * http://site1.com/docs/pic1.gif pic1.gif
1989 * http://site2.com/docs/pic1.gif http://site2.com/docs/pic1.gif
1990 *
1991 * base = "docs/book1.html"
1992 * URI input URI returned
1993 * docs/pic1.gif pic1.gif
1994 * docs/img/pic1.gif img/pic1.gif
1995 * img/pic1.gif ../img/pic1.gif
1996 * http://site1.com/docs/pic1.gif http://site1.com/docs/pic1.gif
1997 *
1998 *
1999 * Note: if the URI reference is really wierd or complicated, it may be
2000 * worthwhile to first convert it into a "nice" one by calling
2001 * xmlBuildURI (using 'base') before calling this routine,
2002 * since this routine (for reasonable efficiency) assumes URI has
2003 * already been through some validation.
2004 *
2005 * Returns a new URI string (to be freed by the caller) or NULL in case
2006 * error.
2007 */
2008xmlChar *
2009xmlBuildRelativeURI (const xmlChar * URI, const xmlChar * base)
2010{
2011 xmlChar *val = NULL;
2012 int ret;
2013 int ix;
2014 int pos = 0;
2015 int nbslash = 0;
2016 xmlURIPtr ref = NULL;
2017 xmlURIPtr bas = NULL;
2018 xmlChar *bptr, *uptr, *vptr;
2019
2020 if ((URI == NULL) || (*URI == 0))
2021 return NULL;
2022 /*
2023 * Special case - if URI starts with '.', we assume it's already
2024 * in relative form, so nothing to do.
2025 */
2026 if (*URI == '.') {
2027 val = xmlStrdup (URI);
2028 goto done;
2029 }
2030
2031 /*
2032 * First parse URI into a standard form
2033 */
2034 ref = xmlCreateURI ();
2035 if (ref == NULL)
2036 return NULL;
2037 ret = xmlParseURIReference (ref, (const char *) URI);
2038 if (ret != 0)
2039 goto done; /* Error in URI, return NULL */
2040
2041 /*
2042 * Next parse base into the same standard form
2043 */
2044 if ((base == NULL) || (*base == 0)) {
2045 val = xmlStrdup (URI);
2046 goto done;
2047 }
2048 bas = xmlCreateURI ();
2049 if (bas == NULL)
2050 goto done;
2051 ret = xmlParseURIReference (bas, (const char *) base);
2052 if (ret != 0)
2053 goto done; /* Error in base, return NULL */
2054
2055 /*
2056 * If the scheme / server on the URI differs from the base,
2057 * just return the URI
2058 */
2059 if ((ref->scheme != NULL) &&
2060 ((bas->scheme == NULL) ||
2061 xmlStrcmp ((xmlChar *)bas->scheme, (xmlChar *)ref->scheme) ||
2062 xmlStrcmp ((xmlChar *)bas->server, (xmlChar *)ref->server))) {
2063 val = xmlStrdup (URI);
2064 goto done;
2065 }
2066
2067 /*
2068 * At this point (at last!) we can compare the two paths
2069 *
2070 * First we compare the two strings and find where they first differ
2071 */
2072 bptr = (xmlChar *)bas->path;
William M. Brackf20fbf72004-06-25 05:49:08 +00002073 if ((ref->path[pos] == '.') && (ref->path[pos+1] == '/'))
2074 pos += 2;
2075 if ((*bptr == '.') && (bptr[1] == '/'))
2076 bptr += 2;
2077 else if ((*bptr == '/') && (ref->path[pos] != '/'))
William M. Brackf7789b12004-06-07 08:57:27 +00002078 bptr++;
2079 while ((bptr[pos] == ref->path[pos]) && (bptr[pos] != 0))
2080 pos++;
2081
2082 if (bptr[pos] == ref->path[pos]) {
2083 val = NULL; /* if no differences, return NULL */
2084 goto done; /* (I can't imagine why anyone would do this) */
2085 }
2086
2087 /*
2088 * In URI, "back up" to the last '/' encountered. This will be the
2089 * beginning of the "unique" suffix of URI
2090 */
2091 ix = pos;
2092 if ((ref->path[ix] == '/') && (ix > 0))
2093 ix--;
2094 for (; ix > 0; ix--) {
2095 if (ref->path[ix] == '/')
2096 break;
2097 }
2098 if (ix == 0)
2099 uptr = (xmlChar *)ref->path;
2100 else
2101 uptr = (xmlChar *)&ref->path[ix + 1];
2102
2103 /*
2104 * In base, count the number of '/' from the differing point
2105 */
2106 if (bptr[pos] != ref->path[pos]) { /* check for trivial URI == base */
2107 for (; bptr[ix] != 0; ix++) {
2108 if (bptr[ix] == '/')
2109 nbslash++;
2110 }
2111 }
2112
2113 if (nbslash == 0) {
2114 val = xmlStrdup (uptr);
2115 goto done;
2116 }
2117 nbslash--;
2118
2119 /*
2120 * Allocate just enough space for the returned string -
2121 * length of the remainder of the URI, plus enough space
2122 * for the "../" groups, plus one for the terminator
2123 */
2124 ix = xmlStrlen (uptr) + 1;
2125 val = (xmlChar *) xmlMalloc (ix + 3 * nbslash);
2126 if (val == NULL) {
2127 goto done;
2128 }
2129 vptr = val;
2130 /*
2131 * Put in as many "../" as needed
2132 */
2133 for (; nbslash>0; nbslash--) {
2134 *vptr++ = '.';
2135 *vptr++ = '.';
2136 *vptr++ = '/';
2137 }
2138 /*
2139 * Finish up with the end of the URI
2140 */
2141 memcpy (vptr, uptr, ix);
2142
2143 done:
2144 /*
2145 * Free the working variables
2146 */
2147 if (ref != NULL)
2148 xmlFreeURI (ref);
2149 if (bas != NULL)
2150 xmlFreeURI (bas);
2151
2152 return val;
2153}
2154
2155/**
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002156 * xmlCanonicPath:
2157 * @path: the resource locator in a filesystem notation
2158 *
2159 * Constructs a canonic path from the specified path.
2160 *
2161 * Returns a new canonic path, or a duplicate of the path parameter if the
2162 * construction fails. The caller is responsible for freeing the memory occupied
2163 * by the returned string. If there is insufficient memory available, or the
2164 * argument is NULL, the function returns NULL.
2165 */
2166#define IS_WINDOWS_PATH(p) \
2167 ((p != NULL) && \
2168 (((p[0] >= 'a') && (p[0] <= 'z')) || \
2169 ((p[0] >= 'A') && (p[0] <= 'Z'))) && \
2170 (p[1] == ':') && ((p[2] == '/') || (p[2] == '\\')))
2171xmlChar*
2172xmlCanonicPath(const xmlChar *path)
2173{
Daniel Veillardc64b8e92003-02-24 11:47:13 +00002174#if defined(_WIN32) && !defined(__CYGWIN__)
Igor Zlatkovicce076162003-02-23 13:39:39 +00002175 int len = 0;
2176 int i = 0;
Igor Zlatkovicce076162003-02-23 13:39:39 +00002177 xmlChar *p = NULL;
Daniel Veillardc64b8e92003-02-24 11:47:13 +00002178#endif
2179 xmlChar *ret;
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002180 xmlURIPtr uri;
2181
2182 if (path == NULL)
2183 return(NULL);
Daniel Veillardc64b8e92003-02-24 11:47:13 +00002184 if ((uri = xmlParseURI((const char *) path)) != NULL) {
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002185 xmlFreeURI(uri);
2186 return xmlStrdup(path);
2187 }
2188
2189 uri = xmlCreateURI();
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002190 if (uri == NULL) {
2191 return(NULL);
2192 }
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002193
Igor Zlatkovicce076162003-02-23 13:39:39 +00002194#if defined(_WIN32) && !defined(__CYGWIN__)
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002195 len = xmlStrlen(path);
2196 if ((len > 2) && IS_WINDOWS_PATH(path)) {
2197 uri->scheme = xmlStrdup(BAD_CAST "file");
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002198 uri->path = xmlMallocAtomic(len + 2);
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002199 uri->path[0] = '/';
Igor Zlatkovicce076162003-02-23 13:39:39 +00002200 p = uri->path + 1;
2201 strncpy(p, path, len + 1);
2202 } else {
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002203 uri->path = xmlStrdup(path);
Igor Zlatkovicce076162003-02-23 13:39:39 +00002204 p = uri->path;
2205 }
2206 while (*p != '\0') {
2207 if (*p == '\\')
2208 *p = '/';
2209 p++;
2210 }
2211#else
Daniel Veillard42f12e92003-03-07 18:32:59 +00002212 uri->path = (char *) xmlStrdup((const xmlChar *) path);
Igor Zlatkovicce076162003-02-23 13:39:39 +00002213#endif
Igor Zlatkovicf2238e62003-02-19 14:50:35 +00002214
2215 ret = xmlSaveUri(uri);
2216 xmlFreeURI(uri);
2217 return(ret);
2218}
Owen Taylor3473f882001-02-23 17:55:21 +00002219