Annotation of libwww/Library/src/HTAccess.c, revision 1.46

1.1       timbl       1: /*             Access Manager                                  HTAccess.c
                      2: **             ==============
                      3: **
                      4: ** Authors
                      5: **     TBL     Tim Berners-Lee timbl@info.cern.ch
1.4       timbl       6: **     JFG     Jean-Francois Groff jfg@dxcern.cern.ch
1.1       timbl       7: **     DD      Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
                      8: ** History
                      9: **       8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
                     10: **     26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
1.42      frystyk    11: **      6 Oct 92 Moved HTClientHost and HTlogfile into here. TBL
1.1       timbl      12: **     17 Dec 92 Tn3270 added, bug fix. DD
1.2       timbl      13: **      4 Feb 93 Access registration, Search escapes bad chars TBL
1.9       timbl      14: **               PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
                     15: **     28 May 93 WAIS gateway explicit if no WAIS library linked in.
1.19      timbl      16: **        Dec 93 Bug change around, more reentrant, etc
1.42      frystyk    17: **     09 May 94 logfile renamed to HTlogfile to avoid clash with WAIS
1.2       timbl      18: ** Bugs
                     19: **     This module assumes that that the graphic object is hypertext, as it
1.9       timbl      20: **     needs to select it when it has been loaded.  A superclass needs to be
1.2       timbl      21: **     defined which accepts select and select_anchor.
1.1       timbl      22: */
                     23: 
1.9       timbl      24: #ifndef DEFAULT_WAIS_GATEWAY
1.8       timbl      25: #define DEFAULT_WAIS_GATEWAY "http://info.cern.ch:8001/"
1.9       timbl      26: #endif
1.8       timbl      27: 
1.1       timbl      28: /* Implements:
                     29: */
                     30: #include "HTAccess.h"
                     31: 
                     32: /* Uses:
                     33: */
                     34: 
                     35: #include "HTParse.h"
                     36: #include "HTUtils.h"
1.4       timbl      37: #include "HTML.h"              /* SCW */
1.2       timbl      38: 
                     39: #ifndef NO_RULES
                     40: #include "HTRules.h"
                     41: #endif
                     42: 
1.1       timbl      43: #include <stdio.h>
                     44: 
1.2       timbl      45: #include "HTList.h"
                     46: #include "HText.h"     /* See bugs above */
                     47: #include "HTAlert.h"
1.17      timbl      48: #include "HTFWriter.h" /* for cache stuff */
                     49: #include "HTTee.h"
1.46    ! frystyk    50: #include "HTError.h"
1.2       timbl      51: 
1.1       timbl      52: /*     These flags may be set to modify the operation of this module
                     53: */
1.34      frystyk    54: PUBLIC char * HTCacheDir = 0;  /* Root for cached files or 0 for no cache */
                     55: PUBLIC char * HTSaveLocallyDir = SAVE_LOCALLY_HOME_DIR;        /* Save & exe files */
1.1       timbl      56: PUBLIC char * HTClientHost = 0;        /* Name of remote login host if any */
1.42      frystyk    57: PUBLIC FILE * HTlogfile = 0;   /* File to which to output one-liners */
1.41      luotonen   58: 
1.34      frystyk    59: PUBLIC BOOL HTForceReload = NO;        /* Force reload from cache or net */
1.12      timbl      60: PUBLIC BOOL HTSecure = NO;     /* Disable access for telnet users? */
1.27      luotonen   61: PUBLIC BOOL using_proxy = NO;  /* are we using a proxy gateway? */
1.43      luotonen   62: PUBLIC char * HTImServer = NULL;/* cern_httpd sets this to the translated URL*/
1.27      luotonen   63: PUBLIC BOOL HTImProxy = NO;    /* cern_httpd as a proxy? */
1.1       timbl      64: 
1.43      luotonen   65: 
1.2       timbl      66: /*     To generate other things, play with these:
                     67: */
                     68: 
1.15      timbl      69: /* PUBLIC HTFormat HTOutputFormat = NULL;      use request->output_format */
                     70: /* PUBLIC HTStream* HTOutputStream = NULL;     use request->output_stream */ 
1.1       timbl      71: 
                     72: PRIVATE HTList * protocols = NULL;   /* List of registered protocol descriptors */
                     73: 
1.24      timbl      74: /*     Superclass defn */
1.1       timbl      75: 
1.24      timbl      76: struct _HTStream {
                     77:        HTStreamClass * isa;
                     78:        /* ... */
                     79: };
                     80: 
1.15      timbl      81: /*     Create  a request structure
                     82: **     ---------------------------
                     83: */
                     84: 
                     85: PUBLIC HTRequest * HTRequest_new NOARGS
                     86: {
1.28      luotonen   87:     HTRequest * me = (HTRequest*) calloc(1, sizeof(*me));  /* zero fill */
1.15      timbl      88:     if (!me) outofmem(__FILE__, "HTRequest_new()");
                     89:     
1.20      luotonen   90:     me->conversions    = HTList_new(); /* No conversions registerd yet */
                     91:     me->output_format  = WWW_PRESENT;  /* default it to present to user */
                     92: 
1.15      timbl      93:     return me;
                     94: }
                     95: 
                     96: 
1.20      luotonen   97: /*     Delete a request structure
                     98: **     --------------------------
                     99: */
                    100: PUBLIC void HTRequest_delete ARGS1(HTRequest *, req)
                    101: {
                    102:     if (req) {
1.34      frystyk   103:        HTFormatDelete(req->conversions);
1.46    ! frystyk   104:        HTErrorFree(req);
1.34      frystyk   105:        HTAACleanup(req);
1.37      luotonen  106:        FREE(req->from);
1.34      frystyk   107:        FREE(req);
1.20      luotonen  108:     }
                    109: }
                    110: 
                    111: 
