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

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

Webmaster