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

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 */
1.59    ! cvs       609: #   ifdef _WINDOWS
        !           610:        _mkdir (ptr);
        !           611: #   else  /* !_WINDOWS */
1.40      cvs       612:        mkdir (ptr, S_IRWXU);
1.59    ! cvs       613: #   endif /* !_WINDOWS */
1.47      cvs       614: 
                    615:       /* don't include the query string within document name */
                    616:       n = strrchr(documentname, '?');
                    617:       if (n != NULL)
                    618:        *n = EOS;
1.46      cvs       619:       /* don't include ':' within document name */
                    620:       n = strchr (documentname, ':');
                    621:       if (n != NULL)
                    622:        *n = EOS;
1.40      cvs       623:       strcat (ptr, documentname);
                    624:       TtaFreeMemory (documentname);
                    625:       /* restore the url */
                    626:       if (noFile)
1.41      cvs       627:        url[len] = url_sep;
1.40      cvs       628:       return (ptr);
                    629:     }
                    630:   else
                    631:     return (NULL);
                    632: }
                    633: 
                    634: 
                    635: /*----------------------------------------------------------------------
1.9       cvs       636:    NormalizeURL
                    637:    normalizes orgName according to a base associated with doc, and
                    638:    following the standard URL format rules.
1.53      cvs       639:    if doc is 0 and otherPath not NULL, normalizes orgName according to this
                    640:    other path.
1.9       cvs       641:    The function returns the new complete and normalized URL 
1.12      cvs       642:    or file name path (newName) and the name of the document (docName).        
1.9       cvs       643:    N.B. If the function can't find out what's the docName, it assigns
                    644:    the name "noname.html".
1.4       cvs       645:   ----------------------------------------------------------------------*/
1.3       cvs       646: #ifdef __STDC__
1.53      cvs       647: void                NormalizeURL (char *orgName, Document doc, char *newName, char *docName, char *otherPath)
1.3       cvs       648: #else  /* __STDC__ */
1.53      cvs       649: void                NormalizeURL (orgName, doc, newName, docName, otherPath)
1.3       cvs       650: char               *orgName;
                    651: Document            doc;
                    652: char               *newName;
                    653: char               *docName;
