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

1.61      frystyk     1: /*                                                                  HTAccess.c
                      2: **     ACCESS MANAGER
                      3: **
                      4: **     (c) COPYRIGHT CERN 1994.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
1.1       timbl       6: **
                      7: ** Authors
                      8: **     TBL     Tim Berners-Lee timbl@info.cern.ch
1.4       timbl       9: **     JFG     Jean-Francois Groff jfg@dxcern.cern.ch
1.1       timbl      10: **     DD      Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
                     11: ** History
                     12: **       8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
                     13: **     26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
1.42      frystyk    14: **      6 Oct 92 Moved HTClientHost and HTlogfile into here. TBL
1.1       timbl      15: **     17 Dec 92 Tn3270 added, bug fix. DD
1.2       timbl      16: **      4 Feb 93 Access registration, Search escapes bad chars TBL
1.9       timbl      17: **               PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
                     18: **     28 May 93 WAIS gateway explicit if no WAIS library linked in.
1.19      timbl      19: **        Dec 93 Bug change around, more reentrant, etc
1.42      frystyk    20: **     09 May 94 logfile renamed to HTlogfile to avoid clash with WAIS
1.53      duns       21: **      8 Jul 94 Insulate free() from _free structure element.
1.2       timbl      22: ** Bugs
                     23: **     This module assumes that that the graphic object is hypertext, as it
1.9       timbl      24: **     needs to select it when it has been loaded.  A superclass needs to be
1.2       timbl      25: **     defined which accepts select and select_anchor.
1.1       timbl      26: */
                     27: 
1.9       timbl      28: #ifndef DEFAULT_WAIS_GATEWAY
1.8       timbl      29: #define DEFAULT_WAIS_GATEWAY "http://info.cern.ch:8001/"
1.54      frystyk    30: #endif
1.8       timbl      31: 
1.1       timbl      32: /* Implements:
                     33: */
                     34: #include "HTAccess.h"
                     35: 
                     36: /* Uses:
                     37: */
                     38: 
                     39: #include "HTParse.h"
                     40: #include "HTUtils.h"
1.4       timbl      41: #include "HTML.h"              /* SCW */
1.2       timbl      42: 
                     43: #ifndef NO_RULES
                     44: #include "HTRules.h"
                     45: #endif
                     46: 
                     47: #include "HTList.h"
                     48: #include "HText.h"     /* See bugs above */
                     49: #include "HTAlert.h"
1.17      timbl      50: #include "HTFWriter.h" /* for cache stuff */
                     51: #include "HTTee.h"
1.46      frystyk    52: #include "HTError.h"
1.57      howcome    53: #include "HTTCP.h"      /* HWL: for HTFindRelatedName */
1.63    ! frystyk    54: #include "HTFile.h"
1.59      frystyk    55: #include "HTThread.h"
1.63    ! frystyk    56: #include "HTEvent.h"
1.2       timbl      57: 
1.54      frystyk    58: /* These flags may be set to modify the operation of this module */
                     59: PUBLIC char * HTCacheDir = NULL;  /* Root for cached files or 0 for no cache */
                     60: PUBLIC char * HTSaveLocallyDir = SAVE_LOCALLY_HOME_DIR;         /* Save & exe files */
                     61: PUBLIC char * HTClientHost = 0;                 /* Name of remote login host if any */
                     62: PUBLIC FILE * HTlogfile = 0;          /* File to which to output one-liners */
1.41      luotonen   63: 
1.34      frystyk    64: PUBLIC BOOL HTForceReload = NO;        /* Force reload from cache or net */
1.12      timbl      65: PUBLIC BOOL HTSecure = NO;     /* Disable access for telnet users? */
1.27      luotonen   66: PUBLIC BOOL using_proxy = NO;  /* are we using a proxy gateway? */
1.43      luotonen   67: PUBLIC char * HTImServer = NULL;/* cern_httpd sets this to the translated URL*/
1.27      luotonen   68: PUBLIC BOOL HTImProxy = NO;    /* cern_httpd as a proxy? */
1.1       timbl      69: 
1.63    ! frystyk    70: PRIVATE HTList * protocols = NULL;          /* List of registered protocols */
1.43      luotonen   71: 
1.63    ! frystyk    72: /* Superclass defn */
1.24      timbl      73: struct _HTStream {
                     74:        HTStreamClass * isa;
                     75:        /* ... */
                     76: };
                     77: 
1.59      frystyk    78: /* --------------------------------------------------------------------------*/
                     79: /*                     Management of the HTRequest structure                */
                     80: /* --------------------------------------------------------------------------*/
                     81: 
1.15      timbl      82: /*     Create  a request structure
                     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.49      frystyk    97: /*     Clear  a request structure
                     98: **     ---------------------------
                     99: **     This function clears the reguest structure so that only the
                    100: **     conversions remain. Everything else is as if it was created from
                    101: **     scratch.
                    102: */
                    103: PUBLIC void HTRequest_clear ARGS1(HTRequest *, req)
                    104: {
                    105:     HTList *conversions;
                    106:     if (!req) {
                    107:        if (TRACE)
                    108:            fprintf(stderr, "Clear....... request: Bad argument!\n");
                    109:        return;
                    110:     }
                    111:     conversions = req->conversions;                 /* Save the conversions */
                    112:     HTErrorFree(req);
                    113:     HTAACleanup(req);
                    114:     memset(req, '\0', sizeof(HTRequest));
                    115: 
                    116:     /* Now initialize as from scratch but with the old list of conversions */
                    117:     req->conversions = conversions;
                    118:     req->output_format = WWW_PRESENT;      /* default it to present to user */
                    119: }
                    120: 
                    121: 
1.20      luotonen  122: /*     Delete a request structure
                    123: **     --------------------------
                    124: */
                    125: PUBLIC void HTRequest_delete ARGS1(HTRequest *, req)
                    126: {
                    127:     if (req) {
1.59      frystyk   128:        FREE(req->redirect);
                    129:        FREE(req->authenticate);
                    130:        HTFormatDelete(req);
1.46      frystyk   131:        HTErrorFree(req);
1.34      frystyk   132:        HTAACleanup(req);
1.61      frystyk   133: 
                    134:        /* These are temporary until we get a MIME thingy */
                    135:        FREE(req->redirect);
                    136:        FREE(req->WWWAAScheme);
                    137:        FREE(req->WWWAARealm);
                    138:        FREE(req->WWWprotection);
                    139: 
1.34      frystyk   140:        FREE(req);
1.20      luotonen  141:     }
                    142: }
                    143: 
1.59      frystyk   144: /* --------------------------------------------------------------------------*/
                    145: /*                     Management of HTTP Methods                           */
                    146: /* --------------------------------------------------------------------------*/
