Annotation of libwww/Library/src/HTParse.c, revision 2.49
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: }
96:
2.16 timbl 97: #ifdef OLD_CODE
1.1 timbl 98: /* Access specified but no host: the anchor was not really one
2.16 timbl 99: e.g. news:j462#36487@foo.bar -- JFG 10/jul/92, from bug report */
100: /* This kludge doesn't work for example when coming across
101: file:/usr/local/www/fred#123
102: which loses its anchor. Correct approach in news is to
103: escape weird characters not allowed in URL. TBL 21/dec/93
104: */
1.1 timbl 105: if (parts->access && ! parts->host && parts->anchor) {
106: *(parts->anchor - 1) = '#'; /* Restore the '#' in the address */
107: parts->anchor = 0;
108: }
2.16 timbl 109: #endif
1.1 timbl 110:
111: #ifdef NOT_DEFINED /* search is just treated as part of path */
112: {
113: char *p = relative ? relative : absolute;
114: if (p) {
115: char * q = strchr(p, '?'); /* Any search string? */
116: if (q) {
117: *q = 0; /* If so, chop that off. */
118: parts->search = q+1;
119: }
120: }
121: }
122: #endif
123: } /*scan */
124:
125:
126: /* Parse a Name relative to another name
127: ** -------------------------------------
128: **
129: ** This returns those parts of a name which are given (and requested)
130: ** substituting bits from the related name where necessary.
131: **
132: ** On entry,
133: ** aName A filename given
2.33 howcome 134: ** relatedName A name relative to which aName is to be parsed. Give
135: ** it an empty string if aName is absolute.
1.1 timbl 136: ** wanted A mask for the bits which are wanted.
137: **
138: ** On exit,
139: ** returns A pointer to a malloc'd string which MUST BE FREED
140: */
2.32 frystyk 141: char * HTParse ARGS3(CONST char *, aName, CONST char *, relatedName,
142: int, wanted)
1.1 timbl 143: {
144: char * result = 0;
145: char * return_value = 0;
146: int len;
147: char * name = 0;
148: char * rel = 0;
149: char * p;
2.12 timbl 150: char * access;
1.1 timbl 151: struct struct_parts given, related;
2.33 howcome 152:
153: if (!relatedName) /* HWL 23/8/94: dont dump due to NULL */
154: relatedName = "";
1.1 timbl 155:
156: /* Make working copies of input strings to cut up:
157: */
158: len = strlen(aName)+strlen(relatedName)+10;
159: result=(char *)malloc(len); /* Lots of space: more than enough */
160: if (result == NULL) outofmem(__FILE__, "HTParse");
161:
162: StrAllocCopy(name, aName);
163: StrAllocCopy(rel, relatedName);
164:
165: scan(name, &given);
166: scan(rel, &related);
167: result[0]=0; /* Clear string */
2.12 timbl 168: access = given.access ? given.access : related.access;
1.1 timbl 169: if (wanted & PARSE_ACCESS)
2.12 timbl 170: if (access) {
171: strcat(result, access);
1.1 timbl 172: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
173: }
174:
175: if (given.access && related.access) /* If different, inherit nothing. */
176: if (strcmp(given.access, related.access)!=0) {
177: related.host=0;
178: related.absolute=0;
179: related.relative=0;
180: related.anchor=0;
181: }
182:
183: if (wanted & PARSE_HOST)
184: if(given.host || related.host) {
185: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
186: strcat(result, given.host ? given.host : related.host);
187: }
188:
189: if (given.host && related.host) /* If different hosts, inherit no path. */
190: if (strcmp(given.host, related.host)!=0) {
191: related.absolute=0;
192: related.relative=0;
193: related.anchor=0;
194: }
195:
196: if (wanted & PARSE_PATH) {
197: if(given.absolute) { /* All is given */
198: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
199: strcat(result, given.absolute);
200: } else if(related.absolute) { /* Adopt path not name */
201: strcat(result, "/");
202: strcat(result, related.absolute);
203: if (given.relative) {
204: p = strchr(result, '?'); /* Search part? */
205: if (!p) p=result+strlen(result)-1;
206: for (; *p!='/'; p--); /* last / */
207: p[1]=0; /* Remove filename */
208: strcat(result, given.relative); /* Add given one */
2.45 frystyk 209: result = HTSimplify (&result);
1.1 timbl 210: }
211: } else if(given.relative) {
212: strcat(result, given.relative); /* what we've got */
213: } else if(related.relative) {
214: strcat(result, related.relative);
215: } else { /* No inheritance */
216: strcat(result, "/");
217: }
218: }
219:
220: if (wanted & PARSE_ANCHOR)
221: if(given.anchor || related.anchor) {
222: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
223: strcat(result, given.anchor ? given.anchor : related.anchor);
224: }
225: free(rel);
226: free(name);
227:
228: StrAllocCopy(return_value, result);
229: free(result);
230: return return_value; /* exactly the right length */
231: }
2.21 frystyk 232:
2.20 timbl 233:
234: /* Simplify a URI
235: // --------------
236: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1 timbl 237: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20 timbl 238: // Simplification helps us recognize duplicate URIs.
1.1 timbl 239: //
240: // Thus, /etc/junk/../fred becomes /etc/fred
241: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 242: //
243: // but we should NOT change
244: // http://fred.xxx.edu/../..
245: //
246: // or ../../albert.html
2.26 frystyk 247: //
248: // In the same manner, the following prefixed are preserved:
249: //
250: // ./<etc>
251: // //<etc>
252: //
253: // In order to avoid empty URLs the following URLs become:
254: //
255: // /fred/.. becomes /fred/..
256: // /fred/././.. becomes /fred/..
2.27 frystyk 257: // /fred/.././junk/.././ becomes /fred/..
2.26 frystyk 258: //
2.30 frystyk 259: // If more than one set of `://' is found (several proxies in cascade) then
260: // only the part after the last `://' is simplified.
2.44 frystyk 261: //
262: // Returns: A string which might be the old one or a new one.
1.1 timbl 263: */
2.45 frystyk 264: PUBLIC char *HTSimplify ARGS1(char **, filename)
1.1 timbl 265: {
2.31 frystyk 266: char *path;
267: char *p;
2.19 frystyk 268:
2.45 frystyk 269: if (!*filename) {
2.31 frystyk 270: if (URI_TRACE)
2.44 frystyk 271: fprintf(TDEST, "HTSimplify.. Bad argument\n");
2.45 frystyk 272: return *filename;
2.31 frystyk 273: }
274: if (URI_TRACE)
2.45 frystyk 275: fprintf(TDEST, "HTSimplify.. `%s\' ", *filename);
2.27 frystyk 276:
2.45 frystyk 277: if ((path = strstr(*filename, "://")) != NULL) { /* Find host name */
2.30 frystyk 278: char *newptr;
2.31 frystyk 279: path += 3;
280: while ((newptr = strstr(path, "://")) != NULL)
281: path = newptr+3;
2.45 frystyk 282: path = HTCanon(filename, path); /* We have a host name */
283: } else if ((path = strstr(*filename, ":/")) != NULL) {
2.31 frystyk 284: path += 2;
2.27 frystyk 285: } else
2.45 frystyk 286: path = *filename;
2.31 frystyk 287: if (*path == '/' && *(path+1)=='/') { /* Some URLs start //<foo> */
288: path += 1;
2.34 frystyk 289: } else if (!strncmp(path, "news:", 5)) {
290: char *ptr = strchr(path+5, '@');
291: if (!ptr) ptr = path+5;
292: while (*ptr) { /* Make group or host lower case */
293: *ptr = TOLOWER(*ptr);
294: ptr++;
2.31 frystyk 295: }
296: if (URI_TRACE)
2.45 frystyk 297: fprintf(TDEST, "into\n............ `%s'\n", *filename);
298: return *filename; /* Doesn't need to do any more */
2.31 frystyk 299: }
300: if ((p = path)) {
301: int segments = 0;
302:
303: /* Parse string first time to find number of `real' tokens */
304: while (*p) {
305: if (*p=='/' || p==path) {
306: if (!((*(p+1)=='/' || !*(p+1)) ||
307: (*(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) ||
308: (*(p+1)=='.' && *(p+2)=='.' &&(*(p+3)=='/' || !*(p+3)))))
309: segments++;
310: }
311: p++;
312: }
2.19 frystyk 313:
2.31 frystyk 314: /* Parse string second time to simplify */
315: p = path;
316: while(*p) {
317: if (*p=='/') {
318: if (p>path && *(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) {
319: char *orig=p, *dest=p+2;
320: while ((*orig++ = *dest++)); /* Remove a slash and a dot */
321: p--;
322: } else if (segments>1 && *(p+1)=='.' && *(p+2)=='.' &&
323: (*(p+3)=='/' || !*(p+3))) {
324: char *q = p;
325: while (q>path && *--q!='/'); /* prev slash */
326: if (strncmp(q, "/../", 4) && strncmp(q, "/./", 3) &&
327: strncmp(q, "./", 2)) {
328: char *orig=q, *dest=p+3;
329: if (*q!='/') dest++;
330: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
331: segments--;
332: p = q-1; /* Start again with prev slash */
333: } else
334: p++;
335: } else if (*(p+1)=='/') {
336: while (*(p+1)=='/') {
337: char *orig=p, *dest=p+1;
338: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2.19 frystyk 339: }
340: }
341: }
2.31 frystyk 342: p++;
343: } /* end while (*p) */
2.19 frystyk 344: }
2.31 frystyk 345: if (URI_TRACE)
2.45 frystyk 346: fprintf(TDEST, "into\n............ `%s'\n", *filename);
347: return *filename;
2.19 frystyk 348: }
2.31 frystyk 349:
2.19 frystyk 350: #ifdef OLD_CODE
2.17 frystyk 351: char * p = filename;
1.1 timbl 352: char * q;
2.17 frystyk 353:
354: if (p) {
355: while (*p && (*p == '/' || *p == '.')) /* Pass starting / or .'s */
356: p++;
357: while(*p) {
358: if (*p=='/') {
1.1 timbl 359: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 360: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
361: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
362: &&!(q-1>filename && q[-1]=='/')) {
2.15 luotonen 363: ari_strcpy(q, p+3); /* Remove /xxx/.. */
1.1 timbl 364: if (!*filename) strcpy(filename, "/");
365: p = q-1; /* Start again with prev slash */
2.11 timbl 366: } else { /* xxx/.. leave it! */
2.9 timbl 367: #ifdef BUG_CODE
2.15 luotonen 368: ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
1.1 timbl 369: p = filename; /* Start again */
2.9 timbl 370: #endif
1.1 timbl 371: }
372: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15 luotonen 373: ari_strcpy(p, p+2); /* Remove a slash and a dot */
2.13 luotonen 374: } else if (p[-1] != ':') {
375: while (p[1] == '/') {
2.15 luotonen 376: ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13 luotonen 377: }
1.1 timbl 378: }
2.17 frystyk 379: }
380: p++;
381: } /* end while (*p) */
382: } /* end if (p) */
1.1 timbl 383: }
2.19 frystyk 384: #endif /* OLD_CODE */
1.1 timbl 385:
386:
387: /* Make Relative Name
388: ** ------------------
389: **
390: ** This function creates and returns a string which gives an expression of
391: ** one address as related to another. Where there is no relation, an absolute
392: ** address is retured.
393: **
394: ** On entry,
395: ** Both names must be absolute, fully qualified names of nodes
396: ** (no anchor bits)
397: **
398: ** On exit,
399: ** The return result points to a newly allocated name which, if
400: ** parsed by HTParse relative to relatedName, will yield aName.
401: ** The caller is responsible for freeing the resulting name later.
402: **
403: */
2.32 frystyk 404: char * HTRelative ARGS2(CONST char *, aName, CONST char *, relatedName)
1.1 timbl 405: {
406: char * result = 0;
407: CONST char *p = aName;
408: CONST char *q = relatedName;
409: CONST char * after_access = 0;
410: CONST char * path = 0;
411: CONST char * last_slash = 0;
412: int slashes = 0;
413:
414: for(;*p; p++, q++) { /* Find extent of match */
415: if (*p!=*q) break;
416: if (*p==':') after_access = p+1;
417: if (*p=='/') {
418: last_slash = p;
419: slashes++;
420: if (slashes==3) path=p;
421: }
422: }
423:
424: /* q, p point to the first non-matching character or zero */
425:
426: if (!after_access) { /* Different access */
427: StrAllocCopy(result, aName);
428: } else if (slashes<3){ /* Different nodes */
429: StrAllocCopy(result, after_access);
2.29 frystyk 430: #if 0 /* Henrik */
1.1 timbl 431: } else if (slashes==3){ /* Same node, different path */
432: StrAllocCopy(result, path);
2.21 frystyk 433: #endif
1.1 timbl 434: } else { /* Some path in common */
435: int levels= 0;
436: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
437: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
438: if (result == NULL) outofmem(__FILE__, "HTRelative");
439: result[0]=0;
440: for(;levels; levels--)strcat(result, "../");
441: strcat(result, last_slash+1);
442: }
2.44 frystyk 443: if (URI_TRACE) fprintf(TDEST,
2.21 frystyk 444: "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
445: aName, relatedName, result);
1.1 timbl 446: return result;
447: }
2.1 timbl 448:
449:
2.31 frystyk 450: /* HTCanon
451: **
452: ** Canonicalizes the URL in the following manner starting from the host
453: ** pointer:
454: **
455: ** 1) The host name is converted to lowercase
456: ** 2) Expands the host name of the URL from a local name to a full
457: ** domain name. A host name is started by `://'.
2.38 frystyk 458: ** 3) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
2.31 frystyk 459: **
460: ** Return: OK The position of the current path part of the URL
2.44 frystyk 461: ** which might be the old one or a new one.
2.31 frystyk 462: */
463: PUBLIC char *HTCanon ARGS2 (char **, filename, char *, host)
464: {
2.32 frystyk 465: char *newname = NULL;
2.31 frystyk 466: char *port;
467: char *strptr;
468: char *path;
2.36 frystyk 469: char *access = host-3;
2.31 frystyk 470:
2.36 frystyk 471: while (access>*filename && *(access-1)!='/') /* Find access method */
472: access--;
2.31 frystyk 473: if ((path = strchr(host, '/')) == NULL) /* Find path */
474: path = host + strlen(host);
475: if ((strptr = strchr(host, '@')) != NULL && strptr<path) /* UserId */
476: host = strptr;
2.39 frystyk 477: if ((port = strchr(host, ':')) != NULL && port>path) /* Port number */
478: port = NULL;
2.31 frystyk 479:
480: strptr = host; /* Convert to lower-case */
481: while (strptr<path) {
482: *strptr = TOLOWER(*strptr);
483: strptr++;
484: }
485:
486: /* Does the URL contain a full domain name? This also works for a
487: numerical host name. The domain name is already made lower-case
488: and without a trailing dot. */
2.35 frystyk 489: if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
490: strncasecomp(host, "localhost", 9)) {
2.31 frystyk 491: CONST char *domain = HTGetDomainName();
492: if (domain) {
2.32 frystyk 493: if ((newname = (char *) calloc(1, strlen(*filename) +
2.31 frystyk 494: strlen(domain)+2)) == NULL)
495: outofmem(__FILE__, "HTCanon");
496: if (port)
2.32 frystyk 497: strncpy(newname, *filename, (int) (port-*filename));
2.31 frystyk 498: else
2.32 frystyk 499: strncpy(newname, *filename, (int) (path-*filename));
500: strcat(newname, ".");
501: strcat(newname, domain);
2.31 frystyk 502: }
503: } else { /* Look for a trailing dot */
504: char *dot = port ? port : path;
505: if (dot > *filename && *--dot=='.') {
506: char *orig=dot, *dest=dot+1;
507: while((*orig++ = *dest++));
508: if (port) port--;
509: path--;
510: }
511: }
2.48 frystyk 512: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
2.36 frystyk 513: if (port) {
2.48 frystyk 514: if (!*(port+1) || *(port+1)=='/') {
515: if (!newname) {
516: char *orig=port, *dest=port+1;
517: while((*orig++ = *dest++));
518: }
519: } else if ((!strncmp(access, "http", 4) &&
2.36 frystyk 520: (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
521: (!strncmp(access, "gopher", 6) &&
522: (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
523: (!strncmp(access, "ftp", 3) &&
524: (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
525: if (!newname) {
526: char *orig=port, *dest=port+3;
527: while((*orig++ = *dest++));
528: }
529: } else if (newname)
530: strncat(newname, port, (int) (path-port));
531: }
532:
2.32 frystyk 533: if (newname) {
534: char *newpath = newname+strlen(newname);
535: strcat(newname, path);
2.31 frystyk 536: path = newpath;
537: free(*filename); /* Free old copy */
2.32 frystyk 538: *filename = newname;
2.31 frystyk 539: }
540: return path;
541: }
2.1 timbl 542:
543:
2.24 luotonen 544: /* HTCleanTelnetString()
545: * Make sure that the given string doesn't contain characters that
546: * could cause security holes, such as newlines in ftp, gopher,
547: * news or telnet URLs; more specifically: allows everything between
2.26 frystyk 548: * ASCII 20-7E, and also A0-FE, inclusive. Also TAB ('\t') allowed!
2.24 luotonen 549: *
550: * On entry,
551: * str the string that is *modified* if necessary. The
552: * string will be truncated at the first illegal
553: * character that is encountered.
554: * On exit,
555: * returns YES, if the string was modified.
556: * NO, otherwise.
557: */
558: PUBLIC BOOL HTCleanTelnetString ARGS1(char *, str)
559: {
560: char * cur = str;
561:
562: if (!str) return NO;
563:
564: while (*cur) {
565: int a = TOASCII(*cur);
2.26 frystyk 566: if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) || a > 0xFE)) {
2.31 frystyk 567: if (URI_TRACE)
2.44 frystyk 568: fprintf(TDEST, "Illegal..... character in URL: \"%s\"\n",str);
2.24 luotonen 569: *cur = 0;
2.31 frystyk 570: if (URI_TRACE)
2.44 frystyk 571: fprintf(TDEST, "Truncated... \"%s\"\n",str);
2.24 luotonen 572: return YES;
573: }
574: cur++;
575: }
576: return NO;
577: }
578:
Webmaster