1.22      luotonen  112: PRIVATE char * method_names[(int)MAX_METHODS + 1] =
                    113: {
                    114:     "INVALID-METHOD",
                    115:     "GET",
                    116:     "HEAD",
                    117:     "POST",
                    118:     "PUT",
                    119:     "DELETE",
                    120:     "CHECKOUT",
                    121:     "CHECKIN",
                    122:     "SHOWMETHOD",
                    123:     "LINK",
                    124:     "UNLINK",
                    125:     NULL
                    126: };
                    127: 
                    128: /*     Get method enum value
                    129: **     ---------------------
                    130: */
                    131: PUBLIC HTMethod HTMethod_enum ARGS1(char *, name)
                    132: {
                    133:     if (name) {
                    134:        int i;
                    135:        for (i=1; i < (int)MAX_METHODS; i++)
                    136:            if (!strcmp(name, method_names[i]))
                    137:                return (HTMethod)i;
                    138:     }
                    139:     return METHOD_INVALID;
                    140: }
                    141: 
                    142: 
                    143: /*     Get method name
                    144: **     ---------------
                    145: */
                    146: PUBLIC char * HTMethod_name ARGS1(HTMethod, method)
                    147: {
                    148:     if ((int)method > (int)METHOD_INVALID  && 
                    149:        (int)method < (int)MAX_METHODS)
                    150:        return method_names[(int)method];
                    151:     else
                    152:        return method_names[(int)METHOD_INVALID];
                    153: }
                    154: 
                    155: 
                    156: /*     Is method in a list of method names?
                    157: **     -----------------------------------
                    158: */
                    159: PUBLIC BOOL HTMethod_inList ARGS2(HTMethod,    method,
                    160:                                  HTList *,     list)
                    161: {
                    162:     char * method_name = HTMethod_name(method);
                    163:     HTList *cur = list;
                    164:     char *item;
                    165: 
                    166:     while (NULL != (item = (char*)HTList_nextObject(cur))) {
                    167:        CTRACE(stderr, " %s", item);
                    168:        if (0==strcasecomp(item, method_name))
                    169:            return YES;
                    170:     }
                    171:     return NO; /* Not found */
                    172: }
                    173: 
                    174: 
                    175: 
                    176: 
1.20      luotonen  177: 
1.1       timbl     178: /*     Register a Protocol                             HTRegisterProtocol
                    179: **     -------------------
                    180: */
                    181: 
                    182: PUBLIC BOOL HTRegisterProtocol(protocol)
                    183:        HTProtocol * protocol;
                    184: {
                    185:     if (!protocols) protocols = HTList_new();
                    186:     HTList_addObject(protocols, protocol);
                    187:     return YES;
                    188: }
                    189: 
                    190: 
                    191: /*     Register all known protocols
                    192: **     ----------------------------
                    193: **
                    194: **     Add to or subtract from this list if you add or remove protocol modules.
                    195: **     This routine is called the first time the protocol list is needed,
                    196: **     unless any protocols are already registered, in which case it is not called.
                    197: **     Therefore the application can override this list.
                    198: **
                    199: **     Compiling with NO_INIT prevents all known protocols from being forced
                    200: **     in at link time.
                    201: */
                    202: #ifndef NO_INIT
                    203: PRIVATE void HTAccessInit NOARGS                       /* Call me once */
                    204: {
1.14      duns      205: GLOBALREF HTProtocol HTTP, HTFile, HTTelnet, HTTn3270, HTRlogin;
1.1       timbl     206: #ifndef DECNET
1.14      duns      207: GLOBALREF  HTProtocol HTFTP, HTNews, HTGopher;
1.42      frystyk   208: 
                    209: /* This is the replacement when HTWhoIs gets a complete protocol module */
                    210: /* GLOBALREF  HTProtocol HTFTP, HTNews, HTGopher, HTWhoIs;             */
                    211: /* -------------------------------------------------------------------- */
                    212: 
1.3       timbl     213: #ifdef DIRECT_WAIS
1.14      duns      214: GLOBALREF  HTProtocol HTWAIS;
1.3       timbl     215: #endif
1.2       timbl     216:     HTRegisterProtocol(&HTFTP);
                    217:     HTRegisterProtocol(&HTNews);
                    218:     HTRegisterProtocol(&HTGopher);
1.42      frystyk   219: 
                    220: /* This should be added when HTWhoIs gets a complete protocol module */
                    221: /*  HTRegisterProtocol(&HTWhoIs);                                   */
                    222: /* ----------------------------------------------------------------- */
1.3       timbl     223: #ifdef DIRECT_WAIS
                    224:     HTRegisterProtocol(&HTWAIS);
                    225: #endif
1.1       timbl     226: #endif
                    227: 
1.2       timbl     228:     HTRegisterProtocol(&HTTP);
                    229:     HTRegisterProtocol(&HTFile);
                    230:     HTRegisterProtocol(&HTTelnet);
                    231:     HTRegisterProtocol(&HTTn3270);
                    232:     HTRegisterProtocol(&HTRlogin);
1.1       timbl     233: }
                    234: #endif
                    235: 
                    236: 
