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

1.1       timbl       1: /*             Parse HyperText Document Address                HTParse.c
                      2: **             ================================
                      3: */
                      4: 
                      5: #include "HTUtils.h"
                      6: #include "HTParse.h"
                      7: #include "tcp.h"
                      8: 
2.6       timbl       9: #define HEX_ESCAPE '%'
                     10: 
1.1       timbl      11: struct struct_parts {
                     12:        char * access;
                     13:        char * host;
                     14:        char * absolute;
                     15:        char * relative;
                     16: /*     char * search;          no - treated as part of path */
                     17:        char * anchor;
                     18: };
                     19: 
                     20: 
                     21: /*     Strip white space off a string
                     22: **     ------------------------------
                     23: **
                     24: ** On exit,
                     25: **     Return value points to first non-white character, or to 0 if none.
                     26: **     All trailing white space is OVERWRITTEN with zero.
                     27: */
                     28: 
                     29: #ifdef __STDC__
                     30: char * HTStrip(char * s)
                     31: #else
                     32: char * HTStrip(s)
                     33:        char *s;
                     34: #endif
                     35: {
                     36: #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n')) 
                     37:     char * p=s;
                     38:     for(p=s;*p;p++);                   /* Find end of string */
                     39:     for(p--;p>=s;p--) {
                     40:        if(SPACE(*p)) *p=0;     /* Zap trailing blanks */
                     41:        else break;
                     42:     }
                     43:     while(SPACE(*s))s++;       /* Strip leading blanks */
                     44:     return s;
                     45: }
                     46: 
                     47: 
                     48: /*     Scan a filename for its consituents
                     49: **     -----------------------------------
                     50: **
                     51: ** On entry,
                     52: **     name    points to a document name which may be incomplete.
                     53: ** On exit,
                     54: **      absolute or relative may be nonzero (but not both).
                     55: **     host, anchor and access may be nonzero if they were specified.
                     56: **     Any which are nonzero point to zero terminated strings.
                     57: */
                     58: #ifdef __STDC__
                     59: PRIVATE void scan(char * name, struct struct_parts *parts)
                     60: #else
                     61: PRIVATE void scan(name, parts)
                     62:     char * name;
                     63:     struct struct_parts *parts;
                     64: #endif
                     65: {
                     66:     char * after_access;
                     67:     char * p;
                     68:     int length = strlen(name);
                     69:     
                     70:     parts->access = 0;
                     71:     parts->host = 0;
                     72:     parts->absolute = 0;
                     73:     parts->relative = 0;
                     74:     parts->anchor = 0;
                     75:     
                     76:     after_access = name;
                     77:     for(p=name; *p; p++) {
                     78:        if (*p==':') {
                     79:                *p = 0;
                     80:                parts->access = name;   /* Access name has been specified */
                     81:                after_access = p+1;
                     82:        }
                     83:        if (*p=='/') break;
                     84:        if (*p=='#') break;
                     85:     }
                     86:     
                     87:     for(p=name+length-1; p>=name; p--) {
                     88:        if (*p =='#') {
                     89:            parts->anchor=p+1;
                     90:            *p=0;                               /* terminate the rest */
                     91:        }
                     92:     }
                     93:     p = after_access;
                     94:     if (*p=='/'){
                     95:        if (p[1]=='/') {
                     96:            parts->host = p+2;          /* host has been specified      */
                     97:            *p=0;                       /* Terminate access             */
                     98:            p=strchr(parts->host,'/');  /* look for end of host name if any */
                     99:            if(p) {
                    100:                *p=0;                   /* Terminate host */
                    101:                parts->absolute = p+1;          /* Root has been found */
                    102:            }
                    103:        } else {
                    104:            parts->absolute = p+1;              /* Root found but no host */
                    105:        }           
                    106:     } else {
                    107:         parts->relative = (*after_access) ? after_access : 0;  /* zero for "" */
                    108:     }
                    109: 
                    110:     /* Access specified but no host: the anchor was not really one
                    111:        e.g. news:j462#36487@foo.bar -- JFG 10/7/92, from bug report */
                    112:     if (parts->access && ! parts->host && parts->anchor) {
                    113:       *(parts->anchor - 1) = '#';  /* Restore the '#' in the address */
                    114:       parts->anchor = 0;
                    115:     }
                    116: 
                    117: #ifdef NOT_DEFINED     /* search is just treated as part of path */
                    118:     {
                    119:         char *p = relative ? relative : absolute;
                    120:        if (p) {
                    121:            char * q = strchr(p, '?');  /* Any search string? */
                    122:            if (q) {
                    123:                *q = 0;                 /* If so, chop that off. */
                    124:                parts->search = q+1;
                    125:            }
                    126:        }
                    127:     }
                    128: #endif
                    129: } /*scan */    
                    130: 
                    131: 
                    132: /*     Parse a Name relative to another name
                    133: **     -------------------------------------
                    134: **
                    135: **     This returns those parts of a name which are given (and requested)
                    136: **     substituting bits from the related name where necessary.
                    137: **
                    138: ** On entry,
                    139: **     aName           A filename given
                    140: **      relatedName     A name relative to which aName is to be parsed
                    141: **      wanted          A mask for the bits which are wanted.
                    142: **
                    143: ** On exit,
                    144: **     returns         A pointer to a malloc'd string which MUST BE FREED
                    145: */
                    146: #ifdef __STDC__
                    147: char * HTParse(const char * aName, const char * relatedName, int wanted)
                    148: #else
                    149: char * HTParse(aName, relatedName, wanted)
                    150:     char * aName;
                    151:     char * relatedName;
                    152:     int wanted;
                    153: #endif
                    154: 
                    155: {
                    156:     char * result = 0;
                    157:     char * return_value = 0;
                    158:     int len;
                    159:     char * name = 0;
                    160:     char * rel = 0;
                    161:     char * p;
                    162:     struct struct_parts given, related;
                    163:     
                    164:     /* Make working copies of input strings to cut up:
                    165:     */
                    166:     len = strlen(aName)+strlen(relatedName)+10;
                    167:     result=(char *)malloc(len);                /* Lots of space: more than enough */
                    168:     if (result == NULL) outofmem(__FILE__, "HTParse");
                    169:     
                    170:     StrAllocCopy(name, aName);
                    171:     StrAllocCopy(rel, relatedName);
                    172:     
                    173:     scan(name, &given);
                    174:     scan(rel,  &related); 
                    175:     result[0]=0;               /* Clear string  */
                    176:     if (wanted & PARSE_ACCESS)
                    177:         if (given.access|| related.access) {
                    178:            strcat(result, given.access ? given.access : related.access);
                    179:            if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
                    180:        }
                    181:        
                    182:     if (given.access && related.access)        /* If different, inherit nothing. */
                    183:         if (strcmp(given.access, related.access)!=0) {
                    184:            related.host=0;
                    185:            related.absolute=0;
                    186:            related.relative=0;
                    187:            related.anchor=0;
                    188:        }
                    189:        
                    190:     if (wanted & PARSE_HOST)
                    191:         if(given.host || related.host) {
                    192:            if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
                    193:            strcat(result, given.host ? given.host : related.host);
                    194:        }
                    195:        
                    196:     if (given.host && related.host)  /* If different hosts, inherit no path. */
                    197:         if (strcmp(given.host, related.host)!=0) {
                    198:            related.absolute=0;
                    199:            related.relative=0;
                    200:            related.anchor=0;
                    201:        }
                    202:        
                    203:     if (wanted & PARSE_PATH) {
                    204:         if(given.absolute) {                           /* All is given */
                    205:            if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
                    206:            strcat(result, given.absolute);
                    207:        } else if(related.absolute) {   /* Adopt path not name */
                    208:            strcat(result, "/");
                    209:            strcat(result, related.absolute);
                    210:            if (given.relative) {
                    211:                p = strchr(result, '?');        /* Search part? */
                    212:                if (!p) p=result+strlen(result)-1;
                    213:                for (; *p!='/'; p--);   /* last / */
                    214:                p[1]=0;                                 /* Remove filename */
                    215:                strcat(result, given.relative);         /* Add given one */
                    216:                HTSimplify (result);
                    217:            }
                    218:        } else if(given.relative) {
                    219:            strcat(result, given.relative);             /* what we've got */
                    220:        } else if(related.relative) {
                    221:            strcat(result, related.relative);
                    222:        } else {  /* No inheritance */
                    223:            strcat(result, "/");
                    224:        }
                    225:     }
                    226:                
                    227:     if (wanted & PARSE_ANCHOR)
                    228:         if(given.anchor || related.anchor) {
                    229:            if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
                    230:            strcat(result, given.anchor ? given.anchor : related.anchor);
                    231:        }
                    232:     free(rel);
                    233:     free(name);
                    234:     
                    235:     StrAllocCopy(return_value, result);
                    236:     free(result);
                    237:     return return_value;               /* exactly the right length */
                    238: }
                    239: 
                    240: /*             Simplify a filename
                    241: //             -------------------
                    242: //
                    243: // A unix-style file is allowed to contain the seqeunce xxx/../ which may be
                    244: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
                    245: // Simplification helps us recognize duplicate filenames.
                    246: //
                    247: //     Thus,   /etc/junk/../fred       becomes /etc/fred
                    248: //             /etc/junk/./fred        becomes /etc/junk/fred
                    249: */
                    250: #ifdef __STDC__
                    251: void HTSimplify(char * filename)
                    252: #else
                    253: void HTSimplify(filename)
                    254:     char * filename;
                    255: #endif
                    256: 
                    257: {
                    258:     char * p;
                    259:     char * q;
2.7       timbl     260:     if (filename[0] && filename[1])    /* Bug fix 12 Mar 93 TBL */
                    261:      for(p=filename+2; *p; p++) {
1.1       timbl     262:         if (*p=='/') {
                    263:            if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
                    264:                for (q=p-1; (q>filename) && (*q!='/'); q--); /* prev slash */
                    265:                if (*q=='/') {
                    266:                    strcpy(q, p+3);     /* Remove  /xxx/..      */
                    267:                    if (!*filename) strcpy(filename, "/");
                    268:                    p = q-1;            /* Start again with prev slash  */
                    269:                } else {                        /*   xxx/..     error?  */
                    270:                    strcpy(filename, p[3] ? p+4 : p+3); /* rm  xxx/../  */
                    271:                    p = filename;               /* Start again */
                    272:                }
                    273:            } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
                    274:                strcpy(p, p+2);                 /* Remove a slash and a dot */
                    275:            }
                    276:        }
                    277:     }
                    278: }
                    279: 
                    280: 
                    281: /*             Make Relative Name
                    282: **             ------------------
                    283: **
                    284: ** This function creates and returns a string which gives an expression of
                    285: ** one address as related to another. Where there is no relation, an absolute
                    286: ** address is retured.
                    287: **
                    288: **  On entry,
                    289: **     Both names must be absolute, fully qualified names of nodes
                    290: **     (no anchor bits)
                    291: **
                    292: **  On exit,
                    293: **     The return result points to a newly allocated name which, if
                    294: **     parsed by HTParse relative to relatedName, will yield aName.
                    295: **     The caller is responsible for freeing the resulting name later.
                    296: **
                    297: */
                    298: #ifdef __STDC__
                    299: char * HTRelative(const char * aName, const char *relatedName)
                    300: #else
                    301: char * HTRelative(aName, relatedName)
                    302:    char * aName;
                    303:    char * relatedName;
                    304: #endif
                    305: {
                    306:     char * result = 0;
                    307:     CONST char *p = aName;
                    308:     CONST char *q = relatedName;
                    309:     CONST char * after_access = 0;
                    310:     CONST char * path = 0;
                    311:     CONST char * last_slash = 0;
                    312:     int slashes = 0;
                    313:     
                    314:     for(;*p; p++, q++) {       /* Find extent of match */
                    315:        if (*p!=*q) break;
                    316:        if (*p==':') after_access = p+1;
                    317:        if (*p=='/') {
                    318:            last_slash = p;
                    319:            slashes++;
                    320:            if (slashes==3) path=p;
                    321:        }
                    322:     }
                    323:     
                    324:     /* q, p point to the first non-matching character or zero */
                    325:     
                    326:     if (!after_access) {                       /* Different access */
                    327:         StrAllocCopy(result, aName);
                    328:     } else if (slashes<3){                     /* Different nodes */
                    329:        StrAllocCopy(result, after_access);
                    330:     } else if (slashes==3){                    /* Same node, different path */
                    331:         StrAllocCopy(result, path);
                    332:     } else {                                   /* Some path in common */
                    333:         int levels= 0;
                    334:         for(; *q && (*q!='#'); q++)  if (*q=='/') levels++;
                    335:        result = (char *)malloc(3*levels + strlen(last_slash) + 1);
                    336:       if (result == NULL) outofmem(__FILE__, "HTRelative");
                    337:        result[0]=0;
                    338:        for(;levels; levels--)strcat(result, "../");
                    339:        strcat(result, last_slash+1);
                    340:     }
                    341:     if (TRACE) fprintf(stderr, "HT: `%s' expressed relative to\n    `%s' is\n   `%s'.",
                    342:                aName, relatedName, result);
                    343:     return result;
                    344: }
