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

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.84    ! cvs       325: ThotBool             IsHTMLName (const CHAR_T* path)
1.3       cvs       326: #else  /* __STDC__ */
1.67      cvs       327: ThotBool             IsHTMLName (path)
1.84    ! cvs       328: const CHAR_T*        path;
1.3       cvs       329: #endif /* __STDC__ */
                    330: {
1.84    ! cvs       331:    CHAR_T            temppath[MAX_LENGTH];
        !           332:    CHAR_T            suffix[MAX_LENGTH];
        !           333:    CHAR_T            nsuffix[MAX_LENGTH];
        !           334:    int               i; 
1.5       cvs       335: 
                    336:    if (!path)
1.37      cvs       337:       return (FALSE);
1.5       cvs       338: 
1.84    ! cvs       339:    ustrcpy (temppath, path);
1.5       cvs       340:    ExtractSuffix (temppath, suffix);
                    341: 
                    342:    /* Normalize the suffix */
                    343:    i = 0;
1.84    ! cvs       344:    while (suffix[i] != WC_EOS && i < MAX_LENGTH -1)
1.13      cvs       345:      {
1.84    ! cvs       346:        nsuffix[i] = utolower (suffix[i]);
1.13      cvs       347:        i++;
                    348:      }
1.84    ! cvs       349:    nsuffix[i] = WC_EOS;
        !           350:    if (!ustrcmp (nsuffix, TEXT("html")) ||
        !           351:        !ustrcmp (nsuffix, TEXT("htm")) ||
        !           352:        !ustrcmp (nsuffix, TEXT("shtml")) ||
        !           353:        !ustrcmp (nsuffix, TEXT("jsp")) ||
        !           354:        !ustrcmp (nsuffix, TEXT("xht")) ||
        !           355:        !ustrcmp (nsuffix, TEXT("xhtm")) ||
        !           356:        !ustrcmp (nsuffix, TEXT("xhtml")))
1.39      cvs       357:      return (TRUE);
1.84    ! 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.84    ! cvs       364:        while (suffix[i] != WC_EOS && i < MAX_LENGTH -1)
1.13      cvs       365:         {
1.84    ! cvs       366:           nsuffix[i] = utolower (suffix[i]);
1.13      cvs       367:           i++;
                    368:         }
1.84    ! cvs       369:        nsuffix[i] = WC_EOS;
        !           370:        if (!ustrcmp (nsuffix, TEXT("html")) ||
        !           371:            !ustrcmp (nsuffix, TEXT("htm")) ||
        !           372:            !ustrcmp (nsuffix, TEXT("shtml")) ||
        !           373:            !ustrcmp (nsuffix, TEXT("jsp")) ||
        !           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.84    ! cvs       635: ThotBool             IsW3Path (const CHAR_T* path)
1.3       cvs       636: #else  /* __STDC__ */
1.67      cvs       637: ThotBool             IsW3Path (path)
1.84    ! cvs       638: const CHAR_T*        path;
1.3       cvs       639: #endif /* __STDC__ */
                    640: {
1.84    ! cvs       641:   if (ustrncmp (path, TEXT("http:"), 5)   && 
        !           642:       ustrncmp (path, TEXT("ftp:"), 4)    &&
        !           643:       ustrncmp (path, TEXT("telnet:"), 7) && 
        !           644:       ustrncmp (path, TEXT("wais:"), 5)   &&
        !           645:       ustrncmp (path, TEXT("news:"), 5)   && 
        !           646:       ustrncmp (path, TEXT("gopher:"), 7) &&
        !           647:       ustrncmp (path, TEXT("mailto:"), 7) && 
        !           648:       ustrncmp (path, TEXT("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.84    ! cvs       685: CHAR_T*             GetBaseURL (Document doc)
1.31      cvs       686: #else  /* __STDC__ */
1.84    ! cvs       687: CHAR_T*             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.84    ! cvs       695:   CHAR_T              *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.84    ! cvs       703:   basename[MAX_LENGTH-1] = WC_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.84    ! cvs       728:          if (basename[0] != WC_EOS && basename[length] != WC_URL_SEP && basename[length] != WC_DIR_SEP) 
1.31      cvs       729:            /* verify if the base has the form "protocol://server:port" */
                    730:            {
1.84    ! cvs       731:              ptr = AmayaParseUrl (basename, TEXT(""), 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.84    ! cvs       737:                  if (ustrchr (basename, WC_DIR_SEP))
        !           738:                    ustrcat (basename, WC_DIR_STR);
1.43      cvs       739:                  else
1.84    ! cvs       740:                    ustrcat (basename, WC_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.84    ! cvs       756:   while (length >= 0  && basename[length] != WC_URL_SEP && basename[length] != WC_DIR_SEP)
1.31      cvs       757:     length--;
                    758:   if (length >= 0)
                    759:     /* found the last DIR_SEP char, end the string there */
1.84    ! cvs       760:     basename[length + 1] = WC_EOS;                
1.31      cvs       761:   else
                    762:     /* search for the first PATH_STR char */
                    763:     {
1.67      cvs       764:       for (length = 0; basename[length] != TEXT(':') && 
1.84    ! cvs       765:             basename[length] != WC_EOS; length ++);
1.67      cvs       766:       if (basename[length] == TEXT(':'))
1.31      cvs       767:        /* found, so end the string there */
1.84    ! cvs       768:        basename[length + 1] = WC_EOS;
1.31      cvs       769:       else
                    770:        /* not found, discard the base */
1.84    ! cvs       771:        basename[0] = WC_EOS;
1.31      cvs       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.84    ! cvs       782: CHAR_T*    GetLocalPath (Document doc, CHAR_T* url)
1.40      cvs       783: #else  /* __STDC__ */
1.84    ! cvs       784: CHAR_T*    GetLocalPath (doc, url)
1.40      cvs       785: Document   doc;
1.84    ! cvs       786: CHAR_T*    url;
1.40      cvs       787: #endif /* __STDC__ */
                    788: {
1.84    ! cvs       789:   CHAR_T*   ptr;
        !           790:   CHAR_T*   n;
        !           791:   CHAR_T*   documentname;
        !           792:   CHAR_T    url_sep;
1.83      cvs       793:   int       len;
1.67      cvs       794:   ThotBool  noFile;
1.40      cvs       795: 
                    796:   if (url != NULL)
                    797:     {
                    798:       /* check whether the file name exists */
1.84    ! cvs       799:       len = ustrlen (url) - 1;
1.71      cvs       800:       if (IsW3Path (url))
1.84    ! cvs       801:          url_sep = TEXT('/');
1.41      cvs       802:       else 
1.84    ! cvs       803:           url_sep = WC_DIR_SEP;
1.41      cvs       804:       noFile = (url[len] == url_sep);
1.40      cvs       805:       if (noFile)
1.84    ! cvs       806:          url[len] = WC_EOS;
        !           807:       ptr = TtaAllocString (MAX_LENGTH);
        !           808:       documentname = TtaAllocString (MAX_LENGTH);
1.78      cvs       809:       TtaExtractName (url, ptr, documentname);
1.84    ! cvs       810:       usprintf (ptr, TEXT("%s%s%d%s"), TempFileDirectory, WC_DIR_STR, doc, WC_DIR_STR);
1.40      cvs       811:       if (!TtaCheckDirectory (ptr))
                    812:        /* directory did not exist */
1.72      cvs       813:        TtaMakeDirectory (ptr);
1.47      cvs       814: 
                    815:       /* don't include the query string within document name */
1.84    ! cvs       816:       n = ustrrchr (documentname, TEXT('?'));
1.47      cvs       817:       if (n != NULL)
1.84    ! cvs       818:          *n = WC_EOS;
1.46      cvs       819:       /* don't include ':' within document name */
1.84    ! cvs       820:       n = ustrchr (documentname, TEXT(':'));
1.46      cvs       821:       if (n != NULL)
1.84    ! cvs       822:          *n = WC_EOS;
1.69      cvs       823:       /* if after all this operations document name
                    824:         is empty, let's use noname.html instead */
1.84    ! cvs       825:       if (documentname[0] == WC_EOS)
        !           826:          ustrcat (ptr, TEXT("noname.html"));
1.69      cvs       827:       else
1.84    ! cvs       828:           ustrcat (ptr, documentname);
1.40      cvs       829:       TtaFreeMemory (documentname);
                    830:       /* restore the url */
                    831:       if (noFile)
1.41      cvs       832:        url[len] = url_sep;
1.40      cvs       833:       return (ptr);
                    834:     }
                    835:   else
                    836:     return (NULL);
                    837: }
                    838: 
1.73      cvs       839: /*----------------------------------------------------------------------
1.79      cvs       840:    ExtractTarget extract the target name from document nane.        
                    841:   ----------------------------------------------------------------------*/
                    842: #ifdef __STDC__
1.84    ! cvs       843: void         ExtractTarget (CHAR_T* aName, CHAR_T* target)
1.79      cvs       844: #else
                    845: void         ExtractTarget (aName, target)
1.84    ! cvs       846: CHAR_T*      aName;
        !           847: CHAR_T*      target;
1.79      cvs       848: #endif
                    849: {
1.82      cvs       850:    int       lg, i;
1.84    ! cvs       851:    CHAR_T*   ptr;
        !           852:    CHAR_T*   oldptr;
1.79      cvs       853: 
                    854:    if (!target || !aName)
                    855:      /* bad target */
                    856:      return;
                    857: 
1.84    ! cvs       858:    target[0] = WC_EOS;
        !           859:    lg = ustrlen (aName);
1.79      cvs       860:    if (lg)
                    861:      {
                    862:        /* the name is not empty */
                    863:        oldptr = ptr = &aName[0];
                    864:        do
                    865:          {
1.84    ! cvs       866:             ptr = ustrrchr (oldptr, TEXT('#'));
1.79      cvs       867:             if (ptr)
                    868:                oldptr = &ptr[1];
                    869:          }
                    870:        while (ptr);
                    871: 
                    872:        i = (int) (oldptr) - (int) (aName);     /* name length */
                    873:        if (i > 1)
                    874:          {
1.84    ! cvs       875:             aName[i - 1] = WC_EOS;
1.79      cvs       876:             if (i != lg)
1.84    ! cvs       877:                ustrcpy (target, oldptr);
1.79      cvs       878:          }
                    879:      }
                    880: }
                    881: 
                    882: /*----------------------------------------------------------------------
1.73      cvs       883:    ConvertFileURL
                    884:    If the URL starts with file: prefix, it removes the protocol so that we
                    885:    can use it as a local filename
                    886:   ----------------------------------------------------------------------*/
                    887: #ifdef __STDC__
1.84    ! cvs       888: void ConvertFileURL (CHAR_T* url)
1.73      cvs       889: #else
                    890: void ConvertFileURL (url)
1.84    ! cvs       891: CHAR_T* url
1.73      cvs       892: #endif /* __STDC__ */
                    893: {
1.84    ! cvs       894:   if (!ustrncasecmp (url, TEXT("file:"), 5))
        !           895:      ustrcpy (url, url + 5);
1.73      cvs       896: }
1.40      cvs       897: 
                    898: /*----------------------------------------------------------------------
1.9       cvs       899:    NormalizeURL
                    900:    normalizes orgName according to a base associated with doc, and
                    901:    following the standard URL format rules.
1.53      cvs       902:    if doc is 0 and otherPath not NULL, normalizes orgName according to this
                    903:    other path.
1.9       cvs       904:    The function returns the new complete and normalized URL 
1.12      cvs       905:    or file name path (newName) and the name of the document (docName).        
1.9       cvs       906:    N.B. If the function can't find out what's the docName, it assigns
                    907:    the name "noname.html".
1.4       cvs       908:   ----------------------------------------------------------------------*/
1.3       cvs       909: #ifdef __STDC__
1.84    ! cvs       910: void                NormalizeURL (CHAR_T* orgName, Document doc, CHAR_T* newName, CHAR_T* docName, CHAR_T* otherPath)
1.3       cvs       911: #else  /* __STDC__ */
1.53      cvs       912: void                NormalizeURL (orgName, doc, newName, docName, otherPath)
1.84    ! cvs       913: CHAR_T*             orgName;
1.3       cvs       914: Document            doc;
1.84    ! cvs       915: CHAR_T*             newName;
        !           916: CHAR_T*             docName;
        !           917: CHAR_T*             otherPath;
1.3       cvs       918: #endif /* __STDC__ */
                    919: {
1.84    ! cvs       920:    CHAR_T*        basename;
        !           921:    CHAR_T         tempOrgName[MAX_LENGTH];
        !           922:    CHAR_T*        ptr;
        !           923:    CHAR_T         used_sep;
        !           924:    int            length;
        !           925:    ThotBool       check;
1.5       cvs       926: 
1.44      cvs       927: #  ifdef _WINDOWS
                    928:    int ndx;
                    929: #  endif /* _WINDOWS */
                    930: 
1.5       cvs       931:    if (!newName || !docName)
                    932:       return;
1.18      cvs       933: 
1.32      cvs       934:    if (doc != 0)
1.53      cvs       935:      basename = GetBaseURL (doc);
                    936:    else if (otherPath != NULL)
1.84    ! cvs       937:      basename = TtaWCSdup (otherPath);
1.32      cvs       938:    else
1.53      cvs       939:      basename = NULL;
1.32      cvs       940: 
1.18      cvs       941:    /*
1.31      cvs       942:     * Clean orgName
                    943:     * Make sure we have a complete orgName, without any leading or trailing
                    944:     * white spaces, or trailinbg new lines
                    945:     */
1.5       cvs       946:    ptr = orgName;
1.18      cvs       947:    /* skip leading white space and new line characters */
1.84    ! cvs       948:    while ((*ptr == WC_SPACE || *ptr == WC_EOL) && *ptr++ != WC_EOS);
        !           949:    ustrncpy (tempOrgName, ptr, MAX_LENGTH -1);
        !           950:    tempOrgName[MAX_LENGTH -1] = WC_EOS;
1.18      cvs       951:    /*
1.31      cvs       952:     * Make orgName a complete URL
                    953:     * If the URL does not include a protocol, then try to calculate
                    954:     * one using the doc's base element (if it exists),
                    955:     */
1.84    ! cvs       956:    if (tempOrgName[0] == WC_EOS)
1.53      cvs       957:      {
1.84    ! cvs       958:        newName[0] = WC_EOS;
1.53      cvs       959:        TtaFreeMemory (basename);
                    960:        return;
                    961:      }
1.49      cvs       962: 
                    963:    /* clean trailing white space */
1.84    ! cvs       964:    length = ustrlen (tempOrgName) - 1;
        !           965:    while (tempOrgName[length] == WC_SPACE && tempOrgName[length] == WC_EOL)
1.53      cvs       966:      {
1.84    ! cvs       967:        tempOrgName[length] = WC_EOS;
1.53      cvs       968:        length--;
                    969:      }
1.50      cvs       970: 
1.55      cvs       971:    /* remove extra dot (which dot???) */
                    972:    /* ugly, but faster than a strcmp */
1.84    ! cvs       973:    if (tempOrgName[length] == TEXT('.')
        !           974:        && (length == 0 || tempOrgName[length-1] != TEXT('.')))
        !           975:         tempOrgName[length] = WC_EOS;
1.50      cvs       976: 
1.53      cvs       977:    if (IsW3Path (tempOrgName))
                    978:      {
                    979:        /* the name is complete, go to the Sixth Step */
1.84    ! cvs       980:        ustrcpy (newName, tempOrgName);
1.53      cvs       981:        SimplifyUrl (&newName);
                    982:        /* verify if the URL has the form "protocol://server:port" */
1.84    ! cvs       983:        ptr = AmayaParseUrl (newName, TEXT(""), AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
        !           984:        if (ptr && !ustrcmp (ptr, newName)) /* it has this form, we complete it by adding a DIR_STR  */
        !           985:          ustrcat (newName, WC_URL_STR);
1.49      cvs       986: 
1.53      cvs       987:        if (ptr)
1.50      cvs       988:          TtaFreeMemory (ptr);
1.53      cvs       989:      }
                    990:    else if ( basename == NULL)
                    991:      /* the name is complete, go to the Sixth Step */
1.84    ! cvs       992:      ustrcpy (newName, tempOrgName);
1.53      cvs       993:    else
                    994:      {
1.31      cvs       995:        /* Calculate the absolute URL, using the base or document URL */
1.44      cvs       996: #      ifdef _WINDOWS
1.53      cvs       997:        if (!IsW3Path (basename))
                    998:         {
1.67      cvs       999:           length = ustrlen (tempOrgName);
1.53      cvs      1000:           for (ndx = 0; ndx < length; ndx++)
1.67      cvs      1001:             if (tempOrgName [ndx] == TEXT('/'))
                   1002:               tempOrgName [ndx] = TEXT('\\');
1.53      cvs      1003:         }
1.44      cvs      1004: #      endif /* _WINDOWS */
1.25      cvs      1005:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53      cvs      1006:        if (ptr)
                   1007:         {
                   1008:           SimplifyUrl (&ptr);
1.84    ! cvs      1009:           ustrcpy (newName, ptr);
1.53      cvs      1010:           TtaFreeMemory (ptr);
                   1011:         }
                   1012:        else
1.84    ! cvs      1013:         newName[0] = WC_EOS;
1.53      cvs      1014:      }
1.36      cvs      1015: 
                   1016:    TtaFreeMemory (basename);
1.18      cvs      1017:    /*
1.31      cvs      1018:     * Prepare the docname that will refer to this ressource in the
                   1019:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                   1020:     * noname.html as a default ressource name
1.18      cvs      1021:    */
1.84    ! cvs      1022:    if (newName[0] != WC_EOS)
1.53      cvs      1023:      {
1.84    ! cvs      1024:        length = ustrlen (newName) - 1;
        !          1025:        if (newName[length] == WC_URL_SEP || newName[length] == WC_DIR_SEP)
1.53      cvs      1026:         {
                   1027:           used_sep = newName[length];
                   1028:           check = TRUE;
                   1029:           while (check)
                   1030:             {
1.50      cvs      1031:                length--;
                   1032:                while (length >= 0 && newName[length] != used_sep)
1.53      cvs      1033:                 length--;
1.84    ! cvs      1034:                if (!ustrncmp (&newName[length+1], TEXT(".."), 2))
1.53      cvs      1035:                 {
1.84    ! cvs      1036:                   newName[length+1] = WC_EOS;
1.53      cvs      1037:                   /* remove also previous directory */
                   1038:                   length--;
                   1039:                   while (length >= 0 && newName[length] != used_sep)
                   1040:                     length--;
1.84    ! cvs      1041:                   if (ustrncmp (&newName[length+1], TEXT("//"), 2))
1.53      cvs      1042:                     /* don't remove server name */
1.84    ! cvs      1043:                      newName[length+1] = WC_EOS;
1.53      cvs      1044:                 }
1.84    ! cvs      1045:               else if (!ustrncmp (&newName[length+1], TEXT("."), 1))
        !          1046:                 newName[length+1] = WC_EOS;
1.50      cvs      1047:                else
1.53      cvs      1048:                 check = FALSE;
                   1049:             }
1.84    ! cvs      1050:           ustrcpy (docName, TEXT("noname.html"));             
1.53      cvs      1051:           /* docname was not comprised inside the URL, so let's */
                   1052:           /* assign the default ressource name */
1.84    ! cvs      1053:           ustrcpy (docName, TEXT("noname.html"));
1.53      cvs      1054:         }
                   1055:        else
                   1056:         { /* docname is comprised inside the URL */
1.84    ! cvs      1057:            while (length >= 0 && newName[length] != WC_URL_SEP && newName[length] != WC_DIR_SEP)
1.53      cvs      1058:             length--;
                   1059:           if (length < 0)
1.84    ! cvs      1060:              ustrcpy (docName, newName);
1.53      cvs      1061:           else
1.84    ! cvs      1062:             ustrcpy (docName, &newName[length+1]);
1.53      cvs      1063:         }
                   1064:      }
                   1065:    else
1.84    ! cvs      1066:      docName[0] = WC_EOS;
1.18      cvs      1067: } 
1.3       cvs      1068: 
1.4       cvs      1069: /*----------------------------------------------------------------------
1.9       cvs      1070:   IsSameHost                                                         
1.4       cvs      1071:   ----------------------------------------------------------------------*/
1.3       cvs      1072: #ifdef __STDC__
1.67      cvs      1073: ThotBool             IsSameHost (const STRING url1, const STRING url2)
1.3       cvs      1074: #else  /* __STDC__ */
1.67      cvs      1075: ThotBool             IsSameHost (url1, url2)
                   1076: const STRING       url1;
                   1077: const STRING       url2;
1.3       cvs      1078: #endif /* __STDC__ */
                   1079: {
1.67      cvs      1080:    STRING           basename_ptr1, basename_ptr2;
                   1081:    ThotBool          result;
1.3       cvs      1082: 
1.84    ! cvs      1083:    basename_ptr1 = AmayaParseUrl (url1, TEXT(""), AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.67      cvs      1084:    basename_ptr2 = AmayaParseUrl (url2, _EMPTYSTR_, AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs      1085: 
1.67      cvs      1086:    if (ustrcmp (basename_ptr1, basename_ptr2))
1.8       cvs      1087:       result = FALSE;
1.5       cvs      1088:    else
1.8       cvs      1089:       result = TRUE;
1.3       cvs      1090: 
1.25      cvs      1091:    TtaFreeMemory (basename_ptr1);
                   1092:    TtaFreeMemory (basename_ptr2);
1.5       cvs      1093:    return (result);
1.3       cvs      1094: }
                   1095: 
                   1096: 
1.4       cvs      1097: /*----------------------------------------------------------------------
1.22      cvs      1098:   HasKnownFileSuffix
                   1099:   returns TRUE if path points to a file ending with a suffix.
                   1100:   ----------------------------------------------------------------------*/
                   1101: #ifdef __STDC__
1.67      cvs      1102: ThotBool             HasKnownFileSuffix (const STRING path)
1.22      cvs      1103: #else  /* __STDC__ */
1.67      cvs      1104: ThotBool             HasKnownFileSuffix (path)
                   1105: const STRING    path;
1.22      cvs      1106: #endif /* __STDC__ */
                   1107: {
1.67      cvs      1108:    STRING      root;
                   1109:    CHAR_T      temppath[MAX_LENGTH];
                   1110:    CHAR_T      suffix[MAX_LENGTH];
1.22      cvs      1111: 
1.67      cvs      1112:    if (!path || path[0] == EOS || path[ustrlen(path)] == DIR_SEP)
1.22      cvs      1113:      return (FALSE);
                   1114: 
1.84    ! cvs      1115:    root = AmayaParseUrl(path, TEXT(""), AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs      1116: 
                   1117:    if (root) 
                   1118:      {
1.67      cvs      1119:        ustrcpy (temppath, root);
1.25      cvs      1120:        TtaFreeMemory (root);
1.22      cvs      1121:        /* Get the suffix */
                   1122:        ExtractSuffix (temppath, suffix); 
                   1123: 
                   1124:        if( suffix[0] == EOS)
                   1125:         /* no suffix */
                   1126:         return (FALSE);
                   1127: 
                   1128:        /* Normalize the suffix */
                   1129:        ConvertToLowerCase (suffix);
                   1130: 
1.67      cvs      1131:        if (!ustrcmp (suffix, TEXT("gz")))
1.22      cvs      1132:         /* skip the compressed suffix */
                   1133:         {
                   1134:         ExtractSuffix (temppath, suffix);
                   1135:         if(suffix[0] == EOS)
                   1136:           /* no suffix */
                   1137:           return (FALSE);
                   1138:          /* Normalize the suffix */
                   1139:          ConvertToLowerCase (suffix);
                   1140:         }
                   1141: 
1.67      cvs      1142:        if (ustrcmp (suffix, TEXT("gif")) &&
                   1143:           ustrcmp (suffix, TEXT("xbm")) &&
                   1144:           ustrcmp (suffix, TEXT("xpm")) &&
                   1145:           ustrcmp (suffix, TEXT("jpg")) &&
                   1146:           ustrcmp (suffix, TEXT("pdf")) &&
                   1147:           ustrcmp (suffix, TEXT("png")) &&
                   1148:           ustrcmp (suffix, TEXT("tgz")) &&
                   1149:           ustrcmp (suffix, TEXT("xpg")) &&
                   1150:           ustrcmp (suffix, TEXT("xpd")) &&
                   1151:           ustrcmp (suffix, TEXT("ps")) &&
                   1152:           ustrcmp (suffix, TEXT("au")) &&
                   1153:           ustrcmp (suffix, TEXT("html")) &&
                   1154:           ustrcmp (suffix, TEXT("htm")) &&
                   1155:           ustrcmp (suffix, TEXT("shtml")) &&
                   1156:           ustrcmp (suffix, TEXT("xht")) &&
                   1157:           ustrcmp (suffix, TEXT("xhtm")) &&
                   1158:           ustrcmp (suffix, TEXT("xhtml")) &&
                   1159:           ustrcmp (suffix, TEXT("txt")) &&
                   1160:           ustrcmp (suffix, TEXT("css")) &&
                   1161:           ustrcmp (suffix, TEXT("eps")))
1.22      cvs      1162:         return (FALSE);
                   1163:        else
                   1164:         return (TRUE);
                   1165:      }
                   1166:    else
                   1167:      return (FALSE);
                   1168: }
                   1169: 
                   1170: 
                   1171: /*----------------------------------------------------------------------
1.24      cvs      1172:   ChopURL
                   1173:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                   1174:   If inputURL is  bigger than that size, outputURL receives
                   1175:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                   1176:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                   1177:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                   1178:   copied into outputURL. 
                   1179:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                   1180:   chars.
                   1181:   ----------------------------------------------------------------------*/
                   1182: #ifdef __STDC__
1.34      cvs      1183: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs      1184: #else
                   1185: void ChopURL (outputURL, inputURL)
1.34      cvs      1186: char       *outputURL;
                   1187: const char *inputURL;
1.24      cvs      1188: #endif
1.22      cvs      1189: 
1.24      cvs      1190: {
                   1191:   int len;
1.9       cvs      1192: 
1.24      cvs      1193:   len = strlen (inputURL);
                   1194:   if (len <= MAX_PRINT_URL_LENGTH) 
1.29      cvs      1195:     strcpy (outputURL, inputURL);
1.24      cvs      1196:   else
                   1197:     /* make a truncated urlName on the status window */
                   1198:     {
                   1199:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                   1200:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                   1201:       strcat (outputURL, "...");
                   1202:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
                   1203:     }
1.25      cvs      1204: }
                   1205: 
                   1206: 
                   1207: /*----------------------------------------------------------------------
                   1208:    scan
1.47      cvs      1209:        Scan a filename for its constituents
1.25      cvs      1210:        -----------------------------------
                   1211:   
                   1212:    On entry,
                   1213:        name    points to a document name which may be incomplete.
                   1214:    On exit,
                   1215:         absolute or relative may be nonzero (but not both).
                   1216:        host, fragment and access may be nonzero if they were specified.
                   1217:        Any which are nonzero point to zero terminated strings.
                   1218:   ----------------------------------------------------------------------*/
                   1219: #ifdef __STDC__
1.67      cvs      1220: static void scan (STRING name, HTURI * parts)
1.25      cvs      1221: #else  /* __STDC__ */
                   1222: static void scan (name, parts)
1.67      cvs      1223: STRING      name;
                   1224: HTURI       *parts;
1.25      cvs      1225: 
                   1226: #endif /* __STDC__ */
                   1227: {
1.67      cvs      1228:   STRING    p;
                   1229:   STRING    after_access = name;
1.32      cvs      1230: 
1.43      cvs      1231:   memset (parts, '\0', sizeof (HTURI));
1.28      cvs      1232:   /* Look for fragment identifier */
1.67      cvs      1233:   if ((p = ustrchr(name, TEXT('#'))) != NULL)
1.28      cvs      1234:     {
1.67      cvs      1235:       *p++ = TEXT('\0');
1.28      cvs      1236:       parts->fragment = p;
1.25      cvs      1237:     }
                   1238:     
1.28      cvs      1239:   for (p=name; *p; p++)
                   1240:     {
1.67      cvs      1241:       if (*p == URL_SEP || *p == DIR_SEP || *p == TEXT('#') || *p == TEXT('?'))
1.28      cvs      1242:        break;
1.67      cvs      1243:       if (*p == TEXT(':'))
1.28      cvs      1244:        {
                   1245:          *p = 0;
                   1246:          parts->access = after_access; /* Scheme has been specified */
                   1247: 
                   1248:          /* The combination of gcc, the "-O" flag and the HP platform is
                   1249:             unhealthy. The following three lines is a quick & dirty fix, but is
                   1250:             not recommended. Rather, turn off "-O". */
                   1251: 
                   1252:          /*            after_access = p;*/
                   1253:          /*            while (*after_access == 0)*/
                   1254:          /*                after_access++;*/
                   1255:          after_access = p+1;
1.67      cvs      1256:          if (!ustrcasecmp(TEXT("URL"), parts->access))
1.28      cvs      1257:            /* Ignore IETF's URL: pre-prefix */
                   1258:            parts->access = NULL;
                   1259:          else
1.25      cvs      1260:            break;
                   1261:        }
                   1262:     }
                   1263:     
                   1264:     p = after_access;
1.43      cvs      1265:     if (*p == URL_SEP || *p == DIR_SEP)
1.28      cvs      1266:       {
1.43      cvs      1267:        if (p[1] == URL_SEP)
1.28      cvs      1268:          {
1.25      cvs      1269:            parts->host = p+2;          /* host has been specified      */
1.28      cvs      1270:            *p = 0;                     /* Terminate access             */
                   1271:            /* look for end of host name if any */
1.67      cvs      1272:            p = ustrchr (parts->host, URL_SEP);
1.28      cvs      1273:            if (p)
                   1274:              {
1.43      cvs      1275:                *p = EOS;                       /* Terminate host */
1.25      cvs      1276:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs      1277:              }
                   1278:          }
                   1279:        else
                   1280:          /* Root found but no host */
                   1281:          parts->absolute = p+1;
                   1282:       }
                   1283:     else
                   1284:       {
1.25      cvs      1285:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs      1286:       }
1.25      cvs      1287: }
                   1288: 
                   1289: 
                   1290: /*----------------------------------------------------------------------
1.28      cvs      1291:   AmayaParseUrl: parse a Name relative to another name
                   1292: 
                   1293:   This returns those parts of a name which are given (and requested)
                   1294:   substituting bits from the related name where necessary.
1.25      cvs      1295:   
1.28      cvs      1296:   On entry,
1.25      cvs      1297:        aName           A filename given
                   1298:         relatedName     A name relative to which aName is to be parsed. Give
                   1299:                         it an empty string if aName is absolute.
                   1300:         wanted          A mask for the bits which are wanted.
                   1301:   
1.28      cvs      1302:   On exit,
1.25      cvs      1303:        returns         A pointer to a malloc'd string which MUST BE FREED
                   1304:   ----------------------------------------------------------------------*/
                   1305: #ifdef __STDC__
1.84    ! cvs      1306: CHAR_T*        AmayaParseUrl (const CHAR_T* aName, CHAR_T* relatedName, int wanted)
1.25      cvs      1307: #else  /* __STDC__ */
1.84    ! cvs      1308: CHAR_T*        AmayaParseUrl (aName, relatedName, wanted)
        !          1309: const CHAR_T*  aName;
        !          1310: CHAR_T*        relatedName;
1.28      cvs      1311: int            wanted;
1.25      cvs      1312: 
                   1313: #endif /* __STDC__ */
                   1314: {
1.84    ! cvs      1315:   CHAR_T*    return_value;
1.67      cvs      1316:   CHAR_T     result[MAX_LENGTH];
                   1317:   CHAR_T     name[MAX_LENGTH];
                   1318:   CHAR_T     rel[MAX_LENGTH];
1.84    ! cvs      1319:   CHAR_T     *p, *access;
1.29      cvs      1320:   HTURI      given, related;
                   1321:   int        len;
1.67      cvs      1322:   CHAR_T     used_sep;
1.84    ! cvs      1323:   CHAR_T*    used_str;
1.32      cvs      1324: 
1.84    ! cvs      1325:   if (ustrchr (aName, WC_DIR_SEP) || ustrchr (relatedName, WC_DIR_SEP))
1.33      cvs      1326:     {
1.84    ! cvs      1327:       used_str = WC_DIR_STR;
        !          1328:       used_sep = WC_DIR_SEP;
1.33      cvs      1329:     }
1.32      cvs      1330:   else
1.33      cvs      1331:     {
1.84    ! cvs      1332:       used_str = WC_URL_STR;
        !          1333:       used_sep = WC_URL_SEP;
1.33      cvs      1334:     }
1.32      cvs      1335: 
1.29      cvs      1336:   /* Make working copies of input strings to cut up: */
                   1337:   return_value = NULL;
                   1338:   result[0] = 0;               /* Clear string  */
1.67      cvs      1339:   ustrcpy (name, aName);
1.29      cvs      1340:   if (relatedName != NULL)  
1.67      cvs      1341:     ustrcpy (rel, relatedName);
1.29      cvs      1342:   else
1.84    ! cvs      1343:     relatedName[0] = WC_EOS;
1.29      cvs      1344:   
                   1345:   scan (name, &given);
                   1346:   scan (rel,  &related); 
                   1347:   access = given.access ? given.access : related.access;
                   1348:   if (wanted & AMAYA_PARSE_ACCESS)
                   1349:     if (access)
                   1350:       {
1.67      cvs      1351:        ustrcat (result, access);
1.29      cvs      1352:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1353:                ustrcat (result, TEXT(":"));
1.29      cvs      1354:       }
                   1355:   
                   1356:   if (given.access && related.access)
                   1357:     /* If different, inherit nothing. */
1.67      cvs      1358:     if (ustrcmp (given.access, related.access) != 0)
1.29      cvs      1359:       {
                   1360:        related.host = 0;
                   1361:        related.absolute = 0;
                   1362:        related.relative = 0;
                   1363:        related.fragment = 0;
                   1364:       }
                   1365:   
                   1366:   if (wanted & AMAYA_PARSE_HOST)
                   1367:     if(given.host || related.host)
                   1368:       {
                   1369:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1370:          ustrcat (result, TEXT("//"));
                   1371:        ustrcat (result, given.host ? given.host : related.host);
1.29      cvs      1372:       }
                   1373:   
                   1374:   if (given.host && related.host)
                   1375:     /* If different hosts, inherit no path. */
1.67      cvs      1376:     if (ustrcmp (given.host, related.host) != 0)
1.29      cvs      1377:       {
                   1378:        related.absolute = 0;
                   1379:        related.relative = 0;
                   1380:        related.fragment = 0;
                   1381:       }
                   1382:   
                   1383:   if (wanted & AMAYA_PARSE_PATH)
                   1384:     {
                   1385:       if (given.absolute)
                   1386:        {
                   1387:          /* All is given */
                   1388:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1389:            ustrcat (result, used_str);
                   1390:          ustrcat (result, given.absolute);
1.25      cvs      1391:        }
1.29      cvs      1392:       else if (related.absolute)
                   1393:        {
                   1394:          /* Adopt path not name */
1.67      cvs      1395:          ustrcat (result, used_str);
                   1396:          ustrcat (result, related.absolute);
1.29      cvs      1397:          if (given.relative)
                   1398:            {
                   1399:              /* Search part? */
1.67      cvs      1400:              p = ustrchr (result, TEXT('?'));
1.29      cvs      1401:              if (!p)
1.67      cvs      1402:                p=result+ustrlen(result)-1;
1.33      cvs      1403:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1404:              /* Remove filename */
                   1405:              p[1]=0;
                   1406:              /* Add given one */
1.67      cvs      1407:              ustrcat (result, given.relative);
1.25      cvs      1408:            }
                   1409:        }
1.29      cvs      1410:       else if (given.relative)
                   1411:        /* what we've got */
1.67      cvs      1412:        ustrcat (result, given.relative);
1.29      cvs      1413:       else if (related.relative)
1.67      cvs      1414:        ustrcat (result, related.relative);
1.29      cvs      1415:       else
                   1416:        /* No inheritance */
1.67      cvs      1417:        ustrcat (result, used_str);
1.25      cvs      1418:     }
1.29      cvs      1419:   
                   1420:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1421:     if (given.fragment || related.fragment)
                   1422:       {
                   1423:        if (given.absolute && given.fragment)
                   1424:          {
                   1425:            /*Fixes for relURLs...*/
                   1426:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1427:              ustrcat (result, TEXT("#"));
                   1428:            ustrcat (result, given.fragment); 
1.29      cvs      1429:          }
                   1430:        else if (!(given.absolute) && !(given.fragment))
1.84    ! cvs      1431:          ustrcat (result, TEXT(""));
1.29      cvs      1432:        else
                   1433:          {
                   1434:            if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67      cvs      1435:              ustrcat (result, TEXT("#"));
                   1436:            ustrcat (result, given.fragment ? given.fragment : related.fragment); 
1.29      cvs      1437:          }
                   1438:       }
1.67      cvs      1439:   len = ustrlen (result);
                   1440:   if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1441:     ustrcpy (return_value, result);
1.29      cvs      1442:   return (return_value);               /* exactly the right length */
1.25      cvs      1443: }
                   1444: 
                   1445: /*----------------------------------------------------------------------
                   1446:      HTCanon
                   1447:        Canonicalizes the URL in the following manner starting from the host
                   1448:        pointer:
                   1449:   
                   1450:        1) The host name is converted to lowercase
                   1451:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1452:   
                   1453:        Return: OK      The position of the current path part of the URL
                   1454:                        which might be the old one or a new one.
                   1455:   
                   1456:   ----------------------------------------------------------------------*/
                   1457: #ifdef __STDC__
1.84    ! cvs      1458: static CHAR_T* HTCanon (CHAR_T** filename, CHAR_T* host)
1.25      cvs      1459: #else  /* __STDC__ */
1.67      cvs      1460: static STRING HTCanon (filename, host)
1.84    ! cvs      1461: CHAR_T** filename;
        !          1462: CHAR_T*  host;
1.25      cvs      1463: #endif /* __STDC__ */
                   1464: {
1.84    ! cvs      1465:     CHAR_T* newname = NULL;
        !          1466:     CHAR_T  used_sep;
        !          1467:     CHAR_T* path;
        !          1468:     CHAR_T* strptr;
        !          1469:     CHAR_T* port;
        !          1470:     CHAR_T* access = host-3;
1.32      cvs      1471: 
                   1472:   
1.84    ! cvs      1473:      if (*filename && ustrchr (*filename, WC_URL_SEP))
1.33      cvs      1474:        {
1.84    ! cvs      1475:         used_sep = WC_URL_SEP;
1.33      cvs      1476:        }
                   1477:      else
                   1478:        {
1.84    ! cvs      1479:         used_sep = WC_DIR_SEP;
1.33      cvs      1480:        }
1.32      cvs      1481:   
1.82      cvs      1482:     while (access > *filename && *(access - 1) != used_sep)       /* Find access method */
1.25      cvs      1483:        access--;
1.84    ! cvs      1484:     if ((path = ustrchr (host, used_sep)) == NULL)                     /* Find path */
        !          1485:        path = host + ustrlen (host);
        !          1486:     if ((strptr = ustrchr (host, TEXT('@'))) != NULL && strptr < path)    /* UserId */
1.82      cvs      1487:        host = strptr;
1.84    ! cvs      1488:     if ((port = ustrchr (host, TEXT(':'))) != NULL && port > path)      /* Port number */
1.82      cvs      1489:        port = NULL;
1.25      cvs      1490: 
                   1491:     strptr = host;                                 /* Convert to lower-case */
1.82      cvs      1492:     while (strptr < path)
1.33      cvs      1493:       {
1.84    ! cvs      1494:          *strptr = utolower (*strptr);
1.82      cvs      1495:          strptr++;
1.33      cvs      1496:       }
1.25      cvs      1497:     
                   1498:     /* Does the URL contain a full domain name? This also works for a
                   1499:        numerical host name. The domain name is already made lower-case
                   1500:        and without a trailing dot. */
                   1501:     {
1.84    ! cvs      1502:       CHAR_T* dot = port ? port : path;
        !          1503:       if (dot > *filename && *--dot == TEXT('.'))
1.33      cvs      1504:        {
1.84    ! cvs      1505:          CHAR_T* orig = dot;
        !          1506:       CHAR_T* dest = dot + 1;
1.82      cvs      1507:          while ((*orig++ = *dest++));
                   1508:             if (port) port--;
1.33      cvs      1509:          path--;
1.25      cvs      1510:        }
                   1511:     }
                   1512:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1513:     if (port)
                   1514:       {
1.82      cvs      1515:        if (!*(port+1) || *(port+1) == used_sep)
1.33      cvs      1516:          {
                   1517:            if (!newname)
                   1518:              {
1.84    ! cvs      1519:                CHAR_T* orig = port; 
        !          1520:         CHAR_T* dest = port + 1;
1.82      cvs      1521:                while ((*orig++ = *dest++));
1.33      cvs      1522:              }
                   1523:          }
1.84    ! cvs      1524:        else if ((!ustrncmp (access, TEXT("http"), 4)   &&
        !          1525:              (*(port + 1) == TEXT('8')                    && 
        !          1526:              *(port+2) == TEXT('0')                       && 
1.82      cvs      1527:              (*(port+3) == used_sep || !*(port + 3))))       ||
1.84    ! cvs      1528:              (!ustrncmp (access, TEXT("gopher"), 6) &&
        !          1529:              (*(port+1) == TEXT('7')                      && 
        !          1530:              *(port+2) == TEXT('0')                       && 
1.82      cvs      1531:              (*(port+3) == used_sep || !*(port+3))))         ||
1.84    ! cvs      1532:              (!ustrncmp (access, TEXT("ftp"), 3)    &&
        !          1533:              (*(port+1) == TEXT('2')                      && 
        !          1534:              *(port + 2) == TEXT('1')                     && 
1.82      cvs      1535:              (*(port+3) == used_sep || !*(port+3))))) {
1.33      cvs      1536:          if (!newname)
                   1537:            {
1.84    ! cvs      1538:              CHAR_T* orig = port; 
        !          1539:           CHAR_T* dest = port + 3;
1.33      cvs      1540:              while((*orig++ = *dest++));
                   1541:              /* Update path position, Henry Minsky */
                   1542:              path -= 3;
1.25      cvs      1543:            }
1.33      cvs      1544:        }
                   1545:        else if (newname)
1.84    ! cvs      1546:          ustrncat (newname, port, (int) (path - port));
1.33      cvs      1547:       }
1.25      cvs      1548: 
1.33      cvs      1549:     if (newname)
                   1550:       {
1.84    ! cvs      1551:        CHAR_T* newpath = newname + ustrlen (newname);
        !          1552:        ustrcat (newname, path);
1.25      cvs      1553:        path = newpath;
1.28      cvs      1554:        /* Free old copy */
                   1555:        TtaFreeMemory(*filename);
1.25      cvs      1556:        *filename = newname;
1.33      cvs      1557:       }
1.25      cvs      1558:     return path;
                   1559: }
                   1560: 
                   1561: 
                   1562: /*----------------------------------------------------------------------
1.29      cvs      1563:   SimplifyUrl: simplify a URI
1.32      cvs      1564:   A URI is allowed to contain the sequence xxx/../ which may be
                   1565:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1566:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1567:   
1.28      cvs      1568:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1569:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1570:   
1.28      cvs      1571:   but we should NOT change
                   1572:                 http://fred.xxx.edu/../..
1.25      cvs      1573:   
                   1574:        or      ../../albert.html
                   1575:   
1.28      cvs      1576:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1577:   
                   1578:                /fred/..                becomes /fred/..
                   1579:                /fred/././..            becomes /fred/..
                   1580:                /fred/.././junk/.././   becomes /fred/..
                   1581:   
1.28      cvs      1582:   If more than one set of `://' is found (several proxies in cascade) then
                   1583:   only the part after the last `://' is simplified.
1.25      cvs      1584:   
1.28      cvs      1585:   Returns: A string which might be the old one or a new one.
1.25      cvs      1586:   ----------------------------------------------------------------------*/
                   1587: #ifdef __STDC__
1.84    ! cvs      1588: void         SimplifyUrl (CHAR_T** url)
1.25      cvs      1589: #else  /* __STDC__ */
1.29      cvs      1590: void         SimplifyUrl (url)
1.84    ! cvs      1591: CHAR_T**     url;
1.25      cvs      1592: #endif /* __STDC__ */
                   1593: {
1.84    ! cvs      1594:   CHAR_T*   path;
        !          1595:   CHAR_T*   access;
        !          1596:   CHAR_T*   newptr; 
        !          1597:   CHAR_T*   p;
        !          1598:   CHAR_T   *orig, *dest, *end;
1.28      cvs      1599: 
1.82      cvs      1600:   CharUnit used_sep;
1.77      cvs      1601:   ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
                   1602:                             something/../ simplification in relative URLs when they start with a ../ */
1.32      cvs      1603: 
                   1604: 
1.28      cvs      1605:   if (!url || !*url)
                   1606:     return;
                   1607: 
1.84    ! cvs      1608:   if (ustrchr (*url, WC_URL_SEP))
1.33      cvs      1609:     {
1.84    ! cvs      1610:       used_sep = WC_URL_SEP;
1.33      cvs      1611:     }
1.32      cvs      1612:   else
1.33      cvs      1613:     {
1.84    ! cvs      1614:       used_sep = WC_DIR_SEP;
1.33      cvs      1615:     }
1.32      cvs      1616: 
1.77      cvs      1617:   /* should we simplify double dot? */
                   1618:   path = *url;
1.84    ! cvs      1619:   if (*path == TEXT('.') && *(path + 1) == TEXT('.'))
1.77      cvs      1620:     ddot_simplify = FALSE;
                   1621:   else
                   1622:     ddot_simplify = TRUE;
                   1623: 
1.28      cvs      1624:   /* Find any scheme name */
1.84    ! cvs      1625:   if ((path = ustrstr (*url, TEXT("://"))) != NULL)
1.33      cvs      1626:     {
                   1627:       /* Find host name */
1.28      cvs      1628:       access = *url;
1.84    ! cvs      1629:       while (access < path && (*access = utolower (*access)))
1.82      cvs      1630:             access++;
1.28      cvs      1631:       path += 3;
1.84    ! cvs      1632:       while ((newptr = ustrstr (path, TEXT ("://"))) != NULL)
1.82      cvs      1633:             /* For proxies */
                   1634:             path = newptr+3;
                   1635:      /* We have a host name */
1.84    ! cvs      1636:       path = HTCanon (url, path);
1.25      cvs      1637:     }
1.84    ! cvs      1638:   else if ((path = ustrstr (*url, TEXT(":/"))) != NULL)
1.28      cvs      1639:     path += 2;
                   1640:   else
                   1641:     path = *url;
1.25      cvs      1642: 
1.84    ! cvs      1643:   if (*path == used_sep && *(path+1) == used_sep)
1.28      cvs      1644:     /* Some URLs start //<foo> */
                   1645:     path += 1;
1.84    ! cvs      1646:   else if (!ustrncmp (path, TEXT("news:"), 5))
1.28      cvs      1647:     {
1.84    ! cvs      1648:       newptr = ustrchr (path+5, TEXT('@'));
1.28      cvs      1649:       if (!newptr)
                   1650:        newptr = path + 5;
                   1651:       while (*newptr)
                   1652:        {
                   1653:          /* Make group or host lower case */
1.84    ! cvs      1654:          *newptr = utolower (*newptr);
1.28      cvs      1655:          newptr++;
1.25      cvs      1656:        }
1.28      cvs      1657:       /* Doesn't need to do any more */
                   1658:       return;
1.25      cvs      1659:     }
1.28      cvs      1660: 
                   1661:   if ((p = path))
                   1662:     {
1.67      cvs      1663:       if (!((end = ustrchr (path, TEXT(';'))) || (end = ustrchr (path, TEXT('?'))) ||
                   1664:            (end = ustrchr (path, TEXT('#')))))
                   1665:        end = path + ustrlen (path);
1.28      cvs      1666:       
                   1667:       /* Parse string second time to simplify */
                   1668:       p = path;
                   1669:       while (p < end)
                   1670:        {
1.77      cvs      1671:          /* if we're pointing to a char, it's safe to reactivate the ../ convertion */
                   1672:          if (!ddot_simplify && *p != TEXT('.') && *p != used_sep)
                   1673:            ddot_simplify = TRUE;
                   1674: 
1.33      cvs      1675:          if (*p==used_sep)
1.28      cvs      1676:            {
1.67      cvs      1677:              if (p > *url && *(p+1) == TEXT('.') && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      1678:                {
                   1679:                  orig = p + 1;
1.84    ! cvs      1680:                  dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52      cvs      1681:                  while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28      cvs      1682:                  end = orig - 1;
                   1683:                }
1.77      cvs      1684:              else if (ddot_simplify && *(p+1) == TEXT('.') && *(p+2) == TEXT('.') 
                   1685:                       && (*(p+3) == used_sep || !*(p+3)))
1.28      cvs      1686:                {
                   1687:                  newptr = p;
1.52      cvs      1688:                  while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
                   1689:                  if (*newptr == used_sep)
                   1690:                    orig = newptr + 1;
1.28      cvs      1691:                  else
1.52      cvs      1692:                    orig = newptr;
                   1693: 
                   1694:                  dest = (*(p+3) != used_sep) ? p+3 : p+4;
                   1695:                  while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   1696:                  end = orig-1;
                   1697:                  /* Start again with prev slash */
                   1698:                  p = newptr;
1.28      cvs      1699:                }
1.33      cvs      1700:              else if (*(p+1) == used_sep)
1.28      cvs      1701:                {
1.33      cvs      1702:                  while (*(p+1) == used_sep)
1.28      cvs      1703:                    {
                   1704:                      orig = p;
                   1705:                      dest = p + 1;
                   1706:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   1707:                      end = orig-1;
                   1708:                    }
                   1709:                }
                   1710:              else
1.25      cvs      1711:                p++;
1.28      cvs      1712:            }
                   1713:          else
                   1714:            p++;
1.25      cvs      1715:        }
                   1716:     }
1.51      cvs      1717: 
                   1718:     /*
                   1719:     **  Check for host/../.. kind of things
                   1720:     */
1.77      cvs      1721:     if (*path == used_sep && *(path+1) == TEXT('.') && *(path+2) == TEXT('.') 
                   1722:        && (!*(path+3) || *(path+3) == used_sep))
1.51      cvs      1723:        *(path+1) = EOS;
                   1724: 
1.28      cvs      1725:   return;
                   1726: }
                   1727: 
                   1728: 
                   1729: /*----------------------------------------------------------------------
                   1730:    NormalizeFile normalizes  local names.                             
                   1731:    Return TRUE if target and src differ.                           
                   1732:   ----------------------------------------------------------------------*/
                   1733: #ifdef __STDC__
1.84    ! cvs      1734: ThotBool             NormalizeFile (CHAR_T* src, CHAR_T* target)
1.28      cvs      1735: #else
1.67      cvs      1736: ThotBool             NormalizeFile (src, target)
1.84    ! cvs      1737: CHAR_T*              src;
        !          1738: CHAR_T*              target;
1.28      cvs      1739: 
                   1740: #endif
                   1741: {
1.84    ! cvs      1742:    CHAR_T*           s;
1.82      cvs      1743:    ThotBool          change;
1.28      cvs      1744: 
1.54      cvs      1745:    change = FALSE;
1.84    ! cvs      1746:    if (ustrncmp (src, TEXT("file:"), 5) == 0)
1.28      cvs      1747:      {
                   1748:        /* remove the prefix file: */
1.84    ! cvs      1749:        if (src[5] == WC_EOS)
        !          1750:           ustrcpy (target, WC_DIR_STR);
        !          1751:        else if (src[0] == TEXT('~'))
1.28      cvs      1752:          {
                   1753:            /* replace ~ */
1.74      cvs      1754:            s = TtaGetEnvString ("HOME");
1.84    ! cvs      1755:            ustrcpy (target, s);
        !          1756:            ustrcat (target, &src[5]);
1.28      cvs      1757:          }
                   1758:        else
1.84    ! cvs      1759:           ustrcpy (target, &src[5]);
1.54      cvs      1760:        change = TRUE;
1.28      cvs      1761:      }
1.53      cvs      1762: #  ifndef _WINDOWS
1.84    ! cvs      1763:    else if (src[0] == TEXT('~'))
1.53      cvs      1764:      {
                   1765:        /* replace ~ */
1.82      cvs      1766:        s = TtaGetEnvString ("HOME");
1.84    ! cvs      1767:        ustrcpy (target, s);
        !          1768:        if (src[1] != WC_DIR_SEP)
        !          1769:          ustrcat (target, WC_DIR_STR);
        !          1770:        ustrcat (target, &src[1]);
1.54      cvs      1771:        change = TRUE;
1.53      cvs      1772:      }
                   1773: #   endif /* _WINDOWS */
1.28      cvs      1774:    else
1.84    ! cvs      1775:       ustrcpy (target, src);
1.28      cvs      1776: 
                   1777:    /* remove /../ and /./ */
1.29      cvs      1778:    SimplifyUrl (&target);
1.54      cvs      1779:    if (!change)
1.84    ! cvs      1780:      change = ustrcmp (src, target);
1.28      cvs      1781:    return (change);
1.25      cvs      1782: }
                   1783: 
1.28      cvs      1784: 
1.25      cvs      1785: /*----------------------------------------------------------------------
1.31      cvs      1786:   MakeRelativeURL: make relative name
1.25      cvs      1787:   
1.28      cvs      1788:   This function creates and returns a string which gives an expression of
                   1789:   one address as related to another. Where there is no relation, an absolute
                   1790:   address is retured.
1.25      cvs      1791:   
1.28      cvs      1792:   On entry,
1.25      cvs      1793:        Both names must be absolute, fully qualified names of nodes
                   1794:        (no fragment bits)
                   1795:   
1.28      cvs      1796:   On exit,
1.25      cvs      1797:        The return result points to a newly allocated name which, if
                   1798:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   1799:        The caller is responsible for freeing the resulting name later.
                   1800:   ----------------------------------------------------------------------*/
                   1801: #ifdef __STDC__
1.67      cvs      1802: STRING           MakeRelativeURL (STRING aName, STRING relatedName)
1.25      cvs      1803: #else  /* __STDC__ */
1.67      cvs      1804: STRING           MakeRelativeURL (aName, relatedName)
                   1805: STRING           aName;
                   1806: STRING           relatedName;
1.25      cvs      1807: #endif  /* __STDC__ */
                   1808: {
1.67      cvs      1809:   STRING         return_value;
                   1810:   CHAR_T         result[MAX_LENGTH];
                   1811:   STRING         p;
                   1812:   STRING         q;
                   1813:   STRING         after_access;
                   1814:   STRING         last_slash = NULL;
1.29      cvs      1815:   int            slashes, levels, len;
                   1816: 
1.44      cvs      1817: # ifdef _WINDOWS
                   1818:   int ndx;
                   1819: # endif /* _WINDOWS */
                   1820: 
1.29      cvs      1821:   if (aName == NULL || relatedName == NULL)
                   1822:     return (NULL);
                   1823: 
                   1824:   slashes = 0;
                   1825:   after_access = NULL;
                   1826:   p = aName;
                   1827:   q = relatedName;
                   1828:   for (; *p && (*p == *q); p++, q++)
1.27      cvs      1829:     {
                   1830:       /* Find extent of match */
1.67      cvs      1831:       if (*p == TEXT(':'))
1.29      cvs      1832:        after_access = p + 1;
1.28      cvs      1833:       if (*p == DIR_SEP)
1.27      cvs      1834:        {
1.29      cvs      1835:          /* memorize the last slash position and count them */
1.27      cvs      1836:          last_slash = p;
                   1837:          slashes++;
1.25      cvs      1838:        }
                   1839:     }
                   1840:     
1.31      cvs      1841:   /* q, p point to the first non-matching character or zero */
                   1842:   if (*q == EOS)
                   1843:     {
                   1844:       /* New name is a subset of the related name */
                   1845:       /* exactly the right length */
1.67      cvs      1846:       len = ustrlen (p);
                   1847:       if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1848:        ustrcpy (return_value, p);
1.31      cvs      1849:     }
                   1850:   else if ((slashes < 2 && after_access == NULL)
                   1851:       || (slashes < 3 && after_access != NULL))
                   1852:     {
                   1853:       /* Two names whitout common path */
                   1854:       /* exactly the right length */
1.67      cvs      1855:       len = ustrlen (aName);
                   1856:       if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1857:        ustrcpy (return_value, aName);
1.31      cvs      1858:     }
                   1859:   else
                   1860:     {
                   1861:       /* Some path in common */
1.67      cvs      1862:       if (slashes == 3 && ustrncmp (aName, TEXT("http:"), 5) == 0)
1.31      cvs      1863:        /* just the same server */
1.67      cvs      1864:        ustrcpy (result, last_slash);
1.31      cvs      1865:       else
                   1866:        {
                   1867:          levels= 0; 
1.67      cvs      1868:          for (; *q && *q != TEXT('#') && *q != TEXT(';') && *q != TEXT('?'); q++)
1.31      cvs      1869:            if (*q == DIR_SEP)
                   1870:              levels++;
                   1871:          
1.52      cvs      1872:          result[0] = EOS;
1.31      cvs      1873:          for (;levels; levels--)
1.67      cvs      1874:            ustrcat (result, TEXT("../"));
                   1875:          ustrcat (result, last_slash+1);
1.31      cvs      1876:        } 
1.52      cvs      1877: 
                   1878:       if (!*result)
1.67      cvs      1879:        ustrcat (result, TEXT("./"));
1.52      cvs      1880: 
1.31      cvs      1881:       /* exactly the right length */
1.67      cvs      1882:       len = ustrlen (result);
                   1883:       if ((return_value = TtaAllocString (len + 1)) != NULL)
                   1884:        ustrcpy (return_value, result);
1.52      cvs      1885: 
1.25      cvs      1886:     }
1.44      cvs      1887: # ifdef _WINDOWS
1.67      cvs      1888:   len = ustrlen (return_value);
1.44      cvs      1889:   for (ndx = 0; ndx < len; ndx ++)
1.67      cvs      1890:          if (return_value[ndx] == TEXT('\\'))
                   1891:             return_value[ndx] = TEXT('/') ;
1.44      cvs      1892: # endif /* _WINDOWS */
1.29      cvs      1893:   return (return_value);
1.24      cvs      1894: }
1.35      cvs      1895: 
                   1896: 

Webmaster