Annotation of libwww/Library/src/HTParse.c, revision 2.16
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: {
303: char * p;
304: char * q;
2.14 luotonen 305: if (filename && filename[0] && filename[1])
2.7 timbl 306: for(p=filename+2; *p; p++) {
2.13 luotonen 307: if (*p=='/') {
1.1 timbl 308: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 309: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
310: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
311: &&!(q-1>filename && q[-1]=='/')) {
2.15 luotonen 312: ari_strcpy(q, p+3); /* Remove /xxx/.. */
1.1 timbl 313: if (!*filename) strcpy(filename, "/");
314: p = q-1; /* Start again with prev slash */
2.11 timbl 315: } else { /* xxx/.. leave it! */
2.9 timbl 316: #ifdef BUG_CODE
2.15 luotonen 317: ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
1.1 timbl 318: p = filename; /* Start again */
2.9 timbl 319: #endif
1.1 timbl 320: }
321: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15 luotonen 322: ari_strcpy(p, p+2); /* Remove a slash and a dot */
2.13 luotonen 323: } else if (p[-1] != ':') {
324: while (p[1] == '/') {
2.15 luotonen 325: ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13 luotonen 326: }
1.1 timbl 327: }
328: }
329: }
330: }
331:
332:
333: /* Make Relative Name
334: ** ------------------
335: **
336: ** This function creates and returns a string which gives an expression of
337: ** one address as related to another. Where there is no relation, an absolute
338: ** address is retured.
339: **
340: ** On entry,
341: ** Both names must be absolute, fully qualified names of nodes
342: ** (no anchor bits)
343: **
344: ** On exit,
345: ** The return result points to a newly allocated name which, if
346: ** parsed by HTParse relative to relatedName, will yield aName.
347: ** The caller is responsible for freeing the resulting name later.
348: **
349: */
350: #ifdef __STDC__
351: char * HTRelative(const char * aName, const char *relatedName)
352: #else
353: char * HTRelative(aName, relatedName)
354: char * aName;
355: char * relatedName;
356: #endif
357: {
358: char * result = 0;
359: CONST char *p = aName;
360: CONST char *q = relatedName;
361: CONST char * after_access = 0;
362: CONST char * path = 0;
363: CONST char * last_slash = 0;
364: int slashes = 0;
365:
366: for(;*p; p++, q++) { /* Find extent of match */
367: if (*p!=*q) break;
368: if (*p==':') after_access = p+1;
369: if (*p=='/') {
370: last_slash = p;
371: slashes++;
372: if (slashes==3) path=p;
373: }
374: }
375:
376: /* q, p point to the first non-matching character or zero */
377:
378: if (!after_access) { /* Different access */
379: StrAllocCopy(result, aName);
380: } else if (slashes<3){ /* Different nodes */
381: StrAllocCopy(result, after_access);
382: } else if (slashes==3){ /* Same node, different path */
383: StrAllocCopy(result, path);
384: } else { /* Some path in common */
385: int levels= 0;
386: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
387: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
388: if (result == NULL) outofmem(__FILE__, "HTRelative");
389: result[0]=0;
390: for(;levels; levels--)strcat(result, "../");
391: strcat(result, last_slash+1);
392: }
393: if (TRACE) fprintf(stderr, "HT: `%s' expressed relative to\n `%s' is\n `%s'.",
394: aName, relatedName, result);
395: return result;
396: }
2.1 timbl 397:
398:
2.6 timbl 399: /* Escape undesirable characters using % HTEscape()
400: ** -------------------------------------
401: **
402: ** This function takes a pointer to a string in which
403: ** some characters may be unacceptable unescaped.
404: ** It returns a string which has these characters
405: ** represented by a '%' character followed by two hex digits.
406: **
407: ** Unlike HTUnEscape(), this routine returns a malloced string.
408: */
409:
410: PRIVATE CONST unsigned char isAcceptable[96] =
411:
412: /* Bit 0 xalpha -- see HTFile.h
413: ** Bit 1 xpalpha -- as xalpha but with plus.
414: ** Bit 3 ... path -- as xpalphas but with /
415: */
416: /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
417: { 0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4, /* 2x !"#$%&'()*+,-./ */
418: 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */
419: 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */
420: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */
421: 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */
422: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 }; /* 7X pqrstuvwxyz{\}~ DEL */
423:
424: PRIVATE char *hex = "0123456789ABCDEF";
425:
2.8 timbl 426: PUBLIC char * HTEscape ARGS2 (CONST char *, str,
2.6 timbl 427: unsigned char, mask)
428: {
429: #define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
430: CONST char * p;
431: char * q;
432: char * result;
433: int unacceptable = 0;
434: for(p=str; *p; p++)
435: if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
436: unacceptable++;
437: result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
438: if (result == NULL) outofmem(__FILE__, "HTEscape");
439: for(q=result, p=str; *p; p++) {
440: unsigned char a = TOASCII(*p);
441: if (!ACCEPTABLE(a)) {
442: *q++ = HEX_ESCAPE; /* Means hex commming */
443: *q++ = hex[a >> 4];
444: *q++ = hex[a & 15];
445: }
446: else *q++ = *p;
447: }
448: *q++ = 0; /* Terminate */
449: return result;
450: }
451:
452:
2.1 timbl 453: /* Decode %xx escaped characters HTUnEscape()
454: ** -----------------------------
455: **
456: ** This function takes a pointer to a string in which some
457: ** characters may have been encoded in %xy form, where xy is
458: ** the acsii hex code for character 16x+y.
459: ** The string is converted in place, as it will never grow.
460: */
461:
462: PRIVATE char from_hex ARGS1(char, c)
463: {
2.6 timbl 464: return c >= '0' && c <= '9' ? c - '0'
465: : c >= 'A' && c <= 'F'? c - 'A' + 10
466: : c - 'a' + 10; /* accept small letters just in case */
2.1 timbl 467: }
468:
469: PUBLIC char * HTUnEscape ARGS1( char *, str)
470: {
471: char * p = str;
472: char * q = str;
473: while(*p) {
2.6 timbl 474: if (*p == HEX_ESCAPE) {
2.1 timbl 475: p++;
476: if (*p) *q = from_hex(*p++) * 16;
477: if (*p) *q = FROMASCII(*q + from_hex(*p++));
478: q++;
479: } else {
480: *q++ = *p++;
481: }
482: }
483:
484: *q++ = 0;
485: return str;
486:
487: } /* HTUnEscape */
488:
489:
Webmaster