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

1.7       cvs         1: /*
                      2:  *
1.110   ! cvs         3:  *  (c) COPYRIGHT MIT and INRIA, 1996-2001
1.7       cvs         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.106     cvs        14:  *          R. Guetari: 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"
1.100     kahan      23: #include "query_f.h"
1.8       cvs        24: 
1.24      cvs        25: #define MAX_PRINT_URL_LENGTH 50
1.106     cvs        26: typedef struct _HTURI
                     27: {
                     28:     char *access;              /* Now known as "scheme" */
                     29:     char *host;
                     30:     char *absolute;
                     31:     char *relative;
                     32:     char *fragment;
1.29      cvs        33: } HTURI;
1.24      cvs        34: 
1.28      cvs        35: 
                     36: /*----------------------------------------------------------------------
                     37:   ConvertToLowerCase
                     38:   Converts a string to lowercase.
                     39:   ----------------------------------------------------------------------*/
1.106     cvs        40: void         ConvertToLowerCase (char *string)
1.28      cvs        41: {
                     42:  int i;
1.93      cvs        43:  
1.28      cvs        44:  if (!string)
                     45:    return;
                     46: 
1.106     cvs        47:  for (i = 0; string[i] != EOS; i++)
1.67      cvs        48:    string[i] = utolower (string[i]);
1.28      cvs        49: }
1.22      cvs        50: 
1.8       cvs        51: /*----------------------------------------------------------------------
1.75      cvs        52:   EscapeChar
                     53:   writes the equivalent escape code of a char in a string
                     54:   ----------------------------------------------------------------------*/
1.109     cvs        55: void EscapeChar (char *string, char c)
1.75      cvs        56: {
1.109     cvs        57:   unsigned int i;
                     58: 
                     59:    i = (unsigned char) c & 0xFF;
                     60:    sprintf (string, "%02x", i);
1.75      cvs        61: }
                     62: 
                     63: /*----------------------------------------------------------------------
1.96      cvs        64:   UnEscapeChar
                     65:   writes the equivalent hex code to a %xx coded char
                     66:   ----------------------------------------------------------------------*/
1.109     cvs        67: static char UnEscapeChar (char c)
1.96      cvs        68: {
1.106     cvs        69:     return  c >= '0' && c <= '9' ?  c - '0'
                     70:             : c >= 'A' && c <= 'F' ? c - 'A' + 10
                     71:             : c - 'a' + 10;   /* accept small letters just in case */
1.96      cvs        72: }
                     73: 
                     74: /*----------------------------------------------------------------------
1.75      cvs        75:   EscapeURL
                     76:   Takes a URL and escapes all protected chars into
                     77:   %xx sequences. Also, removes any leading white spaces
                     78:   Returns either NULL or a new buffer, which must be freed by the caller
                     79:   ----------------------------------------------------------------------*/
1.106     cvs        80: char *EscapeURL (const char *url)
                     81: {
                     82:   char *buffer;
                     83:   int   buffer_len;
                     84:   int   buffer_free_mem;
                     85:   char *ptr;
                     86:   int   new_chars;
1.75      cvs        87:   void *status;
                     88: 
                     89:   if (url && *url)
                     90:     {
1.106     cvs        91:       buffer_free_mem = strlen (url) + 20;
                     92:       buffer = TtaGetMemory (buffer_free_mem + 1);
1.107     kahan      93:       ptr = (char *) url;
1.75      cvs        94:       buffer_len = 0;
                     95: 
                     96:       while (*ptr)
                     97:         {
                     98:           switch (*ptr)
                     99:             {
                    100:               /* put here below all the chars that need to
                    101:                  be escaped into %xx */
1.81      cvs       102:             case 0x27: /* &amp */
                    103:             case 0x20: /* space */
1.75      cvs       104:               new_chars = 3; 
                    105:               break;
                    106: 
                    107:             default:
                    108:               new_chars = 1; 
                    109:               break;
                    110:             }
                    111: 
                    112:           /* see if we need extra room in the buffer */
                    113:           if (new_chars > buffer_free_mem)
                    114:             {
1.76      cvs       115:               buffer_free_mem = 20;
1.106     cvs       116:               status = TtaRealloc (buffer, sizeof (char) 
1.75      cvs       117:                                   * (buffer_len + buffer_free_mem + 1));
                    118:               if (status)
                    119:                 buffer = (STRING) status;
1.106     cvs       120:               else
                    121:                {
                    122:                  /* @@ maybe we should do some other behavior here, like
                    123:                     freeing the buffer and return a void thing */
                    124:                  buffer[buffer_len] = EOS;
                    125:                  break;
                    126:                }
1.75      cvs       127:             }
                    128:          /* escape the char */
                    129:           if (new_chars == 3)
                    130:             {
1.106     cvs       131:               buffer[buffer_len] = '%';
1.75      cvs       132:               EscapeChar (&buffer[buffer_len+1], *ptr);
                    133:             }
                    134:           else
                    135:             buffer[buffer_len] = *ptr;
                    136: 
                    137:           /* update the status */
                    138:           buffer_len += new_chars;
                    139:           buffer_free_mem -= new_chars;
                    140:           /* examine the next char */
                    141:           ptr++;
                    142:         }
1.106     cvs       143:       buffer[buffer_len] = EOS;
1.75      cvs       144:     }
1.76      cvs       145:   else
                    146:     buffer = NULL;
                    147: 
1.75      cvs       148:   return (buffer);
                    149: }
                    150: 
                    151: 
                    152: /*----------------------------------------------------------------------
1.11      cvs       153:   ExplodeURL 
1.8       cvs       154:   ----------------------------------------------------------------------*/
1.106     cvs       155: void ExplodeURL (char *url, char **proto, char **host, char **dir,
                    156:                 char **file)
1.8       cvs       157: {
1.33      cvs       158:    char            *curr, *temp;
                    159:    char             used_sep;
1.32      cvs       160: 
1.33      cvs       161:    if (url && strchr (url, URL_SEP))
                    162:      used_sep = URL_SEP;
                    163:    else
                    164:      used_sep = DIR_SEP;
1.8       cvs       165: 
                    166:    if ((url == NULL) || (proto == NULL) || (host == NULL) ||
                    167:        (dir == NULL) || (file == NULL))
                    168:       return;
                    169: 
                    170:    /* initialize every pointer */
                    171:    *proto = *host = *dir = *file = NULL;
                    172: 
                    173:    /* skip any leading space */
                    174:    while ((*url == SPACE) || (*url == TAB))
                    175:       url++;
1.9       cvs       176:    curr = url;
                    177:    if (*curr == 0)
1.8       cvs       178:       goto finished;
                    179: 
                    180:    /* go to the end of the URL */
1.68      cvs       181:    while ((*curr != EOS) && (*curr != SPACE) && (*curr != BSPACE) &&
                    182:          (*curr != __CR__) && (*curr != EOL))
1.9       cvs       183:       curr++;
1.8       cvs       184: 
                    185:    /* mark the end of the chain */
1.9       cvs       186:    *curr = EOS;
                    187:    curr--;
                    188:    if (curr <= url)
1.8       cvs       189:       goto finished;
                    190: 
                    191:    /* search the next DIR_SEP indicating the beginning of the file name */
                    192:    do
1.11      cvs       193:      curr--;
1.33      cvs       194:    while ((curr >= url) && (*curr != used_sep));
1.11      cvs       195: 
1.9       cvs       196:    if (curr < url)
1.8       cvs       197:       goto finished;
1.9       cvs       198:    *file = curr + 1;
1.8       cvs       199: 
                    200:    /* mark the end of the dir */
1.9       cvs       201:    *curr = EOS;
                    202:    curr--;
                    203:    if (curr < url)
1.8       cvs       204:       goto finished;
                    205: 
1.29      cvs       206:    /* search for the DIR_STR indicating the host name start */
1.33      cvs       207:    while ((curr > url) && ((*curr != used_sep) || (*(curr + 1) != used_sep)))
1.9       cvs       208:       curr--;
1.8       cvs       209: 
                    210:    /* if we found it, separate the host name from the directory */
1.102     kahan     211:    if ((*curr == used_sep) && (*(curr + 1) == used_sep))
1.8       cvs       212:      {
1.9       cvs       213:        *host = temp = curr + 2;
1.33      cvs       214:        while ((*temp != 0) && (*temp != used_sep))
1.8       cvs       215:           temp++;
1.33      cvs       216:        if (*temp == used_sep)
1.8       cvs       217:          {
                    218:             *temp = EOS;
                    219:             *dir = temp + 1;
                    220:          }
                    221:      }
                    222:    else
1.11      cvs       223:      *dir = curr;
                    224: 
1.9       cvs       225:    if (curr <= url)
1.8       cvs       226:       goto finished;
                    227: 
                    228:    /* mark the end of the proto */
1.9       cvs       229:    *curr = EOS;
                    230:    curr--;
                    231:    if (curr < url)
1.8       cvs       232:       goto finished;
                    233: 
1.106     cvs       234:    if (*curr == ':')
1.8       cvs       235:      {
1.9       cvs       236:        *curr = EOS;
                    237:        curr--;
1.8       cvs       238:      }
                    239:    else
                    240:       goto finished;
1.11      cvs       241: 
1.9       cvs       242:    if (curr < url)
1.8       cvs       243:       goto finished;
1.9       cvs       244:    while ((curr > url) && (isalpha (*curr)))
                    245:       curr--;
                    246:    *proto = curr;
1.8       cvs       247: 
                    248:  finished:;
                    249: 
                    250: #ifdef AMAYA_DEBUG
                    251:    fprintf (stderr, "ExplodeURL(%s)\n\t", url);
                    252:    if (*proto)
                    253:       fprintf (stderr, "proto : %s, ", *proto);
                    254:    if (*host)
                    255:       fprintf (stderr, "host : %s, ", *host);
                    256:    if (*dir)
                    257:       fprintf (stderr, "dir : %s, ", *dir);
                    258:    if (*file)
                    259:       fprintf (stderr, "file : %s ", *file);
                    260:    fprintf (stderr, "\n");
                    261: #endif
                    262: 
                    263: }
1.3       cvs       264: 
1.61      cvs       265: 
                    266: /*----------------------------------------------------------------------
                    267:    ExtractSuffix extract suffix from document nane.                
                    268:   ----------------------------------------------------------------------*/
