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

1.7       cvs         1: /*
                      2:  *
                      3:  *  (c) COPYRIGHT MIT and INRIA, 1996.
                      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
1.9       cvs         7: 
1.10      cvs         8: /*
                      9:  * AHTURLTools.c: contains all the functions for testing, manipulating,
1.25      cvs        10:  * and normalizing URLs. It also contains a local copy of the libWWW
                     11:  * URL parsing functions.
1.10      cvs        12:  *
                     13:  * Authors: J. Kahan, I. Vatton
1.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])
                    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;
                    706:   /* get the root element    */
                    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.106     cvs       729:          if (basename[0] != EOS && basename[length] != URL_SEP && basename[length] != DIR_SEP) 
1.31      cvs       730:            /* verify if the base has the form "protocol://server:port" */
                    731:            {
1.106     cvs       732:              ptr = AmayaParseUrl (basename, "", AMAYA_PARSE_ACCESS |
1.33      cvs       733:                                                 AMAYA_PARSE_HOST |
                    734:                                                 AMAYA_PARSE_PUNCTUATION);
1.106     cvs       735:              if (ptr && !strcmp (ptr, basename))
1.31      cvs       736:                {
1.43      cvs       737:                  /* it has this form, we complete it by adding a URL_STR  */
1.106     cvs       738:                  if (strchr (basename, DIR_SEP))
                    739:                    strcat (basename, DIR_STR);
1.43      cvs       740:                  else
1.106     cvs       741:                    strcat (basename, URL_STR);
1.31      cvs       742:                  length++;
                    743:                }
                    744:              if (ptr)
                    745:                TtaFreeMemory (ptr);
                    746:            }
                    747:        }
1.33      cvs       748:       }
                    749:   
1.31      cvs       750:   /* Remove anything after the last DIR_SEP char. If no such char is found,
                    751:    * then search for the first ":" char, hoping that what's before that is a
                    752:    * protocol. If found, end the string there. If neither char is found,
                    753:    * then discard the whole base element.
                    754:    */
1.106     cvs       755:   length = strlen (basename) - 1;
1.31      cvs       756:   /* search for the last DIR_SEP char */
1.106     cvs       757:   while (length >= 0  && basename[length] != URL_SEP && basename[length] != DIR_SEP)
1.31      cvs       758:     length--;
                    759:   if (length >= 0)
                    760:     /* found the last DIR_SEP char, end the string there */
1.106     cvs       761:     basename[length + 1] = EOS;                   
1.31      cvs       762:   else
                    763:     /* search for the first PATH_STR char */
                    764:     {
1.106     cvs       765:       for (length = 0; basename[length] != ':' && 
                    766:             basename[length] != EOS; length ++);
                    767:       if (basename[length] == ':')
1.31      cvs       768:        /* found, so end the string there */
1.106     cvs       769:        basename[length + 1] = EOS;
1.31      cvs       770:       else
                    771:        /* not found, discard the base */
1.106     cvs       772:        basename[0] = EOS;
1.31      cvs       773:     }
                    774:   return (basename);
                    775: }
                    776: 
                    777: 
1.4       cvs       778: /*----------------------------------------------------------------------
1.40      cvs       779:    GetLocalPath
                    780:    Allocate and return the local document path associated to the url
                    781:   ----------------------------------------------------------------------*/
1.106     cvs       782: char  *GetLocalPath (Document doc, char  *url)
                    783: {
                    784:   char     *ptr;
                    785:   char     *n;
                    786:   char     *documentname;
                    787:   char      url_sep;
1.83      cvs       788:   int       len;
1.67      cvs       789:   ThotBool  noFile;
1.40      cvs       790: 
                    791:   if (url != NULL)
                    792:     {
                    793:       /* check whether the file name exists */
1.106     cvs       794:       len = strlen (url) - 1;
1.71      cvs       795:       if (IsW3Path (url))
1.106     cvs       796:          url_sep = '/';
1.41      cvs       797:       else 
1.106     cvs       798:           url_sep = DIR_SEP;
1.41      cvs       799:       noFile = (url[len] == url_sep);
1.40      cvs       800:       if (noFile)
1.106     cvs       801:          url[len] = EOS;
                    802:       ptr = TtaGetMemory (MAX_LENGTH);
                    803:       documentname = TtaGetMemory (MAX_LENGTH);
1.78      cvs       804:       TtaExtractName (url, ptr, documentname);
1.106     cvs       805:       sprintf (ptr, "%s%s%d%s", TempFileDirectory, DIR_STR, doc, DIR_STR);
1.40      cvs       806:       if (!TtaCheckDirectory (ptr))
                    807:        /* directory did not exist */
1.72      cvs       808:        TtaMakeDirectory (ptr);
1.47      cvs       809: 
                    810:       /* don't include the query string within document name */
1.106     cvs       811:       n = strrchr (documentname, '?');
1.47      cvs       812:       if (n != NULL)
1.106     cvs       813:          *n = EOS;
1.46      cvs       814:       /* don't include ':' within document name */
1.106     cvs       815:       n = strchr (documentname, ':');
1.46      cvs       816:       if (n != NULL)
1.106     cvs       817:          *n = EOS;
1.69      cvs       818:       /* if after all this operations document name
                    819:         is empty, let's use noname.html instead */
1.106     cvs       820:       if (documentname[0] == EOS)
                    821:          strcat (ptr, "noname.html");
1.69      cvs       822:       else
1.106     cvs       823:           strcat (ptr, documentname);
1.40      cvs       824:       TtaFreeMemory (documentname);
                    825:       /* restore the url */
                    826:       if (noFile)
1.41      cvs       827:        url[len] = url_sep;
1.40      cvs       828:       return (ptr);
                    829:     }
                    830:   else
                    831:     return (NULL);
                    832: }
                    833: 
1.73      cvs       834: /*----------------------------------------------------------------------
1.79      cvs       835:    ExtractTarget extract the target name from document nane.        
                    836:   ----------------------------------------------------------------------*/