1.53      cvs       654: char               *otherPath;
1.3       cvs       655: #endif /* __STDC__ */
                    656: {
1.31      cvs       657:    char               *basename;
1.18      cvs       658:    char                tempOrgName[MAX_LENGTH];
1.5       cvs       659:    char               *ptr;
1.49      cvs       660:    char                used_sep;
1.5       cvs       661:    int                 length;
1.49      cvs       662:    boolean             check;
1.5       cvs       663: 
1.44      cvs       664: #  ifdef _WINDOWS
                    665:    int ndx;
                    666: #  endif /* _WINDOWS */
                    667: 
1.5       cvs       668:    if (!newName || !docName)
                    669:       return;
1.18      cvs       670: 
1.32      cvs       671:    if (doc != 0)
1.53      cvs       672:      basename = GetBaseURL (doc);
                    673:    else if (otherPath != NULL)
                    674:      basename = TtaStrdup (otherPath);
1.32      cvs       675:    else
1.53      cvs       676:      basename = NULL;
1.32      cvs       677: 
1.18      cvs       678:    /*
1.31      cvs       679:     * Clean orgName
                    680:     * Make sure we have a complete orgName, without any leading or trailing
                    681:     * white spaces, or trailinbg new lines
                    682:     */
1.5       cvs       683:    ptr = orgName;
1.18      cvs       684:    /* skip leading white space and new line characters */
1.19      cvs       685:    while ((*ptr == ' ' || *ptr == EOL) && *ptr++ != EOS);
1.39      cvs       686:    strncpy (tempOrgName, ptr, MAX_LENGTH -1);
                    687:    tempOrgName[MAX_LENGTH -1] = EOS;
1.18      cvs       688:    /*
1.31      cvs       689:     * Make orgName a complete URL
                    690:     * If the URL does not include a protocol, then try to calculate
                    691:     * one using the doc's base element (if it exists),
                    692:     */
1.53      cvs       693:    if (tempOrgName[0] == EOS)
                    694:      {
                    695:        newName[0] = EOS;
                    696:        TtaFreeMemory (basename);
                    697:        return;
                    698:      }
1.49      cvs       699: 
                    700:    /* clean trailing white space */
                    701:    length = strlen (tempOrgName) - 1;
1.53      cvs       702:    while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
                    703:      {
                    704:        tempOrgName[length] = EOS;
                    705:        length--;
                    706:      }
1.50      cvs       707: 
1.55      cvs       708:    /* remove extra dot (which dot???) */
                    709:    /* ugly, but faster than a strcmp */
                    710:    if (tempOrgName[length] == '.'
                    711:        && (length == 0 || tempOrgName[length-1] != '.'))
                    712:         tempOrgName[length] = EOS;
1.50      cvs       713: 
1.53      cvs       714:    if (IsW3Path (tempOrgName))
                    715:      {
                    716:        /* the name is complete, go to the Sixth Step */
                    717:        strcpy (newName, tempOrgName);
                    718:        SimplifyUrl (&newName);
                    719:        /* verify if the URL has the form "protocol://server:port" */
                    720:        ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                    721:        if (ptr && !strcmp (ptr, newName)) /* it has this form, we complete it by adding a DIR_STR  */
1.50      cvs       722:          strcat (newName, URL_STR);
1.49      cvs       723: 
1.53      cvs       724:        if (ptr)
1.50      cvs       725:          TtaFreeMemory (ptr);
1.53      cvs       726:      }
                    727:    else if ( basename == NULL)
                    728:      /* the name is complete, go to the Sixth Step */
1.18      cvs       729:      strcpy (newName, tempOrgName);
1.53      cvs       730:    else
                    731:      {
1.31      cvs       732:        /* Calculate the absolute URL, using the base or document URL */
1.44      cvs       733: #      ifdef _WINDOWS
1.53      cvs       734:        if (!IsW3Path (basename))
                    735:         {
                    736:           length = strlen (tempOrgName);
                    737:           for (ndx = 0; ndx < length; ndx++)
                    738:             if (tempOrgName [ndx] == '/')
                    739:               tempOrgName [ndx] = '\\';
                    740:         }
1.44      cvs       741: #      endif /* _WINDOWS */
1.25      cvs       742:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53      cvs       743:        if (ptr)
                    744:         {
                    745:           SimplifyUrl (&ptr);
                    746:           strcpy (newName, ptr);
                    747:           TtaFreeMemory (ptr);
                    748:         }
                    749:        else
                    750:         newName[0] = EOS;
                    751:      }
1.36      cvs       752: 
                    753:    TtaFreeMemory (basename);
1.18      cvs       754:    /*
1.31      cvs       755:     * Prepare the docname that will refer to this ressource in the
                    756:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                    757:     * noname.html as a default ressource name
1.18      cvs       758:    */
1.53      cvs       759:    if (newName[0] != EOS)
                    760:      {
                    761:        length = strlen (newName) - 1;
                    762:        if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
                    763:         {
                    764:           used_sep = newName[length];
                    765:           check = TRUE;
                    766:           while (check)
                    767:             {
1.50      cvs       768:                length--;
                    769:                while (length >= 0 && newName[length] != used_sep)
1.53      cvs       770:                 length--;
                    771:                if (!strncmp (&newName[length+1], "..", 2))
                    772:                 {
                    773:                   newName[length+1] = EOS;
                    774:                   /* remove also previous directory */
                    775:                   length--;
                    776:                   while (length >= 0 && newName[length] != used_sep)
                    777:                     length--;
                    778:                   if (strncmp (&newName[length+1], "//", 2))
                    779:                     /* don't remove server name */
1.50      cvs       780:                      newName[length+1] = EOS;
1.53      cvs       781:                 }
                    782:               else if (!strncmp (&newName[length+1], ".", 1))
                    783:                 newName[length+1] = EOS;
1.50      cvs       784:                else
1.53      cvs       785:                 check = FALSE;
                    786:             }
                    787:           strcpy (docName, "noname.html");            
                    788:           /* docname was not comprised inside the URL, so let's */
                    789:           /* assign the default ressource name */
                    790:           strcpy (docName, "noname.html");
                    791:         }
                    792:        else
                    793:         { /* docname is comprised inside the URL */
1.50      cvs       794:            while (length >= 0 && newName[length] != URL_SEP && newName[length] != DIR_SEP)
1.53      cvs       795:             length--;
                    796:           if (length < 0)
1.50      cvs       797:              strcpy (docName, newName);
1.53      cvs       798:           else
                    799:             strcpy (docName, &newName[length+1]);
                    800:         }
                    801:      }
                    802:    else
                    803:      docName[0] = EOS;
1.18      cvs       804: } 
1.3       cvs       805: 
1.4       cvs       806: /*----------------------------------------------------------------------
1.9       cvs       807:   IsSameHost                                                         
1.4       cvs       808:   ----------------------------------------------------------------------*/
1.3       cvs       809: #ifdef __STDC__
1.34      cvs       810: boolean             IsSameHost (const char *url1, const char *url2)
1.3       cvs       811: #else  /* __STDC__ */
                    812: boolean             IsSameHost (url1, url2)
1.34      cvs       813: const char         *url1;
                    814: const char         *url2;
