Annotation of Amaya/amaya/AHTURLTools.c, revision 1.51

1.7       cvs         1: /*
                      2:  *
                      3:  *  (c) COPYRIGHT MIT and INRIA, 1996.
                      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
1.9       cvs         7: 
1.10      cvs         8: /*
                      9:  * AHTURLTools.c: contains all the functions for testing, manipulating,
1.25      cvs        10:  * and normalizing URLs. It also contains a local copy of the libWWW
                     11:  * URL parsing functions.
1.10      cvs        12:  *
                     13:  * Authors: J. Kahan, I. Vatton
1.45      cvs        14:  *          R. Guetari (Stuff related to Windows).
1.10      cvs        15:  *
                     16:  */
1.7       cvs        17:  
1.15      cvs        18: #define THOT_EXPORT extern
1.3       cvs        19: #include "amaya.h"
                     20: 
1.8       cvs        21: #include "init_f.h"
                     22: #include "AHTURLTools_f.h"
                     23: 
1.24      cvs        24: #define MAX_PRINT_URL_LENGTH 50
1.29      cvs        25: typedef struct _HTURI {
                     26:     char * access;             /* Now known as "scheme" */
                     27:     char * host;
                     28:     char * absolute;
                     29:     char * relative;
                     30:     char * fragment;
                     31: } HTURI;
1.24      cvs        32: 
1.28      cvs        33: 
                     34: /*----------------------------------------------------------------------
                     35:   ConvertToLowerCase
                     36:   Converts a string to lowercase.
                     37:   ----------------------------------------------------------------------*/
1.22      cvs        38: #ifdef __STDC__
1.38      cvs        39: void         ConvertToLowerCase (char *string)
1.28      cvs        40: #else  /* __STDC__ */
1.38      cvs        41: void         ConvertToLowerCase (string)
1.28      cvs        42: char                *string;
                     43: 
                     44: #endif /* __STDC__ */
                     45: {
                     46:  int i;
                     47: 
                     48:  if (!string)
                     49:    return;
                     50: 
                     51:  for (i = 0; string[i] != EOS; i++)
                     52:    string[i] = tolower (string[i]);
                     53: }
1.22      cvs        54: 
1.8       cvs        55: /*----------------------------------------------------------------------
1.11      cvs        56:   ExplodeURL 
1.8       cvs        57:   ----------------------------------------------------------------------*/
                     58: #ifdef __STDC__
                     59: void                ExplodeURL (char *url, char **proto, char **host, char **dir, char **file)
                     60: #else
                     61: void                ExplodeURL (url, proto, host, dir, file)
                     62: char               *url;
                     63: char              **proto;
                     64: char              **host;
                     65: char              **dir;
                     66: char              **file;
                     67: 
                     68: #endif
                     69: {
1.33      cvs        70:    char            *curr, *temp;
                     71:    char             used_sep;
1.32      cvs        72: 
1.33      cvs        73:    if (url && strchr (url, URL_SEP))
                     74:      used_sep = URL_SEP;
                     75:    else
                     76:      used_sep = DIR_SEP;
1.8       cvs        77: 
                     78:    if ((url == NULL) || (proto == NULL) || (host == NULL) ||
                     79:        (dir == NULL) || (file == NULL))
                     80:       return;
                     81: 
                     82:    /* initialize every pointer */
                     83:    *proto = *host = *dir = *file = NULL;
                     84: 
                     85:    /* skip any leading space */
                     86:    while ((*url == SPACE) || (*url == TAB))
                     87:       url++;
1.9       cvs        88:    curr = url;
                     89:    if (*curr == 0)
1.8       cvs        90:       goto finished;
                     91: 
                     92:    /* go to the end of the URL */
1.9       cvs        93:    while ((*curr != 0) && (*curr != SPACE) && (*curr != '\b') &&
                     94:          (*curr != '\r') && (*curr != EOL))
                     95:       curr++;
1.8       cvs        96: 
                     97:    /* mark the end of the chain */
1.9       cvs        98:    *curr = EOS;
                     99:    curr--;
                    100:    if (curr <= url)
1.8       cvs       101:       goto finished;
                    102: 
                    103:    /* search the next DIR_SEP indicating the beginning of the file name */
                    104:    do
1.11      cvs       105:      curr--;
1.33      cvs       106:    while ((curr >= url) && (*curr != used_sep));
1.11      cvs       107: 
1.9       cvs       108:    if (curr < url)
1.8       cvs       109:       goto finished;
1.9       cvs       110:    *file = curr + 1;
1.8       cvs       111: 
                    112:    /* mark the end of the dir */
1.9       cvs       113:    *curr = EOS;
                    114:    curr--;
                    115:    if (curr < url)
1.8       cvs       116:       goto finished;
                    117: 
1.29      cvs       118:    /* search for the DIR_STR indicating the host name start */
1.33      cvs       119:    while ((curr > url) && ((*curr != used_sep) || (*(curr + 1) != used_sep)))
1.9       cvs       120:       curr--;
1.8       cvs       121: 
                    122:    /* if we found it, separate the host name from the directory */
1.33      cvs       123:    if ((*curr == DIR_SEP) && (*(curr + 1) == used_sep))
1.8       cvs       124:      {
1.9       cvs       125:        *host = temp = curr + 2;
1.33      cvs       126:        while ((*temp != 0) && (*temp != used_sep))
1.8       cvs       127:           temp++;
1.33      cvs       128:        if (*temp == used_sep)
1.8       cvs       129:          {
                    130:             *temp = EOS;
                    131:             *dir = temp + 1;
                    132:          }
                    133:      }
                    134:    else
1.11      cvs       135:      *dir = curr;
                    136: 
1.9       cvs       137:    if (curr <= url)
1.8       cvs       138:       goto finished;
                    139: 
                    140:    /* mark the end of the proto */
1.9       cvs       141:    *curr = EOS;
                    142:    curr--;
                    143:    if (curr < url)
1.8       cvs       144:       goto finished;
                    145: 
1.32      cvs       146:    if (*curr == ':')
1.8       cvs       147:      {
1.9       cvs       148:        *curr = EOS;
                    149:        curr--;
1.8       cvs       150:      }
                    151:    else
                    152:       goto finished;
1.11      cvs       153: 
1.9       cvs       154:    if (curr < url)
1.8       cvs       155:       goto finished;
1.9       cvs       156:    while ((curr > url) && (isalpha (*curr)))
                    157:       curr--;
                    158:    *proto = curr;
1.8       cvs       159: 
                    160:  finished:;
                    161: 
                    162: #ifdef AMAYA_DEBUG
                    163:    fprintf (stderr, "ExplodeURL(%s)\n\t", url);
                    164:    if (*proto)
                    165:       fprintf (stderr, "proto : %s, ", *proto);
                    166:    if (*host)
                    167:       fprintf (stderr, "host : %s, ", *host);
                    168:    if (*dir)
                    169:       fprintf (stderr, "dir : %s, ", *dir);
                    170:    if (*file)
                    171:       fprintf (stderr, "file : %s ", *file);
                    172:    fprintf (stderr, "\n");
                    173: #endif
                    174: 
                    175: }
1.3       cvs       176: 
1.4       cvs       177: /*----------------------------------------------------------------------
1.9       cvs       178:   IsHTMLName                                                         
                    179:   returns TRUE if path points to an HTML resource.
1.4       cvs       180:   ----------------------------------------------------------------------*/
1.3       cvs       181: #ifdef __STDC__
1.34      cvs       182: boolean             IsHTMLName (const char *path)
1.3       cvs       183: #else  /* __STDC__ */
                    184: boolean             IsHTMLName (path)
