Annotation of libwww/Library/src/HTHome.c, revision 2.5

2.1       frystyk     1: /*                                                                  HTHome.c
2.2       frystyk     2: **     ANCHOR TRANSLATIONS
2.1       frystyk     3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
                      6: **
                      7: ** Authors
                      8: **     TBL     Tim Berners-Lee timbl@w3.org
                      9: **     JFG     Jean-Francois Groff jfg@dxcern.cern.ch
                     10: **     DD      Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
2.2       frystyk    11: **     HFN     Henrik Frystyk
2.1       frystyk    12: ** History
                     13: **       8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
                     14: **     26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
                     15: **      6 Oct 92 Moved HTClientHost and HTlogfile into here. TBL
                     16: **     17 Dec 92 Tn3270 added, bug fix. DD
                     17: **      4 Feb 93 Access registration, Search escapes bad chars TBL
                     18: **               PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
                     19: **     28 May 93 WAIS gateway explicit if no WAIS library linked in.
                     20: **        Dec 93 Bug change around, more reentrant, etc
                     21: **     09 May 94 logfile renamed to HTlogfile to avoid clash with WAIS
                     22: **      8 Jul 94 Insulate free() from _free structure element.
                     23: **        Sep 95 Rewritten, HFN
                     24: **        Nov 95 Spawned from HTAccess.c
                     25: */
                     26: 
                     27: /* Library include files */
                     28: #include "WWWLib.h"
2.2       frystyk    29: #include "WWWApp.h"
                     30: #include "HTReqMan.h"
