Annotation of libwww/Library/src/HTParse.c, revision 2.39

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: 
2.38      frystyk     9: /* Platform dependent stuff */
1.1       timbl      10: #include "HTUtils.h"
2.38      frystyk    11: #include "tcp.h"
                     12: 
                     13: /* Library Includes */
                     14: #include "HTParse.h"
2.31      frystyk    15: #include "HTTCP.h"
2.6       timbl      16: 
1.1       timbl      17: struct struct_parts {
2.20      timbl      18:        char * access;          /* Now known as "scheme" */
1.1       timbl      19:        char * host;
                     20:        char * absolute;
                     21:        char * relative;
                     22: /*     char * search;          no - treated as part of path */
                     23:        char * anchor;
                     24: };
                     25: 
                     26: /*     Strip white space off a string
                     27: **     ------------------------------
                     28: **
                     29: ** On exit,
                     30: **     Return value points to first non-white character, or to 0 if none.
                     31: **     All trailing white space is OVERWRITTEN with zero.
                     32: */
                     33: 
2.13      luotonen   34: PUBLIC char * HTStrip ARGS1(char *, s)
1.1       timbl      35: {
                     36: #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n')) 
                     37:     char * p=s;
2.13      luotonen   38:     if (!s) return NULL;       /* Doesn't dump core if NULL */
                     39:     for(p=s;*p;p++);           /* Find end of string */
1.1       timbl      40:     for(p--;p>=s;p--) {
                     41:        if(SPACE(*p)) *p=0;     /* Zap trailing blanks */
                     42:        else break;
                     43:     }
                     44:     while(SPACE(*s))s++;       /* Strip leading blanks */
                     45:     return s;
                     46: }
                     47: 
                     48: 
                     49: /*     Scan a filename for its consituents
                     50: **     -----------------------------------
                     51: **
                     52: ** On entry,
                     53: **     name    points to a document name which may be incomplete.
                     54: ** On exit,
                     55: **      absolute or relative may be nonzero (but not both).
                     56: **     host, anchor and access may be nonzero if they were specified.
                     57: **     Any which are nonzero point to zero terminated strings.
                     58: */
2.32      frystyk    59: PRIVATE void scan ARGS2(char *, name, struct struct_parts *, parts)
1.1       timbl      60: {
                     61:     char * after_access;
                     62:     char * p;
                     63:     int length = strlen(name);
                     64:     
                     65:     parts->access = 0;
                     66:     parts->host = 0;
                     67:     parts->absolute = 0;
                     68:     parts->relative = 0;
                     69:     parts->anchor = 0;
                     70:     
                     71:     after_access = name;
                     72:     for(p=name; *p; p++) {
                     73:        if (*p==':') {
                     74:                *p = 0;
2.20      timbl      75:                parts->access = after_access; /* Scheme has been specified */
2.37      howcome    76: 
1.1       timbl      77:                after_access = p+1;
2.37      howcome    78: 
                     79: /*
                     80:                after_access = p;
                     81:                while (*after_access == 0)      /* HWL 15/10/94: weird bug on HP *//*
                     82:                    after_access++;             /* after_access = p + 1 *//*
                     83: */
2.22      luotonen   84:                if (0==strcasecomp("URL", parts->access)) {
2.20      timbl      85:                    parts->access = NULL;  /* Ignore IETF's URL: pre-prefix */
                     86:                } else break;
1.1       timbl      87:        }
2.20      timbl      88:        if (*p=='/') break;             /* Access has not been specified */
1.1       timbl      89:        if (*p=='#') break;
                     90:     }
                     91:     
                     92:     for(p=name+length-1; p>=name; p--) {
                     93:        if (*p =='#') {
                     94:            parts->anchor=p+1;
                     95:            *p=0;                               /* terminate the rest */
                     96:        }
                     97:     }
                     98:     p = after_access;
                     99:     if (*p=='/'){
                    100:        if (p[1]=='/') {
                    101:            parts->host = p+2;          /* host has been specified      */
                    102:            *p=0;                       /* Terminate access             */
                    103:            p=strchr(parts->host,'/');  /* look for end of host name if any */
                    104:            if(p) {
                    105:                *p=0;                   /* Terminate host */
                    106:                parts->absolute = p+1;          /* Root has been found */
                    107:            }
                    108:        } else {
                    109:            parts->absolute = p+1;              /* Root found but no host */
                    110:        }           
                    111:     } else {
                    112:         parts->relative = (*after_access) ? after_access : 0;  /* zero for "" */
                    113:     }
                    114: 
2.16      timbl     115: #ifdef OLD_CODE
1.1       timbl     116:     /* Access specified but no host: the anchor was not really one
2.16      timbl     117:        e.g. news:j462#36487@foo.bar -- JFG 10/jul/92, from bug report */
                    118:     /* This kludge doesn't work for example when coming across
                    119:        file:/usr/local/www/fred#123
                    120:        which loses its anchor. Correct approach in news is to
                    121:        escape weird characters not allowed in URL.  TBL 21/dec/93
                    122:     */
1.1       timbl     123:     if (parts->access && ! parts->host && parts->anchor) {
                    124:       *(parts->anchor - 1) = '#';  /* Restore the '#' in the address */
                    125:       parts->anchor = 0;
                    126:     }
2.16      timbl     127: #endif
1.1       timbl     128: 
                    129: #ifdef NOT_DEFINED     /* search is just treated as part of path */
                    130:     {
                    131:         char *p = relative ? relative : absolute;
                    132:        if (p) {
                    133:            char * q = strchr(p, '?');  /* Any search string? */
                    134:            if (q) {
                    135:                *q = 0;                 /* If so, chop that off. */
                    136:                parts->search = q+1;
                    137:            }
                    138:        }
                    139:     }
                    140: #endif
                    141: } /*scan */    
                    142: 
                    143: 
                    144: /*     Parse a Name relative to another name
                    145: **     -------------------------------------
                    146: **
                    147: **     This returns those parts of a name which are given (and requested)
                    148: **     substituting bits from the related name where necessary.
                    149: **
                    150: ** On entry,
                    151: **     aName           A filename given
2.33      howcome   152: **      relatedName     A name relative to which aName is to be parsed. Give
                    153: **                      it an empty string if aName is absolute.
1.1       timbl     154: **      wanted          A mask for the bits which are wanted.
                    155: **
                    156: ** On exit,
                    157: **     returns         A pointer to a malloc'd string which MUST BE FREED
                    158: */