1.34      cvs       185: const char         *path;
1.3       cvs       186: #endif /* __STDC__ */
                    187: {
1.5       cvs       188:    char                temppath[MAX_LENGTH];
                    189:    char                suffix[MAX_LENGTH];
                    190:    char                nsuffix[MAX_LENGTH];
                    191:    int                 i;
                    192: 
                    193:    if (!path)
1.37      cvs       194:       return (FALSE);
1.5       cvs       195: 
                    196:    strcpy (temppath, path);
                    197:    ExtractSuffix (temppath, suffix);
                    198: 
                    199:    /* Normalize the suffix */
                    200:    i = 0;
1.39      cvs       201:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       202:      {
1.25      cvs       203:        nsuffix[i] = tolower (suffix[i]);
1.13      cvs       204:        i++;
                    205:      }
1.5       cvs       206:    nsuffix[i] = EOS;
1.39      cvs       207:    if ((!strcmp (nsuffix, "html")) ||
                    208:        (!strcmp (nsuffix, "htm")) ||
                    209:        (!strcmp (nsuffix, "shtml")))
                    210:      return (TRUE);
1.22      cvs       211:    else if (!strcmp (nsuffix, "gz"))
1.13      cvs       212:      {
1.39      cvs       213:        /* take into account compressed files */
1.13      cvs       214:        ExtractSuffix (temppath, suffix);       
                    215:        /* Normalize the suffix */
                    216:        i = 0;
1.39      cvs       217:        while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       218:         {
1.25      cvs       219:           nsuffix[i] = tolower (suffix[i]);
1.13      cvs       220:           i++;
                    221:         }
                    222:        nsuffix[i] = EOS;
1.39      cvs       223:        if ((!strcmp (nsuffix, "html")) ||
                    224:           (!strcmp (nsuffix, "htm")) ||
                    225:           (!strcmp (nsuffix, "shtml")))
                    226:         return (TRUE);
                    227:        else
1.13      cvs       228:         return (FALSE);
                    229:      }
                    230:    else
1.39      cvs       231:      return (FALSE);
1.3       cvs       232: }
                    233: 
1.4       cvs       234: /*----------------------------------------------------------------------
1.9       cvs       235:   IsImageName                                
                    236:   returns TRUE if path points to an image resource.
1.4       cvs       237:   ----------------------------------------------------------------------*/
1.3       cvs       238: #ifdef __STDC__
1.34      cvs       239: boolean             IsImageName (const char *path)
1.3       cvs       240: #else  /* __STDC__ */
                    241: boolean             IsImageName (path)
1.34      cvs       242: const char         *path;
1.3       cvs       243: #endif /* __STDC__ */
                    244: {
1.5       cvs       245:    char                temppath[MAX_LENGTH];
                    246:    char                suffix[MAX_LENGTH];
                    247:    char                nsuffix[MAX_LENGTH];
                    248:    int                 i;
                    249: 
                    250:    if (!path)
1.13      cvs       251:       return (FALSE);
1.5       cvs       252: 
                    253:    strcpy (temppath, path);
                    254:    ExtractSuffix (temppath, suffix);
                    255: 
                    256:    /* Normalize the suffix */
                    257:    i = 0;
1.39      cvs       258:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       259:      {
1.25      cvs       260:        nsuffix[i] = tolower (suffix[i]);
1.13      cvs       261:        i++;
                    262:      }
1.5       cvs       263:    nsuffix[i] = EOS;
1.39      cvs       264:    if ((!strcmp (nsuffix, "gif")) || (!strcmp (nsuffix, "xbm")) ||
                    265:        (!strcmp (nsuffix, "xpm")) || (!strcmp (nsuffix, "jpg")) ||
                    266:        (!strcmp (nsuffix, "png")) || (!strcmp (nsuffix, "au")))
                    267:       return (TRUE);
                    268:    return (FALSE);
1.3       cvs       269: }
                    270: 
1.4       cvs       271: /*----------------------------------------------------------------------
1.9       cvs       272:   IsTextName                                                         
1.4       cvs       273:   ----------------------------------------------------------------------*/
1.3       cvs       274: #ifdef __STDC__
1.34      cvs       275: boolean             IsTextName (const char *path)
1.3       cvs       276: #else  /* __STDC__ */
                    277: boolean             IsTextName (path)
1.34      cvs       278: const char         *path;
1.3       cvs       279: 
                    280: #endif /* __STDC__ */
                    281: {
1.5       cvs       282:    char                temppath[MAX_LENGTH];
                    283:    char                suffix[MAX_LENGTH];
                    284:    char                nsuffix[MAX_LENGTH];
                    285:    int                 i;
                    286: 
                    287:    if (!path)
1.13      cvs       288:      return (FALSE);
1.5       cvs       289: 
                    290:    strcpy (temppath, path);
                    291:    ExtractSuffix (temppath, suffix);
                    292: 
                    293:    /* Normalize the suffix */
                    294:    i = 0;
1.39      cvs       295:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.5       cvs       296:      {
1.25      cvs       297:        nsuffix[i] = tolower (suffix[i]);
1.5       cvs       298:        i++;
                    299:      }
                    300:    nsuffix[i] = EOS;
                    301: 
1.39      cvs       302:    if ((!strcmp (nsuffix, "txt")) || (!strcmp (nsuffix, "dtd")))
1.13      cvs       303:       return (TRUE);
1.22      cvs       304:    else if (!strcmp (nsuffix, "gz"))
1.13      cvs       305:      {
1.39      cvs       306:        /* take into account compressed files */
1.13      cvs       307:        ExtractSuffix (temppath, suffix);       
                    308:        /* Normalize the suffix */
                    309:        i = 0;
1.39      cvs       310:        while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       311:         {
1.25      cvs       312:           nsuffix[i] = tolower (suffix[i]);
1.13      cvs       313:           i++;
                    314:         }
                    315:        nsuffix[i] = EOS;
1.39      cvs       316:        if ((!strcmp (nsuffix, "txt")) || (!strcmp (nsuffix, "dtd")))
1.13      cvs       317:         return (TRUE);
                    318:        else
                    319:         return (FALSE);
                    320:      }
                    321:    else
                    322:      return (FALSE);
1.3       cvs       323: }
                    324: 
1.4       cvs       325: /*----------------------------------------------------------------------
1.9       cvs       326:   IsHTTPPath                                     
                    327:   returns TRUE if path is in fact an http URL.
1.4       cvs       328:   ----------------------------------------------------------------------*/
1.3       cvs       329: #ifdef __STDC__
1.34      cvs       330: boolean             IsHTTPPath (const char *path)
1.3       cvs       331: #else  /* __STDC__ */
                    332: boolean             IsHTTPPath (path)
1.34      cvs       333: const char         *path;
1.3       cvs       334: #endif /* __STDC__ */
                    335: {
1.5       cvs       336:    if (!path)
                    337:       return FALSE;
1.3       cvs       338: 
1.5       cvs       339:    if (strncmp (path, "http:", 5) != 0)
                    340:       return FALSE;
                    341:    return TRUE;
1.3       cvs       342: }
                    343: 
1.4       cvs       344: /*----------------------------------------------------------------------
1.9       cvs       345:   IsWithParameters                           
                    346:   returns TRUE if url has a concatenated query string.
1.4       cvs       347:   ----------------------------------------------------------------------*/
1.3       cvs       348: #ifdef __STDC__
1.34      cvs       349: boolean             IsWithParameters (const char *url)
1.3       cvs       350: #else  /* __STDC__ */
1.9       cvs       351: boolean             IsWithParameters (url)
1.34      cvs       352: const char         *url;
1.3       cvs       353: #endif /* __STDC__ */
                    354: {
1.5       cvs       355:    int                 i;
1.3       cvs       356: 
1.9       cvs       357:    if ((!url) || (url[0] == EOS))
1.5       cvs       358:       return FALSE;
1.3       cvs       359: 
1.9       cvs       360:    i = strlen (url) - 1;
                    361:    while (i > 0 && url[i--] != '?')
1.5       cvs       362:       if (i < 0)
                    363:         return FALSE;
1.3       cvs       364: 
1.5       cvs       365:    /* There is a parameter */
                    366:    return TRUE;
1.3       cvs       367: }
                    368: 
