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 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
|
#ifdef FOR_LT
#include "lt-defs.h"
#include "lt-memory.h"
#include "lt-errmsg.h"
#include "lt-comment.h"
#include "lt-safe.h"
#include "nsl-err.h"
#define Strerror() strErr()
#define Malloc salloc
#define Realloc srealloc
#define Free sfree
#define fopen stdsfopen
#else
#include "system.h"
#define LT_ERROR(err, format) fprintf(stderr, format)
#define LT_ERROR1(err, format, arg) fprintf(stderr, format, arg)
#define LT_ERROR2(err, format, arg1, arg2) fprintf(stderr, format, arg1, arg2)
#define LT_ERROR3(err, format, arg1, arg2, arg3) fprintf(stderr, format, arg1, arg2, arg3)
#define LT_ERROR4(err, format, arg1, arg2, arg3, arg4) fprintf(stderr, format, arg1, arg2, arg3, arg4)
#define WARN(err, format) fprintf(stderr, format)
#define WARN1(err, format, arg) fprintf(stderr, format, arg)
#define Strerror() strerror(errno)
#ifdef MAXPATHLEN
#define CWDBS MAXPATHLEN+1
#else
#define CWDBS 1025
#endif
#define GETWD(buf) getcwd(buf,CWDBS)
#endif /* FOR_LT */
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <string.h> /* that's where strerror is. really. */
#include <sys/types.h>
#if defined(__GNUC__) && defined(__STRICT_ANSI__)
extern FILE *fdopen(int, const char *);
#endif
#ifdef WIN32
#include <direct.h>
#endif
#ifdef SOCKETS_IMPLEMENTED
#ifdef WIN32
#undef boolean
#include <winsock.h>
#include <fcntl.h>
#else
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#endif
#include "string16.h"
#include "stdio16.h"
#include "url.h"
#ifdef HAVE_LIBZ
#include "zlib.h"
#ifdef macintosh
#include <fcntl.h>
#include <unix.h>
#endif
#endif
static FILE16 *http_open(const char *url,
const char *host, int port, const char *path,
const char *type);
static FILE16 *file_open(const char *url,
const char *host, int port, const char *path,
const char *type);
static void parse_url(const char *url,
char **scheme, char **host, int *port, char **path);
/* Mapping of scheme names to opening functions */
struct {
char *scheme;
FILE16 *(*open)(const char *, const char *, int, const char *, const char *);
} schemes[] = {
{(char *)"http", http_open},
{(char *)"file", file_open},
};
#define NSCHEME (sizeof(schemes) / sizeof(schemes[0]))
static int proxy_port;
static char *proxy_host = 0;
int init_url(void)
{
char *p;
#ifdef WIN32
/* random magic for MS Windows */
WORD version = MAKEWORD(1, 1);
WSADATA wsaData;
int err = WSAStartup(version, &wsaData);
if (err)
{
LT_ERROR(LEFILE, "Error: can't init HTTP interface\n");
return -1;
}
else if(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
LT_ERROR(LEFILE, "Error: wrong version of WINSOCK\n");
WSACleanup();
return -1;
}
#endif
/* See if we are supposed to use a proxy */
if((p = getenv("http_proxy")))
{
char *colon, *slash;
if(strncmp(p, "http://", 7) == 0)
p += 7;
proxy_host = strdup8(p);
if((slash = strchr(proxy_host, '/')))
*slash = 0;
if((colon = strchr(proxy_host, ':')))
{
proxy_port = atoi(colon+1);
*colon = 0;
}
else
proxy_port = 80;
}
return 0;
}
void deinit_url(void)
{
#ifdef WIN32
/* No doubt there's some Windozy nonsense that should go here,
but I have no idea what it is. */
#endif
if(proxy_host)
Free(proxy_host);
}
/* Construct a default base URL, essentially file://`pwd`/ */
char *default_base_url(void)
{
char buf[CWDBS];
char *url;
int len;
if(!GETWD(buf))
{
WARN(LEFILE, "Warning: can't get current directory for default base url\n");
return strdup8("file:///");
}
len = strlen(buf);
#ifdef WIN32
/* DOS: translate C:\a\b to file:///C:/a/b/ */
/* XXX should we escape anything? */
{
char *p;
for(p=buf; *p; p++)
if(*p == '\\')
*p = '/';
if(buf[len-1] == '/')
/* happens for C:\ etc */
buf[--len] = 0;
}
url = Malloc(8 + len + 2);
sprintf(url, "file:///%s/", buf);
#else
#ifdef mac_filenames
/* Mac: translate a:b to file:///a/b/ */
/* XXX should escape spaces and slashes, at least */
{
char *p;
for(p=buf; *p; p++)
if(*p == ':')
*p = '/';
if(buf[len-1] == '/')
/* often (always?) has trailing separator */
buf[--len] = 0;
}
url = Malloc(8 + len + 2);
sprintf(url, "file:///%s/", buf);
#else
#ifdef CYGNUS32
/* Cygnus: translate //c/a/b to file:///c:/a/b */
if(buf[len-1] == '/')
/* should only happen for root directory */
buf[--len] = 0;
url = Malloc(8 + len + 2);
if(buf[0] == '/' && buf[1] == '/')
sprintf(url, "file:///%c:%s/", buf[2], buf+3);
else
sprintf(url, "file://%s/", buf);
#else
/* Unix: translate /a/b to file:///a/b/ */
if(buf[len-1] == '/')
/* should only happen for root directory */
buf[--len] = 0;
url = Malloc(7 + len + 2);
sprintf(url, "file://%s/", buf);
#endif
#endif
#endif
return url;
}
/*
* Merge a URL with a base URL if necessary.
* The merged URL is returned.
* The parts of the URL are returned in scheme, host, port and path
* if these are non-null.
* Caller should free the results.
*/
char *url_merge(const char *url, const char *base,
char **_scheme, char **_host, int *_port, char **_path)
{
char *merged_scheme, *merged_host, *merged_path, *merged_url;
char *scheme=0, *host=0, *path=0;
char *base_scheme=0, *base_host=0, *base_path=0;
char *default_base=0;
int port, base_port, merged_port, i, j;
char *p;
/* First see if we have an absolute URL */
parse_url(url, &scheme, &host, &port, &path);
if(scheme && (host || *path == '/'))
{
merged_scheme = scheme;
merged_host = host;
merged_port = port;
merged_path = path;
merged_url = strdup8(url);
goto ok;
}
/* Relative URL, so we need the base URL */
if(!base)
base = default_base = default_base_url();
parse_url(base, &base_scheme, &base_host, &base_port, &base_path);
if(base_scheme && (base_host || *base_path == '/'))
;
else
{
LT_ERROR1(LEFILE, "Error: bad base URL <%s>\n", base);
goto bad;
}
/* Determine merged path */
if(path[0] == '/')
{
/* not relative, use as-is */
merged_path = path;
path = 0;
}
else
{
/* relative, append to base path */
merged_path = Malloc(strlen(base_path) + strlen(path) + 1);
strcpy(merged_path, base_path);
/* strip last component of base */
for(i=strlen(merged_path)-1; i>=0 && merged_path[i] != '/'; i--)
merged_path[i] = '\0';
/* append relative path */
strcat(merged_path, path);
/* Remove . and .. components from path */
p = merged_path;
for(i=0; p[i]; )
{
assert(p[i] == '/');
/* find next segment */
for(j=i+1; p[j] && p[j] != '/'; j++)
;
/* Do we have "." ? */
if(j - i == 2 && p[i+1] == '.')
{
strcpy(&p[i+1], p[j] ? &p[j+1] : &p[j]);
continue;
}
/* Do we have "<segment>/.." with <segment> != ".." ? */
/* (We know we're not looking at "./" so we don't have to
* worry about "./..")
*/
if(p[j] == '/' && p[j+1] == '.' && p[j+2] == '.' &&
(p[j+3] == '/' || p[j+3] == '\0') &&
(j - i != 3 || p[i+1] != '.' || p[i+2] != '.'))
{
strcpy(&p[i+1], p[j+3] ? &p[j+4] : &p[j+3]);
i = 0; /* start again from beginning */
continue;
}
/* move to next segment */
i = j;
}
}
/* Check for deviant relative URLs like file:foo */
if(scheme && !host && *path != '/')
{
if(strcmp(scheme, base_scheme) == 0)
{
WARN1(LEFILE,
"Warning: relative URL <%s> contains scheme, contrary to RFC 1808\n",
url);
}
else
{
LT_ERROR2(LEFILE,
"Error: relative URL <%s> has scheme different from base <%s>\n",
url, base);
goto bad;
}
}
/* Return the parts and the whole thing */
merged_scheme = base_scheme; if(scheme) Free(scheme);
if(host)
{
merged_host = host; Free(base_host);
merged_port = port;
}
else
{
merged_host = base_host;
merged_port = base_port;
}
Free(path); Free(base_path);
merged_url = Malloc(strlen(merged_scheme) + 1 +
(merged_host ? 2 + strlen(merged_host) + 10 : 0) +
strlen(merged_path) + 1);
if(merged_host)
{
if(merged_port == -1)
sprintf(merged_url, "%s://%s%s",
merged_scheme, merged_host, merged_path);
else
sprintf(merged_url, "%s://%s:%d%s",
merged_scheme, merged_host, merged_port, merged_path);
}
else
sprintf(merged_url, "%s:%s", merged_scheme, merged_path);
ok:
Free(default_base);
if(_scheme) *_scheme = merged_scheme; else Free(merged_scheme);
if(_host) *_host = merged_host; else Free(merged_host);
if(_port) *_port = merged_port;
if(_path) *_path = merged_path; else Free(merged_path);
return merged_url;
bad:
Free(default_base);
Free(scheme);
Free(host);
Free(path);
Free(base_scheme);
Free(base_host);
Free(base_path);
return NULL;
}
/*
* Open a stream to a URL.
* url may be a relative URL, in which case it is merged with base,
* which is typically the URL of the containing document. If base
* is null, file://`pwd`/ is used, which is the right thing to do for
* filenames. If base is "", there is no base URL and relative
* URLs will fail.
* If merged_url is non-null the resulting URL is stored in it.
* If type begins "r", the URL is opened for reading, if "w" for
* writing. Writing is only supported for file URLs.
* If the type begins "rl", the data will be copied to a temporary
* file so that seeking is possible (NOT YET IMPLEMENTED).
* Returns a FILE16 for success, NULL for failure.
*/
FILE16 *url_open(const char *url, const char *base, const char *type,
char **merged_url)
{
char *scheme, *host, *path, *m_url;
int port, i;
FILE16 *f;
#ifdef HAVE_LIBZ
int len, gzipped = 0;
#endif
/* Determine the merged URL */
if(!(m_url = url_merge(url, base, &scheme, &host, &port, &path)))
return 0;
#ifdef HAVE_LIBZ
len = strlen(m_url);
if(len > 3 && strcmp8(m_url+len-3, ".gz") == 0)
gzipped = 1;
#endif
/*
printf("<%s> <%s> <%d> <%s>\n", scheme, host ? host : "", port, path);
printf("%s\n", m_url);
*/
/* Pass to the appropriate opening function */
for(i=0; i<NSCHEME; i++)
if(strcmp(scheme, schemes[i].scheme) == 0)
{
f = schemes[i].open(m_url, host, port, path, type);
Free(scheme);
if(host)
Free(host);
Free(path);
if(!f)
return f;
#ifdef HAVE_LIBZ
if(gzipped)
{
/* We have a gzip-compressed file which we hand to gzopen
* for further processing.
*/
gzFile gfile;
FILE *file = GetFILE(f);
if(!f)
{
LT_ERROR1(LEFILE,
"Can't attach gzip processor to URL \"%s\"\n",
m_url);
Free(m_url);
return 0;
}
#ifdef macintosh
gfile =gzdopen(dup(fileno(file)), *type == 'r' ? "rb" : "wb");
#else
gfile = gzdopen(dup(fileno(file)), type);
#endif
Fclose(f);
f = MakeFILE16FromGzip(gfile, type);
}
#endif
if(f && merged_url)
*merged_url = m_url;
else
Free(m_url);
return f;
}
/* Not implemented */
LT_ERROR1(LEFILE, "Error: scheme \"%s\" not implemented\n", scheme);
Free(scheme);
if(host)
Free(host);
Free(path);
Free(m_url);
return 0;
}
/* Open an http URL */
static FILE16 *http_open(const char *url,
const char *host, int port, const char *path,
const char *type)
{
#ifndef SOCKETS_IMPLEMENTED
LT_ERROR(NEUNSUP,
"http: URLs are not yet implemented on this platform\n");
return 0;
#else
FILE16 *f16;
struct sockaddr_in addr;
struct hostent *hostent;
int s, server_major, server_minor, status, count, c;
char reason[81];
const char *server_host, *request_uri;
int server_port;
#ifndef WIN32
FILE *fin,*fout;
#else
int i;
char buf[1024];
#endif
if(*type != 'r')
{
LT_ERROR1(LEFILE, "Error: can't open http URL \"%s\" for writing\n",
url);
return 0;
}
if(!host)
{
LT_ERROR1(LEFILE, "Error: no host part in http URL \"%s\"\n", url);
return 0;
}
if(proxy_host)
{
server_host = proxy_host;
server_port = proxy_port;
request_uri = url;
}
else
{
server_host = host;
server_port = port == -1 ? 80 : port;
request_uri = path;
}
/* Create the socket */
s = socket(PF_INET, SOCK_STREAM, 0);
#ifdef WIN32
if (s == INVALID_SOCKET) {
LT_ERROR1(LEFILE, "Error: system call socket failed: %d\n",
WSAGetLastError());
};
#else
if(s == -1) {
LT_ERROR1(LEFILE, "Error: system call socket failed: %s\n",
Strerror());
return 0;
};
#endif
/* Find the server address */
hostent = gethostbyname(server_host);
if(!hostent)
{
LT_ERROR3(LEFILE, "Error: can't find address for %shost \"%s\" "
"in http URL \"%s\"\n",
proxy_host ? "proxy " : "", server_host, url);
return 0;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
/* If we were really enthusiastic, we would try all the host's addresses */
memcpy(&addr.sin_addr, hostent->h_addr, hostent->h_length);
addr.sin_port = htons((unsigned short)server_port);
/* Connect */
if(connect(s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
{
LT_ERROR4(LEFILE, "Error: connection to %shost \"%s\" failed "
"for http URL \"%s\": %s\n",
proxy_host ? "proxy " : "", server_host, url, Strerror());
return 0;
}
#ifndef WIN32
#ifdef macintosh
fin = fdopen(s, "rb");
setvbuf(fin, 0, _IONBF, 0);
fout = fdopen(dup(s), "wb");
#else
fin = fdopen(s, "r");
setvbuf(fin, 0, _IONBF, 0);
fout = fdopen(dup(s), "w");
#endif
#endif
/* Send the request */
/*
* Apparently on the Macintosh, \n might not be ASCII LF, so we'll
* use numerics to be sure.
*/
#ifdef WIN32
sprintf(buf, "GET %s HTTP/1.0\015\012Connection: close\015\012",
request_uri);
if (send(s,buf,strlen8(buf),0)==SOCKET_ERROR) {
LT_ERROR1(LEFILE, "Error: system call socket failed: %d\n",
WSAGetLastError());
/* XXX close the socket? */
return 0;
};
if(port != -1)
sprintf(buf, "Host: %s:%d\015\012\015\012", host, port);
else
sprintf(buf, "Host: %s\015\012\015\012", host);
if (send(s,buf,strlen8(buf),0)==SOCKET_ERROR) {
LT_ERROR1(LEFILE, "Error: system call socket failed: %d\n",
WSAGetLastError());
/* XXX close the socket? */
return 0;
};
#else
fprintf(fout, "GET %s HTTP/1.0\015\012Connection: close\015\012",
request_uri);
if(port != -1)
fprintf(fout, "Host: %s:%d\015\012\015\012", host, port);
else
fprintf(fout, "Host: %s\015\012\015\012", host);
/* We used to test for errors after doing fclose, but this seemed
to produce spurious errors under Linux (RedHat 4.2), so now we
do fflush and test after that. */
fflush(fout);
if(ferror(fout))
{
LT_ERROR1(LEWRTF, "Error: write to socket failed: %s\n",Strerror());
fclose(fout);
fclose(fin);
return 0;
}
fclose(fout);
#endif
/* Read the status line */
#ifdef WIN32
for(i=0; i<sizeof(buf)-1; i++)
{
if(recv(s, &buf[i], 1, 0) != 1)
LT_ERROR1(LEFILE,
"Error: recv error from server for URL \"%s\"\n",
url);
if(buf[i] == '\n')
break;
}
count=sscanf(buf, "HTTP/%d.%d %d %80[^\012]",
&server_major, &server_minor, &status, reason);
#else
count=fscanf(fin, "HTTP/%d.%d %d %80[^\012]",
&server_major, &server_minor, &status, reason);
#endif
if(count != 4)
{
LT_ERROR3(LEFILE,
"Error: bad header from server for URL \"%s\"\n%d %s\n",
url, count, Strerror());
#ifndef WIN32
fclose(fin);
#endif
return 0;
}
if(status != 200)
{
/* We should handle 301 (redirection) but we don't */
LT_ERROR3(LEFILE, "Error: can't retrieve \"%s\": %d %s\n",
url, status, reason);
#ifndef WIN32
fclose(fin);
#endif
return 0;
}
/* Skip other headers */
count = 0;
#ifdef WIN32
while(recv(s, buf, 1, 0) == 1 && (c = buf[0], 1) || (c = EOF, 0))
#else
while((c = getc(fin)) != EOF)
#endif
{
if(c == '\012')
count++;
else if(c != '\015')
count = 0;
if(count == 2)
break;
}
if(c == EOF)
{
LT_ERROR1(LEFILE, "Error: EOF in headers retrieving \"%s\"\n", url);
#ifndef WIN32
fclose(fin);
#endif
return 0;
}
#ifdef WIN32
f16 = MakeFILE16FromWinsock(s, type);
#else
f16 = MakeFILE16FromFILE(fin, type);
#endif
SetCloseUnderlying(f16, 1);
return f16;
#endif /* SOCKETS_IMPLEMENTED */
}
/* Open a file URL (easy, at least on unix) */
static FILE16 *file_open(const char *url,
const char *host, int port, const char *path,
const char *type)
{
FILE *f;
FILE16 *f16;
char *file;
if(host && host[0])
WARN1(LEFILE, "Warning: ignoring host part in file URL \"%s\"\n", url);
#ifdef WIN32
/* DOS: translate /C:/a/b.c to C:\a\b.c */
if(path[0] == '/' && path[1] && path[2] == ':')
path++;
file = strdup8(path);
{
char *p;
for(p=file; *p; p++)
if(*p == '/')
*p = '\\';
}
#else
#ifdef mac_filenames
/* Mac: translate /a/b.c to a:b.c */
if(*path == '/')
path++;
file = strdup8(path);
{
char *p;
for(p=file; *p; p++)
if(*p == '/')
*p = ':';
}
#else
#ifdef CYGNUS32
/* Cygnus: translate /c:/a/b.c to //c/a/b.c */
file = strdup8(path);
if(path[0] == '/' && path[1] && path[2] == ':')
{
file[0] = file[1] = '/';
file[2] = path[1];
}
#else
/* Unix: a path is a path is a path! */
file = strdup8(path);
#endif
#endif
#endif
/* XXX should undo any escapes */
f = fopen(file, type);
if(!f)
{
perror(file);
Free(file);
return 0;
}
Free(file);
f16 = MakeFILE16FromFILE(f, type);
SetCloseUnderlying(f16, 1);
return f16;
}
static void parse_url(const char *url,
char **scheme, char **host, int *port, char **path)
{
char *p, *q;
int warned = 0;
*scheme = *host = *path = 0;
*port = -1;
/* Does it start with a scheme? */
for(p = (char *)url; *p; p++)
if(*p == ':' || *p == '/')
break;
if(p > url && *p == ':')
{
*scheme = Malloc(p - url + 1);
strncpy(*scheme, url, p - url);
(*scheme)[p - url] = '\0';
url = p+1;
}
/* Does it have a net_loc? */
if(url[0] == '/' && url[1] == '/')
{
url += 2;
for(p = (char *)url; *p; p++)
if(*p == '/')
break;
/* Does it have a port number? */
for(q = p-1; q >= url; q--)
if(!isdigit(*q))
break;
if(q < p-1 && *q == ':')
*port = atoi(q+1);
else
q = p;
*host = Malloc(q - url + 1);
strncpy(*host, url, q - url);
(*host)[q - url] = '\0';
url = p;
}
/* The rest is the path */
if(*url)
*path = strdup8(url);
else
*path = strdup8("/");
/* Windoze users have a tendency to use backslashes instead of slashes */
for(p=*path; *p; p++)
if(*p == '\\')
{
if(!warned)
{
WARN1(LEFILE, "Warning: illegal backslashes in URL path \"%s\""
"replaced by slashes\n", url);
warned = 1;
}
*p = '/';
}
}
|