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

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

Webmaster