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

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

Webmaster