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