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

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.63      frystyk   773:     /* Check if document is already loaded */
1.31      frystyk   774:     if (!HTForceReload && (text=(HText *)HTAnchor_document(request->anchor)))
1.63      frystyk   775:     {
1.59      frystyk   776:         if (PROT_TRACE)
                    777:            fprintf(stderr, "HTAccess.... Document already in memory.\n");
1.19      timbl     778:        if (request->childAnchor) {
                    779:            HText_selectAnchor(text, request->childAnchor);
                    780:        } else {
                    781:            HText_select(text); 
                    782:        }
                    783:        free(full_address);
1.59      frystyk   784:        return HT_LOADED;
1.1       timbl     785:     }
1.17      timbl     786:     
1.34      frystyk   787:     /* Check the Cache */
1.17      timbl     788:     /* Bug: for each format, we only check whether it is ok, we
                    789:        don't check them all and chose the best */
1.54      frystyk   790:     if (request->anchor->cacheItems) {
1.17      timbl     791:         HTList * list = request->anchor->cacheItems;
1.20      luotonen  792:        HTList * cur = list;
                    793:        HTCacheItem * item;
                    794: 
                    795:        while ((item = (HTCacheItem*)HTList_nextObject(cur))) {
1.18      timbl     796:            HTStream * s;
                    797:            
                    798:            request->using_cache = item;
                    799:            
1.59      frystyk   800:            s = HTStreamStack(item->format, request->output_format,
                    801:                              request->output_stream, request, NO);
1.17      timbl     802:            if (s) {            /* format was suitable */
                    803:                FILE * fp = fopen(item->filename, "r");
1.59      frystyk   804:                if (PROT_TRACE) 
1.65.2.1  frystyk   805:                    fprintf(stderr, "Cache...... HIT file %s for %s\n",
                    806:                            item->filename, 
                    807:                            full_address);
1.17      timbl     808:                if (fp) {
                    809:                    HTFileCopy(fp, s);
1.53      duns      810:                    (*s->isa->_free)(s);        /* close up pipeline */
1.17      timbl     811:                    fclose(fp);
1.19      timbl     812:                    free(full_address);
1.59      frystyk   813:                    return HT_LOADED;
1.17      timbl     814:                } else {
                    815:                    fprintf(stderr, "***** Can't read cache file %s !\n",
1.20      luotonen  816:                            item->filename);
1.17      timbl     817:                } /* file open ok */
                    818:            } /* stream ok */
                    819:        } /* next cache item */
                    820:     } /* if cache available for this anchor */
1.1       timbl     821:     
1.61      frystyk   822:     if ((status = HTLoad(request, keep_error_stack)) != HT_WOULD_BLOCK)
                    823:        HTLoadTerminate(request, status);
1.19      timbl     824:     free(full_address);
1.59      frystyk   825:     return status;
1.58      frystyk   826: }
1.1       timbl     827: 
                    828: 
                    829: /*             Load a document from absolute name
                    830: **             ---------------
                    831: **
1.59      frystyk   832: ** On Entry,
1.1       timbl     833: **        addr     The absolute address of the document to be accessed.
                    834: **
1.59      frystyk   835: ** On exit,
                    836: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    837: **                     HT_ERROR        Error has occured
                    838: **                     HT_LOADED       Success
                    839: **                     HT_NO_DATA      Success, but no document loaded.
                    840: **                                     (telnet sesssion started etc)
1.1       timbl     841: */
                    842: 