1.3       cvs       815: #endif /* __STDC__ */
                    816: {
1.34      cvs       817:    char            *basename_ptr1, *basename_ptr2;
                    818:    boolean          result;
1.3       cvs       819: 
1.25      cvs       820:    basename_ptr1 = AmayaParseUrl (url1, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                    821:    basename_ptr2 = AmayaParseUrl (url2, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs       822: 
1.5       cvs       823:    if (strcmp (basename_ptr1, basename_ptr2))
1.8       cvs       824:       result = FALSE;
1.5       cvs       825:    else
1.8       cvs       826:       result = TRUE;
1.3       cvs       827: 
1.25      cvs       828:    TtaFreeMemory (basename_ptr1);
                    829:    TtaFreeMemory (basename_ptr2);
1.5       cvs       830:    return (result);
1.3       cvs       831: }
                    832: 
                    833: 
1.4       cvs       834: /*----------------------------------------------------------------------
1.22      cvs       835:   HasKnownFileSuffix
                    836:   returns TRUE if path points to a file ending with a suffix.
                    837:   ----------------------------------------------------------------------*/
                    838: #ifdef __STDC__
1.34      cvs       839: boolean             HasKnownFileSuffix (const char *path)
1.22      cvs       840: #else  /* __STDC__ */
                    841: boolean             HasKnownFileSuffix (path)
1.34      cvs       842: const char         *path;
1.22      cvs       843: #endif /* __STDC__ */
                    844: {
1.29      cvs       845:    char            *root;
                    846:    char             temppath[MAX_LENGTH];
                    847:    char             suffix[MAX_LENGTH];
1.22      cvs       848: 
1.24      cvs       849:    if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22      cvs       850:      return (FALSE);
                    851: 
1.29      cvs       852:    root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs       853: 
                    854:    if (root) 
                    855:      {
                    856:        strcpy (temppath, root);
1.25      cvs       857:        TtaFreeMemory (root);
1.22      cvs       858:        /* Get the suffix */
                    859:        ExtractSuffix (temppath, suffix); 
                    860: 
                    861:        if( suffix[0] == EOS)
                    862:         /* no suffix */
                    863:         return (FALSE);
                    864: 
                    865:        /* Normalize the suffix */
                    866:        ConvertToLowerCase (suffix);
                    867: 
1.23      cvs       868:        if (!strcmp (suffix, "gz"))
1.22      cvs       869:         /* skip the compressed suffix */
                    870:         {
                    871:         ExtractSuffix (temppath, suffix);
                    872:         if(suffix[0] == EOS)
                    873:           /* no suffix */
                    874:           return (FALSE);
                    875:          /* Normalize the suffix */
                    876:          ConvertToLowerCase (suffix);
                    877:         }
                    878: 
                    879:        if ((strcmp (suffix, "gif")) && (strcmp (suffix, "xbm")) &&
                    880:           (strcmp (suffix, "xpm")) && (strcmp (suffix, "jpg")) &&
                    881:           (strcmp (suffix, "pdf")) && (strcmp (suffix, "png")) &&
                    882:           (strcmp (suffix, "tgz")) && (strcmp (suffix, "xpg")) &&
                    883:           (strcmp (suffix, "xpd")) && (strcmp (suffix, "ps")) &&
                    884:           (strcmp (suffix, "au"))  && (strcmp (suffix, "html")) &&
                    885:           (strcmp (suffix, "htm")) && (strcmp (suffix, "shtml")) &&
                    886:           (strcmp (suffix, "txt")) && (strcmp (suffix, "css")) &&
                    887:           (strcmp (suffix, "eps")))
                    888:         return (FALSE);
                    889:        else
                    890:         return (TRUE);
                    891:      }
                    892:    else
                    893:      return (FALSE);
                    894: }
                    895: 
                    896: 
                    897: /*----------------------------------------------------------------------
1.24      cvs       898:   ChopURL
                    899:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                    900:   If inputURL is  bigger than that size, outputURL receives
                    901:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                    902:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                    903:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                    904:   copied into outputURL. 
                    905:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                    906:   chars.
                    907:   ----------------------------------------------------------------------*/
                    908: #ifdef __STDC__
1.34      cvs       909: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs       910: #else
                    911: void ChopURL (outputURL, inputURL)
1.34      cvs       912: char       *outputURL;
                    913: const char *inputURL;
1.24      cvs       914: #endif
1.22      cvs       915: 
1.24      cvs       916: {
                    917:   int len;
1.9       cvs       918: 
1.24      cvs       919:   len = strlen (inputURL);
                    920:   if (len <= MAX_PRINT_URL_LENGTH) 
1.29      cvs       921:     strcpy (outputURL, inputURL);
1.24      cvs       922:   else
                    923:     /* make a truncated urlName on the status window */
                    924:     {
                    925:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                    926:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                    927:       strcat (outputURL, "...");
                    928:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
                    929:     }
1.25      cvs       930: }
                    931: 
                    932: 
                    933: /*----------------------------------------------------------------------
                    934:    scan
1.47      cvs       935:        Scan a filename for its constituents
1.25      cvs       936:        -----------------------------------
                    937:   
                    938:    On entry,
                    939:        name    points to a document name which may be incomplete.
                    940:    On exit,
                    941:         absolute or relative may be nonzero (but not both).
                    942:        host, fragment and access may be nonzero if they were specified.
                    943:        Any which are nonzero point to zero terminated strings.
                    944:   ----------------------------------------------------------------------*/
                    945: #ifdef __STDC__
                    946: static void scan (char * name, HTURI * parts)
                    947: #else  /* __STDC__ */
                    948: static void scan (name, parts)
                    949: char                *name;
                    950: HTURI               *parts;
                    951: 
                    952: #endif /* __STDC__ */
                    953: {
1.43      cvs       954:   char      *p;
                    955:   char      *after_access = name;
1.32      cvs       956: 
1.43      cvs       957:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs       958:   /* Look for fragment identifier */
1.52      cvs       959:   if ((p = strchr(name, '#')) != NULL)
1.28      cvs       960:     {
                    961:       *p++ = '\0';
                    962:       parts->fragment = p;
1.25      cvs       963:     }
                    964:     
1.28      cvs       965:   for (p=name; *p; p++)
                    966:     {
1.43      cvs       967:       if (*p == URL_SEP || *p == DIR_SEP || *p=='#' || *p=='?')
1.28      cvs       968:        break;
1.32      cvs       969:       if (*p==':')
1.28      cvs       970:        {
                    971:          *p = 0;
                    972:          parts->access = after_access; /* Scheme has been specified */
                    973: 
                    974:          /* The combination of gcc, the "-O" flag and the HP platform is
                    975:             unhealthy. The following three lines is a quick & dirty fix, but is
                    976:             not recommended. Rather, turn off "-O". */
                    977: 
                    978:          /*            after_access = p;*/
                    979:          /*            while (*after_access == 0)*/
                    980:          /*                after_access++;*/
                    981:          after_access = p+1;
1.43      cvs       982:          if (!strcasecmp("URL", parts->access))
1.28      cvs       983:            /* Ignore IETF's URL: pre-prefix */
                    984:            parts->access = NULL;
                    985:          else
1.25      cvs       986:            break;
                    987:        }
                    988:     }
                    989:     
                    990:     p = after_access;
1.43      cvs       991:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs       992:       {
1.43      cvs       993:        if (p[1] == URL_SEP)
1.28      cvs       994:          {
1.25      cvs       995:            parts->host = p+2;          /* host has been specified      */
1.28      cvs       996:            *p = 0;                     /* Terminate access             */
                    997:            /* look for end of host name if any */
1.43      cvs       998:            p = strchr (parts->host, URL_SEP);
1.28      cvs       999:            if (p)
                   1000:              {
1.43      cvs      1001:                *p = EOS;                       /* Terminate host */
1.25      cvs      1002:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs      1003:              }
                   1004:          }
                   1005:        else
                   1006:          /* Root found but no host */
                   1007:          parts->absolute = p+1;
                   1008:       }
                   1009:     else
                   1010:       {
1.25      cvs      1011:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs      1012:       }
1.25      cvs      1013: }
                   1014: 
                   1015: 
                   1016: /*----------------------------------------------------------------------
1.28      cvs      1017:   AmayaParseUrl: parse a Name relative to another name
                   1018: 
                   1019:   This returns those parts of a name which are given (and requested)
                   1020:   substituting bits from the related name where necessary.
1.25      cvs      1021:   
1.28      cvs      1022:   On entry,
1.25      cvs      1023:        aName           A filename given
                   1024:         relatedName     A name relative to which aName is to be parsed. Give
                   1025:                         it an empty string if aName is absolute.
                   1026:         wanted          A mask for the bits which are wanted.
                   1027:   
1.28      cvs      1028:   On exit,
1.25      cvs      1029:        returns         A pointer to a malloc'd string which MUST BE FREED
                   1030:   ----------------------------------------------------------------------*/
                   1031: #ifdef __STDC__
1.34      cvs      1032: char          *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
1.25      cvs      1033: #else  /* __STDC__ */
1.28      cvs      1034: char          *AmayaParseUrl (aName, relatedName, wanted)
1.34      cvs      1035: const char    *aName;
1.28      cvs      1036: char          *relatedName;
                   1037: int            wanted;
1.25      cvs      1038: 
                   1039: #endif /* __STDC__ */
                   1040: {
1.29      cvs      1041:   char      *return_value;
                   1042:   char       result[MAX_LENGTH];
                   1043:   char       name[MAX_LENGTH];
                   1044:   char       rel[MAX_LENGTH];
                   1045:   char      *p, *access;
                   1046:   HTURI      given, related;
                   1047:   int        len;
1.33      cvs      1048:   char       used_sep;
                   1049:   char      *used_str;
1.32      cvs      1050: 
1.43      cvs      1051:   if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33      cvs      1052:     {
1.42      cvs      1053:       used_str = DIR_STR;
                   1054:       used_sep = DIR_SEP;
1.33      cvs      1055:     }
1.32      cvs      1056:   else
1.33      cvs      1057:     {
1.42      cvs      1058:       used_str = URL_STR;
                   1059:       used_sep = URL_SEP;
1.33      cvs      1060:     }
1.32      cvs      1061: 
1.29      cvs      1062:   /* Make working copies of input strings to cut up: */
                   1063:   return_value = NULL;
                   1064:   result[0] = 0;               /* Clear string  */
                   1065:   strcpy (name, aName);
                   1066:   if (relatedName != NULL)  
                   1067:     strcpy (rel, relatedName);
                   1068:   else
                   1069:     relatedName[0] = EOS;
                   1070:   
                   1071:   scan (name, &given);
                   1072:   scan (rel,  &related); 
                   1073:   access = given.access ? given.access : related.access;
                   1074:   if (wanted & AMAYA_PARSE_ACCESS)
                   1075:     if (access)
                   1076:       {
                   1077:        strcat (result, access);
                   1078:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.32      cvs      1079:                strcat (result, ":");
1.29      cvs      1080:       }
                   1081:   
                   1082:   if (given.access && related.access)
                   1083:     /* If different, inherit nothing. */
                   1084:     if (strcmp (given.access, related.access) != 0)
                   1085:       {
                   1086:        related.host = 0;
                   1087:        related.absolute = 0;
                   1088:        related.relative = 0;
                   1089:        related.fragment = 0;
                   1090:       }
                   1091:   
                   1092:   if (wanted & AMAYA_PARSE_HOST)
                   1093:     if(given.host || related.host)
                   1094:       {
                   1095:        if(wanted & AMAYA_PARSE_PUNCTUATION)
                   1096:          strcat (result, "//");
                   1097:        strcat (result, given.host ? given.host : related.host);
                   1098:       }
                   1099:   
                   1100:   if (given.host && related.host)
                   1101:     /* If different hosts, inherit no path. */
                   1102:     if (strcmp(given.host, related.host) != 0)
                   1103:       {
                   1104:        related.absolute = 0;
                   1105:        related.relative = 0;
                   1106:        related.fragment = 0;
                   1107:       }
                   1108:   
                   1109:   if (wanted & AMAYA_PARSE_PATH)
                   1110:     {
                   1111:       if (given.absolute)
                   1112:        {
                   1113:          /* All is given */
                   1114:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.33      cvs      1115:            strcat (result, used_str);
1.29      cvs      1116:          strcat (result, given.absolute);
1.25      cvs      1117:        }
1.29      cvs      1118:       else if (related.absolute)
                   1119:        {
                   1120:          /* Adopt path not name */
1.33      cvs      1121:          strcat (result, used_str);
1.29      cvs      1122:          strcat (result, related.absolute);
                   1123:          if (given.relative)
                   1124:            {
                   1125:              /* Search part? */
                   1126:              p = strchr (result, '?');
                   1127:              if (!p)
                   1128:                p=result+strlen(result)-1;
1.33      cvs      1129:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1130:              /* Remove filename */
                   1131:              p[1]=0;
                   1132:              /* Add given one */
                   1133:              strcat (result, given.relative);
1.25      cvs      1134:            }
                   1135:        }
1.29      cvs      1136:       else if (given.relative)
                   1137:        /* what we've got */
                   1138:        strcat (result, given.relative);
                   1139:       else if (related.relative)
                   1140:        strcat (result, related.relative);
                   1141:       else
                   1142:        /* No inheritance */
1.33      cvs      1143:        strcat (result, used_str);
1.25      cvs      1144:     }
1.29      cvs      1145:   
                   1146:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1147:     if (given.fragment || related.fragment)
                   1148:       {
                   1149:        if (given.absolute && given.fragment)
                   1150:          {
                   1151:            /*Fixes for relURLs...*/
                   1152:            if (wanted & AMAYA_PARSE_PUNCTUATION)
                   1153:              strcat (result, "#");
                   1154:            strcat (result, given.fragment); 
                   1155:          }
                   1156:        else if (!(given.absolute) && !(given.fragment))
                   1157:          strcat (result, "");
                   1158:        else
                   1159:          {
                   1160:            if (wanted & AMAYA_PARSE_PUNCTUATION)
                   1161:              strcat (result, "#");
                   1162:            strcat (result, given.fragment ? given.fragment : related.fragment); 
                   1163:          }
                   1164:       }
                   1165:   len = strlen (result);
                   1166:   if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1167:     strcpy (return_value, result);
                   1168:   return (return_value);               /* exactly the right length */
1.25      cvs      1169: }
                   1170: 
                   1171: /*----------------------------------------------------------------------
                   1172:      HTCanon
                   1173:        Canonicalizes the URL in the following manner starting from the host
                   1174:        pointer:
                   1175:   
                   1176:        1) The host name is converted to lowercase
                   1177:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1178:   
                   1179:        Return: OK      The position of the current path part of the URL
                   1180:                        which might be the old one or a new one.
                   1181:   
                   1182:   ----------------------------------------------------------------------*/
                   1183: #ifdef __STDC__
1.28      cvs      1184: static char *HTCanon (char ** filename, char * host)
1.25      cvs      1185: #else  /* __STDC__ */
1.28      cvs      1186: static char *HTCanon (filename, host)
                   1187: char       **filename;
                   1188: char        *host;
1.25      cvs      1189: #endif /* __STDC__ */
                   1190: {
                   1191:     char *newname = NULL;
                   1192:     char *port;
                   1193:     char *strptr;
                   1194:     char *path;
                   1195:     char *access = host-3;
1.33      cvs      1196:     char       used_sep;
1.32      cvs      1197: 
                   1198:   
1.33      cvs      1199:      if (*filename && strchr (*filename, URL_SEP))
                   1200:        {
                   1201:         used_sep = URL_SEP;
                   1202:        }
                   1203:      else
                   1204:        {
                   1205:         used_sep = DIR_SEP;
                   1206:        }
1.32      cvs      1207:   
1.33      cvs      1208:     while (access>*filename && *(access-1)!= used_sep)       /* Find access method */
1.25      cvs      1209:        access--;
1.33      cvs      1210:     if ((path = strchr(host, used_sep)) == NULL)                       /* Find path */
1.25      cvs      1211:        path = host + strlen(host);
                   1212:     if ((strptr = strchr(host, '@')) != NULL && strptr<path)      /* UserId */
                   1213:        host = strptr;
1.32      cvs      1214:     if ((port = strchr(host, ':')) != NULL && port>path)      /* Port number */
1.25      cvs      1215:        port = NULL;
                   1216: 
                   1217:     strptr = host;                                 /* Convert to lower-case */
1.33      cvs      1218:     while (strptr<path)
                   1219:       {
1.25      cvs      1220:        *strptr = tolower(*strptr);
                   1221:        strptr++;
1.33      cvs      1222:       }
1.25      cvs      1223:     
                   1224:     /* Does the URL contain a full domain name? This also works for a
                   1225:        numerical host name. The domain name is already made lower-case
                   1226:        and without a trailing dot. */
                   1227:     {
1.33      cvs      1228:       char *dot = port ? port : path;
                   1229:       if (dot > *filename && *--dot=='.')
                   1230:        {
                   1231:          char *orig=dot, *dest=dot+1;
                   1232:          while((*orig++ = *dest++));
                   1233:          if (port) port--;
                   1234:          path--;
1.25      cvs      1235:        }
                   1236:     }
                   1237:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1238:     if (port)
                   1239:       {
                   1240:        if (!*(port+1) || *(port+1)==used_sep)
                   1241:          {
                   1242:            if (!newname)
                   1243:              {
1.25      cvs      1244:                char *orig=port, *dest=port+1;
                   1245:                while((*orig++ = *dest++));
1.33      cvs      1246:              }
                   1247:          }
                   1248:        else if ((!strncmp(access, "http", 4) &&
                   1249:                  (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)==used_sep||!*(port+3)))) ||
                   1250:                 (!strncmp(access, "gopher", 6) &&
                   1251:                  (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)==used_sep||!*(port+3)))) ||
                   1252:                 (!strncmp(access, "ftp", 3) &&
                   1253:                  (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)==used_sep||!*(port+3))))) {
                   1254:          if (!newname)
                   1255:            {
                   1256:              char *orig=port, *dest=port+3;
                   1257:              while((*orig++ = *dest++));
                   1258:              /* Update path position, Henry Minsky */
                   1259:              path -= 3;
1.25      cvs      1260:            }
1.33      cvs      1261:        }
                   1262:        else if (newname)
                   1263:          strncat(newname, port, (int) (path-port));
                   1264:       }
