Annotation of libwww/Library/src/HTAccess.c, revision 1.101
1.61 frystyk 1: /* HTAccess.c
2: ** ACCESS MANAGER
3: **
1.75 frystyk 4: ** (c) COPYRIGHT MIT 1995.
1.61 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
1.1 timbl 6: **
7: ** Authors
1.79 frystyk 8: ** TBL Tim Berners-Lee timbl@w3.org
1.4 timbl 9: ** JFG Jean-Francois Groff jfg@dxcern.cern.ch
1.1 timbl 10: ** DD Denis DeLaRoca (310) 825-4580 <CSP1DWD@mvs.oac.ucla.edu>
11: ** History
12: ** 8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
13: ** 26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
1.42 frystyk 14: ** 6 Oct 92 Moved HTClientHost and HTlogfile into here. TBL
1.1 timbl 15: ** 17 Dec 92 Tn3270 added, bug fix. DD
1.2 timbl 16: ** 4 Feb 93 Access registration, Search escapes bad chars TBL
1.9 timbl 17: ** PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
18: ** 28 May 93 WAIS gateway explicit if no WAIS library linked in.
1.19 timbl 19: ** Dec 93 Bug change around, more reentrant, etc
1.42 frystyk 20: ** 09 May 94 logfile renamed to HTlogfile to avoid clash with WAIS
1.53 duns 21: ** 8 Jul 94 Insulate free() from _free structure element.
1.88 frystyk 22: ** Sep 95 Rewritten, HFN
1.1 timbl 23: */
24:
1.68 frystyk 25: #if !defined(HT_DIRECT_WAIS) && !defined(HT_DEFAULT_WAIS_GATEWAY)
26: #define HT_DEFAULT_WAIS_GATEWAY "http://www.w3.org:8001/"
1.54 frystyk 27: #endif
1.8 timbl 28:
1.67 frystyk 29: /* Library include files */
1.88 frystyk 30: #include "WWWLib.h"
1.93 frystyk 31: #include "HTReqMan.h"
32: #include "HTAccess.h" /* Implemented here */
1.88 frystyk 33:
1.99 frystyk 34: #ifndef VC
35: #define VC "unknown"
36: #endif
37:
1.93 frystyk 38: PRIVATE char * HTAppName = NULL; /* Application name: please supply */
39: PRIVATE char * HTAppVersion = NULL; /* Application version: please supply */
1.2 timbl 40:
1.99 frystyk 41: PRIVATE char * HTLibName = "libwww";
42: PRIVATE char * HTLibVersion = VC;
43:
44: PRIVATE BOOL HTSecure = NO; /* Can we access local file system? */
45:
1.59 frystyk 46: /* --------------------------------------------------------------------------*/
1.61 frystyk 47: /* Initialization and Termination of the Library */
48: /* --------------------------------------------------------------------------*/
49:
1.99 frystyk 50: /* Information about the Application
51: ** ---------------------------------
1.93 frystyk 52: */
53: PUBLIC CONST char * HTLib_appName (void)
54: {
1.99 frystyk 55: return HTAppName ? HTAppName : "UNKNOWN";
56: }
57:
58: PUBLIC CONST char * HTLib_appVersion (void)
59: {
60: return HTAppVersion ? HTAppVersion : "0.0";
1.93 frystyk 61: }
62:
1.99 frystyk 63: /* Information about libwww
64: ** ------------------------
65: */
66: PUBLIC CONST char * HTLib_name (void)
67: {
68: return HTLibName ? HTLibName : "UNKNOWN";
69: }
70:
71: PUBLIC CONST char * HTLib_version (void)
72: {
73: return HTLibVersion ? HTLibVersion : "0.0";
74: }
75:
76: /* Access Local File System
77: ** ------------------------
78: ** In this mode we do not tough the local file system at all
1.93 frystyk 79: */
1.99 frystyk 80: PUBLIC BOOL HTLib_secure (void)
81: {
82: return HTSecure;
83: }
84:
85: PUBLIC void HTLib_setSecure (BOOL mode)
1.93 frystyk 86: {
1.99 frystyk 87: HTSecure = mode;
1.93 frystyk 88: }
89:
1.61 frystyk 90: /* HTLibInit
91: **
92: ** This function initiates the Library and it MUST be called when
93: ** starting up an application. See also HTLibTerminate()
94: */
1.93 frystyk 95: PUBLIC BOOL HTLibInit (CONST char * AppName, CONST char * AppVersion)
1.61 frystyk 96: {
1.97 frystyk 97: #if WWWTRACE_MODE == WWWTRACE_FILE /* Open trace file */
1.67 frystyk 98: if ((TDEST = fopen(TRACE_FILE, "a")) != NULL) {
99: if (setvbuf(TDEST, NULL, _IOLBF, 0) < 0) { /* Change to line buffer */
1.97 frystyk 100: TTYPrint(TDEST, "WWWLibInit.. Can't initialize TRACE buffer - no TRACE\n");
1.67 frystyk 101: fclose(TDEST);
102: TDEST = NULL;
103: WWW_TraceFlag = 0;
104: }
105: } else
106: WWW_TraceFlag = 0;
1.97 frystyk 107: #endif /* WWWTRACE_FILE */
1.67 frystyk 108:
1.91 frystyk 109: if (WWWTRACE)
1.97 frystyk 110: TTYPrint(TDEST, "WWWLibInit.. INITIALIZING LIBRARY OF COMMON CODE\n");
1.63 frystyk 111:
1.93 frystyk 112: /* Set the application name and version */
113: if (AppName) {
114: char *ptr;
115: StrAllocCopy(HTAppName, AppName);
116: ptr = HTAppName;
117: while (*ptr) {
118: if (WHITE(*ptr)) *ptr = '_';
119: ptr++;
120: }
121: }
122: if (AppVersion) {
123: char *ptr;
124: StrAllocCopy(HTAppVersion, AppVersion);
125: ptr = HTAppVersion;
126: while (*ptr) {
127: if (WHITE(*ptr)) *ptr = '_';
128: ptr++;
129: }
130: }
131:
132: HTBind_init(); /* Initialize bindings */
1.61 frystyk 133:
1.88 frystyk 134: /* Register a call back function for the Net Manager */
1.101 ! frystyk 135: HTNetCall_addAfter(HTLoad_terminate, HT_ALL);
1.88 frystyk 136:
1.62 frystyk 137: #ifdef WWWLIB_SIG
1.61 frystyk 138: /* On Solaris (and others?) we get a BROKEN PIPE signal when connecting
1.67 frystyk 139: ** to a port where we should get `connection refused'. We ignore this
1.61 frystyk 140: ** using the following function call
141: */
142: HTSetSignal(); /* Set signals in library */
1.1 timbl 143: #endif
144:
1.67 frystyk 145: #ifdef _WINDOWS
146: /*
147: ** Initialise WinSock DLL. This must also be shut down! PMH
148: */
149: {
150: WSADATA wsadata;
151: if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
1.91 frystyk 152: if (WWWTRACE)
1.97 frystyk 153: TTYPrint(TDEST, "WWWLibInit.. Can't initialize WinSoc\n");
1.67 frystyk 154: WSACleanup();
155: return NO;
156: }
157: if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
1.91 frystyk 158: if (WWWTRACE)
1.97 frystyk 159: TTYPrint(TDEST, "WWWLibInit.. Bad version of WinSoc\n");
1.67 frystyk 160: WSACleanup();
161: return NO;
162: }
163: }
164: #endif /* _WINDOWS */
165:
1.71 frystyk 166: #ifndef NO_TIMEGM
167: HTGetTimeZoneOffset(); /* Find offset from GMT if using mktime() */
168: #endif
1.70 frystyk 169: HTTmp_setRoot(NULL); /* Set up default tmp directory */
1.61 frystyk 170: return YES;
171: }
172:
173:
1.90 frystyk 174: /* HTLibTerminate
175: ** --------------
1.61 frystyk 176: ** This function frees memory kept by the Library and should be called
1.63 frystyk 177: ** before exit of an application (if you are on a PC platform)
1.61 frystyk 178: */
1.101 ! frystyk 179: PUBLIC BOOL HTLibTerminate (void)
1.61 frystyk 180: {
1.91 frystyk 181: if (WWWTRACE)
1.97 frystyk 182: TTYPrint(TDEST, "WWWLibTerm.. Cleaning up LIBRARY OF COMMON CODE\n");
1.101 ! frystyk 183: HTAtom_deleteAll(); /* Remove the atoms */
! 184: HTDNS_deleteAll(); /* Remove the DNS host cache */
! 185: HTAnchor_deleteAll(NULL); /* Delete anchors and drop hyperdocs */
1.73 frystyk 186:
1.81 frystyk 187: HTProtocol_deleteAll(); /* Remove bindings between access and protocols */
1.73 frystyk 188: HTBind_deleteAll(); /* Remove bindings between suffixes, media types */
189:
1.77 frystyk 190: HTFreeHostName(); /* Free up some internal strings */
1.63 frystyk 191: HTFreeMailAddress();
1.70 frystyk 192: HTTmp_freeRoot();
1.98 frystyk 193: HTError_freePrefix();
1.67 frystyk 194:
195: #ifdef _WINDOWS
196: WSACleanup();
197: #endif
198:
1.97 frystyk 199: #if WWWTRACE_MODE == WWWTRACE_FILE /* Close trace file */
1.67 frystyk 200: if (TDEST) {
201: fclose(TDEST);
202: TDEST = NULL;
203: WWW_TraceFlag = 0;
204: }
1.97 frystyk 205: #endif /* WWWTRACE_FILE */
1.61 frystyk 206: return YES;
207: }
208:
1.59 frystyk 209: /* --------------------------------------------------------------------------*/
1.88 frystyk 210: /* Access functions */
1.59 frystyk 211: /* --------------------------------------------------------------------------*/
1.33 luotonen 212:
1.90 frystyk 213: /* Request a document
214: ** -----------------
215: ** Private version that requests a document from the request manager
216: ** Returns YES if request accepted, else NO
1.88 frystyk 217: */
1.101 ! frystyk 218: PRIVATE BOOL HTLoadDocument (HTRequest * request, BOOL recursive)
1.88 frystyk 219: {
220: if (PROT_TRACE) {
1.90 frystyk 221: HTParentAnchor *anchor = HTRequest_anchor(request);
222: char * full_address = HTAnchor_address((HTAnchor *) anchor);
1.97 frystyk 223: TTYPrint(TDEST, "HTAccess.... Accessing document %s\n", full_address);
1.88 frystyk 224: free(full_address);
225: }
1.96 frystyk 226: return HTLoad(request, recursive);
1.58 frystyk 227: }
1.1 timbl 228:
229:
1.90 frystyk 230: /* Request a document from absolute name
231: ** -------------------------------------
232: ** Request a document referencd by an absolute URL.
233: ** Returns YES if request accepted, else NO
234: */
235: PUBLIC BOOL HTLoadAbsolute (CONST char * url, HTRequest* request)
236: {
237: if (url && request) {
238: HTAnchor * anchor = HTAnchor_findAddress(url);
239: HTRequest_setAnchor(request, anchor);
240: return HTLoadDocument(request, NO);
241: }
242: return NO;
243: }
244:
245:
246: /* Request a document from absolute name to stream
247: ** -----------------------------------------------
248: ** Request a document referencd by an absolute URL and sending the data
249: ** down a stream. This is _excactly_ the same as HTLoadAbsolute as
250: ** the ourputstream is specified using the function
251: ** HTRequest_setOutputStream(). 'filter' is ignored!
252: ** Returns YES if request accepted, else NO
253: */
254: PUBLIC BOOL HTLoadToStream (CONST char * url, BOOL filter, HTRequest *request)
255: {
256: return HTLoadAbsolute(url, request);
257: }
258:
259:
260: /* Request a document from relative name
261: ** -------------------------------------
262: ** Request a document referenced by a relative URL. The relative URL is
263: ** made absolute by resolving it relative to the address of the 'base'
264: ** anchor.
265: ** Returns YES if request accepted, else NO
266: */
267: PUBLIC BOOL HTLoadRelative (CONST char * relative,
268: HTParentAnchor * base,
269: HTRequest * request)
270: {
271: BOOL status = NO;
272: if (relative && base && request) {
273: char * rel = NULL;
274: char * full_url = NULL;
275: char * base_url = HTAnchor_address((HTAnchor *) base);
276: StrAllocCopy(rel, relative);
277: full_url = HTParse(HTStrip(rel), base_url,
278: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
279: status = HTLoadAbsolute(full_url, request);
280: free(rel);
281: free(full_url);
282: free(base_url);
283: }
284: return status;
285: }
286:
287:
288: /* Request an anchor
289: ** -----------------
290: ** Request the document referenced by the anchor
291: ** Returns YES if request accepted, else NO
292: */
293: PUBLIC BOOL HTLoadAnchor (HTAnchor * anchor, HTRequest * request)
294: {
295: if (anchor && request) {
296: HTRequest_setAnchor(request, anchor);
297: return HTLoadDocument(request, NO);
298: }
299: return NO;
300: }
301:
302:
303: /* Request an anchor
304: ** -----------------
305: ** Same as HTLoadAnchor but any information in the Error Stack in the
306: ** request object is kept, so that any error messages in one
1.52 frystyk 307: ** This function is almost identical to HTLoadAnchor, but it doesn't
308: ** clear the error stack so that the information in there is kept.
1.90 frystyk 309: ** Returns YES if request accepted, else NO
310: */
311: PUBLIC BOOL HTLoadAnchorRecursive (HTAnchor * anchor, HTRequest * request)
312: {
313: if (anchor && request) {
314: HTRequest_setAnchor(request, anchor);
315: return HTLoadDocument(request, YES);
316: }
317: return NO;
318: }
319:
320:
321: /* Search an Anchor
322: ** ----------------
323: ** Performs a keyword search on word given by the user. Adds the keyword
324: ** to the end of the current address and attempts to open the new address.
325: ** The list of keywords must be a space-separated list and spaces will
326: ** be converted to '+' before the request is issued.
327: ** Search can also be performed by HTLoadAbsolute() etc.
328: ** Returns YES if request accepted, else NO
329: */
330: PUBLIC BOOL HTSearch (CONST char * keywords,
331: HTParentAnchor * base,
332: HTRequest * request)
333: {
1.99 frystyk 334: BOOL status = NO;
1.90 frystyk 335: if (keywords && base && request) {
336: char *base_url = HTAnchor_address((HTAnchor *) base);
337: if (*keywords) {
338: char *plus;
339: StrAllocCat(base_url, "?");
340: StrAllocCat(base_url, keywords);
341: plus = strchr(base_url, '?');
342: while (*plus) {
343: if (*plus == ' ') *plus = '+';
344: plus++;
345: }
1.2 timbl 346: }
1.99 frystyk 347: status = HTLoadAbsolute(base_url, request);
348: free(base_url);
1.90 frystyk 349: }
1.99 frystyk 350: return status;
1.2 timbl 351: }
352:
353:
1.90 frystyk 354: /* Search a document from absolute name
355: ** ------------------------------------
356: ** Request a document referencd by an absolute URL appended with the
357: ** keywords given. The URL can NOT contain any fragment identifier!
358: ** The list of keywords must be a space-separated list and spaces will
359: ** be converted to '+' before the request is issued.
360: ** Returns YES if request accepted, else NO
361: */
362: PUBLIC BOOL HTSearchAbsolute (CONST char * keywords,
363: CONST char * url,
364: HTRequest * request)
365: {
366: if (url && request) {
367: HTAnchor * anchor = HTAnchor_findAddress(url);
368: return HTSearch(keywords, HTAnchor_parent(anchor), request);
369: }
370: return NO;
1.57 howcome 371: }
372:
1.70 frystyk 373: /* --------------------------------------------------------------------------*/
374: /* Document Poster */
375: /* --------------------------------------------------------------------------*/
376:
1.90 frystyk 377: /* Copy an anchor
1.70 frystyk 378: ** --------------
1.90 frystyk 379: ** Fetch the URL (possibly local file URL) and send it using either PUT
380: ** or POST to the remote destination using HTTP. The caller can decide the
381: ** exact method used and which HTTP header fields to transmit by setting
382: ** the user fields in the request structure.
1.92 frystyk 383: ** If posting to NNTP then we can't dispatch at this level but must pass
384: ** the source anchor to the news module that then takes all the refs
385: ** to NNTP and puts into the "newsgroups" header
1.90 frystyk 386: ** Returns YES if request accepted, else NO
1.70 frystyk 387: */
1.90 frystyk 388: PUBLIC BOOL HTCopyAnchor (HTAnchor * src_anchor, HTRequest * main_req)
1.80 frystyk 389: {
1.78 frystyk 390: HTRequest *src_req;
1.85 frystyk 391: if (!src_anchor || !main_req)
1.90 frystyk 392: return NO;
1.70 frystyk 393:
1.80 frystyk 394: /* Build the POST web if not already there */
1.85 frystyk 395: if (!main_req->source) {
1.80 frystyk 396: src_req = HTRequest_new(); /* First set up the source */
397: HTAnchor_clearHeader((HTParentAnchor *) src_anchor);
1.85 frystyk 398: src_req->reload = HT_MEM_REFRESH;
1.80 frystyk 399: src_req->source = src_req; /* Point to myself */
400: src_req->output_format = WWW_SOURCE; /* We want source (for now) */
401:
402: /* Set up the main link in the source anchor */
403: {
1.85 frystyk 404: HTLink *main_link = HTAnchor_findMainLink(src_anchor);
405: HTAnchor *main_anchor = HTAnchor_linkDest(main_link);
406: HTMethod method = HTAnchor_linkMethod(main_link);
407: if (!main_link || method==METHOD_INVALID) {
1.91 frystyk 408: if (WWWTRACE)
1.97 frystyk 409: TTYPrint(TDEST, "Copy Anchor. No destination found or unspecified method");
1.80 frystyk 410: HTRequest_delete(src_req);
1.90 frystyk 411: return NO;
1.80 frystyk 412: }
1.85 frystyk 413: if (HTAnchor_linkResult(main_link) == HT_LINK_NONE) {
414: main_req->GenMask |= HT_DATE; /* Send date header */
415: main_req->source = src_req;
416: main_req->reload = HT_CACHE_REFRESH;
417: main_req->method = method;
418: HTRequest_addDestination(src_req, main_req);
419: main_req->input_format = WWW_SOURCE; /* for now :-( @@@ */
1.90 frystyk 420: if (HTLoadAnchor(main_anchor, main_req) == NO)
421: return NO;
1.85 frystyk 422: }
1.80 frystyk 423: }
1.78 frystyk 424:
1.80 frystyk 425: /* For all other links in the source anchor */
426: if (src_anchor->links) {
427: HTList *cur = src_anchor->links;
428: HTLink *pres;
1.85 frystyk 429: while ((pres = (HTLink *) HTList_nextObject(cur)) &&
430: HTAnchor_linkResult(pres) == HT_LINK_NONE) {
431: HTAnchor *dest = HTAnchor_linkDest(pres);
432: HTMethod method = HTAnchor_linkMethod(pres);
1.80 frystyk 433: HTRequest *dest_req;
434: if (!dest || method==METHOD_INVALID) {
1.91 frystyk 435: if (WWWTRACE)
1.97 frystyk 436: TTYPrint(TDEST, "Copy Anchor. Bad anchor setup %p\n",
1.80 frystyk 437: dest);
1.90 frystyk 438: return NO;
1.80 frystyk 439: }
440: dest_req = HTRequest_new();
1.85 frystyk 441: dest_req->GenMask |= HT_DATE; /* Send date header */
1.80 frystyk 442: dest_req->source = src_req;
1.85 frystyk 443: dest_req->reload = HT_CACHE_REFRESH;
1.80 frystyk 444: dest_req->method = method;
445: HTRequest_addDestination(src_req, dest_req);
446: dest_req->input_format = WWW_SOURCE; /* for now :-( @@@ */
1.90 frystyk 447: if (HTLoadAnchor(dest, dest_req) == NO)
448: return NO;
1.80 frystyk 449: }
450: }
451: } else { /* Use the existing Post Web and restart it */
1.85 frystyk 452: src_req = main_req->source;
1.80 frystyk 453: if (src_req->mainDestination)
1.90 frystyk 454: if (HTLoadDocument(main_req, NO) == NO)
455: return NO;
1.80 frystyk 456: if (src_req->destinations) {
457: HTList *cur = src_anchor->links;
458: HTRequest *pres;
459: while ((pres = (HTRequest *) HTList_nextObject(cur)) != NULL) {
1.90 frystyk 460: if (HTLoadDocument(pres, NO) == NO)
461: return NO;
1.80 frystyk 462: }
463: }
1.78 frystyk 464: }
465:
1.80 frystyk 466: /* Now open the source */
467: return HTLoadAnchor(src_anchor, src_req);
1.70 frystyk 468: }
469:
470:
1.90 frystyk 471: /* Upload an Anchor
1.70 frystyk 472: ** ----------------
1.90 frystyk 473: ** Send the contents (in hyperdoc) of the source anchor using either PUT
474: ** or POST to the remote destination using HTTP. The caller can decide the
475: ** exact method used and which HTTP header fields to transmit by setting
476: ** the user fields in the request structure.
477: ** Returns YES if request accepted, else NO
478: */
479: PUBLIC BOOL HTUploadAnchor (HTAnchor * src_anchor,
480: HTParentAnchor * dest_anchor,
481: HTRequest * dest_req)
1.70 frystyk 482: {
1.90 frystyk 483: if (!src_anchor || !dest_anchor || !dest_req)
484: return NO;
1.70 frystyk 485: if (!(dest_anchor->methods & dest_req->method)) {
486: char buf[80];
487: sprintf(buf, "It might not be allowed to %s to this destination, continue?", HTMethod_name(dest_req->method));
1.86 frystyk 488: if (!HTConfirm(dest_req, buf))
1.90 frystyk 489: return NO;
1.70 frystyk 490: }
1.77 frystyk 491:
492: /* @@@ NOT FINISHED @@@ */
1.70 frystyk 493:
1.90 frystyk 494: return NO;
1.1 timbl 495: }
1.97 frystyk 496:
Webmaster