1.106     cvs       837: void         ExtractTarget (char *aName, char *target)
1.79      cvs       838: {
1.106     cvs       839:    int    lg, i;
                    840:    char  *ptr;
                    841:    char  *oldptr;
1.79      cvs       842: 
                    843:    if (!target || !aName)
                    844:      /* bad target */
                    845:      return;
                    846: 
1.106     cvs       847:    target[0] = EOS;
                    848:    lg = strlen (aName);
1.79      cvs       849:    if (lg)
                    850:      {
                    851:        /* the name is not empty */
                    852:        oldptr = ptr = &aName[0];
                    853:        do
                    854:          {
1.106     cvs       855:             ptr = strrchr (oldptr, '#');
1.79      cvs       856:             if (ptr)
                    857:                oldptr = &ptr[1];
                    858:          }
                    859:        while (ptr);
                    860: 
                    861:        i = (int) (oldptr) - (int) (aName);     /* name length */
                    862:        if (i > 1)
                    863:          {
1.106     cvs       864:             aName[i - 1] = EOS;
1.79      cvs       865:             if (i != lg)
1.106     cvs       866:                strcpy (target, oldptr);
1.79      cvs       867:          }
                    868:      }
                    869: }
                    870: 
                    871: /*----------------------------------------------------------------------
1.90      cvs       872:    RemoveNewLines (text)
                    873:    Removes any '\n' chars that are found in text. 
                    874:    Returns TRUE if it did the operation, FALSE otherwise.
1.73      cvs       875:   ----------------------------------------------------------------------*/
1.106     cvs       876: ThotBool RemoveNewLines (char *text)
                    877: {
                    878:   ThotBool   change = FALSE;
                    879:   char      *src;
                    880:   char      *dest;
1.90      cvs       881: 
                    882:   src = text;
                    883:   dest = text;
                    884:   while (*src)
                    885:     {
                    886:       switch (*src)
                    887:        {
1.106     cvs       888:        case '\n':
1.90      cvs       889:          /* don't copy the newline */
                    890:          change = 1;
                    891:          break;
                    892:        default:
                    893:          *dest = *src;
                    894:          dest++;
                    895:          break;
                    896:        }
                    897:       src++;
                    898:     }
                    899:   /* copy the last EOS char */
                    900:   *dest = *src;
                    901: 
                    902:   return (change);
                    903: }
                    904: 
                    905: /*----------------------------------------------------------------------
                    906:    CleanCopyFileURL
                    907:    Copies a file url from a src string to destination string.
1.97      cvs       908:    convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
                    909:    we want to do).
1.90      cvs       910:   ----------------------------------------------------------------------*/
1.106     cvs       911: static void CleanCopyFileURL (char *dest, char *src,
                    912:                              ConvertionType convertion)
