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

2.41      frystyk     1: /*                                                                   HTParse.c
                      2: **     URI MANAGEMENT
                      3: **
2.46      frystyk     4: **     (c) COPYRIGHT MIT 1995.
2.41      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
2.26      frystyk     6: **
                      7: ** history:
                      8: **     May 12 94       TAB added as legal char in HTCleanTelnetString
                      9: **
1.1       timbl      10: */
2.31      frystyk    11: 
2.44      frystyk    12: /* Library include files */
2.63    ! frystyk    13: #include "sysdep.h"
1.1       timbl      14: #include "HTUtils.h"
2.38      frystyk    15: #include "HTParse.h"
2.44      frystyk    16: #include "HTString.h"
2.31      frystyk    17: #include "HTTCP.h"
2.6       timbl      18: 
1.1       timbl      19: struct struct_parts {
2.20      timbl      20:        char * access;          /* Now known as "scheme" */
1.1       timbl      21:        char * host;
                     22:        char * absolute;
                     23:        char * relative;
                     24: /*     char * search;          no - treated as part of path */
                     25:        char * anchor;
                     26: };
                     27: 
                     28: /*     Scan a filename for its consituents
                     29: **     -----------------------------------
                     30: **
                     31: ** On entry,
                     32: **     name    points to a document name which may be incomplete.
                     33: ** On exit,
                     34: **      absolute or relative may be nonzero (but not both).
                     35: **     host, anchor and access may be nonzero if they were specified.
                     36: **     Any which are nonzero point to zero terminated strings.
                     37: */
2.60      frystyk    38: PRIVATE void scan (char * name, struct struct_parts * parts)
1.1       timbl      39: {
                     40:     char * after_access;
                     41:     char * p;
                     42:     int length = strlen(name);
                     43:     
                     44:     parts->access = 0;
                     45:     parts->host = 0;
                     46:     parts->absolute = 0;
                     47:     parts->relative = 0;
                     48:     parts->anchor = 0;
                     49:     
                     50:     after_access = name;
                     51:     for(p=name; *p; p++) {
                     52:        if (*p==':') {
                     53:                *p = 0;
2.20      timbl      54:                parts->access = after_access; /* Scheme has been specified */
2.37      howcome    55: 
2.44      frystyk    56: /* The combination of gcc, the "-O" flag and the HP platform is
                     57:    unhealthy. The following three lines is a quick & dirty fix, but is
                     58:    not recommended. Rather, turn off "-O". */
                     59: 
2.42      howcome    60: /*             after_access = p;*/
2.44      frystyk    61: /*             while (*after_access == 0)*/
                     62: /*                 after_access++;*/
2.42      howcome    63: 
1.1       timbl      64:                after_access = p+1;
2.37      howcome    65: 
2.22      luotonen   66:                if (0==strcasecomp("URL", parts->access)) {
2.20      timbl      67:                    parts->access = NULL;  /* Ignore IETF's URL: pre-prefix */
                     68:                } else break;
1.1       timbl      69:        }
2.20      timbl      70:        if (*p=='/') break;             /* Access has not been specified */
1.1       timbl      71:        if (*p=='#') break;
                     72:     }
                     73:     
                     74:     for(p=name+length-1; p>=name; p--) {
                     75:        if (*p =='#') {
                     76:            parts->anchor=p+1;
                     77:            *p=0;                               /* terminate the rest */
                     78:        }
                     79:     }
                     80:     p = after_access;
                     81:     if (*p=='/'){
                     82:        if (p[1]=='/') {
                     83:            parts->host = p+2;          /* host has been specified      */
                     84:            *p=0;                       /* Terminate access             */
                     85:            p=strchr(parts->host,'/');  /* look for end of host name if any */
                     86:            if(p) {
                     87:                *p=0;                   /* Terminate host */
                     88:                parts->absolute = p+1;          /* Root has been found */
                     89:            }
                     90:        } else {
                     91:            parts->absolute = p+1;              /* Root found but no host */
                     92:        }           
                     93:     } else {
2.49      frystyk    94:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.1       timbl      95:     }
2.50      frystyk    96: }
1.1       timbl      97: 
                     98: 
                     99: /*     Parse a Name relative to another name
                    100: **     -------------------------------------
                    101: **
                    102: **     This returns those parts of a name which are given (and requested)
                    103: **     substituting bits from the related name where necessary.
                    104: **
                    105: ** On entry,
                    106: **     aName           A filename given
2.33      howcome   107: **      relatedName     A name relative to which aName is to be parsed. Give
                    108: **                      it an empty string if aName is absolute.
1.1       timbl     109: **      wanted          A mask for the bits which are wanted.
                    110: **
                    111: ** On exit,
                    112: **     returns         A pointer to a malloc'd string which MUST BE FREED
                    113: */
