Annotation of libwww/Library/src/HTParse.c, revision 2.17
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 {
12: char * access;
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;
63: char * p;
64: int length = strlen(name);
65:
66: parts->access = 0;
67: parts->host = 0;
68: parts->absolute = 0;
69: parts->relative = 0;
70: parts->anchor = 0;
71:
72: after_access = name;
73: for(p=name; *p; p++) {
74: if (*p==':') {
75: *p = 0;
76: parts->access = name; /* Access name has been specified */
77: after_access = p+1;
78: }
79: if (*p=='/') break;
80: if (*p=='#') break;
81: }
82:
83: for(p=name+length-1; p>=name; p--) {
84: if (*p =='#') {
85: parts->anchor=p+1;
86: *p=0; /* terminate the rest */
87: }
88: }
89: p = after_access;
90: if (*p=='/'){
91: if (p[1]=='/') {
92: parts->host = p+2; /* host has been specified */
93: *p=0; /* Terminate access */
94: p=strchr(parts->host,'/'); /* look for end of host name if any */
95: if(p) {
96: *p=0; /* Terminate host */
97: parts->absolute = p+1; /* Root has been found */
98: }
99: } else {
100: parts->absolute = p+1; /* Root found but no host */
101: }
102: } else {
103: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
104: }
105:
2.16 timbl 106: #ifdef OLD_CODE
1.1 timbl 107: /* Access specified but no host: the anchor was not really one
2.16 timbl 108: e.g. news:j462#36487@foo.bar -- JFG 10/jul/92, from bug report */
109: /* This kludge doesn't work for example when coming across
110: file:/usr/local/www/fred#123
111: which loses its anchor. Correct approach in news is to
112: escape weird characters not allowed in URL. TBL 21/dec/93
113: */
1.1 timbl 114: if (parts->access && ! parts->host && parts->anchor) {
115: *(parts->anchor - 1) = '#'; /* Restore the '#' in the address */
116: parts->anchor = 0;
117: }
2.16 timbl 118: #endif
1.1 timbl 119:
120: #ifdef NOT_DEFINED /* search is just treated as part of path */
121: {
122: char *p = relative ? relative : absolute;
123: if (p) {
124: char * q = strchr(p, '?'); /* Any search string? */
125: if (q) {
126: *q = 0; /* If so, chop that off. */
127: parts->search = q+1;
128: }
129: }
130: }
131: #endif
132: } /*scan */
133:
134:
135: /* Parse a Name relative to another name
136: ** -------------------------------------
137: **
138: ** This returns those parts of a name which are given (and requested)
139: ** substituting bits from the related name where necessary.
140: **
141: ** On entry,
142: ** aName A filename given
143: ** relatedName A name relative to which aName is to be parsed
144: ** wanted A mask for the bits which are wanted.
145: **
146: ** On exit,
147: ** returns A pointer to a malloc'd string which MUST BE FREED
148: */
149: #ifdef __STDC__
150: char * HTParse(const char * aName, const char * relatedName, int wanted)
151: #else
152: char * HTParse(aName, relatedName, wanted)
153: char * aName;
154: char * relatedName;
155: int wanted;
156: #endif
157:
158: {
159: char * result = 0;
160: char * return_value = 0;
161: int len;
162: char * name = 0;
163: char * rel = 0;
164: char * p;
2.12 timbl 165: char * access;
1.1 timbl 166: struct struct_parts given, related;
167:
168: /* Make working copies of input strings to cut up:
169: */
170: len = strlen(aName)+strlen(relatedName)+10;
171: result=(char *)malloc(len); /* Lots of space: more than enough */
172: if (result == NULL) outofmem(__FILE__, "HTParse");
173:
174: StrAllocCopy(name, aName);
175: StrAllocCopy(rel, relatedName);
176:
177: scan(name, &given);
178: scan(rel, &related);
179: result[0]=0; /* Clear string */
2.12 timbl 180: access = given.access ? given.access : related.access;
1.1 timbl 181: if (wanted & PARSE_ACCESS)
2.12 timbl 182: if (access) {
183: strcat(result, access);
1.1 timbl 184: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
185: }
186:
187: if (given.access && related.access) /* If different, inherit nothing. */
188: if (strcmp(given.access, related.access)!=0) {
189: related.host=0;
190: related.absolute=0;
191: related.relative=0;
192: related.anchor=0;
193: }
194:
195: if (wanted & PARSE_HOST)
196: if(given.host || related.host) {
2.12 timbl 197: char * tail = result + strlen(result);
1.1 timbl 198: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
199: strcat(result, given.host ? given.host : related.host);
2.12 timbl 200: #define CLEAN_URLS
201: #ifdef CLEAN_URLS
202: /* Ignore default port numbers, and trailing dots on FQDNs
203: which will only cause identical adreesses to look different */
204: {
205: char * p;
206: p = strchr(tail, ':');
207: if (p && access) { /* Port specified */
208: if ( ( strcmp(access, "http") == 0
209: && strcmp(p, ":80") == 0 )
210: ||
211: ( strcmp(access, "gopher") == 0
212: && strcmp(p, ":70") == 0 )
213: )
214: *p = (char)0; /* It is the default: ignore it */
215: }
216: if (!p) p = tail + strlen(tail); /* After hostname */
217: p--; /* End of hostname */
218: if (*p == '.') *p = (char)0; /* chop final . */
219: }
220: #endif
1.1 timbl 221: }
222:
223: if (given.host && related.host) /* If different hosts, inherit no path. */
224: if (strcmp(given.host, related.host)!=0) {
225: related.absolute=0;
226: related.relative=0;
227: related.anchor=0;
228: }
229:
230: if (wanted & PARSE_PATH) {
231: if(given.absolute) { /* All is given */
232: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
233: strcat(result, given.absolute);
234: } else if(related.absolute) { /* Adopt path not name */
235: strcat(result, "/");
236: strcat(result, related.absolute);
237: if (given.relative) {
238: p = strchr(result, '?'); /* Search part? */
239: if (!p) p=result+strlen(result)-1;
240: for (; *p!='/'; p--); /* last / */
241: p[1]=0; /* Remove filename */
242: strcat(result, given.relative); /* Add given one */
243: HTSimplify (result);
244: }
245: } else if(given.relative) {
246: strcat(result, given.relative); /* what we've got */
247: } else if(related.relative) {
248: strcat(result, related.relative);
249: } else { /* No inheritance */
250: strcat(result, "/");
251: }
252: }
253:
254: if (wanted & PARSE_ANCHOR)
255: if(given.anchor || related.anchor) {
256: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
257: strcat(result, given.anchor ? given.anchor : related.anchor);
258: }
259: free(rel);
260: free(name);
261:
262: StrAllocCopy(return_value, result);
263: free(result);
264: return return_value; /* exactly the right length */
265: }
266:
2.11 timbl 267:
2.15 luotonen 268: /*
269: ** As strcpy() but guaranteed to work correctly
270: ** with overlapping parameters. AL 7 Feb 1994
271: */
272: PRIVATE void ari_strcpy ARGS2(char *, to,
273: char *, from)
274: {
275: char * tmp;
276:
277: if (!to || !from) return;
278:
279: tmp = (char*)malloc(strlen(from)+1);
280: if (!tmp) outofmem(__FILE__, "my_strcpy");
281:
282: strcpy(tmp, from);
283: strcpy(to, tmp);
284: free(tmp);
285: }
1.1 timbl 286: /* Simplify a filename
287: // -------------------
288: //
289: // A unix-style file is allowed to contain the seqeunce xxx/../ which may be
290: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
291: // Simplification helps us recognize duplicate filenames.
292: //
293: // Thus, /etc/junk/../fred becomes /etc/fred
294: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 295: //
296: // but we should NOT change
297: // http://fred.xxx.edu/../..
298: //
299: // or ../../albert.html
1.1 timbl 300: */
2.14 luotonen 301: PUBLIC void HTSimplify ARGS1(char *, filename)
1.1 timbl 302: {
2.17 ! frystyk 303: char * p = filename;
1.1 timbl 304: char * q;
2.17 ! frystyk 305:
! 306: if (p) {
! 307: while (*p && (*p == '/' || *p == '.')) /* Pass starting / or .'s */
! 308: p++;
! 309: while(*p) {
! 310: if (*p=='/') {
1.1 timbl 311: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 312: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
313: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
314: &&!(q-1>filename && q[-1]=='/')) {
2.15 luotonen 315: ari_strcpy(q, p+3); /* Remove /xxx/.. */
1.1 timbl 316: if (!*filename) strcpy(filename, "/");
317: p = q-1; /* Start again with prev slash */
2.11 timbl 318: } else { /* xxx/.. leave it! */
2.9 timbl 319: #ifdef BUG_CODE
2.15 luotonen 320: ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
1.1 timbl 321: p = filename; /* Start again */
2.9 timbl 322: #endif
1.1 timbl 323: }
324: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15 luotonen 325: ari_strcpy(p, p+2); /* Remove a slash and a dot */
2.13 luotonen 326: } else if (p[-1] != ':') {
327: while (p[1] == '/') {
2.15 luotonen 328: ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13 luotonen 329: }
1.1 timbl 330: }
2.17 ! frystyk 331: }
! 332: p++;
! 333: } /* end while (*p) */
! 334: } /* end if (p) */
1.1 timbl 335: }
336:
337:
338: /* Make Relative Name
339: ** ------------------
340: **
341: ** This function creates and returns a string which gives an expression of
342: ** one address as related to another. Where there is no relation, an absolute
343: ** address is retured.
344: **
345: ** On entry,
346: ** Both names must be absolute, fully qualified names of nodes
347: ** (no anchor bits)
348: **
349: ** On exit,
350: ** The return result points to a newly allocated name which, if
351: ** parsed by HTParse relative to relatedName, will yield aName.
352: ** The caller is responsible for freeing the resulting name later.
353: **
354: */
355: #ifdef __STDC__
356: char * HTRelative(const char * aName, const char *relatedName)
357: #else
358: char * HTRelative(aName, relatedName)
359: char * aName;
360: char * relatedName;
361: #endif
362: {
363: char * result = 0;
364: CONST char *p = aName;
365: CONST char *q = relatedName;
366: CONST char * after_access = 0;
367: CONST char * path = 0;
368: CONST char * last_slash = 0;
369: int slashes = 0;
370:
371: for(;*p; p++, q++) { /* Find extent of match */
372: if (*p!=*q) break;
373: if (*p==':') after_access = p+1;
374: if (*p=='/') {
375: last_slash = p;
376: slashes++;
377: if (slashes==3) path=p;
378: }
379: }
380:
381: /* q, p point to the first non-matching character or zero */
382:
383: if (!after_access) { /* Different access */
384: StrAllocCopy(result, aName);
385: } else if (slashes<3){ /* Different nodes */
386: StrAllocCopy(result, after_access);
387: } else if (slashes==3){ /* Same node, different path */
388: StrAllocCopy(result, path);
389: } else { /* Some path in common */
390: int levels= 0;
391: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
392: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
393: if (result == NULL) outofmem(__FILE__, "HTRelative");
394: result[0]=0;
395: for(;levels; levels--)strcat(result, "../");
396: strcat(result, last_slash+1);
397: }
398: if (TRACE) fprintf(stderr, "HT: `%s' expressed relative to\n `%s' is\n `%s'.",
399: aName, relatedName, result);
400: return result;
401: }
2.1 timbl 402:
403:
2.6 timbl 404: /* Escape undesirable characters using % HTEscape()
405: ** -------------------------------------
406: **
407: ** This function takes a pointer to a string in which
408: ** some characters may be unacceptable unescaped.
409: ** It returns a string which has these characters
410: ** represented by a '%' character followed by two hex digits.
411: **
412: ** Unlike HTUnEscape(), this routine returns a malloced string.
413: */
414:
415: PRIVATE CONST unsigned char isAcceptable[96] =
416:
417: /* Bit 0 xalpha -- see HTFile.h
418: ** Bit 1 xpalpha -- as xalpha but with plus.
419: ** Bit 3 ... path -- as xpalphas but with /
420: */
421: /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
422: { 0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4, /* 2x !"#$%&'()*+,-./ */
423: 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */
424: 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */
425: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */
426: 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */
427: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 }; /* 7X pqrstuvwxyz{\}~ DEL */
428:
429: PRIVATE char *hex = "0123456789ABCDEF";
430:
2.8 timbl 431: PUBLIC char * HTEscape ARGS2 (CONST char *, str,
2.6 timbl 432: unsigned char, mask)
433: {
434: #define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
435: CONST char * p;
436: char * q;
437: char * result;
438: int unacceptable = 0;
439: for(p=str; *p; p++)
440: if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
441: unacceptable++;
442: result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
443: if (result == NULL) outofmem(__FILE__, "HTEscape");
444: for(q=result, p=str; *p; p++) {
445: unsigned char a = TOASCII(*p);
446: if (!ACCEPTABLE(a)) {
447: *q++ = HEX_ESCAPE; /* Means hex commming */
448: *q++ = hex[a >> 4];
449: *q++ = hex[a & 15];
450: }
451: else *q++ = *p;
452: }
453: *q++ = 0; /* Terminate */
454: return result;
455: }
456:
457:
2.1 timbl 458: /* Decode %xx escaped characters HTUnEscape()
459: ** -----------------------------
460: **
461: ** This function takes a pointer to a string in which some
462: ** characters may have been encoded in %xy form, where xy is
463: ** the acsii hex code for character 16x+y.
464: ** The string is converted in place, as it will never grow.
465: */
466:
467: PRIVATE char from_hex ARGS1(char, c)
468: {
2.6 timbl 469: return c >= '0' && c <= '9' ? c - '0'
470: : c >= 'A' && c <= 'F'? c - 'A' + 10
471: : c - 'a' + 10; /* accept small letters just in case */
2.1 timbl 472: }
473:
474: PUBLIC char * HTUnEscape ARGS1( char *, str)
475: {
476: char * p = str;
477: char * q = str;
478: while(*p) {
2.6 timbl 479: if (*p == HEX_ESCAPE) {
2.1 timbl 480: p++;
481: if (*p) *q = from_hex(*p++) * 16;
482: if (*p) *q = FROMASCII(*q + from_hex(*p++));
483: q++;
484: } else {
485: *q++ = *p++;
486: }
487: }
488:
489: *q++ = 0;
490: return str;
491:
492: } /* HTUnEscape */
493:
494:
Webmaster