1.4       cvs       369: /*----------------------------------------------------------------------
1.9       cvs       370:   IsW3Path                                           
                    371:   returns TRUE if path is in fact a URL.
1.4       cvs       372:   ----------------------------------------------------------------------*/
1.3       cvs       373: #ifdef __STDC__
1.34      cvs       374: boolean             IsW3Path (const char *path)
1.3       cvs       375: #else  /* __STDC__ */
                    376: boolean             IsW3Path (path)
1.34      cvs       377: const char               *path;
1.3       cvs       378: #endif /* __STDC__ */
                    379: {
1.5       cvs       380:    if ((strncmp (path, "http:", 5)) && (strncmp (path, "ftp:", 4)) &&
                    381:        (strncmp (path, "telnet:", 7)) && (strncmp (path, "wais:", 5)) &&
                    382:        (strncmp (path, "news:", 5)) && (strncmp (path, "gopher:", 7)) &&
                    383:        (strncmp (path, "mailto:", 7)) && (strncmp (path, "archie:", 7)))
                    384:       return FALSE;
                    385:    return TRUE;
1.3       cvs       386: }
                    387: 
1.4       cvs       388: /*----------------------------------------------------------------------
1.9       cvs       389:   IsValidProtocol                                                    
                    390:   returns true if the url protocol is supported by Amaya.
1.4       cvs       391:   ----------------------------------------------------------------------*/
1.3       cvs       392: #ifdef __STDC__
1.34      cvs       393: boolean             IsValidProtocol (const char *url)
1.3       cvs       394: #else  /* __STDC__ */
1.9       cvs       395: boolean             IsValidProtocol (url)
1.34      cvs       396: const char         *url;
1.3       cvs       397: #endif /* __STDC__ */
                    398: {
1.26      cvs       399:    if (!strncmp (url, "http:", 5))
1.22      cvs       400:        /* experimental */
1.26      cvs       401:       /***  || !strncmp (url, "ftp:", 4)) ***/
1.24      cvs       402:      /*** || !strncmp (path, "news:", 5)***/ 
1.8       cvs       403:       return (TRUE);
1.5       cvs       404:    else
1.8       cvs       405:       return (FALSE);
1.3       cvs       406: }
                    407: 
1.31      cvs       408: 
                    409: /*----------------------------------------------------------------------
                    410:    GetBaseURL
                    411:    normalizes orgName according to a base associated with doc, and
                    412:    following the standard URL format rules.
                    413:    The function returns the base used to solve relative URL and SRC:
                    414:       - the base of the document,
                    415:       - or the document path (without document name).
                    416:   ----------------------------------------------------------------------*/
                    417: #ifdef __STDC__
                    418: char               *GetBaseURL (Document doc)
                    419: #else  /* __STDC__ */
                    420: char               *GetBaseURL (doc)
                    421: Document            doc;
                    422: #endif /* __STDC__ */
                    423: {
                    424:   Element             el;
                    425:   ElementType         elType;
                    426:   AttributeType       attrType;
                    427:   Attribute           attr;
                    428:   char               *ptr, *basename;
                    429:   int                 length;
                    430: 
                    431:   basename = TtaGetMemory (MAX_LENGTH);
1.39      cvs       432:   strncpy (basename, DocumentURLs[doc], MAX_LENGTH-1);
                    433:   basename[MAX_LENGTH-1] = EOS;
1.31      cvs       434:   length = MAX_LENGTH -1;
                    435:   /* get the root element    */
                    436:   el = TtaGetMainRoot (doc);
                    437:   /* search the BASE element */
                    438:   elType.ElSSchema = TtaGetDocumentSSchema (doc);
                    439:   elType.ElTypeNum = HTML_EL_BASE;
                    440:   el = TtaSearchTypedElement (elType, SearchInTree, el);
                    441:   if (el)
                    442:     {
                    443:       /*  The document has a BASE element -> Get the HREF attribute */
                    444:       attrType.AttrSSchema = elType.ElSSchema;
                    445:       attrType.AttrTypeNum = HTML_ATTR_HREF_;
                    446:       attr = TtaGetAttribute (el, attrType);
                    447:       if (attr)
                    448:        {
                    449:          /* Use the base path of the document */
                    450:          TtaGiveTextAttributeValue (attr, basename, &length);
                    451:          /* base and orgName have to be separated by a DIR_SEP */
                    452:          length--;
1.43      cvs       453:          if (basename[0] != EOS && basename[length] != URL_SEP && basename[length] != DIR_SEP) 
1.31      cvs       454:            /* verify if the base has the form "protocol://server:port" */
                    455:            {
1.33      cvs       456:              ptr = AmayaParseUrl (basename, "", AMAYA_PARSE_ACCESS |
                    457:                                                 AMAYA_PARSE_HOST |
                    458:                                                 AMAYA_PARSE_PUNCTUATION);
1.31      cvs       459:              if (ptr && !strcmp (ptr, basename))
                    460:                {
1.43      cvs       461:                  /* it has this form, we complete it by adding a URL_STR  */
                    462:                  if (strchr (basename, DIR_SEP))
                    463:                    strcat (basename, DIR_STR);
                    464:                  else
                    465:                    strcat (basename, URL_STR);
1.31      cvs       466:                  length++;
                    467:                }
                    468:              if (ptr)
                    469:                TtaFreeMemory (ptr);
                    470:            }
                    471:        }
1.33      cvs       472:       }
                    473:   
1.31      cvs       474:   /* Remove anything after the last DIR_SEP char. If no such char is found,
                    475:    * then search for the first ":" char, hoping that what's before that is a
                    476:    * protocol. If found, end the string there. If neither char is found,
                    477:    * then discard the whole base element.
                    478:    */
                    479:   length = strlen (basename) - 1;
                    480:   /* search for the last DIR_SEP char */
1.43      cvs       481:   while (length >= 0  && basename[length] != URL_SEP && basename[length] != DIR_SEP)
1.31      cvs       482:     length--;
                    483:   if (length >= 0)
                    484:     /* found the last DIR_SEP char, end the string there */
                    485:     basename[length + 1] = EOS;                   
                    486:   else
                    487:     /* search for the first PATH_STR char */
                    488:     {
1.32      cvs       489:       for (length = 0; basename[length] != ':' && 
1.31      cvs       490:             basename[length] != EOS; length ++);
1.32      cvs       491:       if (basename[length] == ':')
1.31      cvs       492:        /* found, so end the string there */
                    493:        basename[length + 1] = EOS;
                    494:       else
                    495:        /* not found, discard the base */
                    496:        basename[0] = EOS;
                    497:     }
                    498:   return (basename);
                    499: }
                    500: 
                    501: 