1.106     cvs       269: void ExtractSuffix (char *aName, char *aSuffix)
1.61      cvs       270: {
1.106     cvs       271:    int               lg, i;
                    272:    char             *ptr, *oldptr;
1.61      cvs       273: 
                    274:    if (!aSuffix || !aName)
                    275:      /* bad suffix */
                    276:      return;
                    277: 
1.106     cvs       278:    aSuffix[0] = EOS;
                    279:    lg = strlen (aName);
1.61      cvs       280:    if (lg)
                    281:      {
                    282:        /* the name is not empty */
                    283:        oldptr = ptr = &aName[0];
                    284:        do
                    285:          {
1.106     cvs       286:             ptr = strrchr (oldptr, '.');
1.61      cvs       287:             if (ptr)
                    288:                oldptr = &ptr[1];
                    289:          }
                    290:        while (ptr);
                    291: 
                    292:        i = (int) (oldptr) - (int) (aName);     /* name length */
                    293:        if (i > 1)
                    294:          {
1.106     cvs       295:             aName[i - 1] = EOS;
1.61      cvs       296:             if (i != lg)
1.106     cvs       297:                strcpy (aSuffix, oldptr);
1.61      cvs       298:          }
                    299:      }
                    300: }
                    301: 
1.4       cvs       302: /*----------------------------------------------------------------------
1.9       cvs       303:   IsHTMLName                                                         
                    304:   returns TRUE if path points to an HTML resource.
1.4       cvs       305:   ----------------------------------------------------------------------*/
1.109     cvs       306: ThotBool IsHTMLName (const char *path)
1.106     cvs       307: {
                    308:   char              temppath[MAX_LENGTH];
                    309:   char              suffix[MAX_LENGTH];
                    310:   char              nsuffix[MAX_LENGTH];
1.101     cvs       311:   int               i; 
1.5       cvs       312: 
1.101     cvs       313:   if (!path)
                    314:     return (FALSE);
1.5       cvs       315: 
1.106     cvs       316:   strcpy (temppath, path);
1.101     cvs       317:   ExtractSuffix (temppath, suffix);
                    318:   i = 0;
1.106     cvs       319:   while (suffix[i] != EOS)
1.101     cvs       320:     {
                    321:       /* Normalize the suffix */
                    322:       i = 0;
1.106     cvs       323:       while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101     cvs       324:        {
                    325:          nsuffix[i] = utolower (suffix[i]);
                    326:          i++;
                    327:        }
1.106     cvs       328:       nsuffix[i] = EOS;
                    329:       if (!strcmp (nsuffix, "html") ||
                    330:          !strcmp (nsuffix, "htm") ||
                    331:          !strcmp (nsuffix, "shtml") ||
                    332:          !strcmp (nsuffix, "jsp") ||
                    333:          !strcmp (nsuffix, "xht") ||
                    334:          !strcmp (nsuffix, "xhtm") ||
                    335:          !strcmp (nsuffix, "xhtml"))
1.101     cvs       336:        return (TRUE);
1.106     cvs       337:       else if (!strcmp (nsuffix, "gz"))
1.101     cvs       338:        {
                    339:          /* take into account compressed files */
                    340:          ExtractSuffix (temppath, suffix);       
                    341:          /* Normalize the suffix */
                    342:          i = 0;
1.106     cvs       343:          while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101     cvs       344:            {
                    345:              nsuffix[i] = utolower (suffix[i]);
                    346:              i++;
                    347:            }
1.106     cvs       348:          nsuffix[i] = EOS;
                    349:          if (!strcmp (nsuffix, "html") ||
                    350:              !strcmp (nsuffix, "htm") ||
                    351:              !strcmp (nsuffix, "shtml") ||
                    352:              !strcmp (nsuffix, "jsp") ||
                    353:              !strcmp (nsuffix, "xht") ||
                    354:              !strcmp (nsuffix, "xhtm") ||
                    355:              !strcmp (nsuffix, "xhtml"))
1.101     cvs       356:            return (TRUE);
                    357:          else
                    358:            return (FALSE);
                    359:        }
                    360:       else
                    361:        /* check if there is another suffix */
                    362:        ExtractSuffix (temppath, suffix);
                    363:     }
1.88      cvs       364:    return (FALSE);
1.3       cvs       365: }
                    366: 
1.4       cvs       367: /*----------------------------------------------------------------------
1.56      cvs       368:   IsXMLName                                                         
                    369:   returns TRUE if path points to an XML resource.
                    370:   ----------------------------------------------------------------------*/
1.106     cvs       371: ThotBool             IsXMLName (const char *path)
1.56      cvs       372: {
1.106     cvs       373:    char                temppath[MAX_LENGTH];
                    374:    char                suffix[MAX_LENGTH];
1.56      cvs       375: 
                    376:    if (!path)
                    377:       return (FALSE);
                    378: 
1.106     cvs       379:    strcpy (temppath, path);
1.56      cvs       380:    ExtractSuffix (temppath, suffix);
                    381: 
1.106     cvs       382:    if (!strcasecmp (suffix, "xml") ||
                    383:        !strcasecmp (suffix, "xht") ||
                    384:        !strcmp (suffix, "xhtm") ||
                    385:        !strcmp (suffix, "xhtml"))
1.56      cvs       386:      return (TRUE);
1.106     cvs       387:    else if (!strcmp (suffix, "gz"))
1.56      cvs       388:      {
                    389:        /* take into account compressed files */
                    390:        ExtractSuffix (temppath, suffix);       
1.106     cvs       391:        if (!strcasecmp (suffix, "xml") ||
                    392:           !strcasecmp (suffix, "xht") ||
                    393:           !strcmp (suffix, "xhtm") ||
                    394:           !strcmp (suffix, "xhtml"))
1.60      cvs       395:         return (TRUE);
                    396:        else
                    397:         return (FALSE);
                    398:      }
                    399:    else
                    400:      return (FALSE);
                    401: }
                    402: 
                    403: /*----------------------------------------------------------------------
1.103     cvs       404:   IsMathMLName                                                         
                    405:   returns TRUE if path points to an MathML resource.
                    406:   ----------------------------------------------------------------------*/
1.106     cvs       407: ThotBool             IsMathMLName (const char *path)
1.103     cvs       408: {
1.106     cvs       409:    char                temppath[MAX_LENGTH];
                    410:    char                suffix[MAX_LENGTH];
1.103     cvs       411: 
                    412:    if (!path)
                    413:       return (FALSE);
                    414: 
1.106     cvs       415:    strcpy (temppath, path);
1.103     cvs       416:    ExtractSuffix (temppath, suffix);
                    417: 
1.106     cvs       418:    if (!strcasecmp (suffix, "mml"))
1.103     cvs       419:      return (TRUE);
1.106     cvs       420:    else if (!strcmp (suffix, "gz"))
1.103     cvs       421:      {
                    422:        /* take into account compressed files */
                    423:        ExtractSuffix (temppath, suffix);       
1.106     cvs       424:        if (!strcasecmp (suffix, "mml"))
1.103     cvs       425:         return (TRUE);
                    426:        else
                    427:         return (FALSE);
                    428:      }
                    429:    else
                    430:      return (FALSE);
                    431: }
                    432: 
                    433: /*----------------------------------------------------------------------
                    434:   IsSVGName                                                         
                    435:   returns TRUE if path points to an MathML resource.
                    436:   ----------------------------------------------------------------------*/
1.106     cvs       437: ThotBool             IsSVGName (const char *path)
1.103     cvs       438: {
1.106     cvs       439:    char                temppath[MAX_LENGTH];
                    440:    char                suffix[MAX_LENGTH];
1.103     cvs       441: 
                    442:    if (!path)
                    443:       return (FALSE);
                    444: 
1.106     cvs       445:    strcpy (temppath, path);
1.103     cvs       446:    ExtractSuffix (temppath, suffix);
                    447: 
1.106     cvs       448:    if (!strcasecmp (suffix, "svg"))
1.103     cvs       449:      return (TRUE);
1.106     cvs       450:    else if (!strcmp (suffix, "gz"))
1.103     cvs       451:      {
                    452:        /* take into account compressed files */
                    453:        ExtractSuffix (temppath, suffix);       
1.106     cvs       454:        if (!strcasecmp (suffix, "svg"))
1.103     cvs       455:         return (TRUE);
                    456:        else
                    457:         return (FALSE);
                    458:      }
                    459:    else
                    460:      return (FALSE);
                    461: }
                    462: 
                    463: /*----------------------------------------------------------------------
1.60      cvs       464:   IsCSSName                                                         
                    465:   returns TRUE if path points to an XML resource.
                    466:   ----------------------------------------------------------------------*/
1.106     cvs       467: ThotBool             IsCSSName (const char *path)
1.60      cvs       468: {
1.106     cvs       469:    char                temppath[MAX_LENGTH];
                    470:    char                suffix[MAX_LENGTH];
1.60      cvs       471: 
                    472:    if (!path)
                    473:       return (FALSE);
                    474: 
1.106     cvs       475:    strcpy (temppath, path);
1.60      cvs       476:    ExtractSuffix (temppath, suffix);
                    477: 
1.106     cvs       478:    if (!strcasecmp (suffix, "css"))
1.60      cvs       479:      return (TRUE);
1.106     cvs       480:    else if (!strcmp (suffix, "gz"))
1.60      cvs       481:      {
                    482:        /* take into account compressed files */
                    483:        ExtractSuffix (temppath, suffix);       
1.106     cvs       484:        if (!strcasecmp (suffix, "css"))
1.56      cvs       485:         return (TRUE);
                    486:        else
                    487:         return (FALSE);
                    488:      }
                    489:    else
                    490:      return (FALSE);
                    491: }
                    492: 
                    493: /*----------------------------------------------------------------------
1.9       cvs       494:   IsImageName                                
                    495:   returns TRUE if path points to an image resource.
1.4       cvs       496:   ----------------------------------------------------------------------*/
1.106     cvs       497: ThotBool             IsImageName (const char *path)
                    498: {
                    499:    char                temppath[MAX_LENGTH];
                    500:    char                suffix[MAX_LENGTH];
                    501:    char                nsuffix[MAX_LENGTH];
1.5       cvs       502:    int                 i;
                    503: 
                    504:    if (!path)
1.13      cvs       505:       return (FALSE);
1.5       cvs       506: 
1.106     cvs       507:    strcpy (temppath, path);
1.5       cvs       508:    ExtractSuffix (temppath, suffix);
                    509: 
                    510:    /* Normalize the suffix */
                    511:    i = 0;
1.106     cvs       512:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       513:      {
1.67      cvs       514:        nsuffix[i] = utolower (suffix[i]);
1.13      cvs       515:        i++;
                    516:      }
1.106     cvs       517:    nsuffix[i] = EOS;
                    518:    if ((!strcmp (nsuffix, "gif")) || (!strcmp (nsuffix, "xbm")) ||
                    519:        (!strcmp (nsuffix, "xpm")) || (!strcmp (nsuffix, "jpg")) ||
                    520:        (!strcmp (nsuffix, "png")) || (!strcmp (nsuffix, "au")))
1.39      cvs       521:       return (TRUE);
                    522:    return (FALSE);
1.3       cvs       523: }
                    524: 
1.4       cvs       525: /*----------------------------------------------------------------------
1.58      cvs       526:   IsImageType                                
                    527:   returns TRUE if type points to an image resource.
                    528:   ----------------------------------------------------------------------*/
