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

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
2.13    ! frystyk    22: **      8 Jul 94 Insulate free from _free structure element.
2.1       frystyk    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"
2.9       frystyk    30: #include "WWWCache.h"
                     31: #include "WWWRules.h"
2.2       frystyk    32: #include "HTReqMan.h"
2.1       frystyk    33: #include "HTHome.h"                                     /* Implemented here */
                     34: 
                     35: /* ------------------------------------------------------------------------- */
                     36: 
                     37: /*     Find Related Name
                     38: **     -----------------
                     39: **     Creates a string that can be used as a related name when 
                     40: **     calling HTParse initially. 
                     41: **  
                     42: **     The code for this routine originates from the Linemode 
                     43: **     browser and was moved here by howcome@w3.org
                     44: **     in order for all clients to take advantage.
2.12      frystyk    45: **     The string returned must be HT_FREEd by the caller
2.1       frystyk    46: */
                     47: PUBLIC char * HTFindRelatedName (void)
                     48: {
                     49:     char* default_default = NULL;            /* Parse home relative to this */
2.12      frystyk    50:     const char *host = HTGetHostName(); 
2.1       frystyk    51:     StrAllocCopy(default_default, "file://");
                     52:     if (host)
                     53:        StrAllocCat(default_default, host);
                     54:     else
                     55:        StrAllocCat(default_default, "localhost");
                     56:     {
                     57:        char wd[HT_MAX_PATH+1];
                     58: 
2.12      frystyk    59: #ifdef HAVE_GETWD
                     60:        char *result = (char *) getwd(wd);
                     61: #else
                     62: #ifdef HAVE_GETCWD           /* System V variant SIGN CHANGED TBL 921006 !! */
2.1       frystyk    63:        char *result = (char *) getcwd(wd, sizeof(wd)); 
                     64: #else
2.12      frystyk    65: #error "This platform does not support neither getwd nor getcwd\n"
2.1       frystyk    66:        char *result = NULL;
2.12      frystyk    67: #endif /* HAVE_GETCWD */
                     68: #endif /* HAVE_GETWD */
2.1       frystyk    69:        *(wd+HT_MAX_PATH) = '\0';
                     70:        if (result) {
                     71: #ifdef VMS 
                     72:             /* convert directory name to Unix-style syntax */
                     73:            char * disk = strchr (wd, ':');
                     74:            char * dir = strchr (wd, '[');
                     75:            if (disk) {
                     76:                *disk = '\0';
                     77:                StrAllocCat (default_default, "/");  /* needs delimiter */
                     78:                StrAllocCat (default_default, wd);
                     79:            }
                     80:            if (dir) {
                     81:                char *p;
                     82:                *dir = '/';  /* Convert leading '[' */
                     83:                for (p = dir ; *p != ']'; ++p)
                     84:                        if (*p == '.') *p = '/';
                     85:                *p = '\0';  /* Cut on final ']' */
                     86:                StrAllocCat (default_default, dir);
                     87:            }
                     88: #else  /* not VMS */
                     89: #ifdef WIN32
                     90:            char * p = wd ;     /* a colon */
                     91:            StrAllocCat(default_default, "/");
                     92:            while( *p != 0 ) { 
                     93:                if (*p == '\\')                  /* change to one true slash */
                     94:                    *p = '/' ;
                     95:                p++;
                     96:            }
                     97:            StrAllocCat( default_default, wd);
                     98: #else /* not WIN32 */
                     99:            StrAllocCat (default_default, wd);
                    100: #endif /* not WIN32 */
                    101: #endif /* not VMS */
                    102:        }
                    103:     }
                    104:     StrAllocCat(default_default, "/default.html");
                    105:     return default_default;
                    106: }
                    107: 
                    108: 
                    109: /*     Generate the anchor for the home page
                    110: **     -------------------------------------
                    111: **
                    112: **     As it involves file access, this should only be done once
                    113: **     when the program first runs.
                    114: **     This is a default algorithm -- browser don't HAVE to use this.
                    115: **     But consistency betwen browsers is STRONGLY recommended!
                    116: **
                    117: **     Priority order is:
                    118: **
                    119: **             1       WWW_HOME environment variable (logical name, etc)
                    120: **             2       ~/WWW/default.html
                    121: **             3       /usr/local/bin/default.html
                    122: **             4       http://www.w3.org/default.html
                    123: **
                    124: */
                    125: PUBLIC HTParentAnchor * HTHomeAnchor (void)
                    126: {
                    127:     char * my_home_document = NULL;
                    128:     char * home = (char *) getenv(LOGICAL_DEFAULT);
                    129:     char * ref;
                    130:     HTParentAnchor * anchor;
                    131:     
                    132:     /* Someone telnets in, they get a special home */
                    133:     if (home) {
                    134:         StrAllocCopy(my_home_document, home);
                    135:     } else  if (HTLib_secure()) {                                  /* Telnet server */
                    136:        FILE * fp = fopen(REMOTE_POINTER, "r");
                    137:        char * status;
                    138:        if (fp) {
2.10      frystyk   139:            if ((my_home_document = (char *) HT_MALLOC(HT_MAX_PATH)) == NULL)
                    140:                HT_OUTOFMEM("my_home_document ");
2.1       frystyk   141:            status = fgets(my_home_document, HT_MAX_PATH, fp);
                    142:            if (!status) {
2.10      frystyk   143:                HT_FREE(my_home_document);
2.1       frystyk   144:                my_home_document = NULL;
                    145:            }
                    146:            fclose(fp);
                    147:        }
                    148:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                    149:     }
                    150: 
                    151:     if (!my_home_document) {
                    152:        FILE * fp = NULL;
                    153:        char * home = (char *) getenv("HOME");
                    154:        if (home) { 
2.10      frystyk   155:            if ((my_home_document = (char  *) HT_MALLOC(strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1)) == NULL)
                    156:                HT_OUTOFMEM("HTLocalName");
2.1       frystyk   157:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                    158:            fp = fopen(my_home_document, "r");
                    159:        }
                    160:        
                    161:        if (!fp) {
                    162:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                    163:            fp = fopen(my_home_document, "r");
                    164:        }
                    165:        if (fp) {
                    166:            fclose(fp);
                    167:        } else {
                    168:            if (WWWTRACE)
2.12      frystyk   169:                HTTrace("Home Anchor. No local home document in ~/%s or %s\n",
2.1       frystyk   170:                        PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
2.10      frystyk   171:            HT_FREE(my_home_document);
2.1       frystyk   172:            my_home_document = NULL;
                    173:        }
                    174:     }
2.12      frystyk   175: 
2.1       frystyk   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)
2.12      frystyk   181:            HTTrace("Home Anchor. `%s\' used for custom home page as\n`%s\'\n",
2.1       frystyk   182:                    my_home_document, ref);
2.10      frystyk   183:        HT_FREE(my_home_document);
2.1       frystyk   184:     }
                    185:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