1.4       cvs       502: /*----------------------------------------------------------------------
1.40      cvs       503:    GetLocalPath
                    504:    Allocate and return the local document path associated to the url
                    505:   ----------------------------------------------------------------------*/
                    506: #ifdef __STDC__
                    507: char      *GetLocalPath (Document doc, char *url)
                    508: #else  /* __STDC__ */
                    509: char      *GetLocalPath (doc, url)
                    510: Document   doc;
                    511: char      *url;
                    512: #endif /* __STDC__ */
                    513: {
1.46      cvs       514:   char    *ptr, *n;
1.40      cvs       515:   char    *documentname;
1.41      cvs       516:   char     url_sep;
1.40      cvs       517:   int      len;
                    518:   boolean  noFile;
                    519: 
                    520:   if (url != NULL)
                    521:     {
                    522:       /* check whether the file name exists */
                    523:       len = strlen (url) - 1;
1.41      cvs       524:       if (IsW3Path)
                    525:        url_sep = '/';
                    526:       else 
                    527:        url_sep = DIR_SEP;
                    528:       noFile = (url[len] == url_sep);
1.40      cvs       529:       if (noFile)
                    530:        url[len] = EOS;
1.47      cvs       531:       ptr = TtaGetMemory (MAX_LENGTH);
                    532:       documentname = TtaGetMemory (MAX_LENGTH);
1.40      cvs       533:       TtaExtractName (url, ptr, documentname);
                    534:       sprintf (ptr, "%s%s%d%s", TempFileDirectory, DIR_STR, doc, DIR_STR);
                    535:       if (!TtaCheckDirectory (ptr))
                    536:        /* directory did not exist */
                    537:        mkdir (ptr, S_IRWXU);
1.47      cvs       538: 
                    539:       /* don't include the query string within document name */
                    540:       n = strrchr(documentname, '?');
                    541:       if (n != NULL)
                    542:        *n = EOS;
1.46      cvs       543:       /* don't include ':' within document name */
                    544:       n = strchr (documentname, ':');
                    545:       if (n != NULL)
                    546:        *n = EOS;
1.40      cvs       547:       strcat (ptr, documentname);
                    548:       TtaFreeMemory (documentname);
                    549:       /* restore the url */
                    550:       if (noFile)
1.41      cvs       551:        url[len] = url_sep;
1.40      cvs       552:       return (ptr);
                    553:     }
                    554:   else
                    555:     return (NULL);
                    556: }
                    557: 
                    558: 
                    559: /*----------------------------------------------------------------------
1.9       cvs       560:    NormalizeURL
                    561:    normalizes orgName according to a base associated with doc, and
                    562:    following the standard URL format rules.
                    563:    The function returns the new complete and normalized URL 
1.12      cvs       564:    or file name path (newName) and the name of the document (docName).        
1.9       cvs       565:    N.B. If the function can't find out what's the docName, it assigns
                    566:    the name "noname.html".
1.4       cvs       567:   ----------------------------------------------------------------------*/
1.3       cvs       568: #ifdef __STDC__
                    569: void                NormalizeURL (char *orgName, Document doc, char *newName, char *docName)
                    570: #else  /* __STDC__ */
                    571: void                NormalizeURL (orgName, doc, newName, docName)
                    572: char               *orgName;
                    573: Document            doc;
                    574: char               *newName;
                    575: char               *docName;
                    576: #endif /* __STDC__ */
                    577: {
1.31      cvs       578:    char               *basename;
1.18      cvs       579:    char                tempOrgName[MAX_LENGTH];
1.5       cvs       580:    char               *ptr;
1.49      cvs       581:    char                used_sep;
1.5       cvs       582:    int                 length;
1.49      cvs       583:    boolean             check;
1.5       cvs       584: 
1.44      cvs       585: #  ifdef _WINDOWS
                    586:    int ndx;
                    587: #  endif /* _WINDOWS */
                    588: 
1.5       cvs       589:    if (!newName || !docName)
                    590:       return;
1.18      cvs       591: 
1.32      cvs       592:    if (doc != 0)
1.50      cvs       593:       basename = GetBaseURL (doc);
1.32      cvs       594:    else
1.50      cvs       595:        basename = (char *) NULL;
1.32      cvs       596: 
1.18      cvs       597:    /*
1.31      cvs       598:     * Clean orgName
                    599:     * Make sure we have a complete orgName, without any leading or trailing
                    600:     * white spaces, or trailinbg new lines
                    601:     */
1.5       cvs       602:    ptr = orgName;
1.18      cvs       603:    /* skip leading white space and new line characters */
1.19      cvs       604:    while ((*ptr == ' ' || *ptr == EOL) && *ptr++ != EOS);
1.39      cvs       605:    strncpy (tempOrgName, ptr, MAX_LENGTH -1);
                    606:    tempOrgName[MAX_LENGTH -1] = EOS;
1.18      cvs       607:    /*
1.31      cvs       608:     * Make orgName a complete URL
                    609:     * If the URL does not include a protocol, then try to calculate
                    610:     * one using the doc's base element (if it exists),
                    611:     */
1.50      cvs       612:    if (tempOrgName[0] == EOS) {
                    613:       newName[0] = EOS;
                    614:       TtaFreeMemory (basename);
                    615:       return;
                    616:    }
1.49      cvs       617: 
                    618:    /* clean trailing white space */
                    619:    length = strlen (tempOrgName) - 1;
1.50      cvs       620:    while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL) {
                    621:          tempOrgName[length] = EOS;
                    622:          length--;
                    623:    }
                    624: 
1.49      cvs       625:    /* remove extra dot */
                    626:    if (tempOrgName[length] == '.')
1.50      cvs       627:       tempOrgName[length] = EOS;
                    628: 
                    629:    if (IsW3Path (tempOrgName)) {
                    630:       /* the name is complete, go to the Sixth Step */
                    631:       strcpy (newName, tempOrgName);
                    632:       /* verify if the URL has the form "protocol://server:port" */
                    633:       ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                    634:       if (ptr && !strcmp (ptr, newName)) /* it has this form, we complete it by adding a DIR_STR  */
                    635:          strcat (newName, URL_STR);
1.49      cvs       636: 
1.50      cvs       637:       if (ptr)
                    638:          TtaFreeMemory (ptr);
                    639:    } else if ( doc == 0) /* the name is complete, go to the Sixth Step */
1.18      cvs       640:      strcpy (newName, tempOrgName);
1.50      cvs       641:    else {
1.31      cvs       642:        /* Calculate the absolute URL, using the base or document URL */
1.44      cvs       643: #      ifdef _WINDOWS
1.45      cvs       644:           if (!IsW3Path (basename)) {
                    645:              length = strlen (tempOrgName);
                    646:              for (ndx = 0; ndx < length; ndx++)
                    647:                      if (tempOrgName [ndx] == '/')
                    648:                         tempOrgName [ndx] = '\\';
                    649:           }
1.44      cvs       650: #      endif /* _WINDOWS */
1.25      cvs       651:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.50      cvs       652:        if (ptr) {
                    653:           SimplifyUrl (&ptr);
                    654:           strcpy (newName, ptr);
                    655:           TtaFreeMemory (ptr);
                    656:        } else
                    657:             newName[0] = EOS;
                    658:    }
1.36      cvs       659: 
                    660:    TtaFreeMemory (basename);
1.18      cvs       661:    /*
1.31      cvs       662:     * Prepare the docname that will refer to this ressource in the
                    663:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                    664:     * noname.html as a default ressource name
1.18      cvs       665:    */
1.50      cvs       666:    if (newName[0] != EOS) {
                    667:       length = strlen (newName) - 1;
                    668:       if (newName[length] == URL_SEP || newName[length] == DIR_SEP) {
                    669:          used_sep = newName[length];
                    670:          check = TRUE;
                    671:          while (check) {
                    672:                length--;
                    673:                while (length >= 0 && newName[length] != used_sep)
                    674:                      length--;
                    675:                if (!strncmp (&newName[length+1], "..", 2)) {
                    676:                   newName[length+1] = EOS;
                    677:                   /* remove also previous directory */
                    678:                   length--;
                    679:                   while (length >= 0 && newName[length] != used_sep)
                    680:                         length--;
                    681:                   if (strncmp (&newName[length+1], "//", 2)) /* don't remove server name */
                    682:                      newName[length+1] = EOS;
                    683:                } else if (!strncmp (&newName[length+1], ".", 1))
                    684:                       newName[length+1] = EOS;
                    685:                else
                    686:                   check = FALSE;
                    687:          }
1.49      cvs       688:             strcpy (docName, "noname.html");          
1.50      cvs       689:          /* docname was not comprised inside the URL, so let's */
                    690:          /* assign the default ressource name */
                    691:          strcpy (docName, "noname.html");
                    692:       } else { /* docname is comprised inside the URL */
                    693:            while (length >= 0 && newName[length] != URL_SEP && newName[length] != DIR_SEP)
                    694:                  length--;
                    695:           if (length < 0)
                    696:              strcpy (docName, newName);
                    697:           else
                    698:               strcpy (docName, &newName[length+1]);
                    699:       }
                    700:    } else
                    701:         docName[0] = EOS;
1.18      cvs       702: } 
1.3       cvs       703: 
1.4       cvs       704: /*----------------------------------------------------------------------
1.9       cvs       705:   IsSameHost                                                         
1.4       cvs       706:   ----------------------------------------------------------------------*/
1.3       cvs       707: #ifdef __STDC__
1.34      cvs       708: boolean             IsSameHost (const char *url1, const char *url2)
1.3       cvs       709: #else  /* __STDC__ */
                    710: boolean             IsSameHost (url1, url2)