1.106     cvs       529: ThotBool             IsImageType (const char *type)
1.58      cvs       530: {
1.106     cvs       531:    char                temptype[MAX_LENGTH];
1.58      cvs       532:    int                 i;
                    533: 
                    534:    if (!type)
                    535:       return (FALSE);
                    536: 
1.106     cvs       537:    strcpy (temptype, type);
1.58      cvs       538:    /* Normalize the type */
                    539:    i = 0;
1.106     cvs       540:    while (temptype[i] != EOS)
1.58      cvs       541:      {
                    542:        temptype[i] = tolower (temptype[i]);
                    543:        i++;
                    544:      }
1.106     cvs       545:    if ((!strcmp (temptype, "gif")) || (!strcmp (temptype, "x-xbitmap")) ||
                    546:        (!strcmp (temptype, "x-xpixmap")) || (!strcmp (temptype, "jpeg")) ||
                    547:        (!strcmp (temptype, "png")))
1.58      cvs       548:       return (TRUE);
                    549:    return (FALSE);
                    550: }
                    551: 
                    552: /*----------------------------------------------------------------------
1.9       cvs       553:   IsTextName                                                         
1.4       cvs       554:   ----------------------------------------------------------------------*/
1.106     cvs       555: ThotBool             IsTextName (const char *path)
                    556: {
                    557:    char                temppath[MAX_LENGTH];
                    558:    char                suffix[MAX_LENGTH];
                    559:    char                nsuffix[MAX_LENGTH];
1.5       cvs       560:    int                 i;
                    561: 
                    562:    if (!path)
1.13      cvs       563:      return (FALSE);
1.5       cvs       564: 
1.106     cvs       565:    strcpy (temppath, path);
1.5       cvs       566:    ExtractSuffix (temppath, suffix);
                    567: 
                    568:    /* Normalize the suffix */
                    569:    i = 0;
1.106     cvs       570:    while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.5       cvs       571:      {
1.25      cvs       572:        nsuffix[i] = tolower (suffix[i]);
1.5       cvs       573:        i++;
                    574:      }
1.106     cvs       575:    nsuffix[i] = EOS;
1.5       cvs       576: 
1.106     cvs       577:    if ((!strcmp (nsuffix, "txt")) || (!strcmp (nsuffix, "dtd")))
1.13      cvs       578:       return (TRUE);
1.106     cvs       579:    else if (!strcmp (nsuffix, "gz"))
1.13      cvs       580:      {
1.39      cvs       581:        /* take into account compressed files */
1.13      cvs       582:        ExtractSuffix (temppath, suffix);       
                    583:        /* Normalize the suffix */
                    584:        i = 0;
1.106     cvs       585:        while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13      cvs       586:         {
1.25      cvs       587:           nsuffix[i] = tolower (suffix[i]);
1.13      cvs       588:           i++;
                    589:         }
1.106     cvs       590:        nsuffix[i] = EOS;
                    591:        if ((!strcmp (nsuffix, "txt")) || (!strcmp (nsuffix, "dtd")))
1.13      cvs       592:         return (TRUE);
                    593:        else
                    594:         return (FALSE);
                    595:      }
                    596:    else
                    597:      return (FALSE);
1.3       cvs       598: }
                    599: 
1.4       cvs       600: /*----------------------------------------------------------------------
1.9       cvs       601:   IsHTTPPath                                     
                    602:   returns TRUE if path is in fact an http URL.
1.4       cvs       603:   ----------------------------------------------------------------------*/
1.106     cvs       604: ThotBool             IsHTTPPath (const char *path)
1.3       cvs       605: {
1.5       cvs       606:    if (!path)
                    607:       return FALSE;
1.3       cvs       608: 
1.106     cvs       609:    if ((!strncmp (path, "http:", 5) != 0)
                    610:        || (AHTFTPURL_flag () && !strncmp (path, "ftp:", 4))
                    611:        || !strncmp (path, "internal:", 9))
1.58      cvs       612:       return TRUE;
                    613:    return FALSE;
1.3       cvs       614: }
                    615: 
1.4       cvs       616: /*----------------------------------------------------------------------
1.9       cvs       617:   IsWithParameters                           
                    618:   returns TRUE if url has a concatenated query string.
1.4       cvs       619:   ----------------------------------------------------------------------*/
1.66      cvs       620: ThotBool            IsWithParameters (const char *url)
1.3       cvs       621: {
1.5       cvs       622:    int                 i;
1.3       cvs       623: 
1.9       cvs       624:    if ((!url) || (url[0] == EOS))
1.5       cvs       625:       return FALSE;
1.3       cvs       626: 
1.9       cvs       627:    i = strlen (url) - 1;
                    628:    while (i > 0 && url[i--] != '?')
1.5       cvs       629:       if (i < 0)
                    630:         return FALSE;
1.3       cvs       631: 
1.5       cvs       632:    /* There is a parameter */
                    633:    return TRUE;
1.3       cvs       634: }
                    635: 
1.4       cvs       636: /*----------------------------------------------------------------------
1.9       cvs       637:   IsW3Path                                           
                    638:   returns TRUE if path is in fact a URL.
1.4       cvs       639:   ----------------------------------------------------------------------*/
1.106     cvs       640: ThotBool             IsW3Path (const char *path)
                    641: {
                    642:   if (strncmp (path, "http:", 5)   && 
                    643:       strncmp (path, "ftp:", 4)    &&
                    644:       strncmp (path, "telnet:", 7) && 
                    645:       strncmp (path, "wais:", 5)   &&
                    646:       strncmp (path, "news:", 5)   && 
                    647:       strncmp (path, "gopher:", 7) &&
                    648:       strncmp (path, "mailto:", 7) && 
                    649:       strncmp (path, "archie:", 7))
1.72      cvs       650:     return FALSE;
                    651:   return TRUE;
1.3       cvs       652: }
                    653: 
1.4       cvs       654: /*----------------------------------------------------------------------
1.90      cvs       655:   IsFilePath                                           
                    656:   returns TRUE if path is in fact a URL.
                    657:   ----------------------------------------------------------------------*/
1.106     cvs       658: ThotBool             IsFilePath (const char *path)
1.90      cvs       659: {
1.106     cvs       660:   if (strncmp (path, "file:", 5))
1.90      cvs       661:     return FALSE;
                    662:   return TRUE;
                    663: }
                    664: 
                    665: /*----------------------------------------------------------------------
1.9       cvs       666:   IsValidProtocol                                                    
                    667:   returns true if the url protocol is supported by Amaya.
1.4       cvs       668:   ----------------------------------------------------------------------*/
1.106     cvs       669: ThotBool             IsValidProtocol (const char *url)
                    670: {
                    671:    if (!strncmp (url, "http:", 5)
                    672:       || !strncmp (url, "internal:", 9)
                    673:       || (AHTFTPURL_flag () && !strncmp (url, "ftp:", 4)))
1.22      cvs       674:        /* experimental */
1.24      cvs       675:      /*** || !strncmp (path, "news:", 5)***/ 
1.8       cvs       676:       return (TRUE);
1.5       cvs       677:    else
1.8       cvs       678:       return (FALSE);
1.3       cvs       679: }
                    680: 
1.31      cvs       681: 
                    682: /*----------------------------------------------------------------------
                    683:    GetBaseURL
                    684:    normalizes orgName according to a base associated with doc, and
                    685:    following the standard URL format rules.
                    686:    The function returns the base used to solve relative URL and SRC:
                    687:       - the base of the document,
                    688:       - or the document path (without document name).
                    689:   ----------------------------------------------------------------------*/
1.106     cvs       690: char  *GetBaseURL (Document doc)
1.31      cvs       691: {
                    692:   Element             el;
                    693:   ElementType         elType;
                    694:   AttributeType       attrType;
                    695:   Attribute           attr;
1.106     cvs       696:   char               *ptr, *basename;
1.31      cvs       697:   int                 length;
                    698: 
1.57      cvs       699:   /* @@@ irene */
                    700:   if (!DocumentURLs[doc])
1.110   ! cvs       701:      return NULL;
1.106     cvs       702:   basename = TtaGetMemory (MAX_LENGTH);
                    703:   strncpy (basename, DocumentURLs[doc], MAX_LENGTH-1);
                    704:   basename[MAX_LENGTH-1] = EOS;
1.31      cvs       705:   length = MAX_LENGTH -1;
1.110   ! cvs       706:   /* get the document element */
1.31      cvs       707:   el = TtaGetMainRoot (doc);
                    708:   /* search the BASE element */
                    709:   elType.ElSSchema = TtaGetDocumentSSchema (doc);
1.65      cvs       710:   elType.ElTypeNum = HTML_EL_HEAD;
                    711:   el = TtaSearchTypedElement (elType, SearchForward, el);
                    712:   if (el)
                    713:     {
                    714:       elType.ElTypeNum = HTML_EL_BASE;
                    715:       el = TtaSearchTypedElement (elType, SearchInTree, el);
                    716:     }
1.31      cvs       717:   if (el)
                    718:     {
                    719:       /*  The document has a BASE element -> Get the HREF attribute */
                    720:       attrType.AttrSSchema = elType.ElSSchema;
                    721:       attrType.AttrTypeNum = HTML_ATTR_HREF_;
                    722:       attr = TtaGetAttribute (el, attrType);
                    723:       if (attr)
                    724:        {
                    725:          /* Use the base path of the document */
                    726:          TtaGiveTextAttributeValue (attr, basename, &length);
                    727:          /* base and orgName have to be separated by a DIR_SEP */
                    728:          length--;
1.110   ! cvs       729:          if (basename[0] != EOS && basename[length] != URL_SEP &&
        !           730:              basename[length] != DIR_SEP) 
1.31      cvs       731:            /* verify if the base has the form "protocol://server:port" */
                    732:            {
1.106     cvs       733:              ptr = AmayaParseUrl (basename, "", AMAYA_PARSE_ACCESS |
1.33      cvs       734:                                                 AMAYA_PARSE_HOST |
                    735:                                                 AMAYA_PARSE_PUNCTUATION);
1.106     cvs       736:              if (ptr && !strcmp (ptr, basename))
1.31      cvs       737:                {
1.43      cvs       738:                  /* it has this form, we complete it by adding a URL_STR  */
1.106     cvs       739:                  if (strchr (basename, DIR_SEP))
                    740:                    strcat (basename, DIR_STR);
1.43      cvs       741:                  else
1.106     cvs       742:                    strcat (basename, URL_STR);
1.31      cvs       743:                  length++;
                    744:                }
                    745:              if (ptr)
                    746:                TtaFreeMemory (ptr);
                    747:            }
                    748:        }
1.33      cvs       749:       }
                    750:   
1.31      cvs       751:   /* Remove anything after the last DIR_SEP char. If no such char is found,
                    752:    * then search for the first ":" char, hoping that what's before that is a
                    753:    * protocol. If found, end the string there. If neither char is found,
                    754:    * then discard the whole base element.
                    755:    */
1.106     cvs       756:   length = strlen (basename) - 1;
1.31      cvs       757:   /* search for the last DIR_SEP char */
1.106     cvs       758:   while (length >= 0  && basename[length] != URL_SEP && basename[length] != DIR_SEP)
1.31      cvs       759:     length--;
                    760:   if (length >= 0)
                    761:     /* found the last DIR_SEP char, end the string there */
1.106     cvs       762:     basename[length + 1] = EOS;                   
1.31      cvs       763:   else
                    764:     /* search for the first PATH_STR char */
                    765:     {
1.106     cvs       766:       for (length = 0; basename[length] != ':' && 
                    767:             basename[length] != EOS; length ++);
                    768:       if (basename[length] == ':')
1.31      cvs       769:        /* found, so end the string there */
1.106     cvs       770:        basename[length + 1] = EOS;
1.31      cvs       771:       else
                    772:        /* not found, discard the base */
1.106     cvs       773:        basename[0] = EOS;
1.31      cvs       774:     }
                    775:   return (basename);
                    776: }
                    777: 
                    778: 