2.63    ! frystyk   114: PUBLIC char * HTParse (const char *aName, const char *relatedName, int wanted)
1.1       timbl     115: {
                    116:     char * result = 0;
                    117:     char * return_value = 0;
                    118:     int len;
                    119:     char * name = 0;
                    120:     char * rel = 0;
                    121:     char * p;
2.12      timbl     122:     char * access;
1.1       timbl     123:     struct struct_parts given, related;
2.33      howcome   124:     
                    125:     if (!relatedName)        /* HWL 23/8/94: dont dump due to NULL */
                    126:         relatedName = "";
1.1       timbl     127:     
2.50      frystyk   128:     /* Make working copies of input strings to cut up: */
1.1       timbl     129:     len = strlen(aName)+strlen(relatedName)+10;
2.61      frystyk   130:     if ((result=(char *) HT_MALLOC(len)) == NULL) /* Lots of space: more than enough */
                    131:        HT_OUTOFMEM("parse space");
1.1       timbl     132:     StrAllocCopy(name, aName);
                    133:     StrAllocCopy(rel, relatedName);
                    134:     
                    135:     scan(name, &given);
                    136:     scan(rel,  &related); 
                    137:     result[0]=0;               /* Clear string  */
2.12      timbl     138:     access = given.access ? given.access : related.access;
1.1       timbl     139:     if (wanted & PARSE_ACCESS)
2.12      timbl     140:         if (access) {
                    141:            strcat(result, access);
1.1       timbl     142:            if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
                    143:        }
                    144:        
                    145:     if (given.access && related.access)        /* If different, inherit nothing. */
                    146:         if (strcmp(given.access, related.access)!=0) {
                    147:            related.host=0;
                    148:            related.absolute=0;
                    149:            related.relative=0;
                    150:            related.anchor=0;
                    151:        }
                    152:        
                    153:     if (wanted & PARSE_HOST)
                    154:         if(given.host || related.host) {
                    155:            if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
                    156:            strcat(result, given.host ? given.host : related.host);
                    157:        }
                    158:        
                    159:     if (given.host && related.host)  /* If different hosts, inherit no path. */
                    160:         if (strcmp(given.host, related.host)!=0) {
                    161:            related.absolute=0;
                    162:            related.relative=0;
                    163:            related.anchor=0;
                    164:        }
                    165:        
                    166:     if (wanted & PARSE_PATH) {
                    167:         if(given.absolute) {                           /* All is given */
                    168:            if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
                    169:            strcat(result, given.absolute);
                    170:        } else if(related.absolute) {   /* Adopt path not name */
                    171:            strcat(result, "/");
                    172:            strcat(result, related.absolute);
                    173:            if (given.relative) {
                    174:                p = strchr(result, '?');        /* Search part? */
                    175:                if (!p) p=result+strlen(result)-1;
                    176:                for (; *p!='/'; p--);   /* last / */
                    177:                p[1]=0;                                 /* Remove filename */
                    178:                strcat(result, given.relative);         /* Add given one */
2.55      frystyk   179: #if 0
2.45      frystyk   180:                result = HTSimplify (&result);
2.55      frystyk   181: #endif
1.1       timbl     182:            }
                    183:        } else if(given.relative) {
                    184:            strcat(result, given.relative);             /* what we've got */
                    185:        } else if(related.relative) {
                    186:            strcat(result, related.relative);
                    187:        } else {  /* No inheritance */
                    188:            strcat(result, "/");
                    189:        }
                    190:     }
                    191:                
                    192:     if (wanted & PARSE_ANCHOR)
2.58      neon      193:        if(given.anchor || related.anchor) {
                    194:            if(given.absolute && given.anchor) {   /*Fixes for relURLs...*/
                    195:                if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
                    196:                strcat(result, given.anchor); 
                    197:            } else if (!(given.absolute) && !(given.anchor)) {
                    198:                strcat(result, "");
                    199:            } else {
                    200:                if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
                    201:                strcat(result, given.anchor ? given.anchor : related.anchor); 
                    202:            }
1.1       timbl     203:        }
2.61      frystyk   204:     HT_FREE(rel);
                    205:     HT_FREE(name);
1.1       timbl     206:     
                    207:     StrAllocCopy(return_value, result);
2.61      frystyk   208:     HT_FREE(result);
1.1       timbl     209:     return return_value;               /* exactly the right length */
                    210: }