2.32      frystyk   159: char * HTParse ARGS3(CONST char *, aName, CONST char *, relatedName,
                    160:                     int, wanted)
1.1       timbl     161: {
                    162:     char * result = 0;
                    163:     char * return_value = 0;
                    164:     int len;
                    165:     char * name = 0;
                    166:     char * rel = 0;
                    167:     char * p;
2.12      timbl     168:     char * access;
1.1       timbl     169:     struct struct_parts given, related;
2.33      howcome   170:     
                    171:     if (!relatedName)        /* HWL 23/8/94: dont dump due to NULL */
                    172:         relatedName = "";
1.1       timbl     173:     
                    174:     /* Make working copies of input strings to cut up:
                    175:     */
                    176:     len = strlen(aName)+strlen(relatedName)+10;
                    177:     result=(char *)malloc(len);                /* Lots of space: more than enough */
                    178:     if (result == NULL) outofmem(__FILE__, "HTParse");
                    179:     
                    180:     StrAllocCopy(name, aName);
                    181:     StrAllocCopy(rel, relatedName);
                    182:     
                    183:     scan(name, &given);
                    184:     scan(rel,  &related); 
                    185:     result[0]=0;               /* Clear string  */
2.12      timbl     186:     access = given.access ? given.access : related.access;
1.1       timbl     187:     if (wanted & PARSE_ACCESS)
2.12      timbl     188:         if (access) {
                    189:            strcat(result, access);
1.1       timbl     190:            if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
                    191:        }
                    192:        
                    193:     if (given.access && related.access)        /* If different, inherit nothing. */
                    194:         if (strcmp(given.access, related.access)!=0) {
                    195:            related.host=0;
                    196:            related.absolute=0;
                    197:            related.relative=0;
                    198:            related.anchor=0;
                    199:        }
                    200:        
                    201:     if (wanted & PARSE_HOST)
                    202:         if(given.host || related.host) {
                    203:            if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
                    204:            strcat(result, given.host ? given.host : related.host);
                    205:        }
                    206:        
                    207:     if (given.host && related.host)  /* If different hosts, inherit no path. */
                    208:         if (strcmp(given.host, related.host)!=0) {
                    209:            related.absolute=0;
                    210:            related.relative=0;
                    211:            related.anchor=0;
                    212:        }
                    213:        
                    214:     if (wanted & PARSE_PATH) {
                    215:         if(given.absolute) {                           /* All is given */
                    216:            if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
                    217:            strcat(result, given.absolute);
                    218:        } else if(related.absolute) {   /* Adopt path not name */
                    219:            strcat(result, "/");
                    220:            strcat(result, related.absolute);
                    221:            if (given.relative) {
                    222:                p = strchr(result, '?');        /* Search part? */
                    223:                if (!p) p=result+strlen(result)-1;
                    224:                for (; *p!='/'; p--);   /* last / */
                    225:                p[1]=0;                                 /* Remove filename */
                    226:                strcat(result, given.relative);         /* Add given one */
2.31      frystyk   227:                result = HTSimplify (result);
1.1       timbl     228:            }
                    229:        } else if(given.relative) {
                    230:            strcat(result, given.relative);             /* what we've got */
                    231:        } else if(related.relative) {
                    232:            strcat(result, related.relative);
                    233:        } else {  /* No inheritance */
                    234:            strcat(result, "/");
                    235:        }
                    236:     }
                    237:                
                    238:     if (wanted & PARSE_ANCHOR)
                    239:         if(given.anchor || related.anchor) {
                    240:            if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
                    241:            strcat(result, given.anchor ? given.anchor : related.anchor);
                    242:        }
                    243:     free(rel);
                    244:     free(name);
                    245:     
                    246:     StrAllocCopy(return_value, result);
                    247:     free(result);
                    248:     return return_value;               /* exactly the right length */
                    249: }
                    250: 
