1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
|
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* raptor_rfc2396.c - Raptor URI resolving
*
* $Id: raptor_rfc2396.c,v 1.8 2004/12/08 15:56:59 cmdjb Exp $
*
* Copyright (C) 2004-2004, David Beckett http://purl.org/net/dajobe/
* Institute for Learning and Research Technology http://www.ilrt.bristol.ac.uk/
* University of Bristol, UK http://www.bristol.ac.uk/
*
* This package is Free Software and part of Redland http://librdf.org/
*
* It is licensed under the following three licenses as alternatives:
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
* 2. GNU General Public License (GPL) V2 or any newer version
* 3. Apache License, V2.0 or any newer version
*
* You may not use this file except in compliance with at least one of
* the above three licenses.
*
* See LICENSE.html or LICENSE.txt at the top of this package for the
* complete terms and further detail along with the license texts for
* the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <raptor_config.h>
#endif
#ifdef WIN32
#include <win32_raptor_config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
/* Raptor includes */
#include "raptor.h"
#include "raptor_internal.h"
#ifndef STANDALONE
/**
* raptor_new_uri_detail - Create a URI detailed structure from a URI string
* @uri_string: The URI string to split
*
**/
raptor_uri_detail*
raptor_new_uri_detail(const unsigned char *uri_string)
{
const unsigned char *s = NULL;
unsigned char *b = NULL;
raptor_uri_detail *ud;
size_t uri_len;
if(!uri_string)
return NULL;
uri_len=strlen((const char*)uri_string);
/* The extra +5 is for the 5 \0s that may be added for each component
* even if the entire URI is empty
*/
ud=(raptor_uri_detail*)RAPTOR_CALLOC(raptor_uri_detail,
sizeof(raptor_uri_detail)+uri_len+5+1,
1);
ud->uri_len=uri_len;
ud->buffer=(unsigned char*)((unsigned char*)ud + sizeof(raptor_uri_detail));
s=uri_string;
b=ud->buffer;
/* Split the URI into it's syntactic components */
/*
* scheme is checked in more detail since it is important
* to recognise absolute URIs for resolving, and it is easy to do.
*
* scheme = alpha *( alpha | digit | "+" | "-" | "." )
* RFC 2396 section 3.1 Scheme Component
*/
if(*s && isalpha((int)*s)) {
s++;
while(*s && (isalnum((int)*s) ||
(*s == '+') || (*s == '-') || (*s == '.')))
s++;
if(*s == ':') {
/* it matches the URI scheme grammar, so store this as a scheme */
ud->scheme=b;
ud->scheme_len=s-uri_string;
while(*uri_string != ':')
*b++ = *uri_string++;
*b++ = '\0';
/* and move past the : */
s++;
} else
s=uri_string;
}
/* authority */
if(*s && s[1] && *s == '/' && s[1] == '/') {
ud->authority=b;
s+=2; /* skip "//" */
while(*s && *s != '/' && *s != '?' && *s != '#')
*b++ = *s++;
ud->authority_len=b-ud->authority;
*b++ = '\0';
}
/* path */
if(*s && *s != '?' && *s != '#') {
ud->path=b;
while(*s && *s != '?' && *s != '#')
*b++ = *s++;
ud->path_len=b-ud->path;
*b++ = '\0';
}
/* query */
if(*s && *s == '?') {
ud->query=b;
s++;
while(*s && *s != '#')
*b++ = *s++;
ud->query_len=b-ud->query;
*b++ = '\0';
}
/* fragment identifier - RFC2396 Section 4.1 */
if(*s && *s == '#') {
ud->fragment=b;
s++;
while(*s)
*b++ = *s++;
ud->fragment_len=b-ud->fragment;
*b='\0';
}
return ud;
}
void
raptor_free_uri_detail(raptor_uri_detail* uri_detail)
{
/* Also frees the uri_detail->buffer allocated in raptor_uri_parse() */
RAPTOR_FREE(raptor_uri_detail, uri_detail);
}
unsigned char*
raptor_uri_detail_to_string(raptor_uri_detail *ud, size_t* len_p)
{
size_t len=0;
unsigned char *buffer, *p;
if(ud->scheme)
len+= ud->scheme_len+1; /* : */
if(ud->authority)
len+= 2 + ud->authority_len; /* // */
if(ud->path)
len+= ud->path_len;
if(ud->fragment)
len+= 1 + ud->fragment_len; /* # */
if(ud->query)
len+= 1 + ud->query_len; /* ? */
if(len_p)
*len_p=len;
buffer=(unsigned char*)RAPTOR_MALLOC(cstring, len+1);
if(!buffer)
return NULL;
p=buffer;
if(ud->scheme) {
unsigned char *src=ud->scheme;
while(*src)
*p++ = *src++;
*p++ = ':';
}
if(ud->authority) {
unsigned char *src=ud->authority;
*p++ = '/';
*p++ = '/';
while(*src)
*p++ = *src++;
}
if(ud->path) {
unsigned char *src=ud->path;
while(*src)
*p++ = *src++;
}
if(ud->fragment) {
unsigned char *src=ud->fragment;
*p++ = '#';
while(*src)
*p++ = *src++;
}
if(ud->query) {
unsigned char *src=ud->query;
*p++ = '?';
while(*src)
*p++ = *src++;
}
*p='\0';
return buffer;
}
/**
* raptor_uri_resolve_uri_reference - Resolve a URI to a base URI
* @base_uri: Base URI string
* @reference_uri: Reference URI string
* @buffer: Destination buffer URI
* @length: Length of destination buffer
*
**/
void
raptor_uri_resolve_uri_reference(const unsigned char *base_uri,
const unsigned char *reference_uri,
unsigned char *buffer, size_t length)
{
raptor_uri_detail *ref=NULL;
raptor_uri_detail *base=NULL;
raptor_uri_detail result; /* static - pointers go to inside ref or base */
unsigned char *path_buffer=NULL;
unsigned char *p, *cur, *prev, *s;
unsigned char last_char;
#if RAPTOR_DEBUG > 2
RAPTOR_DEBUG4("base uri='%s', reference_uri='%s, buffer size %d\n",
(base_uri ? (const char*)base_uri : "NULL"),
(reference_uri ? (const char*)reference_uri : "NULL"),
(int)length);
#endif
*buffer = '\0';
memset(&result, 0, sizeof(result));
ref=raptor_new_uri_detail(reference_uri);
if(!ref)
goto resolve_tidy;
/* is reference URI "" or "#frag"? */
if(!ref->scheme && !ref->authority && !ref->path && !ref->query) {
unsigned char c;
/* Copy base URI to result up to '\0' or '#' */
for(p=buffer; (c= *base_uri) && c != '#'; p++, base_uri++)
*p = c;
*p='\0';
if(ref->fragment) {
unsigned char *src=ref->fragment;
/* Append any fragment */
*p++ = '#';
while(*src)
*p++ = *src++;
*p='\0';
}
goto resolve_tidy;
}
/* reference has a scheme - is an absolute URI */
if(ref->scheme) {
strncpy((char*)buffer, (const char*)reference_uri, ref->uri_len+1);
goto resolve_tidy;
}
/* now the reference URI must be schemeless, i.e. relative */
base=raptor_new_uri_detail(base_uri);
if(!base)
goto resolve_tidy;
/* result URI must be of the base URI scheme */
result.scheme = base->scheme;
result.scheme_len = base->scheme_len;
/* an authority is given ( [user:pass@]hostname[:port] for http)
* so the reference URI is like //authority
*/
if(ref->authority) {
result.authority = ref->authority;
result.authority_len = ref->authority_len;
result.path = ref->path;
result.path_len = ref->path_len;
goto resolve_end;
}
/* no - so now we have path (maybe with query, fragment) relative to base */
result.authority = base->authority;
result.authority_len = base->authority_len;
if(ref->path && *ref->path == '/') {
/* reference path is absolute, just copy the reference path */
result.path = ref->path;
result.path_len = ref->path_len;
goto resolve_end;
}
/* need to resolve relative path */
/* Build the result path in path_buffer */
result.path_len=0;
if(base->path)
result.path_len += base->path_len;
else {
/* Add a missing path - makes the base URI 1 character longer */
base->path=(unsigned char*)"/"; /* static, but copied and not free()d */
base->path_len=1;
base->uri_len++;
result.path_len++;
}
if(ref->path)
result.path_len += ref->path_len;
/* the resulting path can be no longer than result.path_len */
path_buffer=(unsigned char*)RAPTOR_MALLOC(cstring, result.path_len+1);
if(!path_buffer)
goto resolve_tidy;
result.path = path_buffer;
*path_buffer = '\0';
for(p=base->path + base->path_len-1; p > base->path && *p != '/'; p--)
;
if(p >= base->path) {
result.path_len=p-base->path+1;
/* Found a /, copy everything before that to path_buffer */
strncpy((char*)path_buffer, (char*)base->path, result.path_len);
path_buffer[result.path_len]='\0';
}
if(ref->path) {
strncpy((char*)path_buffer+result.path_len, (const char*)ref->path, ref->path_len+1);
result.path_len += ref->path_len;
path_buffer[result.path_len]='\0';
}
/* remove all "./" path components */
for(p=(prev=path_buffer); *p; p++) {
if(*p != '/')
continue;
if(p == (prev+1) && *prev == '.') {
unsigned char *dest=prev;
p++;
while(*dest)
*dest++ = *p++;
*dest= '\0';
p=prev;
result.path_len -=2;
} else {
prev=p+1;
}
}
if(p == (prev+1) && *prev == '.') {
/* Remove "." at the end of a path */
*prev = '\0';
result.path_len--;
}
#if defined(RAPTOR_DEBUG)
if(result.path_len != strlen((const char*)path_buffer))
RAPTOR_FATAL3("Path length %ld does not match calculated %ld.", (long)strlen((const char*)path_buffer), (long)result.path_len);
#endif
/* Remove all "<component>/../" path components */
/*
* The pointers:
* <component>/../<next>
* prev-^ cur-^
* and p points to the previous prev (can be NULL)
*/
prev=NULL;
cur=NULL;
p=NULL;
last_char='\0';
for(s=path_buffer; *s; last_char=*s++) {
/* find the path components */
if(*s != '/') {
/* If it is the start or following a /, record a new path component */
if(!last_char || last_char == '/') {
/* Store 2 path components */
if(!prev)
prev=s;
else if(!cur)
cur=s;
}
continue;
}
/* Wait till there are two path components */
if(!prev || !cur)
continue;
#if defined(RAPTOR_DEBUG)
if(result.path_len != strlen((const char*)path_buffer))
RAPTOR_FATAL3("Path length %ld does not match calculated %ld.", (long)strlen((const char*)path_buffer), (long)result.path_len);
#endif
/* If the current one is '..' */
if(s == (cur+2) && cur[0] == '.' && cur[1] == '.') {
/* and if the previous one isn't '..'
* (which means it is beyond the root such as a path "/foo/../..")
*/
if(cur != (prev+3) || prev[0] != '.' || prev[1] != '.') {
unsigned char *dest=prev;
/* remove the <component>/../<next>
* prev-^ cur-^ ^-s
*/
size_t len=s-prev+1; /* length of path component we are removing */
s++;
while(*s)
*dest++ = *s++;
*dest = '\0';
result.path_len -= len;
if(p && p < prev) {
/* We know the previous prev path component and we didn't do
* two adjustments in a row, so can adjust the
* pointers to continue the newly shortened path:
* s to the / before <next> (autoincremented by the loop)
* prev to the previous prev path component
* cur to NULL. Will be set by the next loop iteration since s
* points to a '/', last_char will be set to *s. */
s=prev-1;
prev=p;
cur=NULL;
p=NULL;
} else {
/* Otherwise must start from the beginning again */
prev=NULL;
cur=NULL;
p=NULL;
last_char='\0';
s=path_buffer;
}
}
} else {
/* otherwise this is not a special path component so
* shift the path components stack
*/
p=prev;
prev=cur;
cur=NULL;
}
}
if(prev && s == (cur+2) && cur[0] == '.' && cur[1] == '.') {
/* Remove <component>/.. at the end of the path */
*prev = '\0';
result.path_len -= (s-prev);
}
#if defined(RAPTOR_DEBUG)
if(result.path_len != strlen((const char*)path_buffer))
RAPTOR_FATAL3("Path length %ld does not match calculated %ld.", (long)strlen((const char*)path_buffer), (long)result.path_len);
#endif
resolve_end:
if(ref->query) {
result.query=ref->query;
result.query_len=ref->query_len;
}
if(ref->fragment) {
result.fragment=ref->fragment;
result.fragment_len=ref->fragment_len;
}
p=buffer;
if(result.scheme) {
strncpy((char*)p, (const char*)result.scheme, result.scheme_len);
p += result.scheme_len;
*p++ = ':';
}
if(result.authority) {
*p++ = '/';
*p++ = '/';
strncpy((char*)p, (const char*)result.authority, result.authority_len);
p+= result.authority_len;
}
if(result.path) {
strncpy((char*)p, (const char*)result.path, result.path_len);
p+= result.path_len;
}
if(result.query) {
*p++ = '?';
strncpy((char*)p, (const char*)result.query, result.query_len);
p+= result.query_len;
}
if(result.fragment) {
*p++ = '#';
strncpy((char*)p, (const char*)result.fragment, result.fragment_len);
p+= result.fragment_len;
}
*p = '\0';
resolve_tidy:
if(path_buffer)
RAPTOR_FREE(cstring, path_buffer);
if(base)
raptor_free_uri_detail(base);
if(ref)
raptor_free_uri_detail(ref);
}
#endif
#ifdef STANDALONE
#include <stdio.h>
/* one more prototype */
int main(int argc, char *argv[]);
static const char *program;
static int
check_resolve(const char *base_uri, const char *reference_uri,
const char *result_uri)
{
unsigned char buffer[1024];
raptor_uri_resolve_uri_reference((const unsigned char*)base_uri,
(const unsigned char*)reference_uri,
buffer, sizeof (buffer));
if(strcmp((const char*)buffer, result_uri)) {
fprintf(stderr,
"%s: raptor_uri_resolve_uri_reference(%s, %s) FAILED giving '%s' != '%s'\n",
program, base_uri, reference_uri,
buffer, result_uri);
return 1;
}
#if RAPTOR_DEBUG > 2
fprintf(stderr,
"%s: raptor_uri_resolve_uri_reference(%s, %s) OK giving '%s'\n",
program, base_uri, reference_uri,
buffer);
#endif
return 0;
}
static int
check_parses(const char *uri_string) {
raptor_uri_detail* ud;
ud=raptor_new_uri_detail((unsigned const char*)uri_string);
if(!ud) {
fprintf(stderr, "%s: raptor_new_uri_detail(%s) FAILED to parse\n",
program, uri_string);
return 1;
}
#if RAPTOR_DEBUG > 2
fprintf(stderr, "%s: raptor_new_uri_detail(%s) OK\n",
program, uri_string);
#endif
raptor_free_uri_detail(ud);
return 0;
}
int
main(int argc, char *argv[])
{
const char *base_uri="http://example.org/bpath/cpath/d;p?querystr#frag";
int failures=0;
program=raptor_basename(argv[0]);
fprintf(stderr, "%s: Using base URI '%s'\n", program, base_uri);
/* Tests from RFC2396 Appendix C
*
* Modifications:
* - add 'path' when items are path components to make easier to read
* - use example.org instead of 'a' for the authority
* - results are against the base_uri above
*/
/* Appendix C.1 Normal Examples */
failures += check_resolve(base_uri, "g:h", "g:h");
failures += check_resolve(base_uri, "gpath", "http://example.org/bpath/cpath/gpath");
failures += check_resolve(base_uri, "./gpath", "http://example.org/bpath/cpath/gpath");
failures += check_resolve(base_uri, "gpath/", "http://example.org/bpath/cpath/gpath/");
failures += check_resolve(base_uri, "/gpath", "http://example.org/gpath");
failures += check_resolve(base_uri, "//gpath", "http://gpath");
failures += check_resolve(base_uri, "?y", "http://example.org/bpath/cpath/?y");
failures += check_resolve(base_uri, "gpath?y", "http://example.org/bpath/cpath/gpath?y");
failures += check_resolve(base_uri, "#s", "http://example.org/bpath/cpath/d;p?querystr#s");
failures += check_resolve(base_uri, "gpath#s", "http://example.org/bpath/cpath/gpath#s");
failures += check_resolve(base_uri, "gpath?y#s", "http://example.org/bpath/cpath/gpath?y#s");
failures += check_resolve(base_uri, ";x", "http://example.org/bpath/cpath/;x");
failures += check_resolve(base_uri, "gpath;x", "http://example.org/bpath/cpath/gpath;x");
failures += check_resolve(base_uri, "gpath;x?y#s", "http://example.org/bpath/cpath/gpath;x?y#s");
failures += check_resolve(base_uri, ".", "http://example.org/bpath/cpath/");
failures += check_resolve(base_uri, "./", "http://example.org/bpath/cpath/");
failures += check_resolve(base_uri, "..", "http://example.org/bpath/");
failures += check_resolve(base_uri, "../", "http://example.org/bpath/");
failures += check_resolve(base_uri, "../gpath", "http://example.org/bpath/gpath");
failures += check_resolve(base_uri, "../..", "http://example.org/");
failures += check_resolve(base_uri, "../../", "http://example.org/");
failures += check_resolve(base_uri, "../../gpath", "http://example.org/gpath");
/* Appendix C.2 Abnormal Examples */
failures += check_resolve(base_uri, "", "http://example.org/bpath/cpath/d;p?querystr");
failures += check_resolve(base_uri, "../../../gpath", "http://example.org/../gpath");
failures += check_resolve(base_uri, "../../../../gpath", "http://example.org/../../gpath");
failures += check_resolve(base_uri, "/./gpath", "http://example.org/./gpath");
failures += check_resolve(base_uri, "/../gpath", "http://example.org/../gpath");
failures += check_resolve(base_uri, "gpath.", "http://example.org/bpath/cpath/gpath.");
failures += check_resolve(base_uri, ".gpath", "http://example.org/bpath/cpath/.gpath");
failures += check_resolve(base_uri, "gpath..", "http://example.org/bpath/cpath/gpath..");
failures += check_resolve(base_uri, "..gpath", "http://example.org/bpath/cpath/..gpath");
failures += check_resolve(base_uri, "./../gpath", "http://example.org/bpath/gpath");
failures += check_resolve(base_uri, "./gpath/.", "http://example.org/bpath/cpath/gpath/");
failures += check_resolve(base_uri, "gpath/./hpath", "http://example.org/bpath/cpath/gpath/hpath");
failures += check_resolve(base_uri, "gpath/../hpath", "http://example.org/bpath/cpath/hpath");
failures += check_resolve(base_uri, "gpath;x=1/./y", "http://example.org/bpath/cpath/gpath;x=1/y");
failures += check_resolve(base_uri, "gpath;x=1/../y", "http://example.org/bpath/cpath/y");
failures += check_resolve(base_uri, "gpath?y/./x", "http://example.org/bpath/cpath/gpath?y/./x");
failures += check_resolve(base_uri, "gpath?y/../x", "http://example.org/bpath/cpath/gpath?y/../x");
failures += check_resolve(base_uri, "gpath#s/./x", "http://example.org/bpath/cpath/gpath#s/./x");
failures += check_resolve(base_uri, "gpath#s/../x", "http://example.org/bpath/cpath/gpath#s/../x");
failures += check_resolve(base_uri, "http:gauthority", "http:gauthority");
/* Examples from 1.3 */
failures += check_parses("ftp://ftp.is.co.za/rfc/rfc1808.txt");
failures += check_parses("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles");
failures += check_parses("http://www.math.uio.no/faq/compression-faq/part1.html");
failures += check_parses("mailto:mduerst@ifi.unizh.ch");
failures += check_parses("news:comp.infosystems.www.servers.unix");
failures += check_parses("telnet://melvyl.ucop.edu/");
failures += check_parses("");
/* This is a not-crashing test */
raptor_new_uri_detail(NULL);
/* Extra checks not in RFC2396 */
/* RDF xml:base check that fragments and query strings are removed */
failures += check_resolve(base_uri, "gpath/../../../hpath", "http://example.org/hpath");
/* RDF xml:base check that extra ../ are not lost */
failures += check_resolve("http://example.org/dir/file", "../../../absfile", "http://example.org/../../absfile");
/* RDF xml:base check that an absolute URI replaces */
failures += check_resolve("http://example.org/dir/file", "http://another.example.org/dir2/file2", "http://another.example.org/dir2/file2");
/* base URI and relative URI with no absolute path works */
failures += check_resolve("foo:", "not_scheme:blah", "foo:/not_scheme:blah");
return failures;
}
#endif
|