Annotation of libwww/Library/src/HTHome.c, revision 2.8
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 {
2.7 frystyk 215: HTRequest_addGnHd(request, HT_G_NO_CACHE); /* No-cache pragma */
2.2 frystyk 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 */
2.6 frystyk 254: HTRequest_addGnHd(request, HT_G_NO_CACHE);
255: HTRequest_addRqHd(request, HT_C_IMS);
2.2 frystyk 256: }
257: } else if ((newaddr = HTProxy_find(addr))) {
258: StrAllocCat(newaddr, addr);
259: request->using_proxy = YES;
260: HTAnchor_setPhysical(anchor, newaddr);
261: } else if ((newaddr = HTGateway_find(addr))) {
262: char * path = HTParse(addr, "",
263: PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
264: /* Chop leading / off to make host into part of path */
265: char * gatewayed = HTParse(path+1, newaddr, PARSE_ALL);
266: HTAnchor_setPhysical(anchor, gatewayed);
267: free(path);
268: free(gatewayed);
269: } else {
270: request->using_proxy = NO; /* We don't use proxy or gateway */
271: }
272: FREE(newaddr);
273: }
274: FREE(addr);
2.4 frystyk 275: return HT_OK;
2.2 frystyk 276: }
277:
278: /* Application "AFTER" Callback
279: ** -----------------------------
280: ** This function uses all the functionaly that the app part of the Library
281: ** gives for handling AFTER a request.
282: */
283: PUBLIC int HTLoadTerminate (HTRequest * request, int status)
2.5 frystyk 284: {
285: char * uri = HTAnchor_address((HTAnchor*)request->anchor);
286: switch (status) {
287: case HT_LOADED:
288: if (PROT_TRACE)
289: TTYPrint(TDEST, "Load End.... OK: `%s\' has been accessed\n", uri);
290: break;
291:
292: case HT_NO_DATA:
293: if (PROT_TRACE)
294: TTYPrint(TDEST, "Load End.... OK BUT NO DATA: `%s\'\n", uri);
295: break;
296:
297: case HT_INTERRUPTED:
298: if (PROT_TRACE)
299: TTYPrint(TDEST, "Load End.... INTERRUPTED: `%s\'\n", uri);
300: break;
301:
302: case HT_RETRY:
303: if (PROT_TRACE)
304: TTYPrint(TDEST, "Load End.... NOT AVAILABLE, RETRY AT %ld\n",
305: HTRequest_retryTime(request));
306: break;
307:
308: case HT_ERROR:
309: {
310: HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
311: if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
312: request->error_stack, NULL);
313: }
314: if (PROT_TRACE)
2.8 ! frystyk 315: TTYPrint(TDEST, "Load End.... ERROR: Can't access `%s\'\n",
! 316: uri ? uri : "<UNKNOWN>");
2.5 frystyk 317: break;
318:
319: default:
320: if (PROT_TRACE)
321: TTYPrint(TDEST, "Load End.... UNKNOWN RETURN CODE %d\n", status);
322: break;
323: }
2.2 frystyk 324:
325: /* Should we do logging? */
326: if (HTLog_isOpen()) HTLog_add(request, status);
2.5 frystyk 327: free(uri);
2.2 frystyk 328: return HT_OK;
2.1 frystyk 329: }
Webmaster