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