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

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

Webmaster