Annotation of libwww/Library/src/HTParse.c, revision 2.13
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:
106: /* Access specified but no host: the anchor was not really one
107: e.g. news:j462#36487@foo.bar -- JFG 10/7/92, from bug report */
108: if (parts->access && ! parts->host && parts->anchor) {
109: *(parts->anchor - 1) = '#'; /* Restore the '#' in the address */
110: parts->anchor = 0;
111: }
112:
113: #ifdef NOT_DEFINED /* search is just treated as part of path */
114: {
115: char *p = relative ? relative : absolute;
116: if (p) {
117: char * q = strchr(p, '?'); /* Any search string? */
118: if (q) {
119: *q = 0; /* If so, chop that off. */
120: parts->search = q+1;
121: }
122: }
123: }
124: #endif
125: } /*scan */
126:
127:
128: /* Parse a Name relative to another name
129: ** -------------------------------------
130: **
131: ** This returns those parts of a name which are given (and requested)
132: ** substituting bits from the related name where necessary.
133: **
134: ** On entry,
135: ** aName A filename given
136: ** relatedName A name relative to which aName is to be parsed
137: ** wanted A mask for the bits which are wanted.
138: **
139: ** On exit,
140: ** returns A pointer to a malloc'd string which MUST BE FREED
141: */
142: #ifdef __STDC__
143: char * HTParse(const char * aName, const char * relatedName, int wanted)
144: #else
145: char * HTParse(aName, relatedName, wanted)
146: char * aName;
147: char * relatedName;
148: int wanted;
149: #endif
150:
151: {
152: char * result = 0;
153: char * return_value = 0;
154: int len;
155: char * name = 0;
156: char * rel = 0;
157: char * p;
2.12 timbl 158: char * access;
1.1 timbl 159: struct struct_parts given, related;
160:
161: /* Make working copies of input strings to cut up:
162: */
163: len = strlen(aName)+strlen(relatedName)+10;
164: result=(char *)malloc(len); /* Lots of space: more than enough */
165: if (result == NULL) outofmem(__FILE__, "HTParse");
166:
167: StrAllocCopy(name, aName);
168: StrAllocCopy(rel, relatedName);
169:
170: scan(name, &given);
171: scan(rel, &related);
172: result[0]=0; /* Clear string */
2.12 timbl 173: access = given.access ? given.access : related.access;
1.1 timbl 174: if (wanted & PARSE_ACCESS)
2.12 timbl 175: if (access) {
176: strcat(result, access);
1.1 timbl 177: if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
178: }
179:
180: if (given.access && related.access) /* If different, inherit nothing. */
181: if (strcmp(given.access, related.access)!=0) {
182: related.host=0;
183: related.absolute=0;
184: related.relative=0;
185: related.anchor=0;
186: }
187:
188: if (wanted & PARSE_HOST)
189: if(given.host || related.host) {
2.12 timbl 190: char * tail = result + strlen(result);
1.1 timbl 191: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
192: strcat(result, given.host ? given.host : related.host);
2.12 timbl 193: #define CLEAN_URLS
194: #ifdef CLEAN_URLS
195: /* Ignore default port numbers, and trailing dots on FQDNs
196: which will only cause identical adreesses to look different */
197: {
198: char * p;
199: p = strchr(tail, ':');
200: if (p && access) { /* Port specified */
201: if ( ( strcmp(access, "http") == 0
202: && strcmp(p, ":80") == 0 )
203: ||
204: ( strcmp(access, "gopher") == 0
205: && strcmp(p, ":70") == 0 )
206: )
207: *p = (char)0; /* It is the default: ignore it */
208: }
209: if (!p) p = tail + strlen(tail); /* After hostname */
210: p--; /* End of hostname */
211: if (*p == '.') *p = (char)0; /* chop final . */
212: }
213: #endif
1.1 timbl 214: }
215:
216: if (given.host && related.host) /* If different hosts, inherit no path. */
217: if (strcmp(given.host, related.host)!=0) {
218: related.absolute=0;
219: related.relative=0;
220: related.anchor=0;
221: }
222:
223: if (wanted & PARSE_PATH) {
224: if(given.absolute) { /* All is given */
225: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
226: strcat(result, given.absolute);
227: } else if(related.absolute) { /* Adopt path not name */
228: strcat(result, "/");
229: strcat(result, related.absolute);
230: if (given.relative) {
231: p = strchr(result, '?'); /* Search part? */
232: if (!p) p=result+strlen(result)-1;
233: for (; *p!='/'; p--); /* last / */
234: p[1]=0; /* Remove filename */
235: strcat(result, given.relative); /* Add given one */
236: HTSimplify (result);
237: }
238: } else if(given.relative) {
239: strcat(result, given.relative); /* what we've got */
240: } else if(related.relative) {
241: strcat(result, related.relative);
242: } else { /* No inheritance */
243: strcat(result, "/");
244: }
245: }
246:
247: if (wanted & PARSE_ANCHOR)
248: if(given.anchor || related.anchor) {
249: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
250: strcat(result, given.anchor ? given.anchor : related.anchor);
251: }
252: free(rel);
253: free(name);
254:
255: StrAllocCopy(return_value, result);
256: free(result);
257: return return_value; /* exactly the right length */
258: }
259:
2.11 timbl 260:
1.1 timbl 261: /* Simplify a filename
262: // -------------------
263: //
264: // A unix-style file is allowed to contain the seqeunce xxx/../ which may be
265: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
266: // Simplification helps us recognize duplicate filenames.
267: //
268: // Thus, /etc/junk/../fred becomes /etc/fred
269: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 270: //
271: // but we should NOT change
272: // http://fred.xxx.edu/../..
273: //
274: // or ../../albert.html
1.1 timbl 275: */
276: #ifdef __STDC__
277: void HTSimplify(char * filename)
278: #else
279: void HTSimplify(filename)
280: char * filename;
281: #endif
282:
283: {
284: char * p;
285: char * q;
2.7 timbl 286: if (filename[0] && filename[1]) /* Bug fix 12 Mar 93 TBL */
287: for(p=filename+2; *p; p++) {
2.13 ! luotonen 288: if (*p=='/') {
1.1 timbl 289: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 290: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
291: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
292: &&!(q-1>filename && q[-1]=='/')) {
1.1 timbl 293: strcpy(q, p+3); /* Remove /xxx/.. */
294: if (!*filename) strcpy(filename, "/");
295: p = q-1; /* Start again with prev slash */
2.11 timbl 296: } else { /* xxx/.. leave it! */
2.9 timbl 297: #ifdef BUG_CODE
1.1 timbl 298: strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
299: p = filename; /* Start again */
2.9 timbl 300: #endif
1.1 timbl 301: }
302: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
303: strcpy(p, p+2); /* Remove a slash and a dot */
2.13 ! luotonen 304: } else if (p[-1] != ':') {
! 305: while (p[1] == '/') {
! 306: strcpy(p, p+1); /* Remove multiple slashes */
! 307: }
1.1 timbl 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