1.4       cvs       779: /*----------------------------------------------------------------------
1.40      cvs       780:    GetLocalPath
                    781:    Allocate and return the local document path associated to the url
                    782:   ----------------------------------------------------------------------*/
1.106     cvs       783: char  *GetLocalPath (Document doc, char  *url)
                    784: {
                    785:   char     *ptr;
                    786:   char     *n;
                    787:   char     *documentname;
                    788:   char      url_sep;
1.83      cvs       789:   int       len;
1.67      cvs       790:   ThotBool  noFile;
1.40      cvs       791: 
                    792:   if (url != NULL)
                    793:     {
                    794:       /* check whether the file name exists */
1.106     cvs       795:       len = strlen (url) - 1;
1.71      cvs       796:       if (IsW3Path (url))
1.106     cvs       797:          url_sep = '/';
1.41      cvs       798:       else 
1.106     cvs       799:           url_sep = DIR_SEP;
1.41      cvs       800:       noFile = (url[len] == url_sep);
1.40      cvs       801:       if (noFile)
1.106     cvs       802:          url[len] = EOS;
                    803:       ptr = TtaGetMemory (MAX_LENGTH);
                    804:       documentname = TtaGetMemory (MAX_LENGTH);
1.78      cvs       805:       TtaExtractName (url, ptr, documentname);
1.106     cvs       806:       sprintf (ptr, "%s%s%d%s", TempFileDirectory, DIR_STR, doc, DIR_STR);
1.40      cvs       807:       if (!TtaCheckDirectory (ptr))
                    808:        /* directory did not exist */
1.72      cvs       809:        TtaMakeDirectory (ptr);
1.47      cvs       810: 
                    811:       /* don't include the query string within document name */
1.106     cvs       812:       n = strrchr (documentname, '?');
1.47      cvs       813:       if (n != NULL)
1.106     cvs       814:          *n = EOS;
1.46      cvs       815:       /* don't include ':' within document name */
1.106     cvs       816:       n = strchr (documentname, ':');
1.46      cvs       817:       if (n != NULL)
1.106     cvs       818:          *n = EOS;
1.69      cvs       819:       /* if after all this operations document name
                    820:         is empty, let's use noname.html instead */
1.106     cvs       821:       if (documentname[0] == EOS)
                    822:          strcat (ptr, "noname.html");
1.69      cvs       823:       else
1.106     cvs       824:           strcat (ptr, documentname);
1.40      cvs       825:       TtaFreeMemory (documentname);
                    826:       /* restore the url */
                    827:       if (noFile)
1.41      cvs       828:        url[len] = url_sep;
1.40      cvs       829:       return (ptr);
                    830:     }
                    831:   else
                    832:     return (NULL);
                    833: }
                    834: 
1.73      cvs       835: /*----------------------------------------------------------------------
1.79      cvs       836:    ExtractTarget extract the target name from document nane.        
                    837:   ----------------------------------------------------------------------*/
1.106     cvs       838: void         ExtractTarget (char *aName, char *target)
1.79      cvs       839: {
1.106     cvs       840:    int    lg, i;
                    841:    char  *ptr;
                    842:    char  *oldptr;
1.79      cvs       843: 
                    844:    if (!target || !aName)
                    845:      /* bad target */
                    846:      return;
                    847: 
1.106     cvs       848:    target[0] = EOS;
                    849:    lg = strlen (aName);
1.79      cvs       850:    if (lg)
                    851:      {
                    852:        /* the name is not empty */
                    853:        oldptr = ptr = &aName[0];
                    854:        do
                    855:          {
1.106     cvs       856:             ptr = strrchr (oldptr, '#');
1.79      cvs       857:             if (ptr)
                    858:                oldptr = &ptr[1];
                    859:          }
                    860:        while (ptr);
                    861: 
                    862:        i = (int) (oldptr) - (int) (aName);     /* name length */
                    863:        if (i > 1)
                    864:          {
1.106     cvs       865:             aName[i - 1] = EOS;
1.79      cvs       866:             if (i != lg)
1.106     cvs       867:                strcpy (target, oldptr);
1.79      cvs       868:          }
                    869:      }
                    870: }
                    871: 
                    872: /*----------------------------------------------------------------------
1.90      cvs       873:    RemoveNewLines (text)
                    874:    Removes any '\n' chars that are found in text. 
                    875:    Returns TRUE if it did the operation, FALSE otherwise.
1.73      cvs       876:   ----------------------------------------------------------------------*/
1.106     cvs       877: ThotBool RemoveNewLines (char *text)
                    878: {
                    879:   ThotBool   change = FALSE;
                    880:   char      *src;
                    881:   char      *dest;
1.90      cvs       882: 
                    883:   src = text;
                    884:   dest = text;
                    885:   while (*src)
                    886:     {
                    887:       switch (*src)
                    888:        {
1.106     cvs       889:        case '\n':
1.90      cvs       890:          /* don't copy the newline */
                    891:          change = 1;
                    892:          break;
                    893:        default:
                    894:          *dest = *src;
                    895:          dest++;
                    896:          break;
                    897:        }
                    898:       src++;
                    899:     }
                    900:   /* copy the last EOS char */
                    901:   *dest = *src;
                    902: 
                    903:   return (change);
                    904: }
                    905: 
                    906: /*----------------------------------------------------------------------
                    907:    CleanCopyFileURL
                    908:    Copies a file url from a src string to destination string.
1.97      cvs       909:    convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
                    910:    we want to do).
1.90      cvs       911:   ----------------------------------------------------------------------*/
1.106     cvs       912: static void CleanCopyFileURL (char *dest, char *src,
                    913:                              ConvertionType convertion)
1.90      cvs       914: {
                    915:   while (*src)
1.89      cvs       916:     {
1.90      cvs       917:       switch (*src)
1.89      cvs       918:        {
                    919: #ifdef _WINDOWS
1.106     cvs       920:        case URL_SEP:
1.96      cvs       921:          /* make DIR_SEP transformation */
1.97      cvs       922:          if (convertion & AM_CONV_URL_SEP)
1.106     cvs       923:            *dest = DIR_SEP;
1.96      cvs       924:          else
                    925:            *dest = *src;
1.90      cvs       926:          dest++;
1.96      cvs       927:          src++;
1.90      cvs       928:          break;
1.89      cvs       929: #endif /* _WINDOWS */
1.96      cvs       930: 
1.106     cvs       931:        case '%':
1.97      cvs       932:          if (convertion & AM_CONV_PERCENT)
1.96      cvs       933:            {
1.97      cvs       934:              /* (code adapted from libwww's HTUnEscape function */
1.96      cvs       935:              src++;
1.106     cvs       936:              if (*src != EOS)
1.97      cvs       937:                {
                    938:                  *dest = UnEscapeChar (*src) * 16;
                    939:                  src++;
                    940:                }
1.106     cvs       941:              if (*src != EOS)
1.97      cvs       942:                {
                    943:                  *dest = *dest + UnEscapeChar (*src);
                    944:                  src++;
                    945:                }
                    946:              dest++;
1.96      cvs       947:            }
1.97      cvs       948:          else
1.96      cvs       949:            {
1.97      cvs       950:              *dest = *src;
                    951:              dest++;
1.96      cvs       952:              src++;
                    953:            }
                    954:          break;
                    955: 
1.90      cvs       956:        default:
                    957:          *dest = *src;
1.89      cvs       958:          dest++;
1.96      cvs       959:          src++;
1.90      cvs       960:          break;
1.89      cvs       961:        }
                    962:     }
1.90      cvs       963:   /* copy the EOS char */
                    964:   *dest = *src;
1.73      cvs       965: }
1.40      cvs       966: 
                    967: /*----------------------------------------------------------------------
1.9       cvs       968:    NormalizeURL
                    969:    normalizes orgName according to a base associated with doc, and
                    970:    following the standard URL format rules.
1.53      cvs       971:    if doc is 0 and otherPath not NULL, normalizes orgName according to this
                    972:    other path.
1.9       cvs       973:    The function returns the new complete and normalized URL 
1.12      cvs       974:    or file name path (newName) and the name of the document (docName).        
1.9       cvs       975:    N.B. If the function can't find out what's the docName, it assigns
                    976:    the name "noname.html".
1.4       cvs       977:   ----------------------------------------------------------------------*/
1.106     cvs       978: void NormalizeURL (char *orgName, Document doc, char *newName,
                    979:                   char *docName, char *otherPath)
                    980: {
                    981:    char          *basename;
                    982:    char           tempOrgName[MAX_LENGTH];
                    983:    char          *ptr;
                    984:    char           used_sep;
1.84      cvs       985:    int            length;
                    986:    ThotBool       check;
1.5       cvs       987: 
1.110   ! cvs       988: #ifdef _WINDOWS
1.44      cvs       989:    int ndx;
1.110   ! cvs       990: #endif /* _WINDOWS */
1.44      cvs       991: 
1.5       cvs       992:    if (!newName || !docName)
                    993:       return;
1.18      cvs       994: 
1.32      cvs       995:    if (doc != 0)
1.53      cvs       996:      basename = GetBaseURL (doc);
                    997:    else if (otherPath != NULL)
1.108     cvs       998:      basename = TtaStrdup (otherPath);
1.32      cvs       999:    else
1.53      cvs      1000:      basename = NULL;
1.32      cvs      1001: 
1.18      cvs      1002:    /*
1.31      cvs      1003:     * Clean orgName
                   1004:     * Make sure we have a complete orgName, without any leading or trailing
                   1005:     * white spaces, or trailinbg new lines
                   1006:     */
1.5       cvs      1007:    ptr = orgName;
1.18      cvs      1008:    /* skip leading white space and new line characters */
1.106     cvs      1009:    while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
                   1010:    strncpy (tempOrgName, ptr, MAX_LENGTH -1);
                   1011:    tempOrgName[MAX_LENGTH -1] = EOS;
1.18      cvs      1012:    /*
1.31      cvs      1013:     * Make orgName a complete URL
                   1014:     * If the URL does not include a protocol, then try to calculate
                   1015:     * one using the doc's base element (if it exists),
                   1016:     */
1.106     cvs      1017:    if (tempOrgName[0] == EOS)
1.53      cvs      1018:      {
1.106     cvs      1019:        newName[0] = EOS;
                   1020:        docName[0] = EOS;
1.53      cvs      1021:        TtaFreeMemory (basename);
                   1022:        return;
                   1023:      }
1.49      cvs      1024: 
                   1025:    /* clean trailing white space */
1.106     cvs      1026:    length = strlen (tempOrgName) - 1;
                   1027:    while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
1.53      cvs      1028:      {
1.106     cvs      1029:        tempOrgName[length] = EOS;
1.53      cvs      1030:        length--;
                   1031:      }
1.50      cvs      1032: 
1.55      cvs      1033:    /* remove extra dot (which dot???) */
                   1034:    /* ugly, but faster than a strcmp */
1.106     cvs      1035:    if (tempOrgName[length] == '.'
                   1036:        && (length == 0 || tempOrgName[length-1] != '.'))
                   1037:         tempOrgName[length] = EOS;
1.50      cvs      1038: 
1.94      cvs      1039:    if (IsW3Path (tempOrgName))
1.53      cvs      1040:      {
                   1041:        /* the name is complete, go to the Sixth Step */
1.106     cvs      1042:        strcpy (newName, tempOrgName);
1.53      cvs      1043:        SimplifyUrl (&newName);
                   1044:        /* verify if the URL has the form "protocol://server:port" */
1.110   ! cvs      1045:        ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS |
        !          1046:                                         AMAYA_PARSE_HOST |
        !          1047:                                         AMAYA_PARSE_PUNCTUATION);
        !          1048:        if (ptr && !strcmp (ptr, newName))
        !          1049:         /* it has this form, we complete it by adding a DIR_STR  */
1.106     cvs      1050:          strcat (newName, URL_STR);
1.49      cvs      1051: 
1.53      cvs      1052:        if (ptr)
1.50      cvs      1053:          TtaFreeMemory (ptr);
1.53      cvs      1054:      }
                   1055:    else if ( basename == NULL)
                   1056:      /* the name is complete, go to the Sixth Step */
