blob: 1ed141048cb12bb825d77fd043034737db43c5de [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
Daniel Veillard06047432000-04-24 11:33:38 +000024/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000025 * alpha = lowalpha | upalpha
26 */
27#define IS_ALPHA(x) (IS_LOWALPHA(x) || IS_UPALPHA(x))
28
29
Daniel Veillard06047432000-04-24 11:33:38 +000030/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000031 * 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
Daniel Veillard06047432000-04-24 11:33:38 +000038/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000039 * 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
Daniel Veillard06047432000-04-24 11:33:38 +000045/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000046 * digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
47 */
48
49#define IS_DIGIT(x) (((x) >= '0') && ((x) <= '9'))
50
Daniel Veillard06047432000-04-24 11:33:38 +000051/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000052 * alphanum = alpha | digit
53 */
54
55#define IS_ALPHANUM(x) (IS_ALPHA(x) || IS_DIGIT(x))
56
Daniel Veillard06047432000-04-24 11:33:38 +000057/*
58 * hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
Daniel Veillard3dd82e72000-03-20 11:48:04 +000059 * "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
Daniel Veillard06047432000-04-24 11:33:38 +000065/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000066 * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
67 */
68
69#define IS_MARK(x) (((x) == '-') || ((x) == '_') || ((x) == '.') || \
70 ((x) == '!') || ((x) == '~') || ((x) == '*') || ((x) == '\'') || \
71 ((x) == '(') || ((x) == ')'))
72
73
Daniel Veillard06047432000-04-24 11:33:38 +000074/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000075 * reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
76 */
77
78#define IS_RESERVED(x) (((x) == ';') || ((x) == '/') || ((x) == '?') || \
79 ((x) == ':') || ((x) == '@') || ((x) == '&') || ((x) == '=') || \
80 ((x) == '+') || ((x) == '$') || ((x) == ','))
81
Daniel Veillard06047432000-04-24 11:33:38 +000082/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000083 * unreserved = alphanum | mark
84 */
85
86#define IS_UNRESERVED(x) (IS_ALPHANUM(x) || IS_MARK(x))
87
Daniel Veillard06047432000-04-24 11:33:38 +000088/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000089 * escaped = "%" hex hex
90 */
91
92#define IS_ESCAPED(p) ((*(p) == '%') && (IS_HEX((p)[1])) && \
93 (IS_HEX((p)[2])))
94
Daniel Veillard06047432000-04-24 11:33:38 +000095/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +000096 * 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
Daniel Veillard06047432000-04-24 11:33:38 +0000104/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000105 * 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
Daniel Veillard06047432000-04-24 11:33:38 +0000112/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000113 * 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
Daniel Veillard06047432000-04-24 11:33:38 +0000122/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000123 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
124 */
125
126#define IS_SCHEME(x) ((IS_ALPHA(x)) || (IS_DIGIT(x)) || \
127 ((x) == '+') || ((x) == '-') || ((x) == '.'))
128
Daniel Veillard06047432000-04-24 11:33:38 +0000129/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000130 * 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
Daniel Veillard06047432000-04-24 11:33:38 +0000139/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000140 * 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
Daniel Veillard06047432000-04-24 11:33:38 +0000148/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000149 * uric = reserved | unreserved | escaped
150 */
151
152#define IS_URIC(p) ((IS_UNRESERVED(*(p))) || (IS_ESCAPED(p)) || \
153 (IS_RESERVED(*(p))))
154
Daniel Veillard06047432000-04-24 11:33:38 +0000155/*
Daniel Veillard3dd82e72000-03-20 11:48:04 +0000156 * Skip to next pointer char, handle escaped sequences
157 */
158
159#define NEXT(p) ((*p == '%')? p += 3 : p++)
160
Daniel Veillard06047432000-04-24 11:33:38 +0000161/*
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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000210 ret = (xmlChar *) xmlMalloc((max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000211 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000222 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000223 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000232 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000233 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000245 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000246 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000281 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000282 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000292 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillard361d8452000-04-03 19:48:13 +0000293 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000305 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillard361d8452000-04-03 19:48:13 +0000306 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000341 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillard361d8452000-04-03 19:48:13 +0000342 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000353 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillard361d8452000-04-03 19:48:13 +0000354 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000364 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillard361d8452000-04-03 19:48:13 +0000365 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000375 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000376 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000387 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000388 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000427 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000428 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000465 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000466 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000476 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000477 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000511 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000512 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000522 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000523 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;
Daniel Veillard32bc74e2000-07-14 14:49:25 +0000556 ret = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar));
Daniel Veillardec303412000-03-24 13:41:54 +0000557 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 Veillard496a1cf2000-05-03 14:20:55 +00001287 * xmlParseURI:
1288 * @str: the URI string to analyze
1289 *
1290 * Parse an URI
1291 *
1292 * URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
1293 *
1294 * Returns a newly build xmlURIPtr or NULL in case of error
1295 */
1296xmlURIPtr
1297xmlParseURI(const char *str) {
1298 xmlURIPtr uri;
1299 int ret;
1300
1301 if (str == NULL)
1302 return(NULL);
1303 uri = xmlCreateURI();
1304 if (uri != NULL) {
1305 ret = xmlParseURIReference(uri, str);
1306 if (ret) {
1307 xmlFreeURI(uri);
1308 return(NULL);
1309 }
1310 }
1311 return(uri);
1312}
1313
1314/**
Daniel Veillardec303412000-03-24 13:41:54 +00001315 * xmlNormalizeURIPath:
1316 * @path: pointer to the path string
1317 *
1318 * applies the 5 normalization steps to a path string
1319 * Normalization occurs directly on the string, no new allocation is done
1320 *
1321 * Returns 0 or an error code
1322 */
1323int
1324xmlNormalizeURIPath(char *path) {
1325 int cur, out;
1326
1327 if (path == NULL)
1328 return(-1);
1329 cur = 0;
1330 out = 0;
1331 while ((path[cur] != 0) && (path[cur] != '/')) cur++;
1332 if (path[cur] == 0)
1333 return(0);
1334
1335 /* we are positionned at the beginning of the first segment */
1336 cur++;
1337 out = cur;
1338
1339 /*
1340 * Analyze each segment in sequence.
1341 */
1342 while (path[cur] != 0) {
1343 /*
1344 * c) All occurrences of "./", where "." is a complete path segment,
1345 * are removed from the buffer string.
1346 */
1347 if ((path[cur] == '.') && (path[cur + 1] == '/')) {
1348 cur += 2;
1349 continue;
1350 }
1351
1352 /*
1353 * d) If the buffer string ends with "." as a complete path segment,
1354 * that "." is removed.
1355 */
1356 if ((path[cur] == '.') && (path[cur + 1] == 0)) {
1357 path[out] = 0;
1358 break;
1359 }
1360
1361 /* read the segment */
1362 while ((path[cur] != 0) && (path[cur] != '/')) {
1363 path[out++] = path[cur++];
1364 }
1365 path[out++] = path[cur];
1366 if (path[cur] != 0) {
1367 cur++;
1368 }
1369 }
1370
1371 cur = 0;
1372 out = 0;
1373 while ((path[cur] != 0) && (path[cur] != '/')) cur++;
1374 if (path[cur] == 0)
1375 return(0);
1376 /* we are positionned at the beginning of the first segment */
1377 cur++;
1378 out = cur;
1379 /*
1380 * Analyze each segment in sequence.
1381 */
1382 while (path[cur] != 0) {
1383 /*
1384 * e) All occurrences of "<segment>/../", where <segment> is a
1385 * complete path segment not equal to "..", are removed from the
1386 * buffer string. Removal of these path segments is performed
1387 * iteratively, removing the leftmost matching pattern on each
1388 * iteration, until no matching pattern remains.
1389 */
1390 if ((cur > 1) && (out > 1) &&
1391 (path[cur] == '/') && (path[cur + 1] == '.') &&
1392 (path[cur + 2] == '.') && (path[cur + 3] == '/') &&
1393 ((path[out] != '.') || (path[out - 1] != '.') ||
1394 (path[out - 2] != '/'))) {
1395 cur += 3;
1396 out --;
1397 while ((out > 0) && (path[out] != '/')) { out --; }
1398 path[out] = 0;
1399 continue;
1400 }
1401
1402 /*
1403 * f) If the buffer string ends with "<segment>/..", where <segment>
1404 * is a complete path segment not equal to "..", that
1405 * "<segment>/.." is removed.
1406 */
1407 if ((path[cur] == '/') && (path[cur + 1] == '.') &&
1408 (path[cur + 2] == '.') && (path[cur + 3] == 0) &&
1409 ((path[out] != '.') || (path[out - 1] != '.') ||
1410 (path[out - 2] != '/'))) {
1411 cur += 4;
1412 out --;
1413 while ((out > 0) && (path[out - 1] != '/')) { out --; }
1414 path[out] = 0;
1415 continue;
1416 }
1417
1418 path[out++] = path[cur++]; /* / or 0 */
1419 }
1420 path[out] = 0;
1421
1422 /*
1423 * g) If the resulting buffer string still begins with one or more
1424 * complete path segments of "..", then the reference is
1425 * considered to be in error. Implementations may handle this
1426 * error by retaining these components in the resolved path (i.e.,
1427 * treating them as part of the final URI), by removing them from
1428 * the resolved path (i.e., discarding relative levels above the
1429 * root), or by avoiding traversal of the reference.
1430 *
1431 * We discard them from the final path.
1432 */
1433 cur = 0;
1434 while ((path[cur] == '/') && (path[cur + 1] == '.') &&
1435 (path[cur + 2] == '.'))
1436 cur += 3;
1437 if (cur != 0) {
1438 out = 0;
1439 while (path[cur] != 0) path[out++] = path[cur++];
1440 path[out] = 0;
1441 }
1442 return(0);
1443}
1444
1445/**
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001446 * xmlBuildURI:
1447 * @URI: the URI instance found in the document
1448 * @base: the base value
1449 *
1450 * Computes he final URI of the reference done by checking that
1451 * the given URI is valid, and building the final URI using the
1452 * base URI. This is processed according to section 5.2 of the
1453 * RFC 2396
1454 *
1455 * 5.2. Resolving Relative References to Absolute Form
1456 *
Daniel Veillardec303412000-03-24 13:41:54 +00001457 * Returns a new URI string (to be freed by the caller) or NULL in case
1458 * of error.
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001459 */
1460xmlChar *
1461xmlBuildURI(const xmlChar *URI, const xmlChar *base) {
Daniel Veillardec303412000-03-24 13:41:54 +00001462 xmlChar *val = NULL;
1463 int ret, len, index, cur, out;
1464 xmlURIPtr ref = NULL;
1465 xmlURIPtr bas = NULL;
1466 xmlURIPtr res = NULL;
1467
1468
1469 /*
1470 * 1) The URI reference is parsed into the potential four components and
1471 * fragment identifier, as described in Section 4.3.
1472 */
1473 ref = xmlCreateURI();
1474 if (ref == NULL)
1475 goto done;
1476 ret = xmlParseURIReference(ref, (const char *) URI);
1477 if (ret != 0)
1478 goto done;
1479 bas = xmlCreateURI();
1480 if (bas == NULL)
1481 goto done;
1482 ret = xmlParseURIReference(bas, (const char *) base);
1483 if (ret != 0)
1484 goto done;
1485
1486 /*
1487 * 2) If the path component is empty and the scheme, authority, and
1488 * query components are undefined, then it is a reference to the
1489 * current document and we are done. Otherwise, the reference URI's
1490 * query and fragment components are defined as found (or not found)
1491 * within the URI reference and not inherited from the base URI.
1492 */
1493 res = xmlCreateURI();
1494 if (res == NULL)
1495 goto done;
1496 if ((ref->scheme == NULL) && (ref->path == NULL) &&
Daniel Veillard361d8452000-04-03 19:48:13 +00001497 ((ref->authority == NULL) && (ref->server == NULL)) &&
1498 (ref->query == NULL)) {
Daniel Veillardec303412000-03-24 13:41:54 +00001499 if (ref->fragment == NULL)
1500 goto done;
1501 res->fragment = xmlMemStrdup(ref->fragment);
1502 val = xmlSaveUri(res);
1503 goto done;
1504 }
1505
1506 /*
1507 * 3) If the scheme component is defined, indicating that the reference
1508 * starts with a scheme name, then the reference is interpreted as an
1509 * absolute URI and we are done. Otherwise, the reference URI's
1510 * scheme is inherited from the base URI's scheme component.
1511 */
1512 if (ref->scheme != NULL) {
1513 val = xmlSaveUri(ref);
1514 goto done;
1515 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001516 if (bas->scheme != NULL)
1517 res->scheme = xmlMemStrdup(bas->scheme);
1518 else
1519 res->scheme = NULL;
Daniel Veillardec303412000-03-24 13:41:54 +00001520
1521 /*
1522 * 4) If the authority component is defined, then the reference is a
1523 * network-path and we skip to step 7. Otherwise, the reference
1524 * URI's authority is inherited from the base URI's authority
1525 * component, which will also be undefined if the URI scheme does not
1526 * use an authority component.
1527 */
Daniel Veillard361d8452000-04-03 19:48:13 +00001528 if ((ref->authority != NULL) || (ref->server != NULL)) {
1529 if (ref->authority != NULL)
1530 res->authority = xmlMemStrdup(ref->authority);
1531 else {
1532 res->server = xmlMemStrdup(ref->server);
1533 if (ref->user != NULL)
1534 res->user = xmlMemStrdup(ref->user);
1535 res->port = ref->port;
1536 }
Daniel Veillardec303412000-03-24 13:41:54 +00001537 if (ref->path != NULL)
1538 res->path = xmlMemStrdup(ref->path);
1539 if (ref->query != NULL)
1540 res->query = xmlMemStrdup(ref->query);
1541 if (ref->fragment != NULL)
1542 res->fragment = xmlMemStrdup(ref->fragment);
1543 goto step_7;
1544 }
1545 if (bas->authority != NULL)
1546 res->authority = xmlMemStrdup(bas->authority);
Daniel Veillard361d8452000-04-03 19:48:13 +00001547 else if (bas->server != NULL) {
1548 res->server = xmlMemStrdup(bas->server);
1549 if (bas->user != NULL)
1550 res->user = xmlMemStrdup(bas->user);
1551 res->port = bas->port;
1552 }
Daniel Veillardec303412000-03-24 13:41:54 +00001553
1554 /*
1555 * 5) If the path component begins with a slash character ("/"), then
1556 * the reference is an absolute-path and we skip to step 7.
1557 */
1558 if ((ref->path != NULL) && (ref->path[0] == '/')) {
1559 res->path = xmlMemStrdup(ref->path);
1560 if (ref->query != NULL)
1561 res->query = xmlMemStrdup(ref->query);
1562 if (ref->fragment != NULL)
1563 res->fragment = xmlMemStrdup(ref->fragment);
1564 goto step_7;
1565 }
1566
1567
1568 /*
1569 * 6) If this step is reached, then we are resolving a relative-path
1570 * reference. The relative path needs to be merged with the base
1571 * URI's path. Although there are many ways to do this, we will
1572 * describe a simple method using a separate string buffer.
1573 *
1574 * Allocate a buffer large enough for the result string.
1575 */
1576 len = 2; /* extra / and 0 */
1577 if (ref->path != NULL)
1578 len += strlen(ref->path);
1579 if (bas->path != NULL)
1580 len += strlen(bas->path);
1581 res->path = (char *) xmlMalloc(len);
1582 if (res->path == NULL) {
1583 fprintf(stderr, "xmlBuildURI: out of memory\n");
1584 goto done;
1585 }
1586 res->path[0] = 0;
1587
1588 /*
1589 * a) All but the last segment of the base URI's path component is
1590 * copied to the buffer. In other words, any characters after the
1591 * last (right-most) slash character, if any, are excluded.
1592 */
1593 cur = 0;
1594 out = 0;
1595 if (bas->path != NULL) {
1596 while (bas->path[cur] != 0) {
1597 while ((bas->path[cur] != 0) && (bas->path[cur] != '/'))
1598 cur++;
1599 if (bas->path[cur] == 0)
1600 break;
1601
1602 cur++;
1603 while (out < cur) {
1604 res->path[out] = bas->path[out];
1605 out++;
1606 }
1607 }
1608 }
1609 res->path[out] = 0;
1610
1611 /*
1612 * b) The reference's path component is appended to the buffer
1613 * string.
1614 */
1615 if (ref->path != NULL) {
1616 index = 0;
1617 while (ref->path[index] != 0) {
1618 res->path[out++] = ref->path[index++];
1619 }
1620 }
1621 res->path[out] = 0;
1622
1623 /*
1624 * Steps c) to h) are really path normalization steps
1625 */
1626 xmlNormalizeURIPath(res->path);
1627
1628step_7:
1629
1630 /*
1631 * 7) The resulting URI components, including any inherited from the
1632 * base URI, are recombined to give the absolute form of the URI
1633 * reference.
1634 */
1635 val = xmlSaveUri(res);
1636
1637done:
1638 if (ref != NULL)
1639 xmlFreeURI(ref);
1640 if (base != NULL)
1641 xmlFreeURI(bas);
1642 if (res != NULL)
1643 xmlFreeURI(res);
1644 return(val);
Daniel Veillard3dd82e72000-03-20 11:48:04 +00001645}
1646
1647