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

1.1       timbl       1: /*             Access Manager                                  HTAccess.c
                      2: **             ==============
                      3: **
                      4: ** Authors
                      5: **     TBL     Tim Berners-Lee timbl@info.cern.ch
1.4       timbl       6: **     JFG     Jean-Francois Groff jfg@dxcern.cern.ch
1.1       timbl       7: **     DD      Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
                      8: ** History
                      9: **       8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
                     10: **     26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
                     11: **      6 Oct 92 Moved HTClientHost and logfile into here. TBL
                     12: **     17 Dec 92 Tn3270 added, bug fix. DD
1.2       timbl      13: **      4 Feb 93 Access registration, Search escapes bad chars TBL
1.9       timbl      14: **               PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
                     15: **     28 May 93 WAIS gateway explicit if no WAIS library linked in.
1.2       timbl      16: **
                     17: ** Bugs
                     18: **     This module assumes that that the graphic object is hypertext, as it
1.9       timbl      19: **     needs to select it when it has been loaded.  A superclass needs to be
1.2       timbl      20: **     defined which accepts select and select_anchor.
1.1       timbl      21: */
                     22: 
1.9       timbl      23: #ifndef DEFAULT_WAIS_GATEWAY
1.8       timbl      24: #define DEFAULT_WAIS_GATEWAY "http://info.cern.ch:8001/"
1.9       timbl      25: #endif
1.8       timbl      26: 
1.1       timbl      27: /* Implements:
                     28: */
                     29: #include "HTAccess.h"
                     30: 
                     31: /* Uses:
                     32: */
                     33: 
                     34: #include "HTParse.h"
                     35: #include "HTUtils.h"
1.4       timbl      36: #include "HTML.h"              /* SCW */
1.2       timbl      37: 
                     38: #ifndef NO_RULES
                     39: #include "HTRules.h"
                     40: #endif
                     41: 
1.1       timbl      42: #include <stdio.h>
                     43: 
1.2       timbl      44: #include "HTList.h"
                     45: #include "HText.h"     /* See bugs above */
                     46: #include "HTAlert.h"
1.17    ! timbl      47: #include "HTFWriter.h" /* for cache stuff */
        !            48: #include "HTTee.h"
1.2       timbl      49: 
1.1       timbl      50: /*     These flags may be set to modify the operation of this module
                     51: */
                     52: PUBLIC char * HTClientHost = 0;        /* Name of remote login host if any */
                     53: PUBLIC FILE * logfile = 0;     /* File to which to output one-liners */
1.12      timbl      54: PUBLIC BOOL HTSecure = NO;     /* Disable access for telnet users? */
1.1       timbl      55: 
1.2       timbl      56: /*     To generate other things, play with these:
                     57: */
                     58: 
1.15      timbl      59: /* PUBLIC HTFormat HTOutputFormat = NULL;      use request->output_format */
                     60: /* PUBLIC HTStream* HTOutputStream = NULL;     use request->output_stream */ 
1.1       timbl      61: 
                     62: PRIVATE HTList * protocols = NULL;   /* List of registered protocol descriptors */
                     63: 
                     64: 
1.15      timbl      65: /*     Create  a request structure
                     66: **     ---------------------------
                     67: */
                     68: 
                     69: PUBLIC HTRequest * HTRequest_new NOARGS
                     70: {
                     71:     HTRequest * me = (HTRequest*) calloc(sizeof(*me), 1);  /* zero fill */
                     72:     if (!me) outofmem(__FILE__, "HTRequest_new()");
                     73:     
                     74:     me->output_format = WWW_PRESENT;   /* default it to present to user */
1.16      timbl      75:     me->conversions = HTList_new();    /* No conversions registerd yet */
1.15      timbl      76:     return me;
                     77: }
                     78: 
                     79: 