1.106     cvs      1057:      strcpy (newName, tempOrgName);
1.53      cvs      1058:    else
                   1059:      {
1.31      cvs      1060:        /* Calculate the absolute URL, using the base or document URL */
1.110   ! cvs      1061: #ifdef _WINDOWS
1.53      cvs      1062:        if (!IsW3Path (basename))
                   1063:         {
1.106     cvs      1064:           length = strlen (tempOrgName);
1.53      cvs      1065:           for (ndx = 0; ndx < length; ndx++)
1.106     cvs      1066:             if (tempOrgName [ndx] == '/')
                   1067:               tempOrgName [ndx] = '\\';
1.53      cvs      1068:         }
1.110   ! cvs      1069: #endif /* _WINDOWS */
1.25      cvs      1070:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53      cvs      1071:        if (ptr)
                   1072:         {
                   1073:           SimplifyUrl (&ptr);
1.106     cvs      1074:           strcpy (newName, ptr);
1.53      cvs      1075:           TtaFreeMemory (ptr);
                   1076:         }
                   1077:        else
1.106     cvs      1078:         newName[0] = EOS;
1.53      cvs      1079:      }
1.36      cvs      1080: 
                   1081:    TtaFreeMemory (basename);
1.18      cvs      1082:    /*
1.31      cvs      1083:     * Prepare the docname that will refer to this ressource in the
                   1084:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                   1085:     * noname.html as a default ressource name
1.18      cvs      1086:    */
1.106     cvs      1087:    if (newName[0] != EOS)
1.53      cvs      1088:      {
1.106     cvs      1089:        length = strlen (newName) - 1;
                   1090:        if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
1.53      cvs      1091:         {
                   1092:           used_sep = newName[length];
                   1093:           check = TRUE;
                   1094:           while (check)
                   1095:             {
1.50      cvs      1096:                length--;
                   1097:                while (length >= 0 && newName[length] != used_sep)
1.53      cvs      1098:                 length--;
1.106     cvs      1099:                if (!strncmp (&newName[length+1], "..", 2))
1.53      cvs      1100:                 {
1.106     cvs      1101:                   newName[length+1] = EOS;
1.53      cvs      1102:                   /* remove also previous directory */
                   1103:                   length--;
                   1104:                   while (length >= 0 && newName[length] != used_sep)
                   1105:                     length--;
1.106     cvs      1106:                   if (strncmp (&newName[length+1], "//", 2))
1.53      cvs      1107:                     /* don't remove server name */
1.106     cvs      1108:                      newName[length+1] = EOS;
1.53      cvs      1109:                 }
1.106     cvs      1110:               else if (!strncmp (&newName[length+1], ".", 1))
                   1111:                 newName[length+1] = EOS;
1.50      cvs      1112:                else
1.53      cvs      1113:                 check = FALSE;
                   1114:             }
1.106     cvs      1115:           strcpy (docName, "noname.html");            
1.53      cvs      1116:           /* docname was not comprised inside the URL, so let's */
                   1117:           /* assign the default ressource name */
1.106     cvs      1118:           strcpy (docName, "noname.html");
1.53      cvs      1119:         }
                   1120:        else
                   1121:         { /* docname is comprised inside the URL */
1.110   ! cvs      1122:            while (length >= 0 && newName[length] != URL_SEP &&
        !          1123:                  newName[length] != DIR_SEP)
1.53      cvs      1124:             length--;
                   1125:           if (length < 0)
1.106     cvs      1126:              strcpy (docName, newName);
1.53      cvs      1127:           else
1.106     cvs      1128:             strcpy (docName, &newName[length+1]);
1.53      cvs      1129:         }
                   1130:      }
                   1131:    else