2.1       frystyk    31: #include "HTHome.h"                                     /* Implemented here */
                     32: 
                     33: /* ------------------------------------------------------------------------- */
                     34: 
                     35: /*     Find Related Name
                     36: **     -----------------
                     37: **     Creates a string that can be used as a related name when 
                     38: **     calling HTParse initially. 
                     39: **  
                     40: **     The code for this routine originates from the Linemode 
                     41: **     browser and was moved here by howcome@w3.org
                     42: **     in order for all clients to take advantage.
                     43: **     The string returned must be freed by the caller
                     44: */
                     45: PUBLIC char * HTFindRelatedName (void)
                     46: {
                     47:     char* default_default = NULL;            /* Parse home relative to this */
                     48:     CONST char *host = HTGetHostName(); 
                     49:     StrAllocCopy(default_default, "file://");
                     50:     if (host)
                     51:        StrAllocCat(default_default, host);
                     52:     else
                     53:        StrAllocCat(default_default, "localhost");
                     54:     {
                     55:        char wd[HT_MAX_PATH+1];
                     56: 
                     57: #ifdef NO_GETWD
                     58: #ifdef HAS_GETCWD            /* System V variant SIGN CHANGED TBL 921006 !! */
                     59:        char *result = (char *) getcwd(wd, sizeof(wd)); 
                     60: #else
                     61:        char *result = NULL;
                     62:        HTAlert("This platform does not support neither getwd nor getcwd\n");
                     63: #endif
                     64: #else
                     65:        char *result = (char *) getwd(wd);
                     66: #endif
                     67:        *(wd+HT_MAX_PATH) = '\0';
                     68:        if (result) {
                     69: #ifdef VMS 
                     70:             /* convert directory name to Unix-style syntax */
                     71:            char * disk = strchr (wd, ':');
                     72:            char * dir = strchr (wd, '[');
                     73:            if (disk) {
                     74:                *disk = '\0';
                     75:                StrAllocCat (default_default, "/");  /* needs delimiter */
                     76:                StrAllocCat (default_default, wd);
                     77:            }
                     78:            if (dir) {
                     79:                char *p;
                     80:                *dir = '/';  /* Convert leading '[' */
                     81:                for (p = dir ; *p != ']'; ++p)
                     82:                        if (*p == '.') *p = '/';
                     83:                *p = '\0';  /* Cut on final ']' */
                     84:                StrAllocCat (default_default, dir);
                     85:            }
                     86: #else  /* not VMS */
                     87: #ifdef WIN32
                     88:            char * p = wd ;     /* a colon */
                     89:            StrAllocCat(default_default, "/");
                     90:            while( *p != 0 ) { 
                     91:                if (*p == '\\')                  /* change to one true slash */
                     92:                    *p = '/' ;
                     93:                p++;
                     94:            }
                     95:            StrAllocCat( default_default, wd);
                     96: #else /* not WIN32 */
                     97:            StrAllocCat (default_default, wd);
                     98: #endif /* not WIN32 */
                     99: #endif /* not VMS */
                    100:        }
                    101:     }
                    102:     StrAllocCat(default_default, "/default.html");
                    103:     return default_default;
                    104: }
                    105: 
                    106: 
                    107: /*     Generate the anchor for the home page
                    108: **     -------------------------------------
                    109: **
                    110: **     As it involves file access, this should only be done once
                    111: **     when the program first runs.
                    112: **     This is a default algorithm -- browser don't HAVE to use this.
                    113: **     But consistency betwen browsers is STRONGLY recommended!
                    114: **
                    115: **     Priority order is:
                    116: **
                    117: **             1       WWW_HOME environment variable (logical name, etc)
                    118: **             2       ~/WWW/default.html
                    119: **             3       /usr/local/bin/default.html
                    120: **             4       http://www.w3.org/default.html
                    121: **
                    122: */
                    123: PUBLIC HTParentAnchor * HTHomeAnchor (void)
                    124: {
                    125:     char * my_home_document = NULL;
                    126:     char * home = (char *) getenv(LOGICAL_DEFAULT);
                    127:     char * ref;
                    128:     HTParentAnchor * anchor;
                    129:     
                    130:     /* Someone telnets in, they get a special home */
                    131:     if (home) {
                    132:         StrAllocCopy(my_home_document, home);
                    133:     } else  if (HTLib_secure()) {                                  /* Telnet server */
                    134:        FILE * fp = fopen(REMOTE_POINTER, "r");
                    135:        char * status;
                    136:        if (fp) {
                    137:            my_home_document = (char*) malloc(HT_MAX_PATH);
                    138:            status = fgets(my_home_document, HT_MAX_PATH, fp);
                    139:            if (!status) {
                    140:                free(my_home_document);
                    141:                my_home_document = NULL;
                    142:            }
                    143:            fclose(fp);
                    144:        }
                    145:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                    146:     }
                    147: 
                    148: #ifdef unix
                    149:     if (!my_home_document) {
                    150:        FILE * fp = NULL;
                    151:        char * home = (char *) getenv("HOME");
                    152:        if (home) { 
                    153:            my_home_document = (char *)malloc(
                    154:                strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1);
                    155:            if (my_home_document == NULL) outofmem(__FILE__, "HTLocalName");
                    156:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                    157:            fp = fopen(my_home_document, "r");
                    158:        }
                    159:        
                    160:        if (!fp) {
                    161:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                    162:            fp = fopen(my_home_document, "r");
                    163:        }
                    164:        if (fp) {
                    165:            fclose(fp);
                    166:        } else {
                    167:            if (WWWTRACE)
                    168:                TTYPrint(TDEST,
                    169:                        "HTBrowse: No local home document ~/%s or %s\n",
                    170:                        PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
                    171:            free(my_home_document);
                    172:            my_home_document = NULL;
                    173:        }
                    174:     }
                    175: #endif
                    176:     ref = HTParse(my_home_document ? my_home_document :
                    177:                  HTLib_secure() ? REMOTE_ADDRESS : LAST_RESORT, "file:",
                    178:                  PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    179:     if (my_home_document) {
                    180:        if (WWWTRACE)
                    181:            TTYPrint(TDEST,
                    182:                   "HTAccess.... `%s\' used for custom home page as\n`%s\'\n",
                    183:                    my_home_document, ref);
                    184:        free(my_home_document);
                    185:     }
                    186:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
                    187:     free(ref);
                    188:     return anchor;
2.2       frystyk   189: }
                    190: 
                    191: /*     Application "BEFORE" Callback
                    192: **     -----------------------------
                    193: **     This function uses all the functionaly that the app part of the Library
                    194: **     gives for URL translations BEFORE a request is isseud.
                    195: **     It checks for Cache, proxy, and gateway (in that order)
2.3       frystyk   196: **     returns         HT_LOADED               We already have this
                    197: **                     HT_ERROR                We can't load this
2.2       frystyk   198: **                     HT_OK                   Success
                    199: */
                    200: PUBLIC int HTLoadStart (HTRequest * request, int status)
                    201: {    
                    202:     HTParentAnchor *anchor = HTRequest_anchor(request);
                    203:     char * addr = HTAnchor_address((HTAnchor *) anchor);
                    204:     HTReload mode = HTRequest_reloadMode(request);
                    205: 
                    206:     /*
                    207:     ** Check if document is already loaded. 
                    208:     */
                    209:     if (mode != HT_FORCE_RELOAD) {
                    210:        if (HTMemoryCache_check(request) == HT_LOADED) {
                    211:            free(addr);
                    212:            return HT_LOADED;
                    213:        }
                    214:     } else {
                    215:        HTRequest_addRqHd(request, HT_NO_CACHE);          /* No-cache pragma */
                    216:        HTAnchor_clearHeader(request->anchor);
                    217:     }
                    218: 
                    219:     /*
                    220:     ** Check if we have any rule translations to do
                    221:     */
                    222:     {
                    223:        HTList *list = HTRule_global();
                    224:        char * physical = HTRule_translate(list, addr, NO);
                    225:        if (!physical) {
                    226:            char *url = HTAnchor_address((HTAnchor *) request->anchor);
                    227:            if (url) {
                    228:                HTUnEscape(url);
                    229:                HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    230:                           (void *) url, (int) strlen(url), "HTLoad");
                    231:            } else {
                    232:                HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    233:                           NULL, 0, "HTLoad");
                    234:            }
                    235:            free(addr);
                    236:            FREE(url);
2.3       frystyk   237:            return HT_ERROR;
2.2       frystyk   238:        }
                    239:        HTAnchor_setPhysical(anchor, physical);
                    240:        free(physical);
                    241:     }
                    242: 
                    243:     /*
                    244:     ** Check local Disk Cache (if we are not forced to reload), then
                    245:     ** for proxy, and finally gateways
                    246:     */
                    247:     {
                    248:        char *newaddr=NULL;
                    249:        if (mode != HT_FORCE_RELOAD && (newaddr = HTCache_getReference(addr))){
                    250:            if (mode != HT_CACHE_REFRESH) {
                    251:                HTAnchor_setPhysical(anchor, newaddr);
                    252:                HTAnchor_setCacheHit(anchor, YES);
                    253:            } else {                     /* If refresh version in file cache */
                    254:                HTRequest_addRqHd(request, HT_IMS+HT_NO_CACHE);
                    255:            }
                    256:        } else if ((newaddr = HTProxy_find(addr))) {
                    257:            StrAllocCat(newaddr, addr);
                    258:            request->using_proxy = YES;
                    259:            HTAnchor_setPhysical(anchor, newaddr);
                    260:        } else if ((newaddr = HTGateway_find(addr))) {
                    261:            char * path = HTParse(addr, "",
                    262:                                  PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
                    263:                /* Chop leading / off to make host into part of path */
                    264:            char * gatewayed = HTParse(path+1, newaddr, PARSE_ALL);
                    265:             HTAnchor_setPhysical(anchor, gatewayed);
                    266:            free(path);
                    267:            free(gatewayed);
                    268:        } else {
                    269:            request->using_proxy = NO;      /* We don't use proxy or gateway */
                    270:        }
                    271:        FREE(newaddr);
                    272:     }
                    273:     FREE(addr);
2.4       frystyk   274:     return HT_OK;
2.2       frystyk   275: }
                    276: 
                    277: /*     Application "AFTER" Callback
                    278: **     -----------------------------
                    279: **     This function uses all the functionaly that the app part of the Library
                    280: **     gives for handling AFTER a request.
                    281: */
                    282: PUBLIC int HTLoadTerminate (HTRequest * request, int status)
2.5     ! frystyk   283: {
        !           284:     char * uri = HTAnchor_address((HTAnchor*)request->anchor);
        !           285:     switch (status) {
        !           286:       case HT_LOADED:
        !           287:        if (PROT_TRACE)
        !           288:            TTYPrint(TDEST, "Load End.... OK: `%s\' has been accessed\n", uri);
        !           289:        break;
        !           290: 
        !           291:       case HT_NO_DATA:
        !           292:        if (PROT_TRACE)
        !           293:            TTYPrint(TDEST, "Load End.... OK BUT NO DATA: `%s\'\n", uri);
        !           294:        break;
        !           295: 
        !           296:       case HT_INTERRUPTED:
        !           297:        if (PROT_TRACE)
        !           298:            TTYPrint(TDEST, "Load End.... INTERRUPTED: `%s\'\n", uri);
        !           299:        break;
        !           300: 
        !           301:       case HT_RETRY:
        !           302:        if (PROT_TRACE)
        !           303:            TTYPrint(TDEST, "Load End.... NOT AVAILABLE, RETRY AT %ld\n",
        !           304:                     HTRequest_retryTime(request));
        !           305:        break;
        !           306: 
        !           307:       case HT_ERROR:
        !           308:        {
        !           309:            HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
        !           310:            if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
        !           311:                            request->error_stack, NULL);
        !           312:        }
        !           313:        if (PROT_TRACE)
        !           314:            TTYPrint(TDEST, "Load End.... ERROR: Can't access `%s\'\n", uri);
        !           315:        break;
        !           316: 
        !           317:       default:
        !           318:        if (PROT_TRACE)
        !           319:            TTYPrint(TDEST, "Load End.... UNKNOWN RETURN CODE %d\n", status);
        !           320:        break;
        !           321:     }
2.2       frystyk   322: 
                    323:     /* Should we do logging? */
                    324:     if (HTLog_isOpen()) HTLog_add(request, status);
2.5     ! frystyk   325:     free(uri);
2.2       frystyk   326:     return HT_OK;
2.1       frystyk   327: }

Webmaster