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

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

Webmaster