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