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

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

Webmaster