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