1.90      cvs       913: {
                    914:   while (*src)
1.89      cvs       915:     {
1.90      cvs       916:       switch (*src)
1.89      cvs       917:        {
                    918: #ifdef _WINDOWS
1.106     cvs       919:        case URL_SEP:
1.96      cvs       920:          /* make DIR_SEP transformation */
1.97      cvs       921:          if (convertion & AM_CONV_URL_SEP)
1.106     cvs       922:            *dest = DIR_SEP;
1.96      cvs       923:          else
                    924:            *dest = *src;
1.90      cvs       925:          dest++;
1.96      cvs       926:          src++;
1.90      cvs       927:          break;
1.89      cvs       928: #endif /* _WINDOWS */
1.96      cvs       929: 
1.106     cvs       930:        case '%':
1.97      cvs       931:          if (convertion & AM_CONV_PERCENT)
1.96      cvs       932:            {
1.97      cvs       933:              /* (code adapted from libwww's HTUnEscape function */
1.96      cvs       934:              src++;
1.106     cvs       935:              if (*src != EOS)
1.97      cvs       936:                {
                    937:                  *dest = UnEscapeChar (*src) * 16;
                    938:                  src++;
                    939:                }
1.106     cvs       940:              if (*src != EOS)
1.97      cvs       941:                {
                    942:                  *dest = *dest + UnEscapeChar (*src);
                    943:                  src++;
                    944:                }
                    945:              dest++;
1.96      cvs       946:            }
1.97      cvs       947:          else
1.96      cvs       948:            {
1.97      cvs       949:              *dest = *src;
                    950:              dest++;
1.96      cvs       951:              src++;
                    952:            }
                    953:          break;
                    954: 
1.90      cvs       955:        default:
                    956:          *dest = *src;
1.89      cvs       957:          dest++;
1.96      cvs       958:          src++;
1.90      cvs       959:          break;
1.89      cvs       960:        }
                    961:     }
1.90      cvs       962:   /* copy the EOS char */
                    963:   *dest = *src;
1.73      cvs       964: }
1.40      cvs       965: 
                    966: /*----------------------------------------------------------------------
1.9       cvs       967:    NormalizeURL
                    968:    normalizes orgName according to a base associated with doc, and
                    969:    following the standard URL format rules.
1.53      cvs       970:    if doc is 0 and otherPath not NULL, normalizes orgName according to this
                    971:    other path.
1.9       cvs       972:    The function returns the new complete and normalized URL 
1.12      cvs       973:    or file name path (newName) and the name of the document (docName).        
1.9       cvs       974:    N.B. If the function can't find out what's the docName, it assigns
                    975:    the name "noname.html".
1.4       cvs       976:   ----------------------------------------------------------------------*/
1.106     cvs       977: void NormalizeURL (char *orgName, Document doc, char *newName,
                    978:                   char *docName, char *otherPath)
                    979: {
                    980:    char          *basename;
                    981:    char           tempOrgName[MAX_LENGTH];
                    982:    char          *ptr;
                    983:    char           used_sep;
1.84      cvs       984:    int            length;
                    985:    ThotBool       check;
1.5       cvs       986: 
1.44      cvs       987: #  ifdef _WINDOWS
                    988:    int ndx;
                    989: #  endif /* _WINDOWS */
                    990: 
1.5       cvs       991:    if (!newName || !docName)
                    992:       return;
1.18      cvs       993: 
1.32      cvs       994:    if (doc != 0)
1.53      cvs       995:      basename = GetBaseURL (doc);
                    996:    else if (otherPath != NULL)
1.108     cvs       997:      basename = TtaStrdup (otherPath);
1.32      cvs       998:    else
1.53      cvs       999:      basename = NULL;
1.32      cvs      1000: 
1.18      cvs      1001:    /*
1.31      cvs      1002:     * Clean orgName
                   1003:     * Make sure we have a complete orgName, without any leading or trailing
                   1004:     * white spaces, or trailinbg new lines
                   1005:     */
1.5       cvs      1006:    ptr = orgName;
1.18      cvs      1007:    /* skip leading white space and new line characters */
1.106     cvs      1008:    while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
                   1009:    strncpy (tempOrgName, ptr, MAX_LENGTH -1);
                   1010:    tempOrgName[MAX_LENGTH -1] = EOS;
1.18      cvs      1011:    /*
1.31      cvs      1012:     * Make orgName a complete URL
                   1013:     * If the URL does not include a protocol, then try to calculate
                   1014:     * one using the doc's base element (if it exists),
                   1015:     */
1.106     cvs      1016:    if (tempOrgName[0] == EOS)
1.53      cvs      1017:      {
1.106     cvs      1018:        newName[0] = EOS;
                   1019:        docName[0] = EOS;
1.53      cvs      1020:        TtaFreeMemory (basename);
                   1021:        return;
                   1022:      }
1.49      cvs      1023: 
                   1024:    /* clean trailing white space */
1.106     cvs      1025:    length = strlen (tempOrgName) - 1;
                   1026:    while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
1.53      cvs      1027:      {
1.106     cvs      1028:        tempOrgName[length] = EOS;
1.53      cvs      1029:        length--;
                   1030:      }
1.50      cvs      1031: 
1.55      cvs      1032:    /* remove extra dot (which dot???) */
                   1033:    /* ugly, but faster than a strcmp */
1.106     cvs      1034:    if (tempOrgName[length] == '.'
                   1035:        && (length == 0 || tempOrgName[length-1] != '.'))
                   1036:         tempOrgName[length] = EOS;
1.50      cvs      1037: 
1.94      cvs      1038:    if (IsW3Path (tempOrgName))
1.53      cvs      1039:      {
                   1040:        /* the name is complete, go to the Sixth Step */
1.106     cvs      1041:        strcpy (newName, tempOrgName);
1.53      cvs      1042:        SimplifyUrl (&newName);
                   1043:        /* verify if the URL has the form "protocol://server:port" */
1.106     cvs      1044:        ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                   1045:        if (ptr && !strcmp (ptr, newName)) /* it has this form, we complete it by adding a DIR_STR  */
                   1046:          strcat (newName, URL_STR);
1.49      cvs      1047: 
1.53      cvs      1048:        if (ptr)
1.50      cvs      1049:          TtaFreeMemory (ptr);
1.53      cvs      1050:      }
                   1051:    else if ( basename == NULL)
                   1052:      /* the name is complete, go to the Sixth Step */
1.106     cvs      1053:      strcpy (newName, tempOrgName);
1.53      cvs      1054:    else
                   1055:      {
1.31      cvs      1056:        /* Calculate the absolute URL, using the base or document URL */
1.44      cvs      1057: #      ifdef _WINDOWS
1.53      cvs      1058:        if (!IsW3Path (basename))
                   1059:         {
1.106     cvs      1060:           length = strlen (tempOrgName);
1.53      cvs      1061:           for (ndx = 0; ndx < length; ndx++)
1.106     cvs      1062:             if (tempOrgName [ndx] == '/')
                   1063:               tempOrgName [ndx] = '\\';
1.53      cvs      1064:         }
1.44      cvs      1065: #      endif /* _WINDOWS */
1.25      cvs      1066:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53      cvs      1067:        if (ptr)
                   1068:         {
                   1069:           SimplifyUrl (&ptr);
1.106     cvs      1070:           strcpy (newName, ptr);
1.53      cvs      1071:           TtaFreeMemory (ptr);
                   1072:         }
                   1073:        else
1.106     cvs      1074:         newName[0] = EOS;
1.53      cvs      1075:      }
1.36      cvs      1076: 
                   1077:    TtaFreeMemory (basename);
1.18      cvs      1078:    /*
1.31      cvs      1079:     * Prepare the docname that will refer to this ressource in the
                   1080:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                   1081:     * noname.html as a default ressource name
1.18      cvs      1082:    */
1.106     cvs      1083:    if (newName[0] != EOS)
1.53      cvs      1084:      {
1.106     cvs      1085:        length = strlen (newName) - 1;
                   1086:        if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
1.53      cvs      1087:         {
                   1088:           used_sep = newName[length];
                   1089:           check = TRUE;
                   1090:           while (check)
                   1091:             {
1.50      cvs      1092:                length--;
                   1093:                while (length >= 0 && newName[length] != used_sep)
1.53      cvs      1094:                 length--;
1.106     cvs      1095:                if (!strncmp (&newName[length+1], "..", 2))
1.53      cvs      1096:                 {
1.106     cvs      1097:                   newName[length+1] = EOS;
1.53      cvs      1098:                   /* remove also previous directory */
                   1099:                   length--;
                   1100:                   while (length >= 0 && newName[length] != used_sep)
                   1101:                     length--;
1.106     cvs      1102:                   if (strncmp (&newName[length+1], "//", 2))
1.53      cvs      1103:                     /* don't remove server name */
1.106     cvs      1104:                      newName[length+1] = EOS;
1.53      cvs      1105:                 }
1.106     cvs      1106:               else if (!strncmp (&newName[length+1], ".", 1))
                   1107:                 newName[length+1] = EOS;
1.50      cvs      1108:                else
1.53      cvs      1109:                 check = FALSE;
                   1110:             }
1.106     cvs      1111:           strcpy (docName, "noname.html");            
1.53      cvs      1112:           /* docname was not comprised inside the URL, so let's */
                   1113:           /* assign the default ressource name */
1.106     cvs      1114:           strcpy (docName, "noname.html");
1.53      cvs      1115:         }
                   1116:        else
                   1117:         { /* docname is comprised inside the URL */
1.106     cvs      1118:            while (length >= 0 && newName[length] != URL_SEP && newName[length] != DIR_SEP)
1.53      cvs      1119:             length--;
                   1120:           if (length < 0)
1.106     cvs      1121:              strcpy (docName, newName);
1.53      cvs      1122:           else
1.106     cvs      1123:             strcpy (docName, &newName[length+1]);
1.53      cvs      1124:         }
                   1125:      }
                   1126:    else
1.106     cvs      1127:      docName[0] = EOS;
1.18      cvs      1128: } 
1.3       cvs      1129: 
1.4       cvs      1130: /*----------------------------------------------------------------------
1.9       cvs      1131:   IsSameHost                                                         
1.4       cvs      1132:   ----------------------------------------------------------------------*/
1.106     cvs      1133: ThotBool IsSameHost (const char *url1, const char *url2)
1.3       cvs      1134: {
1.106     cvs      1135:   char          *basename_ptr1, *basename_ptr2;
                   1136:   ThotBool       result;
1.3       cvs      1137: 
1.106     cvs      1138:   basename_ptr1 = AmayaParseUrl (url1, "",
                   1139:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                   1140:   basename_ptr2 = AmayaParseUrl (url2, "",
                   1141:             AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs      1142: 
1.106     cvs      1143:   if (strcmp (basename_ptr1, basename_ptr2))
                   1144:     result = FALSE;
                   1145:   else
                   1146:     result = TRUE;
                   1147:   TtaFreeMemory (basename_ptr1);
                   1148:   TtaFreeMemory (basename_ptr2);
                   1149:   return (result);
1.3       cvs      1150: }
                   1151: 
                   1152: 
1.4       cvs      1153: /*----------------------------------------------------------------------
1.22      cvs      1154:   HasKnownFileSuffix
                   1155:   returns TRUE if path points to a file ending with a suffix.
                   1156:   ----------------------------------------------------------------------*/
1.106     cvs      1157: ThotBool             HasKnownFileSuffix (const char *path)
                   1158: {
                   1159:    char       *root;
                   1160:    char        temppath[MAX_LENGTH];
                   1161:    char        suffix[MAX_LENGTH];
1.22      cvs      1162: 
1.106     cvs      1163:    if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22      cvs      1164:      return (FALSE);
                   1165: 
1.106     cvs      1166:    root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs      1167: 
                   1168:    if (root) 
                   1169:      {
1.106     cvs      1170:        strcpy (temppath, root);
1.25      cvs      1171:        TtaFreeMemory (root);
1.22      cvs      1172:        /* Get the suffix */
                   1173:        ExtractSuffix (temppath, suffix); 
                   1174: 
1.106     cvs      1175:        if( suffix[0] == EOS)
1.22      cvs      1176:         /* no suffix */
                   1177:         return (FALSE);
                   1178: 
                   1179:        /* Normalize the suffix */
                   1180:        ConvertToLowerCase (suffix);
                   1181: 
1.106     cvs      1182:        if (!strcmp (suffix, "gz"))
1.22      cvs      1183:         /* skip the compressed suffix */
                   1184:         {
                   1185:         ExtractSuffix (temppath, suffix);
1.106     cvs      1186:         if(suffix[0] == EOS)
1.22      cvs      1187:           /* no suffix */
                   1188:           return (FALSE);
                   1189:          /* Normalize the suffix */
                   1190:          ConvertToLowerCase (suffix);
                   1191:         }
                   1192: 
1.106     cvs      1193:        if (strcmp (suffix, "gif") &&
                   1194:           strcmp (suffix, "xbm") &&
                   1195:           strcmp (suffix, "xpm") &&
                   1196:           strcmp (suffix, "jpg") &&
                   1197:           strcmp (suffix, "pdf") &&
                   1198:           strcmp (suffix, "png") &&
                   1199:           strcmp (suffix, "tgz") &&
                   1200:           strcmp (suffix, "xpg") &&
                   1201:           strcmp (suffix, "xpd") &&
                   1202:           strcmp (suffix, "ps") &&
                   1203:           strcmp (suffix, "au") &&
                   1204:           strcmp (suffix, "html") &&
                   1205:           strcmp (suffix, "htm") &&
                   1206:           strcmp (suffix, "shtml") &&
                   1207:           strcmp (suffix, "xht") &&
                   1208:           strcmp (suffix, "xhtm") &&
                   1209:           strcmp (suffix, "xhtml") &&
                   1210:           strcmp (suffix, "txt") &&
                   1211:           strcmp (suffix, "css") &&
                   1212:           strcmp (suffix, "eps"))
1.22      cvs      1213:         return (FALSE);
                   1214:        else
                   1215:         return (TRUE);
                   1216:      }
                   1217:    else
                   1218:      return (FALSE);
                   1219: }
                   1220: 
                   1221: 
                   1222: /*----------------------------------------------------------------------
1.24      cvs      1223:   ChopURL
                   1224:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                   1225:   If inputURL is  bigger than that size, outputURL receives
                   1226:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                   1227:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                   1228:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                   1229:   copied into outputURL. 
                   1230:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                   1231:   chars.
                   1232:   ----------------------------------------------------------------------*/
1.106     cvs      1233: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs      1234: {
                   1235:   int len;
1.9       cvs      1236: 
1.106     cvs      1237:   len = strlen (inputURL);
1.24      cvs      1238:   if (len <= MAX_PRINT_URL_LENGTH) 
1.106     cvs      1239:     strcpy (outputURL, inputURL);
1.24      cvs      1240:   else
                   1241:     /* make a truncated urlName on the status window */
                   1242:     {
1.106     cvs      1243:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                   1244:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                   1245:       strcat (outputURL, "...");
                   1246:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24      cvs      1247:     }
1.25      cvs      1248: }
                   1249: 
                   1250: 
                   1251: /*----------------------------------------------------------------------
                   1252:    scan
1.47      cvs      1253:        Scan a filename for its constituents
1.25      cvs      1254:        -----------------------------------
                   1255:   
                   1256:    On entry,
                   1257:        name    points to a document name which may be incomplete.
                   1258:    On exit,
                   1259:         absolute or relative may be nonzero (but not both).
                   1260:        host, fragment and access may be nonzero if they were specified.
                   1261:        Any which are nonzero point to zero terminated strings.
                   1262:   ----------------------------------------------------------------------*/
1.106     cvs      1263: static void scan (char *name, HTURI *parts)
1.25      cvs      1264: {
1.106     cvs      1265:   char *   p;
                   1266:   char *   after_access = name;
1.32      cvs      1267: 
1.43      cvs      1268:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs      1269:   /* Look for fragment identifier */
1.106     cvs      1270:   if ((p = strchr(name, '#')) != NULL)
1.28      cvs      1271:     {
1.106     cvs      1272:       *p++ = '\0';
1.28      cvs      1273:       parts->fragment = p;
1.25      cvs      1274:     }
                   1275:     
1.28      cvs      1276:   for (p=name; *p; p++)
                   1277:     {
1.106     cvs      1278:       if (*p == URL_SEP || *p == DIR_SEP || *p == '#' || *p == '?')
1.28      cvs      1279:        break;
1.106     cvs      1280:       if (*p == ':')
1.28      cvs      1281:        {
                   1282:          *p = 0;
                   1283:          parts->access = after_access; /* Scheme has been specified */
                   1284: 
                   1285:          /* The combination of gcc, the "-O" flag and the HP platform is
                   1286:             unhealthy. The following three lines is a quick & dirty fix, but is
                   1287:             not recommended. Rather, turn off "-O". */
                   1288: 
                   1289:          /*            after_access = p;*/
                   1290:          /*            while (*after_access == 0)*/
                   1291:          /*                after_access++;*/
                   1292:          after_access = p+1;
1.106     cvs      1293:          if (!strcasecmp("URL", parts->access))
1.28      cvs      1294:            /* Ignore IETF's URL: pre-prefix */
                   1295:            parts->access = NULL;
                   1296:          else
1.25      cvs      1297:            break;
                   1298:        }
                   1299:     }
                   1300:     
                   1301:     p = after_access;
1.43      cvs      1302:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs      1303:       {
1.43      cvs      1304:        if (p[1] == URL_SEP)
1.28      cvs      1305:          {
1.25      cvs      1306:            parts->host = p+2;          /* host has been specified      */
1.28      cvs      1307:            *p = 0;                     /* Terminate access             */
                   1308:            /* look for end of host name if any */
1.106     cvs      1309:            p = strchr (parts->host, URL_SEP);
1.28      cvs      1310:            if (p)
                   1311:              {
1.106     cvs      1312:                *p = EOS;                       /* Terminate host */
1.25      cvs      1313:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs      1314:              }
                   1315:          }
                   1316:        else
                   1317:          /* Root found but no host */
                   1318:          parts->absolute = p+1;
                   1319:       }
                   1320:     else
                   1321:       {
1.25      cvs      1322:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs      1323:       }
1.25      cvs      1324: }
                   1325: 
                   1326: 
                   1327: /*----------------------------------------------------------------------
1.28      cvs      1328:   AmayaParseUrl: parse a Name relative to another name
                   1329: 
                   1330:   This returns those parts of a name which are given (and requested)
                   1331:   substituting bits from the related name where necessary.
1.25      cvs      1332:   
1.28      cvs      1333:   On entry,
1.25      cvs      1334:        aName           A filename given
                   1335:         relatedName     A name relative to which aName is to be parsed. Give
                   1336:                         it an empty string if aName is absolute.
                   1337:         wanted          A mask for the bits which are wanted.
                   1338:   
1.28      cvs      1339:   On exit,
1.25      cvs      1340:        returns         A pointer to a malloc'd string which MUST BE FREED
                   1341:   ----------------------------------------------------------------------*/
1.106     cvs      1342: char   *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
                   1343: {
                   1344:   char      *return_value;
                   1345:   char       result[MAX_LENGTH];
                   1346:   char       name[MAX_LENGTH];
                   1347:   char       rel[MAX_LENGTH];
                   1348:   char      *p, *access;
1.29      cvs      1349:   HTURI      given, related;
                   1350:   int        len;
1.106     cvs      1351:   char       used_sep;
                   1352:   char      *used_str;
1.32      cvs      1353: 
1.106     cvs      1354:   if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33      cvs      1355:     {
1.106     cvs      1356:       used_str = DIR_STR;
                   1357:       used_sep = DIR_SEP;
1.33      cvs      1358:     }
1.32      cvs      1359:   else
1.33      cvs      1360:     {
1.106     cvs      1361:       used_str = URL_STR;
                   1362:       used_sep = URL_SEP;
1.33      cvs      1363:     }
1.32      cvs      1364: 
1.29      cvs      1365:   /* Make working copies of input strings to cut up: */
                   1366:   return_value = NULL;
                   1367:   result[0] = 0;               /* Clear string  */
1.106     cvs      1368:   strcpy (name, aName);
1.29      cvs      1369:   if (relatedName != NULL)  
1.106     cvs      1370:     strcpy (rel, relatedName);
1.29      cvs      1371:   else
1.106     cvs      1372:     relatedName[0] = EOS;
1.29      cvs      1373:   
                   1374:   scan (name, &given);
                   1375:   scan (rel,  &related); 
                   1376:   access = given.access ? given.access : related.access;
                   1377:   if (wanted & AMAYA_PARSE_ACCESS)
                   1378:     if (access)
                   1379:       {
1.106     cvs      1380:        strcat (result, access);
1.29      cvs      1381:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1382:                strcat (result, ":");
1.29      cvs      1383:       }
                   1384:   
                   1385:   if (given.access && related.access)
                   1386:     /* If different, inherit nothing. */
1.106     cvs      1387:     if (strcmp (given.access, related.access) != 0)
1.29      cvs      1388:       {
                   1389:        related.host = 0;
                   1390:        related.absolute = 0;
                   1391:        related.relative = 0;
                   1392:        related.fragment = 0;
                   1393:       }
                   1394:   
                   1395:   if (wanted & AMAYA_PARSE_HOST)
                   1396:     if(given.host || related.host)
                   1397:       {
                   1398:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1399:          strcat (result, "//");
                   1400:        strcat (result, given.host ? given.host : related.host);
1.29      cvs      1401:       }
                   1402:   
                   1403:   if (given.host && related.host)
                   1404:     /* If different hosts, inherit no path. */
1.106     cvs      1405:     if (strcmp (given.host, related.host) != 0)
1.29      cvs      1406:       {
                   1407:        related.absolute = 0;
                   1408:        related.relative = 0;
                   1409:        related.fragment = 0;
                   1410:       }
                   1411:   
                   1412:   if (wanted & AMAYA_PARSE_PATH)
                   1413:     {
                   1414:       if (given.absolute)
                   1415:        {
                   1416:          /* All is given */
                   1417:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1418:            strcat (result, used_str);
                   1419:          strcat (result, given.absolute);
1.25      cvs      1420:        }
1.29      cvs      1421:       else if (related.absolute)
                   1422:        {
                   1423:          /* Adopt path not name */
1.106     cvs      1424:          strcat (result, used_str);
                   1425:          strcat (result, related.absolute);
1.29      cvs      1426:          if (given.relative)
                   1427:            {
                   1428:              /* Search part? */
1.106     cvs      1429:              p = strchr (result, '?');
1.29      cvs      1430:              if (!p)
1.106     cvs      1431:                p=result+strlen(result)-1;
1.33      cvs      1432:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1433:              /* Remove filename */
                   1434:              p[1]=0;
                   1435:              /* Add given one */
1.106     cvs      1436:              strcat (result, given.relative);
1.25      cvs      1437:            }
                   1438:        }
1.29      cvs      1439:       else if (given.relative)
                   1440:        /* what we've got */
1.106     cvs      1441:        strcat (result, given.relative);
1.29      cvs      1442:       else if (related.relative)
1.106     cvs      1443:        strcat (result, related.relative);
1.29      cvs      1444:       else
                   1445:        /* No inheritance */
1.106     cvs      1446:        strcat (result, used_str);
1.25      cvs      1447:     }
1.29      cvs      1448:   
                   1449:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1450:     if (given.fragment || related.fragment)
                   1451:       {
                   1452:        if (given.absolute && given.fragment)
                   1453:          {
                   1454:            /*Fixes for relURLs...*/
                   1455:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1456:              strcat (result, "#");
                   1457:            strcat (result, given.fragment); 
1.29      cvs      1458:          }
                   1459:        else if (!(given.absolute) && !(given.fragment))
1.106     cvs      1460:          strcat (result, "");
1.29      cvs      1461:        else
                   1462:          {
                   1463:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106     cvs      1464:              strcat (result, "#");
                   1465:            strcat (result, given.fragment ? given.fragment : related.fragment); 
1.29      cvs      1466:          }
                   1467:       }
1.106     cvs      1468:   len = strlen (result);
                   1469:   if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1470:     strcpy (return_value, result);
1.29      cvs      1471:   return (return_value);               /* exactly the right length */
1.25      cvs      1472: }
                   1473: 
                   1474: /*----------------------------------------------------------------------
                   1475:      HTCanon
                   1476:        Canonicalizes the URL in the following manner starting from the host
                   1477:        pointer:
                   1478:   
                   1479:        1) The host name is converted to lowercase
                   1480:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1481:   
                   1482:        Return: OK      The position of the current path part of the URL
                   1483:                        which might be the old one or a new one.
                   1484:   
                   1485:   ----------------------------------------------------------------------*/
1.106     cvs      1486: static char   *HTCanon (char **filename, char *host)
                   1487: {
                   1488:     char   *newname = NULL;
                   1489:     char    used_sep;
                   1490:     char   *path;
                   1491:     char   *strptr;
                   1492:     char   *port;
                   1493:     char   *access = host-3;
                   1494:   
                   1495:      if (*filename && strchr (*filename, URL_SEP))
                   1496:         used_sep = URL_SEP;
1.33      cvs      1497:      else
1.106     cvs      1498:         used_sep = DIR_SEP;
1.32      cvs      1499:   
1.82      cvs      1500:     while (access > *filename && *(access - 1) != used_sep)       /* Find access method */
1.25      cvs      1501:        access--;
1.106     cvs      1502:     if ((path = strchr (host, used_sep)) == NULL)                      /* Find path */
                   1503:        path = host + strlen (host);
                   1504:     if ((strptr = strchr (host, '@')) != NULL && strptr < path)           /* UserId */
1.82      cvs      1505:        host = strptr;
1.106     cvs      1506:     if ((port = strchr (host, ':')) != NULL && port > path)      /* Port number */
1.82      cvs      1507:        port = NULL;
1.25      cvs      1508: 
                   1509:     strptr = host;                                 /* Convert to lower-case */
1.82      cvs      1510:     while (strptr < path)
1.33      cvs      1511:       {
1.84      cvs      1512:          *strptr = utolower (*strptr);
1.82      cvs      1513:          strptr++;
1.33      cvs      1514:       }
1.25      cvs      1515:     
                   1516:     /* Does the URL contain a full domain name? This also works for a
                   1517:        numerical host name. The domain name is already made lower-case
                   1518:        and without a trailing dot. */
                   1519:     {
1.106     cvs      1520:       char  *dot = port ? port : path;
                   1521:       if (dot > *filename && *--dot == '.')
1.33      cvs      1522:        {
1.106     cvs      1523:          char  *orig = dot;
                   1524:          char  *dest = dot + 1;
1.82      cvs      1525:          while ((*orig++ = *dest++));
                   1526:             if (port) port--;
1.33      cvs      1527:          path--;
1.25      cvs      1528:        }
                   1529:     }
                   1530:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1531:     if (port)
                   1532:       {
1.82      cvs      1533:        if (!*(port+1) || *(port+1) == used_sep)
1.33      cvs      1534:          {
                   1535:            if (!newname)
                   1536:              {
1.106     cvs      1537:                char  *orig = port; 
                   1538:                char  *dest = port + 1;
1.82      cvs      1539:                while ((*orig++ = *dest++));
1.33      cvs      1540:              }
                   1541:          }
1.106     cvs      1542:        else if ((!strncmp (access, "http", 4)   &&
                   1543:              (*(port + 1) == '8'                    && 
                   1544:              *(port+2) == '0'                       && 
1.82      cvs      1545:              (*(port+3) == used_sep || !*(port + 3))))       ||
1.106     cvs      1546:              (!strncmp (access, "gopher", 6) &&
                   1547:              (*(port+1) == '7'                      && 
                   1548:              *(port+2) == '0'                       && 
1.82      cvs      1549:              (*(port+3) == used_sep || !*(port+3))))         ||
1.106     cvs      1550:              (!strncmp (access, "ftp", 3)    &&
                   1551:              (*(port+1) == '2'                      && 
                   1552:              *(port + 2) == '1'                     && 
1.82      cvs      1553:              (*(port+3) == used_sep || !*(port+3))))) {
1.33      cvs      1554:          if (!newname)
                   1555:            {
1.106     cvs      1556:              char  *orig = port; 
                   1557:              char  *dest = port + 3;
1.33      cvs      1558:              while((*orig++ = *dest++));
                   1559:              /* Update path position, Henry Minsky */
                   1560:              path -= 3;
1.25      cvs      1561:            }
1.33      cvs      1562:        }
                   1563:        else if (newname)
1.106     cvs      1564:          strncat (newname, port, (int) (path - port));
1.33      cvs      1565:       }
1.25      cvs      1566: 
1.33      cvs      1567:     if (newname)
                   1568:       {
1.106     cvs      1569:        char  *newpath = newname + strlen (newname);
                   1570:        strcat (newname, path);
1.25      cvs      1571:        path = newpath;
1.28      cvs      1572:        /* Free old copy */
                   1573:        TtaFreeMemory(*filename);
1.25      cvs      1574:        *filename = newname;
1.33      cvs      1575:       }
1.25      cvs      1576:     return path;
                   1577: }
                   1578: 
                   1579: 
                   1580: /*----------------------------------------------------------------------
1.29      cvs      1581:   SimplifyUrl: simplify a URI
1.32      cvs      1582:   A URI is allowed to contain the sequence xxx/../ which may be
                   1583:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1584:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1585:   
1.28      cvs      1586:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1587:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1588:   
1.28      cvs      1589:   but we should NOT change
                   1590:                 http://fred.xxx.edu/../..
1.25      cvs      1591:   
                   1592:        or      ../../albert.html
                   1593:   
1.28      cvs      1594:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1595:   
                   1596:                /fred/..                becomes /fred/..
                   1597:                /fred/././..            becomes /fred/..
                   1598:                /fred/.././junk/.././   becomes /fred/..
                   1599:   
1.28      cvs      1600:   If more than one set of `://' is found (several proxies in cascade) then
                   1601:   only the part after the last `://' is simplified.
1.25      cvs      1602:   
1.28      cvs      1603:   Returns: A string which might be the old one or a new one.
1.25      cvs      1604:   ----------------------------------------------------------------------*/
1.106     cvs      1605: void         SimplifyUrl (char **url)
                   1606: {
                   1607:   char   *path;
                   1608:   char   *access;
                   1609:   char   *newptr; 
                   1610:   char   *p;
                   1611:   char   *orig, *dest, *end;
1.28      cvs      1612: 
1.106     cvs      1613:   char      used_sep;
1.77      cvs      1614:   ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
                   1615:                             something/../ simplification in relative URLs when they start with a ../ */
1.32      cvs      1616: 
                   1617: 
1.28      cvs      1618:   if (!url || !*url)
                   1619:     return;
                   1620: 
1.106     cvs      1621:   if (strchr (*url, URL_SEP))
                   1622:       used_sep = URL_SEP;
1.32      cvs      1623:   else
1.106     cvs      1624:       used_sep = DIR_SEP;
1.32      cvs      1625: 
1.77      cvs      1626:   /* should we simplify double dot? */
                   1627:   path = *url;
1.106     cvs      1628:   if (*path == '.' && *(path + 1) == '.')
1.77      cvs      1629:     ddot_simplify = FALSE;
                   1630:   else
                   1631:     ddot_simplify = TRUE;
                   1632: 
1.28      cvs      1633:   /* Find any scheme name */
1.106     cvs      1634:   if ((path = strstr (*url, "://")) != NULL)
1.33      cvs      1635:     {
                   1636:       /* Find host name */
1.28      cvs      1637:       access = *url;
1.84      cvs      1638:       while (access < path && (*access = utolower (*access)))
1.82      cvs      1639:             access++;
1.28      cvs      1640:       path += 3;
1.106     cvs      1641:       while ((newptr = strstr (path, "://")) != NULL)
1.82      cvs      1642:             /* For proxies */
1.106     cvs      1643:             path = newptr + 3;
1.82      cvs      1644:      /* We have a host name */
1.84      cvs      1645:       path = HTCanon (url, path);
1.25      cvs      1646:     }
1.106     cvs      1647:   else if ((path = strstr (*url, ":/")) != NULL)
1.28      cvs      1648:     path += 2;
                   1649:   else
                   1650:     path = *url;
1.25      cvs      1651: 
1.84      cvs      1652:   if (*path == used_sep && *(path+1) == used_sep)
1.28      cvs      1653:     /* Some URLs start //<foo> */
                   1654:     path += 1;
1.94      cvs      1655:   else if (IsFilePath (path))
                   1656:     {
                   1657:       /* doesn't need to do anything more */
                   1658:       return;
                   1659:     }
1.106     cvs      1660:   else if (!strncmp (path, "news:", 5))
1.28      cvs      1661:     {
1.106     cvs      1662:       newptr = strchr (path+5, '@');
1.28      cvs      1663:       if (!newptr)
                   1664:        newptr = path + 5;
                   1665:       while (*newptr)
                   1666:        {
                   1667:          /* Make group or host lower case */
1.84      cvs      1668:          *newptr = utolower (*newptr);
1.28      cvs      1669:          newptr++;
1.25      cvs      1670:        }
1.28      cvs      1671:       /* Doesn't need to do any more */
                   1672:       return;
1.25      cvs      1673:     }
1.28      cvs      1674: 
                   1675:   if ((p = path))
                   1676:     {
1.106     cvs      1677:       if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
                   1678:            (end = strchr (path, '#'))))
                   1679:        end = path + strlen (path);
1.28      cvs      1680:       
                   1681:       /* Parse string second time to simplify */
                   1682:       p = path;
                   1683:       while (p < end)
                   1684:        {
1.77      cvs      1685:          /* if we're pointing to a char, it's safe to reactivate the ../ convertion */
1.106     cvs      1686:          if (!ddot_simplify && *p != '.' && *p != used_sep)
1.77      cvs      1687:            ddot_simplify = TRUE;
                   1688: 
1.33      cvs      1689:          if (*p==used_sep)
1.28      cvs      1690:            {
1.106     cvs      1691:              if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      1692:                {
                   1693:                  orig = p + 1;
1.84      cvs      1694:                  dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52      cvs      1695:                  while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28      cvs      1696:                  end = orig - 1;
                   1697:                }
1.106     cvs      1698:              else if (ddot_simplify && *(p+1) == '.' && *(p+2) == '.' 
1.77      cvs      1699:                       && (*(p+3) == used_sep || !*(p+3)))
1.28      cvs      1700:                {
                   1701:                  newptr = p;
1.52      cvs      1702:                  while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
                   1703:                  if (*newptr == used_sep)
                   1704:                    orig = newptr + 1;
1.28      cvs      1705:                  else
1.52      cvs      1706:                    orig = newptr;
                   1707: 
                   1708:                  dest = (*(p+3) != used_sep) ? p+3 : p+4;
                   1709:                  while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   1710:                  end = orig-1;
                   1711:                  /* Start again with prev slash */
                   1712:                  p = newptr;
1.28      cvs      1713:                }
1.33      cvs      1714:              else if (*(p+1) == used_sep)
1.28      cvs      1715:                {
1.33      cvs      1716:                  while (*(p+1) == used_sep)
1.28      cvs      1717:                    {
                   1718:                      orig = p;
                   1719:                      dest = p + 1;
                   1720:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   1721:                      end = orig-1;
                   1722:                    }
                   1723:                }
                   1724:              else
1.25      cvs      1725:                p++;
1.28      cvs      1726:            }
                   1727:          else
                   1728:            p++;
1.25      cvs      1729:        }
                   1730:     }
1.51      cvs      1731: 
                   1732:     /*
                   1733:     **  Check for host/../.. kind of things
                   1734:     */
1.106     cvs      1735:     if (*path == used_sep && *(path+1) == '.' && *(path+2) == '.' 
1.77      cvs      1736:        && (!*(path+3) || *(path+3) == used_sep))
1.106     cvs      1737:        *(path+1) = EOS;
1.51      cvs      1738: 
1.28      cvs      1739:   return;
                   1740: }
                   1741: 
                   1742: 
                   1743: /*----------------------------------------------------------------------
1.96      cvs      1744:    NormalizeFile normalizes local names.                             
1.28      cvs      1745:    Return TRUE if target and src differ.                           
                   1746:   ----------------------------------------------------------------------*/
