Annotation of libwww/Library/src/HTParse.c, revision 2.12
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;
2.12 ! timbl 162: char * access;
1.1 timbl 163: struct struct_parts given, related;
164:
165: /* Make working copies of input strings to cut up:
166: */
167: len = strlen(aName)+strlen(relatedName)+10;
168: result=(char *)malloc(len); /* Lots of space: more than enough */
169: if (result == NULL) outofmem(__FILE__, "HTParse");
170:
171: StrAllocCopy(name, aName);
172: StrAllocCopy(rel, relatedName);
173:
174: scan(name, &given);
175: scan(rel, &related);
176: result[0]=0; /* Clear string */
2.12 ! timbl 177: access = given.access ? given.access : related.access;
1.1 timbl 178: if (wanted & PARSE_ACCESS)
2.12 ! timbl 179: if (access) {
! 180: strcat(result, access);
1.1 timbl 181: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
182: }
183:
184: if (given.access && related.access) /* If different, inherit nothing. */
185: if (strcmp(given.access, related.access)!=0) {
186: related.host=0;
187: related.absolute=0;
188: related.relative=0;
189: related.anchor=0;
190: }
191:
192: if (wanted & PARSE_HOST)
193: if(given.host || related.host) {
2.12 ! timbl 194: char * tail = result + strlen(result);
1.1 timbl 195: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
196: strcat(result, given.host ? given.host : related.host);
2.12 ! timbl 197: #define CLEAN_URLS
! 198: #ifdef CLEAN_URLS
! 199: /* Ignore default port numbers, and trailing dots on FQDNs
! 200: which will only cause identical adreesses to look different */
! 201: {
! 202: char * p;
! 203: p = strchr(tail, ':');
! 204: if (p && access) { /* Port specified */
! 205: if ( ( strcmp(access, "http") == 0
! 206: && strcmp(p, ":80") == 0 )
! 207: ||
! 208: ( strcmp(access, "gopher") == 0
! 209: && strcmp(p, ":70") == 0 )
! 210: )
! 211: *p = (char)0; /* It is the default: ignore it */
! 212: }
! 213: if (!p) p = tail + strlen(tail); /* After hostname */
! 214: p--; /* End of hostname */
! 215: if (*p == '.') *p = (char)0; /* chop final . */
! 216: }
! 217: #endif
1.1 timbl 218: }
219:
220: if (given.host && related.host) /* If different hosts, inherit no path. */
221: if (strcmp(given.host, related.host)!=0) {
222: related.absolute=0;
223: related.relative=0;
224: related.anchor=0;
225: }
226:
227: if (wanted & PARSE_PATH) {
228: if(given.absolute) { /* All is given */
229: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
230: strcat(result, given.absolute);
231: } else if(related.absolute) { /* Adopt path not name */
232: strcat(result, "/");
233: strcat(result, related.absolute);
234: if (given.relative) {
235: p = strchr(result, '?'); /* Search part? */
236: if (!p) p=result+strlen(result)-1;
237: for (; *p!='/'; p--); /* last / */
238: p[1]=0; /* Remove filename */
239: strcat(result, given.relative); /* Add given one */
240: HTSimplify (result);
241: }
242: } else if(given.relative) {
243: strcat(result, given.relative); /* what we've got */
244: } else if(related.relative) {
245: strcat(result, related.relative);
246: } else { /* No inheritance */
247: strcat(result, "/");
248: }
249: }
250:
251: if (wanted & PARSE_ANCHOR)
252: if(given.anchor || related.anchor) {
253: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
254: strcat(result, given.anchor ? given.anchor : related.anchor);
255: }
256: free(rel);
257: free(name);
258:
259: StrAllocCopy(return_value, result);
260: free(result);
261: return return_value; /* exactly the right length */
262: }
263:
2.11 timbl 264:
1.1 timbl 265: /* Simplify a filename
266: // -------------------
267: //
268: // A unix-style file is allowed to contain the seqeunce xxx/../ which may be
269: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
270: // Simplification helps us recognize duplicate filenames.
271: //
272: // Thus, /etc/junk/../fred becomes /etc/fred
273: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 274: //
275: // but we should NOT change
276: // http://fred.xxx.edu/../..
277: //
278: // or ../../albert.html
1.1 timbl 279: */
280: #ifdef __STDC__
281: void HTSimplify(char * filename)
282: #else
283: void HTSimplify(filename)
284: char * filename;
285: #endif
286:
287: {
288: char * p;
289: char * q;
2.7 timbl 290: if (filename[0] && filename[1]) /* Bug fix 12 Mar 93 TBL */
291: for(p=filename+2; *p; p++) {
1.1 timbl 292: if (*p=='/') {
293: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 294: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
295: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
296: &&!(q-1>filename && q[-1]=='/')) {
1.1 timbl 297: strcpy(q, p+3); /* Remove /xxx/.. */
298: if (!*filename) strcpy(filename, "/");
299: p = q-1; /* Start again with prev slash */
2.11 timbl 300: } else { /* xxx/.. leave it! */
2.9 timbl 301: #ifdef BUG_CODE
1.1 timbl 302: strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
303: p = filename; /* Start again */
2.9 timbl 304: #endif
1.1 timbl 305: }
306: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
307: strcpy(p, p+2); /* Remove a slash and a dot */
308: }
309: }
310: }
311: }
312:
313:
314: /* Make Relative Name
315: ** ------------------
316: **
317: ** This function creates and returns a string which gives an expression of
318: ** one address as related to another. Where there is no relation, an absolute
319: ** address is retured.
320: **
321: ** On entry,
322: ** Both names must be absolute, fully qualified names of nodes
323: ** (no anchor bits)
324: **
325: ** On exit,
326: ** The return result points to a newly allocated name which, if
327: ** parsed by HTParse relative to relatedName, will yield aName.
328: ** The caller is responsible for freeing the resulting name later.
329: **
330: */
331: #ifdef __STDC__
332: char * HTRelative(const char * aName, const char *relatedName)
333: #else
334: char * HTRelative(aName, relatedName)
335: char * aName;
336: char * relatedName;
337: #endif
338: {
339: char * result = 0;
340: CONST char *p = aName;
341: CONST char *q = relatedName;
342: CONST char * after_access = 0;
343: CONST char * path = 0;
344: CONST char * last_slash = 0;
345: int slashes = 0;
346:
347: for(;*p; p++, q++) { /* Find extent of match */
348: if (*p!=*q) break;
349: if (*p==':') after_access = p+1;
350: if (*p=='/') {
351: last_slash = p;
352: slashes++;
353: if (slashes==3) path=p;
354: }
355: }
356:
357: /* q, p point to the first non-matching character or zero */
358:
359: if (!after_access) { /* Different access */
360: StrAllocCopy(result, aName);
361: } else if (slashes<3){ /* Different nodes */
362: StrAllocCopy(result, after_access);
363: } else if (slashes==3){ /* Same node, different path */
364: StrAllocCopy(result, path);
365: } else { /* Some path in common */
366: int levels= 0;
367: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
368: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
369: if (result == NULL) outofmem(__FILE__, "HTRelative");
370: result[0]=0;
371: for(;levels; levels--)strcat(result, "../");
372: strcat(result, last_slash+1);
373: }
374: if (TRACE) fprintf(stderr, "HT: `%s' expressed relative to\n `%s' is\n `%s'.",
375: aName, relatedName, result);
376: return result;
377: }
2.1 timbl 378:
379:
2.6 timbl 380: /* Escape undesirable characters using % HTEscape()
381: ** -------------------------------------
382: **
383: ** This function takes a pointer to a string in which
384: ** some characters may be unacceptable unescaped.
385: ** It returns a string which has these characters
386: ** represented by a '%' character followed by two hex digits.
387: **
388: ** Unlike HTUnEscape(), this routine returns a malloced string.
389: */
390:
391: PRIVATE CONST unsigned char isAcceptable[96] =
392:
393: /* Bit 0 xalpha -- see HTFile.h
394: ** Bit 1 xpalpha -- as xalpha but with plus.
395: ** Bit 3 ... path -- as xpalphas but with /
396: */
397: /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
398: { 0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4, /* 2x !"#$%&'()*+,-./ */
399: 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */
400: 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */
401: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */
402: 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */
403: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 }; /* 7X pqrstuvwxyz{\}~ DEL */
404:
405: PRIVATE char *hex = "0123456789ABCDEF";
406:
2.8 timbl 407: PUBLIC char * HTEscape ARGS2 (CONST char *, str,
2.6 timbl 408: unsigned char, mask)
409: {
410: #define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
411: CONST char * p;
412: char * q;
413: char * result;
414: int unacceptable = 0;
415: for(p=str; *p; p++)
416: if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
417: unacceptable++;
418: result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
419: if (result == NULL) outofmem(__FILE__, "HTEscape");
420: for(q=result, p=str; *p; p++) {
421: unsigned char a = TOASCII(*p);
422: if (!ACCEPTABLE(a)) {
423: *q++ = HEX_ESCAPE; /* Means hex commming */
424: *q++ = hex[a >> 4];
425: *q++ = hex[a & 15];
426: }
427: else *q++ = *p;
428: }
429: *q++ = 0; /* Terminate */
430: return result;
431: }
432:
433:
2.1 timbl 434: /* Decode %xx escaped characters HTUnEscape()
435: ** -----------------------------
436: **
437: ** This function takes a pointer to a string in which some
438: ** characters may have been encoded in %xy form, where xy is
439: ** the acsii hex code for character 16x+y.
440: ** The string is converted in place, as it will never grow.
441: */
442:
443: PRIVATE char from_hex ARGS1(char, c)
444: {
2.6 timbl 445: return c >= '0' && c <= '9' ? c - '0'
446: : c >= 'A' && c <= 'F'? c - 'A' + 10
447: : c - 'a' + 10; /* accept small letters just in case */
2.1 timbl 448: }
449:
450: PUBLIC char * HTUnEscape ARGS1( char *, str)
451: {
452: char * p = str;
453: char * q = str;
454: while(*p) {
2.6 timbl 455: if (*p == HEX_ESCAPE) {
2.1 timbl 456: p++;
457: if (*p) *q = from_hex(*p++) * 16;
458: if (*p) *q = FROMASCII(*q + from_hex(*p++));
459: q++;
460: } else {
461: *q++ = *p++;
462: }
463: }
464:
465: *q++ = 0;
466: return str;
467:
468: } /* HTUnEscape */
469:
470:
Webmaster