Annotation of libwww/Library/src/HTParse.c, revision 2.67.2.1
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.67.2.1! frystyk 6: ** @(#) $Id: HTParse.c,v 2.67 1996/10/07 02:05:08 frystyk Exp $
2.26 frystyk 7: **
8: ** history:
9: ** May 12 94 TAB added as legal char in HTCleanTelnetString
10: **
1.1 timbl 11: */
2.31 frystyk 12:
2.44 frystyk 13: /* Library include files */
2.63 frystyk 14: #include "sysdep.h"
2.65 frystyk 15: #include "WWWUtil.h"
16: #include "HTParse.h" /* Implemented here */
2.6 timbl 17:
2.66 frystyk 18: typedef struct _HTURI {
19: char * access; /* Now known as "scheme" */
20: char * host;
21: char * absolute;
22: char * relative;
23: char * fragment;
24: } HTURI;
1.1 timbl 25:
26: /* Scan a filename for its consituents
27: ** -----------------------------------
28: **
29: ** On entry,
30: ** name points to a document name which may be incomplete.
31: ** On exit,
32: ** absolute or relative may be nonzero (but not both).
2.66 frystyk 33: ** host, fragment and access may be nonzero if they were specified.
1.1 timbl 34: ** Any which are nonzero point to zero terminated strings.
35: */
2.66 frystyk 36: PRIVATE void scan (char * name, HTURI * parts)
1.1 timbl 37: {
38: char * p;
2.66 frystyk 39: char * after_access = name;
40: memset(parts, '\0', sizeof(HTURI));
41:
42: /* Look for fragment identifier */
43: if ((p = strrchr(name, '#')) != NULL) {
44: *p++ = '\0';
45: parts->fragment = p;
46: }
1.1 timbl 47:
48: for(p=name; *p; p++) {
2.66 frystyk 49: if (*p=='/' || *p=='#' || *p=='?')
50: break;
1.1 timbl 51: if (*p==':') {
52: *p = 0;
2.20 timbl 53: parts->access = after_access; /* Scheme has been specified */
2.37 howcome 54:
2.44 frystyk 55: /* The combination of gcc, the "-O" flag and the HP platform is
56: unhealthy. The following three lines is a quick & dirty fix, but is
57: not recommended. Rather, turn off "-O". */
58:
2.42 howcome 59: /* after_access = p;*/
2.44 frystyk 60: /* while (*after_access == 0)*/
61: /* after_access++;*/
2.42 howcome 62:
1.1 timbl 63: after_access = p+1;
2.37 howcome 64:
2.22 luotonen 65: if (0==strcasecomp("URL", parts->access)) {
2.20 timbl 66: parts->access = NULL; /* Ignore IETF's URL: pre-prefix */
67: } else break;
1.1 timbl 68: }
69: }
70:
71: p = after_access;
72: if (*p=='/'){
73: if (p[1]=='/') {
74: parts->host = p+2; /* host has been specified */
75: *p=0; /* Terminate access */
76: p=strchr(parts->host,'/'); /* look for end of host name if any */
77: if(p) {
78: *p=0; /* Terminate host */
79: parts->absolute = p+1; /* Root has been found */
80: }
81: } else {
82: parts->absolute = p+1; /* Root found but no host */
83: }
84: } else {
2.49 frystyk 85: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.1 timbl 86: }
2.50 frystyk 87: }
1.1 timbl 88:
89:
90: /* Parse a Name relative to another name
91: ** -------------------------------------
92: **
93: ** This returns those parts of a name which are given (and requested)
94: ** substituting bits from the related name where necessary.
95: **
96: ** On entry,
97: ** aName A filename given
2.33 howcome 98: ** relatedName A name relative to which aName is to be parsed. Give
99: ** it an empty string if aName is absolute.
1.1 timbl 100: ** wanted A mask for the bits which are wanted.
101: **
102: ** On exit,
103: ** returns A pointer to a malloc'd string which MUST BE FREED
104: */
2.63 frystyk 105: PUBLIC char * HTParse (const char *aName, const char *relatedName, int wanted)
1.1 timbl 106: {
107: char * result = 0;
108: char * return_value = 0;
109: int len;
110: char * name = 0;
111: char * rel = 0;
112: char * p;
2.12 timbl 113: char * access;
2.66 frystyk 114: HTURI given, related;
2.33 howcome 115:
116: if (!relatedName) /* HWL 23/8/94: dont dump due to NULL */
117: relatedName = "";
1.1 timbl 118:
2.50 frystyk 119: /* Make working copies of input strings to cut up: */
1.1 timbl 120: len = strlen(aName)+strlen(relatedName)+10;
2.61 frystyk 121: if ((result=(char *) HT_MALLOC(len)) == NULL) /* Lots of space: more than enough */
122: HT_OUTOFMEM("parse space");
1.1 timbl 123: StrAllocCopy(name, aName);
124: StrAllocCopy(rel, relatedName);
125:
126: scan(name, &given);
127: scan(rel, &related);
128: result[0]=0; /* Clear string */
2.12 timbl 129: access = given.access ? given.access : related.access;
1.1 timbl 130: if (wanted & PARSE_ACCESS)
2.12 timbl 131: if (access) {
132: strcat(result, access);
1.1 timbl 133: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
134: }
135:
136: if (given.access && related.access) /* If different, inherit nothing. */
137: if (strcmp(given.access, related.access)!=0) {
138: related.host=0;
139: related.absolute=0;
140: related.relative=0;
2.66 frystyk 141: related.fragment=0;
1.1 timbl 142: }
143:
144: if (wanted & PARSE_HOST)
145: if(given.host || related.host) {
146: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
147: strcat(result, given.host ? given.host : related.host);
148: }
149:
150: if (given.host && related.host) /* If different hosts, inherit no path. */
151: if (strcmp(given.host, related.host)!=0) {
152: related.absolute=0;
153: related.relative=0;
2.66 frystyk 154: related.fragment=0;
1.1 timbl 155: }
156:
157: if (wanted & PARSE_PATH) {
158: if(given.absolute) { /* All is given */
159: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
160: strcat(result, given.absolute);
161: } else if(related.absolute) { /* Adopt path not name */
162: strcat(result, "/");
163: strcat(result, related.absolute);
164: if (given.relative) {
165: p = strchr(result, '?'); /* Search part? */
166: if (!p) p=result+strlen(result)-1;
167: for (; *p!='/'; p--); /* last / */
168: p[1]=0; /* Remove filename */
169: strcat(result, given.relative); /* Add given one */
2.55 frystyk 170: #if 0
2.45 frystyk 171: result = HTSimplify (&result);
2.55 frystyk 172: #endif
1.1 timbl 173: }
174: } else if(given.relative) {
175: strcat(result, given.relative); /* what we've got */
176: } else if(related.relative) {
177: strcat(result, related.relative);
178: } else { /* No inheritance */
179: strcat(result, "/");
180: }
181: }
182:
183: if (wanted & PARSE_ANCHOR)
2.66 frystyk 184: if(given.fragment || related.fragment) {
185: if(given.absolute && given.fragment) { /*Fixes for relURLs...*/
2.58 neon 186: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
2.66 frystyk 187: strcat(result, given.fragment);
188: } else if (!(given.absolute) && !(given.fragment)) {
2.58 neon 189: strcat(result, "");
190: } else {
191: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
2.66 frystyk 192: strcat(result, given.fragment ? given.fragment : related.fragment);
2.58 neon 193: }
1.1 timbl 194: }
2.61 frystyk 195: HT_FREE(rel);
196: HT_FREE(name);
1.1 timbl 197:
198: StrAllocCopy(return_value, result);
2.61 frystyk 199: HT_FREE(result);
1.1 timbl 200: return return_value; /* exactly the right length */
201: }
2.21 frystyk 202:
2.20 timbl 203:
2.65 frystyk 204: /*
2.53 frystyk 205: ** Canonicalizes the URL in the following manner starting from the host
206: ** pointer:
207: **
208: ** 1) The host name is converted to lowercase
2.65 frystyk 209: ** 2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
2.53 frystyk 210: **
211: ** Return: OK The position of the current path part of the URL
212: ** which might be the old one or a new one.
213: */
2.65 frystyk 214: PRIVATE char * HTCanon (char ** filename, char * host)
2.53 frystyk 215: {
216: char *newname = NULL;
217: char *port;
218: char *strptr;
219: char *path;
220: char *access = host-3;
221:
222: while (access>*filename && *(access-1)!='/') /* Find access method */
223: access--;
224: if ((path = strchr(host, '/')) == NULL) /* Find path */
225: path = host + strlen(host);
226: if ((strptr = strchr(host, '@')) != NULL && strptr<path) /* UserId */
227: host = strptr;
228: if ((port = strchr(host, ':')) != NULL && port>path) /* Port number */
229: port = NULL;
230:
231: strptr = host; /* Convert to lower-case */
232: while (strptr<path) {
233: *strptr = TOLOWER(*strptr);
234: strptr++;
235: }
236:
237: /* Does the URL contain a full domain name? This also works for a
238: numerical host name. The domain name is already made lower-case
239: and without a trailing dot. */
2.65 frystyk 240: #if 0
2.53 frystyk 241: if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
242: strncasecomp(host, "localhost", 9)) {
2.63 frystyk 243: const char *domain = HTGetDomainName();
2.53 frystyk 244: if (domain && *domain) {
2.61 frystyk 245: if ((newname = (char *) HT_CALLOC(1, strlen(*filename) + strlen(domain)+2)) == NULL)
246: HT_OUTOFMEM("HTCanon");
2.53 frystyk 247: if (port)
248: strncpy(newname, *filename, (int) (port-*filename));
249: else
250: strncpy(newname, *filename, (int) (path-*filename));
251: strcat(newname, ".");
252: strcat(newname, domain);
253: }
2.65 frystyk 254: } else /* Look for a trailing dot */
255: #endif
256: {
2.53 frystyk 257: char *dot = port ? port : path;
258: if (dot > *filename && *--dot=='.') {
259: char *orig=dot, *dest=dot+1;
260: while((*orig++ = *dest++));
261: if (port) port--;
262: path--;
263: }
264: }
265: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
266: if (port) {
267: if (!*(port+1) || *(port+1)=='/') {
268: if (!newname) {
269: char *orig=port, *dest=port+1;
270: while((*orig++ = *dest++));
271: }
272: } else if ((!strncmp(access, "http", 4) &&
273: (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
274: (!strncmp(access, "gopher", 6) &&
275: (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
276: (!strncmp(access, "ftp", 3) &&
277: (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
278: if (!newname) {
279: char *orig=port, *dest=port+3;
280: while((*orig++ = *dest++));
2.54 frystyk 281: path -= 3; /* Update path position, Henry Minsky */
2.53 frystyk 282: }
283: } else if (newname)
284: strncat(newname, port, (int) (path-port));
285: }
286:
287: if (newname) {
288: char *newpath = newname+strlen(newname);
289: strcat(newname, path);
290: path = newpath;
2.61 frystyk 291: HT_FREE(*filename); /* Free old copy */
2.53 frystyk 292: *filename = newname;
293: }
294: return path;
2.67 frystyk 295: }
296:
297: /*
298: ** Search the URL and determine whether it is a relative or absolute URL.
299: ** We check to see if there is a ":" before any "/", "?", and "#". If this
300: ** is the case then we say it is absolute. Otherwise it is relative.
301: */
302: PUBLIC BOOL HTURL_isAbsolute (const char * url)
303: {
304: if (url) {
305: const char * ptr = url;
306: while (*ptr) {
307: if (*ptr == ':') return YES;
308: if (*ptr == '/' || *ptr == '?' || *ptr == '#') break;
309: ptr ++;
310: }
311: }
312: return NO;
2.53 frystyk 313: }
314:
2.20 timbl 315: /* Simplify a URI
316: // --------------
317: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1 timbl 318: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20 timbl 319: // Simplification helps us recognize duplicate URIs.
1.1 timbl 320: //
321: // Thus, /etc/junk/../fred becomes /etc/fred
322: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 323: //
324: // but we should NOT change
325: // http://fred.xxx.edu/../..
326: //
327: // or ../../albert.html
2.26 frystyk 328: //
329: // In order to avoid empty URLs the following URLs become:
330: //
331: // /fred/.. becomes /fred/..
332: // /fred/././.. becomes /fred/..
2.27 frystyk 333: // /fred/.././junk/.././ becomes /fred/..
2.26 frystyk 334: //
2.30 frystyk 335: // If more than one set of `://' is found (several proxies in cascade) then
336: // only the part after the last `://' is simplified.
2.44 frystyk 337: //
338: // Returns: A string which might be the old one or a new one.
1.1 timbl 339: */
2.60 frystyk 340: PUBLIC char *HTSimplify (char ** url)
1.1 timbl 341: {
2.31 frystyk 342: char *path;
343: char *p;
2.55 frystyk 344: if (!url || !*url) {
2.62 eric 345: if (URI_TRACE) HTTrace("HTSimplify.. Nothing done\n");
2.55 frystyk 346: return *url;
2.31 frystyk 347: }
2.62 eric 348: if (URI_TRACE) HTTrace("HTSimplify.. `%s\' ", *url);
2.27 frystyk 349:
2.55 frystyk 350: /* Find any scheme name */
351: if ((path = strstr(*url, "://")) != NULL) { /* Find host name */
2.30 frystyk 352: char *newptr;
2.60 frystyk 353: char *access = *url;
354: while (access<path && (*access=TOLOWER(*access))) access++;
2.31 frystyk 355: path += 3;
2.60 frystyk 356: while ((newptr = strstr(path, "://")) != NULL) /* For proxies */
2.31 frystyk 357: path = newptr+3;
2.55 frystyk 358: path = HTCanon(url, path); /* We have a host name */
359: } else if ((path = strstr(*url, ":/")) != NULL) {
2.31 frystyk 360: path += 2;
2.27 frystyk 361: } else
2.55 frystyk 362: path = *url;
2.31 frystyk 363: if (*path == '/' && *(path+1)=='/') { /* Some URLs start //<foo> */
364: path += 1;
2.34 frystyk 365: } else if (!strncmp(path, "news:", 5)) {
366: char *ptr = strchr(path+5, '@');
367: if (!ptr) ptr = path+5;
368: while (*ptr) { /* Make group or host lower case */
369: *ptr = TOLOWER(*ptr);
370: ptr++;
2.31 frystyk 371: }
372: if (URI_TRACE)
2.62 eric 373: HTTrace("into\n............ `%s'\n", *url);
2.55 frystyk 374: return *url; /* Doesn't need to do any more */
2.31 frystyk 375: }
376: if ((p = path)) {
2.55 frystyk 377: char *end;
378: if (!((end = strchr(path, ';')) || (end = strchr(path, '?')) ||
379: (end = strchr(path, '#'))))
380: end = path+strlen(path);
2.31 frystyk 381:
382: /* Parse string second time to simplify */
383: p = path;
2.55 frystyk 384: while(p<end) {
2.31 frystyk 385: if (*p=='/') {
2.56 frystyk 386: if (p>*url && *(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) {
387: char *orig = p+1;
388: char *dest = (*(p+2)!='/') ? p+2 : p+3;
2.31 frystyk 389: while ((*orig++ = *dest++)); /* Remove a slash and a dot */
2.55 frystyk 390: end = orig-1;
2.56 frystyk 391: } else if (*(p+1)=='.' && *(p+2)=='.' && (*(p+3)=='/' || !*(p+3))) {
2.31 frystyk 392: char *q = p;
393: while (q>path && *--q!='/'); /* prev slash */
2.56 frystyk 394: if (strncmp(q, "/../", 4)) {
395: char *orig = q+1;
396: char *dest = (*(p+3)!='/') ? p+3 : p+4;
2.31 frystyk 397: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
2.55 frystyk 398: end = orig-1;
2.56 frystyk 399: p = q; /* Start again with prev slash */
2.31 frystyk 400: } else
401: p++;
402: } else if (*(p+1)=='/') {
403: while (*(p+1)=='/') {
404: char *orig=p, *dest=p+1;
405: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2.55 frystyk 406: end = orig-1;
2.19 frystyk 407: }
2.56 frystyk 408: } else
409: p++;
410: } else
411: p++;
412: }
2.19 frystyk 413: }
2.31 frystyk 414: if (URI_TRACE)
2.62 eric 415: HTTrace("into\n............ `%s'\n", *url);
2.55 frystyk 416: return *url;
2.19 frystyk 417: }
2.31 frystyk 418:
1.1 timbl 419: /* Make Relative Name
420: ** ------------------
421: **
422: ** This function creates and returns a string which gives an expression of
423: ** one address as related to another. Where there is no relation, an absolute
424: ** address is retured.
425: **
426: ** On entry,
427: ** Both names must be absolute, fully qualified names of nodes
2.66 frystyk 428: ** (no fragment bits)
1.1 timbl 429: **
430: ** On exit,
431: ** The return result points to a newly allocated name which, if
432: ** parsed by HTParse relative to relatedName, will yield aName.
433: ** The caller is responsible for freeing the resulting name later.
434: **
435: */
2.63 frystyk 436: PUBLIC char * HTRelative (const char * aName, const char * relatedName)
1.1 timbl 437: {
438: char * result = 0;
2.63 frystyk 439: const char *p = aName;
440: const char *q = relatedName;
441: const char * after_access = 0;
442: const char * path = 0;
443: const char * last_slash = 0;
1.1 timbl 444: int slashes = 0;
445:
446: for(;*p; p++, q++) { /* Find extent of match */
447: if (*p!=*q) break;
448: if (*p==':') after_access = p+1;
449: if (*p=='/') {
450: last_slash = p;
451: slashes++;
452: if (slashes==3) path=p;
453: }
454: }
455:
456: /* q, p point to the first non-matching character or zero */
457:
458: if (!after_access) { /* Different access */
459: StrAllocCopy(result, aName);
460: } else if (slashes<3){ /* Different nodes */
461: StrAllocCopy(result, after_access);
462: } else { /* Some path in common */
463: int levels= 0;
464: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
2.61 frystyk 465: if ((result = (char *) HT_MALLOC(3*levels + strlen(last_slash) + 1)) == NULL)
466: HT_OUTOFMEM("HTRelative");
1.1 timbl 467: result[0]=0;
468: for(;levels; levels--)strcat(result, "../");
469: strcat(result, last_slash+1);
470: }
2.62 eric 471: if (URI_TRACE) HTTrace(
2.21 frystyk 472: "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
473: aName, relatedName, result);
1.1 timbl 474: return result;
475: }
2.1 timbl 476:
2.24 luotonen 477: /* HTCleanTelnetString()
478: * Make sure that the given string doesn't contain characters that
479: * could cause security holes, such as newlines in ftp, gopher,
480: * news or telnet URLs; more specifically: allows everything between
2.26 frystyk 481: * ASCII 20-7E, and also A0-FE, inclusive. Also TAB ('\t') allowed!
2.24 luotonen 482: *
483: * On entry,
484: * str the string that is *modified* if necessary. The
485: * string will be truncated at the first illegal
486: * character that is encountered.
487: * On exit,
488: * returns YES, if the string was modified.
489: * NO, otherwise.
490: */
2.60 frystyk 491: PUBLIC BOOL HTCleanTelnetString (char * str)
2.24 luotonen 492: {
493: char * cur = str;
494:
495: if (!str) return NO;
496:
497: while (*cur) {
2.52 frystyk 498: int a = TOASCII((unsigned char) *cur);
2.26 frystyk 499: if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) || a > 0xFE)) {
2.31 frystyk 500: if (URI_TRACE)
2.62 eric 501: HTTrace("Illegal..... character in URL: \"%s\"\n",str);
2.24 luotonen 502: *cur = 0;
2.31 frystyk 503: if (URI_TRACE)
2.62 eric 504: HTTrace("Truncated... \"%s\"\n",str);
2.24 luotonen 505: return YES;
506: }
507: cur++;
508: }
509: return NO;
510: }
511:
Webmaster