Annotation of libwww/Library/src/HTParse.c, revision 2.78
2.41 frystyk 1: /* HTParse.c
2: ** URI MANAGEMENT
3: **
2.46 frystyk 4: ** (c) COPYRIGHT MIT 1995.
2.41 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
2.78 ! frystyk 6: ** @(#) $Id: HTParse.c,v 2.77 1998/05/19 16:49:35 frystyk Exp $
2.26 frystyk 7: **
8: ** history:
9: ** May 12 94 TAB added as legal char in HTCleanTelnetString
10: **
1.1 timbl 11: */
2.31 frystyk 12:
2.44 frystyk 13: /* Library include files */
2.76 frystyk 14: #include "wwwsys.h"
2.65 frystyk 15: #include "WWWUtil.h"
16: #include "HTParse.h" /* Implemented here */
2.6 timbl 17:
2.66 frystyk 18: typedef struct _HTURI {
19: char * access; /* Now known as "scheme" */
20: char * host;
21: char * absolute;
22: char * relative;
23: char * fragment;
24: } HTURI;
1.1 timbl 25:
26: /* Scan a filename for its consituents
27: ** -----------------------------------
28: **
29: ** On entry,
30: ** name points to a document name which may be incomplete.
31: ** On exit,
32: ** absolute or relative may be nonzero (but not both).
2.66 frystyk 33: ** host, fragment and access may be nonzero if they were specified.
1.1 timbl 34: ** Any which are nonzero point to zero terminated strings.
35: */
2.66 frystyk 36: PRIVATE void scan (char * name, HTURI * parts)
1.1 timbl 37: {
38: char * p;
2.66 frystyk 39: char * after_access = name;
40: memset(parts, '\0', sizeof(HTURI));
41:
42: /* Look for fragment identifier */
2.70 frystyk 43: if ((p = strchr(name, '#')) != NULL) {
2.66 frystyk 44: *p++ = '\0';
45: parts->fragment = p;
46: }
2.71 frystyk 47:
2.74 frystyk 48:
49: if ((p = strchr(name, ' ')) != NULL) *p++ = '\0';
1.1 timbl 50:
51: for(p=name; *p; p++) {
2.74 frystyk 52:
53: /*
54: ** Look for any whitespace. This is very bad for pipelining as it
55: ** makes the request invalid
56: */
2.77 frystyk 57: if (isspace((int) *p)) {
2.74 frystyk 58: char *orig=p, *dest=p+1;
59: while ((*orig++ = *dest++));
60: p = p-1;
61: }
2.66 frystyk 62: if (*p=='/' || *p=='#' || *p=='?')
63: break;
1.1 timbl 64: if (*p==':') {
65: *p = 0;
2.20 timbl 66: parts->access = after_access; /* Scheme has been specified */
2.37 howcome 67:
2.44 frystyk 68: /* The combination of gcc, the "-O" flag and the HP platform is
69: unhealthy. The following three lines is a quick & dirty fix, but is
70: not recommended. Rather, turn off "-O". */
71:
2.42 howcome 72: /* after_access = p;*/
2.44 frystyk 73: /* while (*after_access == 0)*/
74: /* after_access++;*/
2.42 howcome 75:
1.1 timbl 76: after_access = p+1;
2.37 howcome 77:
2.22 luotonen 78: if (0==strcasecomp("URL", parts->access)) {
2.20 timbl 79: parts->access = NULL; /* Ignore IETF's URL: pre-prefix */
80: } else break;
1.1 timbl 81: }
82: }
83:
84: p = after_access;
85: if (*p=='/'){
86: if (p[1]=='/') {
87: parts->host = p+2; /* host has been specified */
88: *p=0; /* Terminate access */
89: p=strchr(parts->host,'/'); /* look for end of host name if any */
90: if(p) {
91: *p=0; /* Terminate host */
92: parts->absolute = p+1; /* Root has been found */
93: }
94: } else {
95: parts->absolute = p+1; /* Root found but no host */
96: }
97: } else {
2.49 frystyk 98: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.1 timbl 99: }
2.50 frystyk 100: }
1.1 timbl 101:
102:
103: /* Parse a Name relative to another name
104: ** -------------------------------------
105: **
106: ** This returns those parts of a name which are given (and requested)
107: ** substituting bits from the related name where necessary.
108: **
109: ** On entry,
110: ** aName A filename given
2.33 howcome 111: ** relatedName A name relative to which aName is to be parsed. Give
112: ** it an empty string if aName is absolute.
1.1 timbl 113: ** wanted A mask for the bits which are wanted.
114: **
115: ** On exit,
116: ** returns A pointer to a malloc'd string which MUST BE FREED
117: */
2.63 frystyk 118: PUBLIC char * HTParse (const char *aName, const char *relatedName, int wanted)
1.1 timbl 119: {
120: char * result = 0;
121: char * return_value = 0;
122: int len;
123: char * name = 0;
124: char * rel = 0;
125: char * p;
2.12 timbl 126: char * access;
2.66 frystyk 127: HTURI given, related;
2.33 howcome 128:
129: if (!relatedName) /* HWL 23/8/94: dont dump due to NULL */
130: relatedName = "";
1.1 timbl 131:
2.50 frystyk 132: /* Make working copies of input strings to cut up: */
1.1 timbl 133: len = strlen(aName)+strlen(relatedName)+10;
2.61 frystyk 134: if ((result=(char *) HT_MALLOC(len)) == NULL) /* Lots of space: more than enough */
135: HT_OUTOFMEM("parse space");
1.1 timbl 136: StrAllocCopy(name, aName);
137: StrAllocCopy(rel, relatedName);
138:
139: scan(name, &given);
140: scan(rel, &related);
141: result[0]=0; /* Clear string */
2.12 timbl 142: access = given.access ? given.access : related.access;
1.1 timbl 143: if (wanted & PARSE_ACCESS)
2.12 timbl 144: if (access) {
145: strcat(result, access);
1.1 timbl 146: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
147: }
148:
149: if (given.access && related.access) /* If different, inherit nothing. */
150: if (strcmp(given.access, related.access)!=0) {
151: related.host=0;
152: related.absolute=0;
153: related.relative=0;
2.66 frystyk 154: related.fragment=0;
1.1 timbl 155: }
156:
157: if (wanted & PARSE_HOST)
158: if(given.host || related.host) {
159: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
160: strcat(result, given.host ? given.host : related.host);
161: }
162:
163: if (given.host && related.host) /* If different hosts, inherit no path. */
164: if (strcmp(given.host, related.host)!=0) {
165: related.absolute=0;
166: related.relative=0;
2.66 frystyk 167: related.fragment=0;
1.1 timbl 168: }
169:
170: if (wanted & PARSE_PATH) {
171: if(given.absolute) { /* All is given */
172: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
173: strcat(result, given.absolute);
174: } else if(related.absolute) { /* Adopt path not name */
175: strcat(result, "/");
176: strcat(result, related.absolute);
177: if (given.relative) {
178: p = strchr(result, '?'); /* Search part? */
179: if (!p) p=result+strlen(result)-1;
180: for (; *p!='/'; p--); /* last / */
181: p[1]=0; /* Remove filename */
182: strcat(result, given.relative); /* Add given one */
2.55 frystyk 183: #if 0
2.45 frystyk 184: result = HTSimplify (&result);
2.55 frystyk 185: #endif
1.1 timbl 186: }
187: } else if(given.relative) {
188: strcat(result, given.relative); /* what we've got */
189: } else if(related.relative) {
190: strcat(result, related.relative);
191: } else { /* No inheritance */
192: strcat(result, "/");
193: }
194: }
195:
2.75 frystyk 196: if (wanted & PARSE_VIEW)
2.66 frystyk 197: if(given.fragment || related.fragment) {
198: if(given.absolute && given.fragment) { /*Fixes for relURLs...*/
2.58 neon 199: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
2.66 frystyk 200: strcat(result, given.fragment);
201: } else if (!(given.absolute) && !(given.fragment)) {
2.58 neon 202: strcat(result, "");
203: } else {
204: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
2.66 frystyk 205: strcat(result, given.fragment ? given.fragment : related.fragment);
2.58 neon 206: }
1.1 timbl 207: }
2.61 frystyk 208: HT_FREE(rel);
209: HT_FREE(name);
1.1 timbl 210:
211: StrAllocCopy(return_value, result);
2.61 frystyk 212: HT_FREE(result);
1.1 timbl 213: return return_value; /* exactly the right length */
214: }
2.21 frystyk 215:
2.20 timbl 216:
2.65 frystyk 217: /*
2.53 frystyk 218: ** Canonicalizes the URL in the following manner starting from the host
219: ** pointer:
220: **
221: ** 1) The host name is converted to lowercase
2.65 frystyk 222: ** 2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
2.53 frystyk 223: **
224: ** Return: OK The position of the current path part of the URL
225: ** which might be the old one or a new one.
226: */
2.65 frystyk 227: PRIVATE char * HTCanon (char ** filename, char * host)
2.53 frystyk 228: {
229: char *newname = NULL;
230: char *port;
231: char *strptr;
232: char *path;
233: char *access = host-3;
234:
235: while (access>*filename && *(access-1)!='/') /* Find access method */
236: access--;
237: if ((path = strchr(host, '/')) == NULL) /* Find path */
238: path = host + strlen(host);
239: if ((strptr = strchr(host, '@')) != NULL && strptr<path) /* UserId */
240: host = strptr;
241: if ((port = strchr(host, ':')) != NULL && port>path) /* Port number */
242: port = NULL;
243:
244: strptr = host; /* Convert to lower-case */
245: while (strptr<path) {
246: *strptr = TOLOWER(*strptr);
247: strptr++;
248: }
249:
250: /* Does the URL contain a full domain name? This also works for a
251: numerical host name. The domain name is already made lower-case
252: and without a trailing dot. */
2.65 frystyk 253: #if 0
2.53 frystyk 254: if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
255: strncasecomp(host, "localhost", 9)) {
2.63 frystyk 256: const char *domain = HTGetDomainName();
2.53 frystyk 257: if (domain && *domain) {
2.61 frystyk 258: if ((newname = (char *) HT_CALLOC(1, strlen(*filename) + strlen(domain)+2)) == NULL)
259: HT_OUTOFMEM("HTCanon");
2.53 frystyk 260: if (port)
261: strncpy(newname, *filename, (int) (port-*filename));
262: else
263: strncpy(newname, *filename, (int) (path-*filename));
264: strcat(newname, ".");
265: strcat(newname, domain);
266: }
2.65 frystyk 267: } else /* Look for a trailing dot */
268: #endif
269: {
2.53 frystyk 270: char *dot = port ? port : path;
271: if (dot > *filename && *--dot=='.') {
272: char *orig=dot, *dest=dot+1;
273: while((*orig++ = *dest++));
274: if (port) port--;
275: path--;
276: }
277: }
278: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
279: if (port) {
280: if (!*(port+1) || *(port+1)=='/') {
281: if (!newname) {
282: char *orig=port, *dest=port+1;
283: while((*orig++ = *dest++));
284: }
285: } else if ((!strncmp(access, "http", 4) &&
286: (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
287: (!strncmp(access, "gopher", 6) &&
288: (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
289: (!strncmp(access, "ftp", 3) &&
290: (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
291: if (!newname) {
292: char *orig=port, *dest=port+3;
293: while((*orig++ = *dest++));
2.54 frystyk 294: path -= 3; /* Update path position, Henry Minsky */
2.53 frystyk 295: }
296: } else if (newname)
297: strncat(newname, port, (int) (path-port));
298: }
299:
300: if (newname) {
301: char *newpath = newname+strlen(newname);
302: strcat(newname, path);
303: path = newpath;
2.61 frystyk 304: HT_FREE(*filename); /* Free old copy */
2.53 frystyk 305: *filename = newname;
306: }
307: return path;
2.67 frystyk 308: }
309:
310: /*
311: ** Search the URL and determine whether it is a relative or absolute URL.
312: ** We check to see if there is a ":" before any "/", "?", and "#". If this
313: ** is the case then we say it is absolute. Otherwise it is relative.
314: */
315: PUBLIC BOOL HTURL_isAbsolute (const char * url)
316: {
317: if (url) {
318: const char * ptr = url;
319: while (*ptr) {
320: if (*ptr == ':') return YES;
321: if (*ptr == '/' || *ptr == '?' || *ptr == '#') break;
322: ptr ++;
323: }
324: }
325: return NO;
2.53 frystyk 326: }
327:
2.20 timbl 328: /* Simplify a URI
329: // --------------
330: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1 timbl 331: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20 timbl 332: // Simplification helps us recognize duplicate URIs.
1.1 timbl 333: //
334: // Thus, /etc/junk/../fred becomes /etc/fred
335: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 336: //
337: // but we should NOT change
338: // http://fred.xxx.edu/../..
339: //
340: // or ../../albert.html
2.26 frystyk 341: //
342: // In order to avoid empty URLs the following URLs become:
343: //
344: // /fred/.. becomes /fred/..
345: // /fred/././.. becomes /fred/..
2.27 frystyk 346: // /fred/.././junk/.././ becomes /fred/..
2.26 frystyk 347: //
2.30 frystyk 348: // If more than one set of `://' is found (several proxies in cascade) then
349: // only the part after the last `://' is simplified.
2.44 frystyk 350: //
351: // Returns: A string which might be the old one or a new one.
1.1 timbl 352: */
2.60 frystyk 353: PUBLIC char *HTSimplify (char ** url)
1.1 timbl 354: {
2.31 frystyk 355: char *path;
356: char *p;
2.55 frystyk 357: if (!url || !*url) {
2.62 eric 358: if (URI_TRACE) HTTrace("HTSimplify.. Nothing done\n");
2.55 frystyk 359: return *url;
2.31 frystyk 360: }
2.62 eric 361: if (URI_TRACE) HTTrace("HTSimplify.. `%s\' ", *url);
2.27 frystyk 362:
2.55 frystyk 363: /* Find any scheme name */
364: if ((path = strstr(*url, "://")) != NULL) { /* Find host name */
2.30 frystyk 365: char *newptr;
2.60 frystyk 366: char *access = *url;
367: while (access<path && (*access=TOLOWER(*access))) access++;
2.31 frystyk 368: path += 3;
2.60 frystyk 369: while ((newptr = strstr(path, "://")) != NULL) /* For proxies */
2.31 frystyk 370: path = newptr+3;
2.55 frystyk 371: path = HTCanon(url, path); /* We have a host name */
372: } else if ((path = strstr(*url, ":/")) != NULL) {
2.31 frystyk 373: path += 2;
2.27 frystyk 374: } else
2.55 frystyk 375: path = *url;
2.31 frystyk 376: if (*path == '/' && *(path+1)=='/') { /* Some URLs start //<foo> */
377: path += 1;
2.34 frystyk 378: } else if (!strncmp(path, "news:", 5)) {
379: char *ptr = strchr(path+5, '@');
380: if (!ptr) ptr = path+5;
381: while (*ptr) { /* Make group or host lower case */
382: *ptr = TOLOWER(*ptr);
383: ptr++;
2.31 frystyk 384: }
385: if (URI_TRACE)
2.62 eric 386: HTTrace("into\n............ `%s'\n", *url);
2.55 frystyk 387: return *url; /* Doesn't need to do any more */
2.31 frystyk 388: }
389: if ((p = path)) {
2.55 frystyk 390: char *end;
391: if (!((end = strchr(path, ';')) || (end = strchr(path, '?')) ||
392: (end = strchr(path, '#'))))
393: end = path+strlen(path);
2.31 frystyk 394:
395: /* Parse string second time to simplify */
396: p = path;
2.55 frystyk 397: while(p<end) {
2.31 frystyk 398: if (*p=='/') {
2.56 frystyk 399: if (p>*url && *(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) {
400: char *orig = p+1;
401: char *dest = (*(p+2)!='/') ? p+2 : p+3;
2.31 frystyk 402: while ((*orig++ = *dest++)); /* Remove a slash and a dot */
2.55 frystyk 403: end = orig-1;
2.56 frystyk 404: } else if (*(p+1)=='.' && *(p+2)=='.' && (*(p+3)=='/' || !*(p+3))) {
2.31 frystyk 405: char *q = p;
406: while (q>path && *--q!='/'); /* prev slash */
2.56 frystyk 407: if (strncmp(q, "/../", 4)) {
408: char *orig = q+1;
409: char *dest = (*(p+3)!='/') ? p+3 : p+4;
2.31 frystyk 410: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
2.55 frystyk 411: end = orig-1;
2.56 frystyk 412: p = q; /* Start again with prev slash */
2.31 frystyk 413: } else
414: p++;
415: } else if (*(p+1)=='/') {
416: while (*(p+1)=='/') {
417: char *orig=p, *dest=p+1;
418: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2.55 frystyk 419: end = orig-1;
2.19 frystyk 420: }
2.56 frystyk 421: } else
422: p++;
423: } else
424: p++;
425: }
2.19 frystyk 426: }
2.70 frystyk 427:
428: /*
429: ** Check for host/../.. kind of things
430: */
2.76 frystyk 431: while (*path=='/' && *(path+1)=='.' && *(path+2)=='.' &&
432: (!*(path+3) || *(path+3)=='/')) {
433: char * orig = path;
434: char * dest = path+3;
435: while ((*orig++ = *dest++));
436: }
2.70 frystyk 437: if (URI_TRACE) HTTrace("into\n............ `%s'\n", *url);
2.55 frystyk 438: return *url;
2.19 frystyk 439: }
2.31 frystyk 440:
1.1 timbl 441: /* Make Relative Name
442: ** ------------------
443: **
444: ** This function creates and returns a string which gives an expression of
445: ** one address as related to another. Where there is no relation, an absolute
446: ** address is retured.
447: **
448: ** On entry,
449: ** Both names must be absolute, fully qualified names of nodes
2.66 frystyk 450: ** (no fragment bits)
1.1 timbl 451: **
452: ** On exit,
453: ** The return result points to a newly allocated name which, if
454: ** parsed by HTParse relative to relatedName, will yield aName.
455: ** The caller is responsible for freeing the resulting name later.
456: **
457: */
2.63 frystyk 458: PUBLIC char * HTRelative (const char * aName, const char * relatedName)
1.1 timbl 459: {
460: char * result = 0;
2.63 frystyk 461: const char *p = aName;
462: const char *q = relatedName;
2.76 frystyk 463: const char * after_access = NULL;
2.63 frystyk 464: const char * path = 0;
465: const char * last_slash = 0;
1.1 timbl 466: int slashes = 0;
467:
468: for(;*p; p++, q++) { /* Find extent of match */
469: if (*p!=*q) break;
2.76 frystyk 470: if (*p==':') if (!after_access) after_access = p+1;
1.1 timbl 471: if (*p=='/') {
472: last_slash = p;
473: slashes++;
474: if (slashes==3) path=p;
475: }
476: }
477:
478: /* q, p point to the first non-matching character or zero */
479:
480: if (!after_access) { /* Different access */
481: StrAllocCopy(result, aName);
482: } else if (slashes<3){ /* Different nodes */
483: StrAllocCopy(result, after_access);
484: } else { /* Some path in common */
485: int levels= 0;
2.69 frystyk 486: for(; *q && *q!='#' && *q!=';' && *q!='?'; q++) if (*q=='/') levels++;
2.73 frystyk 487: if ((result = (char *) HT_MALLOC(3*levels + strlen(last_slash) + 4)) == NULL)
2.61 frystyk 488: HT_OUTOFMEM("HTRelative");
2.72 frystyk 489: *result = '\0';
2.78 ! frystyk 490: if (!levels) strcat(result, "./");
1.1 timbl 491: for(;levels; levels--)strcat(result, "../");
492: strcat(result, last_slash+1);
2.72 frystyk 493: if (!*result) strcat(result, "./");
1.1 timbl 494: }
2.72 frystyk 495: if (URI_TRACE)
2.78 ! frystyk 496: HTTrace("HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
2.72 frystyk 497: aName, relatedName, result);
2.78 ! frystyk 498: #if 0
! 499: {
! 500: char * absstr = HTParse(result, relatedName, PARSE_ALL);
! 501: HTSimplify(&absstr);
! 502: HTTrace("HTRelative.. `%s' made absolute based on `%s' is `%s'\n",
! 503: result, relatedName, absstr);
! 504: if (strcmp(absstr, aName) != 0) HTTrace("THEY DIFFER!!!\n");
! 505: HT_FREE(absstr);
! 506: }
! 507: #endif
1.1 timbl 508: return result;
509: }
2.1 timbl 510:
2.24 luotonen 511: /* HTCleanTelnetString()
512: * Make sure that the given string doesn't contain characters that
513: * could cause security holes, such as newlines in ftp, gopher,
514: * news or telnet URLs; more specifically: allows everything between
2.26 frystyk 515: * ASCII 20-7E, and also A0-FE, inclusive. Also TAB ('\t') allowed!
2.24 luotonen 516: *
517: * On entry,
518: * str the string that is *modified* if necessary. The
519: * string will be truncated at the first illegal
520: * character that is encountered.
521: * On exit,
522: * returns YES, if the string was modified.
523: * NO, otherwise.
524: */
2.60 frystyk 525: PUBLIC BOOL HTCleanTelnetString (char * str)
2.24 luotonen 526: {
527: char * cur = str;
528:
529: if (!str) return NO;
530:
531: while (*cur) {
2.52 frystyk 532: int a = TOASCII((unsigned char) *cur);
2.26 frystyk 533: if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) || a > 0xFE)) {
2.31 frystyk 534: if (URI_TRACE)
2.62 eric 535: HTTrace("Illegal..... character in URL: \"%s\"\n",str);
2.24 luotonen 536: *cur = 0;
2.31 frystyk 537: if (URI_TRACE)
2.62 eric 538: HTTrace("Truncated... \"%s\"\n",str);
2.24 luotonen 539: return YES;
540: }
541: cur++;
542: }
543: return NO;
544: }
545:
Webmaster