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

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

Webmaster