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

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

Webmaster