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

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
                     14:  *
                     15:  */
1.7       cvs        16:  
1.15      cvs        17: #define THOT_EXPORT extern
1.3       cvs        18: #include "amaya.h"
                     19: 
1.8       cvs        20: #include "init_f.h"
                     21: #include "AHTURLTools_f.h"
                     22: 
1.24      cvs        23: #define MAX_PRINT_URL_LENGTH 50
1.29      cvs        24: typedef struct _HTURI {
                     25:     char * access;             /* Now known as "scheme" */
                     26:     char * host;
                     27:     char * absolute;
                     28:     char * relative;
                     29:     char * fragment;
                     30: } HTURI;
1.24      cvs        31: 
1.28      cvs        32: 
                     33: /*----------------------------------------------------------------------
                     34:   ConvertToLowerCase
                     35:   Converts a string to lowercase.
                     36:   ----------------------------------------------------------------------*/
1.22      cvs        37: #ifdef __STDC__
1.28      cvs        38: static void         ConvertToLowerCase (char *string)
                     39: #else  /* __STDC__ */
                     40: static void         ConvertToLowerCase (string)
                     41: char                *string;
                     42: 
                     43: #endif /* __STDC__ */
                     44: {
                     45:  int i;
                     46: 
                     47:  if (!string)
                     48:    return;
                     49: 
                     50:  for (i = 0; string[i] != EOS; i++)
                     51:    string[i] = tolower (string[i]);
                     52: }
1.22      cvs        53: 
1.8       cvs        54: /*----------------------------------------------------------------------
1.11      cvs        55:   ExplodeURL 
1.8       cvs        56:   ----------------------------------------------------------------------*/
                     57: #ifdef __STDC__
                     58: void                ExplodeURL (char *url, char **proto, char **host, char **dir, char **file)
                     59: #else
                     60: void                ExplodeURL (url, proto, host, dir, file)
                     61: char               *url;
                     62: char              **proto;
                     63: char              **host;
                     64: char              **dir;
                     65: char              **file;
                     66: 
                     67: #endif
                     68: {
1.33      cvs        69:    char            *curr, *temp;
                     70:    char             used_sep;
1.32      cvs        71: 
1.33      cvs        72:    if (url && strchr (url, URL_SEP))
                     73:      used_sep = URL_SEP;
                     74:    else
                     75:      used_sep = DIR_SEP;
1.8       cvs        76: 
                     77:    if ((url == NULL) || (proto == NULL) || (host == NULL) ||
                     78:        (dir == NULL) || (file == NULL))
                     79:       return;
                     80: 
                     81:    /* initialize every pointer */
                     82:    *proto = *host = *dir = *file = NULL;
                     83: 
                     84:    /* skip any leading space */
                     85:    while ((*url == SPACE) || (*url == TAB))
                     86:       url++;
1.9       cvs        87:    curr = url;
                     88:    if (*curr == 0)
1.8       cvs        89:       goto finished;
                     90: 
                     91:    /* go to the end of the URL */
1.9       cvs        92:    while ((*curr != 0) && (*curr != SPACE) && (*curr != '\b') &&
                     93:          (*curr != '\r') && (*curr != EOL))
                     94:       curr++;
1.8       cvs        95: 
                     96:    /* mark the end of the chain */
1.9       cvs        97:    *curr = EOS;
                     98:    curr--;
                     99:    if (curr <= url)
1.8       cvs       100:       goto finished;
                    101: 
                    102:    /* search the next DIR_SEP indicating the beginning of the file name */
                    103:    do
1.11      cvs       104:      curr--;
1.33      cvs       105:    while ((curr >= url) && (*curr != used_sep));
1.11      cvs       106: 
1.9       cvs       107:    if (curr < url)
1.8       cvs       108:       goto finished;
1.9       cvs       109:    *file = curr + 1;
1.8       cvs       110: 
                    111:    /* mark the end of the dir */
1.9       cvs       112:    *curr = EOS;
                    113:    curr--;
                    114:    if (curr < url)
1.8       cvs       115:       goto finished;
                    116: 
1.29      cvs       117:    /* search for the DIR_STR indicating the host name start */
1.33      cvs       118:    while ((curr > url) && ((*curr != used_sep) || (*(curr + 1) != used_sep)))
1.9       cvs       119:       curr--;
1.8       cvs       120: 
                    121:    /* if we found it, separate the host name from the directory */
1.33      cvs       122:    if ((*curr == DIR_SEP) && (*(curr + 1) == used_sep))
1.8       cvs       123:      {
1.9       cvs       124:        *host = temp = curr + 2;
1.33      cvs       125:        while ((*temp != 0) && (*temp != used_sep))
1.8       cvs       126:           temp++;
1.33      cvs       127:        if (*temp == used_sep)
1.8       cvs       128:          {
                    129:             *temp = EOS;
                    130:             *dir = temp + 1;
                    131:          }
                    132:      }
                    133:    else
1.11      cvs       134:      *dir = curr;
                    135: 
1.9       cvs       136:    if (curr <= url)
1.8       cvs       137:       goto finished;
                    138: 
                    139:    /* mark the end of the proto */
1.9       cvs       140:    *curr = EOS;
                    141:    curr--;
                    142:    if (curr < url)
1.8       cvs       143:       goto finished;
                    144: 
1.32      cvs       145:    if (*curr == ':')
1.8       cvs       146:      {
1.9       cvs       147:        *curr = EOS;
                    148:        curr--;
1.8       cvs       149:      }
                    150:    else
                    151:       goto finished;
1.11      cvs       152: 
1.9       cvs       153:    if (curr < url)
1.8       cvs       154:       goto finished;
1.9       cvs       155:    while ((curr > url) && (isalpha (*curr)))
                    156:       curr--;
                    157:    *proto = curr;
1.8       cvs       158: 
                    159:  finished:;
                    160: 
                    161: #ifdef AMAYA_DEBUG
                    162:    fprintf (stderr, "ExplodeURL(%s)\n\t", url);
                    163:    if (*proto)
                    164:       fprintf (stderr, "proto : %s, ", *proto);
                    165:    if (*host)
                    166:       fprintf (stderr, "host : %s, ", *host);
                    167:    if (*dir)
                    168:       fprintf (stderr, "dir : %s, ", *dir);
                    169:    if (*file)
                    170:       fprintf (stderr, "file : %s ", *file);
                    171:    fprintf (stderr, "\n");
                    172: #endif
                    173: 
                    174: }
1.3       cvs       175: 
1.4       cvs       176: /*----------------------------------------------------------------------
1.9       cvs       177:   IsHTMLName                                                         
                    178:   returns TRUE if path points to an HTML resource.
1.4       cvs       179:   ----------------------------------------------------------------------*/
1.3       cvs       180: #ifdef __STDC__
1.34    ! cvs       181: boolean             IsHTMLName (const char *path)
1.3       cvs       182: #else  /* __STDC__ */
                    183: boolean             IsHTMLName (path)