1.106     cvs      1132:      docName[0] = EOS;
1.18      cvs      1133: } 
1.3       cvs      1134: 
1.4       cvs      1135: /*----------------------------------------------------------------------
1.9       cvs      1136:   IsSameHost                                                         
1.4       cvs      1137:   ----------------------------------------------------------------------*/
1.106     cvs      1138: ThotBool IsSameHost (const char *url1, const char *url2)
1.3       cvs      1139: {
1.106     cvs      1140:   char          *basename_ptr1, *basename_ptr2;
                   1141:   ThotBool       result;
1.3       cvs      1142: 
1.106     cvs      1143:   basename_ptr1 = AmayaParseUrl (url1, "",
                   1144:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                   1145:   basename_ptr2 = AmayaParseUrl (url2, "",
                   1146:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs      1147: 
1.106     cvs      1148:   if (strcmp (basename_ptr1, basename_ptr2))
                   1149:     result = FALSE;
                   1150:   else
                   1151:     result = TRUE;
                   1152:   TtaFreeMemory (basename_ptr1);
                   1153:   TtaFreeMemory (basename_ptr2);
                   1154:   return (result);
1.3       cvs      1155: }
                   1156: 
                   1157: 
1.4       cvs      1158: /*----------------------------------------------------------------------
1.22      cvs      1159:   HasKnownFileSuffix
                   1160:   returns TRUE if path points to a file ending with a suffix.
                   1161:   ----------------------------------------------------------------------*/
1.106     cvs      1162: ThotBool             HasKnownFileSuffix (const char *path)
                   1163: {
                   1164:    char       *root;
                   1165:    char        temppath[MAX_LENGTH];
                   1166:    char        suffix[MAX_LENGTH];
1.22      cvs      1167: 
1.106     cvs      1168:    if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22      cvs      1169:      return (FALSE);
                   1170: 
1.106     cvs      1171:    root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs      1172: 
                   1173:    if (root) 
                   1174:      {
1.106     cvs      1175:        strcpy (temppath, root);
1.25      cvs      1176:        TtaFreeMemory (root);
1.22      cvs      1177:        /* Get the suffix */
                   1178:        ExtractSuffix (temppath, suffix); 
                   1179: 
1.106     cvs      1180:        if( suffix[0] == EOS)
1.22      cvs      1181:         /* no suffix */
                   1182:         return (FALSE);
                   1183: 
                   1184:        /* Normalize the suffix */
                   1185:        ConvertToLowerCase (suffix);
                   1186: 
1.106     cvs      1187:        if (!strcmp (suffix, "gz"))
1.22      cvs      1188:         /* skip the compressed suffix */
                   1189:         {
                   1190:         ExtractSuffix (temppath, suffix);
1.106     cvs      1191:         if(suffix[0] == EOS)
1.22      cvs      1192:           /* no suffix */
                   1193:           return (FALSE);
                   1194:          /* Normalize the suffix */
                   1195:          ConvertToLowerCase (suffix);
                   1196:         }
                   1197: 
1.106     cvs      1198:        if (strcmp (suffix, "gif") &&
                   1199:           strcmp (suffix, "xbm") &&
                   1200:           strcmp (suffix, "xpm") &&
                   1201:           strcmp (suffix, "jpg") &&
                   1202:           strcmp (suffix, "pdf") &&
                   1203:           strcmp (suffix, "png") &&
                   1204:           strcmp (suffix, "tgz") &&
                   1205:           strcmp (suffix, "xpg") &&
                   1206:           strcmp (suffix, "xpd") &&
                   1207:           strcmp (suffix, "ps") &&
                   1208:           strcmp (suffix, "au") &&
                   1209:           strcmp (suffix, "html") &&
                   1210:           strcmp (suffix, "htm") &&
                   1211:           strcmp (suffix, "shtml") &&
                   1212:           strcmp (suffix, "xht") &&
                   1213:           strcmp (suffix, "xhtm") &&
                   1214:           strcmp (suffix, "xhtml") &&
                   1215:           strcmp (suffix, "txt") &&
                   1216:           strcmp (suffix, "css") &&
                   1217:           strcmp (suffix, "eps"))
1.22      cvs      1218:         return (FALSE);
                   1219:        else
                   1220:         return (TRUE);
                   1221:      }
                   1222:    else
                   1223:      return (FALSE);
                   1224: }
                   1225: 
                   1226: 
                   1227: /*----------------------------------------------------------------------
1.24      cvs      1228:   ChopURL
                   1229:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                   1230:   If inputURL is  bigger than that size, outputURL receives
                   1231:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                   1232:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                   1233:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                   1234:   copied into outputURL. 
                   1235:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                   1236:   chars.
                   1237:   ----------------------------------------------------------------------*/
1.106     cvs      1238: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs      1239: {
                   1240:   int len;
1.9       cvs      1241: 
1.106     cvs      1242:   len = strlen (inputURL);
1.24      cvs      1243:   if (len <= MAX_PRINT_URL_LENGTH) 
1.106     cvs      1244:     strcpy (outputURL, inputURL);
1.24      cvs      1245:   else
                   1246:     /* make a truncated urlName on the status window */
                   1247:     {
1.106     cvs      1248:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                   1249:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                   1250:       strcat (outputURL, "...");
                   1251:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24      cvs      1252:     }
1.25      cvs      1253: }
                   1254: 
                   1255: 
                   1256: /*----------------------------------------------------------------------
                   1257:    scan
1.47      cvs      1258:        Scan a filename for its constituents
1.25      cvs      1259:        -----------------------------------
                   1260:   
                   1261:    On entry,
                   1262:        name    points to a document name which may be incomplete.
                   1263:    On exit,
                   1264:         absolute or relative may be nonzero (but not both).
                   1265:        host, fragment and access may be nonzero if they were specified.
                   1266:        Any which are nonzero point to zero terminated strings.
                   1267:   ----------------------------------------------------------------------*/
1.106     cvs      1268: static void scan (char *name, HTURI *parts)
1.25      cvs      1269: {
1.106     cvs      1270:   char *   p;
                   1271:   char *   after_access = name;
1.32      cvs      1272: 
1.43      cvs      1273:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs      1274:   /* Look for fragment identifier */
1.106     cvs      1275:   if ((p = strchr(name, '#')) != NULL)
1.28      cvs      1276:     {
1.106     cvs      1277:       *p++ = '\0';
1.28      cvs      1278:       parts->fragment = p;
1.25      cvs      1279:     }
                   1280:     
1.28      cvs      1281:   for (p=name; *p; p++)
                   1282:     {
1.106     cvs      1283:       if (*p == URL_SEP || *p == DIR_SEP || *p == '#' || *p == '?')
1.28      cvs      1284:        break;
1.106     cvs      1285:       if (*p == ':')
1.28      cvs      1286:        {
                   1287:          *p = 0;
                   1288:          parts->access = after_access; /* Scheme has been specified */
                   1289: 
                   1290:          /* The combination of gcc, the "-O" flag and the HP platform is
                   1291:             unhealthy. The following three lines is a quick & dirty fix, but is
                   1292:             not recommended. Rather, turn off "-O". */
                   1293: 
                   1294:          /*            after_access = p;*/
                   1295:          /*            while (*after_access == 0)*/
                   1296:          /*                after_access++;*/
                   1297:          after_access = p+1;
1.106     cvs      1298:          if (!strcasecmp("URL", parts->access))
1.28      cvs      1299:            /* Ignore IETF's URL: pre-prefix */
                   1300:            parts->access = NULL;
                   1301:          else
1.25      cvs      1302:            break;
                   1303:        }
                   1304:     }
                   1305:     
                   1306:     p = after_access;
1.43      cvs      1307:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs      1308:       {
1.43      cvs      1309:        if (p[1] == URL_SEP)
1.28      cvs      1310:          {
1.25      cvs      1311:            parts->host = p+2;          /* host has been specified      */
1.28      cvs      1312:            *p = 0;                     /* Terminate access             */
                   1313:            /* look for end of host name if any */
1.106     cvs      1314:            p = strchr (parts->host, URL_SEP);
1.28      cvs      1315:            if (p)
                   1316:              {
1.106     cvs      1317:                *p = EOS;                       /* Terminate host */
1.25      cvs      1318:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs      1319:              }
                   1320:          }
                   1321:        else
                   1322:          /* Root found but no host */
                   1323:          parts->absolute = p+1;
                   1324:       }
                   1325:     else
                   1326:       {
1.25      cvs      1327:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs      1328:       }
1.25      cvs      1329: }
                   1330: 
                   1331: 
                   1332: /*----------------------------------------------------------------------
1.28      cvs      1333:   AmayaParseUrl: parse a Name relative to another name
                   1334: 
                   1335:   This returns those parts of a name which are given (and requested)
                   1336:   substituting bits from the related name where necessary.
1.25      cvs      1337:   
1.28      cvs      1338:   On entry,
1.25      cvs      1339:        aName           A filename given
                   1340:         relatedName     A name relative to which aName is to be parsed. Give
                   1341:                         it an empty string if aName is absolute.
                   1342:         wanted          A mask for the bits which are wanted.
                   1343:   
1.28      cvs      1344:   On exit,
1.25      cvs      1345:        returns         A pointer to a malloc'd string which MUST BE FREED
                   1346:   ----------------------------------------------------------------------*/
1.106     cvs      1347: char   *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
                   1348: {
                   1349:   char      *return_value;
                   1350:   char       result[MAX_LENGTH];
                   1351:   char       name[MAX_LENGTH];
                   1352:   char       rel[MAX_LENGTH];
                   1353:   char      *p, *access;
1.29      cvs      1354:   HTURI      given, related;
                   1355:   int        len;
1.106     cvs      1356:   char       used_sep;
                   1357:   char      *used_str;
1.32      cvs      1358: 
1.106     cvs      1359:   if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33      cvs      1360:     {
1.106     cvs      1361:       used_str = DIR_STR;
                   1362:       used_sep = DIR_SEP;
1.33      cvs      1363:     }
1.32      cvs      1364:   else
1.33      cvs      1365:     {
1.106     cvs      1366:       used_str = URL_STR;
                   1367:       used_sep = URL_SEP;
1.33      cvs      1368:     }
1.32      cvs      1369: 
1.29      cvs      1370:   /* Make working copies of input strings to cut up: */
                   1371:   return_value = NULL;
                   1372:   result[0] = 0;               /* Clear string  */
1.106     cvs      1373:   strcpy (name, aName);
1.29      cvs      1374:   if (relatedName != NULL)  
1.106     cvs      1375:     strcpy (rel, relatedName);
1.29      cvs      1376:   else
1.106     cvs      1377:     relatedName[0] = EOS;
1.29      cvs      1378:   
                   1379:   scan (name, &given);
                   1380:   scan (rel,  &related); 
                   1381:   access = given.access ? given.access : related.access;
                   1382:   if (wanted & AMAYA_PARSE_ACCESS)
                   1383:     if (access)
                   1384:       {
1.106     cvs      1385:        strcat (result, access);
1.29      cvs      1386:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1387:                strcat (result, ":");
1.29      cvs      1388:       }
                   1389:   
                   1390:   if (given.access && related.access)
                   1391:     /* If different, inherit nothing. */
1.106     cvs      1392:     if (strcmp (given.access, related.access) != 0)
1.29      cvs      1393:       {
                   1394:        related.host = 0;
                   1395:        related.absolute = 0;
                   1396:        related.relative = 0;
                   1397:        related.fragment = 0;
                   1398:       }
                   1399:   
                   1400:   if (wanted & AMAYA_PARSE_HOST)
                   1401:     if(given.host || related.host)
                   1402:       {
                   1403:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1404:          strcat (result, "//");
                   1405:        strcat (result, given.host ? given.host : related.host);
1.29      cvs      1406:       }
                   1407:   
                   1408:   if (given.host && related.host)
                   1409:     /* If different hosts, inherit no path. */
1.106     cvs      1410:     if (strcmp (given.host, related.host) != 0)
1.29      cvs      1411:       {
                   1412:        related.absolute = 0;
                   1413:        related.relative = 0;
                   1414:        related.fragment = 0;
                   1415:       }
                   1416:   
                   1417:   if (wanted & AMAYA_PARSE_PATH)
                   1418:     {
                   1419:       if (given.absolute)
                   1420:        {
                   1421:          /* All is given */
                   1422:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1423:            strcat (result, used_str);
                   1424:          strcat (result, given.absolute);
1.25      cvs      1425:        }
1.29      cvs      1426:       else if (related.absolute)
                   1427:        {
                   1428:          /* Adopt path not name */
1.106     cvs      1429:          strcat (result, used_str);
                   1430:          strcat (result, related.absolute);
1.29      cvs      1431:          if (given.relative)
                   1432:            {
                   1433:              /* Search part? */
1.106     cvs      1434:              p = strchr (result, '?');
1.29      cvs      1435:              if (!p)
1.106     cvs      1436:                p=result+strlen(result)-1;
1.33      cvs      1437:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1438:              /* Remove filename */
                   1439:              p[1]=0;
                   1440:              /* Add given one */
1.106     cvs      1441:              strcat (result, given.relative);
1.25      cvs      1442:            }
                   1443:        }
1.29      cvs      1444:       else if (given.relative)
                   1445:        /* what we've got */
1.106     cvs      1446:        strcat (result, given.relative);
1.29      cvs      1447:       else if (related.relative)
1.106     cvs      1448:        strcat (result, related.relative);
1.29      cvs      1449:       else
                   1450:        /* No inheritance */
1.106     cvs      1451:        strcat (result, used_str);
1.25      cvs      1452:     }
1.29      cvs      1453:   
                   1454:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1455:     if (given.fragment || related.fragment)
                   1456:       {
                   1457:        if (given.absolute && given.fragment)
                   1458:          {
                   1459:            /*Fixes for relURLs...*/
                   1460:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1461:              strcat (result, "#");
                   1462:            strcat (result, given.fragment); 
1.29      cvs      1463:          }
                   1464:        else if (!(given.absolute) && !(given.fragment))
1.106     cvs      1465:          strcat (result, "");
1.29      cvs      1466:        else
                   1467:          {
1.110   ! cvs      1468:           if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1469:              strcat (result, "#");
1.110   ! cvs      1470:           strcat (result, given.fragment ? given.fragment : related.fragment); 
1.29      cvs      1471:          }
                   1472:       }
1.106     cvs      1473:   len = strlen (result);
                   1474:   if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1475:     strcpy (return_value, result);
1.29      cvs      1476:   return (return_value);               /* exactly the right length */
1.25      cvs      1477: }
                   1478: 
                   1479: /*----------------------------------------------------------------------
                   1480:      HTCanon
                   1481:        Canonicalizes the URL in the following manner starting from the host
                   1482:        pointer:
                   1483:   
                   1484:        1) The host name is converted to lowercase
                   1485:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1486:   
                   1487:        Return: OK      The position of the current path part of the URL
                   1488:                        which might be the old one or a new one.
                   1489:   
                   1490:   ----------------------------------------------------------------------*/
1.106     cvs      1491: static char   *HTCanon (char **filename, char *host)
                   1492: {
                   1493:     char   *newname = NULL;
                   1494:     char    used_sep;
                   1495:     char   *path;
                   1496:     char   *strptr;
                   1497:     char   *port;
                   1498:     char   *access = host-3;
                   1499:   
                   1500:      if (*filename && strchr (*filename, URL_SEP))
                   1501:         used_sep = URL_SEP;
1.33      cvs      1502:      else
1.106     cvs      1503:         used_sep = DIR_SEP;
1.32      cvs      1504:   
1.110   ! cvs      1505:     while (access > *filename && *(access - 1) != used_sep) /* Find access method */
1.25      cvs      1506:        access--;
1.110   ! cvs      1507:     if ((path = strchr (host, used_sep)) == NULL)              /* Find path */
1.106     cvs      1508:        path = host + strlen (host);
                   1509:     if ((strptr = strchr (host, '@')) != NULL && strptr < path)           /* UserId */
1.82      cvs      1510:        host = strptr;
1.110   ! cvs      1511:     if ((port = strchr (host, ':')) != NULL && port > path)   /* Port number */
1.82      cvs      1512:        port = NULL;
1.25      cvs      1513: 
                   1514:     strptr = host;                                 /* Convert to lower-case */
1.82      cvs      1515:     while (strptr < path)
1.33      cvs      1516:       {
1.84      cvs      1517:          *strptr = utolower (*strptr);
1.82      cvs      1518:          strptr++;
1.33      cvs      1519:       }
1.25      cvs      1520:     
                   1521:     /* Does the URL contain a full domain name? This also works for a
                   1522:        numerical host name. The domain name is already made lower-case
                   1523:        and without a trailing dot. */
                   1524:     {
1.106     cvs      1525:       char  *dot = port ? port : path;
                   1526:       if (dot > *filename && *--dot == '.')
1.33      cvs      1527:        {
1.106     cvs      1528:          char  *orig = dot;
                   1529:          char  *dest = dot + 1;
1.82      cvs      1530:          while ((*orig++ = *dest++));
                   1531:             if (port) port--;
1.33      cvs      1532:          path--;
1.25      cvs      1533:        }
                   1534:     }
                   1535:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1536:     if (port)
                   1537:       {
1.82      cvs      1538:        if (!*(port+1) || *(port+1) == used_sep)
1.33      cvs      1539:          {
                   1540:            if (!newname)
                   1541:              {
1.106     cvs      1542:                char  *orig = port; 
                   1543:                char  *dest = port + 1;
1.82      cvs      1544:                while ((*orig++ = *dest++));
1.33      cvs      1545:              }
                   1546:          }
1.106     cvs      1547:        else if ((!strncmp (access, "http", 4)   &&
                   1548:              (*(port + 1) == '8'                    && 
                   1549:              *(port+2) == '0'                       && 
1.82      cvs      1550:              (*(port+3) == used_sep || !*(port + 3))))       ||
1.106     cvs      1551:              (!strncmp (access, "gopher", 6) &&
                   1552:              (*(port+1) == '7'                      && 
                   1553:              *(port+2) == '0'                       && 
1.82      cvs      1554:              (*(port+3) == used_sep || !*(port+3))))         ||
1.106     cvs      1555:              (!strncmp (access, "ftp", 3)    &&
                   1556:              (*(port+1) == '2'                      && 
                   1557:              *(port + 2) == '1'                     && 
1.82      cvs      1558:              (*(port+3) == used_sep || !*(port+3))))) {
1.33      cvs      1559:          if (!newname)
                   1560:            {
1.106     cvs      1561:              char  *orig = port; 
                   1562:              char  *dest = port + 3;
1.33      cvs      1563:              while((*orig++ = *dest++));
                   1564:              /* Update path position, Henry Minsky */
                   1565:              path -= 3;
1.25      cvs      1566:            }
1.33      cvs      1567:        }
                   1568:        else if (newname)
1.106     cvs      1569:          strncat (newname, port, (int) (path - port));
1.33      cvs      1570:       }
1.25      cvs      1571: 
1.33      cvs      1572:     if (newname)
                   1573:       {
1.106     cvs      1574:        char  *newpath = newname + strlen (newname);
                   1575:        strcat (newname, path);
1.25      cvs      1576:        path = newpath;
1.28      cvs      1577:        /* Free old copy */
                   1578:        TtaFreeMemory(*filename);
1.25      cvs      1579:        *filename = newname;
1.33      cvs      1580:       }
1.25      cvs      1581:     return path;
                   1582: }
                   1583: 
                   1584: 
                   1585: /*----------------------------------------------------------------------
1.29      cvs      1586:   SimplifyUrl: simplify a URI
1.32      cvs      1587:   A URI is allowed to contain the sequence xxx/../ which may be
                   1588:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1589:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1590:   
1.28      cvs      1591:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1592:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1593:   
1.28      cvs      1594:   but we should NOT change
                   1595:                 http://fred.xxx.edu/../..
1.25      cvs      1596:   
                   1597:        or      ../../albert.html
                   1598:   
1.28      cvs      1599:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1600:   
                   1601:                /fred/..                becomes /fred/..
                   1602:                /fred/././..            becomes /fred/..
                   1603:                /fred/.././junk/.././   becomes /fred/..
                   1604:   
1.28      cvs      1605:   If more than one set of `://' is found (several proxies in cascade) then
                   1606:   only the part after the last `://' is simplified.
1.25      cvs      1607:   
1.28      cvs      1608:   Returns: A string which might be the old one or a new one.
1.25      cvs      1609:   ----------------------------------------------------------------------*/
1.106     cvs      1610: void         SimplifyUrl (char **url)
                   1611: {
                   1612:   char   *path;
                   1613:   char   *access;
                   1614:   char   *newptr; 
                   1615:   char   *p;
                   1616:   char   *orig, *dest, *end;
1.28      cvs      1617: 
1.106     cvs      1618:   char      used_sep;
1.77      cvs      1619:   ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
                   1620:                             something/../ simplification in relative URLs when they start with a ../ */
1.32      cvs      1621: 
                   1622: 
1.28      cvs      1623:   if (!url || !*url)
                   1624:     return;
                   1625: 
1.106     cvs      1626:   if (strchr (*url, URL_SEP))
                   1627:       used_sep = URL_SEP;
1.32      cvs      1628:   else
1.106     cvs      1629:       used_sep = DIR_SEP;
1.32      cvs      1630: 
1.77      cvs      1631:   /* should we simplify double dot? */
                   1632:   path = *url;
1.106     cvs      1633:   if (*path == '.' && *(path + 1) == '.')
1.77      cvs      1634:     ddot_simplify = FALSE;
                   1635:   else
                   1636:     ddot_simplify = TRUE;
                   1637: 
1.28      cvs      1638:   /* Find any scheme name */
1.106     cvs      1639:   if ((path = strstr (*url, "://")) != NULL)
1.33      cvs      1640:     {
                   1641:       /* Find host name */
1.28      cvs      1642:       access = *url;
1.84      cvs      1643:       while (access < path && (*access = utolower (*access)))
1.82      cvs      1644:             access++;
1.28      cvs      1645:       path += 3;
1.106     cvs      1646:       while ((newptr = strstr (path, "://")) != NULL)
1.82      cvs      1647:             /* For proxies */
1.106     cvs      1648:             path = newptr + 3;
1.82      cvs      1649:      /* We have a host name */
1.84      cvs      1650:       path = HTCanon (url, path);
1.25      cvs      1651:     }
1.106     cvs      1652:   else if ((path = strstr (*url, ":/")) != NULL)
1.28      cvs      1653:     path += 2;
                   1654:   else
                   1655:     path = *url;
1.25      cvs      1656: 
1.84      cvs      1657:   if (*path == used_sep && *(path+1) == used_sep)
1.28      cvs      1658:     /* Some URLs start //<foo> */
                   1659:     path += 1;
1.94      cvs      1660:   else if (IsFilePath (path))
                   1661:     {
                   1662:       /* doesn't need to do anything more */
                   1663:       return;
                   1664:     }
1.106     cvs      1665:   else if (!strncmp (path, "news:", 5))
1.28      cvs      1666:     {
1.106     cvs      1667:       newptr = strchr (path+5, '@');
1.28      cvs      1668:       if (!newptr)
                   1669:        newptr = path + 5;
                   1670:       while (*newptr)
                   1671:        {
                   1672:          /* Make group or host lower case */
1.84      cvs      1673:          *newptr = utolower (*newptr);
1.28      cvs      1674:          newptr++;
1.25      cvs      1675:        }
1.28      cvs      1676:       /* Doesn't need to do any more */
                   1677:       return;
1.25      cvs      1678:     }
1.28      cvs      1679: 
                   1680:   if ((p = path))
                   1681:     {
1.106     cvs      1682:       if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
                   1683:            (end = strchr (path, '#'))))
                   1684:        end = path + strlen (path);
1.28      cvs      1685:       
                   1686:       /* Parse string second time to simplify */
                   1687:       p = path;
                   1688:       while (p < end)
                   1689:        {
1.110   ! cvs      1690:          /* if we're pointing to a char, it's safe to reactivate the 
        !          1691:             ../ convertion */
1.106     cvs      1692:          if (!ddot_simplify && *p != '.' && *p != used_sep)
1.77      cvs      1693:            ddot_simplify = TRUE;
                   1694: 
1.33      cvs      1695:          if (*p==used_sep)
1.28      cvs      1696:            {
1.106     cvs      1697:              if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      1698:                {
                   1699:                  orig = p + 1;
1.84      cvs      1700:                  dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52      cvs      1701:                  while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28      cvs      1702:                  end = orig - 1;
                   1703:                }
1.106     cvs      1704:              else if (ddot_simplify && *(p+1) == '.' && *(p+2) == '.' 
1.77      cvs      1705:                       && (*(p+3) == used_sep || !*(p+3)))
1.28      cvs      1706:                {
                   1707:                  newptr = p;
1.52      cvs      1708:                  while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
                   1709:                  if (*newptr == used_sep)
                   1710:                    orig = newptr + 1;
1.28      cvs      1711:                  else
1.52      cvs      1712:                    orig = newptr;
                   1713: 
                   1714:                  dest = (*(p+3) != used_sep) ? p+3 : p+4;
                   1715:                  while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   1716:                  end = orig-1;
                   1717:                  /* Start again with prev slash */
                   1718:                  p = newptr;
1.28      cvs      1719:                }
1.33      cvs      1720:              else if (*(p+1) == used_sep)
1.28      cvs      1721:                {
1.33      cvs      1722:                  while (*(p+1) == used_sep)
1.28      cvs      1723:                    {
                   1724:                      orig = p;
                   1725:                      dest = p + 1;
                   1726:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   1727:                      end = orig-1;
                   1728:                    }
                   1729:                }
                   1730:              else
1.25      cvs      1731:                p++;
1.28      cvs      1732:            }
                   1733:          else
                   1734:            p++;
1.25      cvs      1735:        }
                   1736:     }
1.51      cvs      1737: 
                   1738:     /*
                   1739:     **  Check for host/../.. kind of things
                   1740:     */
1.106     cvs      1741:     if (*path == used_sep && *(path+1) == '.' && *(path+2) == '.' 
1.77      cvs      1742:        && (!*(path+3) || *(path+3) == used_sep))
1.106     cvs      1743:        *(path+1) = EOS;
1.51      cvs      1744: 
1.28      cvs      1745:   return;
                   1746: }
                   1747: 
                   1748: 
                   1749: /*----------------------------------------------------------------------
1.96      cvs      1750:    NormalizeFile normalizes local names.                             
1.28      cvs      1751:    Return TRUE if target and src differ.                           
                   1752:   ----------------------------------------------------------------------*/