1.25      cvs      1265: 
1.33      cvs      1266:     if (newname)
                   1267:       {
1.25      cvs      1268:        char *newpath = newname+strlen(newname);
                   1269:        strcat(newname, path);
                   1270:        path = newpath;
1.28      cvs      1271:        /* Free old copy */
                   1272:        TtaFreeMemory(*filename);
1.25      cvs      1273:        *filename = newname;
1.33      cvs      1274:       }
1.25      cvs      1275:     return path;
                   1276: }
                   1277: 
                   1278: 
                   1279: /*----------------------------------------------------------------------
1.29      cvs      1280:   SimplifyUrl: simplify a URI
1.32      cvs      1281:   A URI is allowed to contain the sequence xxx/../ which may be
                   1282:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1283:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1284:   
1.28      cvs      1285:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1286:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1287:   
1.28      cvs      1288:   but we should NOT change
                   1289:                 http://fred.xxx.edu/../..
1.25      cvs      1290:   
                   1291:        or      ../../albert.html
                   1292:   
1.28      cvs      1293:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1294:   
                   1295:                /fred/..                becomes /fred/..
                   1296:                /fred/././..            becomes /fred/..
                   1297:                /fred/.././junk/.././   becomes /fred/..
                   1298:   
1.28      cvs      1299:   If more than one set of `://' is found (several proxies in cascade) then
                   1300:   only the part after the last `://' is simplified.
1.25      cvs      1301:   
1.28      cvs      1302:   Returns: A string which might be the old one or a new one.
1.25      cvs      1303:   ----------------------------------------------------------------------*/
                   1304: #ifdef __STDC__