1.20      luotonen  147: 
1.22      luotonen  148: PRIVATE char * method_names[(int)MAX_METHODS + 1] =
                    149: {
                    150:     "INVALID-METHOD",
                    151:     "GET",
                    152:     "HEAD",
                    153:     "POST",
                    154:     "PUT",
                    155:     "DELETE",
                    156:     "CHECKOUT",
                    157:     "CHECKIN",
                    158:     "SHOWMETHOD",
                    159:     "LINK",
                    160:     "UNLINK",
                    161:     NULL
                    162: };
                    163: 
                    164: /*     Get method enum value
                    165: **     ---------------------
                    166: */
                    167: PUBLIC HTMethod HTMethod_enum ARGS1(char *, name)
                    168: {
                    169:     if (name) {
                    170:        int i;
                    171:        for (i=1; i < (int)MAX_METHODS; i++)
                    172:            if (!strcmp(name, method_names[i]))
                    173:                return (HTMethod)i;
                    174:     }
                    175:     return METHOD_INVALID;
                    176: }
                    177: 
                    178: 
                    179: /*     Get method name
                    180: **     ---------------
                    181: */
                    182: PUBLIC char * HTMethod_name ARGS1(HTMethod, method)
                    183: {
                    184:     if ((int)method > (int)METHOD_INVALID  && 
                    185:        (int)method < (int)MAX_METHODS)
                    186:        return method_names[(int)method];
                    187:     else
                    188:        return method_names[(int)METHOD_INVALID];
                    189: }
                    190: 
                    191: 
                    192: /*     Is method in a list of method names?
                    193: **     -----------------------------------
                    194: */
                    195: PUBLIC BOOL HTMethod_inList ARGS2(HTMethod,    method,
                    196:                                  HTList *,     list)
                    197: {
                    198:     char * method_name = HTMethod_name(method);
                    199:     HTList *cur = list;
                    200:     char *item;
                    201: 
                    202:     while (NULL != (item = (char*)HTList_nextObject(cur))) {
                    203:        CTRACE(stderr, " %s", item);
                    204:        if (0==strcasecomp(item, method_name))
                    205:            return YES;
                    206:     }
                    207:     return NO; /* Not found */
                    208: }
                    209: 
                    210: 
1.59      frystyk   211: /* --------------------------------------------------------------------------*/
                    212: /*                   Management of the HTProtocol structure                 */
                    213: /* --------------------------------------------------------------------------*/
1.22      luotonen  214: 
1.63    ! frystyk   215: /*
        !           216: **     Register a Protocol as an active access method
1.1       timbl     217: */
1.56      frystyk   218: PUBLIC BOOL HTRegisterProtocol ARGS1(HTProtocol *, protocol)
1.1       timbl     219: {
                    220:     if (!protocols) protocols = HTList_new();
1.59      frystyk   221:     HTList_addObject(protocols, (void *) protocol);
1.1       timbl     222:     return YES;
                    223: }
                    224: 
1.63    ! frystyk   225: 
        !           226: /*
        !           227: **     Delete the list of registered access methods. This is called from
        !           228: **     within HTLibTerminate. Written by Eric Sink, eric@spyglass.com
        !           229: */
        !           230: PUBLIC void HTDisposeProtocols NOARGS
        !           231: {
        !           232:     if (protocols) {
        !           233:        HTList_delete(protocols);
        !           234:        protocols = NULL;
        !           235:     }
        !           236: }
        !           237: 
        !           238: 
        !           239: /*
        !           240: **     Is a protocol registered as BLOCKING? returns YES or NO
        !           241: */
1.59      frystyk   242: PUBLIC BOOL HTProtocolBlocking ARGS1(HTRequest *, me)
                    243: {
1.63    ! frystyk   244:     if (HTInteractive)
        !           245:        return (me && me->anchor && me->anchor->protocol &&
        !           246:                ((HTProtocol *) (me->anchor->protocol))->block == SOC_BLOCK);
        !           247:     return YES;
1.59      frystyk   248: }
                    249: 
1.61      frystyk   250: /* --------------------------------------------------------------------------*/
                    251: /*                Initialization and Termination of the Library             */
                    252: /* --------------------------------------------------------------------------*/
                    253: 
1.1       timbl     254: /*     Register all known protocols
                    255: **     ----------------------------
                    256: **
1.61      frystyk   257: **     Add to or subtract from this list if you add or remove protocol
                    258: **     modules. This function is called from HTLibInit()
1.1       timbl     259: **
                    260: **     Compiling with NO_INIT prevents all known protocols from being forced
                    261: **     in at link time.
                    262: */
1.61      frystyk   263: PRIVATE void HTAccessInit NOARGS
1.1       timbl     264: {
1.59      frystyk   265:     GLOBALREF HTProtocol HTTP, HTFile, HTTelnet, HTTn3270, HTRlogin;
1.1       timbl     266: #ifndef DECNET
1.54      frystyk   267: #ifdef NEW_CODE
1.59      frystyk   268:     GLOBALREF  HTProtocol HTFTP, HTNews, HTNNTP, HTGopher;
                    269: #endif
                    270:     GLOBALREF  HTProtocol HTFTP, HTNews, HTGopher;
                    271: #ifdef DIRECT_WAIS
                    272:     GLOBALREF  HTProtocol HTWAIS;
1.54      frystyk   273: #endif
1.42      frystyk   274: 
1.2       timbl     275:     HTRegisterProtocol(&HTFTP);
                    276:     HTRegisterProtocol(&HTNews);
1.54      frystyk   277: #ifdef NEW_CODE
                    278:     HTRegisterProtocol(&HTNNTP);
                    279: #endif
1.2       timbl     280:     HTRegisterProtocol(&HTGopher);
1.42      frystyk   281: 
1.3       timbl     282: #ifdef DIRECT_WAIS
                    283:     HTRegisterProtocol(&HTWAIS);
                    284: #endif
1.1       timbl     285: 
1.54      frystyk   286: #endif /* DECNET */
1.2       timbl     287:     HTRegisterProtocol(&HTTP);
                    288:     HTRegisterProtocol(&HTFile);
                    289:     HTRegisterProtocol(&HTTelnet);
                    290:     HTRegisterProtocol(&HTTn3270);
                    291:     HTRegisterProtocol(&HTRlogin);
1.1       timbl     292: }
1.61      frystyk   293: 
                    294: 
                    295: /*                                                                  HTLibInit
                    296: **
                    297: **     This function initiates the Library and it MUST be called when
                    298: **     starting up an application. See also HTLibTerminate()
                    299: */
                    300: PUBLIC BOOL HTLibInit NOARGS
                    301: {
                    302:     if (TRACE)
                    303:        fprintf(stderr, "WWWLibInit.. INITIALIZING LIBRARY OF COMMON CODE\n");
1.63    ! frystyk   304: 
        !           305: #ifndef NO_INIT
1.61      frystyk   306:     if (!protocols)
1.62      frystyk   307:        HTAccessInit();                      /* Initilizing protocol modules */
1.63    ! frystyk   308: #endif
1.61      frystyk   309: 
1.62      frystyk   310: #ifdef WWWLIB_SIG
1.61      frystyk   311:     /* On Solaris (and others?) we get a BROKEN PIPE signal when connecting
                    312:     ** to a port where er should get `connection refused'. We ignore this 
                    313:     ** using the following function call
                    314:     */
                    315:     HTSetSignal();                                /* Set signals in library */
1.1       timbl     316: #endif
                    317: 
1.61      frystyk   318:     HTThreadInit();                                /* Initialize bit arrays */
                    319:     return YES;
                    320: }
                    321: 
                    322: 
                    323: /*                                                              HTLibTerminate
                    324: **
                    325: **     This function frees memory kept by the Library and should be called
1.63    ! frystyk   326: **     before exit of an application (if you are on a PC platform)
1.61      frystyk   327: */
                    328: PUBLIC BOOL HTLibTerminate NOARGS
                    329: {
                    330:     if (TRACE)
1.63    ! frystyk   331:        fprintf(stderr, "WWWLibTerm.. Cleaning up LIBRARY OF COMMON CODE\n");
        !           332:     HTAtom_deleteAll();
        !           333:     HTDisposeProtocols();
        !           334:     HTDisposeConversions();
        !           335:     HTFile_deleteSuffixes();
        !           336:     HTTCPCacheRemoveAll();
        !           337:     HTFreeHostName();
        !           338:     HTFreeMailAddress();
1.61      frystyk   339:     return YES;
                    340: }
                    341: 
                    342: 
