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