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