1.29      cvs      1305: void         SimplifyUrl (char ** url)
1.25      cvs      1306: #else  /* __STDC__ */
1.29      cvs      1307: void         SimplifyUrl (url)
1.28      cvs      1308: char        **url;
1.25      cvs      1309: #endif /* __STDC__ */
                   1310: {
1.28      cvs      1311:   char *path, *p;
                   1312:   char *newptr, *access;
                   1313:   char *orig, *dest, *end;
                   1314: 
1.33      cvs      1315:   char       used_sep;
1.32      cvs      1316: 
                   1317: 
1.28      cvs      1318:   if (!url || !*url)
                   1319:     return;
                   1320: 
1.33      cvs      1321:   if (strchr (*url, URL_SEP))
                   1322:     {
                   1323:       used_sep = URL_SEP;
                   1324:     }
1.32      cvs      1325:   else
1.33      cvs      1326:     {
                   1327:       used_sep = DIR_SEP;
                   1328:     }
1.32      cvs      1329: 
1.28      cvs      1330:   /* Find any scheme name */
                   1331:   if ((path = strstr(*url, "://")) != NULL)
1.33      cvs      1332:     {
                   1333:       /* Find host name */
1.28      cvs      1334:       access = *url;
                   1335:       while (access<path && (*access=tolower(*access)))
                   1336:        access++;
                   1337:       path += 3;
                   1338:       while ((newptr = strstr(path, "://")) != NULL)
                   1339:         /* For proxies */
                   1340:        path = newptr+3;
                   1341:       /* We have a host name */
                   1342:       path = HTCanon(url, path);
1.25      cvs      1343:     }
1.28      cvs      1344:   else if ((path = strstr(*url, ":/")) != NULL)
                   1345:     path += 2;
                   1346:   else
                   1347:     path = *url;
1.25      cvs      1348: 
1.33      cvs      1349:   if (*path == used_sep && *(path+1)==used_sep)
1.28      cvs      1350:     /* Some URLs start //<foo> */
                   1351:     path += 1;
                   1352:   else if (!strncmp(path, "news:", 5))
                   1353:     {
                   1354:       newptr = strchr(path+5, '@');
                   1355:       if (!newptr)
                   1356:        newptr = path + 5;
                   1357:       while (*newptr)
                   1358:        {
                   1359:          /* Make group or host lower case */
                   1360:          *newptr = tolower (*newptr);
                   1361:          newptr++;
1.25      cvs      1362:        }
1.28      cvs      1363:       /* Doesn't need to do any more */
                   1364:       return;
1.25      cvs      1365:     }
1.28      cvs      1366: 
                   1367:   if ((p = path))
                   1368:     {
                   1369:       if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
                   1370:            (end = strchr (path, '#'))))
                   1371:        end = path + strlen (path);
                   1372:       
                   1373:       /* Parse string second time to simplify */
                   1374:       p = path;
                   1375:       while (p < end)
                   1376:        {
1.33      cvs      1377:          if (*p==used_sep)
1.28      cvs      1378:            {
1.33      cvs      1379:              if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      1380:                {
                   1381:                  orig = p + 1;
1.33      cvs      1382:                  dest = (*(p+2)!=used_sep) ? p+2 : p+3;
1.52      cvs      1383:                  while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28      cvs      1384:                  end = orig - 1;
                   1385:                }
1.33      cvs      1386:              else if (*(p+1)=='.' && *(p+2)=='.' && (*(p+3)==used_sep || !*(p+3)))
1.28      cvs      1387:                {
                   1388:                  newptr = p;
1.52      cvs      1389:                  while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
                   1390:                  if (*newptr == used_sep)
                   1391:                    orig = newptr + 1;
1.28      cvs      1392:                  else
1.52      cvs      1393:                    orig = newptr;
                   1394: 
                   1395:                  dest = (*(p+3) != used_sep) ? p+3 : p+4;
                   1396:                  while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   1397:                  end = orig-1;
                   1398:                  /* Start again with prev slash */
                   1399:                  p = newptr;
1.28      cvs      1400:                }
1.33      cvs      1401:              else if (*(p+1) == used_sep)
1.28      cvs      1402:                {
1.33      cvs      1403:                  while (*(p+1) == used_sep)
1.28      cvs      1404:                    {
                   1405:                      orig = p;
                   1406:                      dest = p + 1;
                   1407:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   1408:                      end = orig-1;
                   1409:                    }
                   1410:                }
                   1411:              else
1.25      cvs      1412:                p++;
1.28      cvs      1413:            }
                   1414:          else
                   1415:            p++;
1.25      cvs      1416:        }
                   1417:     }
