Annotation of libwww/Library/src/HTParse.c, revision 2.21
1.1 timbl 1: /* Parse HyperText Document Address HTParse.c
2: ** ================================
3: */
4:
5: #include "HTUtils.h"
6: #include "HTParse.h"
7: #include "tcp.h"
8:
2.6 timbl 9: #define HEX_ESCAPE '%'
10:
1.1 timbl 11: struct struct_parts {
2.20 timbl 12: char * access; /* Now known as "scheme" */
1.1 timbl 13: char * host;
14: char * absolute;
15: char * relative;
16: /* char * search; no - treated as part of path */
17: char * anchor;
18: };
19:
20:
21: /* Strip white space off a string
22: ** ------------------------------
23: **
24: ** On exit,
25: ** Return value points to first non-white character, or to 0 if none.
26: ** All trailing white space is OVERWRITTEN with zero.
27: */
28:
2.13 luotonen 29: PUBLIC char * HTStrip ARGS1(char *, s)
1.1 timbl 30: {
31: #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n'))
32: char * p=s;
2.13 luotonen 33: if (!s) return NULL; /* Doesn't dump core if NULL */
34: for(p=s;*p;p++); /* Find end of string */
1.1 timbl 35: for(p--;p>=s;p--) {
36: if(SPACE(*p)) *p=0; /* Zap trailing blanks */
37: else break;
38: }
39: while(SPACE(*s))s++; /* Strip leading blanks */
40: return s;
41: }
42:
43:
44: /* Scan a filename for its consituents
45: ** -----------------------------------
46: **
47: ** On entry,
48: ** name points to a document name which may be incomplete.
49: ** On exit,
50: ** absolute or relative may be nonzero (but not both).
51: ** host, anchor and access may be nonzero if they were specified.
52: ** Any which are nonzero point to zero terminated strings.
53: */
54: #ifdef __STDC__
55: PRIVATE void scan(char * name, struct struct_parts *parts)
56: #else
57: PRIVATE void scan(name, parts)
58: char * name;
59: struct struct_parts *parts;
60: #endif
61: {
62: char * after_access;
2.20 timbl 63: char * start = name;
1.1 timbl 64: char * p;
65: int length = strlen(name);
66:
67: parts->access = 0;
68: parts->host = 0;
69: parts->absolute = 0;
70: parts->relative = 0;
71: parts->anchor = 0;
72:
73: after_access = name;
74: for(p=name; *p; p++) {
75: if (*p==':') {
76: *p = 0;
2.20 timbl 77: parts->access = after_access; /* Scheme has been specified */
1.1 timbl 78: after_access = p+1;
2.20 timbl 79: if (0==strcasecmp("URL", parts->access)) {
80: parts->access = NULL; /* Ignore IETF's URL: pre-prefix */
81: } else break;
1.1 timbl 82: }
2.20 timbl 83: if (*p=='/') break; /* Access has not been specified */
1.1 timbl 84: if (*p=='#') break;
85: }
86:
87: for(p=name+length-1; p>=name; p--) {
88: if (*p =='#') {
89: parts->anchor=p+1;
90: *p=0; /* terminate the rest */
91: }
92: }
93: p = after_access;
94: if (*p=='/'){
95: if (p[1]=='/') {
96: parts->host = p+2; /* host has been specified */
97: *p=0; /* Terminate access */
98: p=strchr(parts->host,'/'); /* look for end of host name if any */
99: if(p) {
100: *p=0; /* Terminate host */
101: parts->absolute = p+1; /* Root has been found */
102: }
103: } else {
104: parts->absolute = p+1; /* Root found but no host */
105: }
106: } else {
107: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
108: }
109:
2.16 timbl 110: #ifdef OLD_CODE
1.1 timbl 111: /* Access specified but no host: the anchor was not really one
2.16 timbl 112: e.g. news:j462#36487@foo.bar -- JFG 10/jul/92, from bug report */
113: /* This kludge doesn't work for example when coming across
114: file:/usr/local/www/fred#123
115: which loses its anchor. Correct approach in news is to
116: escape weird characters not allowed in URL. TBL 21/dec/93
117: */
1.1 timbl 118: if (parts->access && ! parts->host && parts->anchor) {
119: *(parts->anchor - 1) = '#'; /* Restore the '#' in the address */
120: parts->anchor = 0;
121: }
2.16 timbl 122: #endif
1.1 timbl 123:
124: #ifdef NOT_DEFINED /* search is just treated as part of path */
125: {
126: char *p = relative ? relative : absolute;
127: if (p) {
128: char * q = strchr(p, '?'); /* Any search string? */
129: if (q) {
130: *q = 0; /* If so, chop that off. */
131: parts->search = q+1;
132: }
133: }
134: }
135: #endif
136: } /*scan */
137:
138:
139: /* Parse a Name relative to another name
140: ** -------------------------------------
141: **
142: ** This returns those parts of a name which are given (and requested)
143: ** substituting bits from the related name where necessary.
144: **
145: ** On entry,
146: ** aName A filename given
147: ** relatedName A name relative to which aName is to be parsed
148: ** wanted A mask for the bits which are wanted.
149: **
150: ** On exit,
151: ** returns A pointer to a malloc'd string which MUST BE FREED
152: */
153: #ifdef __STDC__
154: char * HTParse(const char * aName, const char * relatedName, int wanted)
155: #else
156: char * HTParse(aName, relatedName, wanted)
157: char * aName;
158: char * relatedName;
159: int wanted;
160: #endif
161:
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;
171:
172: /* Make working copies of input strings to cut up:
173: */
174: len = strlen(aName)+strlen(relatedName)+10;
175: result=(char *)malloc(len); /* Lots of space: more than enough */
176: if (result == NULL) outofmem(__FILE__, "HTParse");
177:
178: StrAllocCopy(name, aName);
179: StrAllocCopy(rel, relatedName);
180:
181: scan(name, &given);
182: scan(rel, &related);
183: result[0]=0; /* Clear string */
2.12 timbl 184: access = given.access ? given.access : related.access;
1.1 timbl 185: if (wanted & PARSE_ACCESS)
2.12 timbl 186: if (access) {
187: strcat(result, access);
1.1 timbl 188: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
189: }
190:
191: if (given.access && related.access) /* If different, inherit nothing. */
192: if (strcmp(given.access, related.access)!=0) {
193: related.host=0;
194: related.absolute=0;
195: related.relative=0;
196: related.anchor=0;
197: }
198:
199: if (wanted & PARSE_HOST)
200: if(given.host || related.host) {
2.12 timbl 201: char * tail = result + strlen(result);
1.1 timbl 202: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
203: strcat(result, given.host ? given.host : related.host);
2.12 timbl 204: #define CLEAN_URLS
205: #ifdef CLEAN_URLS
206: /* Ignore default port numbers, and trailing dots on FQDNs
207: which will only cause identical adreesses to look different */
208: {
209: char * p;
210: p = strchr(tail, ':');
211: if (p && access) { /* Port specified */
212: if ( ( strcmp(access, "http") == 0
213: && strcmp(p, ":80") == 0 )
214: ||
215: ( strcmp(access, "gopher") == 0
216: && strcmp(p, ":70") == 0 )
217: )
218: *p = (char)0; /* It is the default: ignore it */
219: }
220: if (!p) p = tail + strlen(tail); /* After hostname */
2.21 ! frystyk 221: if (*p) { /* Henrik 17/04-94 */
! 222: p--; /* End of hostname */
! 223: if (*p == '.') *p = (char)0; /* chop final . */
! 224: }
2.12 timbl 225: }
226: #endif
1.1 timbl 227: }
228:
229: if (given.host && related.host) /* If different hosts, inherit no path. */
230: if (strcmp(given.host, related.host)!=0) {
231: related.absolute=0;
232: related.relative=0;
233: related.anchor=0;
234: }
235:
236: if (wanted & PARSE_PATH) {
237: if(given.absolute) { /* All is given */
238: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
239: strcat(result, given.absolute);
240: } else if(related.absolute) { /* Adopt path not name */
241: strcat(result, "/");
242: strcat(result, related.absolute);
243: if (given.relative) {
244: p = strchr(result, '?'); /* Search part? */
245: if (!p) p=result+strlen(result)-1;
246: for (; *p!='/'; p--); /* last / */
247: p[1]=0; /* Remove filename */
248: strcat(result, given.relative); /* Add given one */
249: HTSimplify (result);
250: }
251: } else if(given.relative) {
252: strcat(result, given.relative); /* what we've got */
253: } else if(related.relative) {
254: strcat(result, related.relative);
255: } else { /* No inheritance */
256: strcat(result, "/");
257: }
258: }
259:
260: if (wanted & PARSE_ANCHOR)
261: if(given.anchor || related.anchor) {
262: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
263: strcat(result, given.anchor ? given.anchor : related.anchor);
264: }
265: free(rel);
266: free(name);
267:
268: StrAllocCopy(return_value, result);
269: free(result);
270: return return_value; /* exactly the right length */
271: }
272:
2.11 timbl 273:
2.21 ! frystyk 274: #if 0 /* NOT USED FOR THE MOMENT */
2.15 luotonen 275: /*
276: ** As strcpy() but guaranteed to work correctly
277: ** with overlapping parameters. AL 7 Feb 1994
278: */
279: PRIVATE void ari_strcpy ARGS2(char *, to,
280: char *, from)
281: {
282: char * tmp;
283:
284: if (!to || !from) return;
285:
286: tmp = (char*)malloc(strlen(from)+1);
287: if (!tmp) outofmem(__FILE__, "my_strcpy");
288:
289: strcpy(tmp, from);
290: strcpy(to, tmp);
291: free(tmp);
292: }
2.21 ! frystyk 293: #endif
! 294:
2.20 timbl 295:
296: /* Simplify a URI
297: // --------------
298: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1 timbl 299: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20 timbl 300: // Simplification helps us recognize duplicate URIs.
1.1 timbl 301: //
302: // Thus, /etc/junk/../fred becomes /etc/fred
303: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 304: //
305: // but we should NOT change
306: // http://fred.xxx.edu/../..
307: //
308: // or ../../albert.html
1.1 timbl 309: */
2.14 luotonen 310: PUBLIC void HTSimplify ARGS1(char *, filename)
1.1 timbl 311: {
2.19 frystyk 312: int tokcnt = 0;
313: char *strptr;
314: char *urlptr;
315: if (!filename || !*filename) /* Just to be sure! */
316: return;
317:
318: /* Skip prefix, starting ./ and starting ///<etc> */
319: urlptr = strstr(filename, "://"); /* Find prefix */
320: urlptr = urlptr ? urlptr+3 : filename;
321: if (*urlptr == '.' && *(urlptr+1) == '/') /* Starting ./<etc> */
322: urlptr += 2;
323: else if (*urlptr == '/') { /* Some URLs start //<file> */
324: while (*++urlptr == '/');
325: }
326: if (!*urlptr)
327: return;
328:
329: /* Now we have the string we want to work with */
330: strptr = urlptr;
331: while (*strptr++) { /* Count number of delimiters */
332: if (*strptr == '/')
333: tokcnt++;
334: }
335: {
336: BOOL slashtail = NO;
337: char *empty = "";
338: char *url = NULL;
339: char **tokptr;
340: char **tokstart;
341: StrAllocCopy(url, urlptr);
342:
343: /* Does the URL end with a slash? */
344: if(*(filename+strlen(filename)-1) == '/')
345: slashtail = YES;
346:
347: /* I allocate cnt+2 as I don't know if the url is terminated by '/' */
348: if ((tokstart = (char **) calloc(tokcnt+2, sizeof(char *))) == NULL)
349: outofmem(__FILE__, "HTSimplify");
350:
351: /* Read the tokens forwards */
352: tokptr = tokstart;
353: *tokptr++ = strtok(url, "/");
354: while ((strptr = strtok(NULL, "/")) != NULL)
355: *tokptr++ = strptr;
356:
357: /* Scan backwards for '.' and '..' */
358: tokptr--;
359: while(tokptr >= tokstart) {
360: if (!strcmp(*tokptr, ".")) {
361: *tokptr = empty;
362: } else if (!strcmp(*tokptr, "..")) {
363: char **pptr = tokptr-1;
364: while (pptr >= tokstart) {
365: if (**pptr && strcmp(*pptr, "..") && strcmp(*pptr, ".")) {
366: *pptr = empty;
367: *tokptr = empty;
368: break;
369: }
370: pptr--;
371: }
372: }
373: tokptr--;
374: }
375:
376: /* Write the rest out forwards */
377: *urlptr = '\0';
378: strcat(urlptr, *++tokptr);
379: while (*++tokptr) {
380: if (**tokptr) {
381: strcat(urlptr, "/");
382: strcat(urlptr, *tokptr);
383: }
384: }
385: if (slashtail == YES)
386: strcat(urlptr, "/");
387: free(url);
388: free(tokstart);
389: }
390: if (TRACE)
2.21 ! frystyk 391: fprintf(stderr, "HTSimplify.. `%s\'\n", filename);
2.19 frystyk 392: }
393: #ifdef OLD_CODE
2.17 frystyk 394: char * p = filename;
1.1 timbl 395: char * q;
2.17 frystyk 396:
397: if (p) {
398: while (*p && (*p == '/' || *p == '.')) /* Pass starting / or .'s */
399: p++;
400: while(*p) {
401: if (*p=='/') {
1.1 timbl 402: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 403: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
404: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
405: &&!(q-1>filename && q[-1]=='/')) {
2.15 luotonen 406: ari_strcpy(q, p+3); /* Remove /xxx/.. */
1.1 timbl 407: if (!*filename) strcpy(filename, "/");
408: p = q-1; /* Start again with prev slash */
2.11 timbl 409: } else { /* xxx/.. leave it! */
2.9 timbl 410: #ifdef BUG_CODE
2.15 luotonen 411: ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
1.1 timbl 412: p = filename; /* Start again */
2.9 timbl 413: #endif
1.1 timbl 414: }
415: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15 luotonen 416: ari_strcpy(p, p+2); /* Remove a slash and a dot */
2.13 luotonen 417: } else if (p[-1] != ':') {
418: while (p[1] == '/') {
2.15 luotonen 419: ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13 luotonen 420: }
1.1 timbl 421: }
2.17 frystyk 422: }
423: p++;
424: } /* end while (*p) */
425: } /* end if (p) */
1.1 timbl 426: }
2.19 frystyk 427: #endif /* OLD_CODE */
1.1 timbl 428:
429:
430: /* Make Relative Name
431: ** ------------------
432: **
433: ** This function creates and returns a string which gives an expression of
434: ** one address as related to another. Where there is no relation, an absolute
435: ** address is retured.
436: **
437: ** On entry,
438: ** Both names must be absolute, fully qualified names of nodes
439: ** (no anchor bits)
440: **
441: ** On exit,
442: ** The return result points to a newly allocated name which, if
443: ** parsed by HTParse relative to relatedName, will yield aName.
444: ** The caller is responsible for freeing the resulting name later.
445: **
446: */
447: #ifdef __STDC__
448: char * HTRelative(const char * aName, const char *relatedName)
449: #else
450: char * HTRelative(aName, relatedName)
451: char * aName;
452: char * relatedName;
453: #endif
454: {
455: char * result = 0;
456: CONST char *p = aName;
457: CONST char *q = relatedName;
458: CONST char * after_access = 0;
459: CONST char * path = 0;
460: CONST char * last_slash = 0;
461: int slashes = 0;
462:
463: for(;*p; p++, q++) { /* Find extent of match */
464: if (*p!=*q) break;
465: if (*p==':') after_access = p+1;
466: if (*p=='/') {
467: last_slash = p;
468: slashes++;
469: if (slashes==3) path=p;
470: }
471: }
472:
473: /* q, p point to the first non-matching character or zero */
474:
475: if (!after_access) { /* Different access */
476: StrAllocCopy(result, aName);
477: } else if (slashes<3){ /* Different nodes */
478: StrAllocCopy(result, after_access);
2.21 ! frystyk 479: #if 0
1.1 timbl 480: } else if (slashes==3){ /* Same node, different path */
481: StrAllocCopy(result, path);
2.21 ! frystyk 482: #endif
1.1 timbl 483: } else { /* Some path in common */
484: int levels= 0;
485: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
486: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
487: if (result == NULL) outofmem(__FILE__, "HTRelative");
488: result[0]=0;
489: for(;levels; levels--)strcat(result, "../");
490: strcat(result, last_slash+1);
491: }
2.21 ! frystyk 492: if (TRACE) fprintf(stderr,
! 493: "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
! 494: aName, relatedName, result);
1.1 timbl 495: return result;
496: }
2.1 timbl 497:
498:
2.6 timbl 499: /* Escape undesirable characters using % HTEscape()
500: ** -------------------------------------
501: **
502: ** This function takes a pointer to a string in which
503: ** some characters may be unacceptable unescaped.
504: ** It returns a string which has these characters
505: ** represented by a '%' character followed by two hex digits.
506: **
2.20 timbl 507: ** In the tradition of being conservative in what you do and liberal
508: ** in what you accept, we encode some characters which in fact are
509: ** allowed in URLs unencoded -- so DON'T use the table below for
510: ** parsing!
511: **
2.6 timbl 512: ** Unlike HTUnEscape(), this routine returns a malloced string.
2.20 timbl 513: **
2.6 timbl 514: */
515:
2.20 timbl 516: /* Not BOTH static AND const at the same time in gcc :-(, Henrik 18/03-94
517: ** code gen error in gcc when making random access to
518: ** static const table(!!) */
2.19 frystyk 519: /* PRIVATE CONST unsigned char isAcceptable[96] = */
520: PRIVATE unsigned char isAcceptable[96] =
2.6 timbl 521:
2.20 timbl 522: /* Overencodes */
2.6 timbl 523: /* Bit 0 xalpha -- see HTFile.h
524: ** Bit 1 xpalpha -- as xalpha but with plus.
2.20 timbl 525: ** Bit 2 ... path -- as xpalpha but with /
2.6 timbl 526: */
527: /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
528: { 0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4, /* 2x !"#$%&'()*+,-./ */
529: 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */
530: 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */
531: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */
532: 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */
533: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 }; /* 7X pqrstuvwxyz{\}~ DEL */
534:
535: PRIVATE char *hex = "0123456789ABCDEF";
536:
2.8 timbl 537: PUBLIC char * HTEscape ARGS2 (CONST char *, str,
2.6 timbl 538: unsigned char, mask)
539: {
540: #define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
541: CONST char * p;
542: char * q;
543: char * result;
544: int unacceptable = 0;
545: for(p=str; *p; p++)
546: if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
547: unacceptable++;
548: result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
549: if (result == NULL) outofmem(__FILE__, "HTEscape");
550: for(q=result, p=str; *p; p++) {
551: unsigned char a = TOASCII(*p);
552: if (!ACCEPTABLE(a)) {
553: *q++ = HEX_ESCAPE; /* Means hex commming */
554: *q++ = hex[a >> 4];
555: *q++ = hex[a & 15];
556: }
557: else *q++ = *p;
558: }
559: *q++ = 0; /* Terminate */
560: return result;
561: }
562:
563:
2.1 timbl 564: /* Decode %xx escaped characters HTUnEscape()
565: ** -----------------------------
566: **
567: ** This function takes a pointer to a string in which some
568: ** characters may have been encoded in %xy form, where xy is
569: ** the acsii hex code for character 16x+y.
570: ** The string is converted in place, as it will never grow.
571: */
572:
573: PRIVATE char from_hex ARGS1(char, c)
574: {
2.6 timbl 575: return c >= '0' && c <= '9' ? c - '0'
576: : c >= 'A' && c <= 'F'? c - 'A' + 10
577: : c - 'a' + 10; /* accept small letters just in case */
2.1 timbl 578: }
579:
580: PUBLIC char * HTUnEscape ARGS1( char *, str)
581: {
582: char * p = str;
583: char * q = str;
584: while(*p) {
2.6 timbl 585: if (*p == HEX_ESCAPE) {
2.1 timbl 586: p++;
587: if (*p) *q = from_hex(*p++) * 16;
588: if (*p) *q = FROMASCII(*q + from_hex(*p++));
589: q++;
590: } else {
591: *q++ = *p++;
592: }
593: }
594:
595: *q++ = 0;
596: return str;
597:
598: } /* HTUnEscape */
599:
600:
Webmaster