Annotation of libwww/Library/src/HTParse.c, revision 2.40
1.1 timbl 1: /* Parse HyperText Document Address HTParse.c
2: ** ================================
2.26 frystyk 3: **
4: ** history:
5: ** May 12 94 TAB added as legal char in HTCleanTelnetString
6: **
1.1 timbl 7: */
2.31 frystyk 8:
2.38 frystyk 9: /* Platform dependent stuff */
1.1 timbl 10: #include "HTUtils.h"
2.38 frystyk 11: #include "tcp.h"
12:
13: /* Library Includes */
14: #include "HTParse.h"
2.31 frystyk 15: #include "HTTCP.h"
2.6 timbl 16:
1.1 timbl 17: struct struct_parts {
2.20 timbl 18: char * access; /* Now known as "scheme" */
1.1 timbl 19: char * host;
20: char * absolute;
21: char * relative;
22: /* char * search; no - treated as part of path */
23: char * anchor;
24: };
25:
26: /* Strip white space off a string
27: ** ------------------------------
28: **
29: ** On exit,
30: ** Return value points to first non-white character, or to 0 if none.
31: ** All trailing white space is OVERWRITTEN with zero.
32: */
33:
2.13 luotonen 34: PUBLIC char * HTStrip ARGS1(char *, s)
1.1 timbl 35: {
36: #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n'))
37: char * p=s;
2.13 luotonen 38: if (!s) return NULL; /* Doesn't dump core if NULL */
39: for(p=s;*p;p++); /* Find end of string */
1.1 timbl 40: for(p--;p>=s;p--) {
41: if(SPACE(*p)) *p=0; /* Zap trailing blanks */
42: else break;
43: }
44: while(SPACE(*s))s++; /* Strip leading blanks */
45: return s;
46: }
47:
48:
49: /* Scan a filename for its consituents
50: ** -----------------------------------
51: **
52: ** On entry,
53: ** name points to a document name which may be incomplete.
54: ** On exit,
55: ** absolute or relative may be nonzero (but not both).
56: ** host, anchor and access may be nonzero if they were specified.
57: ** Any which are nonzero point to zero terminated strings.
58: */
2.32 frystyk 59: PRIVATE void scan ARGS2(char *, name, struct struct_parts *, parts)
1.1 timbl 60: {
61: char * after_access;
62: char * p;
63: int length = strlen(name);
64:
65: parts->access = 0;
66: parts->host = 0;
67: parts->absolute = 0;
68: parts->relative = 0;
69: parts->anchor = 0;
70:
71: after_access = name;
72: for(p=name; *p; p++) {
73: if (*p==':') {
74: *p = 0;
2.20 timbl 75: parts->access = after_access; /* Scheme has been specified */
2.37 howcome 76:
2.40 ! howcome 77: #ifdef __hpux
! 78: after_access = p;
! 79: while (*after_access == 0) /* HWL 15/10/94: weird bug on hp */
! 80: after_access++; /* after_access = p + 1 */
! 81: #else
1.1 timbl 82: after_access = p+1;
2.40 ! howcome 83: #endif
2.37 howcome 84:
2.22 luotonen 85: if (0==strcasecomp("URL", parts->access)) {
2.20 timbl 86: parts->access = NULL; /* Ignore IETF's URL: pre-prefix */
87: } else break;
1.1 timbl 88: }
2.20 timbl 89: if (*p=='/') break; /* Access has not been specified */
1.1 timbl 90: if (*p=='#') break;
91: }
92:
93: for(p=name+length-1; p>=name; p--) {
94: if (*p =='#') {
95: parts->anchor=p+1;
96: *p=0; /* terminate the rest */
97: }
98: }
99: p = after_access;
100: if (*p=='/'){
101: if (p[1]=='/') {
102: parts->host = p+2; /* host has been specified */
103: *p=0; /* Terminate access */
104: p=strchr(parts->host,'/'); /* look for end of host name if any */
105: if(p) {
106: *p=0; /* Terminate host */
107: parts->absolute = p+1; /* Root has been found */
108: }
109: } else {
110: parts->absolute = p+1; /* Root found but no host */
111: }
112: } else {
113: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
114: }
115:
2.16 timbl 116: #ifdef OLD_CODE
1.1 timbl 117: /* Access specified but no host: the anchor was not really one
2.16 timbl 118: e.g. news:j462#36487@foo.bar -- JFG 10/jul/92, from bug report */
119: /* This kludge doesn't work for example when coming across
120: file:/usr/local/www/fred#123
121: which loses its anchor. Correct approach in news is to
122: escape weird characters not allowed in URL. TBL 21/dec/93
123: */
1.1 timbl 124: if (parts->access && ! parts->host && parts->anchor) {
125: *(parts->anchor - 1) = '#'; /* Restore the '#' in the address */
126: parts->anchor = 0;
127: }
2.16 timbl 128: #endif
1.1 timbl 129:
130: #ifdef NOT_DEFINED /* search is just treated as part of path */
131: {
132: char *p = relative ? relative : absolute;
133: if (p) {
134: char * q = strchr(p, '?'); /* Any search string? */
135: if (q) {
136: *q = 0; /* If so, chop that off. */
137: parts->search = q+1;
138: }
139: }
140: }
141: #endif
142: } /*scan */
143:
144:
145: /* Parse a Name relative to another name
146: ** -------------------------------------
147: **
148: ** This returns those parts of a name which are given (and requested)
149: ** substituting bits from the related name where necessary.
150: **
151: ** On entry,
152: ** aName A filename given
2.33 howcome 153: ** relatedName A name relative to which aName is to be parsed. Give
154: ** it an empty string if aName is absolute.
1.1 timbl 155: ** wanted A mask for the bits which are wanted.
156: **
157: ** On exit,
158: ** returns A pointer to a malloc'd string which MUST BE FREED
159: */
2.32 frystyk 160: char * HTParse ARGS3(CONST char *, aName, CONST char *, relatedName,
161: int, wanted)
1.1 timbl 162: {
163: char * result = 0;
164: char * return_value = 0;
165: int len;
166: char * name = 0;
167: char * rel = 0;
168: char * p;
2.12 timbl 169: char * access;
1.1 timbl 170: struct struct_parts given, related;
2.33 howcome 171:
172: if (!relatedName) /* HWL 23/8/94: dont dump due to NULL */
173: relatedName = "";
1.1 timbl 174:
175: /* Make working copies of input strings to cut up:
176: */
177: len = strlen(aName)+strlen(relatedName)+10;
178: result=(char *)malloc(len); /* Lots of space: more than enough */
179: if (result == NULL) outofmem(__FILE__, "HTParse");
180:
181: StrAllocCopy(name, aName);
182: StrAllocCopy(rel, relatedName);
183:
184: scan(name, &given);
185: scan(rel, &related);
186: result[0]=0; /* Clear string */
2.12 timbl 187: access = given.access ? given.access : related.access;
1.1 timbl 188: if (wanted & PARSE_ACCESS)
2.12 timbl 189: if (access) {
190: strcat(result, access);
1.1 timbl 191: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
192: }
193:
194: if (given.access && related.access) /* If different, inherit nothing. */
195: if (strcmp(given.access, related.access)!=0) {
196: related.host=0;
197: related.absolute=0;
198: related.relative=0;
199: related.anchor=0;
200: }
201:
202: if (wanted & PARSE_HOST)
203: if(given.host || related.host) {
204: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
205: strcat(result, given.host ? given.host : related.host);
206: }
207:
208: if (given.host && related.host) /* If different hosts, inherit no path. */
209: if (strcmp(given.host, related.host)!=0) {
210: related.absolute=0;
211: related.relative=0;
212: related.anchor=0;
213: }
214:
215: if (wanted & PARSE_PATH) {
216: if(given.absolute) { /* All is given */
217: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
218: strcat(result, given.absolute);
219: } else if(related.absolute) { /* Adopt path not name */
220: strcat(result, "/");
221: strcat(result, related.absolute);
222: if (given.relative) {
223: p = strchr(result, '?'); /* Search part? */
224: if (!p) p=result+strlen(result)-1;
225: for (; *p!='/'; p--); /* last / */
226: p[1]=0; /* Remove filename */
227: strcat(result, given.relative); /* Add given one */
2.31 frystyk 228: result = HTSimplify (result);
1.1 timbl 229: }
230: } else if(given.relative) {
231: strcat(result, given.relative); /* what we've got */
232: } else if(related.relative) {
233: strcat(result, related.relative);
234: } else { /* No inheritance */
235: strcat(result, "/");
236: }
237: }
238:
239: if (wanted & PARSE_ANCHOR)
240: if(given.anchor || related.anchor) {
241: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
242: strcat(result, given.anchor ? given.anchor : related.anchor);
243: }
244: free(rel);
245: free(name);
246:
247: StrAllocCopy(return_value, result);
248: free(result);
249: return return_value; /* exactly the right length */
250: }
251:
2.11 timbl 252:
2.21 frystyk 253: #if 0 /* NOT USED FOR THE MOMENT */
2.15 luotonen 254: /*
255: ** As strcpy() but guaranteed to work correctly
256: ** with overlapping parameters. AL 7 Feb 1994
257: */
258: PRIVATE void ari_strcpy ARGS2(char *, to,
259: char *, from)
260: {
261: char * tmp;
262:
263: if (!to || !from) return;
264:
265: tmp = (char*)malloc(strlen(from)+1);
266: if (!tmp) outofmem(__FILE__, "my_strcpy");
267:
268: strcpy(tmp, from);
269: strcpy(to, tmp);
270: free(tmp);
271: }
2.21 frystyk 272: #endif
273:
2.20 timbl 274:
275: /* Simplify a URI
276: // --------------
277: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1 timbl 278: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20 timbl 279: // Simplification helps us recognize duplicate URIs.
1.1 timbl 280: //
281: // Thus, /etc/junk/../fred becomes /etc/fred
282: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 283: //
284: // but we should NOT change
285: // http://fred.xxx.edu/../..
286: //
287: // or ../../albert.html
2.26 frystyk 288: //
289: // In the same manner, the following prefixed are preserved:
290: //
291: // ./<etc>
292: // //<etc>
293: //
294: // In order to avoid empty URLs the following URLs become:
295: //
296: // /fred/.. becomes /fred/..
297: // /fred/././.. becomes /fred/..
2.27 frystyk 298: // /fred/.././junk/.././ becomes /fred/..
2.26 frystyk 299: //
2.30 frystyk 300: // If more than one set of `://' is found (several proxies in cascade) then
301: // only the part after the last `://' is simplified.
1.1 timbl 302: */
2.31 frystyk 303: PUBLIC char *HTSimplify ARGS1(char *, filename)
1.1 timbl 304: {
2.31 frystyk 305: char *path;
306: char *p;
2.19 frystyk 307:
2.31 frystyk 308: if (!filename) {
309: if (URI_TRACE)
310: fprintf(stderr, "HTSimplify.. Bad argument\n");
311: return filename;
312: }
313: if (URI_TRACE)
2.27 frystyk 314: fprintf(stderr, "HTSimplify.. `%s\' ", filename);
315:
2.31 frystyk 316: if ((path = strstr(filename, "://")) != NULL) { /* Find host name */
2.30 frystyk 317: char *newptr;
2.31 frystyk 318: path += 3;
319: while ((newptr = strstr(path, "://")) != NULL)
320: path = newptr+3;
321: path = HTCanon(&filename, path); /* We have a host name */
322: } else if ((path = strstr(filename, ":/")) != NULL) {
323: path += 2;
2.27 frystyk 324: } else
2.31 frystyk 325: path = filename;
326: if (*path == '/' && *(path+1)=='/') { /* Some URLs start //<foo> */
327: path += 1;
2.34 frystyk 328: } else if (!strncmp(path, "news:", 5)) {
329: char *ptr = strchr(path+5, '@');
330: if (!ptr) ptr = path+5;
331: while (*ptr) { /* Make group or host lower case */
332: *ptr = TOLOWER(*ptr);
333: ptr++;
2.31 frystyk 334: }
335: if (URI_TRACE)
336: fprintf(stderr, "into\n............ `%s'\n", filename);
337: return filename; /* Doesn't need to do any more */
338: }
339: if ((p = path)) {
340: int segments = 0;
341:
342: /* Parse string first time to find number of `real' tokens */
343: while (*p) {
344: if (*p=='/' || p==path) {
345: if (!((*(p+1)=='/' || !*(p+1)) ||
346: (*(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) ||
347: (*(p+1)=='.' && *(p+2)=='.' &&(*(p+3)=='/' || !*(p+3)))))
348: segments++;
349: }
350: p++;
351: }
2.19 frystyk 352:
2.31 frystyk 353: /* Parse string second time to simplify */
354: p = path;
355: while(*p) {
356: if (*p=='/') {
357: if (p>path && *(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) {
358: char *orig=p, *dest=p+2;
359: while ((*orig++ = *dest++)); /* Remove a slash and a dot */
360: p--;
361: } else if (segments>1 && *(p+1)=='.' && *(p+2)=='.' &&
362: (*(p+3)=='/' || !*(p+3))) {
363: char *q = p;
364: while (q>path && *--q!='/'); /* prev slash */
365: if (strncmp(q, "/../", 4) && strncmp(q, "/./", 3) &&
366: strncmp(q, "./", 2)) {
367: char *orig=q, *dest=p+3;
368: if (*q!='/') dest++;
369: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
370: segments--;
371: p = q-1; /* Start again with prev slash */
372: } else
373: p++;
374: } else if (*(p+1)=='/') {
375: while (*(p+1)=='/') {
376: char *orig=p, *dest=p+1;
377: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2.19 frystyk 378: }
379: }
380: }
2.31 frystyk 381: p++;
382: } /* end while (*p) */
2.19 frystyk 383: }
2.31 frystyk 384: if (URI_TRACE)
2.27 frystyk 385: fprintf(stderr, "into\n............ `%s'\n", filename);
2.31 frystyk 386: return filename;
2.19 frystyk 387: }
2.31 frystyk 388:
2.19 frystyk 389: #ifdef OLD_CODE
2.17 frystyk 390: char * p = filename;
1.1 timbl 391: char * q;
2.17 frystyk 392:
393: if (p) {
394: while (*p && (*p == '/' || *p == '.')) /* Pass starting / or .'s */
395: p++;
396: while(*p) {
397: if (*p=='/') {
1.1 timbl 398: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 399: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
400: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
401: &&!(q-1>filename && q[-1]=='/')) {
2.15 luotonen 402: ari_strcpy(q, p+3); /* Remove /xxx/.. */
1.1 timbl 403: if (!*filename) strcpy(filename, "/");
404: p = q-1; /* Start again with prev slash */
2.11 timbl 405: } else { /* xxx/.. leave it! */
2.9 timbl 406: #ifdef BUG_CODE
2.15 luotonen 407: ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
1.1 timbl 408: p = filename; /* Start again */
2.9 timbl 409: #endif
1.1 timbl 410: }
411: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15 luotonen 412: ari_strcpy(p, p+2); /* Remove a slash and a dot */
2.13 luotonen 413: } else if (p[-1] != ':') {
414: while (p[1] == '/') {
2.15 luotonen 415: ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13 luotonen 416: }
1.1 timbl 417: }
2.17 frystyk 418: }
419: p++;
420: } /* end while (*p) */
421: } /* end if (p) */
1.1 timbl 422: }
2.19 frystyk 423: #endif /* OLD_CODE */
1.1 timbl 424:
425:
426: /* Make Relative Name
427: ** ------------------
428: **
429: ** This function creates and returns a string which gives an expression of
430: ** one address as related to another. Where there is no relation, an absolute
431: ** address is retured.
432: **
433: ** On entry,
434: ** Both names must be absolute, fully qualified names of nodes
435: ** (no anchor bits)
436: **
437: ** On exit,
438: ** The return result points to a newly allocated name which, if
439: ** parsed by HTParse relative to relatedName, will yield aName.
440: ** The caller is responsible for freeing the resulting name later.
441: **
442: */
2.32 frystyk 443: char * HTRelative ARGS2(CONST char *, aName, CONST char *, relatedName)
1.1 timbl 444: {
445: char * result = 0;
446: CONST char *p = aName;
447: CONST char *q = relatedName;
448: CONST char * after_access = 0;
449: CONST char * path = 0;
450: CONST char * last_slash = 0;
451: int slashes = 0;
452:
453: for(;*p; p++, q++) { /* Find extent of match */
454: if (*p!=*q) break;
455: if (*p==':') after_access = p+1;
456: if (*p=='/') {
457: last_slash = p;
458: slashes++;
459: if (slashes==3) path=p;
460: }
461: }
462:
463: /* q, p point to the first non-matching character or zero */
464:
465: if (!after_access) { /* Different access */
466: StrAllocCopy(result, aName);
467: } else if (slashes<3){ /* Different nodes */
468: StrAllocCopy(result, after_access);
2.29 frystyk 469: #if 0 /* Henrik */
1.1 timbl 470: } else if (slashes==3){ /* Same node, different path */
471: StrAllocCopy(result, path);
2.21 frystyk 472: #endif
1.1 timbl 473: } else { /* Some path in common */
474: int levels= 0;
475: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
476: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
477: if (result == NULL) outofmem(__FILE__, "HTRelative");
478: result[0]=0;
479: for(;levels; levels--)strcat(result, "../");
480: strcat(result, last_slash+1);
481: }
2.31 frystyk 482: if (URI_TRACE) fprintf(stderr,
2.21 frystyk 483: "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
484: aName, relatedName, result);
1.1 timbl 485: return result;
486: }
2.1 timbl 487:
488:
2.31 frystyk 489: /* HTCanon
490: **
491: ** Canonicalizes the URL in the following manner starting from the host
492: ** pointer:
493: **
494: ** 1) The host name is converted to lowercase
495: ** 2) Expands the host name of the URL from a local name to a full
496: ** domain name. A host name is started by `://'.
2.38 frystyk 497: ** 3) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
2.31 frystyk 498: **
499: ** Return: OK The position of the current path part of the URL
500: */
501: PUBLIC char *HTCanon ARGS2 (char **, filename, char *, host)
502: {
2.32 frystyk 503: char *newname = NULL;
2.31 frystyk 504: char *port;
505: char *strptr;
506: char *path;
2.36 frystyk 507: char *access = host-3;
2.31 frystyk 508:
2.36 frystyk 509: while (access>*filename && *(access-1)!='/') /* Find access method */
510: access--;
2.31 frystyk 511: if ((path = strchr(host, '/')) == NULL) /* Find path */
512: path = host + strlen(host);
513: if ((strptr = strchr(host, '@')) != NULL && strptr<path) /* UserId */
514: host = strptr;
2.39 frystyk 515: if ((port = strchr(host, ':')) != NULL && port>path) /* Port number */
516: port = NULL;
2.31 frystyk 517:
518: strptr = host; /* Convert to lower-case */
519: while (strptr<path) {
520: *strptr = TOLOWER(*strptr);
521: strptr++;
522: }
523:
524: /* Does the URL contain a full domain name? This also works for a
525: numerical host name. The domain name is already made lower-case
526: and without a trailing dot. */
2.35 frystyk 527: if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
528: strncasecomp(host, "localhost", 9)) {
2.31 frystyk 529: CONST char *domain = HTGetDomainName();
530: if (domain) {
2.32 frystyk 531: if ((newname = (char *) calloc(1, strlen(*filename) +
2.31 frystyk 532: strlen(domain)+2)) == NULL)
533: outofmem(__FILE__, "HTCanon");
534: if (port)
2.32 frystyk 535: strncpy(newname, *filename, (int) (port-*filename));
2.31 frystyk 536: else
2.32 frystyk 537: strncpy(newname, *filename, (int) (path-*filename));
538: strcat(newname, ".");
539: strcat(newname, domain);
2.31 frystyk 540: }
541: } else { /* Look for a trailing dot */
542: char *dot = port ? port : path;
543: if (dot > *filename && *--dot=='.') {
544: char *orig=dot, *dest=dot+1;
545: while((*orig++ = *dest++));
546: if (port) port--;
547: path--;
548: }
549: }
2.36 frystyk 550: /* Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp) */
551: if (port) {
552: if ((!strncmp(access, "http", 4) &&
553: (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
554: (!strncmp(access, "gopher", 6) &&
555: (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
556: (!strncmp(access, "ftp", 3) &&
557: (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
558: if (!newname) {
559: char *orig=port, *dest=port+3;
560: while((*orig++ = *dest++));
561: }
562: } else if (newname)
563: strncat(newname, port, (int) (path-port));
564: }
565:
2.32 frystyk 566: if (newname) {
567: char *newpath = newname+strlen(newname);
568: strcat(newname, path);
2.31 frystyk 569: path = newpath;
570: free(*filename); /* Free old copy */
2.32 frystyk 571: *filename = newname;
2.31 frystyk 572: }
573: return path;
574: }
2.1 timbl 575:
576:
2.24 luotonen 577: /* HTCleanTelnetString()
578: * Make sure that the given string doesn't contain characters that
579: * could cause security holes, such as newlines in ftp, gopher,
580: * news or telnet URLs; more specifically: allows everything between
2.26 frystyk 581: * ASCII 20-7E, and also A0-FE, inclusive. Also TAB ('\t') allowed!
2.24 luotonen 582: *
583: * On entry,
584: * str the string that is *modified* if necessary. The
585: * string will be truncated at the first illegal
586: * character that is encountered.
587: * On exit,
588: * returns YES, if the string was modified.
589: * NO, otherwise.
590: */
591: PUBLIC BOOL HTCleanTelnetString ARGS1(char *, str)
592: {
593: char * cur = str;
594:
595: if (!str) return NO;
596:
597: while (*cur) {
598: int a = TOASCII(*cur);
2.26 frystyk 599: if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) || a > 0xFE)) {
2.31 frystyk 600: if (URI_TRACE)
601: fprintf(stderr, "Illegal..... character in URL: \"%s\"\n",str);
2.24 luotonen 602: *cur = 0;
2.31 frystyk 603: if (URI_TRACE)
604: fprintf(stderr, "Truncated... \"%s\"\n",str);
2.24 luotonen 605: return YES;
606: }
607: cur++;
608: }
609: return NO;
610: }
611:
Webmaster