1.34      cvs       711: const char         *url1;
                    712: const char         *url2;
1.3       cvs       713: #endif /* __STDC__ */
                    714: {
1.34      cvs       715:    char            *basename_ptr1, *basename_ptr2;
                    716:    boolean          result;
1.3       cvs       717: 
1.25      cvs       718:    basename_ptr1 = AmayaParseUrl (url1, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                    719:    basename_ptr2 = AmayaParseUrl (url2, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs       720: 
1.5       cvs       721:    if (strcmp (basename_ptr1, basename_ptr2))
1.8       cvs       722:       result = FALSE;
1.5       cvs       723:    else
1.8       cvs       724:       result = TRUE;
1.3       cvs       725: 
1.25      cvs       726:    TtaFreeMemory (basename_ptr1);
                    727:    TtaFreeMemory (basename_ptr2);
1.5       cvs       728:    return (result);
1.3       cvs       729: }
                    730: 
                    731: 
1.4       cvs       732: /*----------------------------------------------------------------------
1.22      cvs       733:   HasKnownFileSuffix
                    734:   returns TRUE if path points to a file ending with a suffix.
                    735:   ----------------------------------------------------------------------*/
                    736: #ifdef __STDC__
1.34      cvs       737: boolean             HasKnownFileSuffix (const char *path)
1.22      cvs       738: #else  /* __STDC__ */
                    739: boolean             HasKnownFileSuffix (path)
1.34      cvs       740: const char         *path;
1.22      cvs       741: #endif /* __STDC__ */
                    742: {
1.29      cvs       743:    char            *root;
                    744:    char             temppath[MAX_LENGTH];
                    745:    char             suffix[MAX_LENGTH];
1.22      cvs       746: 
1.24      cvs       747:    if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22      cvs       748:      return (FALSE);
                    749: 
1.29      cvs       750:    root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs       751: 
                    752:    if (root) 
                    753:      {
                    754:        strcpy (temppath, root);
1.25      cvs       755:        TtaFreeMemory (root);
1.22      cvs       756:        /* Get the suffix */
                    757:        ExtractSuffix (temppath, suffix); 
                    758: 
                    759:        if( suffix[0] == EOS)
                    760:         /* no suffix */
                    761:         return (FALSE);
                    762: 
                    763:        /* Normalize the suffix */
                    764:        ConvertToLowerCase (suffix);
                    765: 
1.23      cvs       766:        if (!strcmp (suffix, "gz"))
1.22      cvs       767:         /* skip the compressed suffix */
                    768:         {
                    769:         ExtractSuffix (temppath, suffix);
                    770:         if(suffix[0] == EOS)
                    771:           /* no suffix */
                    772:           return (FALSE);
                    773:          /* Normalize the suffix */
                    774:          ConvertToLowerCase (suffix);
                    775:         }
                    776: 
                    777:        if ((strcmp (suffix, "gif")) && (strcmp (suffix, "xbm")) &&
                    778:           (strcmp (suffix, "xpm")) && (strcmp (suffix, "jpg")) &&
                    779:           (strcmp (suffix, "pdf")) && (strcmp (suffix, "png")) &&
                    780:           (strcmp (suffix, "tgz")) && (strcmp (suffix, "xpg")) &&
                    781:           (strcmp (suffix, "xpd")) && (strcmp (suffix, "ps")) &&
                    782:           (strcmp (suffix, "au"))  && (strcmp (suffix, "html")) &&
                    783:           (strcmp (suffix, "htm")) && (strcmp (suffix, "shtml")) &&
                    784:           (strcmp (suffix, "txt")) && (strcmp (suffix, "css")) &&
                    785:           (strcmp (suffix, "eps")))
                    786:         return (FALSE);
                    787:        else
                    788:         return (TRUE);
                    789:      }
                    790:    else
                    791:      return (FALSE);
                    792: }
                    793: 
                    794: 
                    795: /*----------------------------------------------------------------------
1.24      cvs       796:   ChopURL
                    797:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                    798:   If inputURL is  bigger than that size, outputURL receives
                    799:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                    800:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                    801:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                    802:   copied into outputURL. 
                    803:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                    804:   chars.
                    805:   ----------------------------------------------------------------------*/
                    806: #ifdef __STDC__
1.34      cvs       807: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs       808: #else
                    809: void ChopURL (outputURL, inputURL)
1.34      cvs       810: char       *outputURL;
                    811: const char *inputURL;
1.24      cvs       812: #endif
1.22      cvs       813: 
1.24      cvs       814: {
                    815:   int len;
1.9       cvs       816: 
1.24      cvs       817:   len = strlen (inputURL);
                    818:   if (len <= MAX_PRINT_URL_LENGTH) 
1.29      cvs       819:     strcpy (outputURL, inputURL);
1.24      cvs       820:   else
                    821:     /* make a truncated urlName on the status window */
                    822:     {
                    823:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                    824:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                    825:       strcat (outputURL, "...");
                    826:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
                    827:     }
1.25      cvs       828: }
                    829: 
                    830: 
                    831: /*----------------------------------------------------------------------
                    832:    scan
1.47      cvs       833:        Scan a filename for its constituents
1.25      cvs       834:        -----------------------------------
                    835:   
                    836:    On entry,
                    837:        name    points to a document name which may be incomplete.
                    838:    On exit,
                    839:         absolute or relative may be nonzero (but not both).
                    840:        host, fragment and access may be nonzero if they were specified.
                    841:        Any which are nonzero point to zero terminated strings.
                    842:   ----------------------------------------------------------------------*/
                    843: #ifdef __STDC__
                    844: static void scan (char * name, HTURI * parts)
                    845: #else  /* __STDC__ */
                    846: static void scan (name, parts)
                    847: char                *name;
                    848: HTURI               *parts;
                    849: 
                    850: #endif /* __STDC__ */
                    851: {
1.43      cvs       852:   char      *p;
                    853:   char      *after_access = name;
1.32      cvs       854: 
1.43      cvs       855:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs       856:   /* Look for fragment identifier */
                    857:   if ((p = strrchr(name, '#')) != NULL)
                    858:     {
                    859:       *p++ = '\0';
                    860:       parts->fragment = p;
1.25      cvs       861:     }
                    862:     
1.28      cvs       863:   for (p=name; *p; p++)
                    864:     {
1.43      cvs       865:       if (*p == URL_SEP || *p == DIR_SEP || *p=='#' || *p=='?')
1.28      cvs       866:        break;
1.32      cvs       867:       if (*p==':')
1.28      cvs       868:        {
                    869:          *p = 0;
                    870:          parts->access = after_access; /* Scheme has been specified */
                    871: 
                    872:          /* The combination of gcc, the "-O" flag and the HP platform is
                    873:             unhealthy. The following three lines is a quick & dirty fix, but is
                    874:             not recommended. Rather, turn off "-O". */
                    875: 
                    876:          /*            after_access = p;*/
                    877:          /*            while (*after_access == 0)*/
                    878:          /*                after_access++;*/
                    879:          after_access = p+1;
1.43      cvs       880:          if (!strcasecmp("URL", parts->access))
1.28      cvs       881:            /* Ignore IETF's URL: pre-prefix */
                    882:            parts->access = NULL;
                    883:          else
1.25      cvs       884:            break;
                    885:        }
                    886:     }
                    887:     
                    888:     p = after_access;
1.43      cvs       889:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs       890:       {
1.43      cvs       891:        if (p[1] == URL_SEP)
1.28      cvs       892:          {
1.25      cvs       893:            parts->host = p+2;          /* host has been specified      */
1.28      cvs       894:            *p = 0;                     /* Terminate access             */
                    895:            /* look for end of host name if any */
1.43      cvs       896:            p = strchr (parts->host, URL_SEP);
1.28      cvs       897:            if (p)
                    898:              {
1.43      cvs       899:                *p = EOS;                       /* Terminate host */
1.25      cvs       900:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs       901:              }
                    902:          }
                    903:        else
                    904:          /* Root found but no host */
                    905:          parts->absolute = p+1;
                    906:       }
                    907:     else
                    908:       {
1.25      cvs       909:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs       910:       }
1.25      cvs       911: }
                    912: 
                    913: 
                    914: /*----------------------------------------------------------------------
1.28      cvs       915:   AmayaParseUrl: parse a Name relative to another name
                    916: 
                    917:   This returns those parts of a name which are given (and requested)
                    918:   substituting bits from the related name where necessary.
1.25      cvs       919:   
1.28      cvs       920:   On entry,
1.25      cvs       921:        aName           A filename given
                    922:         relatedName     A name relative to which aName is to be parsed. Give
                    923:                         it an empty string if aName is absolute.
                    924:         wanted          A mask for the bits which are wanted.
                    925:   
1.28      cvs       926:   On exit,
1.25      cvs       927:        returns         A pointer to a malloc'd string which MUST BE FREED
                    928:   ----------------------------------------------------------------------*/
                    929: #ifdef __STDC__
1.34      cvs       930: char          *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
1.25      cvs       931: #else  /* __STDC__ */
1.28      cvs       932: char          *AmayaParseUrl (aName, relatedName, wanted)
1.34      cvs       933: const char    *aName;
1.28      cvs       934: char          *relatedName;
                    935: int            wanted;
1.25      cvs       936: 
                    937: #endif /* __STDC__ */
                    938: {
1.29      cvs       939:   char      *return_value;
                    940:   char       result[MAX_LENGTH];
                    941:   char       name[MAX_LENGTH];
                    942:   char       rel[MAX_LENGTH];
                    943:   char      *p, *access;
                    944:   HTURI      given, related;
                    945:   int        len;
1.33      cvs       946:   char       used_sep;
                    947:   char      *used_str;
1.32      cvs       948: 
1.43      cvs       949:   if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33      cvs       950:     {
1.42      cvs       951:       used_str = DIR_STR;
                    952:       used_sep = DIR_SEP;
1.33      cvs       953:     }
1.32      cvs       954:   else
1.33      cvs       955:     {
1.42      cvs       956:       used_str = URL_STR;
                    957:       used_sep = URL_SEP;
1.33      cvs       958:     }
1.32      cvs       959: 
1.29      cvs       960:   /* Make working copies of input strings to cut up: */
                    961:   return_value = NULL;
                    962:   result[0] = 0;               /* Clear string  */
                    963:   strcpy (name, aName);
                    964:   if (relatedName != NULL)  
                    965:     strcpy (rel, relatedName);
                    966:   else
                    967:     relatedName[0] = EOS;
                    968:   
                    969:   scan (name, &given);
                    970:   scan (rel,  &related); 
                    971:   access = given.access ? given.access : related.access;
                    972:   if (wanted & AMAYA_PARSE_ACCESS)
                    973:     if (access)
                    974:       {
                    975:        strcat (result, access);
                    976:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.32      cvs       977:                strcat (result, ":");
1.29      cvs       978:       }
                    979:   
                    980:   if (given.access && related.access)
                    981:     /* If different, inherit nothing. */
                    982:     if (strcmp (given.access, related.access) != 0)
                    983:       {
                    984:        related.host = 0;
                    985:        related.absolute = 0;
                    986:        related.relative = 0;
                    987:        related.fragment = 0;
                    988:       }
                    989:   
                    990:   if (wanted & AMAYA_PARSE_HOST)
                    991:     if(given.host || related.host)
                    992:       {
                    993:        if(wanted & AMAYA_PARSE_PUNCTUATION)
                    994:          strcat (result, "//");
                    995:        strcat (result, given.host ? given.host : related.host);
                    996:       }
                    997:   
                    998:   if (given.host && related.host)
                    999:     /* If different hosts, inherit no path. */
                   1000:     if (strcmp(given.host, related.host) != 0)
                   1001:       {
                   1002:        related.absolute = 0;
                   1003:        related.relative = 0;
                   1004:        related.fragment = 0;
                   1005:       }
                   1006:   
                   1007:   if (wanted & AMAYA_PARSE_PATH)
                   1008:     {
                   1009:       if (given.absolute)
                   1010:        {
                   1011:          /* All is given */
                   1012:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.33      cvs      1013:            strcat (result, used_str);
1.29      cvs      1014:          strcat (result, given.absolute);
1.25      cvs      1015:        }
1.29      cvs      1016:       else if (related.absolute)
                   1017:        {
                   1018:          /* Adopt path not name */
1.33      cvs      1019:          strcat (result, used_str);
1.29      cvs      1020:          strcat (result, related.absolute);
                   1021:          if (given.relative)
                   1022:            {
                   1023:              /* Search part? */
                   1024:              p = strchr (result, '?');
                   1025:              if (!p)
                   1026:                p=result+strlen(result)-1;
1.33      cvs      1027:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1028:              /* Remove filename */
                   1029:              p[1]=0;
                   1030:              /* Add given one */
                   1031:              strcat (result, given.relative);
                   1032:              /*SimplifyUrl (&result);*/
1.25      cvs      1033:            }
                   1034:        }
1.29      cvs      1035:       else if (given.relative)
                   1036:        /* what we've got */
                   1037:        strcat (result, given.relative);
                   1038:       else if (related.relative)
                   1039:        strcat (result, related.relative);
                   1040:       else
                   1041:        /* No inheritance */
1.33      cvs      1042:        strcat (result, used_str);
1.25      cvs      1043:     }
1.29      cvs      1044:   
                   1045:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1046:     if (given.fragment || related.fragment)
                   1047:       {
                   1048:        if (given.absolute && given.fragment)
                   1049:          {
                   1050:            /*Fixes for relURLs...*/
                   1051:            if (wanted & AMAYA_PARSE_PUNCTUATION)
                   1052:              strcat (result, "#");
                   1053:            strcat (result, given.fragment); 
                   1054:          }
                   1055:        else if (!(given.absolute) && !(given.fragment))
                   1056:          strcat (result, "");
                   1057:        else
                   1058:          {
                   1059:            if (wanted & AMAYA_PARSE_PUNCTUATION)
                   1060:              strcat (result, "#");
                   1061:            strcat (result, given.fragment ? given.fragment : related.fragment); 
                   1062:          }
                   1063:       }
                   1064:   len = strlen (result);
                   1065:   if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1066:     strcpy (return_value, result);
                   1067:   return (return_value);               /* exactly the right length */
1.25      cvs      1068: }
                   1069: 
                   1070: /*----------------------------------------------------------------------
                   1071:      HTCanon
                   1072:        Canonicalizes the URL in the following manner starting from the host
                   1073:        pointer:
                   1074:   
                   1075:        1) The host name is converted to lowercase
                   1076:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1077:   
                   1078:        Return: OK      The position of the current path part of the URL
                   1079:                        which might be the old one or a new one.
                   1080:   
                   1081:   ----------------------------------------------------------------------*/
                   1082: #ifdef __STDC__
1.28      cvs      1083: static char *HTCanon (char ** filename, char * host)
1.25      cvs      1084: #else  /* __STDC__ */
1.28      cvs      1085: static char *HTCanon (filename, host)
                   1086: char       **filename;
                   1087: char        *host;
1.25      cvs      1088: #endif /* __STDC__ */
                   1089: {
                   1090:     char *newname = NULL;
                   1091:     char *port;
                   1092:     char *strptr;
                   1093:     char *path;
                   1094:     char *access = host-3;
1.33      cvs      1095:     char       used_sep;
1.32      cvs      1096: 
                   1097:   
1.33      cvs      1098:      if (*filename && strchr (*filename, URL_SEP))
                   1099:        {
                   1100:         used_sep = URL_SEP;
                   1101:        }
                   1102:      else
                   1103:        {
                   1104:         used_sep = DIR_SEP;
                   1105:        }
1.32      cvs      1106:   
1.33      cvs      1107:     while (access>*filename && *(access-1)!= used_sep)       /* Find access method */
1.25      cvs      1108:        access--;
1.33      cvs      1109:     if ((path = strchr(host, used_sep)) == NULL)                       /* Find path */
1.25      cvs      1110:        path = host + strlen(host);
                   1111:     if ((strptr = strchr(host, '@')) != NULL && strptr<path)      /* UserId */
                   1112:        host = strptr;
1.32      cvs      1113:     if ((port = strchr(host, ':')) != NULL && port>path)      /* Port number */
1.25      cvs      1114:        port = NULL;
                   1115: 
                   1116:     strptr = host;                                 /* Convert to lower-case */
1.33      cvs      1117:     while (strptr<path)
                   1118:       {
1.25      cvs      1119:        *strptr = tolower(*strptr);
                   1120:        strptr++;
1.33      cvs      1121:       }
1.25      cvs      1122:     
                   1123:     /* Does the URL contain a full domain name? This also works for a
                   1124:        numerical host name. The domain name is already made lower-case
                   1125:        and without a trailing dot. */
                   1126:     {
1.33      cvs      1127:       char *dot = port ? port : path;
                   1128:       if (dot > *filename && *--dot=='.')
                   1129:        {
                   1130:          char *orig=dot, *dest=dot+1;
                   1131:          while((*orig++ = *dest++));
                   1132:          if (port) port--;
                   1133:          path--;
1.25      cvs      1134:        }
                   1135:     }
                   1136:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1137:     if (port)
                   1138:       {
                   1139:        if (!*(port+1) || *(port+1)==used_sep)
                   1140:          {
                   1141:            if (!newname)
                   1142:              {
1.25      cvs      1143:                char *orig=port, *dest=port+1;
                   1144:                while((*orig++ = *dest++));
1.33      cvs      1145:              }
                   1146:          }
                   1147:        else if ((!strncmp(access, "http", 4) &&
                   1148:                  (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)==used_sep||!*(port+3)))) ||
                   1149:                 (!strncmp(access, "gopher", 6) &&
                   1150:                  (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)==used_sep||!*(port+3)))) ||
                   1151:                 (!strncmp(access, "ftp", 3) &&
                   1152:                  (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)==used_sep||!*(port+3))))) {
                   1153:          if (!newname)
                   1154:            {
                   1155:              char *orig=port, *dest=port+3;
                   1156:              while((*orig++ = *dest++));
                   1157:              /* Update path position, Henry Minsky */
                   1158:              path -= 3;
1.25      cvs      1159:            }
1.33      cvs      1160:        }
                   1161:        else if (newname)
                   1162:          strncat(newname, port, (int) (path-port));
                   1163:       }
