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

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.
2.31    ! frystyk     6: **     @(#) $Id: HTHome.c,v 2.30 1998/05/31 19:03:20 frystyk Exp $
2.1       frystyk     7: **
                      8: ** Authors
                      9: **     TBL     Tim Berners-Lee timbl@w3.org
                     10: **     JFG     Jean-Francois Groff jfg@dxcern.cern.ch
                     11: **     DD      Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
2.2       frystyk    12: **     HFN     Henrik Frystyk
2.1       frystyk    13: ** History
                     14: **       8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
                     15: **     26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
                     16: **      6 Oct 92 Moved HTClientHost and HTlogfile into here. TBL
                     17: **     17 Dec 92 Tn3270 added, bug fix. DD
                     18: **      4 Feb 93 Access registration, Search escapes bad chars TBL
                     19: **               PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
                     20: **     28 May 93 WAIS gateway explicit if no WAIS library linked in.
                     21: **        Dec 93 Bug change around, more reentrant, etc
                     22: **     09 May 94 logfile renamed to HTlogfile to avoid clash with WAIS
2.13      frystyk    23: **      8 Jul 94 Insulate free from _free structure element.
2.1       frystyk    24: **        Sep 95 Rewritten, HFN
                     25: **        Nov 95 Spawned from HTAccess.c
                     26: */
                     27: 
                     28: /* Library include files */
                     29: #include "WWWLib.h"
                     30: #include "HTHome.h"                                     /* Implemented here */
                     31: 
                     32: /* ------------------------------------------------------------------------- */
                     33: 
                     34: /*     Find Related Name
                     35: **     -----------------
                     36: **     Creates a string that can be used as a related name when 
                     37: **     calling HTParse initially. 
                     38: **  
                     39: **     The code for this routine originates from the Linemode 
                     40: **     browser and was moved here by howcome@w3.org
                     41: **     in order for all clients to take advantage.
2.18      frystyk    42: **     The string returned must be freed by the caller
2.1       frystyk    43: */
2.18      frystyk    44: PUBLIC char * HTGetCurrentDirectoryURL (void)
2.1       frystyk    45: {
2.30      frystyk    46:     char wd[HT_MAX_PATH+2];
2.1       frystyk    47: 
2.14      frystyk    48: #ifdef HAVE_GETCWD           /* System V variant SIGN CHANGED TBL 921006 !! */
2.29      frystyk    49:     char * result = (char *) getcwd(wd, sizeof(wd)); 
2.14      frystyk    50: #else
2.12      frystyk    51: #ifdef HAVE_GETWD
2.29      frystyk    52:     char * result = (char *) getwd(wd);
2.12      frystyk    53: #else
                     54: #error "This platform does not support neither getwd nor getcwd\n"
2.29      frystyk    55:     char * result = NULL;
2.14      frystyk    56: #endif /* HAVE_GETWD */
2.12      frystyk    57: #endif /* HAVE_GETCWD */
2.29      frystyk    58:     *(wd+HT_MAX_PATH) = '\0';
2.30      frystyk    59:     if (*(wd+strlen(wd)-1) != '/') strcat(wd, "/");
2.29      frystyk    60:     return result ? HTLocalToWWW(result) : NULL;
2.1       frystyk    61: }
                     62: 
                     63: 
                     64: /*     Generate the anchor for the home page
                     65: **     -------------------------------------
                     66: **
                     67: **     As it involves file access, this should only be done once
                     68: **     when the program first runs.
                     69: **     This is a default algorithm -- browser don't HAVE to use this.
                     70: **     But consistency betwen browsers is STRONGLY recommended!
                     71: **
                     72: **     Priority order is:
                     73: **
                     74: **             1       WWW_HOME environment variable (logical name, etc)
                     75: **             2       ~/WWW/default.html
                     76: **             3       /usr/local/bin/default.html
                     77: **             4       http://www.w3.org/default.html
                     78: **
                     79: */
                     80: PUBLIC HTParentAnchor * HTHomeAnchor (void)
                     81: {
                     82:     char * my_home_document = NULL;
                     83:     char * home = (char *) getenv(LOGICAL_DEFAULT);
                     84:     char * ref;
                     85:     HTParentAnchor * anchor;
                     86:     
                     87:     /* Someone telnets in, they get a special home */
                     88:     if (home) {
                     89:         StrAllocCopy(my_home_document, home);
                     90:     } else  if (HTLib_secure()) {                                  /* Telnet server */
                     91:        FILE * fp = fopen(REMOTE_POINTER, "r");
                     92:        char * status;
                     93:        if (fp) {
2.10      frystyk    94:            if ((my_home_document = (char *) HT_MALLOC(HT_MAX_PATH)) == NULL)
                     95:                HT_OUTOFMEM("my_home_document ");
2.1       frystyk    96:            status = fgets(my_home_document, HT_MAX_PATH, fp);
                     97:            if (!status) {
2.10      frystyk    98:                HT_FREE(my_home_document);
2.1       frystyk    99:                my_home_document = NULL;
                    100:            }
                    101:            fclose(fp);
                    102:        }
                    103:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                    104:     }
                    105: 
                    106:     if (!my_home_document) {
                    107:        FILE * fp = NULL;
                    108:        char * home = (char *) getenv("HOME");
                    109:        if (home) { 
2.10      frystyk   110:            if ((my_home_document = (char  *) HT_MALLOC(strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1)) == NULL)
                    111:                HT_OUTOFMEM("HTLocalName");
2.1       frystyk   112:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                    113:            fp = fopen(my_home_document, "r");
                    114:        }
                    115:        
                    116:        if (!fp) {
                    117:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                    118:            fp = fopen(my_home_document, "r");
                    119:        }
                    120:        if (fp) {
                    121:            fclose(fp);
                    122:        } else {
                    123:            if (WWWTRACE)
2.12      frystyk   124:                HTTrace("Home Anchor. No local home document in ~/%s or %s\n",
2.1       frystyk   125:                        PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
2.10      frystyk   126:            HT_FREE(my_home_document);
2.1       frystyk   127:            my_home_document = NULL;
                    128:        }
                    129:     }
2.12      frystyk   130: 
2.1       frystyk   131:     ref = HTParse(my_home_document ? my_home_document :
                    132:                  HTLib_secure() ? REMOTE_ADDRESS : LAST_RESORT, "file:",
                    133:                  PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    134:     if (my_home_document) {
                    135:        if (WWWTRACE)
2.12      frystyk   136:            HTTrace("Home Anchor. `%s\' used for custom home page as\n`%s\'\n",
2.1       frystyk   137:                    my_home_document, ref);
2.10      frystyk   138:        HT_FREE(my_home_document);
2.1       frystyk   139:     }
                    140:     anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
2.10      frystyk   141:     HT_FREE(ref);
2.1       frystyk   142:     return anchor;
2.19      frystyk   143: }
                    144: 
                    145: /*
2.22      frystyk   146: **     Creates a temporary anchor that doesn't exist
                    147: */
                    148: PUBLIC HTParentAnchor * HTTmpAnchor (HTUserProfile * up)
                    149: {
                    150:     static int offset = 0;                         /* Just keep counting... */
                    151:     time_t t = time(NULL);
                    152:     char * tmpfile = HTGetTmpFileName(HTUserProfile_tmp(up));
                    153:     char * tmpurl = HTParse(tmpfile, "file:", PARSE_ALL);
                    154:     if (tmpfile && tmpurl && t >= 0) {
                    155:        char * result;
                    156:        if (!(result = (char *) HT_MALLOC(strlen(tmpurl)+20)))
                    157:            HT_OUTOFMEM("HTTmpAnchor");
2.27      frystyk   158: #ifdef HAVE_LONG_TIME_T
2.23      frystyk   159:        sprintf(result, "%s.%ld.%d", tmpurl, t, offset++);
2.27      frystyk   160: #else
                    161:        sprintf(result, "%s.%d.%d", tmpurl, t, offset++);
                    162: #endif
2.22      frystyk   163:        if (APP_TRACE) HTTrace("Tmp Anchor.. With location `%s\'\n", result);
                    164:        return HTAnchor_parent(HTAnchor_findAddress(result));
                    165:        HT_FREE(result);
                    166:     }
                    167:     HT_FREE(tmpfile);
                    168:     HT_FREE(tmpurl);
                    169:     return NULL;
                    170: }
                    171: 
                    172: /*
2.31    ! frystyk   173: **     Takes a string of the form "a=b" containing HTML form data, escapes
        !           174: **     it accordingly and puts it into the association list so that it
        !           175: **     readily can be passed to any of the HTAccess function that handles
        !           176: **     HTML form data.
        !           177: */
        !           178: PUBLIC BOOL HTParseFormInput (HTAssocList * list, const char * str)
        !           179: {
        !           180:     if (list && str) {
        !           181:        char * me = NULL;
        !           182:        char * name = NULL;
        !           183:        char * value = NULL;
        !           184:        StrAllocCopy(me, str);
        !           185:        value = strchr(me, '=');
        !           186:        if (value) 
        !           187:            *value++ = '\0';
        !           188:        else
        !           189:            value = "";
        !           190:        name = HTStrip(me);
        !           191: 
        !           192:        /* Escape the name and value */
        !           193:        if (name) {
        !           194:            char * escaped_name = HTEscape(name, URL_XALPHAS);
        !           195:            char * escaped_value = HTEscape(value, URL_XALPHAS);
        !           196:            if (APP_TRACE)
        !           197:                HTTrace("Form data... Adding name `%s\' with value `%s\' to %p\n",
        !           198:                        escaped_name, escaped_value, list);
        !           199:            HTAssocList_addObject(list, escaped_name, escaped_value);
        !           200:            HT_FREE(escaped_name);
        !           201:            HT_FREE(escaped_value);
        !           202:        }
        !           203:        HT_FREE(me);
        !           204:        return YES;
        !           205:     }
        !           206:     return NO;
        !           207: }
        !           208: 
        !           209: /*
2.19      frystyk   210: **     Standard interface to libwww TRACE messages. Pass this function a
                    211: **     string of characters and it will set up the appropriate TRACE flags.
                    212: **     The shortnames for the trace messages are not as intuitive as they
                    213: **     could be :-(. The string must be null terminated
                    214: */
                    215: PUBLIC int HTSetTraceMessageMask (const char * shortnames)
                    216: {
                    217: #ifdef WWWTRACE
                    218:     WWWTRACE = 0;
2.24      frystyk   219:     if (shortnames && *shortnames) {
2.21      frystyk   220:        char * ptr = (char *) shortnames;
                    221:        for(; *ptr; ptr++) {
2.19      frystyk   222:            switch (*ptr) {
2.22      frystyk   223:            case 'f': WWWTRACE |= SHOW_UTIL_TRACE;      break;
                    224:            case 'l': WWWTRACE |= SHOW_APP_TRACE;       break;
2.19      frystyk   225:            case 'c': WWWTRACE |= SHOW_CACHE_TRACE;     break;
                    226:            case 'g': WWWTRACE |= SHOW_SGML_TRACE;      break;
2.22      frystyk   227:            case 'b': WWWTRACE |= SHOW_BIND_TRACE;      break;
                    228:            case 't': WWWTRACE |= SHOW_THREAD_TRACE;    break;
                    229:            case 's': WWWTRACE |= SHOW_STREAM_TRACE;    break;
                    230:            case 'p': WWWTRACE |= SHOW_PROTOCOL_TRACE;  break;
                    231:            case 'm': WWWTRACE |= SHOW_MEM_TRACE;       break;
2.28      frystyk   232:            case 'q': WWWTRACE |= SHOW_SQL_TRACE;       break;
2.22      frystyk   233:            case 'u': WWWTRACE |= SHOW_URI_TRACE;       break;
2.19      frystyk   234:            case 'h': WWWTRACE |= SHOW_AUTH_TRACE;      break;
2.22      frystyk   235:            case 'a': WWWTRACE |= SHOW_ANCHOR_TRACE;    break;
2.19      frystyk   236:            case 'i': WWWTRACE |= SHOW_PICS_TRACE;      break;
                    237:            case 'o': WWWTRACE |= SHOW_CORE_TRACE;      break;
2.26      frystyk   238:            case 'x': WWWTRACE |= SHOW_MUX_TRACE;       break;
2.31    ! frystyk   239:            case '*': WWWTRACE |= SHOW_ALL_TRACE;       break;
2.19      frystyk   240:            default:
                    241:                if (WWWTRACE) HTTrace("Trace....... Bad argument\n");
                    242:            }
                    243:        }
2.20      frystyk   244:        if (!WWWTRACE) WWWTRACE = SHOW_ALL_TRACE;
2.25      frystyk   245:     } else {
                    246:        WWWTRACE = SHOW_ALL_TRACE;
2.19      frystyk   247:     }
                    248:     return WWWTRACE;
                    249: #else
                    250:     return 0;
                    251: #endif
2.1       frystyk   252: }

Webmaster