Diff for /Amaya/amaya/AHTURLTools.c between versions 1.28 and 1.29

version 1.28, 1997/05/26 09:01:50 version 1.29, 1997/05/29 09:33:51
Line 14 Line 14
  *   *
  */   */
     
 /* Amaya includes  */  
 #define THOT_EXPORT extern  #define THOT_EXPORT extern
 #include "amaya.h"  #include "amaya.h"
   
 #include "init_f.h"  #include "init_f.h"
 #include "AHTURLTools_f.h"  #include "AHTURLTools_f.h"
   
 /* Local definitions */  
   
 #define MAX_PRINT_URL_LENGTH 50  #define MAX_PRINT_URL_LENGTH 50
   typedef struct _HTURI {
       char * access;              /* Now known as "scheme" */
       char * host;
       char * absolute;
       char * relative;
       char * fragment;
   } HTURI;
   
   
 /*----------------------------------------------------------------------  /*----------------------------------------------------------------------
Line 104  char              **file; Line 108  char              **file;
    if (curr < url)     if (curr < url)
       goto finished;        goto finished;
   
    /* search for the "/" indicating the host name start */     /* search for the DIR_STR indicating the host name start */
    while ((curr > url) && ((*curr != DIR_SEP) || (*(curr + 1) != DIR_SEP)))     while ((curr > url) && ((*curr != DIR_SEP) || (*(curr + 1) != DIR_SEP)))
       curr--;        curr--;
   
Line 132  char              **file; Line 136  char              **file;
    if (curr < url)     if (curr < url)
       goto finished;        goto finished;
   
    if (*curr == ':')     if (*curr == PATH_SEP)
      {       {
         *curr = EOS;          *curr = EOS;
         curr--;          curr--;
Line 471  char               *docName; Line 475  char               *docName;
                       AMAYA_PARSE_PUNCTUATION);                        AMAYA_PARSE_PUNCTUATION);
        if (ptr && !strcmp (ptr, newName))         if (ptr && !strcmp (ptr, newName))
          {           {
            /* it has this form, we complete it by adding a "/"  */             /* it has this form, we complete it by adding a DIR_STR  */
            strcat (newName, "/");             strcat (newName, DIR_STR);
          }           }
        if (ptr)         if (ptr)
          TtaFreeMemory (ptr);           TtaFreeMemory (ptr);
Line 513  char               *docName; Line 517  char               *docName;
                                                 AMAYA_PARSE_PUNCTUATION);                                                  AMAYA_PARSE_PUNCTUATION);
                    if (ptr && !strcmp (ptr, basename))                     if (ptr && !strcmp (ptr, basename))
                      {                       {
                      /* it has this form, we complete it by adding a "/"  */                       /* it has this form, we complete it by adding a DIR_STR  */
                      strcat (basename, "/");                       strcat (basename, DIR_STR);
                      length++;                       length++;
                      }                       }
                    if (ptr)                     if (ptr)
