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

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

Webmaster