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