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

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

Webmaster