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