1.34    ! cvs       184: const char         *path;
1.3       cvs       185: #endif /* __STDC__ */
                    186: {
1.5       cvs       187:    char                temppath[MAX_LENGTH];
                    188:    char                suffix[MAX_LENGTH];
                    189:    char                nsuffix[MAX_LENGTH];
                    190:    int                 i;
                    191: 
                    192:    if (!path)
1.13      cvs       193:      return (FALSE);
1.5       cvs       194: 
                    195:    strcpy (temppath, path);
                    196:    ExtractSuffix (temppath, suffix);
                    197: 
                    198:    /* Normalize the suffix */
                    199:    i = 0;
                    200:    while (suffix[i] != EOS)
1.13      cvs       201:      {
1.25      cvs       202:        nsuffix[i] = tolower (suffix[i]);
1.13      cvs       203:        i++;
                    204:      }
1.5       cvs       205:    nsuffix[i] = EOS;
                    206:    if ((strcmp (nsuffix, "html")) &&
                    207:        (strcmp (nsuffix, "htm")) &&
                    208:        (strcmp (nsuffix, "shtml")))
1.13      cvs       209:      return (FALSE);
1.22      cvs       210:    else if (!strcmp (nsuffix, "gz"))
1.13      cvs       211:      {
                    212:        /* take in account compressed files */
                    213:        ExtractSuffix (temppath, suffix);       
                    214:        /* Normalize the suffix */
                    215:        i = 0;
                    216:        while (suffix[i] != EOS)
                    217:         {
1.25      cvs       218:           nsuffix[i] = tolower (suffix[i]);
1.13      cvs       219:           i++;
                    220:         }
                    221:        nsuffix[i] = EOS;
                    222:        if ((strcmp (nsuffix, "html")) &&
                    223:           (strcmp (nsuffix, "htm")) &&
                    224:           (strcmp (nsuffix, "shtml")))
                    225:         return (FALSE);
                    226:        else
                    227:         return (TRUE);
                    228:      }
                    229:    else
                    230:      return (TRUE);
1.3       cvs       231: }
                    232: 
1.4       cvs       233: /*----------------------------------------------------------------------
1.9       cvs       234:   IsImageName                                
                    235:   returns TRUE if path points to an image resource.
1.4       cvs       236:   ----------------------------------------------------------------------*/
1.3       cvs       237: #ifdef __STDC__
1.34    ! cvs       238: boolean             IsImageName (const char *path)
1.3       cvs       239: #else  /* __STDC__ */
                    240: boolean             IsImageName (path)
1.34    ! cvs       241: const char         *path;
1.3       cvs       242: #endif /* __STDC__ */
                    243: {
1.5       cvs       244:    char                temppath[MAX_LENGTH];
                    245:    char                suffix[MAX_LENGTH];
                    246:    char                nsuffix[MAX_LENGTH];
                    247:    int                 i;
                    248: 
                    249:    if (!path)
1.13      cvs       250:       return (FALSE);
1.5       cvs       251: 
                    252:    strcpy (temppath, path);
                    253:    ExtractSuffix (temppath, suffix);
                    254: 
                    255:    /* Normalize the suffix */
                    256:    i = 0;
                    257:    while (suffix[i] != EOS)
1.13      cvs       258:      {
1.25      cvs       259:        nsuffix[i] = tolower (suffix[i]);
1.13      cvs       260:        i++;
                    261:      }
1.5       cvs       262:    nsuffix[i] = EOS;
                    263:    if ((strcmp (nsuffix, "gif")) && (strcmp (nsuffix, "xbm")) &&
                    264:        (strcmp (nsuffix, "xpm")) && (strcmp (nsuffix, "jpg")) &&
                    265:        (strcmp (nsuffix, "png")) && (strcmp (nsuffix, "au")))
1.13      cvs       266:       return (FALSE);
                    267:    return (TRUE);
1.3       cvs       268: }
                    269: 
1.4       cvs       270: /*----------------------------------------------------------------------
1.9       cvs       271:   IsTextName                                                         
1.4       cvs       272:   ----------------------------------------------------------------------*/
1.3       cvs       273: #ifdef __STDC__
1.34    ! cvs       274: boolean             IsTextName (const char *path)
1.3       cvs       275: #else  /* __STDC__ */
                    276: boolean             IsTextName (path)
1.34    ! cvs       277: const char         *path;
1.3       cvs       278: 
                    279: #endif /* __STDC__ */
                    280: {
1.5       cvs       281:    char                temppath[MAX_LENGTH];
                    282:    char                suffix[MAX_LENGTH];
                    283:    char                nsuffix[MAX_LENGTH];
                    284:    int                 i;
                    285: 
                    286:    if (!path)
1.13      cvs       287:      return (FALSE);
1.5       cvs       288: 
                    289:    strcpy (temppath, path);
                    290:    ExtractSuffix (temppath, suffix);
                    291: 
                    292:    /* Normalize the suffix */
                    293:    i = 0;
                    294:    while (suffix[i] != EOS)
                    295:      {
1.25      cvs       296:        nsuffix[i] = tolower (suffix[i]);
1.5       cvs       297:        i++;
                    298:      }
                    299:    nsuffix[i] = EOS;
                    300: 
                    301:    if ((strcmp (nsuffix, "gif")) && (strcmp (nsuffix, "xbm")) &&
                    302:        (strcmp (nsuffix, "xpm")) && (strcmp (nsuffix, "jpg")) &&
                    303:        (strcmp (nsuffix, "pdf")) && (strcmp (nsuffix, "png")) &&
1.22      cvs       304:        (strcmp (nsuffix, "tgz")) && (strcmp (nsuffix, "tar")) &&
                    305:        (strcmp (nsuffix, "xpg")) && (strcmp (nsuffix, "xpd")) &&
                    306:        (strcmp (nsuffix, "ps"))  && (strcmp (nsuffix, "au")))
1.13      cvs       307:       return (TRUE);
1.22      cvs       308:    else if (!strcmp (nsuffix, "gz"))
1.13      cvs       309:      {
                    310:        /* take in account compressed files */
                    311:        ExtractSuffix (temppath, suffix);       
                    312:        /* Normalize the suffix */
                    313:        i = 0;
                    314:        while (suffix[i] != EOS)
                    315:         {
1.25      cvs       316:           nsuffix[i] = tolower (suffix[i]);
1.13      cvs       317:           i++;
                    318:         }
                    319:        nsuffix[i] = EOS;
                    320:        if ((!strcmp (nsuffix, "html")) ||
                    321:           (!strcmp (nsuffix, "htm")) ||
                    322:           (!strcmp (nsuffix, "shtml")))
                    323:         return (TRUE);
                    324:        else
                    325:         return (FALSE);
                    326:      }
                    327:    else
                    328:      return (FALSE);
1.3       cvs       329: }
                    330: 
1.4       cvs       331: /*----------------------------------------------------------------------
1.9       cvs       332:   IsHTTPPath                                     
                    333:   returns TRUE if path is in fact an http URL.
1.4       cvs       334:   ----------------------------------------------------------------------*/
1.3       cvs       335: #ifdef __STDC__
1.34    ! cvs       336: boolean             IsHTTPPath (const char *path)
1.3       cvs       337: #else  /* __STDC__ */
                    338: boolean             IsHTTPPath (path)
1.34    ! cvs       339: const char         *path;
1.3       cvs       340: #endif /* __STDC__ */
                    341: {
1.5       cvs       342:    if (!path)
                    343:       return FALSE;
1.3       cvs       344: 
1.5       cvs       345:    if (strncmp (path, "http:", 5) != 0)
                    346:       return FALSE;
                    347:    return TRUE;
1.3       cvs       348: }
                    349: 
1.4       cvs       350: /*----------------------------------------------------------------------
1.9       cvs       351:   IsWithParameters                           
                    352:   returns TRUE if url has a concatenated query string.
1.4       cvs       353:   ----------------------------------------------------------------------*/
1.3       cvs       354: #ifdef __STDC__
1.34    ! cvs       355: boolean             IsWithParameters (const char *url)
1.3       cvs       356: #else  /* __STDC__ */
1.9       cvs       357: boolean             IsWithParameters (url)
1.34    ! cvs       358: const char         *url;
1.3       cvs       359: #endif /* __STDC__ */
                    360: {
1.5       cvs       361:    int                 i;
1.3       cvs       362: 
1.9       cvs       363:    if ((!url) || (url[0] == EOS))
1.5       cvs       364:       return FALSE;
1.3       cvs       365: 
1.9       cvs       366:    i = strlen (url) - 1;
                    367:    while (i > 0 && url[i--] != '?')
1.5       cvs       368:       if (i < 0)
                    369:         return FALSE;
1.3       cvs       370: 
1.5       cvs       371:    /* There is a parameter */
                    372:    return TRUE;
1.3       cvs       373: }
                    374: 