1.106     cvs      1753: ThotBool NormalizeFile (char *src, char *target, ConvertionType convertion)
1.28      cvs      1754: {
1.110   ! cvs      1755: #ifndef _WINDOWS
1.106     cvs      1756:    char             *s;
1.93      cvs      1757:    int               i;
1.110   ! cvs      1758: #endif /* !_WINDOWS */
1.82      cvs      1759:    ThotBool          change;
1.90      cvs      1760:    int               start_index; /* the first char that we'll copy */
1.28      cvs      1761: 
1.54      cvs      1762:    change = FALSE;
1.90      cvs      1763:    start_index = 0;
                   1764: 
1.106     cvs      1765:    if (!src || src[0] == EOS)
1.96      cvs      1766:      {
1.106     cvs      1767:        target[0] = EOS;
1.96      cvs      1768:        return FALSE;
                   1769:      }
1.90      cvs      1770: 
                   1771:    /* @@ do I need file: or file:/ here? */
1.106     cvs      1772:    if (strncmp (src, "file:", 5) == 0)
1.28      cvs      1773:      {
1.90      cvs      1774:        /* remove the prefix file: */
                   1775:        start_index += 5;
                   1776:    
                   1777:        /* remove the localhost prefix */
1.106     cvs      1778:        if (strncmp (&src[start_index], "//localhost/", 12) == 0)
1.94      cvs      1779:           start_index += 11;
                   1780:        
                   1781:        /* remove the first two slashes in / / /path */
                   1782:        while (src[start_index] &&
1.106     cvs      1783:              src[start_index] == '/' 
                   1784:              && src[start_index + 1] == '/')
1.94      cvs      1785:         start_index++;
                   1786: 
                   1787: #ifdef _WINDOWS
                   1788:        /* remove any extra slash before the drive name */
1.106     cvs      1789:        if (src[start_index] == '/'
                   1790:           &&src[start_index+2] == ':')
1.94      cvs      1791:         start_index++;
                   1792: #endif /* _WINDOWS */
1.90      cvs      1793: 
1.106     cvs      1794:        if (src[start_index] == EOS)
1.90      cvs      1795:        /* if there's nothing afterwards, add a DIR_STR */
1.106     cvs      1796:         strcpy (target, DIR_STR);
1.90      cvs      1797:        else
1.97      cvs      1798:         /* as we're inside a file: URL, we'll apply all the convertions
                   1799:            we know */
                   1800:         CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96      cvs      1801: 
                   1802:        change = TRUE;
                   1803:      }
1.97      cvs      1804:    else if (convertion != AM_CONV_NONE)
1.96      cvs      1805:      {
                   1806:        /* we are following a "local" relative link, we do all the
                   1807:          convertions except for the HOME_DIR ~ one */
1.97      cvs      1808:        CleanCopyFileURL (target, src, convertion);
1.28      cvs      1809:      }
1.90      cvs      1810: #ifndef _WINDOWS
1.106     cvs      1811:    else if (src[0] == '~')
1.53      cvs      1812:      {
1.96      cvs      1813:        /* it must be a URL typed in a text input field */
                   1814:        /* do the HOME_DIR ~ substitution */
1.82      cvs      1815:        s = TtaGetEnvString ("HOME");
1.106     cvs      1816:        strcpy (target, s);
1.90      cvs      1817: #if 0
1.96      cvs      1818:        /* JK: invalidated this part of the code as it's simpler
                   1819:           to add the DIR_SEP whenever we have something to add
                   1820:           to the path rather than adding it systematically */
1.106     cvs      1821:        if (src[1] != DIR_SEP)
                   1822:          strcat (target, DIR_STR);
1.90      cvs      1823: #endif
1.106     cvs      1824:        i = strlen (target);
                   1825:        strcpy (&target[i], &src[1]);
1.54      cvs      1826:        change = TRUE;
1.53      cvs      1827:      }
1.90      cvs      1828: #endif /* _WINDOWS */
1.28      cvs      1829:    else
1.96      cvs      1830:    /* leave it as it is */
1.106     cvs      1831:      strcpy (target, src);
1.96      cvs      1832:    
1.28      cvs      1833:    /* remove /../ and /./ */
1.29      cvs      1834:    SimplifyUrl (&target);
1.54      cvs      1835:    if (!change)
1.106     cvs      1836:      change = strcmp (src, target);
1.28      cvs      1837:    return (change);
1.25      cvs      1838: }
                   1839: 