1.33      luotonen  237: 
                    238: /*                                                     override_proxy()
                    239: **
                    240: **     Check the no_proxy environment variable to get the list
                    241: **     of hosts for which proxy server is not consulted.
                    242: **
                    243: **     no_proxy is a comma- or space-separated list of machine
                    244: **     or domain names, with optional :port part.  If no :port
                    245: **     part is present, it applies to all ports on that domain.
                    246: **
                    247: **     Example:
                    248: **             no_proxy="cern.ch,some.domain:8001"
                    249: **
                    250: */
                    251: PRIVATE BOOL override_proxy ARGS1(CONST char *, addr)
                    252: {
                    253:     CONST char * no_proxy = getenv("no_proxy");
                    254:     char * p = NULL;
                    255:     char * host = NULL;
                    256:     int port = 0;
                    257:     int h_len = 0;
                    258: 
                    259:     if (!no_proxy || !addr || !(host = HTParse(addr, "", PARSE_HOST)))
                    260:        return NO;
                    261:     if (!*host) { free(host); return NO; }
                    262: 
1.34      frystyk   263:     if ((p = strchr(host, ':')) != NULL) {     /* Port specified */
1.33      luotonen  264:        *p++ = 0;                       /* Chop off port */
                    265:        port = atoi(p);
                    266:     }
                    267:     else {                             /* Use default port */
                    268:        char * access = HTParse(addr, "", PARSE_ACCESS);
                    269:        if (access) {
                    270:            if      (!strcmp(access,"http"))    port = 80;
                    271:            else if (!strcmp(access,"gopher"))  port = 70;
                    272:            else if (!strcmp(access,"ftp"))     port = 21;
                    273:            free(access);
                    274:        }
                    275:     }
                    276:     if (!port) port = 80;              /* Default */
                    277:     h_len = strlen(host);
                    278: 
                    279:     while (*no_proxy) {
                    280:        CONST char * end;
                    281:        CONST char * colon = NULL;
                    282:        int templ_port = 0;
                    283:        int t_len;
                    284: 
                    285:        while (*no_proxy && (WHITE(*no_proxy) || *no_proxy==','))
                    286:            no_proxy++;                 /* Skip whitespace and separators */
                    287: 
                    288:        end = no_proxy;
                    289:        while (*end && !WHITE(*end) && *end != ',') {   /* Find separator */
                    290:            if (*end==':') colon = end;                 /* Port number given */
                    291:            end++;
                    292:        }
                    293: 
                    294:        if (colon) {
                    295:            templ_port = atoi(colon+1);
                    296:            t_len = colon - no_proxy;
                    297:        }
                    298:        else {
                    299:            t_len = end - no_proxy;
                    300:        }
                    301: 
                    302:        if ((!templ_port || templ_port == port)  &&
                    303:            (t_len > 0  &&  t_len <= h_len  &&
                    304:             !strncmp(host + h_len - t_len, no_proxy, t_len))) {
                    305:            free(host);
                    306:            return YES;
                    307:        }
                    308:        if (*end) no_proxy = end+1;
                    309:        else break;
                    310:     }
                    311: 
                    312:     free(host);
                    313:     return NO;
                    314: }
                    315: 
                    316: 
                    317: 
1.2       timbl     318: /*             Find physical name and access protocol
                    319: **             --------------------------------------
1.1       timbl     320: **
                    321: **
                    322: ** On entry,
                    323: **     addr            must point to the fully qualified hypertext reference.
                    324: **     anchor          a pareent anchor with whose address is addr
                    325: **
                    326: ** On exit,
1.2       timbl     327: **     returns         HT_NO_ACCESS            Error has occured.
                    328: **                     HT_OK                   Success
1.1       timbl     329: **
                    330: */