1.59      frystyk   843: PUBLIC int HTLoadAbsolute ARGS2(CONST char *,addr, HTRequest*, request)
1.2       timbl     844: {
1.19      timbl     845:    HTAnchor * anchor = HTAnchor_findAddress(addr);
                    846:    request->anchor = HTAnchor_parent(anchor);
                    847:    request->childAnchor = ((HTAnchor*)request->anchor == anchor) ?
                    848:                        NULL : (HTChildAnchor*) anchor;
1.52      frystyk   849:    return HTLoadDocument(request, NO);
1.2       timbl     850: }
                    851: 
                    852: 
                    853: /*             Load a document from absolute name to stream
                    854: **             --------------------------------------------
                    855: **
1.59      frystyk   856: ** On Entry,
1.2       timbl     857: **        addr     The absolute address of the document to be accessed.
1.15      timbl     858: **        request->output_stream     if non-NULL, send data down this stream
1.2       timbl     859: **
1.59      frystyk   860: ** On exit,
                    861: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    862: **                     HT_ERROR        Error has occured
                    863: **                     HT_LOADED       Success
                    864: **                     HT_NO_DATA      Success, but no document loaded.
                    865: **                                     (telnet sesssion started etc)
1.2       timbl     866: */
                    867: 
1.59      frystyk   868: PUBLIC int HTLoadToStream ARGS3(CONST char *,  addr,
                    869:                                BOOL,           filter,
                    870:                                HTRequest*,     request)
1.1       timbl     871: {
1.63      frystyk   872:     HTAnchor * anchor = HTAnchor_findAddress(addr);
                    873:     request->anchor = HTAnchor_parent(anchor);
                    874:     request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL :
1.19      timbl     875:        (HTChildAnchor*) anchor;
1.15      timbl     876:     request->output_stream = request->output_stream;
1.52      frystyk   877:     return HTLoadDocument(request, NO);
1.1       timbl     878: }
                    879: 
                    880: 
                    881: /*             Load a document from relative name
                    882: **             ---------------
                    883: **
1.59      frystyk   884: ** On Entry,
1.2       timbl     885: **        relative_name     The relative address of the document
                    886: **                         to be accessed.
1.1       timbl     887: **
1.59      frystyk   888: ** On exit,
                    889: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    890: **                     HT_ERROR        Error has occured
                    891: **                     HT_LOADED       Success
                    892: **                     HT_NO_DATA      Success, but no document loaded.
                    893: **                                     (telnet sesssion started etc)
1.1       timbl     894: */
                    895: 
1.59      frystyk   896: PUBLIC int HTLoadRelative ARGS3(CONST char *,          relative_name,
                    897:                                HTParentAnchor *,       here,
                    898:                                HTRequest *,            request)
