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

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

Webmaster