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

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

Webmaster