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

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

Webmaster