1.25      cvs      1164: 
1.33      cvs      1165:     if (newname)
                   1166:       {
1.25      cvs      1167:        char *newpath = newname+strlen(newname);
                   1168:        strcat(newname, path);
                   1169:        path = newpath;
1.28      cvs      1170:        /* Free old copy */
                   1171:        TtaFreeMemory(*filename);
1.25      cvs      1172:        *filename = newname;
1.33      cvs      1173:       }
1.25      cvs      1174:     return path;
                   1175: }
                   1176: 
                   1177: 
                   1178: /*----------------------------------------------------------------------
1.29      cvs      1179:   SimplifyUrl: simplify a URI
1.32      cvs      1180:   A URI is allowed to contain the sequence xxx/../ which may be
                   1181:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1182:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1183:   
1.28      cvs      1184:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1185:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1186:   
1.28      cvs      1187:   but we should NOT change
                   1188:                 http://fred.xxx.edu/../..
1.25      cvs      1189:   
                   1190:        or      ../../albert.html
                   1191:   
1.28      cvs      1192:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1193:   
                   1194:                /fred/..                becomes /fred/..
                   1195:                /fred/././..            becomes /fred/..
                   1196:                /fred/.././junk/.././   becomes /fred/..
                   1197:   
1.28      cvs      1198:   If more than one set of `://' is found (several proxies in cascade) then
                   1199:   only the part after the last `://' is simplified.
1.25      cvs      1200:   
1.28      cvs      1201:   Returns: A string which might be the old one or a new one.
1.25      cvs      1202:   ----------------------------------------------------------------------*/
                   1203: #ifdef __STDC__