1.4       cvs       375: /*----------------------------------------------------------------------
1.9       cvs       376:   IsW3Path                                           
                    377:   returns TRUE if path is in fact a URL.
1.4       cvs       378:   ----------------------------------------------------------------------*/
1.3       cvs       379: #ifdef __STDC__
1.34    ! cvs       380: boolean             IsW3Path (const char *path)
1.3       cvs       381: #else  /* __STDC__ */
                    382: boolean             IsW3Path (path)
1.34    ! cvs       383: const char               *path;
1.3       cvs       384: #endif /* __STDC__ */
                    385: {
1.5       cvs       386:    if ((strncmp (path, "http:", 5)) && (strncmp (path, "ftp:", 4)) &&
                    387:        (strncmp (path, "telnet:", 7)) && (strncmp (path, "wais:", 5)) &&
                    388:        (strncmp (path, "news:", 5)) && (strncmp (path, "gopher:", 7)) &&
                    389:        (strncmp (path, "mailto:", 7)) && (strncmp (path, "archie:", 7)))
                    390:       return FALSE;
                    391:    return TRUE;
1.3       cvs       392: }
                    393: 
1.4       cvs       394: /*----------------------------------------------------------------------
1.9       cvs       395:   IsValidProtocol                                                    
                    396:   returns true if the url protocol is supported by Amaya.
1.4       cvs       397:   ----------------------------------------------------------------------*/
1.3       cvs       398: #ifdef __STDC__
1.34    ! cvs       399: boolean             IsValidProtocol (const char *url)
1.3       cvs       400: #else  /* __STDC__ */
1.9       cvs       401: boolean             IsValidProtocol (url)
1.34    ! cvs       402: const char         *url;
1.3       cvs       403: #endif /* __STDC__ */
                    404: {
1.26      cvs       405:    if (!strncmp (url, "http:", 5))
1.22      cvs       406:        /* experimental */
1.26      cvs       407:       /***  || !strncmp (url, "ftp:", 4)) ***/
1.24      cvs       408:      /*** || !strncmp (path, "news:", 5)***/ 
1.8       cvs       409:       return (TRUE);
1.5       cvs       410:    else
1.8       cvs       411:       return (FALSE);
1.3       cvs       412: }
                    413: 
1.31      cvs       414: 
                    415: /*----------------------------------------------------------------------
                    416:    GetBaseURL
                    417:    normalizes orgName according to a base associated with doc, and
                    418:    following the standard URL format rules.
                    419:    The function returns the base used to solve relative URL and SRC:
                    420:       - the base of the document,
                    421:       - or the document path (without document name).
                    422:   ----------------------------------------------------------------------*/
                    423: #ifdef __STDC__
                    424: char               *GetBaseURL (Document doc)
                    425: #else  /* __STDC__ */
                    426: char               *GetBaseURL (doc)
                    427: Document            doc;
                    428: #endif /* __STDC__ */
                    429: {
                    430:   Element             el;
                    431:   ElementType         elType;
                    432:   AttributeType       attrType;
                    433:   Attribute           attr;
1.34    ! cvs       434:   char                used_sep = URL_SEP;
1.33      cvs       435:   char               *used_str;
1.31      cvs       436:   char               *ptr, *basename;
                    437:   int                 length;
                    438: 
                    439:   basename = TtaGetMemory (MAX_LENGTH);
                    440:   strcpy (basename, DocumentURLs[doc]);
                    441:   length = MAX_LENGTH -1;
                    442:   /* get the root element    */
                    443:   el = TtaGetMainRoot (doc);
                    444:   /* search the BASE element */
                    445:   elType.ElSSchema = TtaGetDocumentSSchema (doc);
                    446:   elType.ElTypeNum = HTML_EL_BASE;
                    447:   el = TtaSearchTypedElement (elType, SearchInTree, el);
                    448:   if (el)
                    449:     {
                    450:       /*  The document has a BASE element -> Get the HREF attribute */
                    451:       attrType.AttrSSchema = elType.ElSSchema;
                    452:       attrType.AttrTypeNum = HTML_ATTR_HREF_;
                    453:       attr = TtaGetAttribute (el, attrType);
                    454:       if (attr)
                    455:        {
                    456:          /* Use the base path of the document */
                    457:          TtaGiveTextAttributeValue (attr, basename, &length);
                    458:          /* base and orgName have to be separated by a DIR_SEP */
                    459:          length--;
1.32      cvs       460: 
1.33      cvs       461:          if (basename [0] != EOS && strchr (basename, URL_SEP))
                    462:            {
                    463:              used_str = URL_STR;
                    464:              used_sep = URL_SEP;
                    465:            }
                    466:          else
                    467:            {
                    468:              used_str = DIR_STR;
                    469:              used_sep = DIR_SEP;
                    470:            }
1.32      cvs       471: 
1.33      cvs       472:          if (basename[0] != EOS && basename[length] != used_sep) 
1.31      cvs       473:            /* verify if the base has the form "protocol://server:port" */
                    474:            {
1.33      cvs       475:              ptr = AmayaParseUrl (basename, "", AMAYA_PARSE_ACCESS |
                    476:                                                 AMAYA_PARSE_HOST |
                    477:                                                 AMAYA_PARSE_PUNCTUATION);
1.31      cvs       478:              if (ptr && !strcmp (ptr, basename))
                    479:                {
                    480:                  /* it has this form, we complete it by adding a DIR_STR  */
1.33      cvs       481:                  strcat (basename, used_str);
1.31      cvs       482:                  length++;
                    483:                }
                    484:              if (ptr)
                    485:                TtaFreeMemory (ptr);
                    486:            }
                    487:        }
1.33      cvs       488:       }
1.32      cvs       489:     else
1.33      cvs       490:       {
                    491:        if (basename [0] != EOS && strchr (basename, URL_SEP))
                    492:          {
                    493:            used_str = URL_STR;
                    494:            used_sep = URL_SEP;
                    495:          }
                    496:        else
                    497:          {
                    498:            used_str = DIR_STR;
                    499:            used_sep = DIR_SEP;
                    500:          }
                    501:       }
                    502:   
1.31      cvs       503:   /* Remove anything after the last DIR_SEP char. If no such char is found,
                    504:    * then search for the first ":" char, hoping that what's before that is a
                    505:    * protocol. If found, end the string there. If neither char is found,
                    506:    * then discard the whole base element.
                    507:    */
                    508:   length = strlen (basename) - 1;
                    509:   /* search for the last DIR_SEP char */
1.33      cvs       510:   while (length >= 0  && basename[length] != used_sep)
1.31      cvs       511:     length--;
                    512:   if (length >= 0)
                    513:     /* found the last DIR_SEP char, end the string there */
                    514:     basename[length + 1] = EOS;                   
                    515:   else
                    516:     /* search for the first PATH_STR char */
                    517:     {
1.32      cvs       518:       for (length = 0; basename[length] != ':' && 
1.31      cvs       519:             basename[length] != EOS; length ++);
1.32      cvs       520:       if (basename[length] == ':')
1.31      cvs       521:        /* found, so end the string there */
                    522:        basename[length + 1] = EOS;
                    523:       else
                    524:        /* not found, discard the base */
                    525:        basename[0] = EOS;
                    526:     }
                    527:   return (basename);
                    528: }
                    529: 
                    530: 