1.1       timbl     899: {
                    900:     char *             full_address = 0;
1.65      frystyk   901:     int                result;
1.1       timbl     902:     char *             mycopy = 0;
                    903:     char *             stripped = 0;
                    904:     char *             current_address =
1.2       timbl     905:                                HTAnchor_address((HTAnchor*)here);
1.1       timbl     906: 
                    907:     StrAllocCopy(mycopy, relative_name);
                    908: 
                    909:     stripped = HTStrip(mycopy);
                    910:     full_address = HTParse(stripped,
                    911:                   current_address,
                    912:                   PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.15      timbl     913:     result = HTLoadAbsolute(full_address, request);
1.1       timbl     914:     free(full_address);
                    915:     free(current_address);
                    916:     free(mycopy);  /* Memory leak fixed 10/7/92 -- JFG */
                    917:     return result;
                    918: }
                    919: 
                    920: 
                    921: /*             Load if necessary, and select an anchor
                    922: **             --------------------------------------
                    923: **
1.59      frystyk   924: ** On Entry,
1.1       timbl     925: **        destination              The child or parenet anchor to be loaded.
                    926: **
1.59      frystyk   927: ** On exit,
                    928: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    929: **                     HT_ERROR        Error has occured
                    930: **                     HT_LOADED       Success
                    931: **                     HT_NO_DATA      Success, but no document loaded.
                    932: **                                     (telnet sesssion started etc)
1.1       timbl     933: */
                    934: 
1.59      frystyk   935: PUBLIC int HTLoadAnchor ARGS2(HTAnchor*, anchor, HTRequest *, request)
1.1       timbl     936: {
1.59      frystyk   937:     if (!anchor) return HT_ERROR;                                /* No link */
1.1       timbl     938:     
1.15      timbl     939:     request->anchor  = HTAnchor_parent(anchor);
1.59      frystyk   940:     request->childAnchor = ((HTAnchor *) request->anchor == anchor) ?
                    941:        NULL : (HTChildAnchor*) anchor;
                    942:     return HTLoadDocument(request, NO);
                    943: }
1.52      frystyk   944: 
                    945: 
                    946: /*             Load if necessary, and select an anchor
                    947: **             --------------------------------------
                    948: **
                    949: **     This function is almost identical to HTLoadAnchor, but it doesn't
                    950: **     clear the error stack so that the information in there is kept.
                    951: **
1.59      frystyk   952: ** On Entry,
1.52      frystyk   953: **        destination              The child or parenet anchor to be loaded.
                    954: **
1.59      frystyk   955: ** On exit,
                    956: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    957: **                     HT_ERROR        Error has occured
                    958: **                     HT_LOADED       Success
                    959: **                     HT_NO_DATA      Success, but no document loaded.
                    960: **                                     (telnet sesssion started etc)
1.52      frystyk   961: */
                    962: 
1.59      frystyk   963: PUBLIC int HTLoadAnchorRecursive ARGS2(HTAnchor*,      anchor,
                    964:                                       HTRequest *,     request)
1.52      frystyk   965: {
1.59      frystyk   966:     if (!anchor) return HT_ERROR;                                /* No link */
1.52      frystyk   967:     
                    968:     request->anchor  = HTAnchor_parent(anchor);
1.59      frystyk   969:     request->childAnchor = ((HTAnchor *) request->anchor == anchor) ?
                    970:        NULL : (HTChildAnchor*) anchor;
1.52      frystyk   971:     
1.59      frystyk   972:     return HTLoadDocument(request, YES);
                    973: }
1.1       timbl     974: 
                    975: 
                    976: /*             Search
                    977: **             ------
                    978: **  Performs a keyword search on word given by the user. Adds the keyword to 
                    979: **  the end of the current address and attempts to open the new address.
                    980: **
                    981: **  On Entry,
                    982: **       *keywords     space-separated keyword list or similar search list
1.2       timbl     983: **     here            is anchor search is to be done on.
1.59      frystyk   984: **
                    985: ** On exit,
                    986: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                    987: **                     HT_ERROR        Error has occured
                    988: **                     HT_LOADED       Success
                    989: **                     HT_NO_DATA      Success, but no document loaded.
                    990: **                                     (telnet sesssion started etc)
1.1       timbl     991: */
                    992: 
1.56      frystyk   993: PRIVATE char hex ARGS1(int, i)
1.2       timbl     994: {
1.13      timbl     995:     char * hexchars = "0123456789ABCDEF";
                    996:     return hexchars[i];
1.2       timbl     997: }
1.1       timbl     998: 
1.59      frystyk   999: PUBLIC int HTSearch ARGS3(CONST char *,                keywords,
                   1000:                          HTParentAnchor *,     here,
                   1001:                          HTRequest *,          request)
1.1       timbl    1002: {
1.2       timbl    1003: 
                   1004: #define acceptable \
                   1005: "1234567890abcdefghijlkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"
                   1006: 
                   1007:     char *q, *u;
                   1008:     CONST char * p, *s, *e;            /* Pointers into keywords */
                   1009:     char * address = HTAnchor_address((HTAnchor*)here);
1.65      frystyk  1010:     int result;
1.56      frystyk  1011:     char * escaped = (char *) malloc(strlen(keywords)*3+1);
1.2       timbl    1012: 
1.29      frystyk  1013:     /* static CONST BOOL isAcceptable[96] = */
                   1014:     /* static AND const is not good for a gnu compiler! Frystyk 25/02-94 */
1.30      luotonen 1015:     static BOOL isAcceptable[96] =
1.2       timbl    1016:     /*   0 1 2 3 4 5 6 7 8 9 A B C D E F */
                   1017:     {    0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,      /* 2x   !"#$%&'()*+,-./  */
                   1018:          1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,      /* 3x  0123456789:;<=>?  */
                   1019:         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,       /* 4x  @ABCDEFGHIJKLMNO  */
                   1020:         1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,       /* 5X  PQRSTUVWXYZ[\]^_  */
                   1021:         0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,       /* 6x  `abcdefghijklmno  */
                   1022:         1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0 };     /* 7X  pqrstuvwxyz{\}~  DEL */
                   1023: 
                   1024:     if (escaped == NULL) outofmem(__FILE__, "HTSearch");
                   1025:     
1.29      frystyk  1026: /* Convert spaces to + and hex escape unacceptable characters */
1.2       timbl    1027: 
1.29      frystyk  1028:     for(s=keywords; *s && WHITE(*s); s++); /*scan */    /* Skip white space */
                   1029:     for(e = s + strlen(s); e>s && WHITE(*(e-1)) ; e--);     /* Skip trailers */
                   1030:     for(q=escaped, p=s; p<e; p++) {                  /* scan stripped field */
1.2       timbl    1031:         int c = (int)TOASCII(*p);
                   1032:         if (WHITE(*p)) {
                   1033:            *q++ = '+';
1.29      frystyk  1034:        } else if (c>=32 && c<=127 && isAcceptable[c-32] != 0) {
1.13      timbl    1035:            *q++ = *p;                  /* 930706 TBL for MVS bug */
1.2       timbl    1036:        } else {
                   1037:            *q++ = '%';
                   1038:            *q++ = hex(c / 16);
                   1039:            *q++ = hex(c % 16);
                   1040:        }
                   1041:     } /* Loop over string */
1.1       timbl    1042:     
1.2       timbl    1043:     *q=0;
                   1044:                                /* terminate escaped sctring */
                   1045:     u=strchr(address, '?');            /* Find old search string */
                   1046:     if (u) *u = 0;                             /* Chop old search off */
1.1       timbl    1047: 
                   1048:     StrAllocCat(address, "?");
1.2       timbl    1049:     StrAllocCat(address, escaped);
                   1050:     free(escaped);
1.15      timbl    1051:     result = HTLoadRelative(address, here, request);
1.1       timbl    1052:     free(address);
1.2       timbl    1053:     
1.1       timbl    1054:     return result;
1.2       timbl    1055: }
                   1056: 
                   1057: 
                   1058: /*             Search Given Indexname
                   1059: **             ------
                   1060: **  Performs a keyword search on word given by the user. Adds the keyword to 
                   1061: **  the end of the current address and attempts to open the new address.
                   1062: **
1.59      frystyk  1063: ** On Entry,
1.2       timbl    1064: **       *keywords     space-separated keyword list or similar search list
                   1065: **     *addres         is name of object search is to be done on.
1.59      frystyk  1066: ** On exit,
                   1067: **     returns         HT_WOULD_BLOCK  An I/O operation would block
                   1068: **                     HT_ERROR        Error has occured
                   1069: **                     HT_LOADED       Success
                   1070: **                     HT_NO_DATA      Success, but no document loaded.
                   1071: **                                     (telnet sesssion started etc)
1.2       timbl    1072: */
                   1073: 
1.59      frystyk  1074: PUBLIC int HTSearchAbsolute ARGS3(CONST char *,        keywords,
                   1075:                                  CONST char *,         indexname,
                   1076:                                  HTRequest *,          request)
1.2       timbl    1077: {
                   1078:     HTParentAnchor * anchor =
                   1079:        (HTParentAnchor*) HTAnchor_findAddress(indexname);
1.15      timbl    1080:     return HTSearch(keywords, anchor, request);
1.57      howcome  1081: }
                   1082: 
                   1083: 
                   1084: /*
                   1085: **             Find Related Name
                   1086: **
                   1087: **  Creates a string that can be used as a related name when 
                   1088: **  calling HTParse initially. 
                   1089: **  
                   1090: **  The code for this routine originates from the Linemode 
                   1091: **  browser and was moved here by howcome@dxcern.cern.ch
                   1092: **  in order for all clients to take advantage.
                   1093: **
1.59      frystyk  1094: **  The string returned must be freed by the caller
1.57      howcome  1095: */
                   1096: PUBLIC char * HTFindRelatedName NOARGS
                   1097: {
1.59      frystyk  1098:     char* default_default = NULL;            /* Parse home relative to this */
                   1099:     CONST char *host = HTGetHostName(); 
1.57      howcome  1100:     StrAllocCopy(default_default, "file://");
1.59      frystyk  1101:     if (host)
                   1102:        StrAllocCat(default_default, host);
                   1103:     else
                   1104:        StrAllocCat(default_default, "localhost");
                   1105:     {
                   1106:        char wd[HT_MAX_PATH+1];
1.57      howcome  1107: 
1.59      frystyk  1108: #ifdef NO_GETWD
                   1109: #ifdef HAS_GETCWD            /* System V variant SIGN CHANGED TBL 921006 !! */
                   1110:        char *result = (char *) getcwd(wd, sizeof(wd)); 
                   1111: #else
                   1112:        char *result = NULL;
                   1113:        HTAlert("This platform does not support neither getwd nor getcwd\n");
1.57      howcome  1114: #endif
1.59      frystyk  1115: #else
                   1116:        char *result = (char *) getwd(wd);
                   1117: #endif
                   1118:        *(wd+HT_MAX_PATH) = '\0';
1.57      howcome  1119:        if (result) {
                   1120: #ifdef VMS 
                   1121:             /* convert directory name to Unix-style syntax */
                   1122:            char * disk = strchr (wd, ':');
                   1123:            char * dir = strchr (wd, '[');
                   1124:            if (disk) {
                   1125:                *disk = '\0';
                   1126:                StrAllocCat (default_default, "/");  /* needs delimiter */
                   1127:                StrAllocCat (default_default, wd);
                   1128:            }
                   1129:            if (dir) {
                   1130:                char *p;
                   1131:                *dir = '/';  /* Convert leading '[' */
                   1132:                for (p = dir ; *p != ']'; ++p)
                   1133:                        if (*p == '.') *p = '/';
                   1134:                *p = '\0';  /* Cut on final ']' */
                   1135:                StrAllocCat (default_default, dir);
                   1136:            }
                   1137: #else  /* not VMS */
                   1138:            StrAllocCat (default_default, wd);
1.59      frystyk  1139: #endif /* not VMS */
1.57      howcome  1140:        }
1.59      frystyk  1141:     }
1.57      howcome  1142:     StrAllocCat(default_default, "/default.html");
                   1143:     return default_default;
1.2       timbl    1144: }
                   1145: 
                   1146: 
                   1147: /*             Generate the anchor for the home page
                   1148: **             -------------------------------------
                   1149: **
                   1150: **     As it involves file access, this should only be done once
                   1151: **     when the program first runs.
1.10      timbl    1152: **     This is a default algorithm -- browser don't HAVE to use this.
                   1153: **     But consistency betwen browsers is STRONGLY recommended!
1.2       timbl    1154: **
1.10      timbl    1155: **     Priority order is:
                   1156: **
                   1157: **             1       WWW_HOME environment variable (logical name, etc)
                   1158: **             2       ~/WWW/default.html
                   1159: **             3       /usr/local/bin/default.html
                   1160: **             4       http://info.cern.ch/default.html
                   1161: **
1.2       timbl    1162: */
                   1163: PUBLIC HTParentAnchor * HTHomeAnchor NOARGS
                   1164: {
1.12      timbl    1165:     char * my_home_document = NULL;
                   1166:     char * home = (char *)getenv(LOGICAL_DEFAULT);
1.2       timbl    1167:     char * ref;
                   1168:     HTParentAnchor * anchor;
1.1       timbl    1169:     
1.12      timbl    1170:     if (home) {
                   1171:         StrAllocCopy(my_home_document, home);
                   1172:     
                   1173: /*     Someone telnets in, they get a special home.
                   1174: */
                   1175:     } else  if (HTClientHost) {                        /* Telnet server */
                   1176:        FILE * fp = fopen(REMOTE_POINTER, "r");
                   1177:        char * status;
                   1178:        if (fp) {
1.59      frystyk  1179:            my_home_document = (char*) malloc(HT_MAX_PATH);
                   1180:            status = fgets(my_home_document, HT_MAX_PATH, fp);
1.12      timbl    1181:            if (!status) {
                   1182:                free(my_home_document);
                   1183:                my_home_document = NULL;
                   1184:            }
                   1185:            fclose(fp);
                   1186:        }
                   1187:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                   1188:     }
                   1189: 
                   1190:     
                   1191: 
1.2       timbl    1192: #ifdef unix
1.12      timbl    1193: 
1.10      timbl    1194:     if (!my_home_document) {
                   1195:        FILE * fp = NULL;
                   1196:        CONST char * home =  (CONST char*)getenv("HOME");
                   1197:        if (home) { 
                   1198:            my_home_document = (char *)malloc(
                   1199:                strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1);
                   1200:            if (my_home_document == NULL) outofmem(__FILE__, "HTLocalName");
                   1201:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                   1202:            fp = fopen(my_home_document, "r");
                   1203:        }
                   1204:        
                   1205:        if (!fp) {
                   1206:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                   1207:            fp = fopen(my_home_document, "r");
                   1208:        }
1.2       timbl    1209:        if (fp) {
                   1210:            fclose(fp);
                   1211:        } else {
1.62      frystyk  1212:            if (TRACE)
                   1213:                fprintf(stderr,
                   1214:                        "HTBrowse: No local home document ~/%s or %s\n",
                   1215:                        PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
1.11      timbl    1216:            free(my_home_document);
                   1217:            my_home_document = NULL;
1.2       timbl    1218:        }
                   1219:     }
                   1220: #endif
1.10      timbl    1221:     ref = HTParse( my_home_document ?  my_home_document :
                   1222:                                HTClientHost ? REMOTE_ADDRESS
                   1223:                                : LAST_RESORT,
                   1224:                    "file:",
1.2       timbl    1225:                    PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.10      timbl    1226:     if (my_home_document) {
1.62      frystyk  1227:        if (TRACE)
                   1228:            fprintf(stderr,
                   1229:                   "HTAccess.... `%s\' used for custom home page as\n`%s\'\n",
                   1230:                    my_home_document, ref);
1.10      timbl    1231:        free(my_home_document);
1.2       timbl    1232:     }
                   1233:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
                   1234:     free(ref);
                   1235:     return anchor;
1.1       timbl    1236: }
1.26      frystyk  1237: 
                   1238: 
                   1239: /*             Bind an Anchor to the request structure
                   1240: **             ---------------------------------------
                   1241: **
                   1242: **    On Entry,
                   1243: **     anchor          The child or parenet anchor to be binded
                   1244: **     request         The request sturcture
                   1245: **    On Exit,
                   1246: **        returns    YES     Success
                   1247: **                   NO      Failure 
                   1248: **
                   1249: **  Note: Actually the same as HTLoadAnchor() but DOES NOT do the loading
                   1250: **                                             Henrik Frystyk 17/02-94
                   1251: */
                   1252: 
                   1253: PUBLIC BOOL HTBindAnchor ARGS2(HTAnchor*, anchor, HTRequest *, request)
                   1254: {
                   1255:     if (!anchor) return NO;    /* No link */
                   1256:     
                   1257:     request->anchor  = HTAnchor_parent(anchor);
                   1258:     request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL
                   1259:                                        : (HTChildAnchor*) anchor;
                   1260:        
1.29      frystyk  1261:     return YES;
1.26      frystyk  1262: } /* HTBindAnchor */
1.59      frystyk  1263: 
1.26      frystyk  1264: 

Webmaster