Annotation of libwww/Library/src/HTParse.c, revision 2.51
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.45 frystyk 181: result = HTSimplify (&result);
1.1 timbl 182: }
183: } else if(given.relative) {
184: strcat(result, given.relative); /* what we've got */
185: } else if(related.relative) {
186: strcat(result, related.relative);
187: } else { /* No inheritance */
188: strcat(result, "/");
189: }
190: }
191:
192: if (wanted & PARSE_ANCHOR)
193: if(given.anchor || related.anchor) {
194: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
195: strcat(result, given.anchor ? given.anchor : related.anchor);
196: }
197: free(rel);
198: free(name);
199:
200: StrAllocCopy(return_value, result);
201: free(result);
202: return return_value; /* exactly the right length */
203: }
2.21 frystyk 204:
2.20 timbl 205:
206: /* Simplify a URI
207: // --------------
208: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1 timbl 209: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20 timbl 210: // Simplification helps us recognize duplicate URIs.
1.1 timbl 211: //
212: // Thus, /etc/junk/../fred becomes /etc/fred
213: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 214: //
215: // but we should NOT change
216: // http://fred.xxx.edu/../..
217: //
218: // or ../../albert.html
2.26 frystyk 219: //
220: // In the same manner, the following prefixed are preserved:
221: //
222: // ./<etc>
223: // //<etc>
224: //
225: // In order to avoid empty URLs the following URLs become:
226: //
227: // /fred/.. becomes /fred/..
228: // /fred/././.. becomes /fred/..
2.27 frystyk 229: // /fred/.././junk/.././ becomes /fred/..
2.26 frystyk 230: //
2.30 frystyk 231: // If more than one set of `://' is found (several proxies in cascade) then
232: // only the part after the last `://' is simplified.
2.44 frystyk 233: //
234: // Returns: A string which might be the old one or a new one.
1.1 timbl 235: */
2.45 frystyk 236: PUBLIC char *HTSimplify ARGS1(char **, filename)
1.1 timbl 237: {
2.31 frystyk 238: char *path;
239: char *p;
2.19 frystyk 240:
2.45 frystyk 241: if (!*filename) {
2.31 frystyk 242: if (URI_TRACE)
2.44 frystyk 243: fprintf(TDEST, "HTSimplify.. Bad argument\n");
2.45 frystyk 244: return *filename;
2.31 frystyk 245: }
246: if (URI_TRACE)
2.45 frystyk 247: fprintf(TDEST, "HTSimplify.. `%s\' ", *filename);
2.27 frystyk 248:
2.45 frystyk 249: if ((path = strstr(*filename, "://")) != NULL) { /* Find host name */
2.30 frystyk 250: char *newptr;
2.31 frystyk 251: path += 3;
252: while ((newptr = strstr(path, "://")) != NULL)
253: path = newptr+3;
2.45 frystyk 254: path = HTCanon(filename, path); /* We have a host name */
255: } else if ((path = strstr(*filename, ":/")) != NULL) {
2.31 frystyk 256: path += 2;
2.27 frystyk 257: } else
2.45 frystyk 258: path = *filename;
2.31 frystyk 259: if (*path == '/' && *(path+1)=='/') { /* Some URLs start //<foo> */
260: path += 1;
2.34 frystyk 261: } else if (!strncmp(path, "news:", 5)) {
262: char *ptr = strchr(path+5, '@');
263: if (!ptr) ptr = path+5;
264: while (*ptr) { /* Make group or host lower case */
265: *ptr = TOLOWER(*ptr);
266: ptr++;
2.31 frystyk 267: }
268: if (URI_TRACE)
2.45 frystyk 269: fprintf(TDEST, "into\n............ `%s'\n", *filename);
270: return *filename; /* Doesn't need to do any more */
2.31 frystyk 271: }
272: if ((p = path)) {
273: int segments = 0;
274:
275: /* Parse string first time to find number of `real' tokens */
276: while (*p) {
277: if (*p=='/' || p==path) {
278: if (!((*(p+1)=='/' || !*(p+1)) ||
279: (*(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) ||
280: (*(p+1)=='.' && *(p+2)=='.' &&(*(p+3)=='/' || !*(p+3)))))
281: segments++;
282: }
283: p++;
284: }
2.19 frystyk 285:
2.31 frystyk 286: /* Parse string second time to simplify */
287: p = path;
288: while(*p) {
289: if (*p=='/') {
290: if (p>path && *(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) {
291: char *orig=p, *dest=p+2;
292: while ((*orig++ = *dest++)); /* Remove a slash and a dot */
293: p--;
294: } else if (segments>1 && *(p+1)=='.' && *(p+2)=='.' &&
295: (*(p+3)=='/' || !*(p+3))) {
296: char *q = p;
297: while (q>path && *--q!='/'); /* prev slash */
298: if (strncmp(q, "/../", 4) && strncmp(q, "/./", 3) &&
299: strncmp(q, "./", 2)) {
300: char *orig=q, *dest=p+3;
301: if (*q!='/') dest++;
302: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
303: segments--;
304: p = q-1; /* Start again with prev slash */
305: } else
306: p++;
307: } else if (*(p+1)=='/') {
308: while (*(p+1)=='/') {
309: char *orig=p, *dest=p+1;
310: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2.19 frystyk 311: }
312: }
313: }
2.31 frystyk 314: p++;
315: } /* end while (*p) */
2.19 frystyk 316: }
2.31 frystyk 317: if (URI_TRACE)
2.45 frystyk 318: fprintf(TDEST, "into\n............ `%s'\n", *filename);
319: return *filename;
2.19 frystyk 320: }
2.31 frystyk 321:
2.19 frystyk 322: #ifdef OLD_CODE
2.17 frystyk 323: char * p = filename;
1.1 timbl 324: char * q;
2.17 frystyk 325:
326: if (p) {
327: while (*p && (*p == '/' || *p == '.')) /* Pass starting / or .'s */
328: p++;
329: while(*p) {
330: if (*p=='/') {
1.1 timbl 331: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 332: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
333: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
334: &&!(q-1>filename && q[-1]=='/')) {
2.15 luotonen 335: ari_strcpy(q, p+3); /* Remove /xxx/.. */
1.1 timbl 336: if (!*filename) strcpy(filename, "/");
337: p = q-1; /* Start again with prev slash */
2.11 timbl 338: } else { /* xxx/.. leave it! */
2.9 timbl 339: #ifdef BUG_CODE
2.15 luotonen 340: ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
1.1 timbl 341: p = filename; /* Start again */
2.9 timbl 342: #endif
1.1 timbl 343: }
344: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15 luotonen 345: ari_strcpy(p, p+2); /* Remove a slash and a dot */
2.13 luotonen 346: } else if (p[-1] != ':') {
347: while (p[1] == '/') {
2.15 luotonen 348: ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13 luotonen 349: }
1.1 timbl 350: }
2.17 frystyk 351: }
352: p++;
353: } /* end while (*p) */
354: } /* end if (p) */
1.1 timbl 355: }
2.19 frystyk 356: #endif /* OLD_CODE */
1.1 timbl 357:
358:
359: /* Make Relative Name
360: ** ------------------
361: **
362: ** This function creates and returns a string which gives an expression of
363: ** one address as related to another. Where there is no relation, an absolute
364: ** address is retured.
365: **
366: ** On entry,
367: ** Both names must be absolute, fully qualified names of nodes
368: ** (no anchor bits)
369: **
370: ** On exit,
371: ** The return result points to a newly allocated name which, if
372: ** parsed by HTParse relative to relatedName, will yield aName.
373: ** The caller is responsible for freeing the resulting name later.
374: **
375: */
2.32 frystyk 376: char * HTRelative ARGS2(CONST char *, aName, CONST char *, relatedName)
1.1 timbl 377: {
378: char * result = 0;
379: CONST char *p = aName;
380: CONST char *q = relatedName;
381: CONST char * after_access = 0;
382: CONST char * path = 0;
383: CONST char * last_slash = 0;
384: int slashes = 0;
385:
386: for(;*p; p++, q++) { /* Find extent of match */
387: if (*p!=*q) break;
388: if (*p==':') after_access = p+1;
389: if (*p=='/') {
390: last_slash = p;
391: slashes++;
392: if (slashes==3) path=p;
393: }
394: }
395:
396: /* q, p point to the first non-matching character or zero */
397:
398: if (!after_access) { /* Different access */
399: StrAllocCopy(result, aName);
400: } else if (slashes<3){ /* Different nodes */
401: StrAllocCopy(result, after_access);
402: } else { /* Some path in common */
403: int levels= 0;
404: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
405: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
406: if (result == NULL) outofmem(__FILE__, "HTRelative");
407: result[0]=0;
408: for(;levels; levels--)strcat(result, "../");
409: strcat(result, last_slash+1);
410: }
2.44 frystyk 411: if (URI_TRACE) fprintf(TDEST,
2.21 frystyk 412: "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
413: aName, relatedName, result);
1.1 timbl 414: return result;
415: }
2.1 timbl 416:
417:
2.31 frystyk 418: /* HTCanon
419: **
420: ** Canonicalizes the URL in the following manner starting from the host
421: ** pointer:
422: **
423: ** 1) The host name is converted to lowercase
424: ** 2) Expands the host name of the URL from a local name to a full
425: ** domain name. A host name is started by `://'.
2.38 frystyk 426: ** 3) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
2.31 frystyk 427: **
428: ** Return: OK The position of the current path part of the URL
2.44 frystyk 429: ** which might be the old one or a new one.
2.31 frystyk 430: */
431: PUBLIC char *HTCanon ARGS2 (char **, filename, char *, host)
432: {
2.32 frystyk 433: char *newname = NULL;
2.31 frystyk 434: char *port;
435: char *strptr;
436: char *path;
2.36 frystyk 437: char *access = host-3;
2.31 frystyk 438:
2.36 frystyk 439: while (access>*filename && *(access-1)!='/') /* Find access method */
440: access--;
2.31 frystyk 441: if ((path = strchr(host, '/')) == NULL) /* Find path */
442: path = host + strlen(host);
443: if ((strptr = strchr(host, '@')) != NULL && strptr<path) /* UserId */
444: host = strptr;
2.39 frystyk 445: if ((port = strchr(host, ':')) != NULL && port>path) /* Port number */
446: port = NULL;
2.31 frystyk 447:
448: strptr = host; /* Convert to lower-case */
449: while (strptr<path) {
450: *strptr = TOLOWER(*strptr);
451: strptr++;
452: }
453:
454: /* Does the URL contain a full domain name? This also works for a
455: numerical host name. The domain name is already made lower-case
456: and without a trailing dot. */
2.35 frystyk 457: if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
458: strncasecomp(host, "localhost", 9)) {
2.31 frystyk 459: CONST char *domain = HTGetDomainName();
2.51 ! frystyk 460: if (domain && *domain) {
2.32 frystyk 461: if ((newname = (char *) calloc(1, strlen(*filename) +
2.31 frystyk 462: strlen(domain)+2)) == NULL)
463: outofmem(__FILE__, "HTCanon");
464: if (port)
2.32 frystyk 465: strncpy(newname, *filename, (int) (port-*filename));
2.31 frystyk 466: else
2.32 frystyk 467: strncpy(newname, *filename, (int) (path-*filename));
468: strcat(newname, ".");
469: strcat(newname, domain);
2.31 frystyk 470: }
471: } else { /* Look for a trailing dot */
472: char *dot = port ? port : path;
473: if (dot > *filename && *--dot=='.') {
474: char *orig=dot, *dest=dot+1;
475: while((*orig++ = *dest++));
476: if (port) port--;
477: path--;
478: }
479: }
2.48 frystyk 480: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
2.36 frystyk 481: if (port) {
2.48 frystyk 482: if (!*(port+1) || *(port+1)=='/') {
483: if (!newname) {
484: char *orig=port, *dest=port+1;
485: while((*orig++ = *dest++));
486: }
487: } else if ((!strncmp(access, "http", 4) &&
2.36 frystyk 488: (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
489: (!strncmp(access, "gopher", 6) &&
490: (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
491: (!strncmp(access, "ftp", 3) &&
492: (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
493: if (!newname) {
494: char *orig=port, *dest=port+3;
495: while((*orig++ = *dest++));
496: }
497: } else if (newname)
498: strncat(newname, port, (int) (path-port));
499: }
500:
2.32 frystyk 501: if (newname) {
502: char *newpath = newname+strlen(newname);
503: strcat(newname, path);
2.31 frystyk 504: path = newpath;
505: free(*filename); /* Free old copy */
2.32 frystyk 506: *filename = newname;
2.31 frystyk 507: }
508: return path;
509: }
2.1 timbl 510:
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: */
526: PUBLIC BOOL HTCleanTelnetString ARGS1(char *, str)
527: {
528: char * cur = str;
529:
530: if (!str) return NO;
531:
532: while (*cur) {
533: int a = TOASCII(*cur);
2.26 frystyk 534: if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) || a > 0xFE)) {
2.31 frystyk 535: if (URI_TRACE)
2.44 frystyk 536: fprintf(TDEST, "Illegal..... character in URL: \"%s\"\n",str);
2.24 luotonen 537: *cur = 0;
2.31 frystyk 538: if (URI_TRACE)
2.44 frystyk 539: fprintf(TDEST, "Truncated... \"%s\"\n",str);
2.24 luotonen 540: return YES;
541: }
542: cur++;
543: }
544: return NO;
545: }
546:
Webmaster