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

1.61      frystyk     1: /*                                                                  HTAccess.c
                      2: **     ACCESS MANAGER
                      3: **
1.75      frystyk     4: **     (c) COPYRIGHT MIT 1995.
1.61      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.1       timbl       6: **
                      7: ** Authors
1.79      frystyk     8: **     TBL     Tim Berners-Lee timbl@w3.org
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.88      frystyk    22: **        Sep 95 Rewritten, HFN
1.1       timbl      23: */
                     24: 
1.68      frystyk    25: #if !defined(HT_DIRECT_WAIS) && !defined(HT_DEFAULT_WAIS_GATEWAY)
                     26: #define HT_DEFAULT_WAIS_GATEWAY "http://www.w3.org:8001/"
1.54      frystyk    27: #endif
1.8       timbl      28: 
1.67      frystyk    29: /* Library include files */
1.88      frystyk    30: #include "WWWLib.h"
1.93      frystyk    31: #include "HTReqMan.h"
                     32: #include "HTAccess.h"                                   /* Implemented here */
1.88      frystyk    33: 
1.93      frystyk    34: PRIVATE char * HTAppName = NULL;         /* Application name: please supply */
                     35: PRIVATE char * HTAppVersion = NULL;    /* Application version: please supply */
1.2       timbl      36: 
1.59      frystyk    37: /* --------------------------------------------------------------------------*/
1.61      frystyk    38: /*                Initialization and Termination of the Library             */
                     39: /* --------------------------------------------------------------------------*/
                     40: 
1.93      frystyk    41: /*     HTLib_appName
                     42: **     -------------
                     43: */
                     44: PUBLIC CONST char * HTLib_appName (void)
                     45: {
                     46:     return HTAppName;
                     47: }
                     48: 
                     49: /*     HTLib_appVersion
                     50: **     ----------------
                     51: */
                     52: PUBLIC CONST char * HTLib_appVersion (void)
                     53: {
                     54:     return HTAppVersion;
                     55: }
                     56: 
1.61      frystyk    57: /*                                                                  HTLibInit
                     58: **
                     59: **     This function initiates the Library and it MUST be called when
                     60: **     starting up an application. See also HTLibTerminate()
                     61: */
