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

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

Webmaster