2.21      frystyk   211: 
2.20      timbl     212: 
2.53      frystyk   213: /*                                                            HTCanon
                    214: **
                    215: **     Canonicalizes the URL in the following manner starting from the host
                    216: **     pointer:
                    217: **
                    218: **     1) The host name is converted to lowercase
                    219: **     2) Expands the host name of the URL from a local name to a full
                    220: **        domain name. A host name is started by `://'.
                    221: **     3) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                    222: **
                    223: **     Return: OK      The position of the current path part of the URL
                    224: **                     which might be the old one or a new one.
                    225: */
2.60      frystyk   226: PRIVATE char *HTCanon  (char ** filename, char * host)
2.53      frystyk   227: {
                    228:     char *newname = NULL;
                    229:     char *port;
                    230:     char *strptr;
                    231:     char *path;
                    232:     char *access = host-3;
                    233: 
                    234:     while (access>*filename && *(access-1)!='/')       /* Find access method */
                    235:        access--;
                    236:     if ((path = strchr(host, '/')) == NULL)                    /* Find path */
                    237:        path = host + strlen(host);
                    238:     if ((strptr = strchr(host, '@')) != NULL && strptr<path)      /* UserId */
                    239:        host = strptr;
                    240:     if ((port = strchr(host, ':')) != NULL && port>path)      /* Port number */
                    241:        port = NULL;
                    242: 
                    243:     strptr = host;                                 /* Convert to lower-case */
                    244:     while (strptr<path) {
                    245:        *strptr = TOLOWER(*strptr);
                    246:        strptr++;
                    247:     }
                    248:     
                    249:     /* Does the URL contain a full domain name? This also works for a
                    250:        numerical host name. The domain name is already made lower-case
                    251:        and without a trailing dot. */
                    252:     if (((strptr = strchr(host, '.')) == NULL || strptr >= path) &&
                    253:        strncasecomp(host, "localhost", 9)) {
2.63    ! frystyk   254:        const char *domain = HTGetDomainName();
2.53      frystyk   255:        if (domain && *domain) {
2.61      frystyk   256:            if ((newname = (char *) HT_CALLOC(1, strlen(*filename) + strlen(domain)+2)) == NULL)
                    257:                HT_OUTOFMEM("HTCanon");
2.53      frystyk   258:            if (port)
                    259:                strncpy(newname, *filename, (int) (port-*filename));
                    260:            else
                    261:                strncpy(newname, *filename, (int) (path-*filename));
                    262:            strcat(newname, ".");
                    263:            strcat(newname, domain);
                    264:        }
                    265:     } else {                                     /* Look for a trailing dot */
                    266:        char *dot = port ? port : path;
                    267:        if (dot > *filename && *--dot=='.') {
                    268:            char *orig=dot, *dest=dot+1;
                    269:            while((*orig++ = *dest++));
                    270:            if (port) port--;
                    271:            path--;
                    272:        }
                    273:     }
                    274:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
                    275:     if (port) {
                    276:        if (!*(port+1) || *(port+1)=='/') {
                    277:            if (!newname) {
                    278:                char *orig=port, *dest=port+1;
                    279:                while((*orig++ = *dest++));
                    280:            }
                    281:        } else if ((!strncmp(access, "http", 4) &&
                    282:             (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
                    283:            (!strncmp(access, "gopher", 6) &&
                    284:             (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)=='/'||!*(port+3)))) ||
                    285:            (!strncmp(access, "ftp", 3) &&
                    286:             (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)=='/'||!*(port+3))))) {
                    287:            if (!newname) {
                    288:                char *orig=port, *dest=port+3;
                    289:                while((*orig++ = *dest++));
2.54      frystyk   290:                path -= 3;             /* Update path position, Henry Minsky */
2.53      frystyk   291:            }
                    292:        } else if (newname)
                    293:            strncat(newname, port, (int) (path-port));
                    294:     }
                    295: 
                    296:     if (newname) {
                    297:        char *newpath = newname+strlen(newname);
                    298:        strcat(newname, path);
                    299:        path = newpath;
2.61      frystyk   300:        HT_FREE(*filename);                                 /* Free old copy */
2.53      frystyk   301:        *filename = newname;
                    302:     }
                    303:     return path;
                    304: }
                    305: 
