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

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

Webmaster