1.29      cvs      1204: void         SimplifyUrl (char ** url)
1.25      cvs      1205: #else  /* __STDC__ */
1.29      cvs      1206: void         SimplifyUrl (url)
1.28      cvs      1207: char        **url;
1.25      cvs      1208: #endif /* __STDC__ */
                   1209: {
1.28      cvs      1210:   char *path, *p;
                   1211:   char *newptr, *access;
                   1212:   char *orig, *dest, *end;
                   1213: 
1.33      cvs      1214:   char       used_sep;
1.32      cvs      1215: 
                   1216: 
1.28      cvs      1217:   if (!url || !*url)
                   1218:     return;
                   1219: 
1.33      cvs      1220:   if (strchr (*url, URL_SEP))
                   1221:     {
                   1222:       used_sep = URL_SEP;
                   1223:     }
1.32      cvs      1224:   else
1.33      cvs      1225:     {
                   1226:       used_sep = DIR_SEP;
                   1227:     }
1.32      cvs      1228: 
1.28      cvs      1229:   /* Find any scheme name */
                   1230:   if ((path = strstr(*url, "://")) != NULL)
1.33      cvs      1231:     {
                   1232:       /* Find host name */
1.28      cvs      1233:       access = *url;
                   1234:       while (access<path && (*access=tolower(*access)))
                   1235:        access++;
                   1236:       path += 3;
                   1237:       while ((newptr = strstr(path, "://")) != NULL)
                   1238:         /* For proxies */
                   1239:        path = newptr+3;
                   1240:       /* We have a host name */
                   1241:       path = HTCanon(url, path);
1.25      cvs      1242:     }
1.28      cvs      1243:   else if ((path = strstr(*url, ":/")) != NULL)
                   1244:     path += 2;
                   1245:   else
                   1246:     path = *url;
1.25      cvs      1247: 
1.33      cvs      1248:   if (*path == used_sep && *(path+1)==used_sep)
1.28      cvs      1249:     /* Some URLs start //<foo> */
                   1250:     path += 1;
                   1251:   else if (!strncmp(path, "news:", 5))
                   1252:     {
                   1253:       newptr = strchr(path+5, '@');
                   1254:       if (!newptr)
                   1255:        newptr = path + 5;
                   1256:       while (*newptr)
                   1257:        {
                   1258:          /* Make group or host lower case */
                   1259:          *newptr = tolower (*newptr);
                   1260:          newptr++;
1.25      cvs      1261:        }
1.28      cvs      1262:       /* Doesn't need to do any more */
                   1263:       return;
1.25      cvs      1264:     }
1.28      cvs      1265: 
                   1266:   if ((p = path))
                   1267:     {
                   1268:       if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
                   1269:            (end = strchr (path, '#'))))
                   1270:        end = path + strlen (path);
                   1271:       
                   1272:       /* Parse string second time to simplify */
                   1273:       p = path;
                   1274:       while (p < end)
                   1275:        {
1.33      cvs      1276:          if (*p==used_sep)
1.28      cvs      1277:            {
1.33      cvs      1278:              if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      1279:                {
                   1280:                  orig = p + 1;
1.33      cvs      1281:                  dest = (*(p+2)!=used_sep) ? p+2 : p+3;
1.28      cvs      1282:                  while ((*orig++ = *dest++)); /* Remove a slash and a dot */
                   1283:                  end = orig - 1;
                   1284:                }
1.33      cvs      1285:              else if (*(p+1)=='.' && *(p+2)=='.' && (*(p+3)==used_sep || !*(p+3)))
1.28      cvs      1286:                {
                   1287:                  newptr = p;
1.33      cvs      1288:                  while (newptr>path && *--newptr!=used_sep); /* prev slash */
1.51    ! cvs      1289:                  if (!strncmp(newptr, "/../", 4))
1.28      cvs      1290:                    {
                   1291:                      orig = newptr + 1;
1.33      cvs      1292:                      dest = (*(p+3)!=used_sep) ? p+3 : p+4;
1.28      cvs      1293:                      while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   1294:                      end = orig-1;
                   1295:                      /* Start again with prev slash */
                   1296:                      p = newptr;
1.25      cvs      1297:                    }
1.28      cvs      1298:                  else
1.25      cvs      1299:                    p++;
1.28      cvs      1300:                }
1.33      cvs      1301:              else if (*(p+1) == used_sep)
1.28      cvs      1302:                {
1.33      cvs      1303:                  while (*(p+1) == used_sep)
1.28      cvs      1304:                    {
                   1305:                      orig = p;
                   1306:                      dest = p + 1;
                   1307:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   1308:                      end = orig-1;
                   1309:                    }
                   1310:                }
                   1311:              else
1.25      cvs      1312:                p++;
1.28      cvs      1313:            }
                   1314:          else
                   1315:            p++;
1.25      cvs      1316:        }
                   1317:     }