1.4       cvs       531: /*----------------------------------------------------------------------
1.9       cvs       532:    NormalizeURL
                    533:    normalizes orgName according to a base associated with doc, and
                    534:    following the standard URL format rules.
                    535:    The function returns the new complete and normalized URL 
1.12      cvs       536:    or file name path (newName) and the name of the document (docName).        
1.9       cvs       537:    N.B. If the function can't find out what's the docName, it assigns
                    538:    the name "noname.html".
1.4       cvs       539:   ----------------------------------------------------------------------*/
1.3       cvs       540: #ifdef __STDC__
                    541: void                NormalizeURL (char *orgName, Document doc, char *newName, char *docName)
                    542: #else  /* __STDC__ */
                    543: void                NormalizeURL (orgName, doc, newName, docName)
                    544: char               *orgName;
                    545: Document            doc;
                    546: char               *newName;
                    547: char               *docName;
                    548: #endif /* __STDC__ */
                    549: {
1.31      cvs       550:    char               *basename;
1.18      cvs       551:    char                tempOrgName[MAX_LENGTH];
1.5       cvs       552:    char               *ptr;
                    553:    int                 length;
1.33      cvs       554:    char                used_sep;
                    555:    char               *used_str;
1.5       cvs       556: 
                    557:    if (!newName || !docName)
                    558:       return;
1.18      cvs       559: 
1.32      cvs       560:    if (doc != 0)
1.33      cvs       561:      basename = GetBaseURL (doc);
1.32      cvs       562:    else
1.33      cvs       563:      basename = (char *) NULL;
1.32      cvs       564: 
1.33      cvs       565:    if (strchr (orgName, URL_SEP) || (basename && strchr (basename, URL_SEP)))
                    566:      { 
                    567:        used_str = URL_STR;
                    568:        used_sep = URL_SEP;
                    569:      }
                    570:    else
                    571:      {
                    572:        used_str = DIR_STR;
                    573:        used_sep = DIR_SEP;
                    574:      }
1.32      cvs       575: 
1.18      cvs       576:    /*
1.31      cvs       577:     * Clean orgName
                    578:     * Make sure we have a complete orgName, without any leading or trailing
                    579:     * white spaces, or trailinbg new lines
                    580:     */
1.18      cvs       581: 
1.5       cvs       582:    ptr = orgName;
1.18      cvs       583:    /* skip leading white space and new line characters */
1.19      cvs       584:    while ((*ptr == ' ' || *ptr == EOL) && *ptr++ != EOS);
1.18      cvs       585:    strcpy (tempOrgName, ptr);
                    586:    /* clean trailing white space */
                    587:    ptr = strchr (tempOrgName, ' ');
                    588:    if (ptr)
                    589:       *ptr = EOS;
                    590:    /* clean trailing new lines */
1.19      cvs       591:    ptr = strchr (tempOrgName, EOL);
1.5       cvs       592:    if (ptr)
                    593:       *ptr = EOS;
                    594: 
1.18      cvs       595:    /*
1.31      cvs       596:     * Make orgName a complete URL
                    597:     * If the URL does not include a protocol, then try to calculate
                    598:     * one using the doc's base element (if it exists),
                    599:     */
1.21      cvs       600:    if (tempOrgName[0] == EOS)
                    601:      {
                    602:        newName[0] = EOS;
                    603:        return;
                    604:      }
                    605:    else if (IsW3Path (tempOrgName))
                    606:      {
                    607:        /* the name is complete, go to the Sixth Step */
                    608:        strcpy (newName, tempOrgName);
                    609:        /* verify if the URL has the form "protocol://server:port" */
1.25      cvs       610:        ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST |
                    611:                      AMAYA_PARSE_PUNCTUATION);
1.21      cvs       612:        if (ptr && !strcmp (ptr, newName))
                    613:         {
1.29      cvs       614:           /* it has this form, we complete it by adding a DIR_STR  */
1.33      cvs       615:           strcat (newName, used_str);
1.21      cvs       616:         }
                    617:        if (ptr)
1.25      cvs       618:         TtaFreeMemory (ptr);
1.21      cvs       619:      }
                    620:    else if ( doc == 0)
1.19      cvs       621:      /* the name is complete, go to the Sixth Step */
1.18      cvs       622:      strcpy (newName, tempOrgName);
1.5       cvs       623:    else
                    624:      {
1.22      cvs       625:      
1.31      cvs       626:        /* Calculate the absolute URL, using the base or document URL */
1.32      cvs       627: 
1.25      cvs       628:        ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.31      cvs       629:        TtaFreeMemory (basename);
1.14      cvs       630:        if (ptr)
                    631:         {
1.29      cvs       632:           SimplifyUrl (&ptr);
1.14      cvs       633:           strcpy (newName, ptr);
1.25      cvs       634:           TtaFreeMemory (ptr);
1.14      cvs       635:         }
                    636:        else
1.18      cvs       637:           newName[0] = EOS;
1.5       cvs       638:      }
                    639: 
1.18      cvs       640:    /*
1.31      cvs       641:     * Prepare the docname that will refer to this ressource in the
                    642:     * .amaya directory. If the new URL finishes on DIR_SEP, then use
                    643:     * noname.html as a default ressource name
1.18      cvs       644:    */
1.19      cvs       645:    if (newName[0] != EOS)
1.5       cvs       646:      {
1.19      cvs       647:        length = strlen (newName) - 1;
1.33      cvs       648:        if (newName[length] == used_sep)
1.18      cvs       649:         {
                    650:           /* docname was not comprised inside the URL, so let's */
                    651:           /* assign the default ressource name */
                    652:           strcpy (docName, "noname.html");
                    653:           /* remove DIR_SEP at the end of complete path */
1.23      cvs       654:           /* newName[length] = EOS; */
1.18      cvs       655:         }
1.14      cvs       656:        else
1.18      cvs       657:         {
                    658:           /* docname is comprised inside the URL */
1.33      cvs       659:           while (length >= 0  && newName[length] != used_sep)
1.18      cvs       660:             length--;
                    661:           if (length < 0)
                    662:             strcpy (docName, newName);
                    663:           else
                    664:             strcpy (docName, &newName[length+1]);
                    665:         }
1.19      cvs       666: 
1.5       cvs       667:      }
1.18      cvs       668:    else
                    669:      docName[0] = EOS;
                    670: } 
1.3       cvs       671: 
1.4       cvs       672: /*----------------------------------------------------------------------
1.9       cvs       673:   IsSameHost                                                         
1.4       cvs       674:   ----------------------------------------------------------------------*/
1.3       cvs       675: #ifdef __STDC__
1.34    ! cvs       676: boolean             IsSameHost (const char *url1, const char *url2)
1.3       cvs       677: #else  /* __STDC__ */
                    678: boolean             IsSameHost (url1, url2)
1.34    ! cvs       679: const char         *url1;
        !           680: const char         *url2;