2.10      frystyk   186:     HT_FREE(ref);
2.1       frystyk   187:     return anchor;
2.2       frystyk   188: }
                    189: 
                    190: /*     Application "BEFORE" Callback
                    191: **     -----------------------------
                    192: **     This function uses all the functionaly that the app part of the Library
                    193: **     gives for URL translations BEFORE a request is isseud.
                    194: **     It checks for Cache, proxy, and gateway (in that order)
2.3       frystyk   195: **     returns         HT_LOADED               We already have this
                    196: **                     HT_ERROR                We can't load this
2.2       frystyk   197: **                     HT_OK                   Success
                    198: */
                    199: PUBLIC int HTLoadStart (HTRequest * request, int status)
                    200: {    
                    201:     HTParentAnchor *anchor = HTRequest_anchor(request);
                    202:     char * addr = HTAnchor_address((HTAnchor *) anchor);
                    203:     HTReload mode = HTRequest_reloadMode(request);
                    204: 
                    205:     /*
                    206:     ** Check if document is already loaded. 
                    207:     */
                    208:     if (mode != HT_FORCE_RELOAD) {
                    209:        if (HTMemoryCache_check(request) == HT_LOADED) {
2.10      frystyk   210:            HT_FREE(addr);
2.2       frystyk   211:            return HT_LOADED;
                    212:        }
                    213:     } else {
2.7       frystyk   214:        HTRequest_addGnHd(request, HT_G_NO_CACHE);        /* No-cache pragma */
2.2       frystyk   215:        HTAnchor_clearHeader(request->anchor);
                    216:     }
                    217: 
                    218:     /*
                    219:     ** Check if we have any rule translations to do
                    220:     */
                    221:     {
                    222:        HTList *list = HTRule_global();
                    223:        char * physical = HTRule_translate(list, addr, NO);
                    224:        if (!physical) {
                    225:            char *url = HTAnchor_address((HTAnchor *) request->anchor);
                    226:            if (url) {
                    227:                HTUnEscape(url);
                    228:                HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    229:                           (void *) url, (int) strlen(url), "HTLoad");
                    230:            } else {
                    231:                HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    232:                           NULL, 0, "HTLoad");
                    233:            }
2.10      frystyk   234:            HT_FREE(addr);
                    235:            HT_FREE(url);
2.3       frystyk   236:            return HT_ERROR;
2.2       frystyk   237:        }
                    238:        HTAnchor_setPhysical(anchor, physical);