1.51    ! cvs      1318: 
        !          1319:     /*
        !          1320:     **  Check for host/../.. kind of things
        !          1321:     */
        !          1322:     if (*path=='/' && *(path+1)=='.' && *(path+2)=='.' && (!*(path+3) || *(path+3)=='/'))
        !          1323:        *(path+1) = EOS;
        !          1324: 
1.28      cvs      1325:   return;
                   1326: }
                   1327: 
                   1328: 
                   1329: /*----------------------------------------------------------------------
                   1330:    NormalizeFile normalizes  local names.                             
                   1331:    Return TRUE if target and src differ.                           
                   1332:   ----------------------------------------------------------------------*/
                   1333: #ifdef __STDC__
                   1334: boolean             NormalizeFile (char *src, char *target)
                   1335: #else
                   1336: boolean             NormalizeFile (src, target)
                   1337: char               *src;
                   1338: char               *target;
                   1339: 
                   1340: #endif
                   1341: {
                   1342:    char               *s;
                   1343:    boolean             change;
                   1344: 
                   1345:    change = FALSE;
1.48      cvs      1346: #  ifdef _WINDOWS
                   1347:    if (src[1] == ':' && src[2] == '\\') {
                   1348:       strcpy (target, src);
                   1349:       change = TRUE;
                   1350:    }
                   1351: #  else  /* !_WINDOWS */
1.28      cvs      1352:    if (src[0] == '~')
                   1353:      {
                   1354:        /* replace ~ */
                   1355:        s = (char *) TtaGetEnvString ("HOME");
                   1356:        strcpy (target, s);
                   1357:        strcat (target, &src[1]);
                   1358:        change = TRUE;
                   1359:      }
1.48      cvs      1360: #   endif /* _WINDOWS */
1.28      cvs      1361:    else if (strncmp (src, "file:", 5) == 0)
                   1362:      {
                   1363:        /* remove the prefix file: */
                   1364:        if (src[5] == EOS)
                   1365:           strcpy (target, DIR_STR);
                   1366:        else if (src[0] == '~')
                   1367:          {
                   1368:            /* replace ~ */
                   1369:            s = (char *) TtaGetEnvString ("HOME");
                   1370:            strcpy (target, s);
                   1371:            strcat (target, &src[5]);
                   1372:          }
                   1373:        else
                   1374:           strcpy (target, &src[5]);
                   1375:        change = TRUE;
                   1376:      }
                   1377:    else
                   1378:       strcpy (target, src);
                   1379: 
                   1380:    /* remove /../ and /./ */
1.29      cvs      1381:    SimplifyUrl (&target);
1.28      cvs      1382:    return (change);
1.25      cvs      1383: }
                   1384: 
1.28      cvs      1385: 
1.25      cvs      1386: /*----------------------------------------------------------------------
1.31      cvs      1387:   MakeRelativeURL: make relative name
1.25      cvs      1388:   
1.28      cvs      1389:   This function creates and returns a string which gives an expression of
                   1390:   one address as related to another. Where there is no relation, an absolute
                   1391:   address is retured.
1.25      cvs      1392:   
1.28      cvs      1393:   On entry,
1.25      cvs      1394:        Both names must be absolute, fully qualified names of nodes
                   1395:        (no fragment bits)
                   1396:   
1.28      cvs      1397:   On exit,
1.25      cvs      1398:        The return result points to a newly allocated name which, if
                   1399:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   1400:        The caller is responsible for freeing the resulting name later.
                   1401:   ----------------------------------------------------------------------*/
                   1402: #ifdef __STDC__
1.31      cvs      1403: char            *MakeRelativeURL (char *aName, char *relatedName)
1.25      cvs      1404: #else  /* __STDC__ */
1.31      cvs      1405: char            *MakeRelativeURL (aName, relatedName)
1.28      cvs      1406: char            *aName;
                   1407: char            *relatedName;
1.25      cvs      1408: #endif  /* __STDC__ */
                   1409: {
1.39      cvs      1410:   char          *return_value;
                   1411:   char           result[MAX_LENGTH];
1.29      cvs      1412:   char          *p;
1.34      cvs      1413:   char          *q;
1.29      cvs      1414:   char          *after_access;
                   1415:   char          *last_slash = NULL;
                   1416:   int            slashes, levels, len;
                   1417: 
1.44      cvs      1418: # ifdef _WINDOWS
                   1419:   int ndx;
                   1420: # endif /* _WINDOWS */
                   1421: 
1.29      cvs      1422:   if (aName == NULL || relatedName == NULL)
                   1423:     return (NULL);
                   1424: 
                   1425:   slashes = 0;
                   1426:   after_access = NULL;
                   1427:   p = aName;
                   1428:   q = relatedName;
                   1429:   for (; *p && (*p == *q); p++, q++)
1.27      cvs      1430:     {
                   1431:       /* Find extent of match */
1.32      cvs      1432:       if (*p == ':')
1.29      cvs      1433:        after_access = p + 1;
1.28      cvs      1434:       if (*p == DIR_SEP)
1.27      cvs      1435:        {
1.29      cvs      1436:          /* memorize the last slash position and count them */
1.27      cvs      1437:          last_slash = p;
                   1438:          slashes++;
1.25      cvs      1439:        }
                   1440:     }
                   1441:     
1.31      cvs      1442:   /* q, p point to the first non-matching character or zero */
                   1443:   if (*q == EOS)
                   1444:     {
                   1445:       /* New name is a subset of the related name */
                   1446:       /* exactly the right length */
                   1447:       len = strlen (p);
                   1448:       if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1449:        strcpy (return_value, p);
                   1450:     }
                   1451:   else if ((slashes < 2 && after_access == NULL)
                   1452:       || (slashes < 3 && after_access != NULL))
                   1453:     {
                   1454:       /* Two names whitout common path */
                   1455:       /* exactly the right length */
                   1456:       len = strlen (aName);
                   1457:       if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1458:        strcpy (return_value, aName);
                   1459:     }
                   1460:   else
                   1461:     {
                   1462:       /* Some path in common */
                   1463:       if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
                   1464:        /* just the same server */
                   1465:        strcpy (result, last_slash);
                   1466:       else
                   1467:        {
                   1468:          levels= 0; 
                   1469:          for (; *q && (*q != '#'); q++)
                   1470:            if (*q == DIR_SEP)
                   1471:              levels++;
                   1472:          
                   1473:          result[0] = 0;
                   1474:          for (;levels; levels--)
                   1475:            strcat (result, "../");
                   1476:          strcat (result, last_slash+1);
                   1477:        } 
                   1478:       
                   1479:       /* exactly the right length */
                   1480:       len = strlen (result);
                   1481:       if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1482:        strcpy (return_value, result);
1.25      cvs      1483:     }
1.44      cvs      1484: # ifdef _WINDOWS
                   1485:   len = strlen (return_value);
                   1486:   for (ndx = 0; ndx < len; ndx ++)
                   1487:          if (return_value[ndx] == '\\')
                   1488:             return_value[ndx] = '/' ;
                   1489: # endif /* _WINDOWS */
1.29      cvs      1490:   return (return_value);
1.24      cvs      1491: }
1.35      cvs      1492: 
                   1493: 

Webmaster