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

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

Webmaster