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