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

Webmaster