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

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.38    ! kahan       6: **     @(#) $Id: HTHome.c,v 2.37 2000/06/07 09:12:06 kahan 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.38    ! kahan      58:     if (result) {
        !            59:       *(wd+HT_MAX_PATH) = '\0';
        !            60:       if (*(wd+strlen(wd)-1) != '/') strcat(wd, "/");
        !            61:     }
2.32      frystyk    62:     return result ? HTLocalToWWW(result, NULL) : NULL;
2.1       frystyk    63: }
                     64: 
                     65: 
                     66: /*     Generate the anchor for the home page
                     67: **     -------------------------------------
                     68: **
                     69: **     As it involves file access, this should only be done once
                     70: **     when the program first runs.
                     71: **     This is a default algorithm -- browser don't HAVE to use this.
                     72: **     But consistency betwen browsers is STRONGLY recommended!
                     73: **
                     74: **     Priority order is:
                     75: **
                     76: **             1       WWW_HOME environment variable (logical name, etc)
                     77: **             2       ~/WWW/default.html
                     78: **             3       /usr/local/bin/default.html
                     79: **             4       http://www.w3.org/default.html
                     80: **
                     81: */
                     82: PUBLIC HTParentAnchor * HTHomeAnchor (void)
                     83: {
                     84:     char * my_home_document = NULL;
                     85:     char * home = (char *) getenv(LOGICAL_DEFAULT);
                     86:     char * ref;
                     87:     HTParentAnchor * anchor;
                     88:     
                     89:     /* Someone telnets in, they get a special home */
                     90:     if (home) {
                     91:         StrAllocCopy(my_home_document, home);
                     92:     } else  if (HTLib_secure()) {                                  /* Telnet server */
                     93:        FILE * fp = fopen(REMOTE_POINTER, "r");
                     94:        char * status;
                     95:        if (fp) {
2.10      frystyk    96:            if ((my_home_document = (char *) HT_MALLOC(HT_MAX_PATH)) == NULL)
                     97:                HT_OUTOFMEM("my_home_document ");
2.1       frystyk    98:            status = fgets(my_home_document, HT_MAX_PATH, fp);
                     99:            if (!status) {
2.10      frystyk   100:                HT_FREE(my_home_document);
2.1       frystyk   101:                my_home_document = NULL;
                    102:            }
                    103:            fclose(fp);
                    104:        }
                    105:        if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
                    106:     }
                    107: 
                    108:     if (!my_home_document) {
                    109:        FILE * fp = NULL;
                    110:        char * home = (char *) getenv("HOME");
                    111:        if (home) { 
2.10      frystyk   112:            if ((my_home_document = (char  *) HT_MALLOC(strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1)) == NULL)
                    113:                HT_OUTOFMEM("HTLocalName");
2.1       frystyk   114:            sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
                    115:            fp = fopen(my_home_document, "r");
                    116:        }
                    117:        
                    118:        if (!fp) {
                    119:            StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
                    120:            fp = fopen(my_home_document, "r");
                    121:        }
                    122:        if (fp) {
                    123:            fclose(fp);
                    124:        } else {
2.34      frystyk   125:            HTTRACE(APP_TRACE, "Home Anchor. No local home document in ~/%s or %s\n" _ 
                    126:                        PERSONAL_DEFAULT _ LOCAL_DEFAULT_FILE);
2.10      frystyk   127:            HT_FREE(my_home_document);
2.1       frystyk   128:            my_home_document = NULL;
                    129:        }
                    130:     }
2.12      frystyk   131: 
2.1       frystyk   132:     ref = HTParse(my_home_document ? my_home_document :
                    133:                  HTLib_secure() ? REMOTE_ADDRESS : LAST_RESORT, "file:",
                    134:                  PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    135:     if (my_home_document) {
2.34      frystyk   136:        HTTRACE(APP_TRACE, "Home Anchor. `%s\' used for custom home page as\n`%s\'\n" _ 
                    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... */
2.37      kahan     151:     HTParentAnchor * htpa = NULL;
2.22      frystyk   152:     time_t t = time(NULL);
                    153:     char * tmpfile = HTGetTmpFileName(HTUserProfile_tmp(up));
                    154:     char * tmpurl = HTParse(tmpfile, "file:", PARSE_ALL);
                    155:     if (tmpfile && tmpurl && t >= 0) {
                    156:        char * result;
                    157:        if (!(result = (char *) HT_MALLOC(strlen(tmpurl)+20)))
                    158:            HT_OUTOFMEM("HTTmpAnchor");
2.27      frystyk   159: #ifdef HAVE_LONG_TIME_T
2.23      frystyk   160:        sprintf(result, "%s.%ld.%d", tmpurl, t, offset++);
2.27      frystyk   161: #else
                    162:        sprintf(result, "%s.%d.%d", tmpurl, t, offset++);
                    163: #endif
2.34      frystyk   164:        HTTRACE(APP_TRACE, "Tmp Anchor.. With location `%s\'\n" _ result);
2.37      kahan     165:        htpa = HTAnchor_parent(HTAnchor_findAddress(result));
2.22      frystyk   166:        HT_FREE(result);
                    167:     }
                    168:     HT_FREE(tmpfile);
                    169:     HT_FREE(tmpurl);
2.37      kahan     170:     return htpa;
2.22      frystyk   171: }
                    172: 
                    173: /*
2.31      frystyk   174: **     Takes a string of the form "a=b" containing HTML form data, escapes
                    175: **     it accordingly and puts it into the association list so that it
                    176: **     readily can be passed to any of the HTAccess function that handles
                    177: **     HTML form data.
                    178: */
                    179: PUBLIC BOOL HTParseFormInput (HTAssocList * list, const char * str)
                    180: {
                    181:     if (list && str) {
                    182:        char * me = NULL;
                    183:        char * name = NULL;
                    184:        char * value = NULL;
                    185:        StrAllocCopy(me, str);
                    186:        value = strchr(me, '=');
                    187:        if (value) 
                    188:            *value++ = '\0';
                    189:        else
                    190:            value = "";
                    191:        name = HTStrip(me);
                    192: 
                    193:        /* Escape the name and value */
                    194:        if (name) {
                    195:            char * escaped_name = HTEscape(name, URL_XALPHAS);
                    196:            char * escaped_value = HTEscape(value, URL_XALPHAS);
2.34      frystyk   197:            HTTRACE(APP_TRACE, "Form data... Adding name `%s\' with value `%s\' to %p\n" _ 
                    198:                        escaped_name _ escaped_value _ list);
2.31      frystyk   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: {
2.35      frystyk   217: #if defined(HTDEBUG) && defined(WWWTRACE)
2.19      frystyk   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.36      frystyk   223:            case 'a': WWWTRACE |= SHOW_ANCHOR_TRACE;    break;
                    224:            case 'b': WWWTRACE |= SHOW_BIND_TRACE;      break;
                    225:            case 'c': WWWTRACE |= SHOW_CACHE_TRACE;     break;
                    226:            case 'e': WWWTRACE |= SHOW_MUX_TRACE;       break;
2.22      frystyk   227:            case 'f': WWWTRACE |= SHOW_UTIL_TRACE;      break;
2.36      frystyk   228:            case 'g': WWWTRACE |= SHOW_SGML_TRACE;      break;
                    229:            case 'h': WWWTRACE |= SHOW_AUTH_TRACE;      break;
                    230:            case 'i': WWWTRACE |= SHOW_PICS_TRACE;      break;
2.22      frystyk   231:            case 'l': WWWTRACE |= SHOW_APP_TRACE;       break;
2.36      frystyk   232:            case 'm': WWWTRACE |= SHOW_MEM_TRACE;       break;
                    233:            case 'o': WWWTRACE |= SHOW_CORE_TRACE;      break;
2.22      frystyk   234:            case 'p': WWWTRACE |= SHOW_PROTOCOL_TRACE;  break;
2.28      frystyk   235:            case 'q': WWWTRACE |= SHOW_SQL_TRACE;       break;
2.36      frystyk   236:            case 's': WWWTRACE |= SHOW_STREAM_TRACE;    break;
                    237:            case 't': WWWTRACE |= SHOW_THREAD_TRACE;    break;
2.22      frystyk   238:            case 'u': WWWTRACE |= SHOW_URI_TRACE;       break;
2.36      frystyk   239:            case 'x': WWWTRACE |= SHOW_XML_TRACE;       break;
2.31      frystyk   240:            case '*': WWWTRACE |= SHOW_ALL_TRACE;       break;
2.19      frystyk   241:            default:
2.34      frystyk   242:                if (WWWTRACE) HTTRACE(APP_TRACE, "Trace....... Bad argument\n");
2.19      frystyk   243:            }
                    244:        }
2.20      frystyk   245:        if (!WWWTRACE) WWWTRACE = SHOW_ALL_TRACE;
2.25      frystyk   246:     } else {
                    247:        WWWTRACE = SHOW_ALL_TRACE;
2.19      frystyk   248:     }
                    249:     return WWWTRACE;
                    250: #else
                    251:     return 0;
                    252: #endif
2.1       frystyk   253: }

Webmaster