Annotation of libwww/Library/src/HTParse.c, revision 2.9
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:
29: #ifdef __STDC__
30: char * HTStrip(char * s)
31: #else
32: char * HTStrip(s)
33: char *s;
34: #endif
35: {
36: #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n'))
37: char * p=s;
38: for(p=s;*p;p++); /* Find end of string */
39: for(p--;p>=s;p--) {
40: if(SPACE(*p)) *p=0; /* Zap trailing blanks */
41: else break;
42: }
43: while(SPACE(*s))s++; /* Strip leading blanks */
44: return s;
45: }
46:
47:
48: /* Scan a filename for its consituents
49: ** -----------------------------------
50: **
51: ** On entry,
52: ** name points to a document name which may be incomplete.
53: ** On exit,
54: ** absolute or relative may be nonzero (but not both).
55: ** host, anchor and access may be nonzero if they were specified.
56: ** Any which are nonzero point to zero terminated strings.
57: */
58: #ifdef __STDC__
59: PRIVATE void scan(char * name, struct struct_parts *parts)
60: #else
61: PRIVATE void scan(name, parts)
62: char * name;
63: struct struct_parts *parts;
64: #endif
65: {
66: char * after_access;
67: char * p;
68: int length = strlen(name);
69:
70: parts->access = 0;
71: parts->host = 0;
72: parts->absolute = 0;
73: parts->relative = 0;
74: parts->anchor = 0;
75:
76: after_access = name;
77: for(p=name; *p; p++) {
78: if (*p==':') {
79: *p = 0;
80: parts->access = name; /* Access name has been specified */
81: after_access = p+1;
82: }
83: if (*p=='/') break;
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:
110: /* Access specified but no host: the anchor was not really one
111: e.g. news:j462#36487@foo.bar -- JFG 10/7/92, from bug report */
112: if (parts->access && ! parts->host && parts->anchor) {
113: *(parts->anchor - 1) = '#'; /* Restore the '#' in the address */
114: parts->anchor = 0;
115: }
116:
117: #ifdef NOT_DEFINED /* search is just treated as part of path */
118: {
119: char *p = relative ? relative : absolute;
120: if (p) {
121: char * q = strchr(p, '?'); /* Any search string? */
122: if (q) {
123: *q = 0; /* If so, chop that off. */
124: parts->search = q+1;
125: }
126: }
127: }
128: #endif
129: } /*scan */
130:
131:
132: /* Parse a Name relative to another name
133: ** -------------------------------------
134: **
135: ** This returns those parts of a name which are given (and requested)
136: ** substituting bits from the related name where necessary.
137: **
138: ** On entry,
139: ** aName A filename given
140: ** relatedName A name relative to which aName is to be parsed
141: ** wanted A mask for the bits which are wanted.
142: **
143: ** On exit,
144: ** returns A pointer to a malloc'd string which MUST BE FREED
145: */
146: #ifdef __STDC__
147: char * HTParse(const char * aName, const char * relatedName, int wanted)
148: #else
149: char * HTParse(aName, relatedName, wanted)
150: char * aName;
151: char * relatedName;
152: int wanted;
153: #endif
154:
155: {
156: char * result = 0;
157: char * return_value = 0;
158: int len;
159: char * name = 0;
160: char * rel = 0;
161: char * p;
162: struct struct_parts given, related;
163:
164: /* Make working copies of input strings to cut up:
165: */
166: len = strlen(aName)+strlen(relatedName)+10;
167: result=(char *)malloc(len); /* Lots of space: more than enough */
168: if (result == NULL) outofmem(__FILE__, "HTParse");
169:
170: StrAllocCopy(name, aName);
171: StrAllocCopy(rel, relatedName);
172:
173: scan(name, &given);
174: scan(rel, &related);
175: result[0]=0; /* Clear string */
176: if (wanted & PARSE_ACCESS)
177: if (given.access|| related.access) {
178: strcat(result, given.access ? given.access : related.access);
179: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
180: }
181:
182: if (given.access && related.access) /* If different, inherit nothing. */
183: if (strcmp(given.access, related.access)!=0) {
184: related.host=0;
185: related.absolute=0;
186: related.relative=0;
187: related.anchor=0;
188: }
189:
190: if (wanted & PARSE_HOST)
191: if(given.host || related.host) {
192: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
193: strcat(result, given.host ? given.host : related.host);
194: }
195:
196: if (given.host && related.host) /* If different hosts, inherit no path. */
197: if (strcmp(given.host, related.host)!=0) {
198: related.absolute=0;
199: related.relative=0;
200: related.anchor=0;
201: }
202:
203: if (wanted & PARSE_PATH) {
204: if(given.absolute) { /* All is given */
205: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
206: strcat(result, given.absolute);
207: } else if(related.absolute) { /* Adopt path not name */
208: strcat(result, "/");
209: strcat(result, related.absolute);
210: if (given.relative) {
211: p = strchr(result, '?'); /* Search part? */
212: if (!p) p=result+strlen(result)-1;
213: for (; *p!='/'; p--); /* last / */
214: p[1]=0; /* Remove filename */
215: strcat(result, given.relative); /* Add given one */
216: HTSimplify (result);
217: }
218: } else if(given.relative) {
219: strcat(result, given.relative); /* what we've got */
220: } else if(related.relative) {
221: strcat(result, related.relative);
222: } else { /* No inheritance */
223: strcat(result, "/");
224: }
225: }
226:
227: if (wanted & PARSE_ANCHOR)
228: if(given.anchor || related.anchor) {
229: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
230: strcat(result, given.anchor ? given.anchor : related.anchor);
231: }
232: free(rel);
233: free(name);
234:
235: StrAllocCopy(return_value, result);
236: free(result);
237: return return_value; /* exactly the right length */
238: }
239:
240: /* Simplify a filename
241: // -------------------
242: //
243: // A unix-style file is allowed to contain the seqeunce xxx/../ which may be
244: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
245: // Simplification helps us recognize duplicate filenames.
246: //
247: // Thus, /etc/junk/../fred becomes /etc/fred
248: // /etc/junk/./fred becomes /etc/junk/fred
249: */
250: #ifdef __STDC__
251: void HTSimplify(char * filename)
252: #else
253: void HTSimplify(filename)
254: char * filename;
255: #endif
256:
257: {
258: char * p;
259: char * q;
2.7 timbl 260: if (filename[0] && filename[1]) /* Bug fix 12 Mar 93 TBL */
261: for(p=filename+2; *p; p++) {
1.1 timbl 262: if (*p=='/') {
263: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
264: for (q=p-1; (q>filename) && (*q!='/'); q--); /* prev slash */
265: if (*q=='/') {
266: strcpy(q, p+3); /* Remove /xxx/.. */
267: if (!*filename) strcpy(filename, "/");
268: p = q-1; /* Start again with prev slash */
2.9 ! timbl 269: } else { /* xxx/.. leve it! */
! 270: #ifdef BUG_CODE
1.1 timbl 271: strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
272: p = filename; /* Start again */
2.9 ! timbl 273: #endif
1.1 timbl 274: }
275: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
276: strcpy(p, p+2); /* Remove a slash and a dot */
277: }
278: }
279: }
280: }
281:
282:
283: /* Make Relative Name
284: ** ------------------
285: **
286: ** This function creates and returns a string which gives an expression of
287: ** one address as related to another. Where there is no relation, an absolute
288: ** address is retured.
289: **
290: ** On entry,
291: ** Both names must be absolute, fully qualified names of nodes
292: ** (no anchor bits)
293: **
294: ** On exit,
295: ** The return result points to a newly allocated name which, if
296: ** parsed by HTParse relative to relatedName, will yield aName.
297: ** The caller is responsible for freeing the resulting name later.
298: **
299: */
300: #ifdef __STDC__
301: char * HTRelative(const char * aName, const char *relatedName)
302: #else
303: char * HTRelative(aName, relatedName)
304: char * aName;
305: char * relatedName;
306: #endif
307: {
308: char * result = 0;
309: CONST char *p = aName;
310: CONST char *q = relatedName;
311: CONST char * after_access = 0;
312: CONST char * path = 0;
313: CONST char * last_slash = 0;
314: int slashes = 0;
315:
316: for(;*p; p++, q++) { /* Find extent of match */
317: if (*p!=*q) break;
318: if (*p==':') after_access = p+1;
319: if (*p=='/') {
320: last_slash = p;
321: slashes++;
322: if (slashes==3) path=p;
323: }
324: }
325:
326: /* q, p point to the first non-matching character or zero */
327:
328: if (!after_access) { /* Different access */
329: StrAllocCopy(result, aName);
330: } else if (slashes<3){ /* Different nodes */
331: StrAllocCopy(result, after_access);
332: } else if (slashes==3){ /* Same node, different path */
333: StrAllocCopy(result, path);
334: } else { /* Some path in common */
335: int levels= 0;
336: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
337: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
338: if (result == NULL) outofmem(__FILE__, "HTRelative");
339: result[0]=0;
340: for(;levels; levels--)strcat(result, "../");
341: strcat(result, last_slash+1);
342: }
343: if (TRACE) fprintf(stderr, "HT: `%s' expressed relative to\n `%s' is\n `%s'.",
344: aName, relatedName, result);
345: return result;
346: }
2.1 timbl 347:
348:
2.6 timbl 349: /* Escape undesirable characters using % HTEscape()
350: ** -------------------------------------
351: **
352: ** This function takes a pointer to a string in which
353: ** some characters may be unacceptable unescaped.
354: ** It returns a string which has these characters
355: ** represented by a '%' character followed by two hex digits.
356: **
357: ** Unlike HTUnEscape(), this routine returns a malloced string.
358: */
359:
360: PRIVATE CONST unsigned char isAcceptable[96] =
361:
362: /* Bit 0 xalpha -- see HTFile.h
363: ** Bit 1 xpalpha -- as xalpha but with plus.
364: ** Bit 3 ... path -- as xpalphas but with /
365: */
366: /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
367: { 0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4, /* 2x !"#$%&'()*+,-./ */
368: 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */
369: 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */
370: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */
371: 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */
372: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 }; /* 7X pqrstuvwxyz{\}~ DEL */
373:
374: PRIVATE char *hex = "0123456789ABCDEF";
375:
2.8 timbl 376: PUBLIC char * HTEscape ARGS2 (CONST char *, str,
2.6 timbl 377: unsigned char, mask)
378: {
379: #define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
380: CONST char * p;
381: char * q;
382: char * result;
383: int unacceptable = 0;
384: for(p=str; *p; p++)
385: if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
386: unacceptable++;
387: result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
388: if (result == NULL) outofmem(__FILE__, "HTEscape");
389: for(q=result, p=str; *p; p++) {
390: unsigned char a = TOASCII(*p);
391: if (!ACCEPTABLE(a)) {
392: *q++ = HEX_ESCAPE; /* Means hex commming */
393: *q++ = hex[a >> 4];
394: *q++ = hex[a & 15];
395: }
396: else *q++ = *p;
397: }
398: *q++ = 0; /* Terminate */
399: return result;
400: }
401:
402:
2.1 timbl 403: /* Decode %xx escaped characters HTUnEscape()
404: ** -----------------------------
405: **
406: ** This function takes a pointer to a string in which some
407: ** characters may have been encoded in %xy form, where xy is
408: ** the acsii hex code for character 16x+y.
409: ** The string is converted in place, as it will never grow.
410: */
411:
412: PRIVATE char from_hex ARGS1(char, c)
413: {
2.6 timbl 414: return c >= '0' && c <= '9' ? c - '0'
415: : c >= 'A' && c <= 'F'? c - 'A' + 10
416: : c - 'a' + 10; /* accept small letters just in case */
2.1 timbl 417: }
418:
419: PUBLIC char * HTUnEscape ARGS1( char *, str)
420: {
421: char * p = str;
422: char * q = str;
423: while(*p) {
2.6 timbl 424: if (*p == HEX_ESCAPE) {
2.1 timbl 425: p++;
426: if (*p) *q = from_hex(*p++) * 16;
427: if (*p) *q = FROMASCII(*q + from_hex(*p++));
428: q++;
429: } else {
430: *q++ = *p++;
431: }
432: }
433:
434: *q++ = 0;
435: return str;
436:
437: } /* HTUnEscape */
438:
439:
Webmaster