2.20      timbl     306: /*             Simplify a URI
                    307: //             --------------
                    308: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1       timbl     309: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20      timbl     310: // Simplification helps us recognize duplicate URIs. 
1.1       timbl     311: //
                    312: //     Thus,   /etc/junk/../fred       becomes /etc/fred
                    313: //             /etc/junk/./fred        becomes /etc/junk/fred
2.11      timbl     314: //
                    315: //      but we should NOT change
                    316: //             http://fred.xxx.edu/../..
                    317: //
                    318: //     or      ../../albert.html
2.26      frystyk   319: //
                    320: // In order to avoid empty URLs the following URLs become:
                    321: //
                    322: //             /fred/..                becomes /fred/..
                    323: //             /fred/././..            becomes /fred/..
2.27      frystyk   324: //             /fred/.././junk/.././   becomes /fred/..
2.26      frystyk   325: //
2.30      frystyk   326: // If more than one set of `://' is found (several proxies in cascade) then
                    327: // only the part after the last `://' is simplified.
2.44      frystyk   328: //
                    329: // Returns: A string which might be the old one or a new one.
1.1       timbl     330: */
2.60      frystyk   331: PUBLIC char *HTSimplify (char ** url)
1.1       timbl     332: {
2.31      frystyk   333:     char *path;
                    334:     char *p;
2.55      frystyk   335:     if (!url || !*url) {
2.62      eric      336:        if (URI_TRACE) HTTrace("HTSimplify.. Nothing done\n");
2.55      frystyk   337:        return *url;
2.31      frystyk   338:     }
2.62      eric      339:     if (URI_TRACE) HTTrace("HTSimplify.. `%s\' ", *url);
2.27      frystyk   340: 
2.55      frystyk   341:     /* Find any scheme name */
                    342:     if ((path = strstr(*url, "://")) != NULL) {                   /* Find host name */
2.30      frystyk   343:        char *newptr;
2.60      frystyk   344:        char *access = *url;
                    345:        while (access<path && (*access=TOLOWER(*access))) access++;
2.31      frystyk   346:        path += 3;
2.60      frystyk   347:        while ((newptr = strstr(path, "://")) != NULL)        /* For proxies */
2.31      frystyk   348:            path = newptr+3;
2.55      frystyk   349:        path = HTCanon(url, path);                    /* We have a host name */
                    350:     } else if ((path = strstr(*url, ":/")) != NULL) {
2.31      frystyk   351:        path += 2;
2.27      frystyk   352:     } else
2.55      frystyk   353:        path = *url;
2.31      frystyk   354:     if (*path == '/' && *(path+1)=='/') {        /* Some URLs start //<foo> */
                    355:        path += 1;
2.34      frystyk   356:     } else if (!strncmp(path, "news:", 5)) {
                    357:        char *ptr = strchr(path+5, '@');
                    358:        if (!ptr) ptr = path+5;
                    359:        while (*ptr) {                      /* Make group or host lower case */
                    360:            *ptr = TOLOWER(*ptr);
                    361:            ptr++;
2.31      frystyk   362:        }
                    363:        if (URI_TRACE)
2.62      eric      364:            HTTrace("into\n............ `%s'\n", *url);
2.55      frystyk   365:        return *url;                  /* Doesn't need to do any more */
2.31      frystyk   366:     }
                    367:     if ((p = path)) {
2.55      frystyk   368:        char *end;
                    369:        if (!((end = strchr(path, ';')) || (end = strchr(path, '?')) ||
                    370:              (end = strchr(path, '#'))))
                    371:            end = path+strlen(path);
2.31      frystyk   372: 
                    373:        /* Parse string second time to simplify */
                    374:        p = path;
2.55      frystyk   375:        while(p<end) {
2.31      frystyk   376:            if (*p=='/') {
2.56      frystyk   377:                if (p>*url && *(p+1)=='.' && (*(p+2)=='/' || !*(p+2))) {
                    378:                    char *orig = p+1;
                    379:                    char *dest = (*(p+2)!='/') ? p+2 : p+3;
2.31      frystyk   380:                    while ((*orig++ = *dest++)); /* Remove a slash and a dot */
2.55      frystyk   381:                    end = orig-1;
2.56      frystyk   382:                } else if (*(p+1)=='.' && *(p+2)=='.' && (*(p+3)=='/' || !*(p+3))) {
2.31      frystyk   383:                    char *q = p;
                    384:                    while (q>path && *--q!='/');               /* prev slash */
2.56      frystyk   385:                    if (strncmp(q, "/../", 4)) {
                    386:                        char *orig = q+1;
                    387:                        char *dest = (*(p+3)!='/') ? p+3 : p+4;
2.31      frystyk   388:                        while ((*orig++ = *dest++));       /* Remove /xxx/.. */
2.55      frystyk   389:                        end = orig-1;
2.56      frystyk   390:                        p = q;                /* Start again with prev slash */
2.31      frystyk   391:                    } else
                    392:                        p++;
                    393:                } else if (*(p+1)=='/') {
                    394:                    while (*(p+1)=='/') {
                    395:                        char *orig=p, *dest=p+1;
                    396:                        while ((*orig++ = *dest++));  /* Remove multiple /'s */
2.55      frystyk   397:                        end = orig-1;
2.19      frystyk   398:                    }
2.56      frystyk   399:                } else
                    400:                    p++;
                    401:            } else
                    402:                p++;
                    403:        }
2.19      frystyk   404:     }
2.31      frystyk   405:     if (URI_TRACE)
2.62      eric      406:        HTTrace("into\n............ `%s'\n", *url);
2.55      frystyk   407:     return *url;
2.19      frystyk   408: }
2.31      frystyk   409: 
1.1       timbl     410: /*             Make Relative Name
                    411: **             ------------------
                    412: **
                    413: ** This function creates and returns a string which gives an expression of
                    414: ** one address as related to another. Where there is no relation, an absolute
                    415: ** address is retured.
                    416: **
                    417: **  On entry,
                    418: **     Both names must be absolute, fully qualified names of nodes
                    419: **     (no anchor bits)
                    420: **
                    421: **  On exit,
                    422: **     The return result points to a newly allocated name which, if
                    423: **     parsed by HTParse relative to relatedName, will yield aName.
                    424: **     The caller is responsible for freeing the resulting name later.
                    425: **
                    426: */