1.1       timbl      80: /*     Register a Protocol                             HTRegisterProtocol
                     81: **     -------------------
                     82: */
                     83: 
                     84: PUBLIC BOOL HTRegisterProtocol(protocol)
                     85:        HTProtocol * protocol;
                     86: {
                     87:     if (!protocols) protocols = HTList_new();
                     88:     HTList_addObject(protocols, protocol);
                     89:     return YES;
                     90: }
                     91: 
                     92: 
                     93: /*     Register all known protocols
                     94: **     ----------------------------
                     95: **
                     96: **     Add to or subtract from this list if you add or remove protocol modules.
                     97: **     This routine is called the first time the protocol list is needed,
                     98: **     unless any protocols are already registered, in which case it is not called.
                     99: **     Therefore the application can override this list.
                    100: **
                    101: **     Compiling with NO_INIT prevents all known protocols from being forced
                    102: **     in at link time.
                    103: */
                    104: #ifndef NO_INIT
                    105: PRIVATE void HTAccessInit NOARGS                       /* Call me once */
                    106: {
1.14      duns      107: GLOBALREF HTProtocol HTTP, HTFile, HTTelnet, HTTn3270, HTRlogin;
1.1       timbl     108: #ifndef DECNET
1.14      duns      109: GLOBALREF  HTProtocol HTFTP, HTNews, HTGopher;
1.3       timbl     110: #ifdef DIRECT_WAIS
1.14      duns      111: GLOBALREF  HTProtocol HTWAIS;
1.3       timbl     112: #endif
1.2       timbl     113:     HTRegisterProtocol(&HTFTP);
                    114:     HTRegisterProtocol(&HTNews);
                    115:     HTRegisterProtocol(&HTGopher);
1.3       timbl     116: #ifdef DIRECT_WAIS
                    117:     HTRegisterProtocol(&HTWAIS);
                    118: #endif
1.1       timbl     119: #endif
                    120: 
1.2       timbl     121:     HTRegisterProtocol(&HTTP);
                    122:     HTRegisterProtocol(&HTFile);
                    123:     HTRegisterProtocol(&HTTelnet);
                    124:     HTRegisterProtocol(&HTTn3270);
                    125:     HTRegisterProtocol(&HTRlogin);
1.1       timbl     126: }
                    127: #endif
                    128: 
                    129: 
1.2       timbl     130: /*             Find physical name and access protocol
                    131: **             --------------------------------------
1.1       timbl     132: **
                    133: **
                    134: ** On entry,
                    135: **     addr            must point to the fully qualified hypertext reference.
                    136: **     anchor          a pareent anchor with whose address is addr
                    137: **
                    138: ** On exit,
1.2       timbl     139: **     returns         HT_NO_ACCESS            Error has occured.
                    140: **                     HT_OK                   Success
1.1       timbl     141: **
                    142: */
1.2       timbl     143: PRIVATE int get_physical ARGS2(
                    144:        CONST char *,           addr,
                    145:        HTParentAnchor *,       anchor)
