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