2.63    ! frystyk   427: PUBLIC char * HTRelative (const char * aName, const char * relatedName)
1.1       timbl     428: {
                    429:     char * result = 0;
2.63    ! frystyk   430:     const char *p = aName;
        !           431:     const char *q = relatedName;
        !           432:     const char * after_access = 0;
        !           433:     const char * path = 0;
        !           434:     const char * last_slash = 0;
1.1       timbl     435:     int slashes = 0;
                    436:     
                    437:     for(;*p; p++, q++) {       /* Find extent of match */
                    438:        if (*p!=*q) break;
                    439:        if (*p==':') after_access = p+1;
                    440:        if (*p=='/') {
                    441:            last_slash = p;
                    442:            slashes++;
                    443:            if (slashes==3) path=p;
                    444:        }
                    445:     }
                    446:     
                    447:     /* q, p point to the first non-matching character or zero */
                    448:     
                    449:     if (!after_access) {                       /* Different access */
                    450:         StrAllocCopy(result, aName);
                    451:     } else if (slashes<3){                     /* Different nodes */
                    452:        StrAllocCopy(result, after_access);
                    453:     } else {                                   /* Some path in common */
                    454:         int levels= 0;
                    455:         for(; *q && (*q!='#'); q++)  if (*q=='/') levels++;
2.61      frystyk   456:        if ((result = (char  *) HT_MALLOC(3*levels + strlen(last_slash) + 1)) == NULL)
                    457:            HT_OUTOFMEM("HTRelative");
1.1       timbl     458:        result[0]=0;
                    459:        for(;levels; levels--)strcat(result, "../");
                    460:        strcat(result, last_slash+1);
                    461:     }
2.62      eric      462:     if (URI_TRACE) HTTrace(
2.21      frystyk   463:                      "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
                    464:                       aName, relatedName, result);
1.1       timbl     465:     return result;
                    466: }
