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

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

Webmaster