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