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

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

Webmaster