1.93      frystyk    62: PUBLIC BOOL HTLibInit (CONST char * AppName, CONST char * AppVersion)
1.61      frystyk    63: {
1.97      frystyk    64: #if WWWTRACE_MODE == WWWTRACE_FILE                       /* Open trace file */
1.67      frystyk    65:     if ((TDEST = fopen(TRACE_FILE, "a")) != NULL) {
                     66:        if (setvbuf(TDEST, NULL, _IOLBF, 0) < 0) {  /* Change to line buffer */
1.97      frystyk    67:            TTYPrint(TDEST, "WWWLibInit.. Can't initialize TRACE buffer - no TRACE\n");
1.67      frystyk    68:            fclose(TDEST);
                     69:            TDEST = NULL;
                     70:            WWW_TraceFlag = 0;
                     71:        }
                     72:     } else
                     73:        WWW_TraceFlag = 0;
1.97      frystyk    74: #endif /* WWWTRACE_FILE */
1.67      frystyk    75: 
1.91      frystyk    76:     if (WWWTRACE)
1.97      frystyk    77:        TTYPrint(TDEST, "WWWLibInit.. INITIALIZING LIBRARY OF COMMON CODE\n");
1.63      frystyk    78: 
1.93      frystyk    79:     /* Set the application name and version */
                     80:     if (AppName) {
                     81:        char *ptr;
                     82:        StrAllocCopy(HTAppName, AppName);
                     83:        ptr = HTAppName;
                     84:        while (*ptr) {
                     85:            if (WHITE(*ptr)) *ptr = '_';
                     86:            ptr++;
                     87:        }
                     88:     }
                     89:     if (AppVersion) {
                     90:        char *ptr;
                     91:        StrAllocCopy(HTAppVersion, AppVersion);
                     92:        ptr = HTAppVersion;
                     93:        while (*ptr) {
                     94:            if (WHITE(*ptr)) *ptr = '_';
                     95:            ptr++;
                     96:        }
                     97:     }
                     98: 
                     99:     HTBind_init();                                   /* Initialize bindings */
1.61      frystyk   100: 
1.77      frystyk   101: #ifndef HT_DIRECT_WAIS
1.95      frystyk   102:     HTGateway_add("wais", HT_DEFAULT_WAIS_GATEWAY);
1.77      frystyk   103: #endif
                    104: 
1.88      frystyk   105:     /* Register a call back function for the Net Manager */
                    106:     HTNet_register (HTLoad_terminate, HT_ALL);
                    107: 
1.62      frystyk   108: #ifdef WWWLIB_SIG
1.61      frystyk   109:     /* On Solaris (and others?) we get a BROKEN PIPE signal when connecting
1.67      frystyk   110:     ** to a port where we should get `connection refused'. We ignore this 
1.61      frystyk   111:     ** using the following function call
                    112:     */
                    113:     HTSetSignal();                                /* Set signals in library */
1.1       timbl     114: #endif
                    115: 
1.67      frystyk   116: #ifdef _WINDOWS
                    117:     /*
                    118:     ** Initialise WinSock DLL. This must also be shut down! PMH
                    119:     */
                    120:     {
                    121:         WSADATA            wsadata;
                    122:        if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
1.91      frystyk   123:            if (WWWTRACE)
1.97      frystyk   124:                TTYPrint(TDEST, "WWWLibInit.. Can't initialize WinSoc\n");
1.67      frystyk   125:             WSACleanup();
                    126:             return NO;
                    127:         }
                    128:         if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
1.91      frystyk   129:             if (WWWTRACE)
1.97      frystyk   130:                TTYPrint(TDEST, "WWWLibInit.. Bad version of WinSoc\n");
1.67      frystyk   131:             WSACleanup();
                    132:             return NO;
                    133:         }
                    134:     }
                    135: #endif /* _WINDOWS */
                    136: 
1.71      frystyk   137: #ifndef NO_TIMEGM
                    138:     HTGetTimeZoneOffset();        /* Find offset from GMT if using mktime() */
                    139: #endif
1.70      frystyk   140:     HTTmp_setRoot(NULL);                    /* Set up default tmp directory */
1.61      frystyk   141:     return YES;
                    142: }
                    143: 
                    144: 
1.90      frystyk   145: /*     HTLibTerminate
                    146: **     --------------
1.61      frystyk   147: **     This function frees memory kept by the Library and should be called
1.63      frystyk   148: **     before exit of an application (if you are on a PC platform)
1.61      frystyk   149: */
                    150: PUBLIC BOOL HTLibTerminate NOARGS
                    151: {
1.91      frystyk   152:     if (WWWTRACE)
1.97      frystyk   153:        TTYPrint(TDEST, "WWWLibTerm.. Cleaning up LIBRARY OF COMMON CODE\n");
1.63      frystyk   154:     HTAtom_deleteAll();
1.89      frystyk   155:     HTDNS_deleteAll();
1.73      frystyk   156: 
1.81      frystyk   157:     HTProtocol_deleteAll();  /* Remove bindings between access and protocols */
1.73      frystyk   158:     HTBind_deleteAll();            /* Remove bindings between suffixes, media types */
                    159: 
1.95      frystyk   160:     HTProxy_deleteAll();          /* Clean up lists of proxies and gateways */
                    161:     HTNoProxy_deleteAll();
                    162:     HTGateway_deleteAll();
1.77      frystyk   163: 
                    164:     HTFreeHostName();                      /* Free up some internal strings */
1.63      frystyk   165:     HTFreeMailAddress();
1.70      frystyk   166:     HTCache_freeRoot();
1.84      frystyk   167:     HTCache_clearMem();                                  /* Keep the disk versions! */
1.70      frystyk   168:     HTTmp_freeRoot();
1.98    ! frystyk   169:     HTError_freePrefix();
1.67      frystyk   170: 
                    171: #ifdef _WINDOWS
                    172:     WSACleanup();
                    173: #endif
                    174: 
1.97      frystyk   175: #if WWWTRACE_MODE == WWWTRACE_FILE                      /* Close trace file */
1.67      frystyk   176:     if (TDEST) {
                    177:        fclose(TDEST);
                    178:        TDEST = NULL;
                    179:        WWW_TraceFlag = 0;
                    180:     }
1.97      frystyk   181: #endif /* WWWTRACE_FILE */
1.61      frystyk   182:     return YES;
                    183: }
                    184: 