1.1       timbl     146: {
                    147:     char * access=0;   /* Name of access method */
1.2       timbl     148:     char * physical = 0;
1.1       timbl     149:     
1.2       timbl     150: #ifndef NO_RULES
                    151:     physical = HTTranslate(addr);
                    152:     if (!physical) {
                    153:        return HT_FORBIDDEN;
                    154:     }
                    155:     HTAnchor_setPhysical(anchor, physical);
                    156:     free(physical);                    /* free our copy */
                    157: #else
                    158:     HTAnchor_setPhysical(anchor, addr);
                    159: #endif
                    160: 
                    161:     access =  HTParse(HTAnchor_physical(anchor),
                    162:                "file:", PARSE_ACCESS);
1.1       timbl     163: 
                    164: /*     Check whether gateway access has been set up for this
1.8       timbl     165: **
                    166: **     This function can be replaced by the rule system above.
1.1       timbl     167: */
1.8       timbl     168: #define USE_GATEWAYS
1.1       timbl     169: #ifdef USE_GATEWAYS
1.2       timbl     170:     {
1.9       timbl     171:        char * gateway_parameter, *gateway;
1.2       timbl     172:        gateway_parameter = (char *)malloc(strlen(access)+20);
                    173:        if (gateway_parameter == NULL) outofmem(__FILE__, "HTLoad");
                    174:        strcpy(gateway_parameter, "WWW_");
                    175:        strcat(gateway_parameter, access);
                    176:        strcat(gateway_parameter, "_GATEWAY");
                    177:        gateway = (char *)getenv(gateway_parameter); /* coerce for decstation */
                    178:        free(gateway_parameter);
1.8       timbl     179:        
                    180: #ifndef DIRECT_WAIS
1.9       timbl     181:        if (!gateway && 0==strcmp(access, "wais")) {
1.8       timbl     182:            gateway = DEFAULT_WAIS_GATEWAY;
                    183:        }
                    184: #endif
1.2       timbl     185:        if (gateway) {
1.9       timbl     186:            char * path = HTParse(addr, "",
                    187:                PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
                    188:                /* Chop leading / off to make host into part of path */
                    189:            char * gatewayed = HTParse(path+1, gateway, PARSE_ALL);
                    190:            free(path);
1.8       timbl     191:             HTAnchor_setPhysical(anchor, gatewayed);
1.9       timbl     192:            free(gatewayed);
1.2       timbl     193:            free(access);
1.9       timbl     194:            
1.8       timbl     195:            access =  HTParse(HTAnchor_physical(anchor),
                    196:                "http:", PARSE_ACCESS);
1.2       timbl     197:        }
                    198:     }
1.1       timbl     199: #endif
                    200: 
                    201: 
                    202: 
                    203: /*     Search registered protocols to find suitable one
                    204: */
                    205:     {
                    206:        int i, n;
                    207: #ifndef NO_INIT
1.2       timbl     208:         if (!protocols) HTAccessInit();
1.1       timbl     209: #endif
                    210:        n = HTList_count(protocols);
                    211:        for (i=0; i<n; i++) {
1.2       timbl     212:            HTProtocol *p = HTList_objectAt(protocols, i);
                    213:            if (strcmp(p->name, access)==0) {
                    214:                HTAnchor_setProtocol(anchor, p);
                    215:                free(access);
                    216:                return (HT_OK);
1.1       timbl     217:            }
                    218:        }
                    219:     }
                    220: 
                    221:     free(access);
1.2       timbl     222:     return HT_NO_ACCESS;
1.1       timbl     223: }
                    224: 
                    225: 
                    226: /*             Load a document
                    227: **             ---------------
                    228: **
1.2       timbl     229: **     This is an internal routine, which has an address AND a matching
                    230: **     anchor.  (The public routines are called with one OR the other.)
                    231: **
                    232: ** On entry,
                    233: **     addr            must point to the fully qualified hypertext reference.
1.15      timbl     234: **     request->
                    235: **         anchor              a parent anchor with whose address is addr
                    236: **         output_format       valid
                    237: **         output_stream       valid on NULL
1.2       timbl     238: **
                    239: ** On exit,
                    240: **     returns         <0              Error has occured.
                    241: **                     HT_LOADED       Success
                    242: **                     HT_NO_DATA      Success, but no document loaded.
1.8       timbl     243: **                                     (telnet sesssion started etc)
1.2       timbl     244: **
                    245: */
1.15      timbl     246: PRIVATE int HTLoad ARGS2(
1.2       timbl     247:        CONST char *,           addr,
1.15      timbl     248:        HTRequest *,            request)
1.2       timbl     249: {
                    250:     HTProtocol* p;
1.15      timbl     251:     int status = get_physical(addr, request->anchor);
1.2       timbl     252:     if (status == HT_FORBIDDEN) {
1.15      timbl     253:         return HTLoadError(request->output_stream, 500,
                    254:                "Access forbidden by rule");
1.2       timbl     255:     }
                    256:     if (status < 0) return status;     /* Can't resolve or forbidden */
                    257:     
1.15      timbl     258:     p = HTAnchor_protocol(request->anchor);
1.17    ! timbl     259:     return (*(p->load))(request);
1.2       timbl     260: }
                    261: 
                    262: 
                    263: /*             Get a save stream for a document
                    264: **             --------------------------------
                    265: */
1.15      timbl     266: PUBLIC HTStream *HTSaveStream ARGS2(
                    267:                        HTParentAnchor *,       anchor,
                    268:                        HTRequest *,            request)
                    269: {
                    270:     HTProtocol * p;
                    271:     request->anchor = anchor;
                    272:     p = HTAnchor_protocol(request->anchor);
1.2       timbl     273:     if (!p) return NULL;
                    274:     
1.15      timbl     275:     return (*p->saveStream)(request);
1.2       timbl     276:     
                    277: }
                    278: 
                    279: 
                    280: /*             Load a document - with logging etc
                    281: **             ----------------------------------
                    282: **
                    283: **     - Checks or documents already loaded
                    284: **     - Logs the access
                    285: **     - Allows stdin filter option
                    286: **     - Trace ouput and error messages
                    287: **
1.1       timbl     288: **    On Entry,
                    289: **        full_address      The address of the document to be accessed.
1.2       timbl     290: **        filter            if YES, treat stdin as HTML
1.1       timbl     291: **
1.15      timbl     292: **       request->anchor   is the node_anchor for the document
                    293: **       request->output_format is valid
                    294: **
1.1       timbl     295: **    On Exit,
                    296: **        returns    YES     Success in opening document
                    297: **                   NO      Failure 
                    298: **
                    299: */
                    300: 
1.15      timbl     301: PRIVATE BOOL HTLoadDocument ARGS2(
1.2       timbl     302:        CONST char *,           full_address,
1.15      timbl     303:        HTRequest *,            request)
1.1       timbl     304: 
                    305: {
                    306:     int                status;
                    307:     HText *    text;
                    308: 
                    309:     if (TRACE) fprintf (stderr,
                    310:       "HTAccess: loading document %s\n", full_address);
                    311: 
1.15      timbl     312:     if (!request->output_format) request->output_format = WWW_PRESENT;
                    313:     
                    314:     if (text=(HText *)HTAnchor_document(request->anchor))
                    315:     {  /* Already loaded */
1.1       timbl     316:         if (TRACE) fprintf(stderr, "HTAccess: Document already in memory.\n");
                    317:         HText_select(text);
                    318:        return YES;
                    319:     }
1.17    ! timbl     320:     
        !           321:     /* Check the Cache
        !           322:     */
        !           323:     /* Bug: for each format, we only check whether it is ok, we
        !           324:        don't check them all and chose the best */
        !           325:     if (request->anchor->cacheItems) {
        !           326:         HTList * list = request->anchor->cacheItems;
        !           327:         int i;
        !           328:        int n = HTList_count(list);
        !           329:        for(i=0; i<n; i++) {
        !           330:            HTCacheItem * item = HTList_objectAt(list, i);
        !           331:            HTStream * s = HTStreamStack(item->format, request);
        !           332:            if (s) {            /* format was suitable */
        !           333:                FILE * fp = fopen(item->filename, "r");
        !           334:                if (TRACE) fprintf(stderr, "Cache hit for %s\n",
        !           335:                                        full_address);
        !           336:                if (fp) {
        !           337:                    HTFileCopy(fp, s);
        !           338:                    fclose(fp);
        !           339:                    return YES;
        !           340:                } else {
        !           341:                    fprintf(stderr, "***** Can't read cache file %s !\n",
        !           342:                        item->filename);
        !           343:                } /* file open ok */
        !           344:            } /* stream ok */
        !           345:        } /* next cache item */
        !           346:     } /* if cache available for this anchor */
1.1       timbl     347:     
1.15      timbl     348:     status = HTLoad(full_address, request);
1.2       timbl     349: 
                    350:     
1.1       timbl     351: /*     Log the access if necessary
                    352: */
                    353:     if (logfile) {
                    354:        time_t theTime;
                    355:        time(&theTime);
                    356:        fprintf(logfile, "%24.24s %s %s %s\n",
                    357:            ctime(&theTime),
                    358:            HTClientHost ? HTClientHost : "local",
                    359:            status<0 ? "FAIL" : "GET",
                    360:            full_address);
                    361:        fflush(logfile);        /* Actually update it on disk */
                    362:        if (TRACE) fprintf(stderr, "Log: %24.24s %s %s %s\n",
                    363:            ctime(&theTime),
                    364:            HTClientHost ? HTClientHost : "local",
                    365:            status<0 ? "FAIL" : "GET",
                    366:            full_address);
                    367:     }
                    368:     
                    369: 
                    370:     if (status == HT_LOADED) {
                    371:        if (TRACE) {
                    372:            fprintf(stderr, "HTAccess: `%s' has been accessed.\n",
                    373:            full_address);
                    374:        }
                    375:        return YES;
                    376:     }
                    377:     
                    378:     if (status == HT_NO_DATA) {
                    379:        if (TRACE) {
                    380:            fprintf(stderr, 
                    381:            "HTAccess: `%s' has been accessed, No data left.\n",
                    382:            full_address);
                    383:        }
                    384:        return NO;
                    385:     }
                    386:     
1.2       timbl     387:     if (status<0) {                  /* Failure in accessing a document */
1.1       timbl     388: #ifdef CURSES
                    389:         user_message("Can't access `%s'", full_address);
                    390: #else
1.5       timbl     391:        if (TRACE) fprintf(stderr, 
                    392:                "HTAccess: Can't access `%s'\n", full_address);
1.1       timbl     393: #endif
1.15      timbl     394:        HTLoadError(request->output_stream, 500, "Unable to access document.");
1.1       timbl     395:        return NO;
                    396:     }
1.9       timbl     397:  
                    398:     /* If you get this, then please find which routine is returning
                    399:        a positive unrecognised error code! */
                    400:  
1.1       timbl     401:     fprintf(stderr,
1.2       timbl     402:     "**** HTAccess: socket or file number returned by obsolete load routine!\n");
1.9       timbl     403:     fprintf(stderr,
                    404:     "**** HTAccess: Internal software error. Please mail www-bug@info.cern.ch!\n");
1.1       timbl     405:     exit(-6996);
                    406: 
1.2       timbl     407: } /* HTLoadDocument */
1.1       timbl     408: 
                    409: 
                    410: 
                    411: /*             Load a document from absolute name
                    412: **             ---------------
                    413: **
                    414: **    On Entry,
                    415: **        addr     The absolute address of the document to be accessed.
                    416: **        filter   if YES, treat document as HTML
                    417: **
                    418: **    On Exit,
                    419: **        returns    YES     Success in opening document
                    420: **                   NO      Failure 
                    421: **
                    422: **
                    423: */
                    424: 
1.15      timbl     425: PUBLIC BOOL HTLoadAbsolute ARGS2(CONST char *,addr, HTRequest*, request)
1.2       timbl     426: {
1.15      timbl     427:    request->anchor = HTAnchor_parent(HTAnchor_findAddress(addr));
                    428: 
                    429:    return HTLoadDocument( addr, request);
1.2       timbl     430: }
                    431: 
                    432: 
                    433: /*             Load a document from absolute name to stream
                    434: **             --------------------------------------------
                    435: **
                    436: **    On Entry,
                    437: **        addr     The absolute address of the document to be accessed.
1.15      timbl     438: **        request->output_stream     if non-NULL, send data down this stream
1.2       timbl     439: **
                    440: **    On Exit,
                    441: **        returns    YES     Success in opening document
                    442: **                   NO      Failure 
                    443: **
                    444: **
                    445: */
                    446: 
                    447: PUBLIC BOOL HTLoadToStream ARGS3(
                    448:                CONST char *,   addr,
                    449:                BOOL,           filter,
1.15      timbl     450:                HTRequest*,     request)
1.1       timbl     451: {
1.15      timbl     452:     request->output_stream = request->output_stream;
                    453:     request->anchor = HTAnchor_parent(HTAnchor_findAddress(addr));
                    454:     return HTLoadDocument(addr, request);
1.1       timbl     455: }
                    456: 
                    457: 
1.2       timbl     458: 
                    459: 
1.1       timbl     460: /*             Load a document from relative name
                    461: **             ---------------
                    462: **
                    463: **    On Entry,
1.2       timbl     464: **        relative_name     The relative address of the document
                    465: **                         to be accessed.
1.1       timbl     466: **
                    467: **    On Exit,
                    468: **        returns    YES     Success in opening document
                    469: **                   NO      Failure 
                    470: **
                    471: **
                    472: */
                    473: 
1.15      timbl     474: PUBLIC BOOL HTLoadRelative ARGS3(
1.2       timbl     475:                CONST char *,           relative_name,
1.15      timbl     476:                HTParentAnchor *,       here,
                    477:                HTRequest*,             request)
1.1       timbl     478: {
                    479:     char *             full_address = 0;
                    480:     BOOL                       result;
                    481:     char *             mycopy = 0;
                    482:     char *             stripped = 0;
                    483:     char *             current_address =
1.2       timbl     484:                                HTAnchor_address((HTAnchor*)here);
1.1       timbl     485: 
                    486:     StrAllocCopy(mycopy, relative_name);
                    487: 
                    488:     stripped = HTStrip(mycopy);
                    489:     full_address = HTParse(stripped,
                    490:                   current_address,
                    491:                   PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.15      timbl     492:     result = HTLoadAbsolute(full_address, request);
1.1       timbl     493:     free(full_address);
                    494:     free(current_address);
                    495:     free(mycopy);  /* Memory leak fixed 10/7/92 -- JFG */
                    496:     return result;
                    497: }
                    498: 
                    499: 
                    500: /*             Load if necessary, and select an anchor
                    501: **             --------------------------------------
                    502: **
                    503: **    On Entry,
                    504: **        destination              The child or parenet anchor to be loaded.
                    505: **
                    506: **    On Exit,
                    507: **        returns    YES     Success
                    508: **                   NO      Failure 
                    509: **
                    510: */
                    511: 
1.15      timbl     512: PUBLIC BOOL HTLoadAnchor ARGS2(HTAnchor*, anchor, HTRequest *, request)
1.1       timbl     513: {
                    514:     BOOL loaded = NO;
1.15      timbl     515:     if (!anchor) return NO;    /* No link */
1.1       timbl     516:     
1.15      timbl     517:     request->anchor  = HTAnchor_parent(anchor);
1.1       timbl     518:     
1.15      timbl     519:     if (HTAnchor_document(request->anchor) == NULL) {/* If not alread loaded */
1.1       timbl     520:         BOOL result;
1.15      timbl     521:         char * address = HTAnchor_address((HTAnchor*) request->anchor);
                    522:        result = HTLoadDocument(address, request);
1.1       timbl     523:        free(address);
                    524:        if (!result) return NO;
                    525:        loaded = YES;
                    526:     }
                    527:     
                    528:     {
1.15      timbl     529:        HText *text = (HText*)HTAnchor_document(request->anchor);
                    530:        if (anchor != (HTAnchor *)request->anchor) {  /* If child anchor */
1.1       timbl     531:            HText_selectAnchor(text, 
1.15      timbl     532:                    (HTChildAnchor*)anchor); /* Double display? @@ */
1.1       timbl     533:        } else {
                    534:            if (!loaded) HText_select(text);
                    535:        }
                    536:     }
                    537:     return YES;
                    538:        
                    539: } /* HTLoadAnchor */
                    540: 
                    541: 
                    542: /*             Search
                    543: **             ------
                    544: **  Performs a keyword search on word given by the user. Adds the keyword to 
                    545: **  the end of the current address and attempts to open the new address.
                    546: **
                    547: **  On Entry,
                    548: **       *keywords     space-separated keyword list or similar search list
1.2       timbl     549: **     here            is anchor search is to be done on.
1.1       timbl     550: */
                    551: 
1.2       timbl     552: PRIVATE char hex(i)
                    553:     int i;
                    554: {
1.13      timbl     555:     char * hexchars = "0123456789ABCDEF";
                    556:     return hexchars[i];
1.2       timbl     557: }
1.1       timbl     558: 
1.15      timbl     559: PUBLIC BOOL HTSearch ARGS3(
1.2       timbl     560:        CONST char *,           keywords,
1.15      timbl     561:        HTParentAnchor *,       here,
                    562:        HTRequest *,            request)
1.1       timbl     563: {
1.2       timbl     564: 
                    565: #define acceptable \
                    566: "1234567890abcdefghijlkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"
                    567: 
                    568:     char *q, *u;
                    569:     CONST char * p, *s, *e;            /* Pointers into keywords */
                    570:     char * address = HTAnchor_address((HTAnchor*)here);
1.1       timbl     571:     BOOL result;
1.2       timbl     572:     char * escaped = malloc(strlen(keywords)*3+1);
                    573: 
                    574:     static CONST BOOL isAcceptable[96] =
                    575: 
                    576:     /*   0 1 2 3 4 5 6 7 8 9 A B C D E F */
                    577:     {    0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,      /* 2x   !"#$%&'()*+,-./  */
                    578:          1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,      /* 3x  0123456789:;<=>?  */
                    579:         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,       /* 4x  @ABCDEFGHIJKLMNO  */
                    580:         1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,       /* 5X  PQRSTUVWXYZ[\]^_  */
                    581:         0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,       /* 6x  `abcdefghijklmno  */
                    582:         1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0 };     /* 7X  pqrstuvwxyz{\}~  DEL */
                    583: 
                    584:     if (escaped == NULL) outofmem(__FILE__, "HTSearch");
                    585:     
                    586: 
                    587: /*     Convert spaces to + and hex escape unacceptable characters
                    588: */
                    589:     for(s=keywords; *s && WHITE(*s); s++) /*scan */ ;  /* Skip white space */
                    590:     for(e = s + strlen(s); e>s && WHITE(*(e-1)) ; e--); /* Skip trailers */
                    591:     for(q=escaped, p=s; p<e; p++) {                    /* scan stripped field */
                    592:         int c = (int)TOASCII(*p);
                    593:         if (WHITE(*p)) {
                    594:            *q++ = '+';
                    595:        } else if (c>=32 && c<=(char)127 && isAcceptable[c-32]) {
1.13      timbl     596:            *q++ = *p;                  /* 930706 TBL for MVS bug */
1.2       timbl     597:        } else {
                    598:            *q++ = '%';
                    599:            *q++ = hex(c / 16);
                    600:            *q++ = hex(c % 16);
                    601:        }
                    602:     } /* Loop over string */
1.1       timbl     603:     
1.2       timbl     604:     *q=0;
                    605:                                /* terminate escaped sctring */
                    606:     u=strchr(address, '?');            /* Find old search string */
                    607:     if (u) *u = 0;                             /* Chop old search off */
1.1       timbl     608: 
                    609:     StrAllocCat(address, "?");
1.2       timbl     610:     StrAllocCat(address, escaped);
                    611:     free(escaped);
1.15      timbl     612:     result = HTLoadRelative(address, here, request);
1.1       timbl     613:     free(address);
1.2       timbl     614:     
1.1       timbl     615:     return result;
1.2       timbl     616: }
                    617: 
                    618: 
                    619: /*             Search Given Indexname
                    620: **             ------
                    621: **  Performs a keyword search on word given by the user. Adds the keyword to 
                    622: **  the end of the current address and attempts to open the new address.
                    623: **
                    624: **  On Entry,
                    625: **       *keywords     space-separated keyword list or similar search list
                    626: **     *addres         is name of object search is to be done on.
                    627: */
                    628: 
1.15      timbl     629: PUBLIC BOOL HTSearchAbsolute ARGS3(
1.2       timbl     630:        CONST char *,   keywords,
1.15      timbl     631:        CONST char *,   indexname,
                    632:        HTRequest *,    request)
1.2       timbl     633: {
                    634:     HTParentAnchor * anchor =
                    635:        (HTParentAnchor*) HTAnchor_findAddress(indexname);
1.15      timbl     636:     return HTSearch(keywords, anchor, request);
1.2       timbl     637: }
                    638: 
                    639: 
                    640: /*             Generate the anchor for the home page
                    641: **             -------------------------------------
                    642: **
                    643: **     As it involves file access, this should only be done once
                    644: **     when the program first runs.
1.10      timbl     645: **     This is a default algorithm -- browser don't HAVE to use this.
                    646: **     But consistency betwen browsers is STRONGLY recommended!
1.2       timbl     647: **
1.10      timbl     648: **     Priority order is:
                    649: **
                    650: **             1       WWW_HOME environment variable (logical name, etc)
                    651: **             2       ~/WWW/default.html
                    652: **             3       /usr/local/bin/default.html
                    653: **             4       http://info.cern.ch/default.html
                    654: **
1.2       timbl     655: */
                    656: PUBLIC HTParentAnchor * HTHomeAnchor NOARGS
                    657: {
1.12      timbl     658:     char * my_home_document = NULL;
                    659:     char * home = (char *)getenv(LOGICAL_DEFAULT);
1.2       timbl     660:     char * ref;
                    661:     HTParentAnchor * anchor;
1.1       timbl     662:     
1.12      timbl     663:     if (home) {
                    664:         StrAllocCopy(my_home_document, home);
                    665:     
                    666: /*     Someone telnets in, they get a special home.
                    667: */
                    668: #define MAX_FILE_NAME 1024                                     /* @@@ */
                    669:     } else  if (HTClientHost) {                        /* Telnet server */
                    670:        FILE * fp = fopen(REMOTE_POINTER, "r");
                    671:        char * status;
                    672:        if (fp) {
                    673:            my_home_document = (char*) malloc(MAX_FILE_NAME);
                    674:            status = fgets(my_home_document, MAX_FILE_NAME, fp);
                    675:            if (!status) {
                    676:                free(my_home_document);
                    677:                my_home_document = NULL;
                    678:            }
                    679:            fclose(fp);
                    680:        }
                    681:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                    682:     }
                    683: 
                    684:     
                    685: 
1.2       timbl     686: #ifdef unix
1.12      timbl     687: 
1.10      timbl     688:     if (!my_home_document) {
                    689:        FILE * fp = NULL;
                    690:        CONST char * home =  (CONST char*)getenv("HOME");
                    691:        if (home) { 
                    692:            my_home_document = (char *)malloc(
                    693:                strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1);
                    694:            if (my_home_document == NULL) outofmem(__FILE__, "HTLocalName");
                    695:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                    696:            fp = fopen(my_home_document, "r");
                    697:        }
                    698:        
                    699:        if (!fp) {
                    700:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                    701:            fp = fopen(my_home_document, "r");
                    702:        }
1.2       timbl     703:        if (fp) {
                    704:            fclose(fp);
                    705:        } else {
                    706:        if (TRACE) fprintf(stderr,
1.10      timbl     707:            "HTBrowse: No local home document ~/%s or %s\n",
                    708:            PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
1.11      timbl     709:            free(my_home_document);
                    710:            my_home_document = NULL;
1.2       timbl     711:        }
                    712:     }
                    713: #endif
1.10      timbl     714:     ref = HTParse( my_home_document ?  my_home_document :
                    715:                                HTClientHost ? REMOTE_ADDRESS
                    716:                                : LAST_RESORT,
                    717:                    "file:",
1.2       timbl     718:                    PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.10      timbl     719:     if (my_home_document) {
1.2       timbl     720:        if (TRACE) fprintf(stderr,
                    721:            "HTAccess: Using custom home page %s i.e. address %s\n",
1.10      timbl     722:            my_home_document, ref);
                    723:        free(my_home_document);
1.2       timbl     724:     }
                    725:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
                    726:     free(ref);
                    727:     return anchor;
1.1       timbl     728: }
                    729: 
                    730: 

Webmaster