1.3       cvs       681: #endif /* __STDC__ */
                    682: {
1.34    ! cvs       683:    char            *basename_ptr1, *basename_ptr2;
        !           684:    boolean          result;
1.3       cvs       685: 
1.25      cvs       686:    basename_ptr1 = AmayaParseUrl (url1, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
                    687:    basename_ptr2 = AmayaParseUrl (url2, "", AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3       cvs       688: 
1.5       cvs       689:    if (strcmp (basename_ptr1, basename_ptr2))
1.8       cvs       690:       result = FALSE;
1.5       cvs       691:    else
1.8       cvs       692:       result = TRUE;
1.3       cvs       693: 
1.25      cvs       694:    TtaFreeMemory (basename_ptr1);
                    695:    TtaFreeMemory (basename_ptr2);
1.5       cvs       696:    return (result);
1.3       cvs       697: }
                    698: 
                    699: 
1.4       cvs       700: /*----------------------------------------------------------------------
1.22      cvs       701:   HasKnownFileSuffix
                    702:   returns TRUE if path points to a file ending with a suffix.
                    703:   ----------------------------------------------------------------------*/
                    704: #ifdef __STDC__
1.34    ! cvs       705: boolean             HasKnownFileSuffix (const char *path)
1.22      cvs       706: #else  /* __STDC__ */
                    707: boolean             HasKnownFileSuffix (path)
1.34    ! cvs       708: const char         *path;
1.22      cvs       709: #endif /* __STDC__ */
                    710: {
1.29      cvs       711:    char            *root;
                    712:    char             temppath[MAX_LENGTH];
                    713:    char             suffix[MAX_LENGTH];
1.22      cvs       714: 
1.24      cvs       715:    if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22      cvs       716:      return (FALSE);
                    717: 
1.29      cvs       718:    root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22      cvs       719: 
                    720:    if (root) 
                    721:      {
                    722:        strcpy (temppath, root);
1.25      cvs       723:        TtaFreeMemory (root);
1.22      cvs       724:        /* Get the suffix */
                    725:        ExtractSuffix (temppath, suffix); 
                    726: 
                    727:        if( suffix[0] == EOS)
                    728:         /* no suffix */
                    729:         return (FALSE);
                    730: 
                    731:        /* Normalize the suffix */
                    732:        ConvertToLowerCase (suffix);
                    733: 
1.23      cvs       734:        if (!strcmp (suffix, "gz"))
1.22      cvs       735:         /* skip the compressed suffix */
                    736:         {
                    737:         ExtractSuffix (temppath, suffix);
                    738:         if(suffix[0] == EOS)
                    739:           /* no suffix */
                    740:           return (FALSE);
                    741:          /* Normalize the suffix */
                    742:          ConvertToLowerCase (suffix);
                    743:         }
                    744: 
                    745:        if ((strcmp (suffix, "gif")) && (strcmp (suffix, "xbm")) &&
                    746:           (strcmp (suffix, "xpm")) && (strcmp (suffix, "jpg")) &&
                    747:           (strcmp (suffix, "pdf")) && (strcmp (suffix, "png")) &&
                    748:           (strcmp (suffix, "tgz")) && (strcmp (suffix, "xpg")) &&
                    749:           (strcmp (suffix, "xpd")) && (strcmp (suffix, "ps")) &&
                    750:           (strcmp (suffix, "au"))  && (strcmp (suffix, "html")) &&
                    751:           (strcmp (suffix, "htm")) && (strcmp (suffix, "shtml")) &&
                    752:           (strcmp (suffix, "txt")) && (strcmp (suffix, "css")) &&
                    753:           (strcmp (suffix, "eps")))
                    754:         return (FALSE);
                    755:        else
                    756:         return (TRUE);
                    757:      }
                    758:    else
                    759:      return (FALSE);
                    760: }
                    761: 
                    762: 
                    763: /*----------------------------------------------------------------------
1.24      cvs       764:   ChopURL
                    765:   Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL). 
                    766:   If inputURL is  bigger than that size, outputURL receives
                    767:   MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...", 
                    768:   and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
                    769:   If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
                    770:   copied into outputURL. 
                    771:   N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
                    772:   chars.
                    773:   ----------------------------------------------------------------------*/
                    774: #ifdef __STDC__
1.34    ! cvs       775: void ChopURL (char *outputURL, const char *inputURL)
1.24      cvs       776: #else
                    777: void ChopURL (outputURL, inputURL)
1.34    ! cvs       778: char       *outputURL;
        !           779: const char *inputURL;
1.24      cvs       780: #endif
1.22      cvs       781: 
1.24      cvs       782: {
                    783:   int len;
1.9       cvs       784: 
1.24      cvs       785:   len = strlen (inputURL);
                    786:   if (len <= MAX_PRINT_URL_LENGTH) 
1.29      cvs       787:     strcpy (outputURL, inputURL);
1.24      cvs       788:   else
                    789:     /* make a truncated urlName on the status window */
                    790:     {
                    791:       strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
                    792:       outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
                    793:       strcat (outputURL, "...");
                    794:       strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
                    795:     }
1.25      cvs       796: }
                    797: 
                    798: 
                    799: /*----------------------------------------------------------------------
                    800:    scan
                    801:        Scan a filename for its consituents
                    802:        -----------------------------------
                    803:   
                    804:    On entry,
                    805:        name    points to a document name which may be incomplete.
                    806:    On exit,
                    807:         absolute or relative may be nonzero (but not both).
                    808:        host, fragment and access may be nonzero if they were specified.
                    809:        Any which are nonzero point to zero terminated strings.
                    810:   ----------------------------------------------------------------------*/
                    811: #ifdef __STDC__
                    812: static void scan (char * name, HTURI * parts)
                    813: #else  /* __STDC__ */
                    814: static void scan (name, parts)
                    815: char                *name;
                    816: HTURI               *parts;
                    817: 
                    818: #endif /* __STDC__ */
                    819: {
1.28      cvs       820:   char * p;
                    821:   char * after_access = name;
1.33      cvs       822:   char       used_sep;
1.32      cvs       823:   char       my_path_sep;
                    824: 
1.33      cvs       825:   if (name && strchr (name, URL_SEP))
                    826:     {
                    827:       used_sep = URL_SEP;
                    828:       my_path_sep = ':';
                    829:     }
                    830:   else
                    831:     {
                    832:       used_sep = DIR_SEP;
                    833:       my_path_sep = ':';
                    834:     }
1.32      cvs       835: 
1.28      cvs       836:   memset(parts, '\0', sizeof(HTURI));
                    837:   /* Look for fragment identifier */
                    838:   if ((p = strrchr(name, '#')) != NULL)
                    839:     {
                    840:       *p++ = '\0';
                    841:       parts->fragment = p;
1.25      cvs       842:     }
                    843:     
1.28      cvs       844:   for (p=name; *p; p++)
                    845:     {
1.33      cvs       846:       if (*p==used_sep || *p=='#' || *p=='?')
1.28      cvs       847:        break;
1.32      cvs       848:       if (*p==':')
1.28      cvs       849:        {
                    850:          *p = 0;
                    851:          parts->access = after_access; /* Scheme has been specified */
                    852: 
                    853:          /* The combination of gcc, the "-O" flag and the HP platform is
                    854:             unhealthy. The following three lines is a quick & dirty fix, but is
                    855:             not recommended. Rather, turn off "-O". */
                    856: 
                    857:          /*            after_access = p;*/
                    858:          /*            while (*after_access == 0)*/
                    859:          /*                after_access++;*/
                    860:          after_access = p+1;
                    861:          if (0==strcasecmp("URL", parts->access))
                    862:            /* Ignore IETF's URL: pre-prefix */
                    863:            parts->access = NULL;
                    864:          else
1.25      cvs       865:            break;
                    866:        }
                    867:     }
                    868:     
                    869:     p = after_access;
1.33      cvs       870:     if (*p==used_sep)
1.28      cvs       871:       {
1.33      cvs       872:        if (p[1]==used_sep)
1.28      cvs       873:          {
1.25      cvs       874:            parts->host = p+2;          /* host has been specified      */
1.28      cvs       875:            *p = 0;                     /* Terminate access             */
                    876:            /* look for end of host name if any */
1.33      cvs       877:            p = strchr(parts->host,used_sep);
1.28      cvs       878:            if (p)
                    879:              {
1.25      cvs       880:                *p=0;                   /* Terminate host */
                    881:                parts->absolute = p+1;          /* Root has been found */
1.28      cvs       882:              }
                    883:          }
                    884:        else
                    885:          /* Root found but no host */
                    886:          parts->absolute = p+1;
                    887:       }
                    888:     else
                    889:       {
1.25      cvs       890:         parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28      cvs       891:       }
1.25      cvs       892: }
                    893: 
                    894: 
                    895: /*----------------------------------------------------------------------
1.28      cvs       896:   AmayaParseUrl: parse a Name relative to another name
                    897: 
                    898:   This returns those parts of a name which are given (and requested)
                    899:   substituting bits from the related name where necessary.
1.25      cvs       900:   
1.28      cvs       901:   On entry,
1.25      cvs       902:        aName           A filename given
                    903:         relatedName     A name relative to which aName is to be parsed. Give
                    904:                         it an empty string if aName is absolute.
                    905:         wanted          A mask for the bits which are wanted.
                    906:   
1.28      cvs       907:   On exit,
1.25      cvs       908:        returns         A pointer to a malloc'd string which MUST BE FREED
                    909:   ----------------------------------------------------------------------*/
                    910: #ifdef __STDC__
1.34    ! cvs       911: char          *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
1.25      cvs       912: #else  /* __STDC__ */
1.28      cvs       913: char          *AmayaParseUrl (aName, relatedName, wanted)
1.34    ! cvs       914: const char    *aName;
1.28      cvs       915: char          *relatedName;
                    916: int            wanted;
1.25      cvs       917: 
                    918: #endif /* __STDC__ */
                    919: {
1.29      cvs       920:   char      *return_value;
                    921:   char       result[MAX_LENGTH];
                    922:   char       name[MAX_LENGTH];
                    923:   char       rel[MAX_LENGTH];
                    924:   char      *p, *access;
                    925:   HTURI      given, related;
                    926:   int        len;
1.33      cvs       927:   char       used_sep;
                    928:   char      *used_str;
1.32      cvs       929: 
1.33      cvs       930:   if (strchr (aName, URL_SEP) || strchr (relatedName, URL_SEP) )
                    931:     {
                    932:       used_str = URL_STR;
                    933:       used_sep = URL_SEP;
                    934:     }
1.32      cvs       935:   else
1.33      cvs       936:     {
                    937:       used_str = DIR_STR;
                    938:       used_sep = DIR_SEP;
                    939:     }
1.32      cvs       940: 
1.29      cvs       941:   /* Make working copies of input strings to cut up: */
                    942:   return_value = NULL;
                    943:   result[0] = 0;               /* Clear string  */
                    944:   strcpy (name, aName);
                    945:   if (relatedName != NULL)  
                    946:     strcpy (rel, relatedName);
                    947:   else
                    948:     relatedName[0] = EOS;
                    949:   
                    950:   scan (name, &given);
                    951:   scan (rel,  &related); 
                    952:   access = given.access ? given.access : related.access;
                    953:   if (wanted & AMAYA_PARSE_ACCESS)
                    954:     if (access)
                    955:       {
                    956:        strcat (result, access);
                    957:        if(wanted & AMAYA_PARSE_PUNCTUATION)
1.32      cvs       958:                strcat (result, ":");
1.29      cvs       959:       }
                    960:   
                    961:   if (given.access && related.access)
                    962:     /* If different, inherit nothing. */
                    963:     if (strcmp (given.access, related.access) != 0)
                    964:       {
                    965:        related.host = 0;
                    966:        related.absolute = 0;
                    967:        related.relative = 0;
                    968:        related.fragment = 0;
                    969:       }
                    970:   
                    971:   if (wanted & AMAYA_PARSE_HOST)
                    972:     if(given.host || related.host)
                    973:       {
                    974:        if(wanted & AMAYA_PARSE_PUNCTUATION)
                    975:          strcat (result, "//");
                    976:        strcat (result, given.host ? given.host : related.host);
                    977:       }
                    978:   
                    979:   if (given.host && related.host)
                    980:     /* If different hosts, inherit no path. */
                    981:     if (strcmp(given.host, related.host) != 0)
                    982:       {
                    983:        related.absolute = 0;
                    984:        related.relative = 0;
                    985:        related.fragment = 0;
                    986:       }
                    987:   
                    988:   if (wanted & AMAYA_PARSE_PATH)
                    989:     {
                    990:       if (given.absolute)
                    991:        {
                    992:          /* All is given */
                    993:          if (wanted & AMAYA_PARSE_PUNCTUATION)
1.33      cvs       994:            strcat (result, used_str);
1.29      cvs       995:          strcat (result, given.absolute);
1.25      cvs       996:        }
1.29      cvs       997:       else if (related.absolute)
                    998:        {
                    999:          /* Adopt path not name */
1.33      cvs      1000:          strcat (result, used_str);
1.29      cvs      1001:          strcat (result, related.absolute);
                   1002:          if (given.relative)
                   1003:            {
                   1004:              /* Search part? */
                   1005:              p = strchr (result, '?');
                   1006:              if (!p)
                   1007:                p=result+strlen(result)-1;
1.33      cvs      1008:              for (; *p!=used_sep; p--);        /* last / */
1.29      cvs      1009:              /* Remove filename */
                   1010:              p[1]=0;
                   1011:              /* Add given one */
                   1012:              strcat (result, given.relative);
                   1013:              /*SimplifyUrl (&result);*/
1.25      cvs      1014:            }
                   1015:        }
1.29      cvs      1016:       else if (given.relative)
                   1017:        /* what we've got */
                   1018:        strcat (result, given.relative);
                   1019:       else if (related.relative)
                   1020:        strcat (result, related.relative);
                   1021:       else
                   1022:        /* No inheritance */
1.33      cvs      1023:        strcat (result, used_str);
1.25      cvs      1024:     }
1.29      cvs      1025:   
                   1026:   if (wanted & AMAYA_PARSE_ANCHOR)
                   1027:     if (given.fragment || related.fragment)
                   1028:       {
                   1029:        if (given.absolute && given.fragment)
                   1030:          {
                   1031:            /*Fixes for relURLs...*/
                   1032:            if (wanted & AMAYA_PARSE_PUNCTUATION)
                   1033:              strcat (result, "#");
                   1034:            strcat (result, given.fragment); 
                   1035:          }
                   1036:        else if (!(given.absolute) && !(given.fragment))
                   1037:          strcat (result, "");
                   1038:        else
                   1039:          {
                   1040:            if (wanted & AMAYA_PARSE_PUNCTUATION)
                   1041:              strcat (result, "#");
                   1042:            strcat (result, given.fragment ? given.fragment : related.fragment); 
                   1043:          }
                   1044:       }
                   1045:   len = strlen (result);
                   1046:   if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1047:     strcpy (return_value, result);
                   1048:   return (return_value);               /* exactly the right length */
1.25      cvs      1049: }
                   1050: 
                   1051: /*----------------------------------------------------------------------
                   1052:      HTCanon
                   1053:        Canonicalizes the URL in the following manner starting from the host
                   1054:        pointer:
                   1055:   
                   1056:        1) The host name is converted to lowercase
                   1057:        2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
                   1058:   
                   1059:        Return: OK      The position of the current path part of the URL
                   1060:                        which might be the old one or a new one.
                   1061:   
                   1062:   ----------------------------------------------------------------------*/
                   1063: #ifdef __STDC__
1.28      cvs      1064: static char *HTCanon (char ** filename, char * host)
1.25      cvs      1065: #else  /* __STDC__ */
1.28      cvs      1066: static char *HTCanon (filename, host)
                   1067: char       **filename;
                   1068: char        *host;
1.25      cvs      1069: #endif /* __STDC__ */
                   1070: {
                   1071:     char *newname = NULL;
                   1072:     char *port;
                   1073:     char *strptr;
                   1074:     char *path;
                   1075:     char *access = host-3;
1.33      cvs      1076:     char       used_sep;
                   1077:     char      *used_str;
1.32      cvs      1078: 
                   1079:   
1.33      cvs      1080:      if (*filename && strchr (*filename, URL_SEP))
                   1081:        {
                   1082:         used_str = URL_STR;
                   1083:         used_sep = URL_SEP;
                   1084:        }
                   1085:      else
                   1086:        {
                   1087:         used_str = DIR_STR;
                   1088:         used_sep = DIR_SEP;
                   1089:        }
1.32      cvs      1090:   
1.33      cvs      1091:     while (access>*filename && *(access-1)!= used_sep)       /* Find access method */
1.25      cvs      1092:        access--;
1.33      cvs      1093:     if ((path = strchr(host, used_sep)) == NULL)                       /* Find path */
1.25      cvs      1094:        path = host + strlen(host);
                   1095:     if ((strptr = strchr(host, '@')) != NULL && strptr<path)      /* UserId */
                   1096:        host = strptr;
1.32      cvs      1097:     if ((port = strchr(host, ':')) != NULL && port>path)      /* Port number */
1.25      cvs      1098:        port = NULL;
                   1099: 
                   1100:     strptr = host;                                 /* Convert to lower-case */
1.33      cvs      1101:     while (strptr<path)
                   1102:       {
1.25      cvs      1103:        *strptr = tolower(*strptr);
                   1104:        strptr++;
1.33      cvs      1105:       }
1.25      cvs      1106:     
                   1107:     /* Does the URL contain a full domain name? This also works for a
                   1108:        numerical host name. The domain name is already made lower-case
                   1109:        and without a trailing dot. */
                   1110:     {
1.33      cvs      1111:       char *dot = port ? port : path;
                   1112:       if (dot > *filename && *--dot=='.')
                   1113:        {
                   1114:          char *orig=dot, *dest=dot+1;
                   1115:          while((*orig++ = *dest++));
                   1116:          if (port) port--;
                   1117:          path--;
1.25      cvs      1118:        }
                   1119:     }
                   1120:     /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33      cvs      1121:     if (port)
                   1122:       {
                   1123:        if (!*(port+1) || *(port+1)==used_sep)
                   1124:          {
                   1125:            if (!newname)
                   1126:              {
1.25      cvs      1127:                char *orig=port, *dest=port+1;
                   1128:                while((*orig++ = *dest++));
1.33      cvs      1129:              }
                   1130:          }
                   1131:        else if ((!strncmp(access, "http", 4) &&
                   1132:                  (*(port+1)=='8'&&*(port+2)=='0'&&(*(port+3)==used_sep||!*(port+3)))) ||
                   1133:                 (!strncmp(access, "gopher", 6) &&
                   1134:                  (*(port+1)=='7'&&*(port+2)=='0'&&(*(port+3)==used_sep||!*(port+3)))) ||
                   1135:                 (!strncmp(access, "ftp", 3) &&
                   1136:                  (*(port+1)=='2'&&*(port+2)=='1'&&(*(port+3)==used_sep||!*(port+3))))) {
                   1137:          if (!newname)
                   1138:            {
                   1139:              char *orig=port, *dest=port+3;
                   1140:              while((*orig++ = *dest++));
                   1141:              /* Update path position, Henry Minsky */
                   1142:              path -= 3;
1.25      cvs      1143:            }
1.33      cvs      1144:        }
                   1145:        else if (newname)
                   1146:          strncat(newname, port, (int) (path-port));
                   1147:       }
1.25      cvs      1148: 
1.33      cvs      1149:     if (newname)
                   1150:       {
1.25      cvs      1151:        char *newpath = newname+strlen(newname);
                   1152:        strcat(newname, path);
                   1153:        path = newpath;
1.28      cvs      1154:        /* Free old copy */
                   1155:        TtaFreeMemory(*filename);
1.25      cvs      1156:        *filename = newname;
1.33      cvs      1157:       }
1.25      cvs      1158:     return path;
                   1159: }
                   1160: 
                   1161: 
                   1162: /*----------------------------------------------------------------------
1.29      cvs      1163:   SimplifyUrl: simplify a URI
1.32      cvs      1164:   A URI is allowed to contain the sequence xxx/../ which may be
                   1165:   replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28      cvs      1166:   Simplification helps us recognize duplicate URIs. 
1.25      cvs      1167:   
1.28      cvs      1168:   Thus,        /etc/junk/../fred       becomes /etc/fred
                   1169:                 /etc/junk/./fred       becomes /etc/junk/fred
1.25      cvs      1170:   
1.28      cvs      1171:   but we should NOT change
                   1172:                 http://fred.xxx.edu/../..
1.25      cvs      1173:   
                   1174:        or      ../../albert.html
                   1175:   
1.28      cvs      1176:   In order to avoid empty URLs the following URLs become:
1.25      cvs      1177:   
                   1178:                /fred/..                becomes /fred/..
                   1179:                /fred/././..            becomes /fred/..
                   1180:                /fred/.././junk/.././   becomes /fred/..
                   1181:   
1.28      cvs      1182:   If more than one set of `://' is found (several proxies in cascade) then
                   1183:   only the part after the last `://' is simplified.
1.25      cvs      1184:   
1.28      cvs      1185:   Returns: A string which might be the old one or a new one.
1.25      cvs      1186:   ----------------------------------------------------------------------*/
                   1187: #ifdef __STDC__
1.29      cvs      1188: void         SimplifyUrl (char ** url)
1.25      cvs      1189: #else  /* __STDC__ */
1.29      cvs      1190: void         SimplifyUrl (url)
1.28      cvs      1191: char        **url;
1.25      cvs      1192: #endif /* __STDC__ */
                   1193: {
1.28      cvs      1194:   char *path, *p;
                   1195:   char *newptr, *access;
                   1196:   char *orig, *dest, *end;
                   1197: 
1.33      cvs      1198:   char       used_sep;
                   1199:   char      *used_str;
1.32      cvs      1200: 
                   1201: 
1.28      cvs      1202:   if (!url || !*url)
                   1203:     return;
                   1204: 
1.33      cvs      1205:   if (strchr (*url, URL_SEP))
                   1206:     {
                   1207:       used_str = URL_STR;
                   1208:       used_sep = URL_SEP;
                   1209:     }
1.32      cvs      1210:   else
1.33      cvs      1211:     {
                   1212:       used_str = DIR_STR;
                   1213:       used_sep = DIR_SEP;
                   1214:     }
1.32      cvs      1215: 
1.28      cvs      1216:   /* Find any scheme name */
                   1217:   if ((path = strstr(*url, "://")) != NULL)
1.33      cvs      1218:     {
                   1219:       /* Find host name */
1.28      cvs      1220:       access = *url;
                   1221:       while (access<path && (*access=tolower(*access)))
                   1222:        access++;
                   1223:       path += 3;
                   1224:       while ((newptr = strstr(path, "://")) != NULL)
                   1225:         /* For proxies */
                   1226:        path = newptr+3;
                   1227:       /* We have a host name */
                   1228:       path = HTCanon(url, path);
1.25      cvs      1229:     }
1.28      cvs      1230:   else if ((path = strstr(*url, ":/")) != NULL)
                   1231:     path += 2;
                   1232:   else
                   1233:     path = *url;
1.25      cvs      1234: 
1.33      cvs      1235:   if (*path == used_sep && *(path+1)==used_sep)
1.28      cvs      1236:     /* Some URLs start //<foo> */
                   1237:     path += 1;
                   1238:   else if (!strncmp(path, "news:", 5))
                   1239:     {
                   1240:       newptr = strchr(path+5, '@');
                   1241:       if (!newptr)
                   1242:        newptr = path + 5;
                   1243:       while (*newptr)
                   1244:        {
                   1245:          /* Make group or host lower case */
                   1246:          *newptr = tolower (*newptr);
                   1247:          newptr++;
1.25      cvs      1248:        }
1.28      cvs      1249:       /* Doesn't need to do any more */
                   1250:       return;
1.25      cvs      1251:     }
1.28      cvs      1252: 
                   1253:   if ((p = path))
                   1254:     {
                   1255:       if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
                   1256:            (end = strchr (path, '#'))))
                   1257:        end = path + strlen (path);
                   1258:       
                   1259:       /* Parse string second time to simplify */
                   1260:       p = path;
                   1261:       while (p < end)
                   1262:        {
1.33      cvs      1263:          if (*p==used_sep)
1.28      cvs      1264:            {
1.33      cvs      1265:              if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28      cvs      1266:                {
                   1267:                  orig = p + 1;
1.33      cvs      1268:                  dest = (*(p+2)!=used_sep) ? p+2 : p+3;
1.28      cvs      1269:                  while ((*orig++ = *dest++)); /* Remove a slash and a dot */
                   1270:                  end = orig - 1;
                   1271:                }
1.33      cvs      1272:              else if (*(p+1)=='.' && *(p+2)=='.' && (*(p+3)==used_sep || !*(p+3)))
1.28      cvs      1273:                {
                   1274:                  newptr = p;
1.33      cvs      1275:                  while (newptr>path && *--newptr!=used_sep); /* prev slash */
1.28      cvs      1276:                  if (strncmp(newptr, "/../", 4))
                   1277:                    {
                   1278:                      orig = newptr + 1;
1.33      cvs      1279:                      dest = (*(p+3)!=used_sep) ? p+3 : p+4;
1.28      cvs      1280:                      while ((*orig++ = *dest++)); /* Remove /xxx/.. */
                   1281:                      end = orig-1;
                   1282:                      /* Start again with prev slash */
                   1283:                      p = newptr;
1.25      cvs      1284:                    }
1.28      cvs      1285:                  else
1.25      cvs      1286:                    p++;
1.28      cvs      1287:                }
1.33      cvs      1288:              else if (*(p+1) == used_sep)
1.28      cvs      1289:                {
1.33      cvs      1290:                  while (*(p+1) == used_sep)
1.28      cvs      1291:                    {
                   1292:                      orig = p;
                   1293:                      dest = p + 1;
                   1294:                      while ((*orig++ = *dest++));  /* Remove multiple /'s */
                   1295:                      end = orig-1;
                   1296:                    }
                   1297:                }
                   1298:              else
1.25      cvs      1299:                p++;
1.28      cvs      1300:            }
                   1301:          else
                   1302:            p++;
1.25      cvs      1303:        }
                   1304:     }
1.28      cvs      1305:   return;
                   1306: }
                   1307: 
                   1308: 
                   1309: /*----------------------------------------------------------------------
                   1310:    NormalizeFile normalizes  local names.                             
                   1311:    Return TRUE if target and src differ.                           
                   1312:   ----------------------------------------------------------------------*/
                   1313: #ifdef __STDC__
                   1314: boolean             NormalizeFile (char *src, char *target)
                   1315: #else
                   1316: boolean             NormalizeFile (src, target)
                   1317: char               *src;
                   1318: char               *target;
                   1319: 
                   1320: #endif
                   1321: {
                   1322:    char               *s;
                   1323:    boolean             change;
                   1324: 
                   1325:    change = FALSE;
                   1326:    if (src[0] == '~')
                   1327:      {
                   1328:        /* replace ~ */
                   1329:        s = (char *) TtaGetEnvString ("HOME");
                   1330:        strcpy (target, s);
                   1331:        strcat (target, &src[1]);
                   1332:        change = TRUE;
                   1333:      }
                   1334:    else if (strncmp (src, "file:", 5) == 0)
                   1335:      {
                   1336:        /* remove the prefix file: */
                   1337:        if (src[5] == EOS)
                   1338:           strcpy (target, DIR_STR);
                   1339:        else if (src[0] == '~')
                   1340:          {
                   1341:            /* replace ~ */
                   1342:            s = (char *) TtaGetEnvString ("HOME");
                   1343:            strcpy (target, s);
                   1344:            strcat (target, &src[5]);
                   1345:          }
                   1346:        else
                   1347:           strcpy (target, &src[5]);
                   1348:        change = TRUE;
                   1349:      }
                   1350:    else
                   1351:       strcpy (target, src);
                   1352: 
                   1353:    /* remove /../ and /./ */
1.29      cvs      1354:    SimplifyUrl (&target);
1.28      cvs      1355:    return (change);
1.25      cvs      1356: }
                   1357: 
