Annotation of libwww/Library/src/HTParse.c, revision 2.60
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.60 ! frystyk 38: PRIVATE void scan (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.60 ! frystyk 114: PUBLIC char * HTParse (CONST char *aName, CONST char *relatedName, int wanted)
1.1 timbl 115: {
116: char * result = 0;
117: char * return_value = 0;
118: int len;
119: char * name = 0;
120: char * rel = 0;
121: char * p;
2.12 timbl 122: char * access;
1.1 timbl 123: struct struct_parts given, related;
2.33 howcome 124:
125: if (!relatedName) /* HWL 23/8/94: dont dump due to NULL */
126: relatedName = "";
1.1 timbl 127:
2.50 frystyk 128: /* Make working copies of input strings to cut up: */
1.1 timbl 129: len = strlen(aName)+strlen(relatedName)+10;
130: result=(char *)malloc(len); /* Lots of space: more than enough */
131: if (result == NULL) outofmem(__FILE__, "HTParse");
132:
133: StrAllocCopy(name, aName);
134: StrAllocCopy(rel, relatedName);
135:
136: scan(name, &given);
137: scan(rel, &related);
138: result[0]=0; /* Clear string */
2.12 timbl 139: access = given.access ? given.access : related.access;
1.1 timbl 140: if (wanted & PARSE_ACCESS)
2.12 timbl 141: if (access) {
142: strcat(result, access);
1.1 timbl 143: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
144: }
145:
146: if (given.access && related.access) /* If different, inherit nothing. */
147: if (strcmp(given.access, related.access)!=0) {
148: related.host=0;
149: related.absolute=0;
150: related.relative=0;
151: related.anchor=0;
152: }
153:
154: if (wanted & PARSE_HOST)
155: if(given.host || related.host) {
156: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
157: strcat(result, given.host ? given.host : related.host);
158: }
159:
160: if (given.host && related.host) /* If different hosts, inherit no path. */
161: if (strcmp(given.host, related.host)!=0) {
162: related.absolute=0;
163: related.relative=0;
164: related.anchor=0;
165: }
166:
167: if (wanted & PARSE_PATH) {
168: if(given.absolute) { /* All is given */
169: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
170: strcat(result, given.absolute);
171: } else if(related.absolute) { /* Adopt path not name */
172: strcat(result, "/");
173: strcat(result, related.absolute);
174: if (given.relative) {
175: p = strchr(result, '?'); /* Search part? */
176: if (!p) p=result+strlen(result)-1;
177: for (; *p!='/'; p--); /* last / */
178: p[1]=0; /* Remove filename */
179: strcat(result, given.relative); /* Add given one */
2.55 frystyk 180: #if 0
2.45 frystyk 181: result = HTSimplify (&result);
2.55 frystyk 182: #endif
1.1 timbl 183: }
184: } else if(given.relative) {
185: strcat(result, given.relative); /* what we've got */
186: } else if(related.relative) {
187: strcat(result, related.relative);
188: } else { /* No inheritance */
189: strcat(result, "/");
190: }
191: }
192:
193: if (wanted & PARSE_ANCHOR)
2.58 neon 194: if(given.anchor || related.anchor) {
195: if(given.absolute && given.anchor) { /*Fixes for relURLs...*/
196: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
197: strcat(result, given.anchor);
198: } else if (!(given.absolute) && !(given.anchor)) {
199: strcat(result, "");
200: } else {
201: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
202: strcat(result, given.anchor ? given.anchor : related.anchor);
203: }
1.1 timbl 204: }
205: free(rel);
206: free(name);
207:
208: StrAllocCopy(return_value, result);
209: free(result);
210: return return_value; /* exactly the right length */
211: }
2.21 frystyk 212:
2.20 timbl 213:
2.53 frystyk 214: /* HTCanon
215: **
216: ** Canonicalizes the URL in the following manner starting from the host
217: ** pointer:
218: **
219: ** 1) The host name is converted to lowercase
220: ** 2) Expands the host name of the URL from a local name to a full
221: ** domain name. A host name is started by `://'.
222: ** 3) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
223: **
224: ** Return: OK The position of the current path part of the URL
225: ** which might be the old one or a new one.
226: */
2.60 ! frystyk 227: PRIVATE char *HTCanon (char ** filename, char * host)
2.53 frystyk 228: {
229: char *newname = NULL;
230: char *port;
231: char *strptr;
232: char *path;
233: char *access = host-3;
234:
235: while (access>*filename && *(access-1)!='/') /* Find access method */
236: access--;
237: if ((path = strchr(host, '/')) == NULL) /* Find path */
238: path = host + strlen(host);
239: if ((strptr = strchr(host, '@')) != NULL && strptr<path) /* UserId */
240: host = strptr;
241: if ((port = strchr(host, ':')) != NULL && port>path) /* Port number */
242: port = NULL;
243:
244: strptr = host; /* Convert to lower-case */
245: while (strptr<path) {
246: *strptr = TOLOWER(*strptr);
247: strptr++;
248: }
249:
250: /* Does the URL contain a full domain name? This also works for a
251: numerical host name. The domain name is already made lower-case
252: and without a trailing dot. */
253: if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
254: strncasecomp(host, "localhost", 9)) {
255: CONST char *domain = HTGetDomainName();
256: if (domain && *domain) {
257: if ((newname = (char *) calloc(1, strlen(*filename) +
258: strlen(domain)+2)) == NULL)
259: outofmem(__FILE__, "HTCanon");
260: if (port)
261: strncpy(newname, *filename, (int) (port-*filename));
262: else
263: strncpy(newname, *filename, (int) (path-*filename));
264: strcat(newname, ".");
265: strcat(newname, domain);
266: }
267: } else { /* Look for a trailing dot */
268: char *dot = port ? port : path;
269: if (dot > *filename && *--dot=='.') {
270: char *orig=dot, *dest=dot+1;
271: while((*orig++ = *dest++));
272: if (port) port--;
273: path--;
274: }
275: }
276: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
277: if (port) {
278: if (!*(port+1) || *(port+1)=='/') {
279: if (!newname) {
280: char *orig=port, *dest=port+1;
281: while((*orig++ = *dest++));
282: }
283: } else if ((!strncmp(access, "http", 4) &&
284: (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
285: (!strncmp(access, "gopher", 6) &&
286: (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
287: (!strncmp(access, "ftp", 3) &&
288: (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
289: if (!newname) {
290: char *orig=port, *dest=port+3;
291: while((*orig++ = *dest++));
2.54 frystyk 292: path -= 3; /* Update path position, Henry Minsky */
2.53 frystyk 293: }
294: } else if (newname)
295: strncat(newname, port, (int) (path-port));
296: }
297:
298: if (newname) {
299: char *newpath = newname+strlen(newname);
300: strcat(newname, path);
301: path = newpath;
302: free(*filename); /* Free old copy */
303: *filename = newname;
304: }
305: return path;
306: }
307:
2.20 timbl 308: /* Simplify a URI
309: // --------------
310: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1 timbl 311: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20 timbl 312: // Simplification helps us recognize duplicate URIs.
1.1 timbl 313: //
314: // Thus, /etc/junk/../fred becomes /etc/fred
315: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 316: //
317: // but we should NOT change
318: // http://fred.xxx.edu/../..
319: //
320: // or ../../albert.html
2.26 frystyk 321: //
322: // In order to avoid empty URLs the following URLs become:
323: //
324: // /fred/.. becomes /fred/..
325: // /fred/././.. becomes /fred/..
2.27 frystyk 326: // /fred/.././junk/.././ becomes /fred/..
2.26 frystyk 327: //
2.30 frystyk 328: // If more than one set of `://' is found (several proxies in cascade) then
329: // only the part after the last `://' is simplified.
2.44 frystyk 330: //
331: // Returns: A string which might be the old one or a new one.
1.1 timbl 332: */
2.60 ! frystyk 333: PUBLIC char *HTSimplify (char ** url)
1.1 timbl 334: {
2.31 frystyk 335: char *path;
336: char *p;
2.55 frystyk 337: if (!url || !*url) {
2.59 frystyk 338: if (URI_TRACE) TTYPrint(TDEST, "HTSimplify.. Nothing done\n");
2.55 frystyk 339: return *url;
2.31 frystyk 340: }
2.59 frystyk 341: if (URI_TRACE) TTYPrint(TDEST, "HTSimplify.. `%s\' ", *url);
2.27 frystyk 342:
2.55 frystyk 343: /* Find any scheme name */
344: if ((path = strstr(*url, "://")) != NULL) { /* Find host name */
2.30 frystyk 345: char *newptr;
2.60 ! frystyk 346: char *access = *url;
! 347: while (access<path && (*access=TOLOWER(*access))) access++;
2.31 frystyk 348: path += 3;
2.60 ! frystyk 349: while ((newptr = strstr(path, "://")) != NULL) /* For proxies */
2.31 frystyk 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.60 ! frystyk 466: PUBLIC char * HTRelative (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: */
2.60 ! frystyk 521: PUBLIC BOOL HTCleanTelnetString (char * str)
2.24 luotonen 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