2.11      timbl     251: 
2.21      frystyk   252: #if 0  /* NOT USED FOR THE MOMENT */
2.15      luotonen  253: /*
                    254: **     As strcpy() but guaranteed to work correctly
                    255: **     with overlapping parameters.    AL 7 Feb 1994
                    256: */
                    257: PRIVATE void ari_strcpy ARGS2(char *, to,
                    258:                              char *, from)
                    259: {
                    260:     char * tmp;
                    261: 
                    262:     if (!to || !from) return;
                    263: 
                    264:     tmp = (char*)malloc(strlen(from)+1);
                    265:     if (!tmp) outofmem(__FILE__, "my_strcpy");
                    266: 
                    267:     strcpy(tmp, from);
                    268:     strcpy(to, tmp);
                    269:     free(tmp);
                    270: }
2.21      frystyk   271: #endif
                    272: 
2.20      timbl     273: 
                    274: /*             Simplify a URI
                    275: //             --------------
                    276: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1       timbl     277: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20      timbl     278: // Simplification helps us recognize duplicate URIs. 
1.1       timbl     279: //
                    280: //     Thus,   /etc/junk/../fred       becomes /etc/fred
                    281: //             /etc/junk/./fred        becomes /etc/junk/fred
2.11      timbl     282: //
                    283: //      but we should NOT change
                    284: //             http://fred.xxx.edu/../..
                    285: //
                    286: //     or      ../../albert.html
2.26      frystyk   287: //
                    288: // In the same manner, the following prefixed are preserved:
                    289: //
                    290: //     ./<etc>
                    291: //     //<etc>
                    292: //
                    293: // In order to avoid empty URLs the following URLs become:
                    294: //
                    295: //             /fred/..                becomes /fred/..
                    296: //             /fred/././..            becomes /fred/..
2.27      frystyk   297: //             /fred/.././junk/.././   becomes /fred/..
2.26      frystyk   298: //
2.30      frystyk   299: // If more than one set of `://' is found (several proxies in cascade) then
                    300: // only the part after the last `://' is simplified.
1.1       timbl     301: */
2.31      frystyk   302: PUBLIC char *HTSimplify ARGS1(char *, filename)
1.1       timbl     303: {
2.31      frystyk   304:     char *path;
                    305:     char *p;
2.19      frystyk   306: 
2.31      frystyk   307:     if (!filename) {
                    308:        if (URI_TRACE)
                    309:            fprintf(stderr, "HTSimplify.. Bad argument\n");
                    310:        return filename;
                    311:     }
                    312:     if (URI_TRACE)
2.27      frystyk   313:        fprintf(stderr, "HTSimplify.. `%s\' ", filename);
                    314: 
2.31      frystyk   315:     if ((path = strstr(filename, "://")) != NULL) {       /* Find host name */
2.30      frystyk   316:        char *newptr;
2.31      frystyk   317:        path += 3;
                    318:        while ((newptr = strstr(path, "://")) != NULL)
                    319:            path = newptr+3;
                    320:        path = HTCanon(&filename, path);              /* We have a host name */
                    321:     } else if ((path = strstr(filename, ":/")) != NULL) {
                    322:        path += 2;
2.27      frystyk   323:     } else
2.31      frystyk   324:        path = filename;
                    325:     if (*path == '/' && *(path+1)=='/') {        /* Some URLs start //<foo> */
                    326:        path += 1;
2.34      frystyk   327:     } else if (!strncmp(path, "news:", 5)) {
                    328:        char *ptr = strchr(path+5, '@');
                    329:        if (!ptr) ptr = path+5;
                    330:        while (*ptr) {                      /* Make group or host lower case */
                    331:            *ptr = TOLOWER(*ptr);
                    332:            ptr++;
2.31      frystyk   333:        }
                    334:        if (URI_TRACE)
                    335:            fprintf(stderr, "into\n............ `%s'\n", filename);
                    336:        return filename;                      /* Doesn't need to do any more */
                    337:     }
                    338:     if ((p = path)) {
                    339:        int segments = 0;
                    340: 
                    341:        /* Parse string first time to find number of `real' tokens */
                    342:        while (*p) {
                    343:            if (*p=='/' || p==path) {
                    344:                if (!((*(p+1)=='/' || !*(p+1)) ||
                    345:                      (*(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) ||
                    346:                      (*(p+1)=='.' && *(p+2)=='.' &&(*(p+3)=='/' || !*(p+3)))))
                    347:                    segments++;
                    348:            }
                    349:            p++;
                    350:        }
2.19      frystyk   351:        
2.31      frystyk   352:        /* Parse string second time to simplify */
                    353:        p = path;
                    354:        while(*p) {
                    355:            if (*p=='/') {
                    356:                if (p>path && *(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) {
                    357:                    char *orig=p, *dest=p+2;
                    358:                    while ((*orig++ = *dest++)); /* Remove a slash and a dot */
                    359:                    p--;
                    360:                } else if (segments>1 && *(p+1)=='.' && *(p+2)=='.' &&
                    361:                    (*(p+3)=='/' || !*(p+3))) {
                    362:                    char *q = p;
                    363:                    while (q>path && *--q!='/');               /* prev slash */
                    364:                    if (strncmp(q, "/../", 4) && strncmp(q, "/./", 3) &&
                    365:                        strncmp(q, "./", 2)) {
                    366:                        char *orig=q, *dest=p+3;
                    367:                        if (*q!='/') dest++;
                    368:                        while ((*orig++ = *dest++));       /* Remove /xxx/.. */
                    369:                        segments--;
                    370:                        p = q-1;              /* Start again with prev slash */
                    371:                    } else
                    372:                        p++;
                    373:                } else if (*(p+1)=='/') {
                    374:                    while (*(p+1)=='/') {
                    375:                        char *orig=p, *dest=p+1;
                    376:                        while ((*orig++ = *dest++));  /* Remove multiple /'s */
2.19      frystyk   377:                    }
                    378:                }
                    379:            }
2.31      frystyk   380:            p++;
                    381:        }  /* end while (*p) */
2.19      frystyk   382:     }
2.31      frystyk   383:     if (URI_TRACE)
2.27      frystyk   384:        fprintf(stderr, "into\n............ `%s'\n", filename);
2.31      frystyk   385:     return filename;
2.19      frystyk   386: }
2.31      frystyk   387: 
2.19      frystyk   388: #ifdef OLD_CODE
2.17      frystyk   389:     char * p = filename;
1.1       timbl     390:     char * q;
2.17      frystyk   391:     
                    392:     if (p) {
                    393:        while (*p && (*p == '/' || *p == '.'))     /* Pass starting / or .'s */
                    394:            p++;
                    395:        while(*p) {
                    396:            if (*p=='/') {
1.1       timbl     397:            if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11      timbl     398:                for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
                    399:                if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
                    400:                        &&!(q-1>filename && q[-1]=='/')) {
2.15      luotonen  401:                    ari_strcpy(q, p+3);         /* Remove  /xxx/..      */
1.1       timbl     402:                    if (!*filename) strcpy(filename, "/");
                    403:                    p = q-1;            /* Start again with prev slash  */
2.11      timbl     404:                } else {                        /*   xxx/.. leave it!   */
2.9       timbl     405: #ifdef BUG_CODE
2.15      luotonen  406:                    ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm  xxx/../ */
1.1       timbl     407:                    p = filename;               /* Start again */
2.9       timbl     408: #endif
1.1       timbl     409:                }
                    410:            } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15      luotonen  411:                ari_strcpy(p, p+2);             /* Remove a slash and a dot */
2.13      luotonen  412:            } else if (p[-1] != ':') {
                    413:                while (p[1] == '/') {
2.15      luotonen  414:                    ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13      luotonen  415:                }
1.1       timbl     416:            }
2.17      frystyk   417:            }
                    418:            p++;
                    419:        }  /* end while (*p) */
                    420:     } /* end if (p) */
