Annotation of libwww/Library/src/HTParse.c, revision 2.36
1.1 timbl 1: /* Parse HyperText Document Address HTParse.c
2: ** ================================
2.26 frystyk 3: **
4: ** history:
5: ** May 12 94 TAB added as legal char in HTCleanTelnetString
6: **
1.1 timbl 7: */
2.31 frystyk 8:
1.1 timbl 9: #include "HTUtils.h"
2.31 frystyk 10: #include "HTTCP.h"
1.1 timbl 11: #include "HTParse.h"
12:
2.6 timbl 13: #define HEX_ESCAPE '%'
14:
1.1 timbl 15: struct struct_parts {
2.20 timbl 16: char * access; /* Now known as "scheme" */
1.1 timbl 17: char * host;
18: char * absolute;
19: char * relative;
20: /* char * search; no - treated as part of path */
21: char * anchor;
22: };
23:
24: /* Strip white space off a string
25: ** ------------------------------
26: **
27: ** On exit,
28: ** Return value points to first non-white character, or to 0 if none.
29: ** All trailing white space is OVERWRITTEN with zero.
30: */
31:
2.13 luotonen 32: PUBLIC char * HTStrip ARGS1(char *, s)
1.1 timbl 33: {
34: #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n'))
35: char * p=s;
2.13 luotonen 36: if (!s) return NULL; /* Doesn't dump core if NULL */
37: for(p=s;*p;p++); /* Find end of string */
1.1 timbl 38: for(p--;p>=s;p--) {
39: if(SPACE(*p)) *p=0; /* Zap trailing blanks */
40: else break;
41: }
42: while(SPACE(*s))s++; /* Strip leading blanks */
43: return s;
44: }
45:
46:
47: /* Scan a filename for its consituents
48: ** -----------------------------------
49: **
50: ** On entry,
51: ** name points to a document name which may be incomplete.
52: ** On exit,
53: ** absolute or relative may be nonzero (but not both).
54: ** host, anchor and access may be nonzero if they were specified.
55: ** Any which are nonzero point to zero terminated strings.
56: */
2.32 frystyk 57: PRIVATE void scan ARGS2(char *, name, struct struct_parts *, parts)
1.1 timbl 58: {
59: char * after_access;
60: char * p;
61: int length = strlen(name);
62:
63: parts->access = 0;
64: parts->host = 0;
65: parts->absolute = 0;
66: parts->relative = 0;
67: parts->anchor = 0;
68:
69: after_access = name;
70: for(p=name; *p; p++) {
71: if (*p==':') {
72: *p = 0;
2.20 timbl 73: parts->access = after_access; /* Scheme has been specified */
1.1 timbl 74: after_access = p+1;
2.22 luotonen 75: if (0==strcasecomp("URL", parts->access)) {
2.20 timbl 76: parts->access = NULL; /* Ignore IETF's URL: pre-prefix */
77: } else break;
1.1 timbl 78: }
2.20 timbl 79: if (*p=='/') break; /* Access has not been specified */
1.1 timbl 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
2.33 howcome 143: ** relatedName A name relative to which aName is to be parsed. Give
144: ** it an empty string if aName is absolute.
1.1 timbl 145: ** wanted A mask for the bits which are wanted.
146: **
147: ** On exit,
148: ** returns A pointer to a malloc'd string which MUST BE FREED
149: */
2.32 frystyk 150: char * HTParse ARGS3(CONST char *, aName, CONST char *, relatedName,
151: int, wanted)
1.1 timbl 152: {
153: char * result = 0;
154: char * return_value = 0;
155: int len;
156: char * name = 0;
157: char * rel = 0;
158: char * p;
2.12 timbl 159: char * access;
1.1 timbl 160: struct struct_parts given, related;
2.33 howcome 161:
162: if (!relatedName) /* HWL 23/8/94: dont dump due to NULL */
163: relatedName = "";
1.1 timbl 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) {
194: if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
195: strcat(result, given.host ? given.host : related.host);
196: }
197:
198: if (given.host && related.host) /* If different hosts, inherit no path. */
199: if (strcmp(given.host, related.host)!=0) {
200: related.absolute=0;
201: related.relative=0;
202: related.anchor=0;
203: }
204:
205: if (wanted & PARSE_PATH) {
206: if(given.absolute) { /* All is given */
207: if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
208: strcat(result, given.absolute);
209: } else if(related.absolute) { /* Adopt path not name */
210: strcat(result, "/");
211: strcat(result, related.absolute);
212: if (given.relative) {
213: p = strchr(result, '?'); /* Search part? */
214: if (!p) p=result+strlen(result)-1;
215: for (; *p!='/'; p--); /* last / */
216: p[1]=0; /* Remove filename */
217: strcat(result, given.relative); /* Add given one */
2.31 frystyk 218: result = HTSimplify (result);
1.1 timbl 219: }
220: } else if(given.relative) {
221: strcat(result, given.relative); /* what we've got */
222: } else if(related.relative) {
223: strcat(result, related.relative);
224: } else { /* No inheritance */
225: strcat(result, "/");
226: }
227: }
228:
229: if (wanted & PARSE_ANCHOR)
230: if(given.anchor || related.anchor) {
231: if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
232: strcat(result, given.anchor ? given.anchor : related.anchor);
233: }
234: free(rel);
235: free(name);
236:
237: StrAllocCopy(return_value, result);
238: free(result);
239: return return_value; /* exactly the right length */
240: }
241:
2.11 timbl 242:
2.21 frystyk 243: #if 0 /* NOT USED FOR THE MOMENT */
2.15 luotonen 244: /*
245: ** As strcpy() but guaranteed to work correctly
246: ** with overlapping parameters. AL 7 Feb 1994
247: */
248: PRIVATE void ari_strcpy ARGS2(char *, to,
249: char *, from)
250: {
251: char * tmp;
252:
253: if (!to || !from) return;
254:
255: tmp = (char*)malloc(strlen(from)+1);
256: if (!tmp) outofmem(__FILE__, "my_strcpy");
257:
258: strcpy(tmp, from);
259: strcpy(to, tmp);
260: free(tmp);
261: }
2.21 frystyk 262: #endif
263:
2.20 timbl 264:
265: /* Simplify a URI
266: // --------------
267: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1 timbl 268: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20 timbl 269: // Simplification helps us recognize duplicate URIs.
1.1 timbl 270: //
271: // Thus, /etc/junk/../fred becomes /etc/fred
272: // /etc/junk/./fred becomes /etc/junk/fred
2.11 timbl 273: //
274: // but we should NOT change
275: // http://fred.xxx.edu/../..
276: //
277: // or ../../albert.html
2.26 frystyk 278: //
279: // In the same manner, the following prefixed are preserved:
280: //
281: // ./<etc>
282: // //<etc>
283: //
284: // In order to avoid empty URLs the following URLs become:
285: //
286: // /fred/.. becomes /fred/..
287: // /fred/././.. becomes /fred/..
2.27 frystyk 288: // /fred/.././junk/.././ becomes /fred/..
2.26 frystyk 289: //
2.30 frystyk 290: // If more than one set of `://' is found (several proxies in cascade) then
291: // only the part after the last `://' is simplified.
1.1 timbl 292: */
2.31 frystyk 293: PUBLIC char *HTSimplify ARGS1(char *, filename)
1.1 timbl 294: {
2.31 frystyk 295: char *path;
296: char *p;
2.19 frystyk 297:
2.31 frystyk 298: if (!filename) {
299: if (URI_TRACE)
300: fprintf(stderr, "HTSimplify.. Bad argument\n");
301: return filename;
302: }
303: if (URI_TRACE)
2.27 frystyk 304: fprintf(stderr, "HTSimplify.. `%s\' ", filename);
305:
2.31 frystyk 306: if ((path = strstr(filename, "://")) != NULL) { /* Find host name */
2.30 frystyk 307: char *newptr;
2.31 frystyk 308: path += 3;
309: while ((newptr = strstr(path, "://")) != NULL)
310: path = newptr+3;
311: path = HTCanon(&filename, path); /* We have a host name */
312: } else if ((path = strstr(filename, ":/")) != NULL) {
313: path += 2;
2.27 frystyk 314: } else
2.31 frystyk 315: path = filename;
316: if (*path == '/' && *(path+1)=='/') { /* Some URLs start //<foo> */
317: path += 1;
2.34 frystyk 318: } else if (!strncmp(path, "news:", 5)) {
319: char *ptr = strchr(path+5, '@');
320: if (!ptr) ptr = path+5;
321: while (*ptr) { /* Make group or host lower case */
322: *ptr = TOLOWER(*ptr);
323: ptr++;
2.31 frystyk 324: }
325: if (URI_TRACE)
326: fprintf(stderr, "into\n............ `%s'\n", filename);
327: return filename; /* Doesn't need to do any more */
328: }
329: if ((p = path)) {
330: int segments = 0;
331:
332: /* Parse string first time to find number of `real' tokens */
333: while (*p) {
334: if (*p=='/' || p==path) {
335: if (!((*(p+1)=='/' || !*(p+1)) ||
336: (*(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) ||
337: (*(p+1)=='.' && *(p+2)=='.' &&(*(p+3)=='/' || !*(p+3)))))
338: segments++;
339: }
340: p++;
341: }
2.19 frystyk 342:
2.31 frystyk 343: /* Parse string second time to simplify */
344: p = path;
345: while(*p) {
346: if (*p=='/') {
347: if (p>path && *(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) {
348: char *orig=p, *dest=p+2;
349: while ((*orig++ = *dest++)); /* Remove a slash and a dot */
350: p--;
351: } else if (segments>1 && *(p+1)=='.' && *(p+2)=='.' &&
352: (*(p+3)=='/' || !*(p+3))) {
353: char *q = p;
354: while (q>path && *--q!='/'); /* prev slash */
355: if (strncmp(q, "/../", 4) && strncmp(q, "/./", 3) &&
356: strncmp(q, "./", 2)) {
357: char *orig=q, *dest=p+3;
358: if (*q!='/') dest++;
359: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
360: segments--;
361: p = q-1; /* Start again with prev slash */
362: } else
363: p++;
364: } else if (*(p+1)=='/') {
365: while (*(p+1)=='/') {
366: char *orig=p, *dest=p+1;
367: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2.19 frystyk 368: }
369: }
370: }
2.31 frystyk 371: p++;
372: } /* end while (*p) */
2.19 frystyk 373: }
2.31 frystyk 374: if (URI_TRACE)
2.27 frystyk 375: fprintf(stderr, "into\n............ `%s'\n", filename);
2.31 frystyk 376: return filename;
2.19 frystyk 377: }
2.31 frystyk 378:
2.19 frystyk 379: #ifdef OLD_CODE
2.17 frystyk 380: char * p = filename;
1.1 timbl 381: char * q;
2.17 frystyk 382:
383: if (p) {
384: while (*p && (*p == '/' || *p == '.')) /* Pass starting / or .'s */
385: p++;
386: while(*p) {
387: if (*p=='/') {
1.1 timbl 388: if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11 timbl 389: for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
390: if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
391: &&!(q-1>filename && q[-1]=='/')) {
2.15 luotonen 392: ari_strcpy(q, p+3); /* Remove /xxx/.. */
1.1 timbl 393: if (!*filename) strcpy(filename, "/");
394: p = q-1; /* Start again with prev slash */
2.11 timbl 395: } else { /* xxx/.. leave it! */
2.9 timbl 396: #ifdef BUG_CODE
2.15 luotonen 397: ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm xxx/../ */
1.1 timbl 398: p = filename; /* Start again */
2.9 timbl 399: #endif
1.1 timbl 400: }
401: } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15 luotonen 402: ari_strcpy(p, p+2); /* Remove a slash and a dot */
2.13 luotonen 403: } else if (p[-1] != ':') {
404: while (p[1] == '/') {
2.15 luotonen 405: ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13 luotonen 406: }
1.1 timbl 407: }
2.17 frystyk 408: }
409: p++;
410: } /* end while (*p) */
411: } /* end if (p) */
1.1 timbl 412: }
2.19 frystyk 413: #endif /* OLD_CODE */
1.1 timbl 414:
415:
416: /* Make Relative Name
417: ** ------------------
418: **
419: ** This function creates and returns a string which gives an expression of
420: ** one address as related to another. Where there is no relation, an absolute
421: ** address is retured.
422: **
423: ** On entry,
424: ** Both names must be absolute, fully qualified names of nodes
425: ** (no anchor bits)
426: **
427: ** On exit,
428: ** The return result points to a newly allocated name which, if
429: ** parsed by HTParse relative to relatedName, will yield aName.
430: ** The caller is responsible for freeing the resulting name later.
431: **
432: */
2.32 frystyk 433: char * HTRelative ARGS2(CONST char *, aName, CONST char *, relatedName)
1.1 timbl 434: {
435: char * result = 0;
436: CONST char *p = aName;
437: CONST char *q = relatedName;
438: CONST char * after_access = 0;
439: CONST char * path = 0;
440: CONST char * last_slash = 0;
441: int slashes = 0;
442:
443: for(;*p; p++, q++) { /* Find extent of match */
444: if (*p!=*q) break;
445: if (*p==':') after_access = p+1;
446: if (*p=='/') {
447: last_slash = p;
448: slashes++;
449: if (slashes==3) path=p;
450: }
451: }
452:
453: /* q, p point to the first non-matching character or zero */
454:
455: if (!after_access) { /* Different access */
456: StrAllocCopy(result, aName);
457: } else if (slashes<3){ /* Different nodes */
458: StrAllocCopy(result, after_access);
2.29 frystyk 459: #if 0 /* Henrik */
1.1 timbl 460: } else if (slashes==3){ /* Same node, different path */
461: StrAllocCopy(result, path);
2.21 frystyk 462: #endif
1.1 timbl 463: } else { /* Some path in common */
464: int levels= 0;
465: for(; *q && (*q!='#'); q++) if (*q=='/') levels++;
466: result = (char *)malloc(3*levels + strlen(last_slash) + 1);
467: if (result == NULL) outofmem(__FILE__, "HTRelative");
468: result[0]=0;
469: for(;levels; levels--)strcat(result, "../");
470: strcat(result, last_slash+1);
471: }
2.31 frystyk 472: if (URI_TRACE) fprintf(stderr,
2.21 frystyk 473: "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
474: aName, relatedName, result);
1.1 timbl 475: return result;
476: }
2.1 timbl 477:
478:
2.31 frystyk 479: /* HTCanon
480: **
481: ** Canonicalizes the URL in the following manner starting from the host
482: ** pointer:
483: **
484: ** 1) The host name is converted to lowercase
485: ** 2) Expands the host name of the URL from a local name to a full
486: ** domain name. A host name is started by `://'.
487: **
488: ** Return: OK The position of the current path part of the URL
489: */
490: PUBLIC char *HTCanon ARGS2 (char **, filename, char *, host)
491: {
2.32 frystyk 492: char *newname = NULL;
2.31 frystyk 493: char *port;
494: char *strptr;
495: char *path;
2.36 ! frystyk 496: char *access = host-3;
2.31 frystyk 497:
2.36 ! frystyk 498: while (access>*filename && *(access-1)!='/') /* Find access method */
! 499: access--;
2.31 frystyk 500: if ((path = strchr(host, '/')) == NULL) /* Find path */
501: path = host + strlen(host);
502: if ((strptr = strchr(host, '@')) != NULL && strptr<path) /* UserId */
503: host = strptr;
504: port = strchr(host, ':'); /* Port number */
505:
506: strptr = host; /* Convert to lower-case */
507: while (strptr<path) {
508: *strptr = TOLOWER(*strptr);
509: strptr++;
510: }
511:
512: /* Does the URL contain a full domain name? This also works for a
513: numerical host name. The domain name is already made lower-case
514: and without a trailing dot. */
2.35 frystyk 515: if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
516: strncasecomp(host, "localhost", 9)) {
2.31 frystyk 517: CONST char *domain = HTGetDomainName();
518: if (domain) {
2.32 frystyk 519: if ((newname = (char *) calloc(1, strlen(*filename) +
2.31 frystyk 520: strlen(domain)+2)) == NULL)
521: outofmem(__FILE__, "HTCanon");
522: if (port)
2.32 frystyk 523: strncpy(newname, *filename, (int) (port-*filename));
2.31 frystyk 524: else
2.32 frystyk 525: strncpy(newname, *filename, (int) (path-*filename));
526: strcat(newname, ".");
527: strcat(newname, domain);
2.31 frystyk 528: }
529: } else { /* Look for a trailing dot */
530: char *dot = port ? port : path;
531: if (dot > *filename && *--dot=='.') {
532: char *orig=dot, *dest=dot+1;
533: while((*orig++ = *dest++));
534: if (port) port--;
535: path--;
536: }
537: }
2.36 ! frystyk 538: /* Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp) */
! 539: if (port) {
! 540: if ((!strncmp(access, "http", 4) &&
! 541: (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
! 542: (!strncmp(access, "gopher", 6) &&
! 543: (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
! 544: (!strncmp(access, "ftp", 3) &&
! 545: (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
! 546: if (!newname) {
! 547: char *orig=port, *dest=port+3;
! 548: while((*orig++ = *dest++));
! 549: }
! 550: } else if (newname)
! 551: strncat(newname, port, (int) (path-port));
! 552: }
! 553:
2.32 frystyk 554: if (newname) {
555: char *newpath = newname+strlen(newname);
556: strcat(newname, path);
2.31 frystyk 557: path = newpath;
558: free(*filename); /* Free old copy */
2.32 frystyk 559: *filename = newname;
2.31 frystyk 560: }
561: return path;
562: }
563:
564:
2.6 timbl 565: /* Escape undesirable characters using % HTEscape()
566: ** -------------------------------------
567: **
568: ** This function takes a pointer to a string in which
569: ** some characters may be unacceptable unescaped.
570: ** It returns a string which has these characters
571: ** represented by a '%' character followed by two hex digits.
572: **
2.20 timbl 573: ** In the tradition of being conservative in what you do and liberal
574: ** in what you accept, we encode some characters which in fact are
575: ** allowed in URLs unencoded -- so DON'T use the table below for
576: ** parsing!
577: **
2.6 timbl 578: ** Unlike HTUnEscape(), this routine returns a malloced string.
2.20 timbl 579: **
2.6 timbl 580: */
581:
2.20 timbl 582: /* Not BOTH static AND const at the same time in gcc :-(, Henrik 18/03-94
583: ** code gen error in gcc when making random access to
584: ** static const table(!!) */
2.19 frystyk 585: /* PRIVATE CONST unsigned char isAcceptable[96] = */
586: PRIVATE unsigned char isAcceptable[96] =
2.6 timbl 587:
2.20 timbl 588: /* Overencodes */
2.6 timbl 589: /* Bit 0 xalpha -- see HTFile.h
590: ** Bit 1 xpalpha -- as xalpha but with plus.
2.20 timbl 591: ** Bit 2 ... path -- as xpalpha but with /
2.6 timbl 592: */
593: /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
594: { 0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4, /* 2x !"#$%&'()*+,-./ */
595: 7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */
596: 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 4x @ABCDEFGHIJKLMNO */
597: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7, /* 5X PQRSTUVWXYZ[\]^_ */
598: 0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /* 6x `abcdefghijklmno */
599: 7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 }; /* 7X pqrstuvwxyz{\}~ DEL */
600:
601: PRIVATE char *hex = "0123456789ABCDEF";
602:
2.8 timbl 603: PUBLIC char * HTEscape ARGS2 (CONST char *, str,
2.6 timbl 604: unsigned char, mask)
605: {
606: #define ACCEPTABLE(a) ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
607: CONST char * p;
608: char * q;
609: char * result;
610: int unacceptable = 0;
611: for(p=str; *p; p++)
612: if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
613: unacceptable++;
614: result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
615: if (result == NULL) outofmem(__FILE__, "HTEscape");
616: for(q=result, p=str; *p; p++) {
617: unsigned char a = TOASCII(*p);
618: if (!ACCEPTABLE(a)) {
619: *q++ = HEX_ESCAPE; /* Means hex commming */
620: *q++ = hex[a >> 4];
621: *q++ = hex[a & 15];
622: }
623: else *q++ = *p;
624: }
625: *q++ = 0; /* Terminate */
626: return result;
627: }
628:
629:
2.1 timbl 630: /* Decode %xx escaped characters HTUnEscape()
631: ** -----------------------------
632: **
633: ** This function takes a pointer to a string in which some
634: ** characters may have been encoded in %xy form, where xy is
635: ** the acsii hex code for character 16x+y.
636: ** The string is converted in place, as it will never grow.
637: */
638:
639: PRIVATE char from_hex ARGS1(char, c)
640: {
2.6 timbl 641: return c >= '0' && c <= '9' ? c - '0'
642: : c >= 'A' && c <= 'F'? c - 'A' + 10
643: : c - 'a' + 10; /* accept small letters just in case */
2.1 timbl 644: }
645:
646: PUBLIC char * HTUnEscape ARGS1( char *, str)
647: {
648: char * p = str;
649: char * q = str;
2.25 frystyk 650:
651: if (!str) { /* Just for safety ;-) */
2.31 frystyk 652: if (URI_TRACE)
2.25 frystyk 653: fprintf(stderr, "HTUnEscape.. Called with NULL argument.\n");
654: return "";
655: }
2.1 timbl 656: while(*p) {
2.6 timbl 657: if (*p == HEX_ESCAPE) {
2.1 timbl 658: p++;
659: if (*p) *q = from_hex(*p++) * 16;
660: if (*p) *q = FROMASCII(*q + from_hex(*p++));
661: q++;
662: } else {
663: *q++ = *p++;
664: }
665: }
666:
667: *q++ = 0;
668: return str;
669:
670: } /* HTUnEscape */
671:
672:
2.24 luotonen 673: /* HTCleanTelnetString()
674: * Make sure that the given string doesn't contain characters that
675: * could cause security holes, such as newlines in ftp, gopher,
676: * news or telnet URLs; more specifically: allows everything between
2.26 frystyk 677: * ASCII 20-7E, and also A0-FE, inclusive. Also TAB ('\t') allowed!
2.24 luotonen 678: *
679: * On entry,
680: * str the string that is *modified* if necessary. The
681: * string will be truncated at the first illegal
682: * character that is encountered.
683: * On exit,
684: * returns YES, if the string was modified.
685: * NO, otherwise.
686: */
687: PUBLIC BOOL HTCleanTelnetString ARGS1(char *, str)
688: {
689: char * cur = str;
690:
691: if (!str) return NO;
692:
693: while (*cur) {
694: int a = TOASCII(*cur);
2.26 frystyk 695: if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) || a > 0xFE)) {
2.31 frystyk 696: if (URI_TRACE)
697: fprintf(stderr, "Illegal..... character in URL: \"%s\"\n",str);
2.24 luotonen 698: *cur = 0;
2.31 frystyk 699: if (URI_TRACE)
700: fprintf(stderr, "Truncated... \"%s\"\n",str);
2.24 luotonen 701: return YES;
702: }
703: cur++;
704: }
705: return NO;
706: }
707:
Webmaster