1.59      frystyk   343: /* --------------------------------------------------------------------------*/
                    344: /*                     Physical Anchor Address Manager                      */
                    345: /* --------------------------------------------------------------------------*/
1.33      luotonen  346: 
                    347: /*                                                     override_proxy()
                    348: **
                    349: **     Check the no_proxy environment variable to get the list
                    350: **     of hosts for which proxy server is not consulted.
                    351: **
                    352: **     no_proxy is a comma- or space-separated list of machine
                    353: **     or domain names, with optional :port part.  If no :port
                    354: **     part is present, it applies to all ports on that domain.
                    355: **
                    356: **     Example:
                    357: **             no_proxy="cern.ch,some.domain:8001"
                    358: **
                    359: */
                    360: PRIVATE BOOL override_proxy ARGS1(CONST char *, addr)
                    361: {
                    362:     CONST char * no_proxy = getenv("no_proxy");
                    363:     char * p = NULL;
                    364:     char * host = NULL;
                    365:     int port = 0;
                    366:     int h_len = 0;
                    367: 
                    368:     if (!no_proxy || !addr || !(host = HTParse(addr, "", PARSE_HOST)))
                    369:        return NO;
                    370:     if (!*host) { free(host); return NO; }
                    371: 
1.34      frystyk   372:     if ((p = strchr(host, ':')) != NULL) {     /* Port specified */
1.33      luotonen  373:        *p++ = 0;                       /* Chop off port */
                    374:        port = atoi(p);
                    375:     }
                    376:     else {                             /* Use default port */
                    377:        char * access = HTParse(addr, "", PARSE_ACCESS);
                    378:        if (access) {
                    379:            if      (!strcmp(access,"http"))    port = 80;
                    380:            else if (!strcmp(access,"gopher"))  port = 70;
                    381:            else if (!strcmp(access,"ftp"))     port = 21;
                    382:            free(access);
                    383:        }
                    384:     }
                    385:     if (!port) port = 80;              /* Default */
                    386:     h_len = strlen(host);
                    387: 
                    388:     while (*no_proxy) {
                    389:        CONST char * end;
                    390:        CONST char * colon = NULL;
                    391:        int templ_port = 0;
                    392:        int t_len;
                    393: 
                    394:        while (*no_proxy && (WHITE(*no_proxy) || *no_proxy==','))
                    395:            no_proxy++;                 /* Skip whitespace and separators */
                    396: 
                    397:        end = no_proxy;
                    398:        while (*end && !WHITE(*end) && *end != ',') {   /* Find separator */
                    399:            if (*end==':') colon = end;                 /* Port number given */
                    400:            end++;
                    401:        }
                    402: 
                    403:        if (colon) {
                    404:            templ_port = atoi(colon+1);
                    405:            t_len = colon - no_proxy;
                    406:        }
                    407:        else {
                    408:            t_len = end - no_proxy;
                    409:        }
                    410: 
                    411:        if ((!templ_port || templ_port == port)  &&
                    412:            (t_len > 0  &&  t_len <= h_len  &&
                    413:             !strncmp(host + h_len - t_len, no_proxy, t_len))) {
                    414:            free(host);
                    415:            return YES;
                    416:        }
                    417:        if (*end) no_proxy = end+1;
                    418:        else break;
                    419:     }
                    420: 
                    421:     free(host);
                    422:     return NO;
                    423: }
                    424: 
                    425: 
                    426: 
1.2       timbl     427: /*             Find physical name and access protocol
                    428: **             --------------------------------------
1.1       timbl     429: **
                    430: **
                    431: ** On entry,
                    432: **     addr            must point to the fully qualified hypertext reference.
                    433: **     anchor          a pareent anchor with whose address is addr
                    434: **
1.59      frystyk   435: ** On exit,    
                    436: **     returns         HT_NO_ACCESS            no protocol module found
                    437: **                     HT_FORBIDDEN            Error has occured.
1.2       timbl     438: **                     HT_OK                   Success
1.1       timbl     439: **
                    440: */
