blob: 6bac167d09f5fbeaf5905fa16758c798c14582a0 [file] [log] [blame]
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001/**
2 * uri.c: set of generic URI related routines
3 *
4 * Reference: RFC 2396
5 *
6 * See Copyright for the status of this software.
7 *
8 * Daniel.Veillard@w3.org
9 */
10
11#ifdef WIN32
12#define INCLUDE_WINSOCK
13#include "win32config.h"
14#else
15#include "config.h"
16#endif
17
18#include <stdio.h>
19#include <string.h>
20
Daniel Veillard361d8452000-04-03 19:48:13 +000021#include <libxml/xmlmemory.h>
22#include <libxml/uri.h>
Daniel Veillard3dd82e72000-03-20 11:48:04 +000023
24/**
25 * alpha = lowalpha | upalpha
26 */
27#define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
28
29
30/**
31 * lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" |
32 * "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" |
33 * "u" | "v" | "w" | "x" | "y" | "z"
34 */
35
36#define IS_LOWALPHA(x) (((x) >= 'a') && ((x) <= 'z'))
37
38/**
39 * upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" |
40 * "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" |
41 * "U" | "V" | "W" | "X" | "Y" | "Z"
42 */
43#define IS_UPALPHA(x) (((x) >= 'A') && ((x) <= 'Z'))
44
45/**
46 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
47 */
48
49#define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
50
51/**
52 * alphanum = alpha | digit
53 */
54
55#define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
56
57/**
58 * he(x) = digit | "A" | "B" | "C" | "D" | "E" | "F" |
59 * "a" | "b" | "c" | "d" | "e" | "f"
60 */
61
62#define IS_HEX(x) ((IS_DIGIT(x)) || (((x) >= 'a') && ((x) <= 'f')) || \
63 (((x) >= 'A') && ((x) <= 'F')))
64
65/**
66 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
67 */
68
69#define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
70 ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
71 ((x) == '(') || ((x) == ')'))
72
73
74/**
75 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
76 */
77
78#define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
79 ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
80 ((x) == '+') || ((x) == '$') || ((x) == ','))
81
82/**
83 * unreserved = alphanum | mark
84 */
85
86#define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
87
88/**
89 * escaped = "%" hex hex
90 */
91
92#define IS_ESCAPED(p) ((*(p) == '%') && (IS_HEX((p)[1])) && \
93 (IS_HEX((p)[2])))
94
95/**
96 * uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
97 * "&" | "=" | "+" | "$" | ","
98 */
99#define IS_URIC_NO_SLASH(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) ||\
100 ((*(p) == ';')) || ((*(p) == '?')) || ((*(p) == ':')) ||\
101 ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) ||\
102 ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ',')))
103
104/**
105 * pchar = unreserved | escaped | ":" | "@" | "&" | "=" | "+" | "$" | ","
106 */
107#define IS_PCHAR(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
108 ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) ||\
109 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||\
110 ((*(p) == ',')))
111
112/**
113 * rel_segment = 1*( unreserved | escaped |
114 * ";" | "@" | "&" | "=" | "+" | "$" | "," )
115 */
116
117#define IS_SEGMENT(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
118 ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || \
119 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \
120 ((*(p) == ',')))
121
122/**
123 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
124 */
125
126#define IS_SCHEME(x) ((IS_ALPHA(x)) || (IS_DIGIT(x)) || \
127 ((x) == '+') || ((x) == '-') || ((x) == '.'))
128
129/**
130 * reg_name = 1*( unreserved | escaped | "$" | "," |
131 * ";" | ":" | "@" | "&" | "=" | "+" )
132 */
133
134#define IS_REG_NAME(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
135 ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || \
136 ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) || \
137 ((*(p) == '=')) || ((*(p) == '+')))
138
139/**
140 * userinfo = *( unreserved | escaped | ";" | ":" | "&" | "=" |
141 * "+" | "$" | "," )
142 */
143#define IS_USERINFO(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
144 ((*(p) == ';')) || ((*(p) == ':')) || ((*(p) == '&')) || \
145 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) || \
146 ((*(p) == ',')))
147
148/**
149 * uric = reserved | unreserved | escaped
150 */
151
152#define IS_URIC(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
153 (IS_RESERVED(*(p))))
154
155/**
156 * Skip to next pointer char, handle escaped sequences
157 */
158
159#define NEXT(p) ((*p == '%')? p += 3 : p++)
160
161/**
Daniel Veillard361d8452000-04-03 19:48:13 +0000162 * Productions from the spec.
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000163 *
Daniel Veillard361d8452000-04-03 19:48:13 +0000164 * authority = server | reg_name
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000165 * reg_name = 1*( unreserved | escaped | "$" | "," |
166 * ";" | ":" | "@" | "&" | "=" | "+" )
Daniel Veillard361d8452000-04-03 19:48:13 +0000167 *
168 * path = [ abs_path | opaque_part ]
169 */
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000170
171/**
172 * xmlCreateURI:
173 *
174 * Simply creates an empty xmlURI
175 *
176 * Returns the new structure or NULL in case of error
177 */
178xmlURIPtr
179xmlCreateURI(void) {
180 xmlURIPtr ret;
181
182 ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI));
183 if (ret == NULL) {
184 fprintf(stderr, "xmlCreateURI: out of memory\n");
185 return(NULL);
186 }
187 memset(ret, 0, sizeof(xmlURI));
188 return(ret);
189}
190
191/**
Daniel Veillardec303412000-03-24 13:41:54 +0000192 * xmlSaveUri:
193 * @uri: pointer to an xmlURI
194 *
195 * Save the URI as an escaped string
196 *
197 * Returns a new string (to be deallocated by caller)
198 */
199xmlChar *
200xmlSaveUri(xmlURIPtr uri) {
201 xmlChar *ret = NULL;
202 const char *p;
203 int len;
204 int max;
205
206 if (uri == NULL) return(NULL);
207
208
209 max = 80;
210 ret = xmlMalloc((max + 1) * sizeof(xmlChar));
211 if (ret == NULL) {
212 fprintf(stderr, "xmlSaveUri: out of memory\n");
213 return(NULL);
214 }
215 len = 0;
216
217 if (uri->scheme != NULL) {
218 p = uri->scheme;
219 while (*p != 0) {
220 if (len >= max) {
221 max *= 2;
222 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
223 if (ret == NULL) {
224 fprintf(stderr, "xmlSaveUri: out of memory\n");
225 return(NULL);
226 }
227 }
228 ret[len++] = *p++;
229 }
230 if (len >= max) {
231 max *= 2;
232 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
233 if (ret == NULL) {
234 fprintf(stderr, "xmlSaveUri: out of memory\n");
235 return(NULL);
236 }
237 }
238 ret[len++] = ':';
239 }
240 if (uri->opaque != NULL) {
241 p = uri->opaque;
242 while (*p != 0) {
243 if (len + 3 >= max) {
244 max *= 2;
245 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
246 if (ret == NULL) {
247 fprintf(stderr, "xmlSaveUri: out of memory\n");
248 return(NULL);
249 }
250 }
251 if ((IS_UNRESERVED(*(p))) ||
252 ((*(p) == ';')) || ((*(p) == '?')) || ((*(p) == ':')) ||
253 ((*(p) == '@')) || ((*(p) == '&')) || ((*(p) == '=')) ||
254 ((*(p) == '+')) || ((*(p) == '$')) || ((*(p) == ',')))
255 ret[len++] = *p++;
256 else {
257 int val = *p++;
258 ret[len++] = '%';
259 switch (val / 0x10) {
260 case 0xF: ret[len++] = 'F'; break;
261 case 0xE: ret[len++] = 'E'; break;
262 case 0xD: ret[len++] = 'D'; break;
263 case 0xC: ret[len++] = 'C'; break;
264 case 0xB: ret[len++] = 'B'; break;
265 case 0xA: ret[len++] = 'A'; break;
266 default: ret[len++] = '0' + (val / 0x10);
267 }
268 switch (val % 0x10) {
269 case 0xF: ret[len++] = 'F'; break;
270 case 0xE: ret[len++] = 'E'; break;
271 case 0xD: ret[len++] = 'D'; break;
272 case 0xC: ret[len++] = 'C'; break;
273 case 0xB: ret[len++] = 'B'; break;
274 case 0xA: ret[len++] = 'A'; break;
275 default: ret[len++] = '0' + (val % 0x10);
276 }
277 }
278 }
279 if (len >= max) {
280 max *= 2;
281 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
282 if (ret == NULL) {
283 fprintf(stderr, "xmlSaveUri: out of memory\n");
284 return(NULL);
285 }
286 }
287 ret[len++] = 0;
288 } else {
Daniel Veillard361d8452000-04-03 19:48:13 +0000289 if (uri->server != NULL) {
290 if (len + 3 >= max) {
291 max *= 2;
292 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
293 if (ret == NULL) {
294 fprintf(stderr, "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 = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
306 if (ret == NULL) {
307 fprintf(stderr, "xmlSaveUri: out of memory\n");
308 return(NULL);
309 }
310 }
311 if ((IS_UNRESERVED(*(p))) ||
312 ((*(p) == ';')) || ((*(p) == ':')) || ((*(p) == '&')) ||
313 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||
314 ((*(p) == ',')))
315 ret[len++] = *p++;
316 else {
317 int val = *p++;
318 ret[len++] = '%';
319 switch (val / 0x10) {
320 case 0xF: ret[len++] = 'F'; break;
321 case 0xE: ret[len++] = 'E'; break;
322 case 0xD: ret[len++] = 'D'; break;
323 case 0xC: ret[len++] = 'C'; break;
324 case 0xB: ret[len++] = 'B'; break;
325 case 0xA: ret[len++] = 'A'; break;
326 default: ret[len++] = '0' + (val / 0x10);
327 }
328 switch (val % 0x10) {
329 case 0xF: ret[len++] = 'F'; break;
330 case 0xE: ret[len++] = 'E'; break;
331 case 0xD: ret[len++] = 'D'; break;
332 case 0xC: ret[len++] = 'C'; break;
333 case 0xB: ret[len++] = 'B'; break;
334 case 0xA: ret[len++] = 'A'; break;
335 default: ret[len++] = '0' + (val % 0x10);
336 }
337 }
338 }
339 if (len + 3 >= max) {
340 max *= 2;
341 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
342 if (ret == NULL) {
343 fprintf(stderr, "xmlSaveUri: out of memory\n");
344 return(NULL);
345 }
346 }
347 ret[len++] = '@';
348 }
349 p = uri->server;
350 while (*p != 0) {
351 if (len >= max) {
352 max *= 2;
353 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
354 if (ret == NULL) {
355 fprintf(stderr, "xmlSaveUri: out of memory\n");
356 return(NULL);
357 }
358 }
359 ret[len++] = *p++;
360 }
361 if (uri->port > 0) {
362 if (len + 10 >= max) {
363 max *= 2;
364 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
365 if (ret == NULL) {
366 fprintf(stderr, "xmlSaveUri: out of memory\n");
367 return(NULL);
368 }
369 }
370 len += sprintf((char *) &ret[len], ":%d", uri->port);
371 }
372 } else if (uri->authority != NULL) {
Daniel Veillardec303412000-03-24 13:41:54 +0000373 if (len + 3 >= max) {
374 max *= 2;
375 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
376 if (ret == NULL) {
377 fprintf(stderr, "xmlSaveUri: out of memory\n");
378 return(NULL);
379 }
380 }
381 ret[len++] = '/';
382 ret[len++] = '/';
383 p = uri->authority;
384 while (*p != 0) {
385 if (len + 3 >= max) {
386 max *= 2;
387 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
388 if (ret == NULL) {
389 fprintf(stderr, "xmlSaveUri: out of memory\n");
390 return(NULL);
391 }
392 }
393 if ((IS_UNRESERVED(*(p))) ||
394 ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) ||
395 ((*(p) == ':')) || ((*(p) == '@')) || ((*(p) == '&')) ||
396 ((*(p) == '=')) || ((*(p) == '+')))
397 ret[len++] = *p++;
398 else {
399 int val = *p++;
400 ret[len++] = '%';
401 switch (val / 0x10) {
402 case 0xF: ret[len++] = 'F'; break;
403 case 0xE: ret[len++] = 'E'; break;
404 case 0xD: ret[len++] = 'D'; break;
405 case 0xC: ret[len++] = 'C'; break;
406 case 0xB: ret[len++] = 'B'; break;
407 case 0xA: ret[len++] = 'A'; break;
408 default: ret[len++] = '0' + (val / 0x10);
409 }
410 switch (val % 0x10) {
411 case 0xF: ret[len++] = 'F'; break;
412 case 0xE: ret[len++] = 'E'; break;
413 case 0xD: ret[len++] = 'D'; break;
414 case 0xC: ret[len++] = 'C'; break;
415 case 0xB: ret[len++] = 'B'; break;
416 case 0xA: ret[len++] = 'A'; break;
417 default: ret[len++] = '0' + (val % 0x10);
418 }
419 }
420 }
421 }
422 if (uri->path != NULL) {
423 p = uri->path;
424 while (*p != 0) {
425 if (len + 3 >= max) {
426 max *= 2;
427 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
428 if (ret == NULL) {
429 fprintf(stderr, "xmlSaveUri: out of memory\n");
430 return(NULL);
431 }
432 }
433 if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) ||
434 ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) ||
435 ((*(p) == '=')) || ((*(p) == '+')) || ((*(p) == '$')) ||
436 ((*(p) == ',')))
437 ret[len++] = *p++;
438 else {
439 int val = *p++;
440 ret[len++] = '%';
441 switch (val / 0x10) {
442 case 0xF: ret[len++] = 'F'; break;
443 case 0xE: ret[len++] = 'E'; break;
444 case 0xD: ret[len++] = 'D'; break;
445 case 0xC: ret[len++] = 'C'; break;
446 case 0xB: ret[len++] = 'B'; break;
447 case 0xA: ret[len++] = 'A'; break;
448 default: ret[len++] = '0' + (val / 0x10);
449 }
450 switch (val % 0x10) {
451 case 0xF: ret[len++] = 'F'; break;
452 case 0xE: ret[len++] = 'E'; break;
453 case 0xD: ret[len++] = 'D'; break;
454 case 0xC: ret[len++] = 'C'; break;
455 case 0xB: ret[len++] = 'B'; break;
456 case 0xA: ret[len++] = 'A'; break;
457 default: ret[len++] = '0' + (val % 0x10);
458 }
459 }
460 }
461 }
462 if (uri->query != NULL) {
463 if (len + 3 >= max) {
464 max *= 2;
465 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
466 if (ret == NULL) {
467 fprintf(stderr, "xmlSaveUri: out of memory\n");
468 return(NULL);
469 }
470 }
471 ret[len++] = '?';
472 p = uri->query;
473 while (*p != 0) {
474 if (len + 3 >= max) {
475 max *= 2;
476 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
477 if (ret == NULL) {
478 fprintf(stderr, "xmlSaveUri: out of memory\n");
479 return(NULL);
480 }
481 }
482 if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
483 ret[len++] = *p++;
484 else {
485 int val = *p++;
486 ret[len++] = '%';
487 switch (val / 0x10) {
488 case 0xF: ret[len++] = 'F'; break;
489 case 0xE: ret[len++] = 'E'; break;
490 case 0xD: ret[len++] = 'D'; break;
491 case 0xC: ret[len++] = 'C'; break;
492 case 0xB: ret[len++] = 'B'; break;
493 case 0xA: ret[len++] = 'A'; break;
494 default: ret[len++] = '0' + (val / 0x10);
495 }
496 switch (val % 0x10) {
497 case 0xF: ret[len++] = 'F'; break;
498 case 0xE: ret[len++] = 'E'; break;
499 case 0xD: ret[len++] = 'D'; break;
500 case 0xC: ret[len++] = 'C'; break;
501 case 0xB: ret[len++] = 'B'; break;
502 case 0xA: ret[len++] = 'A'; break;
503 default: ret[len++] = '0' + (val % 0x10);
504 }
505 }
506 }
507 }
508 if (uri->fragment != NULL) {
509 if (len + 3 >= max) {
510 max *= 2;
511 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
512 if (ret == NULL) {
513 fprintf(stderr, "xmlSaveUri: out of memory\n");
514 return(NULL);
515 }
516 }
517 ret[len++] = '#';
518 p = uri->fragment;
519 while (*p != 0) {
520 if (len + 3 >= max) {
521 max *= 2;
522 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
523 if (ret == NULL) {
524 fprintf(stderr, "xmlSaveUri: out of memory\n");
525 return(NULL);
526 }
527 }
528 if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
529 ret[len++] = *p++;
530 else {
531 int val = *p++;
532 ret[len++] = '%';
533 switch (val / 0x10) {
534 case 0xF: ret[len++] = 'F'; break;
535 case 0xE: ret[len++] = 'E'; break;
536 case 0xD: ret[len++] = 'D'; break;
537 case 0xC: ret[len++] = 'C'; break;
538 case 0xB: ret[len++] = 'B'; break;
539 case 0xA: ret[len++] = 'A'; break;
540 default: ret[len++] = '0' + (val / 0x10);
541 }
542 switch (val % 0x10) {
543 case 0xF: ret[len++] = 'F'; break;
544 case 0xE: ret[len++] = 'E'; break;
545 case 0xD: ret[len++] = 'D'; break;
546 case 0xC: ret[len++] = 'C'; break;
547 case 0xB: ret[len++] = 'B'; break;
548 case 0xA: ret[len++] = 'A'; break;
549 default: ret[len++] = '0' + (val % 0x10);
550 }
551 }
552 }
553 }
554 if (len >= max) {
555 max *= 2;
556 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
557 if (ret == NULL) {
558 fprintf(stderr, "xmlSaveUri: out of memory\n");
559 return(NULL);
560 }
561 }
562 ret[len++] = 0;
563 }
564 return(ret);
565}
566
567/**
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000568 * xmlPrintURI:
569 * @stream: a FILE* for the output
570 * @uri: pointer to an xmlURI
571 *
572 * Prints the URI in the stream @steam.
573 */
574void
575xmlPrintURI(FILE *stream, xmlURIPtr uri) {
Daniel Veillardec303412000-03-24 13:41:54 +0000576 xmlChar *out;
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000577
Daniel Veillardec303412000-03-24 13:41:54 +0000578 out = xmlSaveUri(uri);
579 if (out != NULL) {
580 fprintf(stream, "%s", out);
581 xmlFree(out);
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000582 }
583}
584
585/**
586 * xmlCleanURI:
587 * @uri: pointer to an xmlURI
588 *
589 * Make sure the xmlURI struct is free of content
590 */
591void
592xmlCleanURI(xmlURIPtr uri) {
593 if (uri == NULL) return;
594
595 if (uri->scheme != NULL) xmlFree(uri->scheme);
596 uri->scheme = NULL;
597 if (uri->server != NULL) xmlFree(uri->server);
598 uri->server = NULL;
Daniel Veillard361d8452000-04-03 19:48:13 +0000599 if (uri->user != NULL) xmlFree(uri->user);
600 uri->user = NULL;
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000601 if (uri->path != NULL) xmlFree(uri->path);
602 uri->path = NULL;
603 if (uri->fragment != NULL) xmlFree(uri->fragment);
604 uri->fragment = NULL;
605 if (uri->opaque != NULL) xmlFree(uri->opaque);
606 uri->opaque = NULL;
607 if (uri->authority != NULL) xmlFree(uri->authority);
608 uri->authority = NULL;
609 if (uri->query != NULL) xmlFree(uri->query);
610 uri->query = NULL;
611}
612
613/**
614 * xmlFreeURI:
615 * @uri: pointer to an xmlURI
616 *
617 * Free up the xmlURI struct
618 */
619void
620xmlFreeURI(xmlURIPtr uri) {
621 if (uri == NULL) return;
622
623 if (uri->scheme != NULL) xmlFree(uri->scheme);
624 if (uri->server != NULL) xmlFree(uri->server);
Daniel Veillard361d8452000-04-03 19:48:13 +0000625 if (uri->user != NULL) xmlFree(uri->user);
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000626 if (uri->path != NULL) xmlFree(uri->path);
627 if (uri->fragment != NULL) xmlFree(uri->fragment);
628 if (uri->opaque != NULL) xmlFree(uri->opaque);
629 if (uri->authority != NULL) xmlFree(uri->authority);
630 if (uri->query != NULL) xmlFree(uri->query);
631 memset(uri, -1, sizeof(xmlURI));
632 xmlFree(uri);
633}
634
635/**
Daniel Veillard361d8452000-04-03 19:48:13 +0000636 * xmlURIUnescapeString:
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000637 * @str: the string to unescape
638 * @len: the lenght in bytes to unescape (or <= 0 to indicate full string)
639 * @target: optionnal destination buffer
640 *
641 * Unescaping routine, does not do validity checks !
Daniel Veillardec303412000-03-24 13:41:54 +0000642 * Output is direct unsigned char translation of %XX values (no encoding)
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000643 *
644 * Returns an copy of the string, but unescaped
645 */
646char *
Daniel Veillard361d8452000-04-03 19:48:13 +0000647xmlURIUnescapeString(const char *str, int len, char *target) {
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000648 char *ret, *out;
649 const char *in;
650
651 if (str == NULL)
652 return(NULL);
653 if (len <= 0) len = strlen(str);
654 if (len <= 0) return(NULL);
655
656 if (target == NULL) {
657 ret = (char *) xmlMalloc(len + 1);
658 if (ret == NULL) {
Daniel Veillard361d8452000-04-03 19:48:13 +0000659 fprintf(stderr, "xmlURIUnescapeString: out of memory\n");
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000660 return(NULL);
661 }
662 } else
663 ret = target;
664 in = str;
665 out = ret;
666 while(len > 0) {
667 if (*in == '%') {
668 in++;
669 if ((*in >= '0') && (*in <= '9'))
670 *out = (*in - '0');
671 else if ((*in >= 'a') && (*in <= 'f'))
672 *out = (*in - 'a') + 10;
673 else if ((*in >= 'A') && (*in <= 'F'))
674 *out = (*in - 'A') + 10;
675 in++;
676 if ((*in >= '0') && (*in <= '9'))
677 *out = *out * 16 + (*in - '0');
678 else if ((*in >= 'a') && (*in <= 'f'))
679 *out = *out * 16 + (*in - 'a') + 10;
680 else if ((*in >= 'A') && (*in <= 'F'))
681 *out = *out * 16 + (*in - 'A') + 10;
682 in++;
683 len -= 3;
Daniel Veillardec303412000-03-24 13:41:54 +0000684 out++;
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000685 } else {
686 *out++ = *in++;
687 len--;
688 }
689 }
690 *out = 0;
691 return(ret);
692}
693
694
695/**
696 * xmlParseURIFragment:
697 * @uri: pointer to an URI structure
698 * @str: pointer to the string to analyze
699 *
700 * Parse an URI fragment string and fills in the appropriate fields
701 * of the @uri structure.
702 *
703 * fragment = *uric
704 *
705 * Returns 0 or the error code
706 */
707int
708xmlParseURIFragment(xmlURIPtr uri, const char **str) {
709 const char *cur = *str;
710
711 if (str == NULL) return(-1);
712
713 while (IS_URIC(cur)) NEXT(cur);
714 if (uri != NULL) {
715 if (uri->fragment != NULL) xmlFree(uri->fragment);
Daniel Veillard361d8452000-04-03 19:48:13 +0000716 uri->fragment = xmlURIUnescapeString(*str, cur - *str, NULL);
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000717 }
718 *str = cur;
719 return(0);
720}
721
722/**
723 * xmlParseURIQuery:
724 * @uri: pointer to an URI structure
725 * @str: pointer to the string to analyze
726 *
727 * Parse the query part of an URI
728 *
729 * query = *uric
730 *
731 * Returns 0 or the error code
732 */
733int
734xmlParseURIQuery(xmlURIPtr uri, const char **str) {
735 const char *cur = *str;
736
737 if (str == NULL) return(-1);
738
739 while (IS_URIC(cur)) NEXT(cur);
740 if (uri != NULL) {
741 if (uri->query != NULL) xmlFree(uri->query);
Daniel Veillard361d8452000-04-03 19:48:13 +0000742 uri->query = xmlURIUnescapeString(*str, cur - *str, NULL);
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000743 }
744 *str = cur;
745 return(0);
746}
747
748/**
749 * xmlParseURIScheme:
750 * @uri: pointer to an URI structure
751 * @str: pointer to the string to analyze
752 *
753 * Parse an URI scheme
754 *
755 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
756 *
757 * Returns 0 or the error code
758 */
759int
760xmlParseURIScheme(xmlURIPtr uri, const char **str) {
761 const char *cur;
762
763 if (str == NULL)
764 return(-1);
765
766 cur = *str;
767 if (!IS_ALPHA(*cur))
768 return(2);
769 cur++;
770 while (IS_SCHEME(*cur)) cur++;
771 if (uri != NULL) {
772 if (uri->scheme != NULL) xmlFree(uri->scheme);
Daniel Veillard361d8452000-04-03 19:48:13 +0000773 uri->scheme = xmlURIUnescapeString(*str, cur - *str, NULL); /* !!! strndup */
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000774 }
775 *str = cur;
776 return(0);
777}
778
779/**
780 * xmlParseURIOpaquePart:
781 * @uri: pointer to an URI structure
782 * @str: pointer to the string to analyze
783 *
784 * Parse an URI opaque part
785 *
786 * opaque_part = uric_no_slash *uric
787 *
788 * Returns 0 or the error code
789 */
790int
791xmlParseURIOpaquePart(xmlURIPtr uri, const char **str) {
792 const char *cur;
793
794 if (str == NULL)
795 return(-1);
796
797 cur = *str;
798 if (!IS_URIC_NO_SLASH(cur)) {
799 return(3);
800 }
801 NEXT(cur);
802 while (IS_URIC(cur)) NEXT(cur);
803 if (uri != NULL) {
804 if (uri->opaque != NULL) xmlFree(uri->opaque);
Daniel Veillard361d8452000-04-03 19:48:13 +0000805 uri->opaque = xmlURIUnescapeString(*str, cur - *str, NULL);
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000806 }
807 *str = cur;
808 return(0);
809}
810
811/**
Daniel Veillard361d8452000-04-03 19:48:13 +0000812 * xmlParseURIServer:
813 * @uri: pointer to an URI structure
814 * @str: pointer to the string to analyze
815 *
816 * Parse a server subpart of an URI, it's a finer grain analysis
817 * of the authority part.
818 *
819 * server = [ [ userinfo "@" ] hostport ]
820 * userinfo = *( unreserved | escaped |
821 * ";" | ":" | "&" | "=" | "+" | "$" | "," )
822 * hostport = host [ ":" port ]
823 * host = hostname | IPv4address
824 * hostname = *( domainlabel "." ) toplabel [ "." ]
825 * domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
826 * toplabel = alpha | alpha *( alphanum | "-" ) alphanum
827 * IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
828 * port = *digit
829 *
830 * Returns 0 or the error code
831 */
832int
833xmlParseURIServer(xmlURIPtr uri, const char **str) {
834 const char *cur;
835 const char *host, *tmp;
836
837 if (str == NULL)
838 return(-1);
839
840 cur = *str;
841
842 /*
843 * is there an userinfo ?
844 */
845 while (IS_USERINFO(cur)) NEXT(cur);
846 if (*cur == '@') {
847 if (uri != NULL) {
848 if (uri->user != NULL) xmlFree(uri->user);
849 uri->user = xmlURIUnescapeString(*str, cur - *str, NULL);
850 }
851 cur++;
852 } else {
853 if (uri != NULL) {
854 if (uri->user != NULL) xmlFree(uri->user);
855 uri->user = NULL;
856 }
857 cur = *str;
858 }
859 /*
860 * host part of hostport can derive either an IPV4 address
861 * or an unresolved name. Check the IP first, it easier to detect
862 * errors if wrong one
863 */
864 host = cur;
865 if (IS_DIGIT(*cur)) {
866 while(IS_DIGIT(*cur)) cur++;
867 if (*cur != '.')
868 goto host_name;
869 cur++;
870 if (!IS_DIGIT(*cur))
871 goto host_name;
872 while(IS_DIGIT(*cur)) cur++;
873 if (*cur != '.')
874 goto host_name;
875 cur++;
876 if (!IS_DIGIT(*cur))
877 goto host_name;
878 while(IS_DIGIT(*cur)) cur++;
879 if (*cur != '.')
880 goto host_name;
881 cur++;
882 if (!IS_DIGIT(*cur))
883 goto host_name;
884 while(IS_DIGIT(*cur)) cur++;
885 if (uri != NULL) {
886 if (uri->authority != NULL) xmlFree(uri->authority);
887 uri->authority = NULL;
888 if (uri->server != NULL) xmlFree(uri->server);
889 uri->server = xmlURIUnescapeString(host, cur - host, NULL);
890 }
891 goto host_done;
892 }
893host_name:
894 /*
895 * the hostname production as-is is a parser nightmare.
896 * simplify it to
897 * hostname = *( domainlabel "." ) domainlabel [ "." ]
898 * and just make sure the last label starts with a non numeric char.
899 */
900 if (!IS_ALPHANUM(*cur))
901 return(6);
902 while (IS_ALPHANUM(*cur)) {
903 while ((IS_ALPHANUM(*cur)) || (*cur == '-')) cur++;
904 if (*cur == '.')
905 cur++;
906 }
907 tmp = cur;
908 tmp--;
909 while (IS_ALPHANUM(*tmp) && (*tmp != '.') && (tmp >= host)) tmp--;
910 tmp++;
911 if (!IS_ALPHA(*tmp))
912 return(7);
913 if (uri != NULL) {
914 if (uri->authority != NULL) xmlFree(uri->authority);
915 uri->authority = NULL;
916 if (uri->server != NULL) xmlFree(uri->server);
917 uri->server = xmlURIUnescapeString(host, cur - host, NULL);
918 }
919
920host_done:
921
922 /*
923 * finish by checking for a port presence.
924 */
925 if (*cur == ':') {
926 cur++;
927 if (IS_DIGIT(*cur)) {
928 if (uri != NULL)
929 uri->port = 0;
930 while (IS_DIGIT(*cur)) {
931 if (uri != NULL)
932 uri->port = uri->port * 10 + (*cur - '0');
933 cur++;
934 }
935 }
936 }
937 *str = cur;
938 return(0);
939}
940
941/**
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000942 * xmlParseURIRelSegment:
943 * @uri: pointer to an URI structure
944 * @str: pointer to the string to analyze
945 *
946 * Parse an URI relative segment
947 *
948 * rel_segment = 1*( unreserved | escaped | ";" | "@" | "&" | "=" |
949 * "+" | "$" | "," )
950 *
951 * Returns 0 or the error code
952 */
953int
954xmlParseURIRelSegment(xmlURIPtr uri, const char **str) {
955 const char *cur;
956
957 if (str == NULL)
958 return(-1);
959
960 cur = *str;
961 if (!IS_SEGMENT(cur)) {
962 return(3);
963 }
964 NEXT(cur);
965 while (IS_SEGMENT(cur)) NEXT(cur);
966 if (uri != NULL) {
967 if (uri->path != NULL) xmlFree(uri->path);
Daniel Veillard361d8452000-04-03 19:48:13 +0000968 uri->path = xmlURIUnescapeString(*str, cur - *str, NULL);
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000969 }
970 *str = cur;
971 return(0);
972}
973
974/**
975 * xmlParseURIPathSegments:
976 * @uri: pointer to an URI structure
977 * @str: pointer to the string to analyze
978 * @slash: should we add a leading slash
979 *
980 * Parse an URI set of path segments
981 *
982 * path_segments = segment *( "/" segment )
983 * segment = *pchar *( ";" param )
984 * param = *pchar
985 *
986 * Returns 0 or the error code
987 */
988int
989xmlParseURIPathSegments(xmlURIPtr uri, const char **str, int slash) {
990 const char *cur;
991
992 if (str == NULL)
993 return(-1);
994
995 cur = *str;
996
997 do {
998 while (IS_PCHAR(cur)) NEXT(cur);
999 if (*cur == ';') {
1000 cur++;
1001 while (IS_PCHAR(cur)) NEXT(cur);
1002 }
1003 if (*cur != '/') break;
1004 cur++;
1005 } while (1);
1006 if (uri != NULL) {
1007 int len, len2 = 0;
1008 char *path;
1009
1010 /*
1011 * Concat the set of path segments to the current path
1012 */
1013 len = cur - *str;
1014 if (slash)
1015 len++;
1016
1017 if (uri->path != NULL) {
1018 len2 = strlen(uri->path);
1019 len += len2;
1020 }
1021 path = (char *) xmlMalloc(len + 1);
1022 if (path == NULL) {
1023 fprintf(stderr, "xmlParseURIPathSegments: out of memory\n");
1024 *str = cur;
1025 return(-1);
1026 }
1027 if (uri->path != NULL)
1028 memcpy(path, uri->path, len2);
1029 if (slash) {
1030 path[len2] = '/';
1031 len2++;
1032 }
Daniel Veillard361d8452000-04-03 19:48:13 +00001033 xmlURIUnescapeString(*str, cur - *str, &path[len2]);
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001034 if (uri->path != NULL)
1035 xmlFree(uri->path);
1036 uri->path = path;
1037 }
1038 *str = cur;
1039 return(0);
1040}
1041
1042/**
1043 * xmlParseURIAuthority:
1044 * @uri: pointer to an URI structure
1045 * @str: pointer to the string to analyze
1046 *
1047 * Parse the authority part of an URI.
1048 *
1049 * authority = server | reg_name
1050 * server = [ [ userinfo "@" ] hostport ]
1051 * reg_name = 1*( unreserved | escaped | "$" | "," | ";" | ":" |
1052 * "@" | "&" | "=" | "+" )
1053 *
1054 * Note : this is completely ambiguous since reg_name is allowed to
1055 * use the full set of chars in use by server:
1056 *
1057 * 3.2.1. Registry-based Naming Authority
1058 *
1059 * The structure of a registry-based naming authority is specific
1060 * to the URI scheme, but constrained to the allowed characters
1061 * for an authority component.
1062 *
1063 * Returns 0 or the error code
1064 */
1065int
1066xmlParseURIAuthority(xmlURIPtr uri, const char **str) {
1067 const char *cur;
Daniel Veillard361d8452000-04-03 19:48:13 +00001068 int ret;
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001069
1070 if (str == NULL)
1071 return(-1);
1072
1073 cur = *str;
Daniel Veillard361d8452000-04-03 19:48:13 +00001074
1075 /*
1076 * try first to parse it as a server string.
1077 */
1078 ret = xmlParseURIServer(uri, str);
1079 if (ret == 0)
1080 return(0);
1081
1082 /*
1083 * failed, fallback to reg_name
1084 */
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001085 if (!IS_REG_NAME(cur)) {
1086 return(5);
1087 }
1088 NEXT(cur);
1089 while (IS_REG_NAME(cur)) NEXT(cur);
1090 if (uri != NULL) {
Daniel Veillard361d8452000-04-03 19:48:13 +00001091 if (uri->server != NULL) xmlFree(uri->server);
1092 uri->server = NULL;
1093 if (uri->user != NULL) xmlFree(uri->user);
1094 uri->user = NULL;
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001095 if (uri->authority != NULL) xmlFree(uri->authority);
Daniel Veillard361d8452000-04-03 19:48:13 +00001096 uri->authority = xmlURIUnescapeString(*str, cur - *str, NULL);
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001097 }
1098 *str = cur;
1099 return(0);
1100}
1101
1102/**
1103 * xmlParseURIHierPart:
1104 * @uri: pointer to an URI structure
1105 * @str: pointer to the string to analyze
1106 *
1107 * Parse an URI hirarchical part
1108 *
1109 * hier_part = ( net_path | abs_path ) [ "?" query ]
1110 * abs_path = "/" path_segments
1111 * net_path = "//" authority [ abs_path ]
1112 *
1113 * Returns 0 or the error code
1114 */
1115int
1116xmlParseURIHierPart(xmlURIPtr uri, const char **str) {
1117 int ret;
1118 const char *cur;
1119
1120 if (str == NULL)
1121 return(-1);
1122
1123 cur = *str;
1124
1125 if ((cur[0] == '/') && (cur[1] == '/')) {
1126 cur += 2;
1127 ret = xmlParseURIAuthority(uri, &cur);
1128 if (ret != 0)
1129 return(ret);
1130 if (cur[0] == '/') {
1131 cur++;
1132 ret = xmlParseURIPathSegments(uri, &cur, 1);
1133 }
1134 } else if (cur[0] == '/') {
1135 cur++;
1136 ret = xmlParseURIPathSegments(uri, &cur, 1);
1137 } else {
1138 return(4);
1139 }
1140 if (ret != 0)
1141 return(ret);
1142 if (*cur == '?') {
1143 cur++;
1144 ret = xmlParseURIQuery(uri, &cur);
1145 if (ret != 0)
1146 return(ret);
1147 }
1148 *str = cur;
1149 return(0);
1150}
1151
1152/**
1153 * xmlParseAbsoluteURI:
1154 * @uri: pointer to an URI structure
1155 * @str: pointer to the string to analyze
1156 *
1157 * Parse an URI reference string and fills in the appropriate fields
1158 * of the @uri structure
1159 *
1160 * absoluteURI = scheme ":" ( hier_part | opaque_part )
1161 *
1162 * Returns 0 or the error code
1163 */
1164int
1165xmlParseAbsoluteURI(xmlURIPtr uri, const char **str) {
1166 int ret;
1167
1168 if (str == NULL)
1169 return(-1);
1170
1171 ret = xmlParseURIScheme(uri, str);
1172 if (ret != 0) return(ret);
1173 if (**str != ':')
1174 return(1);
1175 (*str)++;
1176 if (**str == '/')
1177 return(xmlParseURIHierPart(uri, str));
1178 return(xmlParseURIOpaquePart(uri, str));
1179}
1180
1181/**
1182 * xmlParseRelativeURI:
1183 * @uri: pointer to an URI structure
1184 * @str: pointer to the string to analyze
1185 *
1186 * Parse an relative URI string and fills in the appropriate fields
1187 * of the @uri structure
1188 *
1189 * relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
1190 * abs_path = "/" path_segments
1191 * net_path = "//" authority [ abs_path ]
1192 * rel_path = rel_segment [ abs_path ]
1193 *
1194 * Returns 0 or the error code
1195 */
1196int
1197xmlParseRelativeURI(xmlURIPtr uri, const char **str) {
1198 int ret = 0;
1199 const char *cur;
1200
1201 if (str == NULL)
1202 return(-1);
1203
1204 cur = *str;
1205 if ((cur[0] == '/') && (cur[1] == '/')) {
1206 cur += 2;
1207 ret = xmlParseURIAuthority(uri, &cur);
1208 if (ret != 0)
1209 return(ret);
1210 if (cur[0] == '/') {
1211 cur++;
1212 ret = xmlParseURIPathSegments(uri, &cur, 1);
1213 }
1214 } else if (cur[0] == '/') {
1215 cur++;
1216 ret = xmlParseURIPathSegments(uri, &cur, 1);
1217 } else {
1218 ret = xmlParseURIRelSegment(uri, &cur);
1219 if (ret != 0)
1220 return(ret);
1221 if (cur[0] == '/') {
1222 cur++;
1223 ret = xmlParseURIPathSegments(uri, &cur, 1);
1224 }
1225 }
1226 if (ret != 0)
1227 return(ret);
1228 if (*cur == '?') {
1229 cur++;
1230 ret = xmlParseURIQuery(uri, &cur);
1231 if (ret != 0)
1232 return(ret);
1233 }
1234 *str = cur;
1235 return(ret);
1236}
1237
1238/**
1239 * xmlParseURIReference:
1240 * @uri: pointer to an URI structure
1241 * @str: the string to analyze
1242 *
1243 * Parse an URI reference string and fills in the appropriate fields
1244 * of the @uri structure
1245 *
1246 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
1247 *
1248 * Returns 0 or the error code
1249 */
1250int
1251xmlParseURIReference(xmlURIPtr uri, const char *str) {
1252 int ret;
1253 const char *tmp = str;
1254
1255 if (str == NULL)
1256 return(-1);
1257 xmlCleanURI(uri);
1258
1259 /*
1260 * Try first to parse aboslute refs, then fallback to relative if
1261 * it fails.
1262 */
1263 ret = xmlParseAbsoluteURI(uri, &str);
1264 if (ret != 0) {
1265 xmlCleanURI(uri);
1266 str = tmp;
1267 ret = xmlParseRelativeURI(uri, &str);
1268 }
1269 if (ret != 0) {
1270 xmlCleanURI(uri);
1271 return(ret);
1272 }
1273
1274 if (*str == '#') {
1275 str++;
1276 ret = xmlParseURIFragment(uri, &str);
1277 if (ret != 0) return(ret);
1278 }
1279 if (*str != 0) {
1280 xmlCleanURI(uri);
1281 return(1);
1282 }
1283 return(0);
1284}
1285
1286/**
Daniel Veillardec303412000-03-24 13:41:54 +00001287 * xmlNormalizeURIPath:
1288 * @path: pointer to the path string
1289 *
1290 * applies the 5 normalization steps to a path string
1291 * Normalization occurs directly on the string, no new allocation is done
1292 *
1293 * Returns 0 or an error code
1294 */
1295int
1296xmlNormalizeURIPath(char *path) {
1297 int cur, out;
1298
1299 if (path == NULL)
1300 return(-1);
1301 cur = 0;
1302 out = 0;
1303 while ((path[cur] != 0) && (path[cur] != '/')) cur++;
1304 if (path[cur] == 0)
1305 return(0);
1306
1307 /* we are positionned at the beginning of the first segment */
1308 cur++;
1309 out = cur;
1310
1311 /*
1312 * Analyze each segment in sequence.
1313 */
1314 while (path[cur] != 0) {
1315 /*
1316 * c) All occurrences of "./", where "." is a complete path segment,
1317 * are removed from the buffer string.
1318 */
1319 if ((path[cur] == '.') && (path[cur + 1] == '/')) {
1320 cur += 2;
1321 continue;
1322 }
1323
1324 /*
1325 * d) If the buffer string ends with "." as a complete path segment,
1326 * that "." is removed.
1327 */
1328 if ((path[cur] == '.') && (path[cur + 1] == 0)) {
1329 path[out] = 0;
1330 break;
1331 }
1332
1333 /* read the segment */
1334 while ((path[cur] != 0) && (path[cur] != '/')) {
1335 path[out++] = path[cur++];
1336 }
1337 path[out++] = path[cur];
1338 if (path[cur] != 0) {
1339 cur++;
1340 }
1341 }
1342
1343 cur = 0;
1344 out = 0;
1345 while ((path[cur] != 0) && (path[cur] != '/')) cur++;
1346 if (path[cur] == 0)
1347 return(0);
1348 /* we are positionned at the beginning of the first segment */
1349 cur++;
1350 out = cur;
1351 /*
1352 * Analyze each segment in sequence.
1353 */
1354 while (path[cur] != 0) {
1355 /*
1356 * e) All occurrences of "<segment>/../", where <segment> is a
1357 * complete path segment not equal to "..", are removed from the
1358 * buffer string. Removal of these path segments is performed
1359 * iteratively, removing the leftmost matching pattern on each
1360 * iteration, until no matching pattern remains.
1361 */
1362 if ((cur > 1) && (out > 1) &&
1363 (path[cur] == '/') && (path[cur + 1] == '.') &&
1364 (path[cur + 2] == '.') && (path[cur + 3] == '/') &&
1365 ((path[out] != '.') || (path[out - 1] != '.') ||
1366 (path[out - 2] != '/'))) {
1367 cur += 3;
1368 out --;
1369 while ((out > 0) && (path[out] != '/')) { out --; }
1370 path[out] = 0;
1371 continue;
1372 }
1373
1374 /*
1375 * f) If the buffer string ends with "<segment>/..", where <segment>
1376 * is a complete path segment not equal to "..", that
1377 * "<segment>/.." is removed.
1378 */
1379 if ((path[cur] == '/') && (path[cur + 1] == '.') &&
1380 (path[cur + 2] == '.') && (path[cur + 3] == 0) &&
1381 ((path[out] != '.') || (path[out - 1] != '.') ||
1382 (path[out - 2] != '/'))) {
1383 cur += 4;
1384 out --;
1385 while ((out > 0) && (path[out - 1] != '/')) { out --; }
1386 path[out] = 0;
1387 continue;
1388 }
1389
1390 path[out++] = path[cur++]; /* / or 0 */
1391 }
1392 path[out] = 0;
1393
1394 /*
1395 * g) If the resulting buffer string still begins with one or more
1396 * complete path segments of "..", then the reference is
1397 * considered to be in error. Implementations may handle this
1398 * error by retaining these components in the resolved path (i.e.,
1399 * treating them as part of the final URI), by removing them from
1400 * the resolved path (i.e., discarding relative levels above the
1401 * root), or by avoiding traversal of the reference.
1402 *
1403 * We discard them from the final path.
1404 */
1405 cur = 0;
1406 while ((path[cur] == '/') && (path[cur + 1] == '.') &&
1407 (path[cur + 2] == '.'))
1408 cur += 3;
1409 if (cur != 0) {
1410 out = 0;
1411 while (path[cur] != 0) path[out++] = path[cur++];
1412 path[out] = 0;
1413 }
1414 return(0);
1415}
1416
1417/**
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001418 * xmlBuildURI:
1419 * @URI: the URI instance found in the document
1420 * @base: the base value
1421 *
1422 * Computes he final URI of the reference done by checking that
1423 * the given URI is valid, and building the final URI using the
1424 * base URI. This is processed according to section 5.2 of the
1425 * RFC 2396
1426 *
1427 * 5.2. Resolving Relative References to Absolute Form
1428 *
Daniel Veillardec303412000-03-24 13:41:54 +00001429 * Returns a new URI string (to be freed by the caller) or NULL in case
1430 * of error.
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001431 */
1432xmlChar *
1433xmlBuildURI(const xmlChar *URI, const xmlChar *base) {
Daniel Veillardec303412000-03-24 13:41:54 +00001434 xmlChar *val = NULL;
1435 int ret, len, index, cur, out;
1436 xmlURIPtr ref = NULL;
1437 xmlURIPtr bas = NULL;
1438 xmlURIPtr res = NULL;
1439
1440
1441 /*
1442 * 1) The URI reference is parsed into the potential four components and
1443 * fragment identifier, as described in Section 4.3.
1444 */
1445 ref = xmlCreateURI();
1446 if (ref == NULL)
1447 goto done;
1448 ret = xmlParseURIReference(ref, (const char *) URI);
1449 if (ret != 0)
1450 goto done;
1451 bas = xmlCreateURI();
1452 if (bas == NULL)
1453 goto done;
1454 ret = xmlParseURIReference(bas, (const char *) base);
1455 if (ret != 0)
1456 goto done;
1457
1458 /*
1459 * 2) If the path component is empty and the scheme, authority, and
1460 * query components are undefined, then it is a reference to the
1461 * current document and we are done. Otherwise, the reference URI's
1462 * query and fragment components are defined as found (or not found)
1463 * within the URI reference and not inherited from the base URI.
1464 */
1465 res = xmlCreateURI();
1466 if (res == NULL)
1467 goto done;
1468 if ((ref->scheme == NULL) && (ref->path == NULL) &&
Daniel Veillard361d8452000-04-03 19:48:13 +00001469 ((ref->authority == NULL) && (ref->server == NULL)) &&
1470 (ref->query == NULL)) {
Daniel Veillardec303412000-03-24 13:41:54 +00001471 if (ref->fragment == NULL)
1472 goto done;
1473 res->fragment = xmlMemStrdup(ref->fragment);
1474 val = xmlSaveUri(res);
1475 goto done;
1476 }
1477
1478 /*
1479 * 3) If the scheme component is defined, indicating that the reference
1480 * starts with a scheme name, then the reference is interpreted as an
1481 * absolute URI and we are done. Otherwise, the reference URI's
1482 * scheme is inherited from the base URI's scheme component.
1483 */
1484 if (ref->scheme != NULL) {
1485 val = xmlSaveUri(ref);
1486 goto done;
1487 }
1488 res->scheme = xmlMemStrdup(bas->scheme);
1489
1490 /*
1491 * 4) If the authority component is defined, then the reference is a
1492 * network-path and we skip to step 7. Otherwise, the reference
1493 * URI's authority is inherited from the base URI's authority
1494 * component, which will also be undefined if the URI scheme does not
1495 * use an authority component.
1496 */
Daniel Veillard361d8452000-04-03 19:48:13 +00001497 if ((ref->authority != NULL) || (ref->server != NULL)) {
1498 if (ref->authority != NULL)
1499 res->authority = xmlMemStrdup(ref->authority);
1500 else {
1501 res->server = xmlMemStrdup(ref->server);
1502 if (ref->user != NULL)
1503 res->user = xmlMemStrdup(ref->user);
1504 res->port = ref->port;
1505 }
Daniel Veillardec303412000-03-24 13:41:54 +00001506 if (ref->path != NULL)
1507 res->path = xmlMemStrdup(ref->path);
1508 if (ref->query != NULL)
1509 res->query = xmlMemStrdup(ref->query);
1510 if (ref->fragment != NULL)
1511 res->fragment = xmlMemStrdup(ref->fragment);
1512 goto step_7;
1513 }
1514 if (bas->authority != NULL)
1515 res->authority = xmlMemStrdup(bas->authority);
Daniel Veillard361d8452000-04-03 19:48:13 +00001516 else if (bas->server != NULL) {
1517 res->server = xmlMemStrdup(bas->server);
1518 if (bas->user != NULL)
1519 res->user = xmlMemStrdup(bas->user);
1520 res->port = bas->port;
1521 }
Daniel Veillardec303412000-03-24 13:41:54 +00001522
1523 /*
1524 * 5) If the path component begins with a slash character ("/"), then
1525 * the reference is an absolute-path and we skip to step 7.
1526 */
1527 if ((ref->path != NULL) && (ref->path[0] == '/')) {
1528 res->path = xmlMemStrdup(ref->path);
1529 if (ref->query != NULL)
1530 res->query = xmlMemStrdup(ref->query);
1531 if (ref->fragment != NULL)
1532 res->fragment = xmlMemStrdup(ref->fragment);
1533 goto step_7;
1534 }
1535
1536
1537 /*
1538 * 6) If this step is reached, then we are resolving a relative-path
1539 * reference. The relative path needs to be merged with the base
1540 * URI's path. Although there are many ways to do this, we will
1541 * describe a simple method using a separate string buffer.
1542 *
1543 * Allocate a buffer large enough for the result string.
1544 */
1545 len = 2; /* extra / and 0 */
1546 if (ref->path != NULL)
1547 len += strlen(ref->path);
1548 if (bas->path != NULL)
1549 len += strlen(bas->path);
1550 res->path = (char *) xmlMalloc(len);
1551 if (res->path == NULL) {
1552 fprintf(stderr, "xmlBuildURI: out of memory\n");
1553 goto done;
1554 }
1555 res->path[0] = 0;
1556
1557 /*
1558 * a) All but the last segment of the base URI's path component is
1559 * copied to the buffer. In other words, any characters after the
1560 * last (right-most) slash character, if any, are excluded.
1561 */
1562 cur = 0;
1563 out = 0;
1564 if (bas->path != NULL) {
1565 while (bas->path[cur] != 0) {
1566 while ((bas->path[cur] != 0) && (bas->path[cur] != '/'))
1567 cur++;
1568 if (bas->path[cur] == 0)
1569 break;
1570
1571 cur++;
1572 while (out < cur) {
1573 res->path[out] = bas->path[out];
1574 out++;
1575 }
1576 }
1577 }
1578 res->path[out] = 0;
1579
1580 /*
1581 * b) The reference's path component is appended to the buffer
1582 * string.
1583 */
1584 if (ref->path != NULL) {
1585 index = 0;
1586 while (ref->path[index] != 0) {
1587 res->path[out++] = ref->path[index++];
1588 }
1589 }
1590 res->path[out] = 0;
1591
1592 /*
1593 * Steps c) to h) are really path normalization steps
1594 */
1595 xmlNormalizeURIPath(res->path);
1596
1597step_7:
1598
1599 /*
1600 * 7) The resulting URI components, including any inherited from the
1601 * base URI, are recombined to give the absolute form of the URI
1602 * reference.
1603 */
1604 val = xmlSaveUri(res);
1605
1606done:
1607 if (ref != NULL)
1608 xmlFreeURI(ref);
1609 if (base != NULL)
1610 xmlFreeURI(bas);
1611 if (res != NULL)
1612 xmlFreeURI(res);
1613 return(val);
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001614}
1615
1616