1.59      frystyk   185: /* --------------------------------------------------------------------------*/
1.88      frystyk   186: /*                             Access functions                             */
1.59      frystyk   187: /* --------------------------------------------------------------------------*/
1.33      luotonen  188: 
1.90      frystyk   189: /*     Request a document
                    190: **     -----------------
                    191: **     Private version that requests a document from the request manager
                    192: **     Returns YES if request accepted, else NO
1.88      frystyk   193: */
1.90      frystyk   194: PRIVATE BOOL HTLoadDocument ARGS2(HTRequest *, request, BOOL, recursive)
1.88      frystyk   195: {
                    196:     if (PROT_TRACE) {
1.90      frystyk   197:        HTParentAnchor *anchor = HTRequest_anchor(request);
                    198:        char * full_address = HTAnchor_address((HTAnchor *) anchor);
1.97      frystyk   199:        TTYPrint(TDEST, "HTAccess.... Accessing document %s\n", full_address);
1.88      frystyk   200:        free(full_address);
                    201:     }
1.96      frystyk   202:     return HTLoad(request, recursive);
1.58      frystyk   203: }
1.1       timbl     204: 
                    205: 
1.90      frystyk   206: /*     Request a document from absolute name
                    207: **     -------------------------------------
                    208: **     Request a document referencd by an absolute URL.
                    209: **     Returns YES if request accepted, else NO
                    210: */
                    211: PUBLIC BOOL HTLoadAbsolute (CONST char * url, HTRequest* request)
                    212: {
                    213:     if (url && request) {
                    214:        HTAnchor * anchor = HTAnchor_findAddress(url);
                    215:        HTRequest_setAnchor(request, anchor);
                    216:        return HTLoadDocument(request, NO);
                    217:     }
                    218:     return NO;
                    219: }
                    220: 
                    221: 
                    222: /*     Request a document from absolute name to stream
                    223: **     -----------------------------------------------
                    224: **     Request a document referencd by an absolute URL and sending the data
                    225: **     down a stream. This is _excactly_ the same as HTLoadAbsolute as
                    226: **     the ourputstream is specified using the function
                    227: **     HTRequest_setOutputStream(). 'filter' is ignored!
                    228: **     Returns YES if request accepted, else NO
                    229: */
                    230: PUBLIC BOOL HTLoadToStream (CONST char * url, BOOL filter, HTRequest *request)
                    231: {
                    232:     return HTLoadAbsolute(url, request);
                    233: }
                    234: 
                    235: 
                    236: /*     Request a document from relative name
                    237: **     -------------------------------------
                    238: **     Request a document referenced by a relative URL. The relative URL is 
                    239: **     made absolute by resolving it relative to the address of the 'base' 
                    240: **     anchor.
                    241: **     Returns YES if request accepted, else NO
                    242: */
                    243: PUBLIC BOOL HTLoadRelative (CONST char *       relative,
                    244:                            HTParentAnchor *    base,
                    245:                            HTRequest *         request)
                    246: {
                    247:     BOOL status = NO;
                    248:     if (relative && base && request) {
                    249:        char * rel = NULL;
                    250:        char * full_url = NULL;
                    251:        char * base_url = HTAnchor_address((HTAnchor *) base);
                    252:        StrAllocCopy(rel, relative);
                    253:        full_url = HTParse(HTStrip(rel), base_url,
                    254:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    255:        status = HTLoadAbsolute(full_url, request);
                    256:        free(rel);
                    257:        free(full_url);
                    258:        free(base_url);
                    259:     }
                    260:     return status;
                    261: }
                    262: 
                    263: 
                    264: /*     Request an anchor
                    265: **     -----------------
                    266: **     Request the document referenced by the anchor
                    267: **     Returns YES if request accepted, else NO
                    268: */
                    269: PUBLIC BOOL HTLoadAnchor (HTAnchor * anchor, HTRequest * request)
                    270: {
                    271:     if (anchor && request) {
                    272:        HTRequest_setAnchor(request, anchor);
                    273:        return HTLoadDocument(request, NO);
                    274:     }
                    275:     return NO;
                    276: }
                    277: 
                    278: 
                    279: /*     Request an anchor
                    280: **     -----------------
                    281: **     Same as HTLoadAnchor but any information in the Error Stack in the 
                    282: **     request object is kept, so that any error messages in one 
1.52      frystyk   283: **     This function is almost identical to HTLoadAnchor, but it doesn't
                    284: **     clear the error stack so that the information in there is kept.
1.90      frystyk   285: **     Returns YES if request accepted, else NO
                    286: */
                    287: PUBLIC BOOL HTLoadAnchorRecursive (HTAnchor * anchor, HTRequest * request)
                    288: {
                    289:     if (anchor && request) {
                    290:        HTRequest_setAnchor(request, anchor);
                    291:         return HTLoadDocument(request, YES);
                    292:     }
                    293:     return NO;
                    294: }
                    295: 
                    296: 
                    297: /*     Search an Anchor
                    298: **     ----------------
                    299: **     Performs a keyword search on word given by the user. Adds the keyword
                    300: **     to the end of the current address and attempts to open the new address.
                    301: **     The list of keywords must be a space-separated list and spaces will
                    302: **     be converted to '+' before the request is issued.
                    303: **     Search can also be performed by HTLoadAbsolute() etc.
                    304: **     Returns YES if request accepted, else NO
                    305: */
                    306: PUBLIC BOOL HTSearch (CONST char *     keywords,
                    307:                      HTParentAnchor *  base,
                    308:                      HTRequest *       request)
                    309: {
                    310:     if (keywords && base && request) {
                    311:        char *base_url = HTAnchor_address((HTAnchor *) base);
                    312:        if (*keywords) {
                    313:            char *plus;
                    314:            StrAllocCat(base_url, "?");
                    315:            StrAllocCat(base_url, keywords);
                    316:            plus = strchr(base_url, '?');
                    317:            while (*plus) {
                    318:                if (*plus == ' ') *plus = '+';
                    319:                plus++;
                    320:            }
1.2       timbl     321:        }
1.90      frystyk   322:        return HTLoadAbsolute(base_url, request);
                    323:     }
                    324:     return NO;
1.2       timbl     325: }
                    326: 
                    327: 
1.90      frystyk   328: /*     Search a document from absolute name
                    329: **     ------------------------------------
                    330: **     Request a document referencd by an absolute URL appended with the
                    331: **     keywords given. The URL can NOT contain any fragment identifier!
                    332: **     The list of keywords must be a space-separated list and spaces will
                    333: **     be converted to '+' before the request is issued.
                    334: **     Returns YES if request accepted, else NO
                    335: */
                    336: PUBLIC BOOL HTSearchAbsolute (CONST char *     keywords,
                    337:                              CONST char *      url,
                    338:                              HTRequest *       request)
                    339: {
                    340:     if (url && request) {
                    341:        HTAnchor * anchor = HTAnchor_findAddress(url);
                    342:        return HTSearch(keywords, HTAnchor_parent(anchor), request);
                    343:     }
                    344:     return NO;
1.57      howcome   345: }
                    346: 
1.70      frystyk   347: /* --------------------------------------------------------------------------*/
                    348: /*                             Document Poster                              */
                    349: /* --------------------------------------------------------------------------*/
                    350: 
1.90      frystyk   351: /*     Copy an anchor
1.70      frystyk   352: **     --------------
1.90      frystyk   353: **     Fetch the URL (possibly local file URL) and send it using either PUT
                    354: **     or POST to the remote destination using HTTP. The caller can decide the
                    355: **     exact method used and which HTTP header fields to transmit by setting
                    356: **     the user fields in the request structure.
1.92      frystyk   357: **     If posting to NNTP then we can't dispatch at this level but must pass
                    358: **     the source anchor to the news module that then takes all the refs
                    359: **     to NNTP and puts into the "newsgroups" header
1.90      frystyk   360: **     Returns YES if request accepted, else NO
1.70      frystyk   361: */
1.90      frystyk   362: PUBLIC BOOL HTCopyAnchor (HTAnchor * src_anchor, HTRequest * main_req)
1.80      frystyk   363: { 
1.78      frystyk   364:     HTRequest *src_req;
1.85      frystyk   365:     if (!src_anchor || !main_req)
1.90      frystyk   366:        return NO;
1.70      frystyk   367: 
1.80      frystyk   368:     /* Build the POST web if not already there */
1.85      frystyk   369:     if (!main_req->source) {
1.80      frystyk   370:        src_req = HTRequest_new();                /* First set up the source */
                    371:        HTAnchor_clearHeader((HTParentAnchor *) src_anchor);
1.85      frystyk   372:        src_req->reload = HT_MEM_REFRESH;
1.80      frystyk   373:        src_req->source = src_req;                        /* Point to myself */
                    374:        src_req->output_format = WWW_SOURCE;     /* We want source (for now) */
                    375: 
                    376:        /* Set up the main link in the source anchor */
                    377:        {
1.85      frystyk   378:            HTLink *main_link = HTAnchor_findMainLink(src_anchor);
                    379:            HTAnchor *main_anchor = HTAnchor_linkDest(main_link);
                    380:            HTMethod method = HTAnchor_linkMethod(main_link);
                    381:            if (!main_link || method==METHOD_INVALID) {
1.91      frystyk   382:                if (WWWTRACE)
1.97      frystyk   383:                    TTYPrint(TDEST, "Copy Anchor. No destination found or unspecified method");
1.80      frystyk   384:                HTRequest_delete(src_req);
1.90      frystyk   385:                return NO;
1.80      frystyk   386:            }
1.85      frystyk   387:            if (HTAnchor_linkResult(main_link) == HT_LINK_NONE) {
                    388:                main_req->GenMask |= HT_DATE;            /* Send date header */
                    389:                main_req->source = src_req;
                    390:                main_req->reload = HT_CACHE_REFRESH;
                    391:                main_req->method = method;
                    392:                HTRequest_addDestination(src_req, main_req);
                    393:                main_req->input_format = WWW_SOURCE;      /* for now :-( @@@ */
1.90      frystyk   394:                if (HTLoadAnchor(main_anchor, main_req) == NO)
                    395:                    return NO;
1.85      frystyk   396:            }
1.80      frystyk   397:        }
1.78      frystyk   398: 
1.80      frystyk   399:        /* For all other links in the source anchor */
                    400:        if (src_anchor->links) {
                    401:            HTList *cur = src_anchor->links;
                    402:            HTLink *pres;
1.85      frystyk   403:            while ((pres = (HTLink *) HTList_nextObject(cur)) &&
                    404:                   HTAnchor_linkResult(pres) == HT_LINK_NONE) {
                    405:                HTAnchor *dest = HTAnchor_linkDest(pres);
                    406:                HTMethod method = HTAnchor_linkMethod(pres);
1.80      frystyk   407:                HTRequest *dest_req;
                    408:                if (!dest || method==METHOD_INVALID) {
1.91      frystyk   409:                    if (WWWTRACE)
1.97      frystyk   410:                        TTYPrint(TDEST, "Copy Anchor. Bad anchor setup %p\n",
1.80      frystyk   411:                                dest);
1.90      frystyk   412:                    return NO;
1.80      frystyk   413:                }
                    414:                dest_req = HTRequest_new();
1.85      frystyk   415:                dest_req->GenMask |= HT_DATE;            /* Send date header */
1.80      frystyk   416:                dest_req->source = src_req;
1.85      frystyk   417:                dest_req->reload = HT_CACHE_REFRESH;
1.80      frystyk   418:                dest_req->method = method;
                    419:                HTRequest_addDestination(src_req, dest_req);
                    420:                dest_req->input_format = WWW_SOURCE;      /* for now :-( @@@ */
1.90      frystyk   421:                if (HTLoadAnchor(dest, dest_req) == NO)
                    422:                    return NO;
1.80      frystyk   423:            }
                    424:        }
                    425:     } else {                    /* Use the existing Post Web and restart it */
1.85      frystyk   426:        src_req = main_req->source;
1.80      frystyk   427:        if (src_req->mainDestination)
1.90      frystyk   428:            if (HTLoadDocument(main_req, NO) == NO)
                    429:                return NO;
1.80      frystyk   430:        if (src_req->destinations) {
                    431:            HTList *cur = src_anchor->links;
                    432:            HTRequest *pres;
                    433:            while ((pres = (HTRequest *) HTList_nextObject(cur)) != NULL) {
1.90      frystyk   434:                if (HTLoadDocument(pres, NO) == NO)
                    435:                    return NO;
1.80      frystyk   436:            }
                    437:        }
1.78      frystyk   438:     }
                    439: 
1.80      frystyk   440:     /* Now open the source */
                    441:     return HTLoadAnchor(src_anchor, src_req);
1.70      frystyk   442: }
                    443: 
                    444: 
1.90      frystyk   445: /*     Upload an Anchor
1.70      frystyk   446: **     ----------------
1.90      frystyk   447: **     Send the contents (in hyperdoc) of the source anchor using either PUT
                    448: **     or POST to the remote destination using HTTP. The caller can decide the
                    449: **     exact method used and which HTTP header fields to transmit by setting
                    450: **     the user fields in the request structure.
                    451: **     Returns YES if request accepted, else NO
                    452: */
                    453: PUBLIC BOOL HTUploadAnchor (HTAnchor *         src_anchor,
                    454:                            HTParentAnchor *    dest_anchor,
                    455:                            HTRequest *         dest_req)
1.70      frystyk   456: {
1.90      frystyk   457:     if (!src_anchor || !dest_anchor || !dest_req)
                    458:        return NO;
1.70      frystyk   459:     if (!(dest_anchor->methods & dest_req->method)) {
                    460:        char buf[80];
                    461:        sprintf(buf, "It might not be allowed to %s to this destination, continue?", HTMethod_name(dest_req->method));
1.86      frystyk   462:        if (!HTConfirm(dest_req, buf))
1.90      frystyk   463:            return NO;
1.70      frystyk   464:     }
1.77      frystyk   465: 
                    466:     /* @@@ NOT FINISHED @@@ */
1.70      frystyk   467: 
1.90      frystyk   468:     return NO;
1.70      frystyk   469: }
                    470: 
                    471: /* --------------------------------------------------------------------------*/
                    472: /*                             Anchor help routines                         */
                    473: /* --------------------------------------------------------------------------*/
1.57      howcome   474: 
                    475: /*
                    476: **             Find Related Name
                    477: **
                    478: **  Creates a string that can be used as a related name when 
                    479: **  calling HTParse initially. 
                    480: **  
                    481: **  The code for this routine originates from the Linemode 
1.79      frystyk   482: **  browser and was moved here by howcome@w3.org
1.57      howcome   483: **  in order for all clients to take advantage.
                    484: **
1.59      frystyk   485: **  The string returned must be freed by the caller
1.57      howcome   486: */
                    487: PUBLIC char * HTFindRelatedName NOARGS
                    488: {
1.59      frystyk   489:     char* default_default = NULL;            /* Parse home relative to this */
                    490:     CONST char *host = HTGetHostName(); 
1.57      howcome   491:     StrAllocCopy(default_default, "file://");
1.59      frystyk   492:     if (host)
                    493:        StrAllocCat(default_default, host);
                    494:     else
                    495:        StrAllocCat(default_default, "localhost");
                    496:     {
                    497:        char wd[HT_MAX_PATH+1];
1.67      frystyk   498: 
                    499: #ifdef NO_GETWD
                    500: #ifdef HAS_GETCWD            /* System V variant SIGN CHANGED TBL 921006 !! */
                    501:        char *result = (char *) getcwd(wd, sizeof(wd)); 
                    502: #else
                    503:        char *result = NULL;
                    504:        HTAlert("This platform does not support neither getwd nor getcwd\n");
                    505: #endif
                    506: #else
                    507:        char *result = (char *) getwd(wd);
                    508: #endif
1.59      frystyk   509:        *(wd+HT_MAX_PATH) = '\0';
1.57      howcome   510:        if (result) {
                    511: #ifdef VMS 
                    512:             /* convert directory name to Unix-style syntax */
                    513:            char * disk = strchr (wd, ':');
                    514:            char * dir = strchr (wd, '[');
                    515:            if (disk) {
                    516:                *disk = '\0';
                    517:                StrAllocCat (default_default, "/");  /* needs delimiter */
                    518:                StrAllocCat (default_default, wd);
                    519:            }
                    520:            if (dir) {
                    521:                char *p;
                    522:                *dir = '/';  /* Convert leading '[' */
                    523:                for (p = dir ; *p != ']'; ++p)
                    524:                        if (*p == '.') *p = '/';
                    525:                *p = '\0';  /* Cut on final ']' */
                    526:                StrAllocCat (default_default, dir);
                    527:            }
1.74      frystyk   528: #else  /* not VMS */
1.70      frystyk   529: #ifdef WIN32
                    530:            char * p = wd ;     /* a colon */
                    531:            StrAllocCat(default_default, "/");
                    532:            while( *p != 0 ) { 
                    533:                if (*p == '\\')                  /* change to one true slash */
                    534:                    *p = '/' ;
                    535:                p++;
                    536:            }
1.74      frystyk   537:            StrAllocCat( default_default, wd);
                    538: #else /* not WIN32 */
1.57      howcome   539:            StrAllocCat (default_default, wd);
1.70      frystyk   540: #endif /* not WIN32 */
1.67      frystyk   541: #endif /* not VMS */
1.57      howcome   542:        }
1.67      frystyk   543:     }
1.57      howcome   544:     StrAllocCat(default_default, "/default.html");
                    545:     return default_default;
1.2       timbl     546: }
                    547: 
                    548: 
                    549: /*             Generate the anchor for the home page
                    550: **             -------------------------------------
                    551: **
                    552: **     As it involves file access, this should only be done once
                    553: **     when the program first runs.
1.10      timbl     554: **     This is a default algorithm -- browser don't HAVE to use this.
                    555: **     But consistency betwen browsers is STRONGLY recommended!
1.2       timbl     556: **
1.10      timbl     557: **     Priority order is:
                    558: **
                    559: **             1       WWW_HOME environment variable (logical name, etc)
                    560: **             2       ~/WWW/default.html
                    561: **             3       /usr/local/bin/default.html
1.70      frystyk   562: **             4       http://www.w3.org/default.html
1.10      timbl     563: **
1.2       timbl     564: */
                    565: PUBLIC HTParentAnchor * HTHomeAnchor NOARGS
                    566: {
1.12      timbl     567:     char * my_home_document = NULL;
1.70      frystyk   568:     char * home = (char *) getenv(LOGICAL_DEFAULT);
1.2       timbl     569:     char * ref;
                    570:     HTParentAnchor * anchor;
1.1       timbl     571:     
1.70      frystyk   572:     /* Someone telnets in, they get a special home */
1.12      timbl     573:     if (home) {
                    574:         StrAllocCopy(my_home_document, home);
1.70      frystyk   575:     } else  if (HTClientHost) {                                    /* Telnet server */
1.12      timbl     576:        FILE * fp = fopen(REMOTE_POINTER, "r");
                    577:        char * status;
                    578:        if (fp) {
1.59      frystyk   579:            my_home_document = (char*) malloc(HT_MAX_PATH);
                    580:            status = fgets(my_home_document, HT_MAX_PATH, fp);
1.12      timbl     581:            if (!status) {
                    582:                free(my_home_document);
                    583:                my_home_document = NULL;
                    584:            }
                    585:            fclose(fp);
                    586:        }
                    587:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                    588:     }
                    589: 
1.67      frystyk   590: #ifdef unix
1.10      timbl     591:     if (!my_home_document) {
                    592:        FILE * fp = NULL;
1.70      frystyk   593:        char * home = (char *) getenv("HOME");
1.10      timbl     594:        if (home) { 
                    595:            my_home_document = (char *)malloc(
                    596:                strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1);
                    597:            if (my_home_document == NULL) outofmem(__FILE__, "HTLocalName");
                    598:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                    599:            fp = fopen(my_home_document, "r");
                    600:        }
                    601:        
                    602:        if (!fp) {
                    603:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                    604:            fp = fopen(my_home_document, "r");
                    605:        }
1.2       timbl     606:        if (fp) {
                    607:            fclose(fp);
                    608:        } else {
1.91      frystyk   609:            if (WWWTRACE)
1.97      frystyk   610:                TTYPrint(TDEST,
1.62      frystyk   611:                        "HTBrowse: No local home document ~/%s or %s\n",
                    612:                        PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
1.11      timbl     613:            free(my_home_document);
                    614:            my_home_document = NULL;
1.2       timbl     615:        }
                    616:     }
1.67      frystyk   617: #endif
1.70      frystyk   618:     ref = HTParse(my_home_document ? my_home_document :
                    619:                  HTClientHost ? REMOTE_ADDRESS : LAST_RESORT, "file:",
                    620:                  PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.10      timbl     621:     if (my_home_document) {
1.91      frystyk   622:        if (WWWTRACE)
1.97      frystyk   623:            TTYPrint(TDEST,
1.62      frystyk   624:                   "HTAccess.... `%s\' used for custom home page as\n`%s\'\n",
                    625:                    my_home_document, ref);
1.10      timbl     626:        free(my_home_document);
1.2       timbl     627:     }
                    628:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
                    629:     free(ref);
                    630:     return anchor;
1.1       timbl     631: }
1.97      frystyk   632: 
                    633: #if WWWTRACE_MODE == WWWTRACE_TTY && (!defined(_WINDOWS) || defined(_CONSOLE))
                    634: /* single function through which all trace messages must pass - EGP */
                    635: int TTYPrint(FILE* file, const char* fmt, ...)
                    636:        {
                    637:        int len;
                    638:        char space[513];
                    639:        char* pArgs;
                    640: 
                    641:        pArgs = (char*)(&fmt + 1);
                    642:        len = vsprintf(space, (char*)fmt, (char*)pArgs);
                    643:        fprintf(file, space);
                    644:        return (len);
                    645:        }
                    646: /* otherwise handled in www.c with the rest of the window shit */
                    647: #endif
1.26      frystyk   648: 

Webmaster