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