1.1       timbl     421: }
2.19      frystyk   422: #endif /* OLD_CODE */
1.1       timbl     423: 
                    424: 
                    425: /*             Make Relative Name
                    426: **             ------------------
                    427: **
                    428: ** This function creates and returns a string which gives an expression of
                    429: ** one address as related to another. Where there is no relation, an absolute
                    430: ** address is retured.
                    431: **
                    432: **  On entry,
                    433: **     Both names must be absolute, fully qualified names of nodes
                    434: **     (no anchor bits)
                    435: **
                    436: **  On exit,
                    437: **     The return result points to a newly allocated name which, if
                    438: **     parsed by HTParse relative to relatedName, will yield aName.
                    439: **     The caller is responsible for freeing the resulting name later.
                    440: **
                    441: */
2.32      frystyk   442: char * HTRelative ARGS2(CONST char *, aName, CONST char *, relatedName)
1.1       timbl     443: {
                    444:     char * result = 0;
                    445:     CONST char *p = aName;
                    446:     CONST char *q = relatedName;
                    447:     CONST char * after_access = 0;
                    448:     CONST char * path = 0;
                    449:     CONST char * last_slash = 0;
                    450:     int slashes = 0;
                    451:     
                    452:     for(;*p; p++, q++) {       /* Find extent of match */
                    453:        if (*p!=*q) break;
                    454:        if (*p==':') after_access = p+1;
                    455:        if (*p=='/') {
                    456:            last_slash = p;
                    457:            slashes++;
                    458:            if (slashes==3) path=p;
                    459:        }
                    460:     }
                    461:     
                    462:     /* q, p point to the first non-matching character or zero */
                    463:     
                    464:     if (!after_access) {                       /* Different access */
                    465:         StrAllocCopy(result, aName);
                    466:     } else if (slashes<3){                     /* Different nodes */
                    467:        StrAllocCopy(result, after_access);
2.29      frystyk   468: #if 0 /* Henrik */
1.1       timbl     469:     } else if (slashes==3){                    /* Same node, different path */
                    470:         StrAllocCopy(result, path);
2.21      frystyk   471: #endif
1.1       timbl     472:     } else {                                   /* Some path in common */
                    473:         int levels= 0;
                    474:         for(; *q && (*q!='#'); q++)  if (*q=='/') levels++;
                    475:        result = (char *)malloc(3*levels + strlen(last_slash) + 1);
                    476:       if (result == NULL) outofmem(__FILE__, "HTRelative");
                    477:        result[0]=0;
                    478:        for(;levels; levels--)strcat(result, "../");
                    479:        strcat(result, last_slash+1);
                    480:     }
2.31      frystyk   481:     if (URI_TRACE) fprintf(stderr,
2.21      frystyk   482:                      "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
                    483:                       aName, relatedName, result);
1.1       timbl     484:     return result;
                    485: }
