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

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

Webmaster