1.51      cvs      1418: 
                   1419:     /*
                   1420:     **  Check for host/../.. kind of things
                   1421:     */
1.52      cvs      1422:     if (*path==used_sep && *(path+1)=='.' && *(path+2)=='.' && (!*(path+3) || *(path+3)==used_sep))
1.51      cvs      1423:        *(path+1) = EOS;
                   1424: 
1.28      cvs      1425:   return;
                   1426: }
                   1427: 
                   1428: 
                   1429: /*----------------------------------------------------------------------
                   1430:    NormalizeFile normalizes  local names.                             
                   1431:    Return TRUE if target and src differ.                           
                   1432:   ----------------------------------------------------------------------*/
                   1433: #ifdef __STDC__
                   1434: boolean             NormalizeFile (char *src, char *target)
                   1435: #else
                   1436: boolean             NormalizeFile (src, target)
                   1437: char               *src;
                   1438: char               *target;
                   1439: 
                   1440: #endif
                   1441: {
                   1442:    char               *s;
                   1443:    boolean             change;
                   1444: 
1.54      cvs      1445:    change = FALSE;
1.53      cvs      1446:    if (strncmp (src, "file:", 5) == 0)
1.28      cvs      1447:      {
                   1448:        /* remove the prefix file: */
                   1449:        if (src[5] == EOS)
                   1450:           strcpy (target, DIR_STR);
                   1451:        else if (src[0] == '~')
                   1452:          {
                   1453:            /* replace ~ */
                   1454:            s = (char *) TtaGetEnvString ("HOME");
                   1455:            strcpy (target, s);
                   1456:            strcat (target, &src[5]);
                   1457:          }
                   1458:        else
                   1459:           strcpy (target, &src[5]);
1.54      cvs      1460:        change = TRUE;
1.28      cvs      1461:      }
1.53      cvs      1462: #  ifndef _WINDOWS
                   1463:    else if (src[0] == '~')
                   1464:      {
                   1465:        /* replace ~ */
                   1466:        s = (char *) TtaGetEnvString ("HOME");
                   1467:        strcpy (target, s);
                   1468:        if (src[1] != DIR_SEP)
                   1469:          strcat (target, DIR_STR);
                   1470:        strcat (target, &src[1]);
1.54      cvs      1471:        change = TRUE;
1.53      cvs      1472:      }
                   1473: #   endif /* _WINDOWS */
1.28      cvs      1474:    else
                   1475:       strcpy (target, src);
                   1476: 
                   1477:    /* remove /../ and /./ */
1.29      cvs      1478:    SimplifyUrl (&target);
1.54      cvs      1479:    if (!change)
                   1480:      change = strcmp (src, target);
1.28      cvs      1481:    return (change);
1.25      cvs      1482: }
                   1483: 