2.1       timbl     486: 
                    487: 
2.31      frystyk   488: /*                                                            HTCanon
                    489: **
                    490: **     Canonicalizes the URL in the following manner starting from the host
                    491: **     pointer:
                    492: **
                    493: **     1) The host name is converted to lowercase
                    494: **     2) Expands the host name of the URL from a local name to a full
                    495: **        domain name. A host name is started by `://'.
2.38      frystyk   496: **     3) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
2.31      frystyk   497: **
                    498: **     Return: OK      The position of the current path part of the URL
                    499: */
                    500: PUBLIC char *HTCanon ARGS2 (char **, filename, char *, host)
                    501: {
2.32      frystyk   502:     char *newname = NULL;
2.31      frystyk   503:     char *port;
                    504:     char *strptr;
                    505:     char *path;
2.36      frystyk   506:     char *access = host-3;
2.31      frystyk   507: 
2.36      frystyk   508:     while (access>*filename && *(access-1)!='/')       /* Find access method */
                    509:        access--;
2.31      frystyk   510:     if ((path = strchr(host, '/')) == NULL)                    /* Find path */
                    511:        path = host + strlen(host);
                    512:     if ((strptr = strchr(host, '@')) != NULL && strptr<path)      /* UserId */
                    513:        host = strptr;
2.39    ! frystyk   514:     if ((port = strchr(host, ':')) != NULL && port>path)      /* Port number */
        !           515:        port = NULL;
2.31      frystyk   516: 
                    517:     strptr = host;                                 /* Convert to lower-case */
                    518:     while (strptr<path) {
                    519:        *strptr = TOLOWER(*strptr);
                    520:        strptr++;
                    521:     }
                    522:     
                    523:     /* Does the URL contain a full domain name? This also works for a
                    524:        numerical host name. The domain name is already made lower-case
                    525:        and without a trailing dot. */
2.35      frystyk   526:     if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
                    527:        strncasecomp(host, "localhost", 9)) {
2.31      frystyk   528:        CONST char *domain = HTGetDomainName();
                    529:        if (domain) {
2.32      frystyk   530:            if ((newname = (char *) calloc(1, strlen(*filename) +
2.31      frystyk   531:                                       strlen(domain)+2)) == NULL)
                    532:                outofmem(__FILE__, "HTCanon");
                    533:            if (port)
2.32      frystyk   534:                strncpy(newname, *filename, (int) (port-*filename));
2.31      frystyk   535:            else
2.32      frystyk   536:                strncpy(newname, *filename, (int) (path-*filename));
                    537:            strcat(newname, ".");
                    538:            strcat(newname, domain);
2.31      frystyk   539:        }
                    540:     } else {                                     /* Look for a trailing dot */
                    541:        char *dot = port ? port : path;
                    542:        if (dot > *filename && *--dot=='.') {
                    543:            char *orig=dot, *dest=dot+1;
                    544:            while((*orig++ = *dest++));
                    545:            if (port) port--;
                    546:            path--;
                    547:        }
                    548:     }