2.1       timbl     345: 
                    346: 
2.6       timbl     347: /*             Escape undesirable characters using %           HTEscape()
                    348: **             -------------------------------------
                    349: **
                    350: **     This function takes a pointer to a string in which
                    351: **     some characters may be unacceptable unescaped.
                    352: **     It returns a string which has these characters
                    353: **     represented by a '%' character followed by two hex digits.
                    354: **
                    355: **     Unlike HTUnEscape(), this routine returns a malloced string.
                    356: */
                    357: 
                    358: PRIVATE CONST unsigned char isAcceptable[96] =
                    359: 
                    360: /*     Bit 0           xalpha          -- see HTFile.h
                    361: **     Bit 1           xpalpha         -- as xalpha but with plus.
                    362: **     Bit 3 ...       path            -- as xpalphas but with /
                    363: */
                    364:     /*   0 1 2 3 4 5 6 7 8 9 A B C D E F */
                    365:     {    0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4,      /* 2x   !"#$%&'()*+,-./  */
                    366:          7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,      /* 3x  0123456789:;<=>?  */
                    367:         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,       /* 4x  @ABCDEFGHIJKLMNO  */
                    368:         7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7,       /* 5X  PQRSTUVWXYZ[\]^_  */
                    369:         0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,       /* 6x  `abcdefghijklmno  */
                    370:         7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 };     /* 7X  pqrstuvwxyz{\}~  DEL */
                    371: 
                    372: PRIVATE char *hex = "0123456789ABCDEF";
                    373: 