Line 535  char               *docName; Line 539  char               *docName;
                  /* found the last DIR_SEP char, end the string there */                   /* found the last DIR_SEP char, end the string there */
                  basename[length + 1] = EOS;                                  basename[length + 1] = EOS;               
                else                 else
                  /* search for the first ":" char */                   /* search for the first PATH_STR char */
                  {                   {
                    for (length = 0; basename[length] != ':' &&                      for (length = 0; basename[length] != PATH_SEP && 
                           basename[length] != EOS; length++);                            basename[length] != EOS; length++);
                    if (basename[length] == ':')                     if (basename[length] == PATH_SEP)
                      /* found, so end the string there */                       /* found, so end the string there */
                      basename[length + 1] = EOS;                       basename[length + 1] = EOS;
                    else                     else
Line 571  char               *docName; Line 575  char               *docName;
                  /* found the last DIR_SEP char, end the string there */                   /* found the last DIR_SEP char, end the string there */
                  basename[length + 1] = EOS;                                  basename[length + 1] = EOS;               
                else                 else
                  /* search for the first ":" char */                   /* search for the first PATH_STR char */
                  {                   {
                    for (length = 0; basename[length] != ':' &&                      for (length = 0; basename[length] != PATH_SEP && 
                           basename[length] != EOS; length ++);                            basename[length] != EOS; length ++);
                    if (basename[length] == ':')                     if (basename[length] == PATH_SEP)
                      /* found, so end the string there */                       /* found, so end the string there */
                      basename[length + 1] = EOS;                       basename[length + 1] = EOS;
                    else                     else
Line 595  char               *docName; Line 599  char               *docName;
   
        if (ptr)         if (ptr)
          {           {
            AmayaSimplifyUrl (&ptr);             SimplifyUrl (&ptr);
            strcpy (newName, ptr);             strcpy (newName, ptr);
            TtaFreeMemory (ptr);             TtaFreeMemory (ptr);
          }           }
Line 660  char               *path; Line 664  char               *path;
   
    TtaFreeMemory (basename_ptr1);     TtaFreeMemory (basename_ptr1);
    TtaFreeMemory (basename_ptr2);     TtaFreeMemory (basename_ptr2);
   
    return (result);     return (result);
 }  }
   
   
 /*----------------------------------------------------------------------  /*----------------------------------------------------------------------
   AHTMakeRelativeURL                                                  
   converts url into a relative url to base_url.  
   If succesful, returns the new URL, otherwise, it returns NULL.  
   The caller has to free the new URL.  
   ----------------------------------------------------------------------*/  
 #ifdef __STDC__  
 char           *AHTMakeRelativeName (char *url, char *base_url)  
 #else  /* __STDC__ */  
 char           *AHTMakeRelativeName (url, base_url)  
 char            url;  
 char            base_url;  
   
 #endif /* __STDC__ */  
 {  
   char        *base_ptr, *url_ptr;  
   char        *result;  
   
   /* verify if we are in the same host */  
   base_ptr = AmayaParseUrl (base_url, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);  
   url_ptr = AmayaParseUrl (url, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);  
   if (!strcmp (base_ptr, url_ptr))  
     {  
       TtaFreeMemory (base_ptr);  
       TtaFreeMemory (url_ptr);  
         
       /* Normalize the URLs */  
       base_ptr = AmayaParseUrl (base_url, "", AMAYA_PARSE_ALL);  
       url_ptr = AmayaParseUrl (url, "", AMAYA_PARSE_ALL);  
         
       /* Use libwww to make relative name */  
       result = AmayaRelativeUrl (url_ptr, base_ptr);  
       TtaFreeMemory (base_ptr);  
       TtaFreeMemory (url_ptr);  
     }  
   else  
     result = (char *) NULL;  
     
   return (result);  
 }  
 /*----------------------------------------------------------------------  
   HasKnownFileSuffix    HasKnownFileSuffix
   returns TRUE if path points to a file ending with a suffix.    returns TRUE if path points to a file ending with a suffix.
   ----------------------------------------------------------------------*/    ----------------------------------------------------------------------*/
Line 716  boolean             HasKnownFileSuffix ( Line 679  boolean             HasKnownFileSuffix (
 char               *path;  char               *path;
 #endif /* __STDC__ */  #endif /* __STDC__ */
 {  {
    char                *root;     char            *root;
    char                temppath[MAX_LENGTH];     char             temppath[MAX_LENGTH];
    char                suffix[MAX_LENGTH];     char             suffix[MAX_LENGTH];
   
    if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)     if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
      return (FALSE);       return (FALSE);
   
    root = AmayaParseUrl(path, (char *) NULL, AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);     root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
   
    if (root)      if (root) 
      {       {
Line 792  char *inputURL; Line 755  char *inputURL;
   
   len = strlen (inputURL);    len = strlen (inputURL);
   if (len <= MAX_PRINT_URL_LENGTH)     if (len <= MAX_PRINT_URL_LENGTH) 
     {      strcpy (outputURL, inputURL);
       strcpy (outputURL, inputURL);  
     }  
   else    else
     /* make a truncated urlName on the status window */      /* make a truncated urlName on the status window */
     {      {
Line 805  char *inputURL; Line 766  char *inputURL;
     }      }
 }  }
   
 /************************************************************************  
  *                                                                      *  
  *  Local Adaptation of the libWWW Library/src/AmayaParseUrl.c code.    *  
  *                                                                      *  
  ************************************************************************/  
   
 #define StringAllocCopy(dest,src) {                                     \  
     if (src == NULL) dest = NULL;                                       \  
     else {                                                              \  
         if ((dest = (char *) TtaGetMemory(strlen(src) + 1)) == NULL)    \  
                 exit(1);                                                \  
         else strcpy(dest, src); }}                                      \  
   
 typedef struct _HTURI {  
     char * access;              /* Now known as "scheme" */  
     char * host;  
     char * absolute;  
     char * relative;  
     char * fragment;  
 } HTURI;  
   
 /*----------------------------------------------------------------------  /*----------------------------------------------------------------------
    scan     scan
Line 862  HTURI               *parts; Line 803  HTURI               *parts;
     {      {
       if (*p==DIR_SEP || *p=='#' || *p=='?')        if (*p==DIR_SEP || *p=='#' || *p=='?')
         break;          break;
       if (*p==':')        if (*p==PATH_SEP)
         {          {
           *p = 0;            *p = 0;
           parts->access = after_access; /* Scheme has been specified */            parts->access = after_access; /* Scheme has been specified */
Line 934  int            wanted; Line 875  int            wanted;
   
 #endif /* __STDC__ */  #endif /* __STDC__ */
 {  {
     char * result = 0;    char      *return_value;
     char * return_value = 0;    char       result[MAX_LENGTH];
     int len;    char       name[MAX_LENGTH];
     char * name = 0;    char       rel[MAX_LENGTH];
     char * rel = 0;    char      *p, *access;
     char * p;    HTURI      given, related;
     char * access;    int        len;
     HTURI given, related;  
       
     if (!relatedName)        /* HWL 23/8/94: dont dump due to NULL */  
         relatedName = "";  
       
     /* Make working copies of input strings to cut up: */  
     len = strlen(aName)+strlen(relatedName)+10;  
     if ((result=(char *) TtaGetMemory(len)) == NULL) /* Lots of space: more than enough */  
         exit(1);  
     StringAllocCopy(name, aName);  
     StringAllocCopy(rel, relatedName);  
           
     scan(name, &given);    /* Make working copies of input strings to cut up: */
     scan(rel,  &related);     return_value = NULL;
     result[0]=0;                /* Clear string  */    result[0] = 0;                /* Clear string  */
     access = given.access ? given.access : related.access;    strcpy (name, aName);
     if (wanted & AMAYA_PARSE_ACCESS)    if (relatedName != NULL)  
         if (access) {      strcpy (rel, relatedName);
             strcat(result, access);    else
             if(wanted & AMAYA_PARSE_PUNCTUATION) strcat(result, ":");      relatedName[0] = EOS;
         }    
             scan (name, &given);
     if (given.access && related.access) /* If different, inherit nothing. */    scan (rel,  &related); 
         if (strcmp(given.access, related.access)!=0) {    access = given.access ? given.access : related.access;
             related.host=0;    if (wanted & AMAYA_PARSE_ACCESS)
             related.absolute=0;      if (access)
             related.relative=0;        {
             related.fragment=0;          strcat (result, access);
         }          if(wanted & AMAYA_PARSE_PUNCTUATION)
                     strcat (result, PATH_STR);
     if (wanted & AMAYA_PARSE_HOST)        }
         if(given.host || related.host) {    
             if(wanted & AMAYA_PARSE_PUNCTUATION) strcat(result, "//");    if (given.access && related.access)
             strcat(result, given.host ? given.host : related.host);      /* If different, inherit nothing. */
         }      if (strcmp (given.access, related.access) != 0)
                 {
     if (given.host && related.host)  /* If different hosts, inherit no path. */          related.host = 0;
         if (strcmp(given.host, related.host)!=0) {          related.absolute = 0;
             related.absolute=0;          related.relative = 0;
             related.relative=0;          related.fragment = 0;
             related.fragment=0;        }
     
     if (wanted & AMAYA_PARSE_HOST)
       if(given.host || related.host)
         {
           if(wanted & AMAYA_PARSE_PUNCTUATION)
             strcat (result, "//");
           strcat (result, given.host ? given.host : related.host);
         }
     
     if (given.host && related.host)
       /* If different hosts, inherit no path. */
       if (strcmp(given.host, related.host) != 0)
         {
           related.absolute = 0;
           related.relative = 0;
           related.fragment = 0;
         }
     
     if (wanted & AMAYA_PARSE_PATH)
       {
         if (given.absolute)
           {
             /* All is given */
             if (wanted & AMAYA_PARSE_PUNCTUATION)
               strcat (result, DIR_STR);
             strcat (result, given.absolute);
         }          }
                 else if (related.absolute)
     if (wanted & AMAYA_PARSE_PATH) {          {
         if(given.absolute) {                            /* All is given */            /* Adopt path not name */
             if(wanted & AMAYA_PARSE_PUNCTUATION) strcat(result, "/");            strcat (result, DIR_STR);
             strcat(result, given.absolute);            strcat (result, related.absolute);
         } else if(related.absolute) {   /* Adopt path not name */            if (given.relative)
             strcat(result, "/");              {
             strcat(result, related.absolute);                /* Search part? */
             if (given.relative) {                p = strchr (result, '?');
                 p = strchr(result, '?');        /* Search part? */                if (!p)
                 if (!p) p=result+strlen(result)-1;                  p=result+strlen(result)-1;
                 for (; *p!=DIR_SEP; p--);       /* last / */                for (; *p!=DIR_SEP; p--); /* last / */
                 p[1]=0;                                 /* Remove filename */                /* Remove filename */
                 strcat(result, given.relative);         /* Add given one */                p[1]=0;
                 /*AmayaSimplifyUrl (&result);*/                /* Add given one */
                 strcat (result, given.relative);
                 /*SimplifyUrl (&result);*/
             }              }
         } else if(given.relative) {  
             strcat(result, given.relative);             /* what we've got */  
         } else if(related.relative) {  
             strcat(result, related.relative);  
         } else {  /* No inheritance */  
             strcat(result, "/");  
         }          }
         else if (given.relative)
           /* what we've got */
           strcat (result, given.relative);
         else if (related.relative)
           strcat (result, related.relative);
         else
           /* No inheritance */
           strcat (result, DIR_STR);
     }      }
                     
     if (wanted & AMAYA_PARSE_ANCHOR)    if (wanted & AMAYA_PARSE_ANCHOR)
         if(given.fragment || related.fragment) {      if (given.fragment || related.fragment)
             if(given.absolute && given.fragment) {   /*Fixes for relURLs...*/        {
                 if(wanted & AMAYA_PARSE_PUNCTUATION) strcat(result, "#");          if (given.absolute && given.fragment)
                 strcat(result, given.fragment);             {
             } else if (!(given.absolute) && !(given.fragment)) {              /*Fixes for relURLs...*/
                 strcat(result, "");              if (wanted & AMAYA_PARSE_PUNCTUATION)
             } else {                strcat (result, "#");
                 if(wanted & AMAYA_PARSE_PUNCTUATION) strcat(result, "#");              strcat (result, given.fragment); 
                 strcat(result, given.fragment ? given.fragment : related.fragment);             }
             }          else if (!(given.absolute) && !(given.fragment))
         }            strcat (result, "");
     TtaFreeMemory(rel);          else
     TtaFreeMemory(name);            {
                   if (wanted & AMAYA_PARSE_PUNCTUATION)
     StringAllocCopy(return_value, result);                strcat (result, "#");
     TtaFreeMemory(result);              strcat (result, given.fragment ? given.fragment : related.fragment); 
     return return_value;                /* exactly the right length */            }
         }
     len = strlen (result);
     if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
       strcpy (return_value, result);
     return (return_value);                /* exactly the right length */
 }  }
   
 /*----------------------------------------------------------------------  /*----------------------------------------------------------------------
Line 1060  char        *host; Line 1025  char        *host;
         path = host + strlen(host);          path = host + strlen(host);
     if ((strptr = strchr(host, '@')) != NULL && strptr<path)       /* UserId */      if ((strptr = strchr(host, '@')) != NULL && strptr<path)       /* UserId */
         host = strptr;          host = strptr;
     if ((port = strchr(host, ':')) != NULL && port>path)      /* Port number */      if ((port = strchr(host, PATH_SEP)) != NULL && port>path)      /* Port number */
         port = NULL;          port = NULL;
   
     strptr = host;                                  /* Convert to lower-case */      strptr = host;                                  /* Convert to lower-case */
Line 1117  char        *host; Line 1082  char        *host;
   
   
 /*----------------------------------------------------------------------  /*----------------------------------------------------------------------
   AmayaSimplifyUrl: simplify a URI    SimplifyUrl: simplify a URI
   A URI is allowed to contain the seqeunce xxx/../ which may be    A URI is allowed to contain the seqeunce xxx/../ which may be
   replaced by "" , and the seqeunce "/./" which may be replaced by "/".    replaced by "" , and the seqeunce "/./" which may be replaced by DIR_STR.
   Simplification helps us recognize duplicate URIs.     Simplification helps us recognize duplicate URIs. 
       
   Thus,         /etc/junk/../fred       becomes /etc/fred    Thus,         /etc/junk/../fred       becomes /etc/fred
Line 1142  char        *host; Line 1107  char        *host;
   Returns: A string which might be the old one or a new one.    Returns: A string which might be the old one or a new one.
   ----------------------------------------------------------------------*/    ----------------------------------------------------------------------*/
 #ifdef __STDC__  #ifdef __STDC__
 void         AmayaSimplifyUrl (char ** url)  void         SimplifyUrl (char ** url)
 #else  /* __STDC__ */  #else  /* __STDC__ */
 void         AmayaSimplifyUrl (url)  void         SimplifyUrl (url)
 char        **url;  char        **url;
 #endif /* __STDC__ */  #endif /* __STDC__ */
 {  {
Line 1292  char               *target; Line 1257  char               *target;
       strcpy (target, src);        strcpy (target, src);
   
    /* remove /../ and /./ */     /* remove /../ and /./ */
    AmayaSimplifyUrl (&target);     SimplifyUrl (&target);
    return (change);     return (change);
 }  }
   
   
 /*----------------------------------------------------------------------  /*----------------------------------------------------------------------
   AmayaRelativeUrl: make Relative Name    MakeRelativeUrl: make relative name
       
   This function creates and returns a string which gives an expression of    This function creates and returns a string which gives an expression of
   one address as related to another. Where there is no relation, an absolute    one address as related to another. Where there is no relation, an absolute
Line 1314  char               *target; Line 1279  char               *target;
         The caller is responsible for freeing the resulting name later.          The caller is responsible for freeing the resulting name later.
   ----------------------------------------------------------------------*/    ----------------------------------------------------------------------*/
 #ifdef __STDC__  #ifdef __STDC__
 char            *AmayaRelativeUrl (char *aName, char *relatedName)  char            *MakeRelativeUrl (char *aName, char *relatedName)
 #else  /* __STDC__ */  #else  /* __STDC__ */
 char            *AmayaRelativeUrl (aName, relatedName)  char            *MakeRelativeUrl (aName, relatedName)
 char            *aName;  char            *aName;
 char            *relatedName;  char            *relatedName;
 #endif  /* __STDC__ */  #endif  /* __STDC__ */
 {  {
   char               *result = 0;    char      *return_value;
   const char         *p = aName;    char       result[MAX_LENGTH];
   const char         *q = relatedName;    char          *p;
   const char         *after_access = 0;    char          *q = relatedName;
   const char         *last_slash = 0;    char          *after_access;
   int                 slashes = 0;    char          *last_slash = NULL;
         int            slashes, levels, len;
   for (;*p; p++, q++)  
     if (aName == NULL || relatedName == NULL)
       return (NULL);
   
     slashes = 0;
     after_access = NULL;
     p = aName;
     q = relatedName;
     for (; *p && (*p == *q); p++, q++)
     {      {
       /* Find extent of match */        /* Find extent of match */
       if (*p != *q)        if (*p == PATH_SEP)
         break;          after_access = p + 1;
       if (*p == ':')  
         after_access = p+1;  
       if (*p == DIR_SEP)        if (*p == DIR_SEP)
         {          {
             /* memorize the last slash position and count them */
           last_slash = p;            last_slash = p;
           slashes++;            slashes++;
         }          }
     }      }
           
     /* q, p point to the first non-matching character or zero */      /* q, p point to the first non-matching character or zero */
     if (!after_access)      if ((slashes < 2 && after_access == NULL)
           || (slashes < 3 && after_access != NULL))
       {        {
         /* Different access */          /* Local files or remote files whitout common path */
         StringAllocCopy (result, aName);          /* exactly the right length */
       }          len = strlen (aName);
     else if (slashes < 3)          if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
       {            strcpy (return_value, aName);
         /* Different nodes */  
         StringAllocCopy(result, after_access);  
       }        }
     else      else
       {        {
         /* Some path in common */          /* Some path in common */
         int levels= 0;          if (slashes == 3 && strncmp (aName, "http:", 5) != 0)
         for (; *q && (*q!='#'); q++)            /* just the same server */
           if (*q==DIR_SEP)            strcpy (result, last_slash);
             levels++;          else
         result = (char  *) TtaGetMemory (3*levels + strlen(last_slash) + 1);            {
         if (result == NULL)              levels= 0; 
           exit(1);              for (; *q && (*q != '#'); q++)
         result[0] = 0;                if (*q == DIR_SEP)
         for (;levels; levels--)                  levels++;
           strcat (result, "../");              
         strcat (result, last_slash+1);              result[0] = 0;
               for (;levels; levels--)
                 strcat (result, "../");
               strcat (result, last_slash+1);
             } 
   
           /* exactly the right length */
           len = strlen (result);
           if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
             strcpy (return_value, result);
     }      }
     return result;    return (return_value);
 }  }

Removed from v.1.28  
changed lines
  Added in v.1.29


Webmaster