1.106     cvs      1747: ThotBool NormalizeFile (char *src, char *target, ConvertionType convertion)
1.28      cvs      1748: {
1.93      cvs      1749: #  ifndef _WINDOWS
1.106     cvs      1750:    char             *s;
1.93      cvs      1751:    int               i;
                   1752: #  endif /* !_WINDOWS */
1.82      cvs      1753:    ThotBool          change;
1.90      cvs      1754:    int               start_index; /* the first char that we'll copy */
1.28      cvs      1755: 
1.54      cvs      1756:    change = FALSE;
1.90      cvs      1757:    start_index = 0;
                   1758: 
1.106     cvs      1759:    if (!src || src[0] == EOS)
1.96      cvs      1760:      {
1.106     cvs      1761:        target[0] = EOS;
1.96      cvs      1762:        return FALSE;
                   1763:      }
1.90      cvs      1764: 
                   1765:    /* @@ do I need file: or file:/ here? */
1.106     cvs      1766:    if (strncmp (src, "file:", 5) == 0)
1.28      cvs      1767:      {
1.90      cvs      1768:        /* remove the prefix file: */
                   1769:        start_index += 5;
                   1770:    
                   1771:        /* remove the localhost prefix */
1.106     cvs      1772:        if (strncmp (&src[start_index], "//localhost/", 12) == 0)
1.94      cvs      1773:           start_index += 11;
                   1774:        
                   1775:        /* remove the first two slashes in / / /path */
                   1776:        while (src[start_index] &&
1.106     cvs      1777:              src[start_index] == '/' 
                   1778:              && src[start_index + 1] == '/')
1.94      cvs      1779:         start_index++;
                   1780: 
                   1781: #ifdef _WINDOWS
                   1782:        /* remove any extra slash before the drive name */
1.106     cvs      1783:        if (src[start_index] == '/'
                   1784:           &&src[start_index+2] == ':')
1.94      cvs      1785:         start_index++;
                   1786: #endif /* _WINDOWS */
1.90      cvs      1787: 
1.106     cvs      1788:        if (src[start_index] == EOS)
1.90      cvs      1789:        /* if there's nothing afterwards, add a DIR_STR */
1.106     cvs      1790:         strcpy (target, DIR_STR);
1.90      cvs      1791:        else
1.97      cvs      1792:         /* as we're inside a file: URL, we'll apply all the convertions
                   1793:            we know */
                   1794:         CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96      cvs      1795: 
                   1796:        change = TRUE;
                   1797:      }
1.97      cvs      1798:    else if (convertion != AM_CONV_NONE)
1.96      cvs      1799:      {
                   1800:        /* we are following a "local" relative link, we do all the
                   1801:          convertions except for the HOME_DIR ~ one */
1.97      cvs      1802:        CleanCopyFileURL (target, src, convertion);
1.28      cvs      1803:      }
1.90      cvs      1804: #ifndef _WINDOWS
1.106     cvs      1805:    else if (src[0] == '~')
1.53      cvs      1806:      {
1.96      cvs      1807:        /* it must be a URL typed in a text input field */
                   1808:        /* do the HOME_DIR ~ substitution */
1.82      cvs      1809:        s = TtaGetEnvString ("HOME");
1.106     cvs      1810:        strcpy (target, s);
1.90      cvs      1811: #if 0
1.96      cvs      1812:        /* JK: invalidated this part of the code as it's simpler
                   1813:           to add the DIR_SEP whenever we have something to add
                   1814:           to the path rather than adding it systematically */
1.106     cvs      1815:        if (src[1] != DIR_SEP)
                   1816:          strcat (target, DIR_STR);
1.90      cvs      1817: #endif
1.106     cvs      1818:        i = strlen (target);
                   1819:        strcpy (&target[i], &src[1]);
1.54      cvs      1820:        change = TRUE;
1.53      cvs      1821:      }
1.90      cvs      1822: #endif /* _WINDOWS */
1.28      cvs      1823:    else
1.96      cvs      1824:    /* leave it as it is */
1.106     cvs      1825:      strcpy (target, src);
1.96      cvs      1826:    
1.28      cvs      1827:    /* remove /../ and /./ */
1.29      cvs      1828:    SimplifyUrl (&target);
1.54      cvs      1829:    if (!change)
1.106     cvs      1830:      change = strcmp (src, target);
1.28      cvs      1831:    return (change);
1.25      cvs      1832: }
                   1833: 