2.10      frystyk   239:        HT_FREE(physical);
2.2       frystyk   240:     }
                    241: 
                    242:     /*
                    243:     ** Check local Disk Cache (if we are not forced to reload), then
                    244:     ** for proxy, and finally gateways
                    245:     */
                    246:     {
                    247:        char *newaddr=NULL;
                    248:        if (mode != HT_FORCE_RELOAD && (newaddr = HTCache_getReference(addr))){
                    249:            if (mode != HT_CACHE_REFRESH) {
                    250:                HTAnchor_setPhysical(anchor, newaddr);
                    251:                HTAnchor_setCacheHit(anchor, YES);
                    252:            } else {                     /* If refresh version in file cache */
2.6       frystyk   253:                HTRequest_addGnHd(request, HT_G_NO_CACHE);
                    254:                HTRequest_addRqHd(request, HT_C_IMS);
2.2       frystyk   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);
2.10      frystyk   266:            HT_FREE(path);
                    267:            HT_FREE(gatewayed);
2.2       frystyk   268:        } else {
                    269:            request->using_proxy = NO;      /* We don't use proxy or gateway */
                    270:        }
2.10      frystyk   271:        HT_FREE(newaddr);
2.2       frystyk   272:     }
2.10      frystyk   273:     HT_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_RETRY:
                    287:        if (PROT_TRACE)
2.11      eric      288:            HTTrace("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
2.5       frystyk   289:                     HTRequest_retryTime(request));
                    290:        break;
                    291: 
                    292:       case HT_ERROR:
                    293:        {
                    294:            HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    295:            if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    296:                            request->error_stack, NULL);
                    297:        }
                    298:        if (PROT_TRACE)
2.11      eric      299:            HTTrace("Load End.... ERROR: Can't access `%s\'\n",
2.8       frystyk   300:                     uri ? uri : "<UNKNOWN>");
2.5       frystyk   301:        break;
                    302: 
                    303:       default:
                    304:        if (PROT_TRACE)
2.13    ! frystyk   305:            HTTrace("Load End.... Request ended with code %d\n", status);
2.5       frystyk   306:        break;
                    307:     }
2.2       frystyk   308: 
                    309:     /* Should we do logging? */
                    310:     if (HTLog_isOpen()) HTLog_add(request, status);
2.10      frystyk   311:     HT_FREE(uri);
2.2       frystyk   312:     return HT_OK;
2.1       frystyk   313: }

Webmaster