2.36      frystyk   549:     /* Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp) */
                    550:     if (port) {
                    551:        if ((!strncmp(access, "http", 4) &&
                    552:             (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
                    553:            (!strncmp(access, "gopher", 6) &&
                    554:             (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
                    555:            (!strncmp(access, "ftp", 3) &&
                    556:             (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
                    557:            if (!newname) {
                    558:                char *orig=port, *dest=port+3;
                    559:                while((*orig++ = *dest++));
                    560:            }
                    561:        } else if (newname)
                    562:            strncat(newname, port, (int) (path-port));
                    563:     }
                    564: 
2.32      frystyk   565:     if (newname) {
                    566:        char *newpath = newname+strlen(newname);
                    567:        strcat(newname, path);
2.31      frystyk   568:        path = newpath;
                    569:        free(*filename);                                    /* Free old copy */
2.32      frystyk   570:        *filename = newname;
2.31      frystyk   571:     }
                    572:     return path;
                    573: }
2.1       timbl     574: 
                    575: 
2.24      luotonen  576: /*                                                     HTCleanTelnetString()
                    577:  *     Make sure that the given string doesn't contain characters that
                    578:  *     could cause security holes, such as newlines in ftp, gopher,
                    579:  *     news or telnet URLs; more specifically: allows everything between
2.26      frystyk   580:  *     ASCII 20-7E, and also A0-FE, inclusive. Also TAB ('\t') allowed!
2.24      luotonen  581:  *
                    582:  * On entry,
                    583:  *     str     the string that is *modified* if necessary.  The
                    584:  *             string will be truncated at the first illegal
                    585:  *             character that is encountered.
                    586:  * On exit,
                    587:  *     returns YES, if the string was modified.
                    588:  *             NO, otherwise.
                    589:  */
                    590: PUBLIC BOOL HTCleanTelnetString ARGS1(char *, str)
                    591: {
                    592:     char * cur = str;
                    593: 
                    594:     if (!str) return NO;
                    595: 
                    596:     while (*cur) {
                    597:        int a = TOASCII(*cur);
2.26      frystyk   598:        if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) ||  a > 0xFE)) {
2.31      frystyk   599:            if (URI_TRACE)
                    600:                fprintf(stderr, "Illegal..... character in URL: \"%s\"\n",str);
2.24      luotonen  601:            *cur = 0;
2.31      frystyk   602:            if (URI_TRACE)
                    603:                fprintf(stderr, "Truncated... \"%s\"\n",str);
2.24      luotonen  604:            return YES;
                    605:        }
                    606:        cur++;
                    607:     }
                    608:     return NO;
                    609: }
                    610: 

Webmaster