1.28      cvs      1840: 
1.25      cvs      1841: /*----------------------------------------------------------------------
1.31      cvs      1842:   MakeRelativeURL: make relative name
1.25      cvs      1843:   
1.28      cvs      1844:   This function creates and returns a string which gives an expression of
                   1845:   one address as related to another. Where there is no relation, an absolute
                   1846:   address is retured.
1.25      cvs      1847:   
1.28      cvs      1848:   On entry,
1.25      cvs      1849:        Both names must be absolute, fully qualified names of nodes
                   1850:        (no fragment bits)
                   1851:   
1.28      cvs      1852:   On exit,
1.25      cvs      1853:        The return result points to a newly allocated name which, if
                   1854:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   1855:        The caller is responsible for freeing the resulting name later.
                   1856:   ----------------------------------------------------------------------*/
1.106     cvs      1857: char      *MakeRelativeURL (char *aName, char *relatedName)
                   1858: {
                   1859:   char  *return_value;
                   1860:   char   result[MAX_LENGTH];
                   1861:   char  *p;
                   1862:   char  *q;
                   1863:   char  *after_access;
                   1864:   char  *last_slash = NULL;
                   1865:   int    slashes, levels, len;
1.110   ! cvs      1866: #ifdef _WINDOWS
1.44      cvs      1867:   int ndx;
1.110   ! cvs      1868: #endif /* _WINDOWS */
1.44      cvs      1869: 
1.29      cvs      1870:   if (aName == NULL || relatedName == NULL)
                   1871:     return (NULL);
                   1872: 
                   1873:   slashes = 0;
                   1874:   after_access = NULL;
                   1875:   p = aName;
                   1876:   q = relatedName;
                   1877:   for (; *p && (*p == *q); p++, q++)
1.27      cvs      1878:     {
                   1879:       /* Find extent of match */
1.106     cvs      1880:       if (*p == ':')
1.29      cvs      1881:        after_access = p + 1;
1.28      cvs      1882:       if (*p == DIR_SEP)
1.27      cvs      1883:        {
1.29      cvs      1884:          /* memorize the last slash position and count them */
1.27      cvs      1885:          last_slash = p;
                   1886:          slashes++;
1.25      cvs      1887:        }
                   1888:     }
                   1889:     
1.31      cvs      1890:   /* q, p point to the first non-matching character or zero */
1.106     cvs      1891:   if (*q == EOS)
1.31      cvs      1892:     {
                   1893:       /* New name is a subset of the related name */
                   1894:       /* exactly the right length */
1.106     cvs      1895:       len = strlen (p);
                   1896:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1897:        strcpy (return_value, p);
1.31      cvs      1898:     }
                   1899:   else if ((slashes < 2 && after_access == NULL)
                   1900:       || (slashes < 3 && after_access != NULL))
                   1901:     {
                   1902:       /* Two names whitout common path */
                   1903:       /* exactly the right length */
1.106     cvs      1904:       len = strlen (aName);
                   1905:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1906:        strcpy (return_value, aName);
1.31      cvs      1907:     }
                   1908:   else
                   1909:     {
                   1910:       /* Some path in common */
1.106     cvs      1911:       if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
1.31      cvs      1912:        /* just the same server */
1.106     cvs      1913:        strcpy (result, last_slash);
1.31      cvs      1914:       else
                   1915:        {
                   1916:          levels= 0; 
1.106     cvs      1917:          for (; *q && *q != '#' && *q != ';' && *q != '?'; q++)
1.31      cvs      1918:            if (*q == DIR_SEP)
                   1919:              levels++;
                   1920:          
1.106     cvs      1921:          result[0] = EOS;
1.31      cvs      1922:          for (;levels; levels--)
1.106     cvs      1923:            strcat (result, "../");
                   1924:          strcat (result, last_slash+1);
1.31      cvs      1925:        } 
1.52      cvs      1926: 
                   1927:       if (!*result)
1.106     cvs      1928:        strcat (result, "./");
1.52      cvs      1929: 
1.31      cvs      1930:       /* exactly the right length */
1.106     cvs      1931:       len = strlen (result);
                   1932:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1933:        strcpy (return_value, result);
1.52      cvs      1934: 
1.25      cvs      1935:     }
1.110   ! cvs      1936: #ifdef _WINDOWS
1.106     cvs      1937:   len = strlen (return_value);
1.44      cvs      1938:   for (ndx = 0; ndx < len; ndx ++)
1.106     cvs      1939:          if (return_value[ndx] == '\\')
                   1940:             return_value[ndx] = '/' ;
1.110   ! cvs      1941: #endif /* _WINDOWS */
1.29      cvs      1942:   return (return_value);
1.24      cvs      1943: }
1.35      cvs      1944: 
1.104     kahan    1945: /*----------------------------------------------------------------------
                   1946:   AM_GetFileSize
                   1947:   Returns TRUE and the filesize in the 2nd parameter.
                   1948:   Otherwise, in case of a system error, returns FALSE, with a 
                   1949:   filesize of 0L.
                   1950:   ---------------------------------------------------------------------*/
1.106     cvs      1951: ThotBool AM_GetFileSize (char *filename, unsigned long *file_size)
1.104     kahan    1952: {
1.106     cvs      1953:   ThotFileHandle   handle = ThotFile_BADHANDLE;
                   1954:   ThotFileInfo     info;
1.35      cvs      1955: 
1.104     kahan    1956:   *file_size = 0L;
                   1957:   if (!TtaFileExist (filename))
                   1958:     return FALSE;
                   1959: 
                   1960:   handle = TtaFileOpen (filename, ThotFile_READWRITE);
                   1961:   if (handle == ThotFile_BADHANDLE)
                   1962:     /* ThotFile_BADHANDLE */
                   1963:     return FALSE;
                   1964:    if (TtaFileStat (handle, &info) == 0)
                   1965:      /* bad stat */
                   1966:      info.size = 0L;
                   1967:    TtaFileClose (handle);
                   1968:    *file_size = (unsigned long) info.size;
                   1969:    return TRUE;
                   1970: }

Webmaster