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

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

Webmaster