1.28      cvs      1358: 
1.25      cvs      1359: /*----------------------------------------------------------------------
1.31      cvs      1360:   MakeRelativeURL: make relative name
1.25      cvs      1361:   
1.28      cvs      1362:   This function creates and returns a string which gives an expression of
                   1363:   one address as related to another. Where there is no relation, an absolute
                   1364:   address is retured.
1.25      cvs      1365:   
1.28      cvs      1366:   On entry,
1.25      cvs      1367:        Both names must be absolute, fully qualified names of nodes
                   1368:        (no fragment bits)
                   1369:   
1.28      cvs      1370:   On exit,
1.25      cvs      1371:        The return result points to a newly allocated name which, if
                   1372:        parsed by AmayaParseUrl relative to relatedName, will yield aName.
                   1373:        The caller is responsible for freeing the resulting name later.
                   1374:   ----------------------------------------------------------------------*/
                   1375: #ifdef __STDC__
1.31      cvs      1376: char            *MakeRelativeURL (char *aName, char *relatedName)
1.25      cvs      1377: #else  /* __STDC__ */
1.31      cvs      1378: char            *MakeRelativeURL (aName, relatedName)
1.28      cvs      1379: char            *aName;
                   1380: char            *relatedName;
1.25      cvs      1381: #endif  /* __STDC__ */
                   1382: {
1.29      cvs      1383:   char      *return_value;
                   1384:   char       result[MAX_LENGTH];
                   1385:   char          *p;
1.34    ! cvs      1386:   char          *q;
1.29      cvs      1387:   char          *after_access;
                   1388:   char          *last_slash = NULL;
                   1389:   int            slashes, levels, len;
                   1390: 
                   1391:   if (aName == NULL || relatedName == NULL)
                   1392:     return (NULL);
                   1393: 
                   1394:   slashes = 0;
                   1395:   after_access = NULL;
                   1396:   p = aName;
                   1397:   q = relatedName;
                   1398:   for (; *p && (*p == *q); p++, q++)
1.27      cvs      1399:     {
                   1400:       /* Find extent of match */
1.32      cvs      1401:       if (*p == ':')
1.29      cvs      1402:        after_access = p + 1;
1.28      cvs      1403:       if (*p == DIR_SEP)
1.27      cvs      1404:        {
1.29      cvs      1405:          /* memorize the last slash position and count them */
1.27      cvs      1406:          last_slash = p;
                   1407:          slashes++;
1.25      cvs      1408:        }
                   1409:     }
                   1410:     
1.31      cvs      1411:   /* q, p point to the first non-matching character or zero */
                   1412:   if (*q == EOS)
                   1413:     {
                   1414:       /* New name is a subset of the related name */
                   1415:       /* exactly the right length */
                   1416:       len = strlen (p);
                   1417:       if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1418:        strcpy (return_value, p);
                   1419:     }
                   1420:   else if ((slashes < 2 && after_access == NULL)
                   1421:       || (slashes < 3 && after_access != NULL))
                   1422:     {
                   1423:       /* Two names whitout common path */
                   1424:       /* exactly the right length */
                   1425:       len = strlen (aName);
                   1426:       if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1427:        strcpy (return_value, aName);
                   1428:     }
                   1429:   else
                   1430:     {
                   1431:       /* Some path in common */
                   1432:       if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
                   1433:        /* just the same server */
                   1434:        strcpy (result, last_slash);
                   1435:       else
                   1436:        {
                   1437:          levels= 0; 
                   1438:          for (; *q && (*q != '#'); q++)
                   1439:            if (*q == DIR_SEP)
                   1440:              levels++;
                   1441:          
                   1442:          result[0] = 0;
                   1443:          for (;levels; levels--)
                   1444:            strcat (result, "../");
                   1445:          strcat (result, last_slash+1);
                   1446:        } 
                   1447:       
                   1448:       /* exactly the right length */
                   1449:       len = strlen (result);
                   1450:       if ((return_value = (char *) TtaGetMemory (len + 1)) != NULL)
                   1451:        strcpy (return_value, result);
1.25      cvs      1452:     }
1.29      cvs      1453:   return (return_value);
1.24      cvs      1454: }

Webmaster