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

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

Webmaster