2.1       timbl     467: 
2.24      luotonen  468: /*                                                     HTCleanTelnetString()
                    469:  *     Make sure that the given string doesn't contain characters that
                    470:  *     could cause security holes, such as newlines in ftp, gopher,
                    471:  *     news or telnet URLs; more specifically: allows everything between
2.26      frystyk   472:  *     ASCII 20-7E, and also A0-FE, inclusive. Also TAB ('\t') allowed!
2.24      luotonen  473:  *
                    474:  * On entry,
                    475:  *     str     the string that is *modified* if necessary.  The
                    476:  *             string will be truncated at the first illegal
                    477:  *             character that is encountered.
                    478:  * On exit,
                    479:  *     returns YES, if the string was modified.
                    480:  *             NO, otherwise.
                    481:  */
2.60      frystyk   482: PUBLIC BOOL HTCleanTelnetString (char * str)
2.24      luotonen  483: {
                    484:     char * cur = str;
                    485: 
                    486:     if (!str) return NO;
                    487: 
                    488:     while (*cur) {
2.52      frystyk   489:        int a = TOASCII((unsigned char) *cur);
2.26      frystyk   490:        if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) ||  a > 0xFE)) {
2.31      frystyk   491:            if (URI_TRACE)
2.62      eric      492:                HTTrace("Illegal..... character in URL: \"%s\"\n",str);
2.24      luotonen  493:            *cur = 0;
2.31      frystyk   494:            if (URI_TRACE)
2.62      eric      495:                HTTrace("Truncated... \"%s\"\n",str);
2.24      luotonen  496:            return YES;
                    497:        }
                    498:        cur++;
                    499:     }
                    500:     return NO;
                    501: }
                    502: 

Webmaster