1.21      luotonen  331: PRIVATE int get_physical ARGS1(HTRequest *, req)
                    332: {    
1.1       timbl     333:     char * access=0;   /* Name of access method */
1.21      luotonen  334:     char * addr = HTAnchor_address((HTAnchor*)req->anchor);    /* free me */
1.27      luotonen  335: 
1.35      luotonen  336:     /*
                    337:     ** This HACK is here until we have redirection implemented.
                    338:     ** This is used when we are recursively calling HTLoad().
                    339:     ** We then take the physical address, because currently the
                    340:     ** virtual address is kept in a hash table so it can't be
                    341:     ** changed -- otherwise it wouldn't be found anymore.
                    342:     */
1.36      luotonen  343:     if (HTAnchor_physical(req->anchor))
                    344:        StrAllocCopy(addr, HTAnchor_physical(req->anchor));
1.35      luotonen  345: 
1.2       timbl     346: #ifndef NO_RULES
1.45      luotonen  347:     if (HTImServer)    /* cern_httpd has already done its own translations */
                    348:        HTAnchor_setPhysical(req->anchor, HTImServer);
                    349: #ifdef OLD_CODE
1.27      luotonen  350:        HTAnchor_setPhysical(req->anchor, addr);
1.45      luotonen  351: #endif
1.21      luotonen  352:     else {
1.27      luotonen  353:        char * physical = HTTranslate(addr);
1.21      luotonen  354:        if (!physical) {
                    355:            return HT_FORBIDDEN;
                    356:        }
                    357:        HTAnchor_setPhysical(req->anchor, physical);
                    358:        free(physical);                 /* free our copy */
1.2       timbl     359:     }
                    360: #else
1.21      luotonen  361:     HTAnchor_setPhysical(req->anchor, addr);
1.2       timbl     362: #endif
                    363: 
1.21      luotonen  364:     access =  HTParse(HTAnchor_physical(req->anchor),
1.27      luotonen  365:                      "file:", PARSE_ACCESS);
1.1       timbl     366: 
                    367: /*     Check whether gateway access has been set up for this
1.8       timbl     368: **
                    369: **     This function can be replaced by the rule system above.
1.1       timbl     370: */
1.8       timbl     371: #define USE_GATEWAYS
1.1       timbl     372: #ifdef USE_GATEWAYS
1.39      luotonen  373: 
                    374:     /* make sure the using_proxy variable is false */
                    375:     using_proxy = NO;
                    376: 
1.33      luotonen  377:     if (!override_proxy(addr)) {
1.27      luotonen  378:        char * gateway_parameter, *gateway, *proxy;
                    379: 
1.2       timbl     380:        gateway_parameter = (char *)malloc(strlen(access)+20);
                    381:        if (gateway_parameter == NULL) outofmem(__FILE__, "HTLoad");
1.27      luotonen  382: 
                    383:        /* search for proxy gateways */
1.2       timbl     384:        strcpy(gateway_parameter, "WWW_");
                    385:        strcat(gateway_parameter, access);
                    386:        strcat(gateway_parameter, "_GATEWAY");
                    387:        gateway = (char *)getenv(gateway_parameter); /* coerce for decstation */
1.27      luotonen  388: 
                    389:        /* search for proxy servers */
                    390:        strcpy(gateway_parameter, access);
                    391:        strcat(gateway_parameter, "_proxy");
                    392:        proxy = (char *)getenv(gateway_parameter);
                    393: 
1.2       timbl     394:        free(gateway_parameter);
1.27      luotonen  395: 
                    396:        if (TRACE && gateway)
                    397:            fprintf(stderr,"Gateway found: %s\n",gateway);
                    398:        if (TRACE && proxy)
                    399:            fprintf(stderr,"Proxy server found: %s\n",proxy);
                    400: 
1.8       timbl     401: #ifndef DIRECT_WAIS
1.9       timbl     402:        if (!gateway && 0==strcmp(access, "wais")) {
1.8       timbl     403:            gateway = DEFAULT_WAIS_GATEWAY;
                    404:        }
                    405: #endif
1.27      luotonen  406: 
                    407:        /* proxy servers have precedence over gateway servers */
                    408:        if (proxy) {
                    409:            char * gatewayed=0;
                    410: 
                    411:             StrAllocCopy(gatewayed,proxy);
                    412:            StrAllocCat(gatewayed,addr);
                    413:            using_proxy = YES;
                    414:            HTAnchor_setPhysical(req->anchor, gatewayed);
                    415:            free(gatewayed);
                    416:            free(access);
                    417: 
                    418:            access =  HTParse(HTAnchor_physical(req->anchor),
                    419:                              "http:", PARSE_ACCESS);
                    420:        } else if (gateway) {
1.9       timbl     421:            char * path = HTParse(addr, "",
                    422:                PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
                    423:                /* Chop leading / off to make host into part of path */
                    424:            char * gatewayed = HTParse(path+1, gateway, PARSE_ALL);
                    425:            free(path);
1.21      luotonen  426:             HTAnchor_setPhysical(req->anchor, gatewayed);
1.9       timbl     427:            free(gatewayed);
1.2       timbl     428:            free(access);
1.9       timbl     429:            
1.21      luotonen  430:            access =  HTParse(HTAnchor_physical(req->anchor),
1.8       timbl     431:                "http:", PARSE_ACCESS);
1.2       timbl     432:        }
                    433:     }
1.1       timbl     434: #endif
                    435: 
1.19      timbl     436:     free(addr);
1.1       timbl     437: 
                    438: 
                    439: /*     Search registered protocols to find suitable one
                    440: */
                    441:     {
1.20      luotonen  442:        HTList *cur;
                    443:        HTProtocol *p;
1.1       timbl     444: #ifndef NO_INIT
1.2       timbl     445:         if (!protocols) HTAccessInit();
1.1       timbl     446: #endif
1.20      luotonen  447:        cur = protocols;
                    448:        while ((p = (HTProtocol*)HTList_nextObject(cur))) {
1.2       timbl     449:            if (strcmp(p->name, access)==0) {
1.21      luotonen  450:                HTAnchor_setProtocol(req->anchor, p);
1.2       timbl     451:                free(access);
                    452:                return (HT_OK);
1.1       timbl     453:            }
                    454:        }
                    455:     }
                    456: 
                    457:     free(access);
1.2       timbl     458:     return HT_NO_ACCESS;
1.1       timbl     459: }
                    460: 
                    461: 
                    462: /*             Load a document
                    463: **             ---------------
                    464: **
1.2       timbl     465: **     This is an internal routine, which has an address AND a matching
                    466: **     anchor.  (The public routines are called with one OR the other.)
                    467: **
                    468: ** On entry,
1.15      timbl     469: **     request->
1.35      luotonen  470: **         anchor              a parent anchor with fully qualified
                    471: **                             hypertext reference as its address set
1.15      timbl     472: **         output_format       valid
                    473: **         output_stream       valid on NULL
1.2       timbl     474: **
                    475: ** On exit,
                    476: **     returns         <0              Error has occured.
                    477: **                     HT_LOADED       Success
                    478: **                     HT_NO_DATA      Success, but no document loaded.
1.8       timbl     479: **                                     (telnet sesssion started etc)
1.2       timbl     480: **
                    481: */
1.35      luotonen  482: PUBLIC int HTLoad ARGS1(HTRequest *, request)
1.2       timbl     483: {
1.25      frystyk   484:     char       *arg = NULL;
                    485:     HTProtocol *p;
                    486:     int        status;
                    487: 
1.22      luotonen  488:     if (request->method == METHOD_INVALID)
                    489:        request->method = METHOD_GET;
1.21      luotonen  490:     status = get_physical(request);
1.2       timbl     491:     if (status == HT_FORBIDDEN) {
1.21      luotonen  492:         return HTLoadError(request, 500,
                    493:                           "Access forbidden by rule");
1.2       timbl     494:     }
                    495:     if (status < 0) return status;     /* Can't resolve or forbidden */
1.25      frystyk   496: 
                    497:     if(!(arg = HTAnchor_physical(request->anchor)) || !*arg) 
                    498:        return (-1);
1.27      luotonen  499: 
1.15      timbl     500:     p = HTAnchor_protocol(request->anchor);
1.17      timbl     501:     return (*(p->load))(request);
1.2       timbl     502: }
                    503: 
                    504: 
                    505: /*             Get a save stream for a document
                    506: **             --------------------------------
                    507: */
1.19      timbl     508: PUBLIC HTStream *HTSaveStream ARGS1(HTRequest *, request)
1.15      timbl     509: {
                    510:     HTProtocol * p;
1.19      timbl     511:     int status;
1.22      luotonen  512:     request->method = METHOD_PUT;
1.21      luotonen  513:     status = get_physical(request);
1.19      timbl     514:     if (status == HT_FORBIDDEN) {
1.21      luotonen  515:         HTLoadError(request, 500,
                    516:                    "Access forbidden by rule");
1.19      timbl     517:        return NULL;    /* should return error status? */
                    518:     }
                    519:     if (status < 0) return NULL; /* @@ error. Can't resolve or forbidden */
                    520:     
1.15      timbl     521:     p = HTAnchor_protocol(request->anchor);
1.2       timbl     522:     if (!p) return NULL;
                    523:     
1.15      timbl     524:     return (*p->saveStream)(request);
1.2       timbl     525:     
                    526: }
                    527: 
                    528: 
                    529: /*             Load a document - with logging etc
                    530: **             ----------------------------------
                    531: **
                    532: **     - Checks or documents already loaded
                    533: **     - Logs the access
                    534: **     - Allows stdin filter option
                    535: **     - Trace ouput and error messages
                    536: **
1.1       timbl     537: **    On Entry,
1.19      timbl     538: **        request->anchor      valid for of the document to be accessed.
                    539: **      request->childAnchor   optional anchor within doc to be selected
                    540: **
1.2       timbl     541: **        filter            if YES, treat stdin as HTML
1.1       timbl     542: **
1.15      timbl     543: **       request->anchor   is the node_anchor for the document
                    544: **       request->output_format is valid
                    545: **
1.1       timbl     546: **    On Exit,
                    547: **        returns    YES     Success in opening document
                    548: **                   NO      Failure 
                    549: **
                    550: */
                    551: 
1.19      timbl     552: PRIVATE BOOL HTLoadDocument ARGS1(HTRequest *,         request)
1.1       timbl     553: 
                    554: {
                    555:     int                status;
                    556:     HText *    text;
1.19      timbl     557:     char * full_address = HTAnchor_address((HTAnchor*)request->anchor);
                    558:     
1.1       timbl     559:     if (TRACE) fprintf (stderr,
                    560:       "HTAccess: loading document %s\n", full_address);
                    561: 
1.18      timbl     562:     request->using_cache = NULL;
                    563:     
1.15      timbl     564:     if (!request->output_format) request->output_format = WWW_PRESENT;
1.25      frystyk   565: 
1.31      frystyk   566:     if (!HTForceReload && (text=(HText *)HTAnchor_document(request->anchor)))
1.15      timbl     567:     {  /* Already loaded */
1.1       timbl     568:         if (TRACE) fprintf(stderr, "HTAccess: Document already in memory.\n");
1.19      timbl     569:        if (request->childAnchor) {
                    570:            HText_selectAnchor(text, request->childAnchor);
                    571:        } else {
                    572:            HText_select(text); 
                    573:        }
                    574:        free(full_address);
1.1       timbl     575:        return YES;
                    576:     }
1.17      timbl     577:     
1.34      frystyk   578:     /* Check the Cache */
                    579:     /* Caching is ONLY done if (char*) HTCacheDir is set. Henrik 09/03-94 */
1.17      timbl     580:     /* Bug: for each format, we only check whether it is ok, we
                    581:        don't check them all and chose the best */
1.38      timbl     582:     if (/* HTCacheDir && */ request->anchor->cacheItems) {
1.17      timbl     583:         HTList * list = request->anchor->cacheItems;
1.20      luotonen  584:        HTList * cur = list;
                    585:        HTCacheItem * item;
                    586: 
                    587:        while ((item = (HTCacheItem*)HTList_nextObject(cur))) {
1.18      timbl     588:            HTStream * s;
                    589:            
                    590:            request->using_cache = item;
                    591:            
1.37      luotonen  592:            s = HTStreamStack(item->format, request, NO);
1.17      timbl     593:            if (s) {            /* format was suitable */
                    594:                FILE * fp = fopen(item->filename, "r");
1.18      timbl     595:                if (TRACE) fprintf(stderr, "Cache: HIT file %s for %s\n",
1.20      luotonen  596:                                   item->filename, 
                    597:                                   full_address);
1.17      timbl     598:                if (fp) {
                    599:                    HTFileCopy(fp, s);
1.24      timbl     600:                    (*s->isa->free)(s); /* close up pipeline */
1.17      timbl     601:                    fclose(fp);
1.19      timbl     602:                    free(full_address);
1.17      timbl     603:                    return YES;
                    604:                } else {
                    605:                    fprintf(stderr, "***** Can't read cache file %s !\n",
1.20      luotonen  606:                            item->filename);
1.17      timbl     607:                } /* file open ok */
                    608:            } /* stream ok */
                    609:        } /* next cache item */
                    610:     } /* if cache available for this anchor */
1.1       timbl     611:     
1.35      luotonen  612:     status = HTLoad(request);
1.2       timbl     613: 
                    614:     
1.1       timbl     615: /*     Log the access if necessary
                    616: */
1.42      frystyk   617:     if (HTlogfile) {
1.1       timbl     618:        time_t theTime;
                    619:        time(&theTime);
1.42      frystyk   620:        fprintf(HTlogfile, "%24.24s %s %s %s\n",
1.1       timbl     621:            ctime(&theTime),
                    622:            HTClientHost ? HTClientHost : "local",
                    623:            status<0 ? "FAIL" : "GET",
                    624:            full_address);
1.42      frystyk   625:        fflush(HTlogfile);      /* Actually update it on disk */
1.1       timbl     626:        if (TRACE) fprintf(stderr, "Log: %24.24s %s %s %s\n",
                    627:            ctime(&theTime),
                    628:            HTClientHost ? HTClientHost : "local",
                    629:            status<0 ? "FAIL" : "GET",
                    630:            full_address);
                    631:     }
                    632: 
                    633:     if (status == HT_LOADED) {
                    634:        if (TRACE) {
                    635:            fprintf(stderr, "HTAccess: `%s' has been accessed.\n",
                    636:            full_address);
                    637:        }
1.19      timbl     638:        free(full_address);
1.1       timbl     639:        return YES;
                    640:     }
                    641:     
                    642:     if (status == HT_NO_DATA) {
                    643:        if (TRACE) {
                    644:            fprintf(stderr, 
                    645:            "HTAccess: `%s' has been accessed, No data left.\n",
                    646:            full_address);
                    647:        }
1.19      timbl     648:        free(full_address);
1.1       timbl     649:        return NO;
                    650:     }
                    651:     
1.34      frystyk   652:     /* Bug fix thanks to Lou Montulli. Henrik 10/03-94 */
                    653:     if (status<=0) {                 /* Failure in accessing a document */
1.1       timbl     654: #ifdef CURSES
                    655:         user_message("Can't access `%s'", full_address);
                    656: #else
1.5       timbl     657:        if (TRACE) fprintf(stderr, 
                    658:                "HTAccess: Can't access `%s'\n", full_address);
1.1       timbl     659: #endif
1.32      frystyk   660:        /* This is done in the specific load procedures... Henrik 07/03-94 */
1.46    ! frystyk   661: #if 0
1.39      luotonen  662:        if (request->error_stack)
                    663:            HTLoadError(request, 500, "Unable to access document.");
1.46    ! frystyk   664: #endif
1.19      timbl     665:        free(full_address);
1.1       timbl     666:        return NO;
                    667:     }
1.9       timbl     668:  
                    669:     /* If you get this, then please find which routine is returning
                    670:        a positive unrecognised error code! */
                    671:  
1.1       timbl     672:     fprintf(stderr,
1.2       timbl     673:     "**** HTAccess: socket or file number returned by obsolete load routine!\n");
1.9       timbl     674:     fprintf(stderr,
1.19      timbl     675:     "**** HTAccess: Internal software error. Please mail www-bug@info.cern.ch quoting the version number of this software and the URL: %s!\n",
                    676:        full_address);
                    677:     free(full_address);
                    678:    
1.1       timbl     679:     exit(-6996);
1.20      luotonen  680:     return NO; /* For gcc :-( */
1.2       timbl     681: } /* HTLoadDocument */
1.1       timbl     682: 
                    683: 
                    684: 
                    685: /*             Load a document from absolute name
                    686: **             ---------------
                    687: **
                    688: **    On Entry,
                    689: **        addr     The absolute address of the document to be accessed.
                    690: **        filter   if YES, treat document as HTML
                    691: **
                    692: **    On Exit,
                    693: **        returns    YES     Success in opening document
                    694: **                   NO      Failure 
                    695: **
                    696: **
                    697: */
                    698: 
1.15      timbl     699: PUBLIC BOOL HTLoadAbsolute ARGS2(CONST char *,addr, HTRequest*, request)
1.2       timbl     700: {
1.19      timbl     701:    HTAnchor * anchor = HTAnchor_findAddress(addr);
                    702:    request->anchor = HTAnchor_parent(anchor);
                    703:    request->childAnchor = ((HTAnchor*)request->anchor == anchor) ?
                    704:                        NULL : (HTChildAnchor*) anchor;
                    705:    return HTLoadDocument(request);
1.2       timbl     706: }
                    707: 
                    708: 
                    709: /*             Load a document from absolute name to stream
                    710: **             --------------------------------------------
                    711: **
                    712: **    On Entry,
                    713: **        addr     The absolute address of the document to be accessed.
1.15      timbl     714: **        request->output_stream     if non-NULL, send data down this stream
1.2       timbl     715: **
                    716: **    On Exit,
                    717: **        returns    YES     Success in opening document
                    718: **                   NO      Failure 
                    719: **
                    720: **
                    721: */
                    722: 
                    723: PUBLIC BOOL HTLoadToStream ARGS3(
                    724:                CONST char *,   addr,
                    725:                BOOL,           filter,
1.15      timbl     726:                HTRequest*,     request)
1.1       timbl     727: {
1.19      timbl     728:    HTAnchor * anchor = HTAnchor_findAddress(addr);
                    729:    request->anchor = HTAnchor_parent(anchor);
                    730:    request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL :
                    731:        (HTChildAnchor*) anchor;
1.15      timbl     732:     request->output_stream = request->output_stream;
1.19      timbl     733:     return HTLoadDocument(request);
1.1       timbl     734: }
                    735: 
                    736: 
1.2       timbl     737: 
                    738: 
1.1       timbl     739: /*             Load a document from relative name
                    740: **             ---------------
                    741: **
                    742: **    On Entry,
1.2       timbl     743: **        relative_name     The relative address of the document
                    744: **                         to be accessed.
1.1       timbl     745: **
                    746: **    On Exit,
                    747: **        returns    YES     Success in opening document
                    748: **                   NO      Failure 
                    749: **
                    750: **
                    751: */
                    752: 
1.15      timbl     753: PUBLIC BOOL HTLoadRelative ARGS3(
1.2       timbl     754:                CONST char *,           relative_name,
1.15      timbl     755:                HTParentAnchor *,       here,
1.20      luotonen  756:                HTRequest *,            request)
1.1       timbl     757: {
                    758:     char *             full_address = 0;
                    759:     BOOL                       result;
                    760:     char *             mycopy = 0;
                    761:     char *             stripped = 0;
                    762:     char *             current_address =
1.2       timbl     763:                                HTAnchor_address((HTAnchor*)here);
1.1       timbl     764: 
                    765:     StrAllocCopy(mycopy, relative_name);
                    766: 
                    767:     stripped = HTStrip(mycopy);
                    768:     full_address = HTParse(stripped,
                    769:                   current_address,
                    770:                   PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.15      timbl     771:     result = HTLoadAbsolute(full_address, request);
1.1       timbl     772:     free(full_address);
                    773:     free(current_address);
                    774:     free(mycopy);  /* Memory leak fixed 10/7/92 -- JFG */
                    775:     return result;
                    776: }
                    777: 
                    778: 
                    779: /*             Load if necessary, and select an anchor
                    780: **             --------------------------------------
                    781: **
                    782: **    On Entry,
                    783: **        destination              The child or parenet anchor to be loaded.
                    784: **
                    785: **    On Exit,
                    786: **        returns    YES     Success
                    787: **                   NO      Failure 
                    788: **
                    789: */
                    790: 
1.15      timbl     791: PUBLIC BOOL HTLoadAnchor ARGS2(HTAnchor*, anchor, HTRequest *, request)
1.1       timbl     792: {
1.15      timbl     793:     if (!anchor) return NO;    /* No link */
1.1       timbl     794:     
1.15      timbl     795:     request->anchor  = HTAnchor_parent(anchor);
1.19      timbl     796:     request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL
                    797:                                        : (HTChildAnchor*) anchor;
1.1       timbl     798:     
1.19      timbl     799:     return HTLoadDocument(request) ? YES : NO;
1.1       timbl     800:        
                    801: } /* HTLoadAnchor */
                    802: 
                    803: 
                    804: /*             Search
                    805: **             ------
                    806: **  Performs a keyword search on word given by the user. Adds the keyword to 
                    807: **  the end of the current address and attempts to open the new address.
                    808: **
                    809: **  On Entry,
                    810: **       *keywords     space-separated keyword list or similar search list
1.2       timbl     811: **     here            is anchor search is to be done on.
1.1       timbl     812: */
                    813: 
1.2       timbl     814: PRIVATE char hex(i)
                    815:     int i;
                    816: {
1.13      timbl     817:     char * hexchars = "0123456789ABCDEF";
                    818:     return hexchars[i];
1.2       timbl     819: }
1.1       timbl     820: 
1.15      timbl     821: PUBLIC BOOL HTSearch ARGS3(
1.2       timbl     822:        CONST char *,           keywords,
1.15      timbl     823:        HTParentAnchor *,       here,
                    824:        HTRequest *,            request)
1.1       timbl     825: {
1.2       timbl     826: 
                    827: #define acceptable \
                    828: "1234567890abcdefghijlkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"
                    829: 
                    830:     char *q, *u;
                    831:     CONST char * p, *s, *e;            /* Pointers into keywords */
                    832:     char * address = HTAnchor_address((HTAnchor*)here);
1.1       timbl     833:     BOOL result;
1.2       timbl     834:     char * escaped = malloc(strlen(keywords)*3+1);
                    835: 
1.29      frystyk   836:     /* static CONST BOOL isAcceptable[96] = */
                    837:     /* static AND const is not good for a gnu compiler! Frystyk 25/02-94 */
1.30      luotonen  838:     static BOOL isAcceptable[96] =
1.2       timbl     839:     /*   0 1 2 3 4 5 6 7 8 9 A B C D E F */
                    840:     {    0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,      /* 2x   !"#$%&'()*+,-./  */
                    841:          1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,      /* 3x  0123456789:;<=>?  */
                    842:         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,       /* 4x  @ABCDEFGHIJKLMNO  */
                    843:         1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,       /* 5X  PQRSTUVWXYZ[\]^_  */
                    844:         0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,       /* 6x  `abcdefghijklmno  */
                    845:         1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0 };     /* 7X  pqrstuvwxyz{\}~  DEL */
                    846: 
                    847:     if (escaped == NULL) outofmem(__FILE__, "HTSearch");
                    848:     
1.29      frystyk   849: /* Convert spaces to + and hex escape unacceptable characters */
1.2       timbl     850: 
1.29      frystyk   851:     for(s=keywords; *s && WHITE(*s); s++); /*scan */    /* Skip white space */
                    852:     for(e = s + strlen(s); e>s && WHITE(*(e-1)) ; e--);     /* Skip trailers */
                    853:     for(q=escaped, p=s; p<e; p++) {                  /* scan stripped field */
1.2       timbl     854:         int c = (int)TOASCII(*p);
                    855:         if (WHITE(*p)) {
                    856:            *q++ = '+';
1.29      frystyk   857:        } else if (c>=32 && c<=127 && isAcceptable[c-32] != 0) {
1.13      timbl     858:            *q++ = *p;                  /* 930706 TBL for MVS bug */
1.2       timbl     859:        } else {
                    860:            *q++ = '%';
                    861:            *q++ = hex(c / 16);
                    862:            *q++ = hex(c % 16);
                    863:        }
                    864:     } /* Loop over string */
1.1       timbl     865:     
1.2       timbl     866:     *q=0;
                    867:                                /* terminate escaped sctring */
                    868:     u=strchr(address, '?');            /* Find old search string */
                    869:     if (u) *u = 0;                             /* Chop old search off */
1.1       timbl     870: 
                    871:     StrAllocCat(address, "?");
1.2       timbl     872:     StrAllocCat(address, escaped);
                    873:     free(escaped);
1.15      timbl     874:     result = HTLoadRelative(address, here, request);
1.1       timbl     875:     free(address);
1.2       timbl     876:     
1.1       timbl     877:     return result;
1.2       timbl     878: }
                    879: 
                    880: 
                    881: /*             Search Given Indexname
                    882: **             ------
                    883: **  Performs a keyword search on word given by the user. Adds the keyword to 
                    884: **  the end of the current address and attempts to open the new address.
                    885: **
                    886: **  On Entry,
                    887: **       *keywords     space-separated keyword list or similar search list
                    888: **     *addres         is name of object search is to be done on.
                    889: */
                    890: 
1.15      timbl     891: PUBLIC BOOL HTSearchAbsolute ARGS3(
1.2       timbl     892:        CONST char *,   keywords,
1.15      timbl     893:        CONST char *,   indexname,
                    894:        HTRequest *,    request)
1.2       timbl     895: {
                    896:     HTParentAnchor * anchor =
                    897:        (HTParentAnchor*) HTAnchor_findAddress(indexname);
1.15      timbl     898:     return HTSearch(keywords, anchor, request);
1.2       timbl     899: }
                    900: 
                    901: 
                    902: /*             Generate the anchor for the home page
                    903: **             -------------------------------------
                    904: **
                    905: **     As it involves file access, this should only be done once
                    906: **     when the program first runs.
1.10      timbl     907: **     This is a default algorithm -- browser don't HAVE to use this.
                    908: **     But consistency betwen browsers is STRONGLY recommended!
1.2       timbl     909: **
1.10      timbl     910: **     Priority order is:
                    911: **
                    912: **             1       WWW_HOME environment variable (logical name, etc)
                    913: **             2       ~/WWW/default.html
                    914: **             3       /usr/local/bin/default.html
                    915: **             4       http://info.cern.ch/default.html
                    916: **
1.2       timbl     917: */
                    918: PUBLIC HTParentAnchor * HTHomeAnchor NOARGS
                    919: {
1.12      timbl     920:     char * my_home_document = NULL;
                    921:     char * home = (char *)getenv(LOGICAL_DEFAULT);
1.2       timbl     922:     char * ref;
                    923:     HTParentAnchor * anchor;
1.1       timbl     924:     
1.12      timbl     925:     if (home) {
                    926:         StrAllocCopy(my_home_document, home);
                    927:     
                    928: /*     Someone telnets in, they get a special home.
                    929: */
                    930: #define MAX_FILE_NAME 1024                                     /* @@@ */
                    931:     } else  if (HTClientHost) {                        /* Telnet server */
                    932:        FILE * fp = fopen(REMOTE_POINTER, "r");
                    933:        char * status;
                    934:        if (fp) {
                    935:            my_home_document = (char*) malloc(MAX_FILE_NAME);
                    936:            status = fgets(my_home_document, MAX_FILE_NAME, fp);
                    937:            if (!status) {
                    938:                free(my_home_document);
                    939:                my_home_document = NULL;
                    940:            }
                    941:            fclose(fp);
                    942:        }
                    943:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                    944:     }
                    945: 
                    946:     
                    947: 
1.2       timbl     948: #ifdef unix
1.12      timbl     949: 
1.10      timbl     950:     if (!my_home_document) {
                    951:        FILE * fp = NULL;
                    952:        CONST char * home =  (CONST char*)getenv("HOME");
                    953:        if (home) { 
                    954:            my_home_document = (char *)malloc(
                    955:                strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1);
                    956:            if (my_home_document == NULL) outofmem(__FILE__, "HTLocalName");
                    957:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                    958:            fp = fopen(my_home_document, "r");
                    959:        }
                    960:        
                    961:        if (!fp) {
                    962:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                    963:            fp = fopen(my_home_document, "r");
                    964:        }
1.2       timbl     965:        if (fp) {
                    966:            fclose(fp);
                    967:        } else {
                    968:        if (TRACE) fprintf(stderr,
1.10      timbl     969:            "HTBrowse: No local home document ~/%s or %s\n",
                    970:            PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
1.11      timbl     971:            free(my_home_document);
                    972:            my_home_document = NULL;
1.2       timbl     973:        }
                    974:     }
                    975: #endif
1.10      timbl     976:     ref = HTParse( my_home_document ?  my_home_document :
                    977:                                HTClientHost ? REMOTE_ADDRESS
                    978:                                : LAST_RESORT,
                    979:                    "file:",
1.2       timbl     980:                    PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.10      timbl     981:     if (my_home_document) {
1.2       timbl     982:        if (TRACE) fprintf(stderr,
                    983:            "HTAccess: Using custom home page %s i.e. address %s\n",
1.10      timbl     984:            my_home_document, ref);
                    985:        free(my_home_document);
1.2       timbl     986:     }
                    987:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
                    988:     free(ref);
                    989:     return anchor;
1.1       timbl     990: }
1.26      frystyk   991: 
                    992: 
                    993: /*             Bind an Anchor to the request structure
                    994: **             ---------------------------------------
                    995: **
                    996: **    On Entry,
                    997: **     anchor          The child or parenet anchor to be binded
                    998: **     request         The request sturcture
                    999: **    On Exit,
                   1000: **        returns    YES     Success
                   1001: **                   NO      Failure 
                   1002: **
                   1003: **  Note: Actually the same as HTLoadAnchor() but DOES NOT do the loading
                   1004: **                                             Henrik Frystyk 17/02-94
                   1005: */
                   1006: 
                   1007: PUBLIC BOOL HTBindAnchor ARGS2(HTAnchor*, anchor, HTRequest *, request)
                   1008: {
                   1009:     if (!anchor) return NO;    /* No link */
                   1010:     
                   1011:     request->anchor  = HTAnchor_parent(anchor);
                   1012:     request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL
                   1013:                                        : (HTChildAnchor*) anchor;
                   1014:        
1.29      frystyk  1015:     return YES;
1.26      frystyk  1016: } /* HTBindAnchor */
1.39      luotonen 1017: 
                   1018: 
                   1019: 
                   1020: /*
                   1021:  *     Error diagnostics
                   1022:  */
                   1023: PUBLIC void HTAddError ARGS2(HTRequest *,      req,
1.40      luotonen 1024:                             CONST char *,      msg)
1.39      luotonen 1025: {
                   1026:     HTAddError2(req,msg,NULL);
                   1027: }
                   1028: 
                   1029: PUBLIC void HTAddError2 ARGS3(HTRequest *,     req,
1.40      luotonen 1030:                              CONST char *,     msg,
                   1031:                              CONST char *,     param)
1.39      luotonen 1032: {
                   1033:     int mlen = msg ? strlen(msg) : 0;
                   1034:     int plen = param ? strlen(param) : 0;
                   1035:     char * str;
                   1036: 
                   1037:     if (!req) return;
                   1038:     if (!req->error_stack) req->error_stack = HTList_new();
                   1039: 
                   1040:     str = (char*)malloc(mlen + plen + 2);
                   1041:     if (!str) outofmem(__FILE__,"HTAddError2");
                   1042: 
                   1043:     if (msg) strcpy(str,msg);
                   1044:     strcpy(str+mlen," ");
                   1045:     if (param) strcpy(str+mlen+1,param);
                   1046: 
                   1047:     HTList_addObject(req->error_stack, (void*)str);
                   1048:     CTRACE(stderr, "libwww error: %s\n", str);
                   1049: }
                   1050: 
                   1051: PUBLIC void HTAddErrorN ARGS3(HTRequest *,     req,
1.40      luotonen 1052:                              CONST char *,     msg,
1.39      luotonen 1053:                              int,              num)
                   1054: {
                   1055:     char buf[20];
                   1056:     sprintf(buf,"%d",num);
                   1057:     HTAddError2(req,msg,buf);
                   1058: }
                   1059: 
                   1060: PUBLIC void HTClearErrors ARGS1(HTRequest *,   req)
                   1061: {
                   1062:     if (req && req->error_stack) {
                   1063:        HTList * cur = req->error_stack;
                   1064:        char * str;
                   1065:        while ((str = (char*)HTList_nextObject(cur)))
                   1066:            free(str);
                   1067:        HTList_delete(req->error_stack);
                   1068:        req->error_stack = NULL;
                   1069:     }
                   1070: }
1.26      frystyk  1071: 

Webmaster