2.8     ! timbl     374: PUBLIC char * HTEscape ARGS2 (CONST char *, str,
2.6       timbl     375:        unsigned char, mask)
                    376: {
                    377: #define ACCEPTABLE(a)  ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
                    378:     CONST char * p;
                    379:     char * q;
                    380:     char * result;
                    381:     int unacceptable = 0;
                    382:     for(p=str; *p; p++)
                    383:         if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
                    384:                unacceptable++;
                    385:     result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
                    386:     if (result == NULL) outofmem(__FILE__, "HTEscape");
                    387:     for(q=result, p=str; *p; p++) {
                    388:        unsigned char a = TOASCII(*p);
                    389:        if (!ACCEPTABLE(a)) {
                    390:            *q++ = HEX_ESCAPE;  /* Means hex commming */
                    391:            *q++ = hex[a >> 4];
                    392:            *q++ = hex[a & 15];
                    393:        }
                    394:        else *q++ = *p;
                    395:     }
                    396:     *q++ = 0;                  /* Terminate */
                    397:     return result;
                    398: }
                    399: 
                    400: 
2.1       timbl     401: /*             Decode %xx escaped characters                   HTUnEscape()
                    402: **             -----------------------------
                    403: **
                    404: **     This function takes a pointer to a string in which some
                    405: **     characters may have been encoded in %xy form, where xy is
                    406: **     the acsii hex code for character 16x+y.
                    407: **     The string is converted in place, as it will never grow.
                    408: */
                    409: 
                    410: PRIVATE char from_hex ARGS1(char, c)
                    411: {
2.6       timbl     412:     return  c >= '0' && c <= '9' ?  c - '0' 
                    413:            : c >= 'A' && c <= 'F'? c - 'A' + 10
                    414:            : c - 'a' + 10;     /* accept small letters just in case */
2.1       timbl     415: }
                    416: 
                    417: PUBLIC char * HTUnEscape ARGS1( char *, str)
                    418: {
                    419:     char * p = str;
                    420:     char * q = str;
                    421:     while(*p) {
2.6       timbl     422:         if (*p == HEX_ESCAPE) {
2.1       timbl     423:            p++;
                    424:            if (*p) *q = from_hex(*p++) * 16;
                    425:            if (*p) *q = FROMASCII(*q + from_hex(*p++));
                    426:            q++;
                    427:        } else {
                    428:            *q++ = *p++; 
                    429:        }
                    430:     }
                    431:     
                    432:     *q++ = 0;
                    433:     return str;
                    434:     
                    435: } /* HTUnEscape */
                    436: 
                    437: 

Webmaster