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

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"
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.
                     45: **     The string returned must be freed by the caller
                     46: */
                     47: PUBLIC char * HTFindRelatedName (void)
                     48: {
                     49:     char* default_default = NULL;            /* Parse home relative to this */
                     50:     CONST char *host = HTGetHostName(); 
                     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: 
                     59: #ifdef NO_GETWD
                     60: #ifdef HAS_GETCWD            /* System V variant SIGN CHANGED TBL 921006 !! */
                     61:        char *result = (char *) getcwd(wd, sizeof(wd)); 
                     62: #else
                     63:        char *result = NULL;
                     64:        HTAlert("This platform does not support neither getwd nor getcwd\n");
                     65: #endif
                     66: #else
                     67:        char *result = (char *) getwd(wd);
                     68: #endif
                     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: #ifdef unix
                    152:     if (!my_home_document) {
                    153:        FILE * fp = NULL;
                    154:        char * home = (char *) getenv("HOME");
                    155:        if (home) { 
2.10    ! frystyk   156:            if ((my_home_document = (char  *) HT_MALLOC(strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1)) == NULL)
        !           157:                HT_OUTOFMEM("HTLocalName");
2.1       frystyk   158:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                    159:            fp = fopen(my_home_document, "r");
                    160:        }
                    161:        
                    162:        if (!fp) {
                    163:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                    164:            fp = fopen(my_home_document, "r");
                    165:        }
                    166:        if (fp) {
                    167:            fclose(fp);
                    168:        } else {
                    169:            if (WWWTRACE)
                    170:                TTYPrint(TDEST,
                    171:                        "HTBrowse: No local home document ~/%s or %s\n",
                    172:                        PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
2.10    ! frystyk   173:            HT_FREE(my_home_document);
2.1       frystyk   174:            my_home_document = NULL;
                    175:        }
                    176:     }
                    177: #endif
                    178:     ref = HTParse(my_home_document ? my_home_document :
                    179:                  HTLib_secure() ? REMOTE_ADDRESS : LAST_RESORT, "file:",
                    180:                  PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    181:     if (my_home_document) {
                    182:        if (WWWTRACE)
                    183:            TTYPrint(TDEST,
                    184:                   "HTAccess.... `%s\' used for custom home page as\n`%s\'\n",
                    185:                    my_home_document, ref);
2.10    ! frystyk   186:        HT_FREE(my_home_document);
2.1       frystyk   187:     }
                    188:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
2.10    ! frystyk   189:     HT_FREE(ref);
2.1       frystyk   190:     return anchor;
2.2       frystyk   191: }
                    192: 
                    193: /*     Application "BEFORE" Callback
                    194: **     -----------------------------
                    195: **     This function uses all the functionaly that the app part of the Library
                    196: **     gives for URL translations BEFORE a request is isseud.
                    197: **     It checks for Cache, proxy, and gateway (in that order)
2.3       frystyk   198: **     returns         HT_LOADED               We already have this
                    199: **                     HT_ERROR                We can't load this
2.2       frystyk   200: **                     HT_OK                   Success
                    201: */
                    202: PUBLIC int HTLoadStart (HTRequest * request, int status)
                    203: {    
                    204:     HTParentAnchor *anchor = HTRequest_anchor(request);
                    205:     char * addr = HTAnchor_address((HTAnchor *) anchor);
                    206:     HTReload mode = HTRequest_reloadMode(request);
                    207: 
                    208:     /*
                    209:     ** Check if document is already loaded. 
                    210:     */
                    211:     if (mode != HT_FORCE_RELOAD) {
                    212:        if (HTMemoryCache_check(request) == HT_LOADED) {
2.10    ! frystyk   213:            HT_FREE(addr);
2.2       frystyk   214:            return HT_LOADED;
                    215:        }
                    216:     } else {
2.7       frystyk   217:        HTRequest_addGnHd(request, HT_G_NO_CACHE);        /* No-cache pragma */
2.2       frystyk   218:        HTAnchor_clearHeader(request->anchor);
                    219:     }
                    220: 
                    221:     /*
                    222:     ** Check if we have any rule translations to do
                    223:     */
                    224:     {
                    225:        HTList *list = HTRule_global();
                    226:        char * physical = HTRule_translate(list, addr, NO);
                    227:        if (!physical) {
                    228:            char *url = HTAnchor_address((HTAnchor *) request->anchor);
                    229:            if (url) {
                    230:                HTUnEscape(url);
                    231:                HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    232:                           (void *) url, (int) strlen(url), "HTLoad");
                    233:            } else {
                    234:                HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                    235:                           NULL, 0, "HTLoad");
                    236:            }
2.10    ! frystyk   237:            HT_FREE(addr);
        !           238:            HT_FREE(url);
2.3       frystyk   239:            return HT_ERROR;
2.2       frystyk   240:        }
                    241:        HTAnchor_setPhysical(anchor, physical);
2.10    ! frystyk   242:        HT_FREE(physical);
2.2       frystyk   243:     }
                    244: 
                    245:     /*
                    246:     ** Check local Disk Cache (if we are not forced to reload), then
                    247:     ** for proxy, and finally gateways
                    248:     */
                    249:     {
                    250:        char *newaddr=NULL;
                    251:        if (mode != HT_FORCE_RELOAD && (newaddr = HTCache_getReference(addr))){
                    252:            if (mode != HT_CACHE_REFRESH) {
                    253:                HTAnchor_setPhysical(anchor, newaddr);
                    254:                HTAnchor_setCacheHit(anchor, YES);
                    255:            } else {                     /* If refresh version in file cache */
2.6       frystyk   256:                HTRequest_addGnHd(request, HT_G_NO_CACHE);
                    257:                HTRequest_addRqHd(request, HT_C_IMS);
2.2       frystyk   258:            }
                    259:        } else if ((newaddr = HTProxy_find(addr))) {
                    260:            StrAllocCat(newaddr, addr);
                    261:            request->using_proxy = YES;
                    262:            HTAnchor_setPhysical(anchor, newaddr);
                    263:        } else if ((newaddr = HTGateway_find(addr))) {
                    264:            char * path = HTParse(addr, "",
                    265:                                  PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
                    266:                /* Chop leading / off to make host into part of path */
                    267:            char * gatewayed = HTParse(path+1, newaddr, PARSE_ALL);
                    268:             HTAnchor_setPhysical(anchor, gatewayed);
2.10    ! frystyk   269:            HT_FREE(path);
        !           270:            HT_FREE(gatewayed);
2.2       frystyk   271:        } else {
                    272:            request->using_proxy = NO;      /* We don't use proxy or gateway */
                    273:        }
2.10    ! frystyk   274:        HT_FREE(newaddr);
2.2       frystyk   275:     }
2.10    ! frystyk   276:     HT_FREE(addr);
2.4       frystyk   277:     return HT_OK;
2.2       frystyk   278: }
                    279: 
                    280: /*     Application "AFTER" Callback
                    281: **     -----------------------------
                    282: **     This function uses all the functionaly that the app part of the Library
                    283: **     gives for handling AFTER a request.
                    284: */
                    285: PUBLIC int HTLoadTerminate (HTRequest * request, int status)
2.5       frystyk   286: {
                    287:     char * uri = HTAnchor_address((HTAnchor*)request->anchor);
                    288:     switch (status) {
                    289:       case HT_LOADED:
                    290:        if (PROT_TRACE)
                    291:            TTYPrint(TDEST, "Load End.... OK: `%s\' has been accessed\n", uri);
                    292:        break;
                    293: 
                    294:       case HT_NO_DATA:
                    295:        if (PROT_TRACE)
                    296:            TTYPrint(TDEST, "Load End.... OK BUT NO DATA: `%s\'\n", uri);
                    297:        break;
                    298: 
                    299:       case HT_INTERRUPTED:
                    300:        if (PROT_TRACE)
                    301:            TTYPrint(TDEST, "Load End.... INTERRUPTED: `%s\'\n", uri);
                    302:        break;
                    303: 
                    304:       case HT_RETRY:
                    305:        if (PROT_TRACE)
                    306:            TTYPrint(TDEST, "Load End.... NOT AVAILABLE, RETRY AT %ld\n",
                    307:                     HTRequest_retryTime(request));
                    308:        break;
                    309: 
                    310:       case HT_ERROR:
                    311:        {
                    312:            HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    313:            if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    314:                            request->error_stack, NULL);
                    315:        }
                    316:        if (PROT_TRACE)
2.8       frystyk   317:            TTYPrint(TDEST, "Load End.... ERROR: Can't access `%s\'\n",
                    318:                     uri ? uri : "<UNKNOWN>");
2.5       frystyk   319:        break;
                    320: 
                    321:       default:
                    322:        if (PROT_TRACE)
                    323:            TTYPrint(TDEST, "Load End.... UNKNOWN RETURN CODE %d\n", status);
                    324:        break;
                    325:     }
2.2       frystyk   326: 
                    327:     /* Should we do logging? */
                    328:     if (HTLog_isOpen()) HTLog_add(request, status);
2.10    ! frystyk   329:     HT_FREE(uri);
2.2       frystyk   330:     return HT_OK;
2.1       frystyk   331: }

Webmaster