Debian Codegraph @

deps/libneon/src/ne_uri.c

raw
1 /*
2 URI manipulation routines.
3 Copyright (C) 1999-2006, Joe Orton <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA
19
20 */
21
22 #include "config.h"
23
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27 #ifdef HAVE_STRINGS_H
28 #include <strings.h>
29 #endif
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36
37 #include <stdio.h>
38
39 #include <ctype.h>
40
41 #include "ne_string.h" /* for ne_buffer */
42 #include "ne_alloc.h"
43 #include "ne_uri.h"
44
45 /* URI ABNF from RFC 3986: */
46
47 #define PS (0x0001) /* "+" */
48 #define PC (0x0002) /* "%" */
49 #define DS (0x0004) /* "-" */
50 #define DT (0x0008) /* "." */
51 #define US (0x0010) /* "_" */
52 #define TD (0x0020) /* "~" */
53 #define FS (0x0040) /* "/" */
54 #define CL (0x0080) /* ":" */
55 #define AT (0x0100) /* "@" */
56 #define QU (0x0200) /* "?" */
57
58 #define DG (0x0400) /* DIGIT */
59 #define AL (0x0800) /* ALPHA */
60
61 #define GD (0x1000) /* gen-delims = "#" / "[" / "]"
62 * ... except ":", "/", "@", and "?" */
63
64 #define SD (0x2000) /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
65 * / "*" / "+" / "," / ";" / "="
66 * ... except "+" which is PS */
67
68 #define OT (0x4000) /* others */
69
70 #define URI_ALPHA (AL)
71 #define URI_DIGIT (DG)
72
73 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
74 #define URI_UNRESERVED (AL | DG | DS | DT | US | TD)
75 /* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */
76 #define URI_SCHEME (AL | DG | PS | DS | DT)
77 /* real sub-delims definition, including "+" */
78 #define URI_SUBDELIM (PS | SD)
79 /* real gen-delims definition, including ":", "/", "@" and "?" */
80 #define URI_GENDELIM (GD | CL | FS | AT | QU)
81 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
82 #define URI_USERINFO (URI_UNRESERVED | PC | URI_SUBDELIM | CL)
83 /* pchar = unreserved / pct-encoded / sub-delims / ":" / "@" */
84 #define URI_PCHAR (URI_UNRESERVED | PC | URI_SUBDELIM | CL | AT)
85 /* invented: segchar = pchar / "/" */
86 #define URI_SEGCHAR (URI_PCHAR | FS)
87 /* query = *( pchar / "/" / "?" ) */
88 #define URI_QUERY (URI_PCHAR | FS | QU)
89 /* fragment == query */
90 #define URI_FRAGMENT URI_QUERY
91
92 /* any characters which should be path-escaped: */
93 #define URI_ESCAPE ((URI_GENDELIM & ~(FS)) | URI_SUBDELIM | OT | PC)
94
95 static const unsigned int uri_chars[256] = {
96 /* 0xXX x0 x2 x4 x6 x8 xA xC xE */
97 /* 0x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
98 /* 1x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
99 /* 2x */ OT, SD, OT, GD, SD, PC, SD, SD, SD, SD, SD, PS, SD, DS, DT, FS,
100 /* 3x */ DG, DG, DG, DG, DG, DG, DG, DG, DG, DG, CL, SD, OT, SD, OT, QU,
101 /* 4x */ AT, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
102 /* 5x */ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, GD, OT, GD, OT, US,
103 /* 6x */ OT, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL,
104 /* 7x */ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, OT, OT, OT, TD, OT,
105 /* 8x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
106 /* 9x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
107 /* Ax */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
108 /* Bx */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
109 /* Cx */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
110 /* Dx */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
111 /* Ex */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
112 /* Fx */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT
113 };
114
115 #define uri_lookup(ch) (uri_chars[(unsigned char)ch])
116
117 char *ne_path_parent(const char *uri)
118 {
119 size_t len = strlen(uri);
120 const char *pnt = uri + len - 1;
121 /* skip trailing slash (parent of "/foo/" is "/") */
122 if (pnt >= uri && *pnt == '/')
123 pnt--;
124 /* find previous slash */
125 while (pnt > uri && *pnt != '/')
126 pnt--;
127 if (pnt < uri || (pnt == uri && *pnt != '/'))
128 return NULL;
129 return ne_strndup(uri, pnt - uri + 1);
130 }
131
132 int ne_path_has_trailing_slash(const char *uri)
133 {
134 size_t len = strlen(uri);
135 return ((len > 0) &&
136 (uri[len-1] == '/'));
137 }
138
139 unsigned int ne_uri_defaultport(const char *scheme)
140 {
141 /* RFC2616/3.2.3 says use case-insensitive comparisons here. */
142 if (ne_strcasecmp(scheme, "http") == 0)
143 return 80;
144 else if (ne_strcasecmp(scheme, "https") == 0)
145 return 443;
146 else
147 return 0;
148 }
149
150 int ne_uri_parse(const char *uri, ne_uri *parsed)
151 {
152 const char *p, *s;
153
154 memset(parsed, 0, sizeof *parsed);
155
156 p = s = uri;
157
158 /* => s = p = URI-reference */
159
160 if (uri_lookup(*p) & URI_ALPHA) {
161 while (uri_lookup(*p) & URI_SCHEME)
162 p++;
163
164 if (*p == ':') {
165 parsed->scheme = ne_strndup(uri, p - s);
166 s = p + 1;
167 }
168 }
169
170 /* => s = heir-part, or s = relative-part */
171
172 if (s[0] == '/' && s[1] == '/') {
173 const char *pa;
174
175 /* => s = "//" authority path-abempty (from expansion of
176 * either heir-part of relative-part) */
177
178 /* authority = [ userinfo "@" ] host [ ":" port ] */
179
180 s = pa = s + 2; /* => s = authority */
181
182 while (*pa != '/' && *pa != '\0')
183 pa++;
184 /* => pa = path-abempty */
185
186 p = s;
187 while (p < pa && uri_lookup(*p) & URI_USERINFO)
188 p++;
189
190 if (*p == '@') {
191 parsed->userinfo = ne_strndup(s, p - s);
192 s = p + 1;
193 }
194 /* => s = host */
195
196 if (s[0] == '[') {
197 p = s + 1;
198
199 while (*p != ']' && p < pa)
200 p++;
201
202 if (p == pa || (p + 1 != pa && p[1] != ':')) {
203 /* Ill-formed IP-literal. */
204 return -1;
205 }
206
207 p++; /* => p = colon */
208 } else {
209 /* Find the colon. */
210 p = pa;
211 while (*p != ':' && p > s)
212 p--;
213 }
214
215 if (p == s) {
216 p = pa;
217 /* No colon; => p = path-abempty */
218 } else if (p + 1 != pa) {
219 /* => p = colon */
220 parsed->port = atoi(p + 1);
221 if(parsed->port == 0)
222 parsed->port = UINT_MAX;
223 }
224 parsed->host = ne_strndup(s, p - s);
225
226 s = pa;
227
228 if (*s == '\0') {
229 s = "/"; /* FIXME: scheme-specific. */
230 }
231 }
232
233 /* => s = path-abempty / path-absolute / path-rootless
234 * / path-empty / path-noscheme */
235
236 p = s;
237
238 while (uri_lookup(*p) & URI_SEGCHAR)
239 p++;
240
241 /* => p = [ "?" query ] [ "#" fragment ] */
242
243 parsed->path = ne_strndup(s, p - s);
244
245 if (*p != '\0') {
246 s = p++;
247
248 while (uri_lookup(*p) & URI_QUERY)
249 p++;
250
251 /* => p = [ "#" fragment ] */
252 /* => s = [ "?" query ] [ "#" fragment ] */
253
254 if (*s == '?') {
255 parsed->query = ne_strndup(s + 1, p - s - 1);
256
257 if (*p != '\0') {
258 s = p++;
259
260 while (uri_lookup(*p) & URI_FRAGMENT)
261 p++;
262 }
263 }
264
265 /* => p now points to the next character after the
266 * URI-reference; which should be the NUL byte. */
267
268 if (*s == '#') {
269 parsed->fragment = ne_strndup(s + 1, p - s - 1);
270 }
271 else if (*p || *s != '?') {
272 return -1;
273 }
274 }
275
276 return 0;
277 }
278
279 /* This function directly implements the "Merge Paths" algorithm
280 * described in RFC 3986 section 5.2.3. */
281 static char *merge_paths(const ne_uri *base, const char *path)
282 {
283 const char *p;
284
285 if (base->host && base->path[0] == '\0') {
286 return ne_concat("/", path, NULL);
287 }
288
289 p = strrchr(base->path, '/');
290 if (p == NULL) {
291 return ne_strdup(path);
292 } else {
293 size_t len = p - base->path + 1;
294 char *ret = ne_malloc(strlen(path) + len + 1);
295
296 memcpy(ret, base->path, len);
297 memcpy(ret + len, path, strlen(path) + 1);
298 return ret;
299 }
300 }
301
302 /* This function directly implements the "Remove Dot Segments"
303 * algorithm described in RFC 3986 section 5.2.4. */
304 static char *remove_dot_segments(const char *path)
305 {
306 char *in, *inc, *out;
307
308 inc = in = ne_strdup(path);
309 out = ne_malloc(strlen(path) + 1);
310 out[0] = '\0';
311
312 while (in[0]) {
313 /* case 2.A: */
314 if (strncmp(in, "./", 2) == 0) {
315 in += 2;
316 }
317 else if (strncmp(in, "../", 3) == 0) {
318 in += 3;
319 }
320
321 /* case 2.B: */
322 else if (strncmp(in, "/./", 3) == 0) {
323 in += 2;
324 }
325 else if (strcmp(in, "/.") == 0) {
326 in[1] = '\0';
327 }
328
329 /* case 2.C: */
330 else if (strncmp(in, "/../", 4) == 0 || strcmp(in, "/..") == 0) {
331 char *p;
332
333 /* Make the next character in the input buffer a "/": */
334 if (in[3] == '\0') {
335 /* terminating "/.." case */
336 in += 2;
337 in[0] = '/';
338 } else {
339 /* "/../" prefix case */
340 in += 3;
341 }
342
343 /* Trim the last component from the output buffer, or
344 * empty it. */
345 p = strrchr(out, '/');
346 if (p) {
347 *p = '\0';
348 } else {
349 out[0] = '\0';
350 }
351 }
352
353 /* case 2.D: */
354 else if (strcmp(in, ".") == 0 || strcmp(in, "..") == 0) {
355 in[0] = '\0';
356 }
357
358 /* case 2.E */
359 else {
360 char *p;
361
362 /* Search for the *second* "/" if the leading character is
363 * already "/": */
364 p = strchr(in + (in[0] == '/'), '/');
365 /* Otherwise, copy the whole string */
366 if (p == NULL) p = strchr(in, '\0');
367
368 strncat(out, in, p - in);
369 in = p;
370 }
371 }
372
373 ne_free(inc);
374
375 return out;
376 }
377
378 /* Copy authority components from 'src' to 'dest' if defined. */
379 static void copy_authority(ne_uri *dest, const ne_uri *src)
380 {
381 if (src->host) dest->host = ne_strdup(src->host);
382 dest->port = src->port;
383 if (src->userinfo) dest->userinfo = ne_strdup(src->userinfo);
384 }
385
386 /* This function directly implements the "Transform References"
387 * algorithm described in RFC 3986 section 5.2.2. */
388 ne_uri *ne_uri_resolve(const ne_uri *base, const ne_uri *relative,
389 ne_uri *target)
390 {
391 memset(target, 0, sizeof *target);
392
393 if (relative->scheme) {
394 target->scheme = ne_strdup(relative->scheme);
395 copy_authority(target, relative);
396 target->path = remove_dot_segments(relative->path);
397 if (relative->query) target->query = ne_strdup(relative->query);
398 } else {
399 if (relative->host) {
400 copy_authority(target, relative);
401 target->path = remove_dot_segments(relative->path);
402 if (relative->query) target->query = ne_strdup(relative->query);
403 } else {
404 if (relative->path[0] == '\0') {
405 target->path = ne_strdup(base->path);
406 if (relative->query) {
407 target->query = ne_strdup(relative->query);
408 } else if (base->query) {
409 target->query = ne_strdup(base->query);
410 }
411 } else {
412 if (relative->path[0] == '/') {
413 target->path = remove_dot_segments(relative->path);
414 } else {
415 char *merged = merge_paths(base, relative->path);
416 target->path = remove_dot_segments(merged);
417 ne_free(merged);
418 }
419 if (relative->query) target->query = ne_strdup(relative->query);
420 }
421 copy_authority(target, base);
422 }
423 if (base->scheme) target->scheme = ne_strdup(base->scheme);
424 }
425
426 if (relative->fragment) target->fragment = ne_strdup(relative->fragment);
427
428 return target;
429 }
430
431 ne_uri *ne_uri_copy(ne_uri *dest, const ne_uri *src)
432 {
433 memset(dest, 0, sizeof *dest);
434
435 if (src->scheme) dest->scheme = ne_strdup(src->scheme);
436 copy_authority(dest, src);
437 if (src->path) dest->path = ne_strdup(src->path);
438 if (src->query) dest->query = ne_strdup(src->query);
439 if (src->fragment) dest->fragment = ne_strdup(src->fragment);
440
441 return dest;
442 }
443
444 void ne_uri_free(ne_uri *u)
445 {
446 if (u->host) ne_free(u->host);
447 if (u->path) ne_free(u->path);
448 if (u->scheme) ne_free(u->scheme);
449 if (u->userinfo) ne_free(u->userinfo);
450 if (u->fragment) ne_free(u->fragment);
451 if (u->query) ne_free(u->query);
452 memset(u, 0, sizeof *u);
453 }
454
455 char *ne_path_unescape(const char *uri)
456 {
457 const char *pnt;
458 char *ret, *retpos, buf[5] = { "0x00" };
459 retpos = ret = ne_malloc(strlen(uri) + 1);
460 for (pnt = uri; *pnt != '\0'; pnt++) {
461 if (*pnt == '%') {
462 if (!isxdigit((unsigned char) pnt[1]) ||
463 !isxdigit((unsigned char) pnt[2])) {
464 /* Invalid URI */
465 ne_free(ret);
466 return NULL;
467 }
468 buf[2] = *++pnt; buf[3] = *++pnt; /* bit faster than memcpy */
469 *retpos++ = (char)strtol(buf, NULL, 16);
470 } else {
471 *retpos++ = *pnt;
472 }
473 }
474 *retpos = '\0';
475 return ret;
476 }
477
478 /* CH must be an unsigned char; evaluates to 1 if CH should be
479 * percent-encoded. */
480 #define path_escape_ch(ch) (uri_lookup(ch) & URI_ESCAPE)
481
482 char *ne_path_escape(const char *path)
483 {
484 const unsigned char *pnt;
485 char *ret, *p;
486 size_t count = 0;
487
488 for (pnt = (const unsigned char *)path; *pnt != '\0'; pnt++) {
489 count += path_escape_ch(*pnt);
490 }
491
492 if (count == 0) {
493 return ne_strdup(path);
494 }
495
496 p = ret = ne_malloc(strlen(path) + 2 * count + 1);
497 for (pnt = (const unsigned char *)path; *pnt != '\0'; pnt++) {
498 if (path_escape_ch(*pnt)) {
499 /* Escape it - %<hex><hex> */
500 sprintf(p, "%%%02x", (unsigned char) *pnt);
501 p += 3;
502 } else {
503 *p++ = *pnt;
504 }
505 }
506 *p = '\0';
507 return ret;
508 }
509
510 #undef path_escape_ch
511
512 #define CMPWITH(field, func) \
513 do { \
514 if (u1->field) { \
515 if (!u2->field) return -1; \
516 n = func(u1->field, u2->field); \
517 if (n) return n; \
518 } else if (u2->field) { \
519 return 1; \
520 } \
521 } while (0)
522
523 #define CMP(field) CMPWITH(field, strcmp)
524 #define CASECMP(field) CMPWITH(field, ne_strcasecmp)
525
526 /* As specified by RFC 2616, section 3.2.3. */
527 int ne_uri_cmp(const ne_uri *u1, const ne_uri *u2)
528 {
529 int n;
530
531 CMP(path);
532 CASECMP(host);
533 CASECMP(scheme);
534 CMP(query);
535 CMP(fragment);
536 CMP(userinfo);
537
538 return u2->port - u1->port;
539 }
540
541 #undef CMP
542 #undef CASECMP
543 #undef CMPWITH
544
545 /* TODO: implement properly */
546 int ne_path_compare(const char *a, const char *b)
547 {
548 int ret = ne_strcasecmp(a, b);
549 if (ret) {
550 /* This logic says: "If the lengths of the two URIs differ by
551 * exactly one, and the LONGER of the two URIs has a trailing
552 * slash and the SHORTER one DOESN'T, then..." */
553 int traila = ne_path_has_trailing_slash(a),
554 trailb = ne_path_has_trailing_slash(b),
555 lena = strlen(a), lenb = strlen(b);
556 if (traila != trailb && abs(lena - lenb) == 1 &&
557 ((traila && lena > lenb) || (trailb && lenb > lena))) {
558 /* Compare them, ignoring the trailing slash on the longer
559 * URI */
560 if (strncasecmp(a, b, lena < lenb ? lena : lenb) == 0)
561 ret = 0;
562 }
563 }
564 return ret;
565 }
566
567 char *ne_uri_unparse(const ne_uri *uri)
568 {
569 ne_buffer *buf = ne_buffer_create();
570
571 if (uri->scheme) {
572 ne_buffer_concat(buf, uri->scheme, ":", NULL);
573 }
574
575 if (uri->host) {
576 ne_buffer_czappend(buf, "//");
577 if (uri->userinfo) {
578 ne_buffer_concat(buf, uri->userinfo, "@", NULL);
579 }
580 ne_buffer_zappend(buf, uri->host);
581
582 if (uri->port > 0
583 && (!uri->scheme
584 || ne_uri_defaultport(uri->scheme) != uri->port)) {
585 char str[20];
586 ne_snprintf(str, 20, ":%d", uri->port);
587 ne_buffer_zappend(buf, str);
588 }
589 }
590
591 ne_buffer_zappend(buf, uri->path);
592
593 if (uri->query) {
594 ne_buffer_concat(buf, "?", uri->query, NULL);
595 }
596
597 if (uri->fragment) {
598 ne_buffer_concat(buf, "#", uri->fragment, NULL);
599 }
600
601 return ne_buffer_finish(buf);
602 }
603
604 /* Give it a path segment, it returns non-zero if child is
605 * a child of parent. */
606 int ne_path_childof(const char *parent, const char *child)
607 {
608 char *root = ne_strdup(child);
609 int ret;
610 if (strlen(parent) >= strlen(child)) {
611 ret = 0;
612 } else {
613 /* root is the first of child, equal to length of parent */
614 root[strlen(parent)] = '\0';
615 ret = (ne_path_compare(parent, root) == 0);
616 }
617 ne_free(root);
618 return ret;
619 }