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

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

Webmaster