Annotation of libwww/Library/src/HTAccess.c, revision 1.52
1.1 timbl 1: /* Access Manager HTAccess.c
2: ** ==============
3: **
4: ** Authors
5: ** TBL Tim Berners-Lee timbl@info.cern.ch
1.4 timbl 6: ** JFG Jean-Francois Groff jfg@dxcern.cern.ch
1.1 timbl 7: ** DD Denis DeLaRoca (310) 825-4580 <CSP1DWD@mvs.oac.ucla.edu>
8: ** History
9: ** 8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
10: ** 26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
1.42 frystyk 11: ** 6 Oct 92 Moved HTClientHost and HTlogfile into here. TBL
1.1 timbl 12: ** 17 Dec 92 Tn3270 added, bug fix. DD
1.2 timbl 13: ** 4 Feb 93 Access registration, Search escapes bad chars TBL
1.9 timbl 14: ** PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
15: ** 28 May 93 WAIS gateway explicit if no WAIS library linked in.
1.19 timbl 16: ** Dec 93 Bug change around, more reentrant, etc
1.42 frystyk 17: ** 09 May 94 logfile renamed to HTlogfile to avoid clash with WAIS
1.2 timbl 18: ** Bugs
19: ** This module assumes that that the graphic object is hypertext, as it
1.9 timbl 20: ** needs to select it when it has been loaded. A superclass needs to be
1.2 timbl 21: ** defined which accepts select and select_anchor.
1.1 timbl 22: */
23:
1.9 timbl 24: #ifndef DEFAULT_WAIS_GATEWAY
1.8 timbl 25: #define DEFAULT_WAIS_GATEWAY "http://info.cern.ch:8001/"
1.9 timbl 26: #endif
1.8 timbl 27:
1.1 timbl 28: /* Implements:
29: */
30: #include "HTAccess.h"
31:
32: /* Uses:
33: */
34:
35: #include "HTParse.h"
36: #include "HTUtils.h"
1.4 timbl 37: #include "HTML.h" /* SCW */
1.2 timbl 38:
39: #ifndef NO_RULES
40: #include "HTRules.h"
41: #endif
42:
1.1 timbl 43: #include <stdio.h>
44:
1.2 timbl 45: #include "HTList.h"
46: #include "HText.h" /* See bugs above */
47: #include "HTAlert.h"
1.17 timbl 48: #include "HTFWriter.h" /* for cache stuff */
49: #include "HTTee.h"
1.46 frystyk 50: #include "HTError.h"
1.2 timbl 51:
1.1 timbl 52: /* These flags may be set to modify the operation of this module
53: */
1.34 frystyk 54: PUBLIC char * HTCacheDir = 0; /* Root for cached files or 0 for no cache */
55: PUBLIC char * HTSaveLocallyDir = SAVE_LOCALLY_HOME_DIR; /* Save & exe files */
1.1 timbl 56: PUBLIC char * HTClientHost = 0; /* Name of remote login host if any */
1.42 frystyk 57: PUBLIC FILE * HTlogfile = 0; /* File to which to output one-liners */
1.41 luotonen 58:
1.34 frystyk 59: PUBLIC BOOL HTForceReload = NO; /* Force reload from cache or net */
1.12 timbl 60: PUBLIC BOOL HTSecure = NO; /* Disable access for telnet users? */
1.27 luotonen 61: PUBLIC BOOL using_proxy = NO; /* are we using a proxy gateway? */
1.43 luotonen 62: PUBLIC char * HTImServer = NULL;/* cern_httpd sets this to the translated URL*/
1.27 luotonen 63: PUBLIC BOOL HTImProxy = NO; /* cern_httpd as a proxy? */
1.1 timbl 64:
1.43 luotonen 65:
1.2 timbl 66: /* To generate other things, play with these:
67: */
68:
1.15 timbl 69: /* PUBLIC HTFormat HTOutputFormat = NULL; use request->output_format */
70: /* PUBLIC HTStream* HTOutputStream = NULL; use request->output_stream */
1.1 timbl 71:
72: PRIVATE HTList * protocols = NULL; /* List of registered protocol descriptors */
73:
1.24 timbl 74: /* Superclass defn */
1.1 timbl 75:
1.24 timbl 76: struct _HTStream {
77: HTStreamClass * isa;
78: /* ... */
79: };
80:
1.15 timbl 81: /* Create a request structure
82: ** ---------------------------
83: */
84:
85: PUBLIC HTRequest * HTRequest_new NOARGS
86: {
1.28 luotonen 87: HTRequest * me = (HTRequest*) calloc(1, sizeof(*me)); /* zero fill */
1.15 timbl 88: if (!me) outofmem(__FILE__, "HTRequest_new()");
89:
1.20 luotonen 90: me->conversions = HTList_new(); /* No conversions registerd yet */
91: me->output_format = WWW_PRESENT; /* default it to present to user */
92:
1.15 timbl 93: return me;
94: }
95:
96:
1.49 frystyk 97: /* Clear a request structure
98: ** ---------------------------
99: ** This function clears the reguest structure so that only the
100: ** conversions remain. Everything else is as if it was created from
101: ** scratch.
102: */
103: PUBLIC void HTRequest_clear ARGS1(HTRequest *, req)
104: {
105: HTList *conversions;
106: if (!req) {
107: if (TRACE)
108: fprintf(stderr, "Clear....... request: Bad argument!\n");
109: return;
110: }
111: conversions = req->conversions; /* Save the conversions */
112: HTErrorFree(req);
113: HTAACleanup(req);
114: FREE(req->from);
115: memset(req, '\0', sizeof(HTRequest));
116:
117: /* Now initialize as from scratch but with the old list of conversions */
118: req->conversions = conversions;
119: req->output_format = WWW_PRESENT; /* default it to present to user */
120: }
121:
122:
1.20 luotonen 123: /* Delete a request structure
124: ** --------------------------
125: */
126: PUBLIC void HTRequest_delete ARGS1(HTRequest *, req)
127: {
128: if (req) {
1.34 frystyk 129: HTFormatDelete(req->conversions);
1.46 frystyk 130: HTErrorFree(req);
1.34 frystyk 131: HTAACleanup(req);
1.37 luotonen 132: FREE(req->from);
1.34 frystyk 133: FREE(req);
1.20 luotonen 134: }
135: }
136:
137:
1.22 luotonen 138: PRIVATE char * method_names[(int)MAX_METHODS + 1] =
139: {
140: "INVALID-METHOD",
141: "GET",
142: "HEAD",
143: "POST",
144: "PUT",
145: "DELETE",
146: "CHECKOUT",
147: "CHECKIN",
148: "SHOWMETHOD",
149: "LINK",
150: "UNLINK",
151: NULL
152: };
153:
154: /* Get method enum value
155: ** ---------------------
156: */
157: PUBLIC HTMethod HTMethod_enum ARGS1(char *, name)
158: {
159: if (name) {
160: int i;
161: for (i=1; i < (int)MAX_METHODS; i++)
162: if (!strcmp(name, method_names[i]))
163: return (HTMethod)i;
164: }
165: return METHOD_INVALID;
166: }
167:
168:
169: /* Get method name
170: ** ---------------
171: */
172: PUBLIC char * HTMethod_name ARGS1(HTMethod, method)
173: {
174: if ((int)method > (int)METHOD_INVALID &&
175: (int)method < (int)MAX_METHODS)
176: return method_names[(int)method];
177: else
178: return method_names[(int)METHOD_INVALID];
179: }
180:
181:
182: /* Is method in a list of method names?
183: ** -----------------------------------
184: */
185: PUBLIC BOOL HTMethod_inList ARGS2(HTMethod, method,
186: HTList *, list)
187: {
188: char * method_name = HTMethod_name(method);
189: HTList *cur = list;
190: char *item;
191:
192: while (NULL != (item = (char*)HTList_nextObject(cur))) {
193: CTRACE(stderr, " %s", item);
194: if (0==strcasecomp(item, method_name))
195: return YES;
196: }
197: return NO; /* Not found */
198: }
199:
200:
201:
202:
1.20 luotonen 203:
1.1 timbl 204: /* Register a Protocol HTRegisterProtocol
205: ** -------------------
206: */
207:
208: PUBLIC BOOL HTRegisterProtocol(protocol)
209: HTProtocol * protocol;
210: {
211: if (!protocols) protocols = HTList_new();
212: HTList_addObject(protocols, protocol);
213: return YES;
214: }
215:
216:
217: /* Register all known protocols
218: ** ----------------------------
219: **
220: ** Add to or subtract from this list if you add or remove protocol modules.
221: ** This routine is called the first time the protocol list is needed,
1.52 ! frystyk 222: ** unless any protocols are already registered, in which case it is not
! 223: ** called. Therefore the application can override this list.
1.1 timbl 224: **
225: ** Compiling with NO_INIT prevents all known protocols from being forced
226: ** in at link time.
227: */
228: #ifndef NO_INIT
229: PRIVATE void HTAccessInit NOARGS /* Call me once */
230: {
1.14 duns 231: GLOBALREF HTProtocol HTTP, HTFile, HTTelnet, HTTn3270, HTRlogin;
1.1 timbl 232: #ifndef DECNET
1.14 duns 233: GLOBALREF HTProtocol HTFTP, HTNews, HTGopher;
1.42 frystyk 234:
235: /* This is the replacement when HTWhoIs gets a complete protocol module */
236: /* GLOBALREF HTProtocol HTFTP, HTNews, HTGopher, HTWhoIs; */
237: /* -------------------------------------------------------------------- */
238:
1.3 timbl 239: #ifdef DIRECT_WAIS
1.14 duns 240: GLOBALREF HTProtocol HTWAIS;
1.3 timbl 241: #endif
1.2 timbl 242: HTRegisterProtocol(&HTFTP);
243: HTRegisterProtocol(&HTNews);
244: HTRegisterProtocol(&HTGopher);
1.42 frystyk 245:
246: /* This should be added when HTWhoIs gets a complete protocol module */
247: /* HTRegisterProtocol(&HTWhoIs); */
248: /* ----------------------------------------------------------------- */
1.3 timbl 249: #ifdef DIRECT_WAIS
250: HTRegisterProtocol(&HTWAIS);
251: #endif
1.1 timbl 252: #endif
253:
1.2 timbl 254: HTRegisterProtocol(&HTTP);
255: HTRegisterProtocol(&HTFile);
256: HTRegisterProtocol(&HTTelnet);
257: HTRegisterProtocol(&HTTn3270);
258: HTRegisterProtocol(&HTRlogin);
1.1 timbl 259: }
260: #endif
261:
262:
1.33 luotonen 263:
264: /* override_proxy()
265: **
266: ** Check the no_proxy environment variable to get the list
267: ** of hosts for which proxy server is not consulted.
268: **
269: ** no_proxy is a comma- or space-separated list of machine
270: ** or domain names, with optional :port part. If no :port
271: ** part is present, it applies to all ports on that domain.
272: **
273: ** Example:
274: ** no_proxy="cern.ch,some.domain:8001"
275: **
276: */
277: PRIVATE BOOL override_proxy ARGS1(CONST char *, addr)
278: {
279: CONST char * no_proxy = getenv("no_proxy");
280: char * p = NULL;
281: char * host = NULL;
282: int port = 0;
283: int h_len = 0;
284:
285: if (!no_proxy || !addr || !(host = HTParse(addr, "", PARSE_HOST)))
286: return NO;
287: if (!*host) { free(host); return NO; }
288:
1.34 frystyk 289: if ((p = strchr(host, ':')) != NULL) { /* Port specified */
1.33 luotonen 290: *p++ = 0; /* Chop off port */
291: port = atoi(p);
292: }
293: else { /* Use default port */
294: char * access = HTParse(addr, "", PARSE_ACCESS);
295: if (access) {
296: if (!strcmp(access,"http")) port = 80;
297: else if (!strcmp(access,"gopher")) port = 70;
298: else if (!strcmp(access,"ftp")) port = 21;
299: free(access);
300: }
301: }
302: if (!port) port = 80; /* Default */
303: h_len = strlen(host);
304:
305: while (*no_proxy) {
306: CONST char * end;
307: CONST char * colon = NULL;
308: int templ_port = 0;
309: int t_len;
310:
311: while (*no_proxy && (WHITE(*no_proxy) || *no_proxy==','))
312: no_proxy++; /* Skip whitespace and separators */
313:
314: end = no_proxy;
315: while (*end && !WHITE(*end) && *end != ',') { /* Find separator */
316: if (*end==':') colon = end; /* Port number given */
317: end++;
318: }
319:
320: if (colon) {
321: templ_port = atoi(colon+1);
322: t_len = colon - no_proxy;
323: }
324: else {
325: t_len = end - no_proxy;
326: }
327:
328: if ((!templ_port || templ_port == port) &&
329: (t_len > 0 && t_len <= h_len &&
330: !strncmp(host + h_len - t_len, no_proxy, t_len))) {
331: free(host);
332: return YES;
333: }
334: if (*end) no_proxy = end+1;
335: else break;
336: }
337:
338: free(host);
339: return NO;
340: }
341:
342:
343:
1.2 timbl 344: /* Find physical name and access protocol
345: ** --------------------------------------
1.1 timbl 346: **
347: **
348: ** On entry,
349: ** addr must point to the fully qualified hypertext reference.
350: ** anchor a pareent anchor with whose address is addr
351: **
352: ** On exit,
1.2 timbl 353: ** returns HT_NO_ACCESS Error has occured.
354: ** HT_OK Success
1.1 timbl 355: **
356: */
1.21 luotonen 357: PRIVATE int get_physical ARGS1(HTRequest *, req)
358: {
1.1 timbl 359: char * access=0; /* Name of access method */
1.21 luotonen 360: char * addr = HTAnchor_address((HTAnchor*)req->anchor); /* free me */
1.27 luotonen 361:
1.35 luotonen 362: /*
363: ** This HACK is here until we have redirection implemented.
364: ** This is used when we are recursively calling HTLoad().
365: ** We then take the physical address, because currently the
366: ** virtual address is kept in a hash table so it can't be
367: ** changed -- otherwise it wouldn't be found anymore.
368: */
1.36 luotonen 369: if (HTAnchor_physical(req->anchor))
370: StrAllocCopy(addr, HTAnchor_physical(req->anchor));
1.35 luotonen 371:
1.2 timbl 372: #ifndef NO_RULES
1.47 luotonen 373: if (HTImServer) { /* cern_httpd has already done its own translations */
1.45 luotonen 374: HTAnchor_setPhysical(req->anchor, HTImServer);
1.47 luotonen 375: StrAllocCopy(addr, HTImServer); /* Oops, queries thru many proxies */
376: /* didn't work without this -- AL */
377: }
1.21 luotonen 378: else {
1.27 luotonen 379: char * physical = HTTranslate(addr);
1.21 luotonen 380: if (!physical) {
1.47 luotonen 381: free(addr);
1.21 luotonen 382: return HT_FORBIDDEN;
383: }
384: HTAnchor_setPhysical(req->anchor, physical);
385: free(physical); /* free our copy */
1.2 timbl 386: }
387: #else
1.21 luotonen 388: HTAnchor_setPhysical(req->anchor, addr);
1.2 timbl 389: #endif
390:
1.21 luotonen 391: access = HTParse(HTAnchor_physical(req->anchor),
1.27 luotonen 392: "file:", PARSE_ACCESS);
1.1 timbl 393:
394: /* Check whether gateway access has been set up for this
1.8 timbl 395: **
396: ** This function can be replaced by the rule system above.
1.1 timbl 397: */
1.8 timbl 398: #define USE_GATEWAYS
1.1 timbl 399: #ifdef USE_GATEWAYS
1.39 luotonen 400:
401: /* make sure the using_proxy variable is false */
402: using_proxy = NO;
403:
1.33 luotonen 404: if (!override_proxy(addr)) {
1.27 luotonen 405: char * gateway_parameter, *gateway, *proxy;
406:
1.2 timbl 407: gateway_parameter = (char *)malloc(strlen(access)+20);
408: if (gateway_parameter == NULL) outofmem(__FILE__, "HTLoad");
1.27 luotonen 409:
410: /* search for proxy gateways */
1.2 timbl 411: strcpy(gateway_parameter, "WWW_");
412: strcat(gateway_parameter, access);
413: strcat(gateway_parameter, "_GATEWAY");
414: gateway = (char *)getenv(gateway_parameter); /* coerce for decstation */
1.27 luotonen 415:
416: /* search for proxy servers */
417: strcpy(gateway_parameter, access);
418: strcat(gateway_parameter, "_proxy");
419: proxy = (char *)getenv(gateway_parameter);
420:
1.2 timbl 421: free(gateway_parameter);
1.27 luotonen 422:
423: if (TRACE && gateway)
424: fprintf(stderr,"Gateway found: %s\n",gateway);
425: if (TRACE && proxy)
426: fprintf(stderr,"Proxy server found: %s\n",proxy);
427:
1.8 timbl 428: #ifndef DIRECT_WAIS
1.9 timbl 429: if (!gateway && 0==strcmp(access, "wais")) {
1.8 timbl 430: gateway = DEFAULT_WAIS_GATEWAY;
431: }
432: #endif
1.27 luotonen 433:
434: /* proxy servers have precedence over gateway servers */
435: if (proxy) {
436: char * gatewayed=0;
437:
438: StrAllocCopy(gatewayed,proxy);
439: StrAllocCat(gatewayed,addr);
440: using_proxy = YES;
441: HTAnchor_setPhysical(req->anchor, gatewayed);
442: free(gatewayed);
443: free(access);
444:
445: access = HTParse(HTAnchor_physical(req->anchor),
446: "http:", PARSE_ACCESS);
447: } else if (gateway) {
1.9 timbl 448: char * path = HTParse(addr, "",
449: PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
450: /* Chop leading / off to make host into part of path */
451: char * gatewayed = HTParse(path+1, gateway, PARSE_ALL);
452: free(path);
1.21 luotonen 453: HTAnchor_setPhysical(req->anchor, gatewayed);
1.9 timbl 454: free(gatewayed);
1.2 timbl 455: free(access);
1.9 timbl 456:
1.21 luotonen 457: access = HTParse(HTAnchor_physical(req->anchor),
1.8 timbl 458: "http:", PARSE_ACCESS);
1.2 timbl 459: }
460: }
1.1 timbl 461: #endif
462:
1.19 timbl 463: free(addr);
1.1 timbl 464:
465:
466: /* Search registered protocols to find suitable one
467: */
468: {
1.20 luotonen 469: HTList *cur;
470: HTProtocol *p;
1.1 timbl 471: #ifndef NO_INIT
1.2 timbl 472: if (!protocols) HTAccessInit();
1.1 timbl 473: #endif
1.20 luotonen 474: cur = protocols;
475: while ((p = (HTProtocol*)HTList_nextObject(cur))) {
1.2 timbl 476: if (strcmp(p->name, access)==0) {
1.21 luotonen 477: HTAnchor_setProtocol(req->anchor, p);
1.2 timbl 478: free(access);
479: return (HT_OK);
1.1 timbl 480: }
481: }
482: }
483:
484: free(access);
1.2 timbl 485: return HT_NO_ACCESS;
1.1 timbl 486: }
487:
488:
489: /* Load a document
490: ** ---------------
491: **
1.2 timbl 492: ** This is an internal routine, which has an address AND a matching
493: ** anchor. (The public routines are called with one OR the other.)
494: **
495: ** On entry,
1.15 timbl 496: ** request->
1.35 luotonen 497: ** anchor a parent anchor with fully qualified
498: ** hypertext reference as its address set
1.15 timbl 499: ** output_format valid
500: ** output_stream valid on NULL
1.2 timbl 501: **
502: ** On exit,
503: ** returns <0 Error has occured.
504: ** HT_LOADED Success
505: ** HT_NO_DATA Success, but no document loaded.
1.8 timbl 506: ** (telnet sesssion started etc)
1.2 timbl 507: **
508: */
1.52 ! frystyk 509: PUBLIC int HTLoad ARGS2(HTRequest *, request, BOOL, keep_error_stack)
1.2 timbl 510: {
1.25 frystyk 511: char *arg = NULL;
512: HTProtocol *p;
513: int status;
514:
1.22 luotonen 515: if (request->method == METHOD_INVALID)
516: request->method = METHOD_GET;
1.52 ! frystyk 517: if (!keep_error_stack) {
! 518: HTErrorFree(request);
! 519: request->error_block = NO;
! 520: }
! 521:
1.21 luotonen 522: status = get_physical(request);
1.2 timbl 523: if (status == HT_FORBIDDEN) {
1.49 frystyk 524: char *url = HTAnchor_address((HTAnchor *) request->anchor);
525: if (url) {
526: HTUnEscape(url);
527: HTErrorAdd(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
528: (void *) url, (int) strlen(url), "HTLoad");
529: free(url);
530: } else {
531: HTErrorAdd(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
532: NULL, 0, "HTLoad");
533: }
534: return -1;
1.2 timbl 535: }
536: if (status < 0) return status; /* Can't resolve or forbidden */
1.25 frystyk 537:
538: if(!(arg = HTAnchor_physical(request->anchor)) || !*arg)
539: return (-1);
1.27 luotonen 540:
1.15 timbl 541: p = HTAnchor_protocol(request->anchor);
1.17 timbl 542: return (*(p->load))(request);
1.2 timbl 543: }
544:
545:
546: /* Get a save stream for a document
547: ** --------------------------------
548: */
1.19 timbl 549: PUBLIC HTStream *HTSaveStream ARGS1(HTRequest *, request)
1.15 timbl 550: {
551: HTProtocol * p;
1.19 timbl 552: int status;
1.22 luotonen 553: request->method = METHOD_PUT;
1.21 luotonen 554: status = get_physical(request);
1.19 timbl 555: if (status == HT_FORBIDDEN) {
1.49 frystyk 556: char *url = HTAnchor_address((HTAnchor *) request->anchor);
557: if (url) {
558: HTUnEscape(url);
559: HTErrorAdd(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
560: (void *) url, (int) strlen(url), "HTLoad");
561: free(url);
562: } else {
563: HTErrorAdd(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
564: NULL, 0, "HTLoad");
565: }
566: return NULL; /* should return error status? */
1.19 timbl 567: }
568: if (status < 0) return NULL; /* @@ error. Can't resolve or forbidden */
569:
1.15 timbl 570: p = HTAnchor_protocol(request->anchor);
1.2 timbl 571: if (!p) return NULL;
572:
1.15 timbl 573: return (*p->saveStream)(request);
1.2 timbl 574:
575: }
576:
577:
578: /* Load a document - with logging etc
579: ** ----------------------------------
580: **
581: ** - Checks or documents already loaded
582: ** - Logs the access
583: ** - Allows stdin filter option
584: ** - Trace ouput and error messages
585: **
1.1 timbl 586: ** On Entry,
1.19 timbl 587: ** request->anchor valid for of the document to be accessed.
588: ** request->childAnchor optional anchor within doc to be selected
589: **
1.2 timbl 590: ** filter if YES, treat stdin as HTML
1.1 timbl 591: **
1.15 timbl 592: ** request->anchor is the node_anchor for the document
593: ** request->output_format is valid
594: **
1.1 timbl 595: ** On Exit,
596: ** returns YES Success in opening document
597: ** NO Failure
598: **
599: */
600:
1.52 ! frystyk 601: PRIVATE BOOL HTLoadDocument ARGS2(HTRequest *, request,
! 602: BOOL, keep_error_stack)
1.1 timbl 603:
604: {
605: int status;
606: HText * text;
1.19 timbl 607: char * full_address = HTAnchor_address((HTAnchor*)request->anchor);
608:
1.49 frystyk 609: if (TRACE) fprintf (stderr, "HTAccess.... Loading document %s\n",
610: full_address);
1.1 timbl 611:
1.18 timbl 612: request->using_cache = NULL;
613:
1.15 timbl 614: if (!request->output_format) request->output_format = WWW_PRESENT;
1.25 frystyk 615:
1.31 frystyk 616: if (!HTForceReload && (text=(HText *)HTAnchor_document(request->anchor)))
1.15 timbl 617: { /* Already loaded */
1.1 timbl 618: if (TRACE) fprintf(stderr, "HTAccess: Document already in memory.\n");
1.19 timbl 619: if (request->childAnchor) {
620: HText_selectAnchor(text, request->childAnchor);
621: } else {
622: HText_select(text);
623: }
624: free(full_address);
1.1 timbl 625: return YES;
626: }
1.17 timbl 627:
1.34 frystyk 628: /* Check the Cache */
629: /* Caching is ONLY done if (char*) HTCacheDir is set. Henrik 09/03-94 */
1.17 timbl 630: /* Bug: for each format, we only check whether it is ok, we
631: don't check them all and chose the best */
1.38 timbl 632: if (/* HTCacheDir && */ request->anchor->cacheItems) {
1.17 timbl 633: HTList * list = request->anchor->cacheItems;
1.20 luotonen 634: HTList * cur = list;
635: HTCacheItem * item;
636:
637: while ((item = (HTCacheItem*)HTList_nextObject(cur))) {
1.18 timbl 638: HTStream * s;
639:
640: request->using_cache = item;
641:
1.37 luotonen 642: s = HTStreamStack(item->format, request, NO);
1.17 timbl 643: if (s) { /* format was suitable */
644: FILE * fp = fopen(item->filename, "r");
1.18 timbl 645: if (TRACE) fprintf(stderr, "Cache: HIT file %s for %s\n",
1.20 luotonen 646: item->filename,
647: full_address);
1.17 timbl 648: if (fp) {
649: HTFileCopy(fp, s);
1.24 timbl 650: (*s->isa->free)(s); /* close up pipeline */
1.17 timbl 651: fclose(fp);
1.19 timbl 652: free(full_address);
1.17 timbl 653: return YES;
654: } else {
655: fprintf(stderr, "***** Can't read cache file %s !\n",
1.20 luotonen 656: item->filename);
1.17 timbl 657: } /* file open ok */
658: } /* stream ok */
659: } /* next cache item */
660: } /* if cache available for this anchor */
1.1 timbl 661:
1.52 ! frystyk 662: status = HTLoad(request, keep_error_stack);
1.2 timbl 663:
1.1 timbl 664: /* Log the access if necessary
665: */
1.42 frystyk 666: if (HTlogfile) {
1.1 timbl 667: time_t theTime;
668: time(&theTime);
1.42 frystyk 669: fprintf(HTlogfile, "%24.24s %s %s %s\n",
1.1 timbl 670: ctime(&theTime),
671: HTClientHost ? HTClientHost : "local",
672: status<0 ? "FAIL" : "GET",
673: full_address);
1.42 frystyk 674: fflush(HTlogfile); /* Actually update it on disk */
1.1 timbl 675: if (TRACE) fprintf(stderr, "Log: %24.24s %s %s %s\n",
676: ctime(&theTime),
677: HTClientHost ? HTClientHost : "local",
678: status<0 ? "FAIL" : "GET",
679: full_address);
680: }
681:
1.52 ! frystyk 682: /* The error stack might contain general information to the client
! 683: about what has been going on in the library (not only errors) */
! 684: if (request->error_stack)
! 685: HTErrorMsg(request);
! 686:
1.1 timbl 687: if (status == HT_LOADED) {
688: if (TRACE) {
689: fprintf(stderr, "HTAccess: `%s' has been accessed.\n",
690: full_address);
691: }
1.19 timbl 692: free(full_address);
1.1 timbl 693: return YES;
694: }
695:
696: if (status == HT_NO_DATA) {
697: if (TRACE) {
698: fprintf(stderr,
699: "HTAccess: `%s' has been accessed, No data left.\n",
700: full_address);
701: }
1.19 timbl 702: free(full_address);
1.1 timbl 703: return NO;
704: }
705:
1.34 frystyk 706: /* Bug fix thanks to Lou Montulli. Henrik 10/03-94 */
707: if (status<=0) { /* Failure in accessing a document */
1.1 timbl 708: #ifdef CURSES
709: user_message("Can't access `%s'", full_address);
710: #else
1.5 timbl 711: if (TRACE) fprintf(stderr,
712: "HTAccess: Can't access `%s'\n", full_address);
1.1 timbl 713: #endif
1.19 timbl 714: free(full_address);
1.1 timbl 715: return NO;
716: }
1.9 timbl 717:
718: /* If you get this, then please find which routine is returning
719: a positive unrecognised error code! */
1.1 timbl 720: fprintf(stderr,
1.50 frystyk 721: "**** HTAccess: Internal software error in CERN WWWLib version %s ****\n\nPlease mail www-bug@info.cern.ch quoting what software and what version you are using\nand the URL: %s that caused the problem, thanks!\n",
722: HTLibraryVersion,
723: full_address);
1.19 timbl 724: free(full_address);
725:
1.1 timbl 726: exit(-6996);
1.20 luotonen 727: return NO; /* For gcc :-( */
1.2 timbl 728: } /* HTLoadDocument */
1.1 timbl 729:
730:
731:
732: /* Load a document from absolute name
733: ** ---------------
734: **
735: ** On Entry,
736: ** addr The absolute address of the document to be accessed.
737: ** filter if YES, treat document as HTML
738: **
739: ** On Exit,
740: ** returns YES Success in opening document
741: ** NO Failure
742: **
743: **
744: */
745:
1.15 timbl 746: PUBLIC BOOL HTLoadAbsolute ARGS2(CONST char *,addr, HTRequest*, request)
1.2 timbl 747: {
1.19 timbl 748: HTAnchor * anchor = HTAnchor_findAddress(addr);
749: request->anchor = HTAnchor_parent(anchor);
750: request->childAnchor = ((HTAnchor*)request->anchor == anchor) ?
751: NULL : (HTChildAnchor*) anchor;
1.52 ! frystyk 752: return HTLoadDocument(request, NO);
1.2 timbl 753: }
754:
755:
756: /* Load a document from absolute name to stream
757: ** --------------------------------------------
758: **
759: ** On Entry,
760: ** addr The absolute address of the document to be accessed.
1.15 timbl 761: ** request->output_stream if non-NULL, send data down this stream
1.2 timbl 762: **
763: ** On Exit,
764: ** returns YES Success in opening document
765: ** NO Failure
766: **
767: **
768: */
769:
770: PUBLIC BOOL HTLoadToStream ARGS3(
771: CONST char *, addr,
772: BOOL, filter,
1.15 timbl 773: HTRequest*, request)
1.1 timbl 774: {
1.19 timbl 775: HTAnchor * anchor = HTAnchor_findAddress(addr);
776: request->anchor = HTAnchor_parent(anchor);
777: request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL :
778: (HTChildAnchor*) anchor;
1.15 timbl 779: request->output_stream = request->output_stream;
1.52 ! frystyk 780: return HTLoadDocument(request, NO);
1.1 timbl 781: }
782:
783:
1.2 timbl 784:
785:
1.1 timbl 786: /* Load a document from relative name
787: ** ---------------
788: **
789: ** On Entry,
1.2 timbl 790: ** relative_name The relative address of the document
791: ** to be accessed.
1.1 timbl 792: **
793: ** On Exit,
794: ** returns YES Success in opening document
795: ** NO Failure
796: **
797: **
798: */
799:
1.15 timbl 800: PUBLIC BOOL HTLoadRelative ARGS3(
1.2 timbl 801: CONST char *, relative_name,
1.15 timbl 802: HTParentAnchor *, here,
1.20 luotonen 803: HTRequest *, request)
1.1 timbl 804: {
805: char * full_address = 0;
806: BOOL result;
807: char * mycopy = 0;
808: char * stripped = 0;
809: char * current_address =
1.2 timbl 810: HTAnchor_address((HTAnchor*)here);
1.1 timbl 811:
812: StrAllocCopy(mycopy, relative_name);
813:
814: stripped = HTStrip(mycopy);
815: full_address = HTParse(stripped,
816: current_address,
817: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.15 timbl 818: result = HTLoadAbsolute(full_address, request);
1.1 timbl 819: free(full_address);
820: free(current_address);
821: free(mycopy); /* Memory leak fixed 10/7/92 -- JFG */
822: return result;
823: }
824:
825:
826: /* Load if necessary, and select an anchor
827: ** --------------------------------------
828: **
829: ** On Entry,
830: ** destination The child or parenet anchor to be loaded.
831: **
832: ** On Exit,
833: ** returns YES Success
834: ** NO Failure
835: **
836: */
837:
1.15 timbl 838: PUBLIC BOOL HTLoadAnchor ARGS2(HTAnchor*, anchor, HTRequest *, request)
1.1 timbl 839: {
1.15 timbl 840: if (!anchor) return NO; /* No link */
1.1 timbl 841:
1.15 timbl 842: request->anchor = HTAnchor_parent(anchor);
1.19 timbl 843: request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL
844: : (HTChildAnchor*) anchor;
1.1 timbl 845:
1.52 ! frystyk 846: return HTLoadDocument(request, NO) ? YES : NO;
! 847:
! 848: } /* HTLoadAnchor */
! 849:
! 850:
! 851: /* Load if necessary, and select an anchor
! 852: ** --------------------------------------
! 853: **
! 854: ** This function is almost identical to HTLoadAnchor, but it doesn't
! 855: ** clear the error stack so that the information in there is kept.
! 856: **
! 857: ** On Entry,
! 858: ** destination The child or parenet anchor to be loaded.
! 859: **
! 860: ** On Exit,
! 861: ** returns YES Success
! 862: ** NO Failure
! 863: **
! 864: */
! 865:
! 866: PUBLIC BOOL HTLoadAnchorRecursive ARGS2(HTAnchor*, anchor,
! 867: HTRequest *, request)
! 868: {
! 869: if (!anchor) return NO; /* No link */
! 870:
! 871: request->anchor = HTAnchor_parent(anchor);
! 872: request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL
! 873: : (HTChildAnchor*) anchor;
! 874:
! 875: return HTLoadDocument(request, YES) ? YES : NO;
1.1 timbl 876:
877: } /* HTLoadAnchor */
878:
879:
880: /* Search
881: ** ------
882: ** Performs a keyword search on word given by the user. Adds the keyword to
883: ** the end of the current address and attempts to open the new address.
884: **
885: ** On Entry,
886: ** *keywords space-separated keyword list or similar search list
1.2 timbl 887: ** here is anchor search is to be done on.
1.1 timbl 888: */
889:
1.2 timbl 890: PRIVATE char hex(i)
891: int i;
892: {
1.13 timbl 893: char * hexchars = "0123456789ABCDEF";
894: return hexchars[i];
1.2 timbl 895: }
1.1 timbl 896:
1.15 timbl 897: PUBLIC BOOL HTSearch ARGS3(
1.2 timbl 898: CONST char *, keywords,
1.15 timbl 899: HTParentAnchor *, here,
900: HTRequest *, request)
1.1 timbl 901: {
1.2 timbl 902:
903: #define acceptable \
904: "1234567890abcdefghijlkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"
905:
906: char *q, *u;
907: CONST char * p, *s, *e; /* Pointers into keywords */
908: char * address = HTAnchor_address((HTAnchor*)here);
1.1 timbl 909: BOOL result;
1.2 timbl 910: char * escaped = malloc(strlen(keywords)*3+1);
911:
1.29 frystyk 912: /* static CONST BOOL isAcceptable[96] = */
913: /* static AND const is not good for a gnu compiler! Frystyk 25/02-94 */
1.30 luotonen 914: static BOOL isAcceptable[96] =
1.2 timbl 915: /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
916: { 0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0, /* 2x !"#$%&'()*+,-./ */
917: 1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, /* 3x 0123456789:;<=>? */
918: 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4x @ABCDEFGHIJKLMNO */
919: 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1, /* 5X PQRSTUVWXYZ[\]^_ */
920: 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6x `abcdefghijklmno */
921: 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0 }; /* 7X pqrstuvwxyz{\}~ DEL */
922:
923: if (escaped == NULL) outofmem(__FILE__, "HTSearch");
924:
1.29 frystyk 925: /* Convert spaces to + and hex escape unacceptable characters */
1.2 timbl 926:
1.29 frystyk 927: for(s=keywords; *s && WHITE(*s); s++); /*scan */ /* Skip white space */
928: for(e = s + strlen(s); e>s && WHITE(*(e-1)) ; e--); /* Skip trailers */
929: for(q=escaped, p=s; p<e; p++) { /* scan stripped field */
1.2 timbl 930: int c = (int)TOASCII(*p);
931: if (WHITE(*p)) {
932: *q++ = '+';
1.29 frystyk 933: } else if (c>=32 && c<=127 && isAcceptable[c-32] != 0) {
1.13 timbl 934: *q++ = *p; /* 930706 TBL for MVS bug */
1.2 timbl 935: } else {
936: *q++ = '%';
937: *q++ = hex(c / 16);
938: *q++ = hex(c % 16);
939: }
940: } /* Loop over string */
1.1 timbl 941:
1.2 timbl 942: *q=0;
943: /* terminate escaped sctring */
944: u=strchr(address, '?'); /* Find old search string */
945: if (u) *u = 0; /* Chop old search off */
1.1 timbl 946:
947: StrAllocCat(address, "?");
1.2 timbl 948: StrAllocCat(address, escaped);
949: free(escaped);
1.15 timbl 950: result = HTLoadRelative(address, here, request);
1.1 timbl 951: free(address);
1.2 timbl 952:
1.1 timbl 953: return result;
1.2 timbl 954: }
955:
956:
957: /* Search Given Indexname
958: ** ------
959: ** Performs a keyword search on word given by the user. Adds the keyword to
960: ** the end of the current address and attempts to open the new address.
961: **
962: ** On Entry,
963: ** *keywords space-separated keyword list or similar search list
964: ** *addres is name of object search is to be done on.
965: */
966:
1.15 timbl 967: PUBLIC BOOL HTSearchAbsolute ARGS3(
1.2 timbl 968: CONST char *, keywords,
1.15 timbl 969: CONST char *, indexname,
970: HTRequest *, request)
1.2 timbl 971: {
972: HTParentAnchor * anchor =
973: (HTParentAnchor*) HTAnchor_findAddress(indexname);
1.15 timbl 974: return HTSearch(keywords, anchor, request);
1.2 timbl 975: }
976:
977:
978: /* Generate the anchor for the home page
979: ** -------------------------------------
980: **
981: ** As it involves file access, this should only be done once
982: ** when the program first runs.
1.10 timbl 983: ** This is a default algorithm -- browser don't HAVE to use this.
984: ** But consistency betwen browsers is STRONGLY recommended!
1.2 timbl 985: **
1.10 timbl 986: ** Priority order is:
987: **
988: ** 1 WWW_HOME environment variable (logical name, etc)
989: ** 2 ~/WWW/default.html
990: ** 3 /usr/local/bin/default.html
991: ** 4 http://info.cern.ch/default.html
992: **
1.2 timbl 993: */
994: PUBLIC HTParentAnchor * HTHomeAnchor NOARGS
995: {
1.12 timbl 996: char * my_home_document = NULL;
997: char * home = (char *)getenv(LOGICAL_DEFAULT);
1.2 timbl 998: char * ref;
999: HTParentAnchor * anchor;
1.1 timbl 1000:
1.12 timbl 1001: if (home) {
1002: StrAllocCopy(my_home_document, home);
1003:
1004: /* Someone telnets in, they get a special home.
1005: */
1006: #define MAX_FILE_NAME 1024 /* @@@ */
1007: } else if (HTClientHost) { /* Telnet server */
1008: FILE * fp = fopen(REMOTE_POINTER, "r");
1009: char * status;
1010: if (fp) {
1011: my_home_document = (char*) malloc(MAX_FILE_NAME);
1012: status = fgets(my_home_document, MAX_FILE_NAME, fp);
1013: if (!status) {
1014: free(my_home_document);
1015: my_home_document = NULL;
1016: }
1017: fclose(fp);
1018: }
1019: if (!my_home_document) StrAllocCopy(my_home_document, REMOTE_ADDRESS);
1020: }
1021:
1022:
1023:
1.2 timbl 1024: #ifdef unix
1.12 timbl 1025:
1.10 timbl 1026: if (!my_home_document) {
1027: FILE * fp = NULL;
1028: CONST char * home = (CONST char*)getenv("HOME");
1029: if (home) {
1030: my_home_document = (char *)malloc(
1031: strlen(home)+1+ strlen(PERSONAL_DEFAULT)+1);
1032: if (my_home_document == NULL) outofmem(__FILE__, "HTLocalName");
1033: sprintf(my_home_document, "%s/%s", home, PERSONAL_DEFAULT);
1034: fp = fopen(my_home_document, "r");
1035: }
1036:
1037: if (!fp) {
1038: StrAllocCopy(my_home_document, LOCAL_DEFAULT_FILE);
1039: fp = fopen(my_home_document, "r");
1040: }
1.2 timbl 1041: if (fp) {
1042: fclose(fp);
1043: } else {
1044: if (TRACE) fprintf(stderr,
1.10 timbl 1045: "HTBrowse: No local home document ~/%s or %s\n",
1046: PERSONAL_DEFAULT, LOCAL_DEFAULT_FILE);
1.11 timbl 1047: free(my_home_document);
1048: my_home_document = NULL;
1.2 timbl 1049: }
1050: }
1051: #endif
1.10 timbl 1052: ref = HTParse( my_home_document ? my_home_document :
1053: HTClientHost ? REMOTE_ADDRESS
1054: : LAST_RESORT,
1055: "file:",
1.2 timbl 1056: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.10 timbl 1057: if (my_home_document) {
1.2 timbl 1058: if (TRACE) fprintf(stderr,
1059: "HTAccess: Using custom home page %s i.e. address %s\n",
1.10 timbl 1060: my_home_document, ref);
1061: free(my_home_document);
1.2 timbl 1062: }
1063: anchor = (HTParentAnchor*) HTAnchor_findAddress(ref);
1064: free(ref);
1065: return anchor;
1.1 timbl 1066: }
1.26 frystyk 1067:
1068:
1069: /* Bind an Anchor to the request structure
1070: ** ---------------------------------------
1071: **
1072: ** On Entry,
1073: ** anchor The child or parenet anchor to be binded
1074: ** request The request sturcture
1075: ** On Exit,
1076: ** returns YES Success
1077: ** NO Failure
1078: **
1079: ** Note: Actually the same as HTLoadAnchor() but DOES NOT do the loading
1080: ** Henrik Frystyk 17/02-94
1081: */
1082:
1083: PUBLIC BOOL HTBindAnchor ARGS2(HTAnchor*, anchor, HTRequest *, request)
1084: {
1085: if (!anchor) return NO; /* No link */
1086:
1087: request->anchor = HTAnchor_parent(anchor);
1088: request->childAnchor = ((HTAnchor*)request->anchor == anchor) ? NULL
1089: : (HTChildAnchor*) anchor;
1090:
1.29 frystyk 1091: return YES;
1.26 frystyk 1092: } /* HTBindAnchor */
1093:
Webmaster