1.28      cvs      1834: 
1.25      cvs      1835: /*----------------------------------------------------------------------
1.31      cvs      1836:   MakeRelativeURL: make relative name
1.25      cvs      1837:   
1.28      cvs      1838:   This function creates and returns a string which gives an expression of
                   1839:   one address as related to another. Where there is no relation, an absolute
                   1840:   address is retured.
1.25      cvs      1841:   
1.28      cvs      1842:   On entry,
1.25      cvs      1843:        Both names must be absolute, fully qualified names of nodes
                   1844:        (no fragment bits)
                   1845:   
1.28      cvs      1846:   On exit,
1.25      cvs      1847:        The return result points to a newly allocated name which, if
                   1848:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   1849:        The caller is responsible for freeing the resulting name later.
                   1850:   ----------------------------------------------------------------------*/
1.106     cvs      1851: char      *MakeRelativeURL (char *aName, char *relatedName)
                   1852: {
                   1853:   char  *return_value;
                   1854:   char   result[MAX_LENGTH];
                   1855:   char  *p;
                   1856:   char  *q;
                   1857:   char  *after_access;
                   1858:   char  *last_slash = NULL;
                   1859:   int    slashes, levels, len;
1.29      cvs      1860: 
1.44      cvs      1861: # ifdef _WINDOWS
                   1862:   int ndx;
                   1863: # endif /* _WINDOWS */
                   1864: 
1.29      cvs      1865:   if (aName == NULL || relatedName == NULL)
                   1866:     return (NULL);
                   1867: 
                   1868:   slashes = 0;
                   1869:   after_access = NULL;
                   1870:   p = aName;
                   1871:   q = relatedName;
                   1872:   for (; *p && (*p == *q); p++, q++)
1.27      cvs      1873:     {
                   1874:       /* Find extent of match */
1.106     cvs      1875:       if (*p == ':')
1.29      cvs      1876:        after_access = p + 1;
1.28      cvs      1877:       if (*p == DIR_SEP)
1.27      cvs      1878:        {
1.29      cvs      1879:          /* memorize the last slash position and count them */
1.27      cvs      1880:          last_slash = p;
                   1881:          slashes++;
1.25      cvs      1882:        }
                   1883:     }
                   1884:     
1.31      cvs      1885:   /* q, p point to the first non-matching character or zero */
1.106     cvs      1886:   if (*q == EOS)
1.31      cvs      1887:     {
                   1888:       /* New name is a subset of the related name */
                   1889:       /* exactly the right length */
1.106     cvs      1890:       len = strlen (p);
                   1891:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1892:        strcpy (return_value, p);
1.31      cvs      1893:     }
                   1894:   else if ((slashes < 2 && after_access == NULL)
                   1895:       || (slashes < 3 && after_access != NULL))
                   1896:     {
                   1897:       /* Two names whitout common path */
                   1898:       /* exactly the right length */
1.106     cvs      1899:       len = strlen (aName);
                   1900:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1901:        strcpy (return_value, aName);
1.31      cvs      1902:     }
                   1903:   else
                   1904:     {
                   1905:       /* Some path in common */
1.106     cvs      1906:       if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
1.31      cvs      1907:        /* just the same server */
1.106     cvs      1908:        strcpy (result, last_slash);
1.31      cvs      1909:       else
                   1910:        {
                   1911:          levels= 0; 
1.106     cvs      1912:          for (; *q && *q != '#' && *q != ';' && *q != '?'; q++)
1.31      cvs      1913:            if (*q == DIR_SEP)
                   1914:              levels++;
                   1915:          
1.106     cvs      1916:          result[0] = EOS;
1.31      cvs      1917:          for (;levels; levels--)
1.106     cvs      1918:            strcat (result, "../");
                   1919:          strcat (result, last_slash+1);
1.31      cvs      1920:        } 
1.52      cvs      1921: 
                   1922:       if (!*result)
1.106     cvs      1923:        strcat (result, "./");
1.52      cvs      1924: 
1.31      cvs      1925:       /* exactly the right length */
1.106     cvs      1926:       len = strlen (result);
                   1927:       if ((return_value = TtaGetMemory (len + 1)) != NULL)
                   1928:        strcpy (return_value, result);
1.52      cvs      1929: 
1.25      cvs      1930:     }
1.44      cvs      1931: # ifdef _WINDOWS
1.106     cvs      1932:   len = strlen (return_value);
1.44      cvs      1933:   for (ndx = 0; ndx < len; ndx ++)
1.106     cvs      1934:          if (return_value[ndx] == '\\')
                   1935:             return_value[ndx] = '/' ;
1.44      cvs      1936: # endif /* _WINDOWS */
1.29      cvs      1937:   return (return_value);
1.24      cvs      1938: }
1.35      cvs      1939: 
1.104     kahan    1940: /*----------------------------------------------------------------------
                   1941:   AM_GetFileSize
                   1942:   Returns TRUE and the filesize in the 2nd parameter.
                   1943:   Otherwise, in case of a system error, returns FALSE, with a 
                   1944:   filesize of 0L.
                   1945:   ---------------------------------------------------------------------*/
1.106     cvs      1946: ThotBool AM_GetFileSize (char *filename, unsigned long *file_size)
1.104     kahan    1947: {
1.106     cvs      1948:   ThotFileHandle   handle = ThotFile_BADHANDLE;
                   1949:   ThotFileInfo     info;
1.35      cvs      1950: 
1.104     kahan    1951:   *file_size = 0L;
                   1952:   if (!TtaFileExist (filename))
                   1953:     return FALSE;
                   1954: 
                   1955:   handle = TtaFileOpen (filename, ThotFile_READWRITE);
                   1956:   if (handle == ThotFile_BADHANDLE)
                   1957:     /* ThotFile_BADHANDLE */
                   1958:     return FALSE;
                   1959:    if (TtaFileStat (handle, &info) == 0)
                   1960:      /* bad stat */
                   1961:      info.size = 0L;
                   1962:    TtaFileClose (handle);
                   1963:    *file_size = (unsigned long) info.size;
                   1964:    return TRUE;
                   1965: }

Webmaster