1.21      luotonen  441: PRIVATE int get_physical ARGS1(HTRequest *, req)
                    442: {    
1.1       timbl     443:     char * access=0;   /* Name of access method */
1.21      luotonen  444:     char * addr = HTAnchor_address((HTAnchor*)req->anchor);    /* free me */
1.27      luotonen  445: 
1.2       timbl     446: #ifndef NO_RULES
1.47      luotonen  447:     if (HTImServer) {  /* cern_httpd has already done its own translations */
1.45      luotonen  448:        HTAnchor_setPhysical(req->anchor, HTImServer);
1.47      luotonen  449:        StrAllocCopy(addr, HTImServer); /* Oops, queries thru many proxies */
                    450:                                        /* didn't work without this -- AL  */
                    451:     }
1.21      luotonen  452:     else {
1.27      luotonen  453:        char * physical = HTTranslate(addr);
1.21      luotonen  454:        if (!physical) {
1.47      luotonen  455:            free(addr);
1.21      luotonen  456:            return HT_FORBIDDEN;
                    457:        }
                    458:        HTAnchor_setPhysical(req->anchor, physical);
                    459:        free(physical);                 /* free our copy */
1.2       timbl     460:     }
                    461: #else
1.21      luotonen  462:     HTAnchor_setPhysical(req->anchor, addr);
1.2       timbl     463: #endif
                    464: 
1.21      luotonen  465:     access =  HTParse(HTAnchor_physical(req->anchor),
1.27      luotonen  466:                      "file:", PARSE_ACCESS);
1.1       timbl     467: 
                    468: /*     Check whether gateway access has been set up for this
1.8       timbl     469: **
                    470: **     This function can be replaced by the rule system above.
1.1       timbl     471: */
1.8       timbl     472: #define USE_GATEWAYS
1.1       timbl     473: #ifdef USE_GATEWAYS
1.39      luotonen  474: 
                    475:     /* make sure the using_proxy variable is false */
                    476:     using_proxy = NO;
                    477: 
1.33      luotonen  478:     if (!override_proxy(addr)) {
1.27      luotonen  479:        char * gateway_parameter, *gateway, *proxy;
                    480: 
1.2       timbl     481:        gateway_parameter = (char *)malloc(strlen(access)+20);
                    482:        if (gateway_parameter == NULL) outofmem(__FILE__, "HTLoad");
1.27      luotonen  483: 
                    484:        /* search for proxy gateways */
1.2       timbl     485:        strcpy(gateway_parameter, "WWW_");
                    486:        strcat(gateway_parameter, access);
                    487:        strcat(gateway_parameter, "_GATEWAY");
                    488:        gateway = (char *)getenv(gateway_parameter); /* coerce for decstation */
1.27      luotonen  489: 
                    490:        /* search for proxy servers */
                    491:        strcpy(gateway_parameter, access);
                    492:        strcat(gateway_parameter, "_proxy");
                    493:        proxy = (char *)getenv(gateway_parameter);
                    494: 
1.2       timbl     495:        free(gateway_parameter);
1.27      luotonen  496: 
                    497:        if (TRACE && gateway)
1.60      frystyk   498:            fprintf(stderr,"Gateway..... Found: `%s\'\n", gateway);
1.27      luotonen  499:        if (TRACE && proxy)
1.60      frystyk   500:            fprintf(stderr,"Proxy....... Found: `%s\'\n", proxy);
1.27      luotonen  501: 
1.8       timbl     502: #ifndef DIRECT_WAIS
1.9       timbl     503:        if (!gateway && 0==strcmp(access, "wais")) {
1.8       timbl     504:            gateway = DEFAULT_WAIS_GATEWAY;
                    505:        }
                    506: #endif
1.27      luotonen  507: 
                    508:        /* proxy servers have precedence over gateway servers */
1.60      frystyk   509:        if (proxy && *proxy) {
1.27      luotonen  510:            char * gatewayed=0;
                    511: 
                    512:             StrAllocCopy(gatewayed,proxy);
                    513:            StrAllocCat(gatewayed,addr);
                    514:            using_proxy = YES;
                    515:            HTAnchor_setPhysical(req->anchor, gatewayed);
                    516:            free(gatewayed);
                    517:            free(access);
                    518: 
                    519:            access =  HTParse(HTAnchor_physical(req->anchor),
                    520:                              "http:", PARSE_ACCESS);
1.60      frystyk   521:        } else if (gateway && *gateway) {
1.9       timbl     522:            char * path = HTParse(addr, "",
                    523:                PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
                    524:                /* Chop leading / off to make host into part of path */
                    525:            char * gatewayed = HTParse(path+1, gateway, PARSE_ALL);
                    526:            free(path);
1.21      luotonen  527:             HTAnchor_setPhysical(req->anchor, gatewayed);
1.9       timbl     528:            free(gatewayed);
1.2       timbl     529:            free(access);
1.9       timbl     530:            
1.21      luotonen  531:            access =  HTParse(HTAnchor_physical(req->anchor),
1.8       timbl     532:                "http:", PARSE_ACCESS);
1.2       timbl     533:        }
                    534:     }
1.1       timbl     535: #endif
                    536: 
1.19      timbl     537:     free(addr);
1.1       timbl     538: 
1.61      frystyk   539:     /* Search registered protocols to find suitable one */
1.1       timbl     540:     {
1.61      frystyk   541:        HTList *cur = protocols;
1.20      luotonen  542:        HTProtocol *p;
1.61      frystyk   543:        if (!cur) {
                    544:            if (TRACE)
                    545:                fprintf(stderr, "HTAccess.... NO PROTOCOL MODULES INITIATED\n");
                    546:        } else {
                    547:            while ((p = (HTProtocol*)HTList_nextObject(cur))) {
                    548:                if (strcmp(p->name, access)==0) {
                    549:                    HTAnchor_setProtocol(req->anchor, p);
                    550:                    free(access);
                    551:                    return (HT_OK);
                    552:                }
1.1       timbl     553:            }
                    554:        }
                    555:     }
                    556:     free(access);
1.2       timbl     557:     return HT_NO_ACCESS;
1.1       timbl     558: }
                    559: 
1.59      frystyk   560: /* --------------------------------------------------------------------------*/
                    561: /*                             Document Poster                              */
                    562: /* --------------------------------------------------------------------------*/
                    563: 
                    564: /*             Get a save stream for a document
                    565: **             --------------------------------
                    566: */
                    567: PUBLIC HTStream *HTSaveStream ARGS1(HTRequest *, request)
                    568: {
                    569:     HTProtocol * p;
                    570:     int status;
                    571:     request->method = METHOD_PUT;
                    572:     status = get_physical(request);
                    573:     if (status == HT_FORBIDDEN) {
                    574:        char *url = HTAnchor_address((HTAnchor *) request->anchor);
                    575:        if (url) {
                    576:            HTUnEscape(url);
                    577:            HTErrorAdd(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    578:                       (void *) url, (int) strlen(url), "HTLoad");
                    579:            free(url);
                    580:        } else {
                    581:            HTErrorAdd(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    582:                       NULL, 0, "HTLoad");
                    583:        }
                    584:        return NULL;    /* should return error status? */
                    585:     }
                    586:     if (status < 0) return NULL; /* @@ error. Can't resolve or forbidden */
                    587:     
                    588:     p = (HTProtocol *) HTAnchor_protocol(request->anchor);
                    589:     if (!p) return NULL;
                    590:     
                    591:     return (*p->saveStream)(request);
                    592:     
                    593: }
                    594: 
                    595: 
                    596: /* --------------------------------------------------------------------------*/
                    597: /*                             Document Loader                              */
                    598: /* --------------------------------------------------------------------------*/
1.1       timbl     599: 
                    600: /*             Load a document
                    601: **             ---------------
                    602: **
1.2       timbl     603: **     This is an internal routine, which has an address AND a matching
                    604: **     anchor.  (The public routines are called with one OR the other.)
                    605: **
                    606: ** On entry,
1.15      timbl     607: **     request->
1.35      luotonen  608: **         anchor              a parent anchor with fully qualified
                    609: **                             hypertext reference as its address set
1.15      timbl     610: **         output_format       valid
                    611: **         output_stream       valid on NULL
1.2       timbl     612: **
                    613: ** On exit,
1.59      frystyk   614: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    615: **                     HT_ERROR        Error has occured
1.2       timbl     616: **                     HT_LOADED       Success
                    617: **                     HT_NO_DATA      Success, but no document loaded.
1.8       timbl     618: **                                     (telnet sesssion started etc)
1.2       timbl     619: **
                    620: */
1.52      frystyk   621: PUBLIC int HTLoad ARGS2(HTRequest *, request, BOOL, keep_error_stack)
1.2       timbl     622: {
1.25      frystyk   623:     char       *arg = NULL;
                    624:     HTProtocol *p;
                    625:     int        status;
                    626: 
1.22      luotonen  627:     if (request->method == METHOD_INVALID)
                    628:        request->method = METHOD_GET;
1.52      frystyk   629:     if (!keep_error_stack) {
                    630:        HTErrorFree(request);
                    631:        request->error_block = NO;
                    632:     }
                    633: 
1.59      frystyk   634:     if ((status = get_physical(request)) < 0) {
                    635:        if (status == HT_FORBIDDEN) {
                    636:            char *url = HTAnchor_address((HTAnchor *) request->anchor);
                    637:            if (url) {
                    638:                HTUnEscape(url);
                    639:                HTErrorAdd(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    640:                           (void *) url, (int) strlen(url), "HTLoad");
                    641:                free(url);
                    642:            } else {
                    643:                HTErrorAdd(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    644:                           NULL, 0, "HTLoad");
                    645:            }
                    646:        } 
                    647:        return HT_ERROR;                       /* Can't resolve or forbidden */
1.2       timbl     648:     }
1.25      frystyk   649: 
                    650:     if(!(arg = HTAnchor_physical(request->anchor)) || !*arg) 
1.59      frystyk   651:        return HT_ERROR;
1.27      luotonen  652: 
1.56      frystyk   653:     p = (HTProtocol *) HTAnchor_protocol(request->anchor);
1.17      timbl     654:     return (*(p->load))(request);
1.2       timbl     655: }
                    656: 
                    657: 
1.61      frystyk   658: /*             Terminate a LOAD
                    659: **             ----------------
                    660: **
                    661: **     This function looks at the status code from the HTLoadDocument
                    662: **     function and updates logfiles, creates error messages etc.
                    663: **
                    664: **    On Entry,
                    665: **     Status code from load function
                    666: */
                    667: PUBLIC BOOL HTLoadTerminate ARGS2(HTRequest *, request, int, status)
                    668: {
                    669:     char * uri = HTAnchor_address((HTAnchor*)request->anchor);
                    670: 
                    671:     /* Log the access if necessary */
                    672:     if (HTlogfile) {
                    673:        time_t theTime;
                    674:        time(&theTime);
                    675:        fprintf(HTlogfile, "%24.24s %s %s %s\n",
                    676:            ctime(&theTime),
                    677:            HTClientHost ? HTClientHost : "local",
                    678:            status<0 ? "FAIL" : "GET", uri);
                    679:        fflush(HTlogfile);      /* Actually update it on disk */
                    680:        if (PROT_TRACE) fprintf(stderr, "Log: %24.24s %s %s %s\n",
                    681:            ctime(&theTime),
                    682:            HTClientHost ? HTClientHost : "local",
                    683:            status<0 ? "FAIL" : "GET", uri);
                    684:     }
                    685: 
                    686:     /* The error stack might contain general information to the client
                    687:        about what has been going on in the library (not only errors) */
                    688:     if (!HTImProxy && request->error_stack)
                    689:        HTErrorMsg(request);
                    690: 
                    691:     switch (status) {
                    692:       case HT_LOADED:
                    693:        if (PROT_TRACE) {
                    694:            fprintf(stderr, "HTAccess.... OK: `%s' has been accessed.\n", uri);
                    695:        }
                    696:        break;
                    697: 
                    698:       case HT_NO_DATA:
                    699:        if (PROT_TRACE) {
                    700:            fprintf(stderr, "HTAccess.... OK BUT NO DATA: `%s'\n", uri);
                    701:        }
                    702:        break;
                    703: 
                    704:       case HT_WOULD_BLOCK:
                    705:        if (PROT_TRACE) {
                    706:            fprintf(stderr, "HTAccess.... WOULD BLOCK: `%s'\n", uri);
                    707:        }
                    708:        break;
                    709: 
                    710:       case HT_ERROR:
                    711:        if (HTImProxy)
                    712:            HTErrorMsg(request);                     /* Only on a real error */
                    713:        if (PROT_TRACE) {
                    714:            fprintf(stderr, "HTAccess.... ERROR: Can't access `%s'\n", uri);
                    715:        }
                    716:        break;
                    717: 
                    718:       default:
                    719:        if (PROT_TRACE) {
                    720:            fprintf(stderr, "HTAccess.... Internal software error in CERN WWWLib version %s ****\n\nPlease mail www-bug@info.cern.ch quoting what software and what version you are using\nand the URL: %s that caused the problem, thanks!\n",
                    721:                    HTLibraryVersion, uri);
                    722:        }
                    723:        break;
                    724:     }
                    725:     free(uri);
                    726:     return YES;
                    727: }
                    728: 
                    729: 
1.2       timbl     730: /*             Load a document - with logging etc
                    731: **             ----------------------------------
                    732: **
                    733: **     - Checks or documents already loaded
                    734: **     - Logs the access
                    735: **     - Allows stdin filter option
                    736: **     - Trace ouput and error messages
                    737: **
1.1       timbl     738: **    On Entry,
1.19      timbl     739: **        request->anchor      valid for of the document to be accessed.
                    740: **      request->childAnchor   optional anchor within doc to be selected
                    741: **
1.2       timbl     742: **        filter            if YES, treat stdin as HTML
1.1       timbl     743: **
1.15      timbl     744: **       request->anchor   is the node_anchor for the document
                    745: **       request->output_format is valid
                    746: **
1.59      frystyk   747: ** On exit,
                    748: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    749: **                     HT_ERROR        Error has occured
                    750: **                     HT_LOADED       Success
                    751: **                     HT_NO_DATA      Success, but no document loaded.
                    752: **                                     (telnet sesssion started etc)
1.1       timbl     753: */
1.59      frystyk   754: PRIVATE int HTLoadDocument ARGS2(HTRequest *,  request,
                    755:                                 BOOL,          keep_error_stack)
1.1       timbl     756: 
                    757: {
                    758:     int                status;
                    759:     HText *    text;
1.19      timbl     760:     char * full_address = HTAnchor_address((HTAnchor*)request->anchor);
1.54      frystyk   761: 
1.59      frystyk   762:     if (PROT_TRACE) fprintf (stderr, "HTAccess.... Loading document %s\n",
                    763:                             full_address);
1.1       timbl     764: 
1.18      timbl     765:     request->using_cache = NULL;
                    766:     
1.15      timbl     767:     if (!request->output_format) request->output_format = WWW_PRESENT;
1.25      frystyk   768: 
1.63    ! frystyk   769:     /* Check if document is already loaded */
1.31      frystyk   770:     if (!HTForceReload && (text=(HText *)HTAnchor_document(request->anchor)))
1.63    ! frystyk   771:     {
1.59      frystyk   772:         if (PROT_TRACE)
                    773:            fprintf(stderr, "HTAccess.... Document already in memory.\n");
1.19      timbl     774:        if (request->childAnchor) {
                    775:            HText_selectAnchor(text, request->childAnchor);
                    776:        } else {
                    777:            HText_select(text); 
                    778:        }
                    779:        free(full_address);
1.59      frystyk   780:        return HT_LOADED;
1.1       timbl     781:     }
1.17      timbl     782:     
1.34      frystyk   783:     /* Check the Cache */
1.17      timbl     784:     /* Bug: for each format, we only check whether it is ok, we
                    785:        don't check them all and chose the best */
1.54      frystyk   786:     if (request->anchor->cacheItems) {
1.17      timbl     787:         HTList * list = request->anchor->cacheItems;
1.20      luotonen  788:        HTList * cur = list;
                    789:        HTCacheItem * item;
                    790: 
                    791:        while ((item = (HTCacheItem*)HTList_nextObject(cur))) {
1.18      timbl     792:            HTStream * s;
                    793:            
                    794:            request->using_cache = item;
                    795:            
1.59      frystyk   796:            s = HTStreamStack(item->format, request->output_format,
                    797:                              request->output_stream, request, NO);
1.17      timbl     798:            if (s) {            /* format was suitable */
                    799:                FILE * fp = fopen(item->filename, "r");
1.59      frystyk   800:                if (PROT_TRACE) 
1.57      howcome   801:                    fprintf(stderr, "Cache: HIT file %s for %s\n",
1.20      luotonen  802:                                   item->filename, 
                    803:                                   full_address);
1.17      timbl     804:                if (fp) {
                    805:                    HTFileCopy(fp, s);
1.53      duns      806:                    (*s->isa->_free)(s);        /* close up pipeline */
1.17      timbl     807:                    fclose(fp);
1.19      timbl     808:                    free(full_address);
1.59      frystyk   809:                    return HT_LOADED;
1.17      timbl     810:                } else {
                    811:                    fprintf(stderr, "***** Can't read cache file %s !\n",
1.20      luotonen  812:                            item->filename);
1.17      timbl     813:                } /* file open ok */
                    814:            } /* stream ok */
                    815:        } /* next cache item */
                    816:     } /* if cache available for this anchor */
1.1       timbl     817:     
1.61      frystyk   818:     if ((status = HTLoad(request, keep_error_stack)) != HT_WOULD_BLOCK)
                    819:        HTLoadTerminate(request, status);
1.19      timbl     820:     free(full_address);
1.59      frystyk   821:     return status;
1.58      frystyk   822: }
1.1       timbl     823: 
                    824: 
                    825: /*             Load a document from absolute name
                    826: **             ---------------
                    827: **
1.59      frystyk   828: ** On Entry,
1.1       timbl     829: **        addr     The absolute address of the document to be accessed.
                    830: **        filter   if YES, treat document as HTML
                    831: **
1.59      frystyk   832: ** On exit,
                    833: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    834: **                     HT_ERROR        Error has occured
                    835: **                     HT_LOADED       Success
                    836: **                     HT_NO_DATA      Success, but no document loaded.
                    837: **                                     (telnet sesssion started etc)
1.1       timbl     838: */
                    839: 
1.59      frystyk   840: PUBLIC int HTLoadAbsolute ARGS2(CONST char *,addr, HTRequest*, request)
1.2       timbl     841: {
1.19      timbl     842:    HTAnchor * anchor = HTAnchor_findAddress(addr);
                    843:    request->anchor = HTAnchor_parent(anchor);
                    844:    request->childAnchor = ((HTAnchor*)request->anchor == anchor) ?
                    845:                        NULL : (HTChildAnchor*) anchor;
1.52      frystyk   846:    return HTLoadDocument(request, NO);
1.2       timbl     847: }
                    848: 
                    849: 
                    850: /*             Load a document from absolute name to stream
                    851: **             --------------------------------------------
                    852: **
1.59      frystyk   853: ** On Entry,
1.2       timbl     854: **        addr     The absolute address of the document to be accessed.
1.15      timbl     855: **        request->output_stream     if non-NULL, send data down this stream
1.2       timbl     856: **
1.59      frystyk   857: ** On exit,
                    858: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    859: **                     HT_ERROR        Error has occured
                    860: **                     HT_LOADED       Success
                    861: **                     HT_NO_DATA      Success, but no document loaded.
                    862: **                                     (telnet sesssion started etc)
1.2       timbl     863: */
                    864: 
1.59      frystyk   865: PUBLIC int HTLoadToStream ARGS3(CONST char *,  addr,
                    866:                                BOOL,           filter,
                    867:                                HTRequest*,     request)
1.1       timbl     868: {
1.63    ! frystyk   869:     HTAnchor * anchor = HTAnchor_findAddress(addr);
        !           870:     request->anchor = HTAnchor_parent(anchor);
        !           871:     request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL :
1.19      timbl     872:        (HTChildAnchor*) anchor;
1.15      timbl     873:     request->output_stream = request->output_stream;
1.52      frystyk   874:     return HTLoadDocument(request, NO);
1.1       timbl     875: }
                    876: 
                    877: 
                    878: /*             Load a document from relative name
                    879: **             ---------------
                    880: **
1.59      frystyk   881: ** On Entry,
1.2       timbl     882: **        relative_name     The relative address of the document
                    883: **                         to be accessed.
1.1       timbl     884: **
1.59      frystyk   885: ** On exit,
                    886: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    887: **                     HT_ERROR        Error has occured
                    888: **                     HT_LOADED       Success
                    889: **                     HT_NO_DATA      Success, but no document loaded.
                    890: **                                     (telnet sesssion started etc)
1.1       timbl     891: */
                    892: 
1.59      frystyk   893: PUBLIC int HTLoadRelative ARGS3(CONST char *,          relative_name,
                    894:                                HTParentAnchor *,       here,
                    895:                                HTRequest *,            request)
1.1       timbl     896: {
                    897:     char *             full_address = 0;
                    898:     BOOL                       result;
                    899:     char *             mycopy = 0;
                    900:     char *             stripped = 0;
                    901:     char *             current_address =
1.2       timbl     902:                                HTAnchor_address((HTAnchor*)here);
1.1       timbl     903: 
                    904:     StrAllocCopy(mycopy, relative_name);
                    905: 
                    906:     stripped = HTStrip(mycopy);
                    907:     full_address = HTParse(stripped,
                    908:                   current_address,
                    909:                   PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.15      timbl     910:     result = HTLoadAbsolute(full_address, request);
1.1       timbl     911:     free(full_address);
                    912:     free(current_address);
                    913:     free(mycopy);  /* Memory leak fixed 10/7/92 -- JFG */
                    914:     return result;
                    915: }
                    916: 
                    917: 
                    918: /*             Load if necessary, and select an anchor
                    919: **             --------------------------------------
                    920: **
1.59      frystyk   921: ** On Entry,
1.1       timbl     922: **        destination              The child or parenet anchor to be loaded.
                    923: **
1.59      frystyk   924: ** On exit,
                    925: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    926: **                     HT_ERROR        Error has occured
                    927: **                     HT_LOADED       Success
                    928: **                     HT_NO_DATA      Success, but no document loaded.
                    929: **                                     (telnet sesssion started etc)
1.1       timbl     930: */
                    931: 
1.59      frystyk   932: PUBLIC int HTLoadAnchor ARGS2(HTAnchor*, anchor, HTRequest *, request)
1.1       timbl     933: {
1.59      frystyk   934:     if (!anchor) return HT_ERROR;                                /* No link */
1.1       timbl     935:     
1.15      timbl     936:     request->anchor  = HTAnchor_parent(anchor);
1.59      frystyk   937:     request->childAnchor = ((HTAnchor *) request->anchor == anchor) ?
                    938:        NULL : (HTChildAnchor*) anchor;
                    939:     return HTLoadDocument(request, NO);
                    940: }
1.52      frystyk   941: 
                    942: 
                    943: /*             Load if necessary, and select an anchor
                    944: **             --------------------------------------
                    945: **
                    946: **     This function is almost identical to HTLoadAnchor, but it doesn't
                    947: **     clear the error stack so that the information in there is kept.
                    948: **
1.59      frystyk   949: ** On Entry,
1.52      frystyk   950: **        destination              The child or parenet anchor to be loaded.
                    951: **
1.59      frystyk   952: ** On exit,
                    953: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    954: **                     HT_ERROR        Error has occured
                    955: **                     HT_LOADED       Success
                    956: **                     HT_NO_DATA      Success, but no document loaded.
                    957: **                                     (telnet sesssion started etc)
1.52      frystyk   958: */
                    959: 
1.59      frystyk   960: PUBLIC int HTLoadAnchorRecursive ARGS2(HTAnchor*,      anchor,
                    961:                                       HTRequest *,     request)
1.52      frystyk   962: {
1.59      frystyk   963:     if (!anchor) return HT_ERROR;                                /* No link */
1.52      frystyk   964:     
                    965:     request->anchor  = HTAnchor_parent(anchor);
1.59      frystyk   966:     request->childAnchor = ((HTAnchor *) request->anchor == anchor) ?
                    967:        NULL : (HTChildAnchor*) anchor;
1.52      frystyk   968:     
1.59      frystyk   969:     return HTLoadDocument(request, YES);
                    970: }
1.1       timbl     971: 
                    972: 
                    973: /*             Search
                    974: **             ------
                    975: **  Performs a keyword search on word given by the user. Adds the keyword to 
                    976: **  the end of the current address and attempts to open the new address.
                    977: **
                    978: **  On Entry,
                    979: **       *keywords     space-separated keyword list or similar search list
1.2       timbl     980: **     here            is anchor search is to be done on.
1.59      frystyk   981: **
                    982: ** On exit,
                    983: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    984: **                     HT_ERROR        Error has occured
                    985: **                     HT_LOADED       Success
                    986: **                     HT_NO_DATA      Success, but no document loaded.
                    987: **                                     (telnet sesssion started etc)
1.1       timbl     988: */
                    989: 
1.56      frystyk   990: PRIVATE char hex ARGS1(int, i)
1.2       timbl     991: {
1.13      timbl     992:     char * hexchars = "0123456789ABCDEF";
                    993:     return hexchars[i];
1.2       timbl     994: }
1.1       timbl     995: 
1.59      frystyk   996: PUBLIC int HTSearch ARGS3(CONST char *,                keywords,
                    997:                          HTParentAnchor *,     here,
                    998:                          HTRequest *,          request)
1.1       timbl     999: {
1.2       timbl    1000: 
                   1001: #define acceptable \
                   1002: "1234567890abcdefghijlkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"
                   1003: 
                   1004:     char *q, *u;
                   1005:     CONST char * p, *s, *e;            /* Pointers into keywords */
                   1006:     char * address = HTAnchor_address((HTAnchor*)here);
1.1       timbl    1007:     BOOL result;
1.56      frystyk  1008:     char * escaped = (char *) malloc(strlen(keywords)*3+1);
1.2       timbl    1009: 
1.29      frystyk  1010:     /* static CONST BOOL isAcceptable[96] = */
                   1011:     /* static AND const is not good for a gnu compiler! Frystyk 25/02-94 */
1.30      luotonen 1012:     static BOOL isAcceptable[96] =
1.2       timbl    1013:     /*   0 1 2 3 4 5 6 7 8 9 A B C D E F */
                   1014:     {    0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,      /* 2x   !"#$%&'()*+,-./  */
                   1015:          1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,      /* 3x  0123456789:;<=>?  */
                   1016:         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,       /* 4x  @ABCDEFGHIJKLMNO  */
                   1017:         1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,       /* 5X  PQRSTUVWXYZ[\]^_  */
                   1018:         0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,       /* 6x  `abcdefghijklmno  */
                   1019:         1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0 };     /* 7X  pqrstuvwxyz{\}~  DEL */
                   1020: 
                   1021:     if (escaped == NULL) outofmem(__FILE__, "HTSearch");
                   1022:     
1.29      frystyk  1023: /* Convert spaces to + and hex escape unacceptable characters */
1.2       timbl    1024: 
1.29      frystyk  1025:     for(s=keywords; *s && WHITE(*s); s++); /*scan */    /* Skip white space */
                   1026:     for(e = s + strlen(s); e>s && WHITE(*(e-1)) ; e--);     /* Skip trailers */
                   1027:     for(q=escaped, p=s; p<e; p++) {                  /* scan stripped field */
1.2       timbl    1028:         int c = (int)TOASCII(*p);
                   1029:         if (WHITE(*p)) {
                   1030:            *q++ = '+';
1.29      frystyk  1031:        } else if (c>=32 && c<=127 && isAcceptable[c-32] != 0) {
1.13      timbl    1032:            *q++ = *p;                  /* 930706 TBL for MVS bug */
1.2       timbl    1033:        } else {
                   1034:            *q++ = '%';
                   1035:            *q++ = hex(c / 16);
                   1036:            *q++ = hex(c % 16);
                   1037:        }
                   1038:     } /* Loop over string */
1.1       timbl    1039:     
1.2       timbl    1040:     *q=0;
                   1041:                                /* terminate escaped sctring */
                   1042:     u=strchr(address, '?');            /* Find old search string */
                   1043:     if (u) *u = 0;                             /* Chop old search off */
1.1       timbl    1044: 
                   1045:     StrAllocCat(address, "?");
1.2       timbl    1046:     StrAllocCat(address, escaped);
                   1047:     free(escaped);
1.15      timbl    1048:     result = HTLoadRelative(address, here, request);
1.1       timbl    1049:     free(address);
1.2       timbl    1050:     
1.1       timbl    1051:     return result;
1.2       timbl    1052: }
                   1053: 
                   1054: 
                   1055: /*             Search Given Indexname
                   1056: **             ------
                   1057: **  Performs a keyword search on word given by the user. Adds the keyword to 
                   1058: **  the end of the current address and attempts to open the new address.
                   1059: **
1.59      frystyk  1060: ** On Entry,
1.2       timbl    1061: **       *keywords     space-separated keyword list or similar search list
                   1062: **     *addres         is name of object search is to be done on.
1.59      frystyk  1063: ** On exit,
                   1064: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                   1065: **                     HT_ERROR        Error has occured
                   1066: **                     HT_LOADED       Success
                   1067: **                     HT_NO_DATA      Success, but no document loaded.
                   1068: **                                     (telnet sesssion started etc)
1.2       timbl    1069: */
                   1070: 
1.59      frystyk  1071: PUBLIC int HTSearchAbsolute ARGS3(CONST char *,        keywords,
                   1072:                                  CONST char *,         indexname,
                   1073:                                  HTRequest *,          request)
1.2       timbl    1074: {
                   1075:     HTParentAnchor * anchor =
                   1076:        (HTParentAnchor*) HTAnchor_findAddress(indexname);
1.15      timbl    1077:     return HTSearch(keywords, anchor, request);
1.57      howcome  1078: }
                   1079: 
                   1080: 
                   1081: /*
                   1082: **             Find Related Name
                   1083: **
                   1084: **  Creates a string that can be used as a related name when 
                   1085: **  calling HTParse initially. 
                   1086: **  
                   1087: **  The code for this routine originates from the Linemode 
                   1088: **  browser and was moved here by howcome@dxcern.cern.ch
                   1089: **  in order for all clients to take advantage.
                   1090: **
1.59      frystyk  1091: **  The string returned must be freed by the caller
1.57      howcome  1092: */
                   1093: PUBLIC char * HTFindRelatedName NOARGS
                   1094: {
1.59      frystyk  1095:     char* default_default = NULL;            /* Parse home relative to this */
                   1096:     CONST char *host = HTGetHostName(); 
1.57      howcome  1097:     StrAllocCopy(default_default, "file://");
1.59      frystyk  1098:     if (host)
                   1099:        StrAllocCat(default_default, host);
                   1100:     else
                   1101:        StrAllocCat(default_default, "localhost");
                   1102:     {
                   1103:        char wd[HT_MAX_PATH+1];
1.57      howcome  1104: 
1.59      frystyk  1105: #ifdef NO_GETWD
                   1106: #ifdef HAS_GETCWD            /* System V variant SIGN CHANGED TBL 921006 !! */
                   1107:        char *result = (char *) getcwd(wd, sizeof(wd)); 
                   1108: #else
                   1109:        char *result = NULL;
                   1110:        HTAlert("This platform does not support neither getwd nor getcwd\n");
1.57      howcome  1111: #endif
1.59      frystyk  1112: #else
                   1113:        char *result = (char *) getwd(wd);
                   1114: #endif
                   1115:        *(wd+HT_MAX_PATH) = '\0';
1.57      howcome  1116:        if (result) {
                   1117: #ifdef VMS 
                   1118:             /* convert directory name to Unix-style syntax */
                   1119:            char * disk = strchr (wd, ':');
                   1120:            char * dir = strchr (wd, '[');
                   1121:            if (disk) {
                   1122:                *disk = '\0';
                   1123:                StrAllocCat (default_default, "/");  /* needs delimiter */
                   1124:                StrAllocCat (default_default, wd);
                   1125:            }
                   1126:            if (dir) {
                   1127:                char *p;
                   1128:                *dir = '/';  /* Convert leading '[' */
                   1129:                for (p = dir ; *p != ']'; ++p)
                   1130:                        if (*p == '.') *p = '/';
                   1131:                *p = '\0';  /* Cut on final ']' */
                   1132:                StrAllocCat (default_default, dir);
                   1133:            }
                   1134: #else  /* not VMS */
                   1135:            StrAllocCat (default_default, wd);
1.59      frystyk  1136: #endif /* not VMS */
1.57      howcome  1137:        }
1.59      frystyk  1138:     }
1.57      howcome  1139:     StrAllocCat(default_default, "/default.html");
                   1140:     return default_default;
1.2       timbl    1141: }
                   1142: 
                   1143: 
                   1144: /*             Generate the anchor for the home page
                   1145: **             -------------------------------------
                   1146: **
                   1147: **     As it involves file access, this should only be done once
                   1148: **     when the program first runs.
1.10      timbl    1149: **     This is a default algorithm -- browser don't HAVE to use this.
                   1150: **     But consistency betwen browsers is STRONGLY recommended!
1.2       timbl    1151: **
1.10      timbl    1152: **     Priority order is:
                   1153: **
                   1154: **             1       WWW_HOME environment variable (logical name, etc)
                   1155: **             2       ~/WWW/default.html
                   1156: **             3       /usr/local/bin/default.html
                   1157: **             4       http://info.cern.ch/default.html
                   1158: **
1.2       timbl    1159: */
                   1160: PUBLIC HTParentAnchor * HTHomeAnchor NOARGS
                   1161: {
1.12      timbl    1162:     char * my_home_document = NULL;
                   1163:     char * home = (char *)getenv(LOGICAL_DEFAULT);
1.2       timbl    1164:     char * ref;
                   1165:     HTParentAnchor * anchor;
1.1       timbl    1166:     
1.12      timbl    1167:     if (home) {
                   1168:         StrAllocCopy(my_home_document, home);
                   1169:     
                   1170: /*     Someone telnets in, they get a special home.
                   1171: */
                   1172:     } else  if (HTClientHost) {                        /* Telnet server */
                   1173:        FILE * fp = fopen(REMOTE_POINTER, "r");
                   1174:        char * status;
                   1175:        if (fp) {
1.59      frystyk  1176:            my_home_document = (char*) malloc(HT_MAX_PATH);
                   1177:            status = fgets(my_home_document, HT_MAX_PATH, fp);
1.12      timbl    1178:            if (!status) {
                   1179:                free(my_home_document);
                   1180:                my_home_document = NULL;
                   1181:            }
                   1182:            fclose(fp);
                   1183:        }
                   1184:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                   1185:     }
                   1186: 
                   1187:     
                   1188: 
1.2       timbl    1189: #ifdef unix
1.12      timbl    1190: 
1.10      timbl    1191:     if (!my_home_document) {
                   1192:        FILE * fp = NULL;
                   1193:        CONST char * home =  (CONST char*)getenv("HOME");
                   1194:        if (home) { 
                   1195:            my_home_document = (char *)malloc(
                   1196:                strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1);
                   1197:            if (my_home_document == NULL) outofmem(__FILE__, "HTLocalName");
                   1198:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                   1199:            fp = fopen(my_home_document, "r");
                   1200:        }
                   1201:        
                   1202:        if (!fp) {
                   1203:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                   1204:            fp = fopen(my_home_document, "r");
                   1205:        }
1.2       timbl    1206:        if (fp) {
                   1207:            fclose(fp);
                   1208:        } else {
1.62      frystyk  1209:            if (TRACE)
                   1210:                fprintf(stderr,
                   1211:                        "HTBrowse: No local home document ~/%s or %s\n",
                   1212:                        PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
1.11      timbl    1213:            free(my_home_document);
                   1214:            my_home_document = NULL;
1.2       timbl    1215:        }
                   1216:     }
                   1217: #endif
1.10      timbl    1218:     ref = HTParse( my_home_document ?  my_home_document :
                   1219:                                HTClientHost ? REMOTE_ADDRESS
                   1220:                                : LAST_RESORT,
                   1221:                    "file:",
1.2       timbl    1222:                    PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.10      timbl    1223:     if (my_home_document) {
1.62      frystyk  1224:        if (TRACE)
                   1225:            fprintf(stderr,
                   1226:                   "HTAccess.... `%s\' used for custom home page as\n`%s\'\n",
                   1227:                    my_home_document, ref);
1.10      timbl    1228:        free(my_home_document);
1.2       timbl    1229:     }
                   1230:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
                   1231:     free(ref);
                   1232:     return anchor;
1.1       timbl    1233: }
1.26      frystyk  1234: 
                   1235: 
                   1236: /*             Bind an Anchor to the request structure
                   1237: **             ---------------------------------------
                   1238: **
                   1239: **    On Entry,
                   1240: **     anchor          The child or parenet anchor to be binded
                   1241: **     request         The request sturcture
                   1242: **    On Exit,
                   1243: **        returns    YES     Success
                   1244: **                   NO      Failure 
                   1245: **
                   1246: **  Note: Actually the same as HTLoadAnchor() but DOES NOT do the loading
                   1247: **                                             Henrik Frystyk 17/02-94
                   1248: */
                   1249: 
                   1250: PUBLIC BOOL HTBindAnchor ARGS2(HTAnchor*, anchor, HTRequest *, request)
                   1251: {
                   1252:     if (!anchor) return NO;    /* No link */
                   1253:     
                   1254:     request->anchor  = HTAnchor_parent(anchor);
                   1255:     request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL
                   1256:                                        : (HTChildAnchor*) anchor;
                   1257:        
1.29      frystyk  1258:     return YES;
1.26      frystyk  1259: } /* HTBindAnchor */
1.59      frystyk  1260: 
1.26      frystyk  1261: 

Webmaster