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

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

Webmaster