home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTParse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  13.4 KB  |  512 lines

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