1.28      cvs      1484: 
1.25      cvs      1485: /*----------------------------------------------------------------------
1.31      cvs      1486:   MakeRelativeURL: make relative name
1.25      cvs      1487:   
1.28      cvs      1488:   This function creates and returns a string which gives an expression of
                   1489:   one address as related to another. Where there is no relation, an absolute
                   1490:   address is retured.
1.25      cvs      1491:   
1.28      cvs      1492:   On entry,
1.25      cvs      1493:        Both names must be absolute, fully qualified names of nodes
                   1494:        (no fragment bits)
                   1495:   
1.28      cvs      1496:   On exit,
1.25      cvs      1497:        The return result points to a newly allocated name which, if
                   1498:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   1499:        The caller is responsible for freeing the resulting name later.
                   1500:   ----------------------------------------------------------------------*/
                   1501: #ifdef __STDC__
1.31      cvs      1502: char            *MakeRelativeURL (char *aName, char *relatedName)
1.25      cvs      1503: #else  /* __STDC__ */
1.31      cvs      1504: char            *MakeRelativeURL (aName, relatedName)
1.28      cvs      1505: char            *aName;
                   1506: char            *relatedName;
1.25      cvs      1507: #endif  /* __STDC__ */
                   1508: {
1.39      cvs      1509:   char          *return_value;
                   1510:   char           result[MAX_LENGTH];
1.29      cvs      1511:   char          *p;
1.34      cvs      1512:   char          *q;
1.29      cvs      1513:   char          *after_access;
                   1514:   char          *last_slash = NULL;
                   1515:   int            slashes, levels, len;
                   1516: 
1.44      cvs      1517: # ifdef _WINDOWS
                   1518:   int ndx;
                   1519: # endif /* _WINDOWS */
                   1520: 
1.29      cvs      1521:   if (aName == NULL || relatedName == NULL)
                   1522:     return (NULL);
                   1523: 
                   1524:   slashes = 0;
                   1525:   after_access = NULL;
                   1526:   p = aName;
                   1527:   q = relatedName;
                   1528:   for (; *p && (*p == *q); p++, q++)
1.27      cvs      1529:     {
                   1530:       /* Find extent of match */
1.32      cvs      1531:       if (*p == ':')
1.29      cvs      1532:        after_access = p + 1;
1.28      cvs      1533:       if (*p == DIR_SEP)
1.27      cvs      1534:        {
1.29      cvs      1535:          /* memorize the last slash position and count them */
1.27      cvs      1536:          last_slash = p;
                   1537:          slashes++;
1.25      cvs      1538:        }
                   1539:     }
                   1540:     
1.31      cvs      1541:   /* q, p point to the first non-matching character or zero */
                   1542:   if (*q == EOS)
                   1543:     {
                   1544:       /* New name is a subset of the related name */
                   1545:       /* exactly the right length */
                   1546:       len = strlen (p);
                   1547:       if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1548:        strcpy (return_value, p);
                   1549:     }
                   1550:   else if ((slashes < 2 && after_access == NULL)
                   1551:       || (slashes < 3 && after_access != NULL))
                   1552:     {
                   1553:       /* Two names whitout common path */
                   1554:       /* exactly the right length */
                   1555:       len = strlen (aName);
                   1556:       if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1557:        strcpy (return_value, aName);
                   1558:     }
                   1559:   else
                   1560:     {
                   1561:       /* Some path in common */
                   1562:       if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
                   1563:        /* just the same server */
                   1564:        strcpy (result, last_slash);
                   1565:       else
                   1566:        {
                   1567:          levels= 0; 
1.52      cvs      1568:          for (; *q && *q != '#' && *q!=';' && *q!='?'; q++)
1.31      cvs      1569:            if (*q == DIR_SEP)
                   1570:              levels++;
                   1571:          
1.52      cvs      1572:          result[0] = EOS;
1.31      cvs      1573:          for (;levels; levels--)
                   1574:            strcat (result, "../");
                   1575:          strcat (result, last_slash+1);
                   1576:        } 
1.52      cvs      1577: 
                   1578:       if (!*result)
                   1579:        strcat (result, "./");
                   1580: 
1.31      cvs      1581:       /* exactly the right length */
                   1582:       len = strlen (result);
                   1583:       if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1584:        strcpy (return_value, result);
1.52      cvs      1585: 
1.25      cvs      1586:     }
1.44      cvs      1587: # ifdef _WINDOWS
                   1588:   len = strlen (return_value);
                   1589:   for (ndx = 0; ndx < len; ndx ++)
                   1590:          if (return_value[ndx] == '\\')
                   1591:             return_value[ndx] = '/' ;
                   1592: # endif /* _WINDOWS */
1.29      cvs      1593:   return (return_value);
1.24      cvs      1594: }
1.35      cvs      1595: 
                   1596: 

Webmaster