Annotation of libwww/Library/src/HTParse.c, revision 2.47
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 {
94: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
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: }
232:
2.11 timbl 233:
2.21 frystyk 234: #if 0 /* NOT USED FOR THE MOMENT */
2.15 luotonen 235: /*
236: ** As strcpy() but guaranteed to work correctly
237: ** with overlapping parameters. AL 7 Feb 1994
238: */
239: PRIVATE void ari_strcpy ARGS2(char *, to,
240: char *, from)
241: {
242: char * tmp;
243:
244: if (!to || !from) return;
245:
246: tmp = (char*)malloc(strlen(from)+1);
247: if (!tmp) outofmem(__FILE__, "my_strcpy");
248:
249: strcpy(tmp, from);
250: strcpy(to, tmp);
251: free(tmp);
252: }
2.21 frystyk 253: #endif
254:
2.20 timbl 255:
256: /* Simplify a URI
257: // --------------
258: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1 timbl 259: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20 timbl 260: // Simplification helps us recognize duplicate URIs.
1.1 timbl 261: //
262: // Thus, /etc/junk/../fred becomes /etc/fred
263: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 264: //
265: // but we should NOT change
266: // http://fred.xxx.edu/../..
267: //
268: // or ../../albert.html
2.26 frystyk 269: //
270: // In the same manner, the following prefixed are preserved:
271: //
272: // ./<etc>
273: // //<etc>
274: //
275: // In order to avoid empty URLs the following URLs become:
276: //
277: // /fred/.. becomes /fred/..
278: // /fred/././.. becomes /fred/..
2.27 frystyk 279: // /fred/.././junk/.././ becomes /fred/..
2.26 frystyk 280: //
2.30 frystyk 281: // If more than one set of `://' is found (several proxies in cascade) then
282: // only the part after the last `://' is simplified.
2.44 frystyk 283: //
284: // Returns: A string which might be the old one or a new one.
1.1 timbl 285: */
2.45 frystyk 286: PUBLIC char *HTSimplify ARGS1(char **, filename)
1.1 timbl 287: {
2.31 frystyk 288: char *path;
289: char *p;
2.19 frystyk 290:
2.45 frystyk 291: if (!*filename) {
2.31 frystyk 292: if (URI_TRACE)
2.44 frystyk 293: fprintf(TDEST, "HTSimplify.. Bad argument\n");
2.45 frystyk 294: return *filename;
2.31 frystyk 295: }
296: if (URI_TRACE)
2.45 frystyk 297: fprintf(TDEST, "HTSimplify.. `%s\' ", *filename);
2.27 frystyk 298:
2.45 frystyk 299: if ((path = strstr(*filename, "://")) != NULL) { /* Find host name */
2.30 frystyk 300: char *newptr;
2.31 frystyk 301: path += 3;
302: while ((newptr = strstr(path, "://")) != NULL)
303: path = newptr+3;
2.45 frystyk 304: path = HTCanon(filename, path); /* We have a host name */
305: } else if ((path = strstr(*filename, ":/")) != NULL) {
2.31 frystyk 306: path += 2;
2.27 frystyk 307: } else
2.45 frystyk 308: path = *filename;
2.31 frystyk 309: if (*path == '/' && *(path+1)=='/') { /* Some URLs start //<foo> */
310: path += 1;
2.34 frystyk 311: } else if (!strncmp(path, "news:", 5)) {
312: char *ptr = strchr(path+5, '@');
313: if (!ptr) ptr = path+5;
314: while (*ptr) { /* Make group or host lower case */
315: *ptr = TOLOWER(*ptr);
316: ptr++;
2.31 frystyk 317: }
318: if (URI_TRACE)
2.45 frystyk 319: fprintf(TDEST, "into\n............ `%s'\n", *filename);
320: return *filename; /* Doesn't need to do any more */
2.31 frystyk 321: }
322: if ((p = path)) {
323: int segments = 0;
324:
325: /* Parse string first time to find number of `real' tokens */
326: while (*p) {
327: if (*p=='/' || p==path) {
328: if (!((*(p+1)=='/' || !*(p+1)) ||
329: (*(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) ||
330: (*(p+1)=='.' && *(p+2)=='.' &&(*(p+3)=='/' || !*(p+3)))))
331: segments++;
332: }
333: p++;
334: }
2.19 frystyk 335:
2.31 frystyk 336: /* Parse string second time to simplify */
337: p = path;
338: while(*p) {
339: if (*p=='/') {
340: if (p>path && *(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) {
341: char *orig=p, *dest=p+2;
342: while ((*orig++ = *dest++)); /* Remove a slash and a dot */
343: p--;
344: } else if (segments>1 && *(p+1)=='.' && *(p+2)=='.' &&
345: (*(p+3)=='/' || !*(p+3))) {
346: char *q = p;
347: while (q>path && *--q!='/'); /* prev slash */
348: if (strncmp(q, "/../", 4) && strncmp(q, "/./", 3) &&
349: strncmp(q, "./", 2)) {
350: char *orig=q, *dest=p+3;
351: if (*q!='/') dest++;
352: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
353: segments--;
354: p = q-1; /* Start again with prev slash */
355: } else
356: p++;
357: } else if (*(p+1)=='/') {
358: while (*(p+1)=='/') {
359: char *orig=p, *dest=p+1;
360: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2.19 frystyk 361: }
362: }
363: }
2.31 frystyk 364: p++;
365: } /* end while (*p) */
2.19 frystyk 366: }
2.31 frystyk 367: if (URI_TRACE)
2.45 frystyk 368: fprintf(TDEST, "into\n............ `%s'\n", *filename);
369: return *filename;
2.19 frystyk 370: }
2.31 frystyk 371:
2.19 frystyk 372: #ifdef OLD_CODE
2.17 frystyk 373: char * p = filename;
1.1 timbl 374: char * q;
2.17 frystyk 375:
376: if (p) {
377: while (*p && (*p == '/' || *p == '.')) /* Pass starting / or .'s */
378: p++;
379: while(*p) {
380: if (*p=='/') {
1.1 timbl 381: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 382: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
383: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
384: &&!(q-1>filename && q[-1]=='/')) {
2.15 luotonen 385: ari_strcpy(q, p+3); /* Remove /xxx/.. */
1.1 timbl 386: if (!*filename) strcpy(filename, "/");
387: p = q-1; /* Start again with prev slash */
2.11 timbl 388: } else { /* xxx/.. leave it! */
2.9 timbl 389: #ifdef BUG_CODE
2.15 luotonen 390: ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
1.1 timbl 391: p = filename; /* Start again */
2.9 timbl 392: #endif
1.1 timbl 393: }
394: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15 luotonen 395: ari_strcpy(p, p+2); /* Remove a slash and a dot */
2.13 luotonen 396: } else if (p[-1] != ':') {
397: while (p[1] == '/') {
2.15 luotonen 398: ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13 luotonen 399: }
1.1 timbl 400: }
2.17 frystyk 401: }
402: p++;
403: } /* end while (*p) */
404: } /* end if (p) */
1.1 timbl 405: }
2.19 frystyk 406: #endif /* OLD_CODE */
1.1 timbl 407:
408:
409: /* Make Relative Name
410: ** ------------------
411: **
412: ** This function creates and returns a string which gives an expression of
413: ** one address as related to another. Where there is no relation, an absolute
414: ** address is retured.
415: **
416: ** On entry,
417: ** Both names must be absolute, fully qualified names of nodes
418: ** (no anchor bits)
419: **
420: ** On exit,
421: ** The return result points to a newly allocated name which, if
422: ** parsed by HTParse relative to relatedName, will yield aName.
423: ** The caller is responsible for freeing the resulting name later.
424: **
425: */
2.32 frystyk 426: char * HTRelative ARGS2(CONST char *, aName, CONST char *, relatedName)
1.1 timbl 427: {
428: char * result = 0;
429: CONST char *p = aName;
430: CONST char *q = relatedName;
431: CONST char * after_access = 0;
432: CONST char * path = 0;
433: CONST char * last_slash = 0;
434: int slashes = 0;
435:
436: for(;*p; p++, q++) { /* Find extent of match */
437: if (*p!=*q) break;
438: if (*p==':') after_access = p+1;
439: if (*p=='/') {
440: last_slash = p;
441: slashes++;
442: if (slashes==3) path=p;
443: }
444: }
445:
446: /* q, p point to the first non-matching character or zero */
447:
448: if (!after_access) { /* Different access */
449: StrAllocCopy(result, aName);
450: } else if (slashes<3){ /* Different nodes */
451: StrAllocCopy(result, after_access);
2.29 frystyk 452: #if 0 /* Henrik */
1.1 timbl 453: } else if (slashes==3){ /* Same node, different path */
454: StrAllocCopy(result, path);
2.21 frystyk 455: #endif
1.1 timbl 456: } else { /* Some path in common */
457: int levels= 0;
458: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
459: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
460: if (result == NULL) outofmem(__FILE__, "HTRelative");
461: result[0]=0;
462: for(;levels; levels--)strcat(result, "../");
463: strcat(result, last_slash+1);
464: }
2.44 frystyk 465: if (URI_TRACE) fprintf(TDEST,
2.21 frystyk 466: "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
467: aName, relatedName, result);
1.1 timbl 468: return result;
469: }
2.1 timbl 470:
471:
2.31 frystyk 472: /* HTCanon
473: **
474: ** Canonicalizes the URL in the following manner starting from the host
475: ** pointer:
476: **
477: ** 1) The host name is converted to lowercase
478: ** 2) Expands the host name of the URL from a local name to a full
479: ** domain name. A host name is started by `://'.
2.38 frystyk 480: ** 3) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
2.31 frystyk 481: **
482: ** Return: OK The position of the current path part of the URL
2.44 frystyk 483: ** which might be the old one or a new one.
2.31 frystyk 484: */
485: PUBLIC char *HTCanon ARGS2 (char **, filename, char *, host)
486: {
2.32 frystyk 487: char *newname = NULL;
2.31 frystyk 488: char *port;
489: char *strptr;
490: char *path;
2.36 frystyk 491: char *access = host-3;
2.31 frystyk 492:
2.36 frystyk 493: while (access>*filename && *(access-1)!='/') /* Find access method */
494: access--;
2.31 frystyk 495: if ((path = strchr(host, '/')) == NULL) /* Find path */
496: path = host + strlen(host);
497: if ((strptr = strchr(host, '@')) != NULL && strptr<path) /* UserId */
498: host = strptr;
2.39 frystyk 499: if ((port = strchr(host, ':')) != NULL && port>path) /* Port number */
500: port = NULL;
2.31 frystyk 501:
502: strptr = host; /* Convert to lower-case */
503: while (strptr<path) {
504: *strptr = TOLOWER(*strptr);
505: strptr++;
506: }
507:
508: /* Does the URL contain a full domain name? This also works for a
509: numerical host name. The domain name is already made lower-case
510: and without a trailing dot. */
2.35 frystyk 511: if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
512: strncasecomp(host, "localhost", 9)) {
2.31 frystyk 513: CONST char *domain = HTGetDomainName();
514: if (domain) {
2.32 frystyk 515: if ((newname = (char *) calloc(1, strlen(*filename) +
2.31 frystyk 516: strlen(domain)+2)) == NULL)
517: outofmem(__FILE__, "HTCanon");
518: if (port)
2.32 frystyk 519: strncpy(newname, *filename, (int) (port-*filename));
2.31 frystyk 520: else
2.32 frystyk 521: strncpy(newname, *filename, (int) (path-*filename));
522: strcat(newname, ".");
523: strcat(newname, domain);
2.31 frystyk 524: }
525: } else { /* Look for a trailing dot */
526: char *dot = port ? port : path;
527: if (dot > *filename && *--dot=='.') {
528: char *orig=dot, *dest=dot+1;
529: while((*orig++ = *dest++));
530: if (port) port--;
531: path--;
532: }
533: }
2.36 frystyk 534: /* Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp) */
535: if (port) {
536: if ((!strncmp(access, "http", 4) &&
537: (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
538: (!strncmp(access, "gopher", 6) &&
539: (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
540: (!strncmp(access, "ftp", 3) &&
541: (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
542: if (!newname) {
543: char *orig=port, *dest=port+3;
544: while((*orig++ = *dest++));
545: }
546: } else if (newname)
547: strncat(newname, port, (int) (path-port));
548: }
549:
2.32 frystyk 550: if (newname) {
551: char *newpath = newname+strlen(newname);
552: strcat(newname, path);
2.31 frystyk 553: path = newpath;
554: free(*filename); /* Free old copy */
2.32 frystyk 555: *filename = newname;
2.31 frystyk 556: }
557: return path;
558: }
2.1 timbl 559:
560:
2.24 luotonen 561: /* HTCleanTelnetString()
562: * Make sure that the given string doesn't contain characters that
563: * could cause security holes, such as newlines in ftp, gopher,
564: * news or telnet URLs; more specifically: allows everything between
2.26 frystyk 565: * ASCII 20-7E, and also A0-FE, inclusive. Also TAB ('\t') allowed!
2.24 luotonen 566: *
567: * On entry,
568: * str the string that is *modified* if necessary. The
569: * string will be truncated at the first illegal
570: * character that is encountered.
571: * On exit,
572: * returns YES, if the string was modified.
573: * NO, otherwise.
574: */
575: PUBLIC BOOL HTCleanTelnetString ARGS1(char *, str)
576: {
577: char * cur = str;
578:
579: if (!str) return NO;
580:
581: while (*cur) {
582: int a = TOASCII(*cur);
2.26 frystyk 583: if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) || a > 0xFE)) {
2.31 frystyk 584: if (URI_TRACE)
2.44 frystyk 585: fprintf(TDEST, "Illegal..... character in URL: \"%s\"\n",str);
2.24 luotonen 586: *cur = 0;
2.31 frystyk 587: if (URI_TRACE)
2.44 frystyk 588: fprintf(TDEST, "Truncated... \"%s\"\n",str);
2.24 luotonen 589: return YES;
590: }
591: cur++;
592: }
593: return NO;
594: }
595:
Webmaster