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

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

Webmaster