Annotation of Amaya/amaya/query.c, revision 1.295
1.14 cvs 1: /*
2: *
1.265 cvs 3: * (c) COPYRIGHT MIT and INRIA, 1996-2001.
1.14 cvs 4: * Please first read the full copyright statement in file COPYRIGHT.
1.156 cvs 5: *
1.14 cvs 6: */
1.243 cvs 7:
1.19 cvs 8: /*
1.83 cvs 9: * query.c : contains all the functions for requesting and publishing
1.19 cvs 10: * URLs via libwww. It handles any eventual HTTP error code
11: * (redirection, authentication needed, not found, etc.)
12: *
1.69 cvs 13: * Author: J. Kahan
1.92 cvs 14: * J. Kahan/R. Guetari Windows 95/NT routines
1.19 cvs 15: */
1.17 cvs 16:
1.116 cvs 17: /* defines to include elsewhere
18: *********************************/
19:
1.185 cvs 20: #ifdef _WINDOWS
21: #include <string.h>
1.130 cvs 22: #endif /* !_WINDOWS */
1.116 cvs 23: #define AMAYA_WWW_CACHE
1.168 cvs 24: #define AMAYA_LOST_UPDATE
1.116 cvs 25:
1.12 cvs 26: /* Amaya includes */
1.25 cvs 27: #define THOT_EXPORT extern
1.4 cvs 28: #include "amaya.h"
1.214 cvs 29: #include "init_f.h"
1.130 cvs 30: #include <sys/types.h>
1.70 cvs 31: #include <fcntl.h>
1.140 cvs 32: #include "HTEvtLst.h"
1.157 cvs 33: #include "HTAABrow.h"
1.272 cvs 34: #include "string.h"
1.279 cvs 35: #include "interface.h"
1.7 cvs 36:
1.141 cvs 37: #if defined(__svr4__) || defined (_AIX)
1.4 cvs 38: #define CATCH_SIG
39: #endif
40:
41: /* local structures coming from libwww and which are
1.17 cvs 42: not found in any .h file
1.7 cvs 43: */
1.68 cvs 44:
1.7 cvs 45: struct _HTStream
46: {
1.15 cvs 47: const HTStreamClass *isa;
48: FILE *fp;
1.45 cvs 49: BOOL leave_open; /* Close file when TtaFreeMemory? */
1.15 cvs 50: char *end_command; /* Command to execute */
51: BOOL remove_on_close; /* Remove file? */
52: char *filename; /* Name of file */
53: HTRequest *request; /* saved for callback */
54: HTRequestCallback *callback;
1.7 cvs 55: };
56:
1.15 cvs 57:
1.7 cvs 58: struct _HTError
59: {
60: HTErrorElement element; /* Index number into HTError */
61: HTSeverity severity; /* A la VMS */
62: BOOL ignore; /* YES if msg should not go to user */
63: void *par; /* Explanation, e.g. filename */
64: int length; /* For copying by generic routine */
65: char *where; /* Which function */
66: };
67:
1.4 cvs 68: /* Type definitions and global variables etc. local to this module */
69:
1.17 cvs 70: /*----------------------------------------------------------------------*/
1.293 kahan 71:
72: /* libwww default parameters */
73: #ifdef _WINDOWS
74: #define CACHE_DIR_NAME "\\libwww-cache\\"
75: #else
76: #define CACHE_DIR_NAME "/libwww-cache/"
77: #endif
78:
79: #define DEFAULT_CACHE_SIZE 10
80: #define DEFAULT_MAX_CACHE_ENTRY_SIZE 3
81: #define DEFAULT_MAX_SOCKET 32
82: #define DEFAULT_DNS_TIMEOUT 1800L
83: #define DEFAULT_PERSIST_TIMEOUT 60L
84: #define DEFAULT_NET_EVENT_TIMEOUT 60000
1.294 kahan 85: /* defines the priority for general content negotiation */
86: #define GENERAL_ACCEPT_NEGOTIATION "*/*;q=0.1,"AM_SVG_MIME_TYPE","AM_MATHML_MIME_TYPE","AM_XHTML_MIME_TYPE
1.293 kahan 87: /* defines the priority for image content negotiation */
88: #define IMAGE_ACCEPT_NEGOTIATION "*/*;q=0.1,image/*,image/gif,image/jpeg,image/png,image/svg+xml,"AM_SVG_MIME_TYPE
89:
1.4 cvs 90: /*** private variables ***/
1.17 cvs 91:
1.4 cvs 92: static HTList *converters = NULL; /* List of global converters */
1.27 cvs 93: static HTList *acceptTypes = NULL; /* List of types for the Accept header */
1.193 cvs 94: static HTList *acceptLanguages = NULL; /* List of language priorities for the Accept header */
1.151 cvs 95: static HTList *transfer_encodings = NULL;
96: static HTList *content_encodings = NULL;
1.207 cvs 97: /* List of domains where we can do a safe PUT redirect */
98: static HTList *safeput_list = NULL;
99:
1.16 cvs 100: static int object_counter = 0; /* loaded objects counter */
1.191 cvs 101: static ThotBool AmayaAlive_flag; /* set to 1 if the application is active;
1.140 cvs 102: 0 if we have killed */
1.191 cvs 103: static ThotBool CanDoStop_flag; /* set to 1 if we can do a stop, 0
1.140 cvs 104: if we're inside a critical section */
1.214 cvs 105: static ThotBool UserAborted_flag; /* set to 1 if we're processing a user
106: induced stop request operation */
1.128 cvs 107: #ifdef AMAYA_WWW_CACHE
1.130 cvs 108: static int fd_cachelock; /* open handle to the .lock cache file */
109: #endif /* AMAYA_WWW_CACHE */
1.4 cvs 110:
1.246 kahan 111: static ThotBool FTPURL_flag; /* if set to true, we can browse FTP URLs */
112:
1.248 kahan 113: static FILE *trace_fp = NULL; /* file pointer to the trace logs */
114:
1.15 cvs 115: #include "answer_f.h"
116: #include "query_f.h"
117: #include "AHTURLTools_f.h"
118: #include "AHTBridge_f.h"
119: #include "AHTMemConv_f.h"
120: #include "AHTFWrite_f.h"
1.4 cvs 121:
1.116 cvs 122: /* prototypes */
123:
1.123 cvs 124: #ifdef _WINDOWS
1.70 cvs 125: int WIN_Activate_Request (HTRequest* , HTAlertOpcode, int, const char*, void*, HTAlertPar*);
1.116 cvs 126: #endif /* _WINDOWS */
127:
1.15 cvs 128:
1.130 cvs 129: #ifdef AMAYA_WWW_CACHE
130: /***************************************************************
131: lock functions, used to avoid concurrent use of the cache
132: Mostly based on W. Richard Stevens' APUE book.
133: ***************************************************************/
134: /*----------------------------------------------------------------------
135: set_cachelock
136: sets a write lock on filename.
137: Returns -1 in case of failure, otherwise, it'll open a handle on
138: filename and fd_cachelock will take its value.
139: ----------------------------------------------------------------------*/
1.265 cvs 140: static int set_cachelock (char *filename)
1.130 cvs 141: {
1.188 cvs 142: int status;
1.130 cvs 143: #ifdef _WINDOWS
1.188 cvs 144:
145: status = TtaFileExist (filename);
146: return ((status) ? 0 : -1);
1.130 cvs 147: #else
148: struct flock lock;
149:
150: lock.l_type = F_WRLCK;
151: lock.l_start = 0;
152: lock.l_whence = SEEK_SET;
153: lock.l_len = 0;
154:
155: fd_cachelock = open (filename, O_WRONLY);
156:
157: if (fd_cachelock == -1)
158: status = -1;
159: else
160: status = fcntl(fd_cachelock, F_SETLK, &lock);
161:
162: if (status == -1)
1.190 cvs 163: {
1.179 cvs 164: if (fd_cachelock > 0)
1.190 cvs 165: close (fd_cachelock);
1.179 cvs 166: fd_cachelock = 0;
1.178 cvs 167: }
1.130 cvs 168: return (status);
169: #endif /* _WINDOWS */
170: }
171:
172: /*----------------------------------------------------------------------
173: clear_cachelock
174: remove the write lock set on a filename.
175: It'll close the fd handle on filename and reset *fd to 0.
176: ----------------------------------------------------------------------*/
177: static int clear_cachelock (void)
178: {
179: #ifdef _WINDOWS
180: return 0;
181: #else
182: int status;
183:
1.178 cvs 184: if (!fd_cachelock)
1.130 cvs 185: return (-1);
186:
1.178 cvs 187: status = close (fd_cachelock);
1.130 cvs 188: fd_cachelock = 0;
189:
190: return (status);
191: #endif /* _WINDOWS */
192: }
193:
194: /*----------------------------------------------------------------------
195: test_cachelock
196: returns 0 if a fd is not locked by other process, otherwise
197: returns the pid of the process who has the lock
198: ----------------------------------------------------------------------*/
1.265 cvs 199: static int test_cachelock (char *filename)
1.130 cvs 200: {
201: #ifdef _WINDOWS
1.185 cvs 202: /* if the lock is set, we can't unlink the file under Windows */
1.188 cvs 203: if (!TtaFileUnlink (filename))
204: return 0;
205: else
1.185 cvs 206: return -1;
1.130 cvs 207: #else
208: struct flock lock;
209: int fd, status;
210:
211: lock.l_type = F_WRLCK;
212: lock.l_start = 0;
213: lock.l_whence = SEEK_SET;
214: lock.l_len = 0;
215:
216: fd = open (filename, O_WRONLY);
217:
218: if (fd < 0)
219: return (-1);
220: /* is this file locked ? */
221: status = fcntl (fd, F_GETLK, &lock);
222: close (fd);
223:
224: if (status < 0)
225: return (-1);
226:
227: if (lock.l_type == F_UNLCK)
228: return (0); /* false, region is not locked by another proc */
229: return (lock.l_pid); /* true, return pid of lock owner */
230: #endif /* _WINDOWS */
231: }
232:
233: #endif /* AMAYA_WWW_CACHE */
234:
1.15 cvs 235: /*----------------------------------------------------------------------
1.17 cvs 236: GetDocIdStatus
237: gets the status associated to a docid
1.15 cvs 238: ----------------------------------------------------------------------*/
239: AHTDocId_Status *GetDocIdStatus (int docid, HTList * documents)
240: {
241: AHTDocId_Status *me;
242: HTList *cur;
243:
244: if (documents)
245: {
246: cur = documents;
247:
248: while ((me = (AHTDocId_Status *) HTList_nextObject (cur)))
249: {
250: if (me->docid == docid)
251: return (me);
1.18 cvs 252: }
253: }
1.15 cvs 254: return (AHTDocId_Status *) NULL;
255:
256: }
257:
1.5 cvs 258: /*----------------------------------------------------------------------
1.27 cvs 259: AHTGuessAtom_for
260: Converts an Amaya type descriptor into the equivalent MIME type.
261: ----------------------------------------------------------------------*/
1.276 kahan 262: static HTAtom *AHTGuessAtom_for (char *urlName, char *contentType)
1.27 cvs 263: {
264: HTAtom *atom;
1.265 cvs 265: char * filename;
1.160 cvs 266: HTEncoding enc = NULL;
267: HTEncoding cte = NULL;
268: HTLanguage lang = NULL;
1.50 cvs 269: double quality = 1.0;
1.27 cvs 270:
1.276 kahan 271: /* we already have a MIME type, we jsut return the atom */
272: if (contentType && *contentType)
273: atom = HTAtom_for (contentType);
274: else
1.27 cvs 275: {
1.276 kahan 276: /* we don't have the MIME type, so
277: we try to use the filename's suffix to infer it */
1.265 cvs 278: filename = AmayaParseUrl (urlName, "",
279: AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
280: HTBind_getFormat (filename, &atom, &enc, &cte, &lang, &quality);
1.45 cvs 281: TtaFreeMemory (filename);
1.27 cvs 282: if (atom == WWW_UNKNOWN)
1.276 kahan 283: {
1.27 cvs 284: /*
1.276 kahan 285: ** we could not identify the suffix, so we give it a default type.
286: ** (we should ask the user, but we're not ready for that yet).
1.27 cvs 287: */
288: atom = HTAtom_for ("text/html");
1.276 kahan 289: }
1.27 cvs 290: }
291: return atom;
292: }
293:
294: /*----------------------------------------------------------------------
1.238 cvs 295: HTTP_Headers_set
296: Copies the headers in which the application is interested, doing
297: any in-between conversions as needed.
298: ----------------------------------------------------------------------*/
299: void HTTP_headers_set (HTRequest * request, HTResponse * response, void *context, int status)
300: {
1.267 kahan 301: AHTReqContext *me;
1.283 cvs 302: HTAtom *tmp_atom = NULL;
1.267 kahan 303: char *tmp_char;
1.285 kahan 304: char *tmp_char2;
1.284 kahan 305: char tmp_string[20];
1.267 kahan 306: HTParentAnchor *anchor;
1.287 kahan 307: ThotBool use_anchor;
1.267 kahan 308:
309: me = (AHTReqContext *) HTRequest_context (request);
1.282 kahan 310:
1.267 kahan 311: anchor = HTRequest_anchor (request);
1.238 cvs 312:
1.287 kahan 313: /* if we get a cached answer, we should get all the header information
314: from the anchor, instead of from the response object */
315: tmp_char = HTAnchor_physical (anchor);
316: if (tmp_char && !strncmp (tmp_char, "cache:", sizeof ("cache:") - 1))
317: use_anchor = TRUE;
318: else
319: use_anchor = FALSE;
320:
1.263 kahan 321: /* @@@ JK: we need a function here to specify which headers we
1.238 cvs 322: want to copy */
323:
1.267 kahan 324: /* copy the content_type if we didn't force it before */
325: if (me->http_headers.content_type == NULL)
1.260 kahan 326: {
1.267 kahan 327: /* trying to use the info in the anchor, rather than in the response.
328: Seems it's more recent */
1.282 kahan 329:
330: /* @@ JK: trying to use the content type stored in the response object */
1.287 kahan 331: if (!use_anchor && response)
1.282 kahan 332: tmp_atom = HTResponse_format (response);
333: if (!tmp_atom)
334: tmp_atom = HTAnchor_format (anchor);
1.260 kahan 335:
1.267 kahan 336: if (tmp_atom)
337: tmp_char = HTAtom_name (tmp_atom);
338: else
339: tmp_char = "www/unknown";
340:
341: if (tmp_char && tmp_char[0] != EOS)
1.238 cvs 342: {
1.267 kahan 343: /* libwww gives www/unknown when it gets an error. As this is
344: an HTML test, we force the type to text/html */
345: if (!strcmp (tmp_char, "www/unknown"))
346: {
347: /* if it's not a file downloaded from FTP, initialize the
348: content type to text/html by default */
349: me->http_headers.content_type = NULL;
350: }
351: else
1.284 kahan 352: me->http_headers.content_type = TtaStrdup (tmp_char);
1.267 kahan 353:
1.238 cvs 354: #ifdef DEBUG_LIBWWW
1.267 kahan 355: fprintf (stderr, "Set content type to: %s\n",
356: me->http_headers.content_type);
1.238 cvs 357: #endif /* DEBUG_LIBWWW */
1.267 kahan 358: }
1.238 cvs 359: }
1.267 kahan 360:
1.238 cvs 361: /* copy the charset */
1.267 kahan 362:
363: /* @@ JK I think there was a problem when using info directly from
364: the anchor... but what it was? The response object doesn't have
365: it anyway */
366: tmp_atom = HTAnchor_charset (anchor);
367: #if 0
1.260 kahan 368: if (use_anchor)
369: tmp_atom = HTAnchor_charset (anchor);
370: else
371: tmp_atom = HTResponse_charset (response);
1.267 kahan 372: #endif
373:
1.238 cvs 374: if (tmp_atom)
375: {
1.284 kahan 376: tmp_char = HTAtom_name (tmp_atom);
377: if (tmp_char)
378: me->http_headers.charset = TtaStrdup (tmp_char);
1.238 cvs 379: }
1.256 kahan 380:
1.268 kahan 381: /* copy the content length */
1.284 kahan 382: snprintf (tmp_string, sizeof (tmp_string), "%ld", HTAnchor_length (anchor));
383: me->http_headers.content_length = TtaStrdup (tmp_string);
1.268 kahan 384:
1.256 kahan 385: /* copy the reason */
386: tmp_char = HTResponse_reason (response);
387: if (tmp_char)
1.284 kahan 388: me->http_headers.reason = TtaStrdup (tmp_char);
389:
390: /* copy the content-location */
391: tmp_char = HTAnchor_location (anchor);
1.285 kahan 392: if (tmp_char && *tmp_char)
393: {
394: /* only include the filename. We suppose we have either a
395: relative or an absolute URL and that everything after the last
396: slash is the */
397: if (HTURL_isAbsolute (tmp_char))
398: {
399: tmp_char2 = tmp_char + strlen (tmp_char) -1;
400: while (*tmp_char2 != URL_SEP && tmp_char2 != tmp_char)
401: tmp_char2--;
402: if (tmp_char2 != tmp_char && *(tmp_char2 + 1))
403: tmp_char = tmp_char2 + 1;
404: }
405: me->http_headers.content_location = TtaStrdup (tmp_char);
406: }
1.238 cvs 407: }
408:
409: /*----------------------------------------------------------------------
410: HTTP_headers_delete
411: Deletes all the paramaters that were assigned to the response type
412: ----------------------------------------------------------------------*/
413: static void HTTP_headers_delete (AHTHeaders me)
414: {
415: if (me.content_type)
416: TtaFreeMemory (me.content_type);
417:
418: if (me.charset)
419: TtaFreeMemory (me.charset);
1.268 kahan 420:
421: if (me.content_length)
422: TtaFreeMemory (me.content_length);
1.256 kahan 423:
424: if (me.reason)
425: TtaFreeMemory (me.reason);
1.284 kahan 426:
427: if (me.content_location)
428: TtaFreeMemory (me.content_location);
1.238 cvs 429: }
430:
431: /*----------------------------------------------------------------------
432: HTTP_headers
433: Returns the value of a parameter in the HTTP response structure.
434: Returns null if this structure is empty.
435: ----------------------------------------------------------------------*/
1.265 cvs 436: char *HTTP_headers (AHTHeaders *me, AHTHeaderName param)
1.238 cvs 437: {
1.265 cvs 438: char *result;
1.238 cvs 439:
440: if (!me)
441: return NULL;
442:
443: switch (param)
444: {
445: case AM_HTTP_CONTENT_TYPE:
446: result = me->content_type;
447: break;
448: case AM_HTTP_CHARSET:
449: result = me->charset;
1.268 kahan 450: break;
451: case AM_HTTP_CONTENT_LENGTH:
452: result = me->content_length;
1.238 cvs 453: break;
1.256 kahan 454: case AM_HTTP_REASON:
455: result = me->reason;
1.284 kahan 456: break;
457: case AM_HTTP_CONTENT_LOCATION:
458: result = me->content_location;
1.256 kahan 459: break;
1.238 cvs 460: default:
461: result = NULL;
462: }
463:
464: return result;
465: }
466:
467: /*----------------------------------------------------------------------
1.17 cvs 468: AHTReqContext_new
469: create a new Amaya Context Object and update the global Amaya
470: request status.
1.5 cvs 471: ----------------------------------------------------------------------*/
1.4 cvs 472: static AHTReqContext *AHTReqContext_new (int docid)
473: {
474: AHTReqContext *me;
475: AHTDocId_Status *docid_status;
476:
477: if ((me = (AHTReqContext *) TtaGetMemory (sizeof (AHTReqContext))) == NULL)
1.116 cvs 478: outofmem (__FILE__, "AHTReqContext_new");
479:
480: /* clear the structure */
481: memset ((void *) me, 0, sizeof (AHTReqContext));
1.4 cvs 482:
1.81 cvs 483: /* Bind the Context object together with the Request Object */
484: me->request = HTRequest_new ();
485:
1.80 cvs 486: /* clean the associated file structure) */
1.79 cvs 487: HTRequest_setOutputStream (me->request, NULL);
1.81 cvs 488:
1.4 cvs 489: /* Initialize the other members of the structure */
1.17 cvs 490: me->reqStatus = HT_NEW; /* initial status of a request */
1.4 cvs 491: me->docid = docid;
492: HTRequest_setMethod (me->request, METHOD_GET);
493: HTRequest_setOutputFormat (me->request, WWW_SOURCE);
494: HTRequest_setContext (me->request, me);
1.116 cvs 495:
1.36 cvs 496: /* experimental */
497: me->read_sock = INVSOC;
498: me->write_sock = INVSOC;
499: me->except_sock = INVSOC;
1.4 cvs 500:
501: /* Update the global context */
502: HTList_appendObject (Amaya->reqlist, (void *) me);
503:
504: docid_status = GetDocIdStatus (docid, Amaya->docid_status);
505:
1.7 cvs 506: if (docid_status == NULL)
507: {
1.116 cvs 508: docid_status = (AHTDocId_Status *)
509: TtaGetMemory (sizeof (AHTDocId_Status));
1.7 cvs 510: docid_status->docid = docid;
511: docid_status->counter = 1;
512: HTList_addObject (Amaya->docid_status, (void *) docid_status);
513: }
514: else
1.4 cvs 515: docid_status->counter++;
516:
517: Amaya->open_requests++;
518:
1.74 cvs 519: #ifdef DEBUG_LIBWWW
520: fprintf (stderr, "AHTReqContext_new: Created object %p\n", me);
521: #endif
1.116 cvs 522:
1.4 cvs 523: return me;
524: }
525:
1.5 cvs 526: /*----------------------------------------------------------------------
1.17 cvs 527: AHTReqContext_delete
528: Delete an Amaya Context Object and update the global Amaya request
529: status.
1.5 cvs 530: ----------------------------------------------------------------------*/
1.191 cvs 531: ThotBool AHTReqContext_delete (AHTReqContext * me)
1.4 cvs 532: {
533: AHTDocId_Status *docid_status;
534:
1.7 cvs 535: if (me)
536: {
1.74 cvs 537: #ifdef DEBUG_LIBWWW
1.138 cvs 538: fprintf (stderr, "AHTReqContext_delete: Deleting object %p\n", me);
1.74 cvs 539: #endif
1.138 cvs 540: if (Amaya->reqlist)
541: HTList_removeObject (Amaya->reqlist, (void *) me);
542:
543: docid_status = GetDocIdStatus (me->docid, Amaya->docid_status);
544: if (docid_status)
545: {
546: docid_status->counter--;
547:
548: if (docid_status->counter == 0)
549: {
550: HTList_removeObject (Amaya->docid_status, (void *) docid_status);
551: TtaFreeMemory ((void *) docid_status);
552: }
553: }
1.215 cvs 554:
555: HTNoFreeStream_delete (HTRequest_outputStream (me->request));
556: HTRequest_setOutputStream (me->request, NULL);
1.190 cvs 557: /* JK: no longer needed in libwww 5.2.3
558: if (me->method != METHOD_PUT && HTRequest_outputStream (me->request))
559: AHTFWriter_FREE (HTRequest_outputStream (me->request));
560: */
1.138 cvs 561: HTRequest_delete (me->request);
562:
563: if (me->output && me->output != stdout)
564: {
1.85 cvs 565: #ifdef DEBUG_LIBWWW
1.138 cvs 566: fprintf (stderr, "AHTReqContext_delete: URL is %s, closing "
567: "FILE %p\n", me->urlName, me->output);
1.85 cvs 568: #endif
1.138 cvs 569: fclose (me->output);
570: me->output = NULL;
571: }
1.79 cvs 572:
1.138 cvs 573: if (me->error_stream != (char *) NULL)
574: HT_FREE (me->error_stream);
575: #ifndef _WINDOWS
576: #ifdef WWW_XWINDOWS
577: if (me->read_xtinput_id || me->write_xtinput_id ||
578: me->except_xtinput_id)
579: RequestKillAllXtevents(me);
580: #endif /* WWW_XWINDOWS */
581: #endif /* !_WINDOWS */
582:
583: if (me->reqStatus == HT_ABORT)
584: {
585: if (me->outputfile && me->outputfile[0] != EOS)
586: {
1.233 cvs 587: TtaFileUnlink (me->outputfile);
1.138 cvs 588: me->outputfile[0] = EOS;
589: }
590: }
591:
592: if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC))
593: /* for the ASYNC mode, free the memory we allocated in GetObjectWWW
594: or in PutObjectWWW */
595: {
596: if (me->urlName)
597: TtaFreeMemory (me->urlName);
598: if (me->outputfile)
599: TtaFreeMemory (me->outputfile);
600: }
1.206 cvs 601:
1.208 cvs 602: /* the real name to which we are publishing */
603: if (me->default_put_name)
604: TtaFreeMemory (me->default_put_name);
605:
1.206 cvs 606: /* @@ temp change for the %esc conversion */
607: if (me->method == METHOD_PUT && me->urlName)
608: TtaFreeMemory (me->urlName);
609:
1.138 cvs 610: if (me->formdata)
611: HTAssocList_delete (me->formdata);
1.238 cvs 612:
613: /* erase the response headers */
614: HTTP_headers_delete (me->http_headers);
1.138 cvs 615:
1.228 cvs 616: #ifdef ANNOTATIONS
617: if (me->document)
618: TtaFreeMemory (me->document);
619: #endif /* ANNOTATIONS */
620:
1.138 cvs 621: /* to trace bugs */
622: memset ((void *) me, 0, sizeof (AHTReqContext));
623:
624: TtaFreeMemory ((void *) me);
625:
626: Amaya->open_requests--;
627:
628: return TRUE;
1.7 cvs 629: }
1.15 cvs 630: return FALSE;
1.4 cvs 631: }
632:
633:
1.15 cvs 634: /*----------------------------------------------------------------------
1.17 cvs 635: Thread_deleteAll
636: this function deletes the whole list of active threads.
1.5 cvs 637: ----------------------------------------------------------------------*/
1.4 cvs 638: static void Thread_deleteAll (void)
639: {
1.21 cvs 640: HTList *cur;
641: AHTReqContext *me;
642: AHTDocId_Status *docid_status;
643:
1.74 cvs 644: if (Amaya && Amaya->reqlist)
645: {
646: if (Amaya->open_requests > 0)
647: #ifdef DEBUG_LIBWWW
648: fprintf (stderr, "Thread_deleteAll: Killing %d outstanding "
649: "requests\n", Amaya->open_requests);
650: #endif
651: {
652: cur = Amaya->reqlist;
653:
654: /* erase the requests */
655: while ((me = (AHTReqContext *) HTList_removeLastObject (cur)))
656: {
657: if (me->request)
658: {
1.142 cvs 659: #ifndef _WINDOWS
660: #ifdef WWW_XWINDOWS
1.74 cvs 661: RequestKillAllXtevents (me);
1.142 cvs 662: #endif /* WWW_XWINDOWS */
1.138 cvs 663: #endif /* !_WINDOWS */
1.140 cvs 664: if (!HTRequest_kill (me->request))
665: AHTReqContext_delete (me);
1.74 cvs 666: }
667: } /* while */
668:
669: /* erase the docid_status entities */
670: while ((docid_status = (AHTDocId_Status *) HTList_removeLastObject ((void *) Amaya->docid_status)))
671: TtaFreeMemory ((void *) docid_status);
672:
673: } /* if */
1.84 cvs 674:
1.74 cvs 675: }
1.4 cvs 676: }
1.198 cvs 677:
1.5 cvs 678: /*----------------------------------------------------------------------
1.83 cvs 679: AHTOpen_file
680: ----------------------------------------------------------------------*/
1.85 cvs 681: int AHTOpen_file (HTRequest * request)
1.83 cvs 682: {
683: AHTReqContext *me; /* current request */
684:
685: me = HTRequest_context (request);
1.93 cvs 686: if (!me)
687: return HT_ERROR;
1.83 cvs 688: #ifdef DEBUG_LIBWWW
1.116 cvs 689: fprintf(stderr, "AHTOpen_file: start for object : %p\n", me);
1.85 cvs 690: #endif /* DEBUG_LIBWWW */
691: if (me->reqStatus == HT_ABORT)
692: {
693: #ifdef DEBUG_LIBWWW
694: fprintf(stderr, "AHTOpen_file: caught an abort request, skipping it\n");
1.83 cvs 695: #endif /* DEBUG_LIBWWW */
1.85 cvs 696: return HT_OK;
697: }
698:
1.136 cvs 699: if (me->method == METHOD_PUT)
700: {
701: me->reqStatus = HT_WAITING;
702: return HT_OK;
703: }
704:
1.85 cvs 705: if (HTRequest_outputStream (me->request))
706: {
707: #ifdef DEBUG_LIBWWW
708: fprintf(stderr, "AHTOpen_file: output stream already existed for url %s\n", me->urlName);
709: #endif /* DEBUG_LIBWWW */
710: return HT_OK;
711: }
712:
713: #ifdef DEBUG_LIBWWW
1.265 cvs 714: fprintf(stderr, "AHTOpen_file: opening output stream for url %s\n", me->urlName);
1.85 cvs 715: #endif /* DEBUG_LIBWWW */
716:
1.83 cvs 717: if (!(me->output) &&
718: (me->output != stdout) &&
719: #ifndef _WINDOWS
1.265 cvs 720: (me->output = fopen (me->outputfile, "w")) == NULL)
1.116 cvs 721: #else /* !_WINDOWS */
1.265 cvs 722: (me->output = fopen (me->outputfile, "wb")) == NULL)
723: #endif /* !_WINDOWS */
1.83 cvs 724: {
1.131 cvs 725: me->outputfile[0] = EOS; /* file could not be opened */
1.85 cvs 726: #ifdef DEBUG_LIBWWW
727: fprintf(stderr, "AHTOpen_file: couldn't open output stream for url %s\n", me->urlName);
728: #endif
1.265 cvs 729: TtaSetStatus (me->docid, 1,
730: TtaGetMessage (AMAYA, AM_CANNOT_CREATE_FILE),
731: me->outputfile);
1.83 cvs 732: me->reqStatus = HT_ERR;
733: return (HT_ERROR);
734: }
735:
736: HTRequest_setOutputStream (me->request,
1.265 cvs 737: AHTFWriter_new (me->request, me->output, YES));
1.83 cvs 738: me->reqStatus = HT_WAITING;
739: return HT_OK;
740: }
741:
742: /*----------------------------------------------------------------------
1.265 cvs 743: SafePut_init
744: Sets up the domains which are authorized to make a redirect on
745: a PUT.
746: ----------------------------------------------------------------------*/
747: static void SafePut_init (void)
748: {
749: char *strptr;
1.266 cvs 750: char *ptr, *ptr2;
751: char *domain;
1.265 cvs 752:
753: /* get the proxy settings from the thot.ini file */
754: strptr = TtaGetEnvString ("SAFE_PUT_REDIRECT");
755: if (strptr && *strptr)
756: {
757: /* Get copy we can mutilate */
1.266 cvs 758: ptr2 = TtaStrdup (strptr);
1.265 cvs 759: /* convert to lowercase */
1.266 cvs 760: ptr = ptr2;
1.265 cvs 761: while (*ptr)
762: {
763: *ptr = tolower (*ptr);
764: ptr++;
765: }
766:
767: /* create the list container */
1.266 cvs 768: safeput_list = HTList_new ();
769:
1.265 cvs 770: /* store the domain list */
1.266 cvs 771: ptr = ptr2;
772: while ((domain = HTNextField (&ptr)) != NULL)
1.265 cvs 773: HTList_addObject (safeput_list, TtaStrdup (domain));
774:
775: TtaFreeMemory (ptr2);
776: }
777: }
778:
779: /*----------------------------------------------------------------------
780: SafePut_delete
781: Deletes the safeput_list variable
782: ----------------------------------------------------------------------*/
783: static void SafePut_delete (void)
784: {
785: HTList *cur = safeput_list;
786: char *domain;
787:
788: if (!safeput_list)
789: return;
790:
791: while ((domain = (char *) HTList_nextObject (cur)))
792: TtaFreeMemory (domain);
793: HTList_delete (safeput_list);
794: safeput_list = NULL;
795: }
796:
797: /*----------------------------------------------------------------------
798: SafePut_query
799: returns true if the domain to which belongs the URL accepts an automatic
800: PUT redirect.
801: ----------------------------------------------------------------------*/
802: static ThotBool SafePut_query (char *url)
803: {
1.266 cvs 804: HTList *cur;
805: char *me;
806: ThotBool found;
807: char tmp[MAX_LENGTH];
1.265 cvs 808:
809: /* extract the domain path of the url and normalize it */
810: /* domain = url; */
811: cur = safeput_list;
812: found = FALSE;
813:
1.266 cvs 814: while ((me = (char *) HTList_nextObject (cur)))
815: {
1.273 cvs 816: strcpy (tmp, me);
1.266 cvs 817: if (strstr (url, tmp))
818: {
819: found = TRUE;
820: break;
821: }
822: }
1.265 cvs 823:
824: return (found);
825: }
826:
827: /*----------------------------------------------------------------------
1.17 cvs 828: redirection_handler
829: this function is registered to handle permanent and temporary
830: redirections.
831: ----------------------------------------------------------------------*/
1.265 cvs 832: static int redirection_handler (HTRequest *request, HTResponse *response,
833: void *param, int status)
1.4 cvs 834: {
1.265 cvs 835: HTAnchor *new_anchor = HTResponse_redirection (response);
836: AHTReqContext *me = HTRequest_context (request);
837: HTMethod method = HTRequest_method (request);
838: char *ref;
839: char *escape_src, *dst;
840: char urlAdr[MAX_LENGTH];
841:
842: if (!me)
843: /* if the redirect doesn't come from Amaya, we call libwww's standard redirect filter */
844: return (HTRedirectFilter (request, response, param, status));
845:
846: if (!new_anchor)
847: {
848: if (PROT_TRACE)
1.235 cvs 849: HTTrace ("Redirection. No destination\n");
1.265 cvs 850: return HT_OK;
851: }
1.4 cvs 852:
853: /*
1.207 cvs 854: ** Only do redirect on GET, HEAD, and authorized domains for PUT
1.265 cvs 855: */
856: if ((me->method == METHOD_PUT && !SafePut_query (me->urlName)) ||
857: (me->method != METHOD_PUT && !HTMethod_isSafe (method)))
858: {
859: /*
860: ** If we got a 303 See Other then change the method to GET.
861: ** Otherwise ask the user whether we should continue.
862: */
863: if (status == HT_SEE_OTHER)
864: {
1.235 cvs 865: if (PROT_TRACE)
1.265 cvs 866: HTTrace("Redirection. Changing method from %s to GET\n", HTMethod_name(method));
867: HTRequest_setMethod(request, METHOD_GET);
868: }
869: else
870: {
871: HTAlertCallback *prompt = HTAlert_find (HT_A_CONFIRM);
872: if (prompt)
873: {
874: if ((*prompt) (request, HT_A_CONFIRM, HT_MSG_REDIRECTION, NULL, NULL, NULL) != YES)
875: {
876: /* the user didn't agree on the redirection, so
877: we consider it's an abort */
878: me->reqStatus = HT_ABORT;
879: /* and we return HT_OK so that the terminate_handler
880: will be called */
881: return HT_OK;
882: }
883: }
884: }
885: }
886:
1.4 cvs 887: /*
1.265 cvs 888: ** Start new request with the redirect anchor found in the headers.
889: ** Note that we reuse the same request object which means that we must
890: ** keep this around until the redirected request has terminated. It also
891: ** allows us in an easy way to keep track of the number of redirections
892: ** so that we can detect endless loops.
893: */
894: if (HTRequest_doRetry (request))
895: {
896: /*
897: ** Start request with new credentials
898: */
899: /* only do a redirect using a network protocol understood by Amaya */
1.273 cvs 900: strcpy (urlAdr, new_anchor->parent->address);
1.265 cvs 901: if (IsValidProtocol (urlAdr))
902: {
903: /* if it's a valid URL, we try to normalize it */
904: /* We use the pre-redirection anchor as a base name */
905: /* @@ how to obtain this address here? */
906: dst = urlAdr;
907: escape_src = EscapeURL (me->urlName);
908: if (escape_src)
909: {
910: ref = AmayaParseUrl (dst, escape_src, AMAYA_PARSE_ALL);
911: TtaFreeMemory (escape_src);
912: }
913: else
914: ref = NULL;
915:
916: if (ref)
917: {
918: HTAnchor_setPhysical (HTAnchor_parent (new_anchor), ref);
919: TtaFreeMemory (ref);
920: }
921: else
922: return HT_OK; /* We can't redirect anymore */
923: }
924: else
925: return HT_OK; /* We can't redirect anymore */
926:
927: /* update the current file name */
928: if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC) ||
929: (me->method == METHOD_PUT))
930: {
931: TtaFreeMemory (me->urlName);
1.272 cvs 932: me->urlName = TtaStrdup (urlAdr);
1.265 cvs 933: }
934: else
935: {
936: /* it's a SYNC mode, so we should keep the urlName */
937: strncpy (me->urlName, urlAdr, MAX_LENGTH - 1);
938: me->urlName[MAX_LENGTH - 1] = EOS;
939: }
940: ChopURL (me->status_urlName, me->urlName);
1.166 cvs 941:
1.265 cvs 942: /* @@ verify if this is important */
943: /* @@@ new libwww doesn't need this free stream while making
944: a PUT. Is it the case everywhere or just for PUT? */
945: if (me->method != METHOD_PUT &&
946: me->request->orig_output_stream != NULL)
947: {
948: AHTFWriter_FREE (me->request->orig_output_stream);
949: me->request->orig_output_stream = NULL;
950: if (me->output != stdout)
951: {
952: /* Are we writing to a file? */
1.257 kahan 953: #ifdef DEBUG_LIBWWW
1.265 cvs 954: fprintf (stderr, "redirection_handler: New URL is %s, closing FILE %p\n", me->urlName, me->output);
1.257 kahan 955: #endif
1.265 cvs 956: fclose (me->output);
957: me->output = NULL;
958: }
959: }
960:
961: /* tell the user what we're doing */
962: TtaSetStatus (me->docid, 1,
963: TtaGetMessage (AMAYA, AM_RED_FETCHING), me->status_urlName);
964: /*
965: ** launch the request
966: */
967: /* add a link relationship? */
968: /* reset the request status */
969: me->reqStatus = HT_NEW;
970: /* clear the errors */
971: HTError_deleteAll (HTRequest_error (request));
972: HTRequest_setError (request, NULL);
973: /* clear the authentication credentials, as they get regenerated */
974: HTRequest_deleteCredentialsAll (request);
975:
976: if (me->method == METHOD_POST || me->method == METHOD_PUT)
1.285 kahan 977: {
978: /* PUT, POST etc. */
979: /* for PUT, we memorize there was a redirection, so that we
980: can clear the original cache entry in the terminate_handler */
981: if (me->method == METHOD_PUT)
982: me->put_redirection = TRUE;
983: status = HTLoadAbsolute (me->urlName, request);
984: }
1.265 cvs 985: else
986: HTLoadAnchor (new_anchor, request);
987: }
988: else
989: {
990: HTRequest_addError (request, ERR_FATAL, NO, HTERR_MAX_REDIRECT, NULL, 0, "HTRedirectFilter");
991: /* so that we can show the error message */
992: if (me->error_html)
993: DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
994: me->reqStatus = HT_ERR;
995: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REDIRECTIONS_LIMIT), NULL);
996: }
997:
1.4 cvs 998: /*
1.74 cvs 999: ** By returning HT_ERROR we make sure that this is the last handler to be
1000: ** called. We do this as we don't want any other filter to delete the
1001: ** request object now when we have just started a new one ourselves
1002: */
1.4 cvs 1003: return HT_ERROR;
1004: }
1005:
1.151 cvs 1006: #ifdef AMAYA_LOST_UPDATE
1007: /*----------------------------------------------------------------------
1008: precondition_handler
1009: 412 "Precondition failed" handler
1010: ----------------------------------------------------------------------*/
1.265 cvs 1011: static int precondition_handler (HTRequest *request, HTResponse *response,
1012: void *context, int status)
1.151 cvs 1013: {
1.168 cvs 1014: AHTReqContext *me = (AHTReqContext *) HTRequest_context (request);
1015: HTAlertCallback *prompt = HTAlert_find (HT_A_CONFIRM);
1.191 cvs 1016: ThotBool force_put;
1.151 cvs 1017:
1018: if (!me)
1019: return HT_OK; /* not an Amaya request */
1020:
1.168 cvs 1021: if (prompt)
1022: force_put = (*prompt) (request, HT_A_CONFIRM, HT_MSG_RULES,
1023: NULL, NULL, NULL);
1024: else
1025: force_put = NO;
1026:
1027: if (force_put)
1028: {
1029: /* start a new PUT request without preconditions */
1030: /* @@ do we need to kill the request? */
1031: if (me->output && me->output != stdout)
1032: {
1033: fclose (me->output);
1034: me->output = NULL;
1035: }
1036: /*
1037: ** reset the Amaya and libwww request status
1038: */
1.176 cvs 1039:
1040: /* start with a new, clean request structure */
1041: HTRequest_delete (me->request);
1042: me->request = HTRequest_new ();
1043: /* clean the associated file structure) */
1044: HTRequest_setOutputStream (me->request, NULL);
1045: /* Initialize the other members of the structure */
1.171 cvs 1046: HTRequest_setMethod (me->request, METHOD_GET);
1.176 cvs 1047: HTRequest_setOutputFormat (me->request, WWW_SOURCE);
1048: HTRequest_setContext (me->request, me);
1049: HTRequest_setPreemptive (me->request, NO);
1050: /*
1051: ** Make sure that the first request is flushed immediately and not
1052: ** buffered in the output buffer
1053: */
1054: if (me->mode & AMAYA_FLUSH_REQUEST)
1055: HTRequest_setFlush(me->request, YES);
1056:
1.171 cvs 1057: /* turn off preconditions */
1058: HTRequest_setPreconditions(me->request, HT_NO_MATCH);
1059: me->reqStatus = HT_NEW;
1060: /* make the request */
1.176 cvs 1061: status = HTPutDocumentAnchor (HTAnchor_parent (me->source),
1062: me->dest, me->request);
1.168 cvs 1063: /* stop here */
1064: return HT_ERROR;
1065: }
1066: else
1067: {
1068: /* abort the request */
1069: /* @@ should we call StopRequest here? */
1070: me->reqStatus = HT_ABORT;
1071: /* stop here */
1072: return HT_ERROR;
1073: }
1074: }
1.151 cvs 1075:
1.168 cvs 1076: /*----------------------------------------------------------------------
1077: check_handler
1078: Request HEAD checker filter
1079: ----------------------------------------------------------------------*/
1080: static int check_handler (HTRequest * request, HTResponse * response,
1.151 cvs 1081: void * param, int status)
1082: {
1.168 cvs 1083: AHTReqContext *me = (AHTReqContext *) HTRequest_context (request);
1084: HTAlertCallback *prompt = HTAlert_find (HT_A_CONFIRM);
1.191 cvs 1085: ThotBool force_put;
1.168 cvs 1086:
1087: if (!me)
1088: return HT_OK; /* not an Amaya request */
1089:
1090: HTRequest_deleteAfter(me->request, check_handler);
1091: /*
1092: ** If head request showed that the document doesn't exist
1093: ** then just go ahead and PUT it. Otherwise ask for help
1094: */
1095: if (status == HT_ERROR || status == HT_INTERRUPTED || status == HT_TIMEOUT)
1096: {
1097: /* we'd need to call terminate_handler, to free the resources */
1098: /* abort the request */
1099: /* @@ should we call StopRequest here? */
1100: me->reqStatus = HT_ABORT;
1101: /* stop here */
1102: return HT_ERROR;
1103: }
1104: else if (status == HT_NO_ACCESS || status == HT_NO_PROXY_ACCESS)
1105: {
1106: /* we'd need to call terminate_handler, to free the resources */
1107: /* abort the request */
1108: /* @@ should we call StopRequest here? */
1109: me->reqStatus = HT_ABORT;
1110: /* stop here */
1111: return HT_ERROR;
1112: }
1113: else if ( (abs(status)/100) != 2)
1114: {
1.176 cvs 1115: /* start with a new, clean request structure */
1116: HTRequest_delete (me->request);
1117: me->request = HTRequest_new ();
1.208 cvs 1118: /* add the default PUT name */
1119: HTRequest_setDefaultPutName (me->request, me->default_put_name);
1.176 cvs 1120: /* clean the associated file structure) */
1121: HTRequest_setOutputStream (me->request, NULL);
1122: /* Initialize the other members of the structure */
1.171 cvs 1123: HTRequest_setMethod (me->request, METHOD_GET);
1.176 cvs 1124: HTRequest_setOutputFormat (me->request, WWW_SOURCE);
1125: HTRequest_setContext (me->request, me);
1126: HTRequest_setPreemptive (me->request, NO);
1127: /*
1128: ** Make sure that the first request is flushed immediately and not
1129: ** buffered in the output buffer
1130: */
1131: if (me->mode & AMAYA_FLUSH_REQUEST)
1132: HTRequest_setFlush(me->request, YES);
1133:
1134: /* turn on the special preconditions, to avoid having this
1135: ressource appear before we do the PUT */
1.168 cvs 1136: HTRequest_setPreconditions(me->request, HT_DONT_MATCH_ANY);
1.171 cvs 1137: me->reqStatus = HT_NEW;
1.168 cvs 1138: status = HTPutDocumentAnchor (HTAnchor_parent (me->source), me->dest, me->request);
1139: return HT_ERROR; /* stop here */
1.176 cvs 1140: }
1.168 cvs 1141: else
1142: {
1143: if (prompt)
1144: force_put = (*prompt) (request, HT_A_CONFIRM, HT_MSG_FILE_REPLACE,
1145: NULL, NULL, NULL);
1146: else
1147: force_put = FALSE;
1148:
1149: if (force_put)
1.151 cvs 1150: {
1151: /* Start a new PUT request without preconditions */
1.176 cvs 1152:
1153: /* get a new, clean request structure */
1154: HTRequest_delete (me->request);
1155: me->request = HTRequest_new ();
1.208 cvs 1156: /* add the default PUT name */
1157: HTRequest_setDefaultPutName (me->request, me->default_put_name);
1.176 cvs 1158: /* clean the associated file structure) */
1159: HTRequest_setOutputStream (me->request, NULL);
1160: /* Initialize the other members of the structure */
1.171 cvs 1161: HTRequest_setMethod (me->request, METHOD_GET);
1.176 cvs 1162: HTRequest_setOutputFormat (me->request, WWW_SOURCE);
1163: HTRequest_setContext (me->request, me);
1164: HTRequest_setPreemptive (me->request, NO);
1165: /*
1166: ** Make sure that the first request is flushed immediately and not
1167: ** buffered in the output buffer
1168: */
1169: if (me->mode & AMAYA_FLUSH_REQUEST)
1170: HTRequest_setFlush(me->request, YES);
1171: /* remove the preconditions */
1.168 cvs 1172: HTRequest_setPreconditions(me->request, HT_NO_MATCH);
1.171 cvs 1173: me->reqStatus = HT_NEW;
1.176 cvs 1174: status = HTPutDocumentAnchor (HTAnchor_parent (me->source),
1175: me->dest, me->request);
1.168 cvs 1176: return HT_ERROR; /* stop here */
1.151 cvs 1177: }
1.168 cvs 1178: else
1179: {
1180: /* we'd need to call terminate_handler, to free the resources */
1181: /* abort the request */
1182: /* @@ should we call StopRequest here? */
1183: me->reqStatus = HT_ABORT;
1184: /* stop here */
1185: return HT_ERROR;
1186: }
1187: }
1188: return HT_ERROR;
1.151 cvs 1189: }
1190: #endif /* AMAYA_LOST_UPDATE */
1191:
1.5 cvs 1192: /*----------------------------------------------------------------------
1.17 cvs 1193: terminate_handler
1194: this function is registered to handle the result of the request
1.5 cvs 1195: ----------------------------------------------------------------------*/
1.265 cvs 1196: static int terminate_handler (HTRequest *request, HTResponse *response,
1197: void *context, int status)
1.4 cvs 1198: {
1.116 cvs 1199: AHTReqContext *me = (AHTReqContext *) HTRequest_context (request);
1.191 cvs 1200: ThotBool error_flag;
1.116 cvs 1201:
1202: if (!me)
1.214 cvs 1203: return HT_ERROR; /* not an Amaya request */
1.116 cvs 1204:
1205: if (me->reqStatus == HT_END)
1206: /* we have already processed this request! */
1207: return HT_OK;
1208:
1209: /* if Amaya was killed, treat with this request as if it were
1210: issued by a Stop button event */
1211:
1212: if (!AmayaIsAlive ())
1.80 cvs 1213: me->reqStatus = HT_ABORT;
1.220 cvs 1214:
1215: /* trying to protect against this problem... hard to place
1216: the detection elsewhere :-/ */
1.240 cvs 1217: /* @@ JK: Is this still needed? */
1.220 cvs 1218: if (IsHTTP09Error (request))
1219: status = -1;
1220:
1.238 cvs 1221: if (status == HT_LOADED
1222: || status == HT_CREATED
1223: || status == HT_NO_DATA
1.249 kahan 1224: /* JK: removed it as the kludge doesn't seem useful anymore */
1.160 cvs 1225: /* kludge for work around libwww problem */
1.249 kahan 1226: /* || (status == HT_INTERRUPTED && me->method == METHOD_PUT) */
1.116 cvs 1227: #ifdef AMAYA_WWW_CACHE
1228: /* what status to use to know we're downloading from a cache? */
1.238 cvs 1229: || status == HT_NOT_MODIFIED
1230: || status == HT_PARTIAL_CONTENT
1.116 cvs 1231: #endif /* AMAYA_WWW_CACHE */
1.238 cvs 1232: || me->reqStatus == HT_ABORT)
1.74 cvs 1233: error_flag = FALSE;
1.256 kahan 1234: else
1.74 cvs 1235: error_flag = TRUE;
1.130 cvs 1236:
1.285 kahan 1237: /* If we did a PUT that was redirected, clean the original
1238: cache entry */
1239: if (status == 204 && me->method == METHOD_PUT && me->put_redirection)
1240: {
1241: HTCache * cache;
1242: HTParentAnchor * anchor = HTAnchor_parent (me->dest);
1243: cache = HTCache_find (anchor, NULL);
1244: if (cache)
1245: {
1246: /* what a problem... we update the cache with the wrong data
1247: from the response... after the redirection */
1248: HTCache_resetMeta (cache, request, response);
1249: }
1250: else
1251: /* If entry doesn't already exist then create a new entry */
1252: HTCache_touch(request, response, HTAnchor_parent (me->dest));
1253: }
1254:
1.4 cvs 1255: /* output any errors from the server */
1.130 cvs 1256:
1.77 cvs 1257: /*
1.74 cvs 1258: ** me->output = output file which will receive an html file
1259: ** me->error_html = yes, output HTML errors in the screen
1260: ** request->error_stack == if there are any errors, they will be here
1261: ** me->error_stream_size If it's != 0 means an error message has already
1262: ** been written to the stack
1263: */
1264:
1.4 cvs 1265: /* First, we verify if there are any errors and if they are not
1.17 cvs 1266: ** yet written to the error stack. If no, then let's try to write them
1267: ** ourselves
1268: */
1.74 cvs 1269:
1270: #ifdef DEBUG_LIBWWW
1271: fprintf (stderr, "terminate_handler: URL is "
1272: "%s, closing FILE %p status is %d\n", me->urlName, me->output,
1273: status);
1.69 cvs 1274: #endif
1.74 cvs 1275:
1.69 cvs 1276: if (me->output && me->output != stdout)
1277: {
1.214 cvs 1278: /* we are writing to a local file */
1.74 cvs 1279: if (me->reqStatus != HT_ABORT)
1280: { /* if the request was not aborted and */
1.80 cvs 1281: if (error_flag &&
1282: me->error_html == TRUE)
1283: /* there were some errors and we want to print them */
1284: {
1285: if (me->error_stream_size == 0)/* and the stream is empty */
1.210 cvs 1286: {
1287: /* if the transfer was interrupted, the file may not be
1288: empty. So, we erase it */
1289: fflush (me->output);
1290: rewind (me->output);
1291: AHTError_MemPrint (request); /* copy errors from
1292: **the error stack
1293: ** into the error stream */
1294: }
1.80 cvs 1295: if (me->error_stream)
1296: { /* if the stream is non-empty */
1297: fprintf (me->output, me->error_stream);/* output the errors */
1.85 cvs 1298: /* Clear the error context, so that we can deal with
1299: this answer as if it were a normal reply */
1300: HTError_deleteAll( HTRequest_error (request));
1301: HTRequest_setError (request, NULL);
1302: error_flag = 0;
1.74 cvs 1303: }
1304: } /* if error_stack */
1.85 cvs 1305: }
1306:
1307: /* if != HT_ABORT */
1308:
1309: #ifdef DEBUG_LIBWWW
1310: fprintf (stderr, "terminate_handler: URL is %s, closing "
1311: "FILE %p\n", me->urlName, me->output);
1312: #endif
1.74 cvs 1313: fclose (me->output);
1314: me->output = NULL;
1.69 cvs 1315: }
1.80 cvs 1316:
1.214 cvs 1317: /* a very special handling so that we can put a message box
1318: to tell the user WHAT he has just done... */
1319: if (UserAborted_flag && me->method == METHOD_PUT)
1320: {
1321: HTRequest_addError (request, ERR_FATAL, NO, HTERR_INTERRUPTED,
1322: "Operation aborted by user", 0, NULL);
1323: }
1324:
1.80 cvs 1325: if (error_flag)
1326: me->reqStatus = HT_ERR;
1.214 cvs 1327: else
1328: if (me->reqStatus != HT_ABORT)
1329: me->reqStatus = HT_END;
1.154 cvs 1330:
1.238 cvs 1331: /* copy the headers in which the application is interested */
1332: HTTP_headers_set (request, response, context, status);
1.114 cvs 1333:
1.115 cvs 1334: /* to avoid a hangup while downloading css files */
1.140 cvs 1335: if (AmayaAlive_flag && (me->mode & AMAYA_LOAD_CSS))
1.265 cvs 1336: TtaSetStatus (me->docid, 1,
1337: TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
1338: me->status_urlName);
1.116 cvs 1339:
1340: /* don't remove or Xt will hang up during the PUT */
1341: if (AmayaIsAlive () && ((me->method == METHOD_POST) ||
1342: (me->method == METHOD_PUT)))
1.7 cvs 1343: {
1.80 cvs 1344: PrintTerminateStatus (me, status);
1345: }
1.114 cvs 1346:
1.111 cvs 1347: ProcessTerminateRequest (request, response, context, status);
1.116 cvs 1348:
1.151 cvs 1349: /* stop here */
1.150 cvs 1350: return HT_ERROR;
1.4 cvs 1351: }
1352:
1.5 cvs 1353: /*----------------------------------------------------------------------
1.17 cvs 1354: AHTLoadTerminate_handler
1.74 cvs 1355: this is an application "AFTER" Callback. It's called by the library
1356: when a request has ended, so that we can setup the correct status.
1.5 cvs 1357: ----------------------------------------------------------------------*/
1.265 cvs 1358: int AHTLoadTerminate_handler (HTRequest *request, HTResponse *response,
1359: void *param, int status)
1.4 cvs 1360: {
1.116 cvs 1361: /** @@@@ use this with printstatus ?? */
1.4 cvs 1362: AHTReqContext *me = HTRequest_context (request);
1363: HTAlertCallback *cbf;
1364: AHTDocId_Status *docid_status;
1365:
1.7 cvs 1366: switch (status)
1.116 cvs 1367: {
1368: case HT_LOADED:
1369: if (PROT_TRACE)
1370: HTTrace ("Load End.... OK: `%s\' has been accessed\n",
1371: me->status_urlName);
1.4 cvs 1372:
1.116 cvs 1373: docid_status = GetDocIdStatus (me->docid,
1374: Amaya->docid_status);
1.7 cvs 1375:
1.116 cvs 1376: if (docid_status != NULL && docid_status->counter > 1)
1377: TtaSetStatus (me->docid, 1,
1378: TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
1.233 cvs 1379: me->status_urlName);
1.7 cvs 1380: break;
1.116 cvs 1381:
1382: case HT_NO_DATA:
1383: if (PROT_TRACE)
1384: HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n",
1385: me->status_urlName);
1386: TtaSetStatus (me->docid, 1,
1387: TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
1.233 cvs 1388: me->status_urlName);
1.116 cvs 1389: break;
1390:
1391: case HT_INTERRUPTED:
1392: if (PROT_TRACE)
1393: HTTrace ("Load End.... INTERRUPTED: `%s\'\n",
1394: me->status_urlName);
1395: TtaSetStatus (me->docid, 1,
1396: TtaGetMessage (AMAYA, AM_LOAD_ABORT),
1397: NULL);
1398: break;
1.7 cvs 1399:
1.116 cvs 1400: case HT_RETRY:
1401: if (PROT_TRACE)
1402: HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
1403: HTResponse_retryTime (response));
1404: TtaSetStatus (me->docid, 1,
1405: TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
1.233 cvs 1406: me->status_urlName);
1.116 cvs 1407: break;
1.7 cvs 1408:
1.247 kahan 1409: case HT_NOT_MODIFIED:
1410: if (PROT_TRACE)
1.265 cvs 1411: HTTrace ("Load End.... NOT MODIFIED (%s)",
1412: me->status_urlName ? me->status_urlName :" <UNKNOWN>");
1.247 kahan 1413: break;
1414:
1.116 cvs 1415: case HT_ERROR:
1416: cbf = HTAlert_find (HT_A_MESSAGE);
1417: if (cbf)
1418: (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
1419: HTRequest_error (request), NULL);
1.247 kahan 1420: if (PROT_TRACE)
1.265 cvs 1421: HTTrace ("Load End.... ERROR: Can't access `%s\'\n",
1422: me->status_urlName ? me->status_urlName :"<UNKNOWN>");
1423: TtaSetStatus (me->docid, 1,
1424: TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1425: me->status_urlName ? me->status_urlName : "<UNKNOWN>");
1.116 cvs 1426: break;
1.247 kahan 1427:
1428: case HTERR_TIMEOUT:
1429: cbf = HTAlert_find (HT_A_MESSAGE);
1430: if (cbf)
1431: (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
1432: HTRequest_error (request), NULL);
1.116 cvs 1433: if (PROT_TRACE)
1.265 cvs 1434: HTTrace ("Load End.... REQUEST TIMEOUT: Can't access `%s\'\n",
1435: me->status_urlName ? me->status_urlName :"<UNKNOWN>");
1436: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1437: me->status_urlName ? me->status_urlName : "<UNKNOWN>");
1.116 cvs 1438: break;
1.247 kahan 1439:
1.116 cvs 1440: default:
1441: if (PROT_TRACE)
1442: HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
1443: break;
1444: }
1445:
1446: return HT_OK;
1447: }
1.7 cvs 1448:
1.248 kahan 1449: /*----------------------------------------------------------------------
1450: LineTrace
1451: Makes a file copy of libwww's traces
1452: ----------------------------------------------------------------------*/
1.116 cvs 1453: static int LineTrace (const char * fmt, va_list pArgs)
1454: {
1.248 kahan 1455: vfprintf (trace_fp, fmt, pArgs);
1456: return (fflush (trace_fp));
1.4 cvs 1457: }
1458:
1.27 cvs 1459: /*----------------------------------------------------------------------
1460: AHTAcceptTypesInit
1.74 cvs 1461: This function prepares the Accept header used by Amaya during
1462: the HTTP content negotiation phase
1.27 cvs 1463: ----------------------------------------------------------------------*/
1464: static void AHTAcceptTypesInit (HTList *c)
1465: {
1.223 cvs 1466: if (c == (HTList *) NULL)
1.27 cvs 1467: return;
1468:
1.223 cvs 1469: /* define here all the mime types that Amaya can accept */
1.27 cvs 1470:
1.223 cvs 1471: HTConversion_add (c, "image/png", "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1472: HTConversion_add (c, "image/jpeg", "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1473: HTConversion_add (c, "image/gif", "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1474: HTConversion_add (c, "image/xbm", "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1475: HTConversion_add (c, "image/xpm", "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1.183 cvs 1476:
1.27 cvs 1477: /* Define here the equivalences between MIME types and file extensions for
1.223 cvs 1478: the types that Amaya can display */
1.27 cvs 1479:
1.176 cvs 1480: /* Initialize suffix bindings for local files */
1481: HTBind_init();
1482:
1.27 cvs 1483: /* Register the default set of file suffix bindings */
1484: HTFileInit ();
1485:
1.257 kahan 1486: /* Register additional bindings */
1.276 kahan 1487: HTBind_add("htm", "text/html", NULL, "8bit", NULL, 1.0);
1.257 kahan 1488: HTBind_add("tgz", "application/gnutar", NULL, "binary", NULL, 1.0);
1.293 kahan 1489: HTBind_add("mml", AM_MATHML_MIME_TYPE, NULL, "8bit", NULL, 1.0);
1490: HTBind_add("svg", AM_SVG_MIME_TYPE, NULL, "8bit", NULL, 1.0);
1.260 kahan 1491: HTBind_add("xsl", "text/xml", NULL, "8bit", NULL, 1.0);
1.27 cvs 1492: /* Don't do any case distinction */
1493: HTBind_caseSensitive (FALSE);
1494: }
1.4 cvs 1495:
1.5 cvs 1496: /*----------------------------------------------------------------------
1.193 cvs 1497: AHTAcceptLanguagesInit
1498: This function prepares the Accept header used by Amaya during
1499: the HTTP content negotiation phase
1500: ----------------------------------------------------------------------*/
1.196 cvs 1501: static void AHTAcceptLanguagesInit (HTList *c)
1.193 cvs 1502: {
1.265 cvs 1503: char *ptr;
1504: char *lang_list;
1.223 cvs 1505: char s[3];
1.196 cvs 1506: int count, lg;
1507: double quality;
1508: ThotBool still;
1.193 cvs 1509:
1510: if (c == (HTList *) NULL)
1.265 cvs 1511: return;
1.193 cvs 1512:
1.203 cvs 1513: lang_list = TtaGetEnvString ("ACCEPT_LANGUAGES");
1.197 cvs 1514: s[2] = EOS;
1.272 cvs 1515: if (lang_list && *lang_list != EOS)
1.193 cvs 1516: {
1517: /* add the default language first */
1518: HTLanguage_add (c, "*", -1.0);
1519: /* how many languages do we have? */
1.196 cvs 1520: ptr = lang_list;
1521: count = 0;
1.272 cvs 1522: while (*ptr != EOS)
1.265 cvs 1523: {
1.272 cvs 1524: while (*ptr != EOS &&
1.265 cvs 1525: (*ptr < 'A' || (*ptr > 'Z' && *ptr < 'a') || *ptr > 'z'))
1526: /* skip the whole separator */
1527: ptr++;
1528: lg = 0;
1529: while ((*ptr >= 'A' &&
1530: *ptr <= 'Z') || (*ptr >= 'a' && *ptr <= 'z') || *ptr == '-')
1531: {
1532: /* it's a new language */
1533: ptr++;
1534: lg++;
1535: }
1536: if (lg >= 2)
1537: count++;
1.272 cvs 1538: if (*ptr != EOS)
1.265 cvs 1539: ptr++;
1540: }
1.193 cvs 1541:
1.196 cvs 1542: if (count > 0)
1.265 cvs 1543: quality = 1.1 - (double) count/10.0;
1.193 cvs 1544: else
1.265 cvs 1545: quality = 1.0;
1.193 cvs 1546:
1.265 cvs 1547: /* Read the languages from the registry, then inject them one, by one.
1548: The first one is the one with the highest priority.
1549: The libwww ask for the lowest priority first.
1.196 cvs 1550: */
1551: ptr--;
1552: still = TRUE;
1.265 cvs 1553: while (count)
1554: {
1555: while (still && (*ptr < 'A' || (*ptr > 'Z' && *ptr < 'a') || *ptr > 'z'))
1556: /* skip the whole separator */
1557: if (ptr > lang_list)
1558: ptr--;
1559: else
1560: still = FALSE;
1561: lg = 0;
1562: while (still &&
1563: ((*ptr >= 'A' &&
1564: *ptr <= 'Z') || (*ptr >= 'a' && *ptr <= 'z') || *ptr == '-'))
1565: {
1566: /* it's a new language */
1567: if (ptr > lang_list)
1568: ptr--;
1569: else
1570: still = FALSE;
1571: lg++;
1572: }
1573: if (lg >= 2)
1574: {
1575: if (still)
1576: strncpy (s, &ptr[1], 2);
1577: else
1578: strncpy (s, lang_list, 2);
1579: count--;
1580: HTLanguage_add (c, s, quality);
1581: quality += 0.1;
1582: }
1583: ptr--;
1584: }
1585: }
1.193 cvs 1586: }
1587:
1588: /*----------------------------------------------------------------------
1.17 cvs 1589: AHTConverterInit
1590: Bindings between a source media type and a destination media type
1591: (conversion).
1.5 cvs 1592: ----------------------------------------------------------------------*/
1.15 cvs 1593: static void AHTConverterInit (HTList *c)
1.4 cvs 1594: {
1595: /* Handler for custom http error messages */
1.7 cvs 1596: HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4 cvs 1597:
1598: /*
1599: ** These are converters that converts to something other than www/present,
1600: ** that is not directly outputting someting to the user on the screen
1601: */
1602:
1.223 cvs 1603: HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 1.0, 0.0, 0.0);
1604: HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter, 1.0, 0.0, 0.0);
1605: HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader, 1.0, 0.0, 0.0);
1606: HTConversion_add(c,"message/x-rfc822-cont", "*/*", HTMIMEContinue, 1.0, 0.0, 0.0);
1607: HTConversion_add(c,"message/x-rfc822-partial","*/*", HTMIMEPartial, 1.0, 0.0, 0.0);
1608: HTConversion_add (c, "multipart/*", "*/*", HTBoundary, 1.0, 0.0, 0.0);
1609: HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML, 1.0, 0.0, 0.0);
1.4 cvs 1610:
1611: /*
1.116 cvs 1612: ** The following conversions are converting ASCII output from various
1613: ** protocols to HTML objects.
1614: */
1.223 cvs 1615: HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new, 1.0, 0.0, 0.0);
1.4 cvs 1616:
1617: /*
1.116 cvs 1618: ** We also register a special content type guess stream that can figure out
1619: ** the content type by reading the first bytes of the stream
1620: */
1.223 cvs 1621: HTConversion_add (c, "www/unknown", "*/*", HTGuess_new, 1.0, 0.0, 0.0);
1.4 cvs 1622:
1.116 cvs 1623: #ifdef AMAYA_WWW_CACHE
1.4 cvs 1624: /*
1.116 cvs 1625: ** Register a persistent cache stream which can save an object to local
1626: ** file
1627: */
1.223 cvs 1628: HTConversion_add (c, "www/cache", "*/*", HTCacheWriter, 1.0, 0.0, 0.0);
1629: HTConversion_add(c,"www/cache-append", "*/*", HTCacheAppend, 1.0, 0.0, 0.0);
1.116 cvs 1630: #endif AMAYA_WWW_CACHE
1.4 cvs 1631:
1632: /*
1.116 cvs 1633: ** This dumps all other formats to local disk without any further
1634: ** action taken
1635: */
1.223 cvs 1636: HTConversion_add (c, "*/*", "www/present", HTSaveLocally, 0.3, 0.0, 0.0);
1.4 cvs 1637: }
1638:
1.27 cvs 1639:
1.15 cvs 1640: /*----------------------------------------------------------------------
1.17 cvs 1641: AHTProtocolInit
1642: Registers all amaya supported protocols.
1.15 cvs 1643: ----------------------------------------------------------------------*/
1.4 cvs 1644: static void AHTProtocolInit (void)
1645: {
1.265 cvs 1646: char *strptr;
1.4 cvs 1647:
1.116 cvs 1648: /*
1649: NB. Preemptive == YES means Blocking requests
1650: Non-preemptive == NO means Non-blocking requests
1651: */
1652: HTTransport_add("tcp", HT_TP_SINGLE, HTReader_new, HTWriter_new);
1653: HTTransport_add("buffered_tcp", HT_TP_SINGLE, HTReader_new,
1654: HTBufferWriter_new);
1655: HTProtocol_add ("http", "buffered_tcp", HTTP_PORT, NO, HTLoadHTTP, NULL);
1.136 cvs 1656: #ifdef _WINDOWS
1657: HTProtocol_add ("file", "local", 0, YES, HTLoadFile, NULL);
1658: #else
1.116 cvs 1659: HTProtocol_add ("file", "local", 0, NO, HTLoadFile, NULL);
1.198 cvs 1660: #endif /* _WINDOWS */
1.116 cvs 1661: #ifdef AMAYA_WWW_CACHE
1662: HTProtocol_add("cache", "local", 0, YES, HTLoadCache, NULL);
1663: #endif /* AMAYA_WWW_CACHE */
1.103 cvs 1664: #if 0 /* experimental code */
1.245 kahan 1665: #endif
1.116 cvs 1666: HTProtocol_add ("ftp", "tcp", FTP_PORT, NO, HTLoadFTP, NULL);
1.245 kahan 1667:
1.116 cvs 1668:
1669: /* initialize pipelining */
1.223 cvs 1670: strptr = TtaGetEnvString ("ENABLE_PIPELINING");
1.265 cvs 1671: if (strptr && *strptr && strcasecmp (strptr, "yes"))
1.116 cvs 1672: HTTP_setConnectionMode (HTTP_11_NO_PIPELINING);
1.4 cvs 1673: }
1674:
1.15 cvs 1675: /*----------------------------------------------------------------------
1.17 cvs 1676: AHTNetInit
1677: Reegisters "before" and "after" request filters.
1.15 cvs 1678: ----------------------------------------------------------------------*/
1.4 cvs 1679: static void AHTNetInit (void)
1680: {
1681:
1682: /* Register BEFORE filters
1.74 cvs 1683: ** The BEFORE filters handle proxies, caches, rule files etc.
1684: ** The filters are called in the order by which the are registered
1685: ** Not done automaticly - may be done by application!
1686: */
1.4 cvs 1687:
1.116 cvs 1688: HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, HT_FILTER_LATE);
1689: HTNet_addBefore (HTProxyFilter, NULL, NULL, HT_FILTER_LATE);
1.85 cvs 1690: HTHost_setActivateRequestCallback (AHTOpen_file);
1.4 cvs 1691:
1692: /* register AFTER filters
1.74 cvs 1693: ** The AFTER filters handle error messages, logging, redirection,
1694: ** authentication etc.
1695: ** The filters are called in the order by which the are registered
1696: ** Not done automaticly - may be done by application!
1697: */
1.4 cvs 1698:
1.223 cvs 1699: HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS, HT_FILTER_MIDDLE);
1700: HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_REAUTH, HT_FILTER_MIDDLE);
1701: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT, HT_FILTER_MIDDLE);
1702: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_FOUND, HT_FILTER_MIDDLE);
1703: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_SEE_OTHER, HT_FILTER_MIDDLE);
1704: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT, HT_FILTER_MIDDLE);
1705: HTNet_addAfter (HTAuthInfoFilter, "http://*", NULL, HT_ALL, HT_FILTER_MIDDLE);
1706: HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY, HT_FILTER_MIDDLE);
1.151 cvs 1707: #ifdef AMAYA_LOST_UPDATE
1.223 cvs 1708: HTNet_addAfter (precondition_handler, NULL, NULL, HT_PRECONDITION_FAILED, HT_FILTER_MIDDLE);
1.151 cvs 1709: #endif /* AMAYA_LOST_UPDATE */
1.111 cvs 1710: #ifndef _WINDOWS
1.223 cvs 1711: HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.111 cvs 1712: #endif /* !_WINDOWS */
1.116 cvs 1713: /**** for later ?? ****/
1714: /* HTNet_addAfter(HTInfoFilter, NULL, NULL, HT_ALL, HT_FILTER_LATE); */
1.245 kahan 1715: /* @@ JK: Filters for doing ftp authentication */
1716: HTNet_addAfter (HTAuthFilter, "ftp://*", NULL, HT_NO_ACCESS, HT_FILTER_MIDDLE);
1717: HTNet_addAfter (HTAuthFilter, "ftp://*", NULL, HT_REAUTH, HT_FILTER_MIDDLE);
1.51 cvs 1718: /* handles all errors */
1.116 cvs 1719: HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4 cvs 1720: }
1721:
1.15 cvs 1722: /*----------------------------------------------------------------------
1.17 cvs 1723: AHTAlertInit
1724: Register alert messages and their callbacks.
1.15 cvs 1725: ----------------------------------------------------------------------*/
1726: static void AHTAlertInit (void)
1727: {
1728: HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.120 cvs 1729: #ifdef __WINDOWS
1.70 cvs 1730: HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
1.116 cvs 1731: #endif /* _WINDOWS */
1.15 cvs 1732: HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48 cvs 1733: HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG)); /* process all messages except debug ones*/
1.15 cvs 1734: HTAlert_add (AHTConfirm, HT_A_CONFIRM);
1735: HTAlert_add (AHTPrompt, HT_A_PROMPT);
1736: HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
1.116 cvs 1737: }
1738:
1.118 cvs 1739: #ifdef AMAYA_WWW_CACHE
1740: /*----------------------------------------------------------------------
1.128 cvs 1741: RecCleanCache
1.118 cvs 1742: Clears an existing cache directory
1743: ----------------------------------------------------------------------*/
1.265 cvs 1744: static void RecCleanCache (char *dirname)
1745: {
1.128 cvs 1746: #ifdef _WINDOWS
1.223 cvs 1747: HANDLE hFindFile;
1748: ThotBool status;
1.128 cvs 1749: WIN32_FIND_DATA ffd;
1.290 vatton 1750: char t_dir [MAX_LENGTH];
1751: char *ptr;
1.128 cvs 1752:
1.130 cvs 1753: /* create a t_dir name to start searching for files */
1.265 cvs 1754: if ((strlen (dirname) + 10) > MAX_LENGTH)
1.128 cvs 1755: /* ERROR: directory name is too big */
1756: return;
1757:
1.265 cvs 1758: strcpy (t_dir, dirname);
1.130 cvs 1759: /* save the end of the dirname. We'll use it to make
1760: a complete pathname when erasing files */
1.265 cvs 1761: ptr = &t_dir[strlen (t_dir)];
1762: strcat (t_dir, "*");
1.128 cvs 1763:
1.130 cvs 1764: hFindFile = FindFirstFile (t_dir, &ffd);
1.128 cvs 1765:
1766: if (hFindFile == INVALID_HANDLE_VALUE)
1767: /* nothing to erase? */
1768: return;
1769:
1770: status = TRUE;
1.130 cvs 1771: while (status)
1772: {
1773: if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1.133 cvs 1774: {
1775: /* it's a directory, erase it recursively */
1.265 cvs 1776: if (strcmp (ffd.cFileName, "..") && strcmp (ffd.cFileName, "."))
1.133 cvs 1777: {
1.265 cvs 1778: strcpy (ptr, ffd.cFileName);
1.272 cvs 1779: strcat (ptr, DIR_STR);
1.133 cvs 1780: RecCleanCache (t_dir);
1.274 cvs 1781: rmdir (t_dir);
1.133 cvs 1782: }
1783: }
1784: else
1.130 cvs 1785: {
1.133 cvs 1786: /* it's a file, erase it */
1.265 cvs 1787: strcpy (ptr, ffd.cFileName);
1.133 cvs 1788: TtaFileUnlink (t_dir);
1.130 cvs 1789: }
1790: status = FindNextFile (hFindFile, &ffd);
1791: }
1.128 cvs 1792: FindClose (hFindFile);
1793:
1794: #else /* _WINDOWS */
1.118 cvs 1795: DIR *dp;
1796: struct stat st;
1797: #ifdef HAVE_DIRENT_H
1798: struct dirent *d;
1799: #else
1800: struct direct *d;
1801: #endif /* HAVE_DIRENT_H */
1.128 cvs 1802: char filename[BUFSIZ+1];
1.118 cvs 1803:
1804: if ((dp = opendir (dirname)) == NULL)
1805: {
1806: /* @@@ we couldn't open the directory ... we need some msg */
1807: perror (dirname);
1.176 cvs 1808: return;
1.118 cvs 1809: }
1810:
1811: while ((d = readdir (dp)) != NULL)
1812: {
1813: /* skip the UNIX . and .. links */
1814: if (!strcmp (d->d_name, "..")
1815: || !strcmp (d->d_name, "."))
1816: continue;
1817:
1.188 cvs 1818: sprintf (filename, "%s%c%s", dirname, DIR_SEP, d->d_name);
1.118 cvs 1819: if (lstat (filename, &st) < 0 )
1820: {
1821: /* @@2 need some error message */
1822: perror (filename);
1823: continue;
1824: }
1825:
1826: switch (st.st_mode & S_IFMT)
1827: {
1828: case S_IFDIR:
1829: /* if we find a directory, we erase it, recursively */
1.128 cvs 1830: strcat (filename, DIR_STR);
1831: RecCleanCache (filename);
1832: rmdir (filename);
1.118 cvs 1833: break;
1834: case S_IFLNK:
1835: /* skip any links we find */
1836: continue;
1837: break;
1838: default:
1839: /* erase the filename */
1840: TtaFileUnlink (filename);
1841: break;
1842: }
1843: }
1844: closedir (dp);
1.265 cvs 1845: #endif /* _WINDOWS */
1.118 cvs 1846: }
1847: #endif /* AMAYA_WWW_CACHE */
1848:
1.116 cvs 1849: /*----------------------------------------------------------------------
1.265 cvs 1850: libwww_CleanCache
1851: frontend to the recursive cache cleaning function
1852: ----------------------------------------------------------------------*/
1853: void libwww_CleanCache (void)
1854: {
1855: #ifdef AMAYA_WWW_CACHE
1856: char * real_dir;
1857: char* cache_dir;
1858: char* tmp;
1859: int cache_size;
1860: int cache_expire;
1861: int cache_disconnect;
1862: ThotBool error;
1863: char * ptr;
1864:
1865: if (!HTCacheMode_enabled ())
1866: /* don't do anything if we're not using a cache */
1867: return;
1868: /* temporarily close down the cache, purge it, then restart */
1869: tmp = HTCacheMode_getRoot ();
1870: /* don't do anything if we don't have a valid cache dir */
1871: if (!tmp || *tmp == EOS)
1872: return;
1873: cache_dir = TtaStrdup (tmp);
1874: HT_FREE (tmp);
1875: cache_size = HTCacheMode_maxSize ();
1876: cache_expire = HTCacheMode_expires ();
1877: cache_disconnect = HTCacheMode_disconnected ();
1878:
1879: /* get something we can work on :) */
1880: tmp = HTWWWToLocal (cache_dir, "file:", NULL);
1881: real_dir = TtaGetMemory (strlen (tmp) + 20);
1.273 cvs 1882: strcpy (real_dir, tmp);
1.265 cvs 1883: HT_FREE (tmp);
1884:
1885: /* safeguard... abort the operation if cache_dir doesn't end with
1886: CACHE_DIR_NAME */
1887: error = TRUE;
1888: ptr = strstr (real_dir, CACHE_DIR_NAME);
1889: if (ptr && *ptr && !strcasecmp (ptr, CACHE_DIR_NAME))
1890: error = FALSE;
1891: if (error)
1892: return;
1893:
1894: /* remove the concurrent cache lock */
1895: #ifdef DEBUG_LIBWWW
1896: fprintf (stderr, "Clearing the cache lock\n");
1897: #endif /* DEBUG_LIBWWW */
1898: clear_cachelock ();
1899: HTCacheTerminate ();
1900: HTCacheMode_setEnabled (FALSE);
1901:
1902: RecCleanCache (real_dir);
1903:
1904: HTCacheMode_setExpires (cache_expire);
1905: HTCacheMode_setDisconnected (cache_disconnect);
1906: HTCacheInit (cache_dir, cache_size);
1907: /* set a new concurrent cache lock */
1908: strcat (real_dir, ".lock");
1909: if (set_cachelock (real_dir) == -1)
1910: /* couldn't open the .lock file, so, we close the cache to
1911: be in the safe side */
1912: {
1913: #ifdef DEBUG_LIBWWW
1914: fprintf (stderr, "couldnt set the cache lock\n");
1915: #endif /* DEBUG_LIBWWW */
1916: HTCacheTerminate ();
1917: HTCacheMode_setEnabled (FALSE);
1918: }
1919: #ifdef DEBUG_LIBWWW
1920: fprintf (stderr, "set a cache lock\n");
1921: #endif /* DEBUG_LIBWWW */
1922: TtaFreeMemory (real_dir);
1923: TtaFreeMemory (cache_dir);
1924: #endif /* AMAYA_WWW_CACHE */
1925: }
1926:
1927: /*----------------------------------------------------------------------
1.116 cvs 1928: CacheInit
1929: Reads the cache settings from the thot.ini file.
1930: ----------------------------------------------------------------------*/
1931: static void CacheInit (void)
1932: {
1933: #ifndef AMAYA_WWW_CACHE
1934: HTCacheMode_setEnabled (NO);
1935:
1936: #else /* AMAYA_WWW_CACHE */
1.265 cvs 1937: char *strptr;
1938: char *real_dir = NULL;
1939: char *cache_lockfile;
1940: char *cache_dir = NULL;
1.223 cvs 1941: int cache_size;
1942: int cache_entry_size;
1943: ThotBool cache_enabled;
1944: ThotBool cache_locked;
1945: ThotBool tmp_bool;
1.130 cvs 1946:
1.185 cvs 1947: int i;
1948:
1.116 cvs 1949: /* activate cache? */
1.203 cvs 1950: strptr = TtaGetEnvString ("ENABLE_CACHE");
1.265 cvs 1951: if (strptr && *strptr && strcasecmp (strptr, "yes"))
1.130 cvs 1952: cache_enabled = NO;
1.116 cvs 1953: else
1.130 cvs 1954: cache_enabled = YES;
1.116 cvs 1955:
1.151 cvs 1956: /* cache protected documents? */
1.203 cvs 1957: strptr = TtaGetEnvString ("CACHE_PROTECTED_DOCS");
1.265 cvs 1958: if (strptr && *strptr && !strcasecmp (strptr, "yes"))
1.151 cvs 1959: HTCacheMode_setProtected (YES);
1960: else
1961: HTCacheMode_setProtected (NO);
1962:
1.116 cvs 1963: /* get the cache dir (or use a default one) */
1.203 cvs 1964: strptr = TtaGetEnvString ("CACHE_DIR");
1.116 cvs 1965: if (strptr && *strptr)
1966: {
1.265 cvs 1967: real_dir = TtaGetMemory (strlen (strptr) + strlen (CACHE_DIR_NAME) + 20);
1968: strcpy (real_dir, strptr);
1.272 cvs 1969: if (*(real_dir + strlen (real_dir) - 1) != DIR_SEP)
1970: strcat (real_dir, DIR_STR);
1.116 cvs 1971: }
1972: else
1973: {
1.265 cvs 1974: real_dir = TtaGetMemory (strlen (TempFileDirectory) + strlen (CACHE_DIR_NAME) + 20);
1.272 cvs 1975: sprintf (real_dir, "%s%s", TempFileDirectory, CACHE_DIR_NAME);
1.185 cvs 1976: }
1977:
1978: /* compatiblity with previous versions of Amaya: does real_dir
1979: include CACHE_DIR_NAME? If not, add it */
1.265 cvs 1980: strptr = strstr (real_dir, CACHE_DIR_NAME);
1.185 cvs 1981: if (!strptr)
1.265 cvs 1982: strcat (real_dir, CACHE_DIR_NAME);
1.185 cvs 1983: else
1984: {
1.265 cvs 1985: i = strlen (CACHE_DIR_NAME);
1.272 cvs 1986: if (strptr[i] != EOS)
1.265 cvs 1987: strcat (real_dir, CACHE_DIR_NAME);
1.116 cvs 1988: }
1.185 cvs 1989:
1.211 cvs 1990: /* convert the local cache dir into a file URL, as expected by
1991: libwww */
1.265 cvs 1992: cache_dir = HTLocalToWWW (real_dir, "file:");
1.116 cvs 1993: /* get the cache size (or use a default one) */
1.203 cvs 1994: strptr = TtaGetEnvString ("CACHE_SIZE");
1.116 cvs 1995: if (strptr && *strptr)
1.288 vatton 1996: cache_size = atoi (strptr);
1.116 cvs 1997: else
1998: cache_size = DEFAULT_CACHE_SIZE;
1.176 cvs 1999: /* get the max cached file size (or use a default one) */
1.223 cvs 2000: if (!TtaGetEnvInt ("MAX_CACHE_ENTRY_SIZE", &cache_entry_size))
1.176 cvs 2001: cache_entry_size = DEFAULT_MAX_CACHE_ENTRY_SIZE;
1.130 cvs 2002: if (cache_enabled)
1.116 cvs 2003: {
2004: /* how to remove the lock? force remove it? */
1.265 cvs 2005: cache_lockfile = TtaGetMemory (strlen (real_dir) + 20);
2006: strcpy (cache_lockfile, real_dir);
2007: strcat (cache_lockfile, ".lock");
1.130 cvs 2008: cache_locked = FALSE;
1.223 cvs 2009: if (TtaFileExist (cache_lockfile) && !(cache_locked = test_cachelock (cache_lockfile)))
1.116 cvs 2010: {
1.130 cvs 2011: #ifdef DEBUG_LIBWWW
2012: fprintf (stderr, "found a stale cache, removing it\n");
2013: #endif /* DEBUG_LIBWWW */
2014: /* remove the lock and clean the cache (the clean cache
2015: will remove all, making the following call unnecessary */
2016: /* little trick to win some memory */
1.265 cvs 2017: strptr = strrchr (cache_lockfile, '.');
1.272 cvs 2018: *strptr = EOS;
1.130 cvs 2019: RecCleanCache (cache_lockfile);
1.265 cvs 2020: *strptr = '.';
1.116 cvs 2021: }
2022:
1.130 cvs 2023: if (!cache_locked)
2024: {
2025: /* initialize the cache if there's no other amaya
2026: instance running */
1.177 cvs 2027: HTCacheMode_setMaxCacheEntrySize (cache_entry_size);
1.223 cvs 2028: if (TtaGetEnvBoolean ("CACHE_EXPIRE_IGNORE", &tmp_bool) && tmp_bool)
1.177 cvs 2029: HTCacheMode_setExpires (HT_EXPIRES_IGNORE);
2030: else
2031: HTCacheMode_setExpires (HT_EXPIRES_AUTO);
1.223 cvs 2032: TtaGetEnvBoolean ("CACHE_DISCONNECTED_MODE", &tmp_bool);
1.177 cvs 2033: if (tmp_bool)
2034: HTCacheMode_setDisconnected (HT_DISCONNECT_NORMAL);
2035: else
2036: HTCacheMode_setDisconnected (HT_DISCONNECT_NONE);
1.211 cvs 2037: if (HTCacheInit (cache_dir, cache_size))
1.130 cvs 2038: {
1.188 cvs 2039: if (set_cachelock (cache_lockfile) == -1)
2040: /* couldn't open the .lock file, so, we close the cache to
2041: be in the safe side */
2042: {
2043: HTCacheTerminate ();
2044: HTCacheMode_setEnabled (FALSE);
2045: #ifdef DEBUG_LIBWWW
2046: fprintf (stderr, "couldnt set the cache lock\n");
2047: #endif /* DEBUG_LIBWWW */
2048: }
1.130 cvs 2049: #ifdef DEBUG_LIBWWW
1.188 cvs 2050: else
2051: fprintf (stderr, "created the cache lock\n");
1.130 cvs 2052: #endif /* DEBUG_LIBWWW */
2053: }
1.188 cvs 2054: else
2055: {
1.130 cvs 2056: #ifdef DEBUG_LIBWWW
1.188 cvs 2057: fprintf (stderr, "couldn't create the cache\n");
2058: #endif /* DEBUG_LIBWWW */
2059: HTCacheTerminate ();
2060: }
1.130 cvs 2061: }
2062: else
2063: {
1.185 cvs 2064: HTCacheMode_setEnabled (FALSE);
1.130 cvs 2065: #ifdef DEBUG_LIBWWW
2066: fprintf (stderr, "lock detected, starting libwww without a cache/n");
2067: #endif /* DEBUG_LIBWWW */
2068: }
2069: TtaFreeMemory (cache_lockfile);
1.116 cvs 2070: }
2071: else
1.185 cvs 2072: {
2073: HTCacheMode_setEnabled (FALSE);
2074: }
1.116 cvs 2075: if (cache_dir)
1.211 cvs 2076: HT_FREE (cache_dir);
1.189 cvs 2077: if (real_dir)
2078: TtaFreeMemory (real_dir);
1.185 cvs 2079: /* warn the user if the cache isn't active */
2080: if (cache_enabled && !HTCacheMode_enabled ())
1.291 kahan 2081: {
2082: ThotBool warn_user;
2083:
2084: TtaGetEnvBoolean ("WARN_NO_CACHE", &warn_user);
2085: if (warn_user)
2086: InitInfo ("Cache", TtaGetMessage (AMAYA, AM_CANT_CREATE_CACHE));
2087: }
1.116 cvs 2088: #endif /* AMAYA_WWW_CACHE */
1.15 cvs 2089: }
2090:
2091: /*----------------------------------------------------------------------
1.97 cvs 2092: ProxyInit
2093: Reads any proxies settings which may be declared as environmental
2094: variables or in the thot.ini file. The former overrides the latter.
2095: ----------------------------------------------------------------------*/
2096: static void ProxyInit (void)
2097: {
1.265 cvs 2098: char *strptr;
2099: char *name;
2100: char *tmp = NULL;
2101: char *strptrA;
1.223 cvs 2102: ThotBool proxy_is_onlyproxy;
1.97 cvs 2103:
2104: /* get the proxy settings from the thot.ini file */
1.203 cvs 2105: strptr = TtaGetEnvString ("HTTP_PROXY");
1.228 cvs 2106: if (strptr && *strptr)
2107: {
1.265 cvs 2108: tmp = (char *) TtaGetMemory (strlen (strptr) + 1);
2109: strcpy (tmp, strptr);
1.223 cvs 2110:
1.228 cvs 2111: /* does the proxy env string has an "http://" prefix? */
1.265 cvs 2112: if (!strncasecmp (strptr, "http://", 7))
1.223 cvs 2113: HTProxy_add ("http", tmp);
1.228 cvs 2114: else
2115: {
1.265 cvs 2116: strptrA = (char *) TtaGetMemory (strlen (strptr) + 9);
1.228 cvs 2117: strcpy (strptrA, "http://");
2118: strcat (strptrA, tmp);
2119: HTProxy_add ("http", strptrA);
2120: TtaFreeMemory (strptrA);
2121: }
2122: TtaFreeMemory (tmp);
2123: }
1.216 cvs 2124:
1.97 cvs 2125: /* get the no_proxy settings from the thot.ini file */
1.212 cvs 2126: strptr = TtaGetEnvString ("PROXYDOMAIN");
1.228 cvs 2127: if (strptr && *strptr)
2128: {
1.265 cvs 2129: strptrA = (char *) TtaGetMemory (strlen (strptr) + 1);
2130: tmp = strptrA;
2131: strcpy (tmp, strptr);
1.228 cvs 2132: /* as HTNextField changes the ptr we pass as an argument, we'll
1.265 cvs 2133: work with another variable, so that we can free the tmp
1.228 cvs 2134: block later on */
2135: while ((name = HTNextField (&tmp)) != NULL)
2136: {
2137: char* portstr = strchr (name, ':');
2138: unsigned port=0;
1.265 cvs 2139: if (portstr)
2140: {
2141: *portstr++ = EOS;
2142: if (*portstr)
2143: port = (unsigned) atoi (portstr);
2144: }
1.228 cvs 2145: /* Register it for all access methods */
2146: HTNoProxy_add (name, NULL, port);
2147: }
2148: TtaFreeMemory (strptrA);
2149: }
2150:
1.212 cvs 2151: /* how should we interpret the proxy domain list? */
1.291 kahan 2152:
1.212 cvs 2153: TtaGetEnvBoolean ("PROXYDOMAIN_IS_ONLYPROXY", &proxy_is_onlyproxy);
2154: HTProxy_setNoProxyIsOnlyProxy (proxy_is_onlyproxy);
2155:
2156: /* use libwww's routine to get all proxy settings from the environment */
1.97 cvs 2157: HTProxy_getEnvVar ();
2158: }
2159:
1.207 cvs 2160: /*----------------------------------------------------------------------
1.17 cvs 2161: AHTProfile_newAmaya
2162: creates the Amaya client profile for libwww.
1.15 cvs 2163: ----------------------------------------------------------------------*/
1.265 cvs 2164: static void AHTProfile_newAmaya (char *AppName, char *AppVersion)
1.4 cvs 2165: {
1.265 cvs 2166: char *strptr;
1.158 cvs 2167:
1.4 cvs 2168: /* If the Library is not already initialized then do it */
1.257 kahan 2169: if (!HTLib_isInitialized ())
1.273 cvs 2170: HTLibInit (AppName, AppVersion);
1.4 cvs 2171:
2172: if (!converters)
2173: converters = HTList_new ();
1.27 cvs 2174: if (!acceptTypes)
2175: acceptTypes = HTList_new ();
1.193 cvs 2176: if (!acceptLanguages)
2177: acceptLanguages = HTList_new ();
1.151 cvs 2178: if (!transfer_encodings)
2179: transfer_encodings = HTList_new ();
2180: if (!content_encodings)
2181: content_encodings = HTList_new ();
1.4 cvs 2182:
1.169 cvs 2183: /* inhibits libwww's automatic file_suffix_binding */
2184: HTFile_doFileSuffixBinding (FALSE);
2185:
1.4 cvs 2186: /* Register the default set of transport protocols */
2187: HTTransportInit ();
2188:
2189: /* Register the default set of application protocol modules */
2190: AHTProtocolInit ();
2191:
1.116 cvs 2192: /* Register the default set of messages and dialog functions */
2193: AHTAlertInit ();
2194: HTAlert_setInteractive (YES);
2195:
2196: #ifdef AMAYA_WWW_CACHE
2197: /* Enable the persistent cache */
2198: CacheInit ();
2199: #else
2200: HTCacheMode_setEnabled (NO);
2201: #endif /* AMAYA_WWW_CACHE */
1.4 cvs 2202:
2203: /* Register the default set of BEFORE and AFTER filters */
2204: AHTNetInit ();
2205:
2206: /* Set up the default set of Authentication schemes */
1.223 cvs 2207: HTAA_newModule ("basic", HTBasic_generate, HTBasic_parse, NULL, HTBasic_delete);
1.158 cvs 2208: /* activate MDA by defaul */
1.203 cvs 2209: strptr = TtaGetEnvString ("ENABLE_MDA");
1.265 cvs 2210: if (!strptr || (strptr && *strptr && strcasecmp (strptr, "no")))
1.223 cvs 2211: HTAA_newModule ("digest", HTDigest_generate, HTDigest_parse, HTDigest_updateInfo, HTDigest_delete);
1.257 kahan 2212:
1.97 cvs 2213: /* Get any proxy settings */
2214: ProxyInit ();
1.4 cvs 2215:
1.207 cvs 2216: /* Set up the domains where we accept a redirect on PUT */
2217: SafePut_init ();
2218:
1.4 cvs 2219: /* Register the default set of converters */
2220: AHTConverterInit (converters);
1.151 cvs 2221: HTFormat_setConversion (converters);
1.27 cvs 2222: AHTAcceptTypesInit (acceptTypes);
1.193 cvs 2223: AHTAcceptLanguagesInit (acceptLanguages);
1.4 cvs 2224:
2225: /* Register the default set of transfer encoders and decoders */
1.151 cvs 2226: HTTransferEncoderInit (transfer_encodings);
1.190 cvs 2227: HTFormat_setTransferCoding (transfer_encodings);
2228: /* Register the default set of content encoders and decoders */
2229: HTContentEncoderInit (content_encodings);
2230: /* ignore all other encoding formats (or libwww will send them
1.181 cvs 2231: thru a blackhole otherwise */
2232: HTCoding_add (content_encodings, "*", NULL, HTIdentityCoding, 1.0);
1.151 cvs 2233: if (HTList_count(content_encodings) > 0)
2234: HTFormat_setContentCoding(content_encodings);
2235: else
2236: {
2237: HTList_delete(content_encodings);
2238: content_encodings = NULL;
2239: }
1.4 cvs 2240:
2241: /* Register the default set of MIME header parsers */
1.74 cvs 2242: HTMIMEInit (); /* must be called again for language selector */
1.4 cvs 2243:
2244: /* Register the default set of Icons for directory listings */
1.27 cvs 2245: /*HTIconInit(NULL); *//* experimental */
1.4 cvs 2246: }
2247:
1.5 cvs 2248: /*----------------------------------------------------------------------
1.17 cvs 2249: AHTProfile_delete
2250: deletes the Amaya client profile.
1.5 cvs 2251: ----------------------------------------------------------------------*/
1.4 cvs 2252: static void AHTProfile_delete (void)
2253: {
1.22 cvs 2254: /* free the Amaya global context */
1.151 cvs 2255: /* Clean up all the registred converters */
2256: HTFormat_deleteAll ();
2257: if (acceptTypes)
1.74 cvs 2258: HTConversion_deleteAll (acceptTypes);
1.193 cvs 2259: if (acceptLanguages)
2260: HTLanguage_deleteAll (acceptLanguages);
1.151 cvs 2261:
1.209 cvs 2262: StopAllRequests (1);
1.74 cvs 2263: HTList_delete (Amaya->docid_status);
2264: HTList_delete (Amaya->reqlist);
2265: TtaFreeMemory (Amaya);
1.61 cvs 2266:
1.120 cvs 2267: #ifdef _WINDOWS
1.209 cvs 2268: if (HTLib_isInitialized ())
1.151 cvs 2269: HTEventTerminate ();
1.198 cvs 2270: #endif /* _WINDOWS; */
1.74 cvs 2271:
1.151 cvs 2272: /* Clean up the persistent cache (if any) */
1.116 cvs 2273: #ifdef AMAYA_WWW_CACHE
1.151 cvs 2274: clear_cachelock ();
2275: HTCacheTerminate ();
1.116 cvs 2276: #endif /* AMAYA_WWW_CACHE */
1.217 cvs 2277:
1.218 cvs 2278: /* call remove functions that are not called automatically by libwww */
2279: HTNetCall_deleteBeforeAll (HTNet_before ());
2280: HTNetCall_deleteAfterAll (HTNet_after ());
2281: HTAA_deleteAllModules ();
2282: HTAlertCall_deleteAll (HTAlert_global () );
2283: HTAlert_setGlobal ((HTList *) NULL);
1.259 cvs 2284: HTTimer_deleteAll ();
1.249 kahan 2285: HTEventList_unregisterAll ();
1.218 cvs 2286: /* these two functions are broken, so we can't call them right now
2287: HTHeader_deleteAll ();
2288: HTTimer_deleteAll ();
2289: */
2290: HTTransport_deleteAll ();
1.217 cvs 2291: /* remove bindings between suffixes, media types*/
2292: HTBind_deleteAll ();
1.151 cvs 2293: /* Terminate libwww */
2294: HTLibTerminate ();
1.4 cvs 2295: }
2296:
1.5 cvs 2297: /*----------------------------------------------------------------------
1.116 cvs 2298: AmayacontextInit
2299: initializes an internal Amaya context for our libwww interface
2300: ----------------------------------------------------------------------*/
1.265 cvs 2301: static void AmayaContextInit ()
1.116 cvs 2302: {
1.140 cvs 2303: AmayaAlive_flag = TRUE;
1.116 cvs 2304: /* Initialization of the global context */
2305: Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
2306: Amaya->reqlist = HTList_new ();
2307: Amaya->docid_status = HTList_new ();
2308: Amaya->open_requests = 0;
2309: }
2310:
2311: /*----------------------------------------------------------------------
1.17 cvs 2312: QueryInit
2313: initializes the libwww interface
1.5 cvs 2314: ----------------------------------------------------------------------*/
1.265 cvs 2315: void QueryInit ()
1.4 cvs 2316: {
1.265 cvs 2317: char *strptr;
2318: int tmp_i;
2319: long tmp_l;
1.4 cvs 2320:
1.116 cvs 2321: AmayaContextInit ();
1.4 cvs 2322: AHTProfile_newAmaya (HTAppName, HTAppVersion);
1.140 cvs 2323: CanDoStop_set (TRUE);
1.214 cvs 2324: UserAborted_flag = FALSE;
1.4 cvs 2325:
1.116 cvs 2326: #ifdef _WINDOWS
1.125 cvs 2327: HTEventInit ();
1.116 cvs 2328: #endif /* _WINDOWS */
1.72 cvs 2329:
1.123 cvs 2330: #ifndef _WINDOWS
1.116 cvs 2331: HTEvent_setRegisterCallback ((void *) AHTEvent_register);
2332: HTEvent_setUnregisterCallback ((void *) AHTEvent_unregister);
1.120 cvs 2333: HTTimer_registerSetTimerCallback ((void *) AMAYA_SetTimer);
2334: HTTimer_registerDeleteTimerCallback ((void *) AMAYA_DeleteTimer);
1.116 cvs 2335: #endif /* !_WINDOWS */
1.190 cvs 2336:
1.213 cvs 2337: #ifdef HTDEBUG
1.247 kahan 2338: /* an undocumented option for being able to generate an HTTP protocol
2339: trace. The flag can take values from 1-10, which are interpreted as
2340: different kinds of debug traces. Value 99 means a complete trace.
2341: 0 or other values means No Debug.1 No Debug (default), up to Full debug, respectively. */
1.238 cvs 2342: if (TtaGetEnvInt ("ENABLE_LIBWWW_DEBUG", &tmp_i))
1.247 kahan 2343: {
2344: switch (tmp_i)
2345: {
2346: case 1:
2347: WWW_TraceFlag = SHOW_URI_TRACE;
2348: break;
2349: case 2:
2350: WWW_TraceFlag = SHOW_BIND_TRACE;
2351: break;
2352: case 3:
2353: WWW_TraceFlag = SHOW_THREAD_TRACE;
2354: break;
2355: case 4:
2356: WWW_TraceFlag = SHOW_STREAM_TRACE;
2357: break;
2358: case 5:
2359: WWW_TraceFlag = SHOW_PROTOCOL_TRACE;
2360: break;
2361: case 6:
2362: WWW_TraceFlag = SHOW_MEM_TRACE;
2363: break;
2364: case 7:
2365: WWW_TraceFlag = SHOW_URI_TRACE;
2366: break;
2367: case 8:
2368: WWW_TraceFlag = SHOW_AUTH_TRACE;
2369: break;
2370: case 9:
2371: WWW_TraceFlag = SHOW_ANCHOR_TRACE;
2372: break;
2373: case 10 :
2374: WWW_TraceFlag = SHOW_CORE_TRACE;
2375: break;
1.251 kahan 2376: case 11 :
2377: WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_ANCHOR_TRACE | SHOW_AUTH_TRACE | SHOW_URI_TRACE | SHOW_THREAD_TRACE | SHOW_STREAM_TRACE;
2378: break;
1.247 kahan 2379: case 99 :
2380: WWW_TraceFlag = SHOW_ALL_TRACE;
2381: break;
2382: default:
2383: WWW_TraceFlag = 0;
2384: break;
2385: }
1.248 kahan 2386: if (WWW_TraceFlag)
2387: {
2388: /* Trace activation (for debugging) */
1.265 cvs 2389: char *s, *tmp;
1.248 kahan 2390: s = TtaGetEnvString ("APP_TMPDIR");
1.265 cvs 2391: tmp = TtaGetMemory (strlen (s) + sizeof ("/libwww.log") + 1);
2392: strcpy (tmp, s);
2393: strcat (tmp, "/libwww.log");
2394: trace_fp = fopen (tmp, "ab");
1.248 kahan 2395: TtaFreeMemory (tmp);
2396: if (trace_fp)
2397: HTTrace_setCallback(LineTrace);
2398: }
1.247 kahan 2399: }
1.208 cvs 2400: else
2401: WWW_TraceFlag = 0;
1.213 cvs 2402: #endif /* HTDEBUG */
1.208 cvs 2403:
1.74 cvs 2404: #ifdef DEBUG_LIBWWW
1.116 cvs 2405: /* forwards error messages to our own function */
2406: WWW_TraceFlag = THD_TRACE;
1.138 cvs 2407: HTTrace_setCallback(LineTrace);
1.248 kahan 2408:
1.148 cvs 2409: /***
1.138 cvs 2410: WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | SHOW_PROTOCOL_TRACE;
2411: ***/
1.152 cvs 2412: #endif
1.247 kahan 2413:
2414: TtaGetEnvBoolean ("ENABLE_FTP", &FTPURL_flag);
1.152 cvs 2415:
1.148 cvs 2416: /* Setting up different network parameters */
1.151 cvs 2417:
1.148 cvs 2418: /* Maximum number of simultaneous open sockets */
1.203 cvs 2419: strptr = TtaGetEnvString ("MAX_SOCKET");
1.151 cvs 2420: if (strptr && *strptr)
1.288 vatton 2421: tmp_i = atoi (strptr);
1.151 cvs 2422: else
2423: tmp_i = DEFAULT_MAX_SOCKET;
2424: HTNet_setMaxSocket (tmp_i);
2425:
1.148 cvs 2426: /* different network services timeouts */
1.151 cvs 2427: /* dns timeout */
1.203 cvs 2428: strptr = TtaGetEnvString ("DNS_TIMEOUT");
1.151 cvs 2429: if (strptr && *strptr)
1.288 vatton 2430: tmp_i = atoi (strptr);
1.151 cvs 2431: else
2432: tmp_i = DEFAULT_DNS_TIMEOUT;
2433: HTDNS_setTimeout (tmp_i);
2434:
2435: /* persistent connections timeout */
1.203 cvs 2436: strptr = TtaGetEnvString ("PERSIST_CX_TIMEOUT");
1.151 cvs 2437: if (strptr && *strptr)
1.288 vatton 2438: tmp_l = atol (strptr);
1.151 cvs 2439: else
2440: tmp_l = DEFAULT_PERSIST_TIMEOUT;
2441: HTHost_setPersistTimeout (tmp_l);
2442:
1.148 cvs 2443: /* default timeout in ms */
1.203 cvs 2444: strptr = TtaGetEnvString ("NET_EVENT_TIMEOUT");
1.151 cvs 2445: if (strptr && *strptr)
1.288 vatton 2446: tmp_i = atoi (strptr);
1.151 cvs 2447: else
2448: tmp_i = DEFAULT_NET_EVENT_TIMEOUT;
2449: HTHost_setEventTimeout (tmp_i);
2450:
1.173 cvs 2451: HTRequest_setMaxRetry (8);
1.4 cvs 2452: #ifdef CATCH_SIG
1.148 cvs 2453: signal (SIGPIPE, SIG_IGN);
1.4 cvs 2454: #endif
1.15 cvs 2455: }
2456:
2457: /*----------------------------------------------------------------------
1.17 cvs 2458: LoopForStop
1.136 cvs 2459: a copy of the Thop event loop so we can handle the stop button in Unix
2460: and preemptive requests under Windows
1.15 cvs 2461: ----------------------------------------------------------------------*/
2462: static int LoopForStop (AHTReqContext * me)
2463: {
1.136 cvs 2464: #ifdef _WINDOWS
2465: MSG msg;
2466: unsigned long libwww_msg;
2467: HWND old_active_window, libwww_window;
2468: int status_req = HT_OK;
2469:
2470: old_active_window = GetActiveWindow ();
2471: libwww_window = HTEventList_getWinHandle (&libwww_msg);
1.137 cvs 2472:
1.149 cvs 2473: while (me->reqStatus != HT_END && me->reqStatus != HT_ERR
1.253 cvs 2474: && me->reqStatus != HT_ABORT && AmayaIsAlive () &&
2475: GetMessage (&msg, NULL, 0, 0))
2476: {
2477: if (msg.message != WM_QUIT)
2478: TtaHandleOneEvent (&msg);
2479: else
2480: break;
2481: }
1.136 cvs 2482: if (!AmayaIsAlive ())
2483: /* Amaya was killed by one of the callback handlers */
2484: exit (0);
2485: #else /* _WINDOWS */
1.277 kahan 2486: ThotEvent ev;
1.17 cvs 2487: int status_req = HT_OK;
1.15 cvs 2488:
2489: /* to test the async calls */
1.17 cvs 2490: /* Loop while waiting for new events, exists when the request is over */
1.15 cvs 2491: while (me->reqStatus != HT_ABORT &&
2492: me->reqStatus != HT_END &&
1.69 cvs 2493: me->reqStatus != HT_ERR) {
1.116 cvs 2494: if (!AmayaIsAlive ())
1.69 cvs 2495: /* Amaya was killed by one of the callback handlers */
2496: exit (0);
1.277 kahan 2497:
2498: if (TtaFetchOneAvailableEvent (&ev))
2499: TtaHandleOneEvent (&ev);
1.69 cvs 2500: }
1.136 cvs 2501: #endif /* _WINDOWS */
1.69 cvs 2502: switch (me->reqStatus) {
2503: case HT_ERR:
2504: case HT_ABORT:
1.130 cvs 2505: status_req = NO;
1.15 cvs 2506: break;
2507:
1.69 cvs 2508: case HT_END:
1.130 cvs 2509: status_req = YES;
1.15 cvs 2510: break;
2511:
1.69 cvs 2512: default:
1.15 cvs 2513: break;
1.69 cvs 2514: }
1.15 cvs 2515: return (status_req);
1.4 cvs 2516: }
2517:
1.5 cvs 2518: /*----------------------------------------------------------------------
1.15 cvs 2519: QueryClose
1.21 cvs 2520: closes all existing threads, frees all non-automatically deallocated
2521: memory and then ends libwww.
1.5 cvs 2522: ----------------------------------------------------------------------*/
1.116 cvs 2523: void QueryClose ()
1.4 cvs 2524: {
1.24 cvs 2525:
1.140 cvs 2526: AmayaAlive_flag = FALSE;
1.24 cvs 2527:
1.116 cvs 2528: /* remove all the handlers and callbacks that may output a message to
2529: a non-existent Amaya window */
2530: HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
2531: HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
2532: #ifndef _WINDOWS
2533: /** need to erase all existing timers too **/
2534: HTTimer_registerSetTimerCallback (NULL);
2535: HTTimer_registerDeleteTimerCallback (NULL);
2536: #endif /* !_WINDOWS */
2537: HTHost_setActivateRequestCallback (NULL);
2538: Thread_deleteAll ();
1.21 cvs 2539:
1.116 cvs 2540: HTProxy_deleteAll ();
2541: HTNoProxy_deleteAll ();
1.207 cvs 2542: SafePut_delete ();
1.116 cvs 2543: HTGateway_deleteAll ();
2544: AHTProfile_delete ();
1.248 kahan 2545:
2546: /* close the trace file (if it exists) */
2547: if (trace_fp)
2548: fclose (trace_fp);
2549:
1.116 cvs 2550: }
2551:
2552: /*----------------------------------------------------------------------
2553: NextNameValue
2554: ---------------------------------------------------------------------*/
1.265 cvs 2555: static char * NextNameValue (char **pstr, char **name, char **value)
1.116 cvs 2556: {
2557: char * p = *pstr;
2558: char * start = NULL;
2559: if (!pstr || !*pstr) return NULL;
2560:
2561: if (!*p) {
2562: *pstr = p;
2563: *name = NULL;
2564: *value = NULL;
2565: return NULL; /* No field */
2566: }
2567:
2568: /* Now search for the next '&' and ';' delimitators */
2569: start = p;
2570: while (*p && *p != '&' && *p != ';') p++;
2571: if (*p)
2572: *p++ = '\0';
2573: *pstr = p;
2574:
2575: /* Search for the name and value */
2576: *name = start;
2577: p = start;
2578:
2579: while(*p && *p != '=')
2580: p++;
2581: if (*p)
2582: *p++ = '\0';
2583: *value = p;
2584:
2585: return start;
2586: }
2587:
2588: /*----------------------------------------------------------------------
2589: PrepareFormdata
2590: ---------------------------------------------------------------------*/
1.265 cvs 2591: static HTAssocList * PrepareFormdata (char *string)
1.116 cvs 2592: {
1.266 cvs 2593: char *tmp_string, *tmp_string_ptr;
2594: char *name;
2595: char *value;
2596: HTAssocList *formdata;
1.116 cvs 2597:
2598: if (!string)
2599: return NULL;
2600:
2601: /* store the ptr in another variable, as the original address will
2602: change
2603: */
1.236 cvs 2604:
1.265 cvs 2605: tmp_string_ptr = TtaGetMemory (strlen (string) + 1);
1.236 cvs 2606: tmp_string = tmp_string_ptr;
1.273 cvs 2607: strcpy (tmp_string_ptr, string);
1.116 cvs 2608: formdata = HTAssocList_new();
2609:
2610: while (*tmp_string)
2611: {
2612: NextNameValue (&tmp_string, &name, &value);
2613: HTAssocList_addObject(formdata,
2614: name, value);
2615: }
2616:
2617: TtaFreeMemory (tmp_string_ptr);
2618: return formdata;
1.4 cvs 2619: }
2620:
1.236 cvs 2621: /*----------------------------------------------------------------------
2622: ---------------------------------------------------------------------*/
1.216 cvs 2623: void AHTRequest_setCustomAcceptHeader (HTRequest *request, char *value)
2624: {
2625: HTRqHd rqhd = HTRequest_rqHd (request);
2626: rqhd = rqhd & (~HT_C_ACCEPT_TYPE);
2627: HTRequest_setRqHd (request, rqhd);
1.221 cvs 2628: HTRequest_addExtraHeader (request, "Accept", value);
1.216 cvs 2629: }
2630:
1.228 cvs 2631: /*----------------------------------------------------------------------
1.99 cvs 2632: InvokeGetObjectWWW_callback
2633: A simple function to invoke a callback function whenever there's an error
2634: in GetObjectWWW
2635: ---------------------------------------------------------------------*/
1.265 cvs 2636: void InvokeGetObjectWWW_callback (int docid, char *urlName,
2637: char *outputfile, TTcbf *terminate_cbf,
2638: void *context_tcbf, int status)
1.99 cvs 2639: {
2640: if (!terminate_cbf)
2641: return;
2642:
1.116 cvs 2643: (*terminate_cbf) (docid, status, urlName, outputfile,
1.99 cvs 2644: NULL, context_tcbf);
2645: }
2646:
2647:
2648:
1.5 cvs 2649: /*----------------------------------------------------------------------
1.15 cvs 2650: GetObjectWWW
1.17 cvs 2651: this function requests a resource designated by a URLname into a
2652: temporary filename. The download can come from a simple GET operation,
2653: or can come from POSTING/GETTING a form. In the latter
2654: case, the function receives a query string to send to the server.
2655:
1.5 cvs 2656: 4 file retrieval modes are proposed:
2657: AMAYA_SYNC : blocking mode
2658: AMAYA_ISYNC : incremental, blocking mode
2659: AMAYA_ASYNC : non-blocking mode
2660: AMAYA_IASYNC : incremental, non-blocking mode
2661:
2662: In the incremental mode, each time a package arrives, it will be
2663: stored in the temporary file. In addition, if an
2664: incremental_callback function is defined, this function will be
2665: called and handled a copy of the newly received data package.
2666: Finally, if a terminate_callback function is defined, it will be
2667: invoked when the request terminates. The caller of this function
1.4 cvs 2668: can define two different contexts to be passed to the callback
2669: functions.
2670:
2671: When the function is called with the SYNC mode, the function will
2672: return only when the requested file has been loaded.
2673: The ASYNC mode will immediately return after setting up the
2674: call.
2675:
2676: Notes:
2677: At the end of a succesful request, the urlName string contains the
2678: name of the actually retrieved URL. As a URL can change over the time,
2679: (e.g., be redirected elsewhere), it is advised that the function
1.17 cvs 2680: caller verify the value of the urlName variable at the end of
1.4 cvs 2681: a request.
2682:
2683: Inputs:
2684: - docid Document identifier for the set of objects being
2685: retrieved.
2686: - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
2687: - outputfile A pointer to an empty string of MAX_URL_LENGTH.
2688: - mode The retrieval mode.
2689: - incremental_cbf
2690: - context_icbf
2691: Callback and context for the incremental modes
2692: - terminate_cbf
2693: - context_icbf
2694: Callback and context for a terminate handler
1.17 cvs 2695: -error_html if TRUE, then display any server error message as an
2696: HTML document.
1.88 cvs 2697: - content_type a string
2698:
1.4 cvs 2699: Outputs:
2700: - urlName The URL that was retrieved
2701: - outputfile The name of the temporary file which holds the
2702: retrieved data. (Only in case of success)
1.88 cvs 2703: - if content_type wasn't NULL, it will contain a copy of the parameter
2704: sent in the HTTP answer
1.4 cvs 2705: Returns:
2706: HT_ERROR
2707: HT_OK
1.5 cvs 2708:
2709: ----------------------------------------------------------------------*/
1.265 cvs 2710: int GetObjectWWW (int docid, char *urlName, char *formdata,
2711: char *outputfile, int mode, TIcbf *incremental_cbf,
2712: void *context_icbf, TTcbf *terminate_cbf,
2713: void *context_tcbf, ThotBool error_html, char *content_type)
1.4 cvs 2714: {
2715: AHTReqContext *me;
1.286 kahan 2716: char * ref;
2717: char * tmp;
2718: char * esc_url;
1.107 cvs 2719: int status, l;
1.114 cvs 2720: int tempsubdir;
1.191 cvs 2721: ThotBool bool_tmp;
1.7 cvs 2722:
1.116 cvs 2723: if (urlName == NULL || docid == 0 || outputfile == NULL)
2724: {
2725: /* no file to be loaded */
2726: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.69 cvs 2727:
1.116 cvs 2728: if (error_html)
1.69 cvs 2729: /* so we can show the error message */
2730: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116 cvs 2731: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
2732: context_tcbf, HT_ERROR);
2733: return HT_ERROR;
2734: }
1.7 cvs 2735:
1.159 cvs 2736: /* if it's a 'docImage', we have already downloaded it */
1.265 cvs 2737: if (!strncmp ("internal:", urlName, 9))
1.159 cvs 2738: {
1.265 cvs 2739: strcpy (outputfile, urlName);
1.159 cvs 2740: InvokeGetObjectWWW_callback (docid, urlName, outputfile,
2741: terminate_cbf, context_tcbf, HT_OK);
2742: return HT_OK;
2743: }
2744:
1.4 cvs 2745: /* do we support this protocol? */
1.116 cvs 2746: if (IsValidProtocol (urlName) == NO)
2747: {
2748: /* return error */
2749: outputfile[0] = EOS; /* file could not be opened */
2750: TtaSetStatus (docid, 1,
2751: TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
2752: urlName);
2753:
2754: if (error_html)
1.69 cvs 2755: /* so we can show the error message */
2756: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116 cvs 2757: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
2758: context_tcbf, HT_ERROR);
2759: return HT_ERROR;
2760: }
1.4 cvs 2761:
1.114 cvs 2762: /* we store CSS in subdir named 0; all the other files go to a subidr
2763: named after their own docid */
2764:
2765: tempsubdir = (mode & AMAYA_LOAD_CSS) ? 0 : docid;
2766:
1.116 cvs 2767: /* create a tempfilename */
1.272 cvs 2768: sprintf (outputfile, "%s%c%d%c%04dAM", TempFileDirectory, DIR_SEP, tempsubdir, DIR_SEP, object_counter);
1.116 cvs 2769: /* update the object_counter (used for the tempfilename) */
1.4 cvs 2770: object_counter++;
1.116 cvs 2771:
1.4 cvs 2772: /* normalize the URL */
1.286 kahan 2773: tmp = URLToUTF8 (urlName, docid);
2774: if (tmp)
2775: {
2776: esc_url = EscapeURL (tmp);
2777: TtaFreeMemory (tmp);
2778: }
2779: else
2780: esc_url = EscapeURL (urlName);
2781:
1.206 cvs 2782: if (esc_url)
2783: {
1.265 cvs 2784: ref = AmayaParseUrl (esc_url, "", AMAYA_PARSE_ALL);
1.206 cvs 2785: TtaFreeMemory (esc_url);
2786: }
2787: else
2788: ref = NULL;
2789:
1.4 cvs 2790: /* should we abort the request if we could not normalize the url? */
1.198 cvs 2791: if (ref == NULL || ref[0] == EOS) {
1.69 cvs 2792: /*error */
2793: outputfile[0] = EOS;
2794: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.214 cvs 2795: if (ref)
2796: TtaFreeMemory (ref);
1.69 cvs 2797: if (error_html)
1.116 cvs 2798: /* so we can show the error message */
2799: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99 cvs 2800: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.116 cvs 2801: context_tcbf, HT_ERROR);
1.69 cvs 2802: return HT_ERROR;
2803: }
1.116 cvs 2804:
1.4 cvs 2805: /* verify if that file name existed */
1.9 cvs 2806: if (TtaFileExist (outputfile))
1.77 cvs 2807: TtaFileUnlink (outputfile);
1.116 cvs 2808:
1.4 cvs 2809: /* Initialize the request structure */
2810: me = AHTReqContext_new (docid);
1.116 cvs 2811: if (me == NULL)
2812: {
2813: outputfile[0] = EOS;
2814: /* need an error message here */
2815: TtaFreeMemory (ref);
2816: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
2817: context_tcbf, HT_ERROR);
2818: return HT_ERROR;
2819: }
2820:
1.4 cvs 2821: /* Specific initializations for POST and GET */
1.222 cvs 2822: if (mode & AMAYA_FORM_POST
1.228 cvs 2823: || mode & AMAYA_FILE_POST)
1.116 cvs 2824: {
2825: me->method = METHOD_POST;
2826: HTRequest_setMethod (me->request, METHOD_POST);
2827: }
2828: else
2829: {
1.254 kahan 2830: #ifdef ANNOTATIONS
2831: /* we support the DELETE method for deleting annotations.
2832: the rest of the request is similar to the GET */
2833: if (mode & AMAYA_DELETE)
2834: {
2835: me->method = METHOD_GET;
2836: HTRequest_setMethod (me->request, METHOD_DELETE);
2837: }
2838: else
2839: #endif /* ANNOTATIONS */
2840: me->method = METHOD_GET;
1.295 ! kahan 2841: #ifdef ANNOTATIONS
! 2842: /* use the custom sent content_type */
! 2843: if (content_type && content_type[0] != EOS)
! 2844: AHTRequest_setCustomAcceptHeader (me->request, content_type);
! 2845: else
! 2846: #endif /* ANNOTATIONS */
! 2847: if (!HasKnownFileSuffix (ref))
1.216 cvs 2848: {
2849: /* try to adjust the Accept header in an netwise economical way */
2850: if (mode & AMAYA_LOAD_IMAGE)
2851: AHTRequest_setCustomAcceptHeader (me->request, IMAGE_ACCEPT_NEGOTIATION);
2852: else if (mode & AMAYA_LOAD_CSS)
2853: AHTRequest_setCustomAcceptHeader (me->request, "*/*;q=0.1,css/*");
1.294 kahan 2854: else
2855: AHTRequest_setCustomAcceptHeader (me->request, GENERAL_ACCEPT_NEGOTIATION);
1.216 cvs 2856: /*
2857: HTRequest_setConversion(me->request, acceptTypes, TRUE);
2858: */
2859: }
1.295 ! kahan 2860: else
! 2861: AHTRequest_setCustomAcceptHeader (me->request, GENERAL_ACCEPT_NEGOTIATION);
! 2862: /* language negotiation */
! 2863: HTRequest_setLanguage (me->request, acceptLanguages, TRUE);
1.116 cvs 2864: }
1.93 cvs 2865:
1.116 cvs 2866: /* Common initialization for all HTML methods */
1.4 cvs 2867: me->mode = mode;
2868: me->error_html = error_html;
2869: me->incremental_cbf = incremental_cbf;
2870: me->context_icbf = context_icbf;
2871: me->terminate_cbf = terminate_cbf;
2872: me->context_tcbf = context_tcbf;
1.64 cvs 2873:
1.69 cvs 2874: /* for the async. request modes, we need to have our
1.4 cvs 2875: own copy of outputfile and urlname
1.116 cvs 2876: */
1.4 cvs 2877:
1.116 cvs 2878: if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC))
2879: {
1.265 cvs 2880: l = strlen (outputfile);
1.116 cvs 2881: if (l > MAX_LENGTH)
1.265 cvs 2882: me->outputfile = TtaGetMemory (l + 2);
1.116 cvs 2883: else
1.265 cvs 2884: me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
2885: strcpy (me->outputfile, outputfile);
2886: l = strlen (urlName);
1.116 cvs 2887: if (l > MAX_LENGTH)
1.265 cvs 2888: me->urlName = TtaGetMemory (l + 2);
1.116 cvs 2889: else
1.265 cvs 2890: me->urlName = TtaGetMemory (MAX_LENGTH + 2);
2891: strcpy (me->urlName, urlName);
1.116 cvs 2892: #ifdef _WINDOWS
2893: /* force windows ASYNC requests to always be non preemptive */
2894: HTRequest_setPreemptive (me->request, NO);
2895: #endif /*_WINDOWS */
2896: } /* AMAYA_ASYNC mode */
2897: else
2898: #ifdef _WINDOWS
2899: {
1.233 cvs 2900: me->outputfile = outputfile;
2901: me->urlName = urlName;
1.116 cvs 2902: /* force windows SYNC requests to always be non preemptive */
2903: HTRequest_setPreemptive (me->request, YES);
2904: }
2905: #else /* !_WINDOWS */
2906: {
2907: me->outputfile = outputfile;
2908: me->urlName = urlName;
2909: }
2910: /***
1.136 cvs 2911: In order to take into account the stop button,
2912: the requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1.57 cvs 2913: we will loop until the document has been received or a stop signal
2914: generated
1.77 cvs 2915: ****/
1.116 cvs 2916: HTRequest_setPreemptive (me->request, NO);
2917: #endif /* _WINDOWS */
1.61 cvs 2918:
1.151 cvs 2919: /*
2920: ** Make sure that the first request is flushed immediately and not
2921: ** buffered in the output buffer
2922: */
2923: if (mode & AMAYA_FLUSH_REQUEST)
2924: HTRequest_setFlush(me->request, YES);
2925: HTRequest_setFlush(me->request, YES);
2926:
1.61 cvs 2927: /* prepare the URLname that will be displayed in teh status bar */
2928: ChopURL (me->status_urlName, me->urlName);
1.77 cvs 2929: TtaSetStatus (me->docid, 1,
2930: TtaGetMessage (AMAYA, AM_FETCHING),
1.233 cvs 2931: me->status_urlName);
1.4 cvs 2932:
1.265 cvs 2933: me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45 cvs 2934: TtaFreeMemory (ref);
1.177 cvs 2935:
1.223 cvs 2936: TtaGetEnvBoolean ("CACHE_DISCONNECTED_MODE", &bool_tmp);
1.177 cvs 2937: if (!bool_tmp && (mode & AMAYA_NOCACHE))
1.116 cvs 2938: HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
2939:
2940: /* prepare the query string and format for POST */
2941: if (mode & AMAYA_FORM_POST)
1.114 cvs 2942: {
1.116 cvs 2943: HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
2944: HTAtom_for ("application/x-www-form-urlencoded"));
2945: HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
2946: HTRequest_setEntityAnchor (me->request, me->anchor);
2947: }
1.114 cvs 2948:
1.127 cvs 2949: /* create the formdata element for libwww */
1.236 cvs 2950: if (formdata && ! (mode & AMAYA_FILE_POST))
2951: me->formdata = PrepareFormdata (formdata);
1.127 cvs 2952:
1.116 cvs 2953: /* do the request */
2954: if (mode & AMAYA_FORM_POST)
1.119 cvs 2955: {
1.191 cvs 2956: /* this call doesn't give back a ThotBool */
1.119 cvs 2957: HTParentAnchor * posted = NULL;
2958:
2959: posted = HTPostFormAnchor (me->formdata, (HTAnchor *) me->anchor,
2960: me->request);
2961: status = posted ? YES : NO;
2962: }
1.222 cvs 2963: #ifdef ANNOTATIONS
1.251 kahan 2964: else if (mode & AMAYA_FILE_POST)
1.222 cvs 2965: {
1.228 cvs 2966: unsigned long filesize;
2967: char *fileURL;
2968:
2969: /* @@@ a very ugly patch :)))
2970: I'm copying here some of the functionality I use in the PUT
2971: I need to put the common parts in another module */
1.252 kahan 2972: AM_GetFileSize (formdata, &filesize);
1.228 cvs 2973: me->block_size = filesize;
2974:
1.233 cvs 2975: fileURL = HTParse (formdata, "file:/", PARSE_ALL);
1.228 cvs 2976: me->source = HTAnchor_findAddress (fileURL);
2977: HT_FREE (fileURL);
1.222 cvs 2978:
1.228 cvs 2979: /* hardcoded ... */
2980: AHTRequest_setCustomAcceptHeader (me->request, "application/xml");
1.222 cvs 2981: HTAnchor_setFormat (HTAnchor_parent (me->source),
2982: HTAtom_for ("application/xml"));
1.228 cvs 2983: HTAnchor_setFormat (me->anchor,
2984: HTAtom_for ("application/xml"));
2985: HTAnchor_setLength ((HTParentAnchor *) me->source, me->block_size);
2986: /* @@ here I need to actually read the file and put it in document,
2987: then, when I kill the request, I need to kill it */
2988: {
2989: FILE *fp;
2990: int i;
2991: char c;
2992:
1.255 swick 2993: me->document = TtaGetMemory (me->block_size + 1);
1.233 cvs 2994: fp = fopen (formdata, "r");
1.244 kahan 2995: i = 0;
1.228 cvs 2996: c = getc (fp);
2997: while (!feof (fp))
2998: {
2999: me->document[i++] = c;
3000: c = getc (fp);
3001: }
3002: me->document[i] = '\0';
3003: fclose (fp);
3004: }
3005: HTAnchor_setDocument ( (HTParentAnchor *) me->source,
3006: (void * ) me->document);
3007: HTRequest_setEntityAnchor (me->request, HTAnchor_parent (me->source));
3008:
3009: status = HTPostAnchor (HTAnchor_parent (me->source),
1.222 cvs 3010: (HTAnchor *) me->anchor,
3011: me->request);
3012: }
3013: #endif /* ANNOTATIONS */
1.127 cvs 3014: else if (formdata)
3015: status = HTGetFormAnchor(me->formdata, (HTAnchor *) me->anchor,
3016: me->request);
1.116 cvs 3017: else
1.77 cvs 3018: status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69 cvs 3019:
1.123 cvs 3020: /* @@@ may need some special windows error msg here */
1.116 cvs 3021: /* control the errors */
1.4 cvs 3022:
1.130 cvs 3023: if (status == NO)
1.116 cvs 3024: /* the request invocation failed */
3025: {
3026: /* show an error message on the status bar */
3027: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
3028: TtaSetStatus (docid, 1,
3029: TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
3030: urlName);
3031: if (me->reqStatus == HT_NEW)
3032: /* manually invoke the last processing that usually gets done
3033: in a succesful request */
3034: InvokeGetObjectWWW_callback (docid, urlName, outputfile,
3035: terminate_cbf, context_tcbf, HT_ERROR);
3036: /* terminate_handler wasn't called */
1.136 cvs 3037: AHTReqContext_delete (me);
1.116 cvs 3038: }
3039: else
1.136 cvs 3040: /* end treatment for SYNC requests */
1.120 cvs 3041: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
3042: {
1.136 cvs 3043: /* wait here untilt the asynchronous request finishes */
1.130 cvs 3044: status = LoopForStop (me);
1.136 cvs 3045: /* if status returns HT_ERROR, should we invoke the callback? */
1.120 cvs 3046: if (!HTRequest_kill (me->request))
3047: AHTReqContext_delete (me);
3048: }
1.130 cvs 3049:
1.136 cvs 3050: /* an interface problem!!! */
3051: return (status == YES ? 0 : -1);
1.4 cvs 3052: }
3053:
1.5 cvs 3054: /*----------------------------------------------------------------------
1.17 cvs 3055: PutObjectWWW
3056: frontend for uploading a resource to a URL. This function downloads
3057: a file to be uploaded into memory, it then calls UploadMemWWW to
3058: finish the job.
3059:
1.5 cvs 3060: 2 upload modes are proposed:
3061: AMAYA_SYNC : blocking mode
3062: AMAYA_ASYNC : non-blocking mode
3063:
1.4 cvs 3064: When the function is called with the SYNC mode, the function will
3065: return only when the file has been uploaded.
3066: The ASYNC mode will immediately return after setting up the
3067: call. Furthermore, at the end of an upload, the ASYNC mode will
3068: call back terminate_cbf, handling it the context defined in
3069: context_tcbf.
3070:
3071: Notes:
3072: At the end of a succesful request, the urlName string contains the
3073: name of the actually uploaded URL. As a URL can change over the time,
3074: (e.g., be redirected elsewhere), it is advised that the function
3075: caller verifies the value of the urlName variable at the end of
3076: a request.
3077:
3078: Inputs:
3079: - docid Document identifier for the set of objects being
3080: retrieved.
3081: - fileName A pointer to the local file to upload
3082: - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
3083: - mode The retrieval mode.
3084: - terminate_cbf
3085: - context_icbf
3086: Callback and context for a terminate handler
3087:
3088: Outputs:
3089: - urlName The URL that was uploaded
3090:
3091: Returns:
3092: HT_ERROR
3093: HT_OK
1.5 cvs 3094: ----------------------------------------------------------------------*/
1.276 kahan 3095: int PutObjectWWW (int docid, char *fileName, char *urlName,
3096: char *contentType, int mode,
1.265 cvs 3097: TTcbf *terminate_cbf, void *context_tcbf)
1.4 cvs 3098: {
1.116 cvs 3099: AHTReqContext *me;
1.250 cvs 3100: CHARSET charset;
1.4 cvs 3101: int status;
1.250 cvs 3102: unsigned long file_size;
1.116 cvs 3103: char *fileURL;
1.168 cvs 3104: char *etag = NULL;
1.169 cvs 3105: HTParentAnchor *dest_anc_parent;
1.265 cvs 3106: char *tmp;
3107: char *esc_url;
1.168 cvs 3108: int UsePreconditions;
1.191 cvs 3109: ThotBool lost_update_check = TRUE;
1.235 cvs 3110: char url_name[MAX_LENGTH];
1.285 kahan 3111: char *resource_name;
1.265 cvs 3112: char *tmp2;
1.239 cvs 3113: #ifdef _WINDOWS
3114: char file_name[MAX_LENGTH];
3115: #endif /* _WINDOWS */
1.172 cvs 3116:
3117: /* should we protect the PUT against lost updates? */
1.203 cvs 3118: tmp = TtaGetEnvString ("ENABLE_LOST_UPDATE_CHECK");
1.265 cvs 3119: if (tmp && *tmp && strcasecmp (tmp, "yes"))
1.172 cvs 3120: lost_update_check = FALSE;
1.168 cvs 3121:
3122: UsePreconditions = mode & AMAYA_USE_PRECONDITIONS;
1.4 cvs 3123:
1.33 cvs 3124: AmayaLastHTTPErrorMsg [0] = EOS;
1.256 kahan 3125: AmayaLastHTTPErrorMsgR [0] = EOS;
3126:
1.33 cvs 3127:
1.116 cvs 3128: if (urlName == NULL || docid == 0 || fileName == NULL
3129: || !TtaFileExist (fileName))
1.4 cvs 3130: /* no file to be uploaded */
3131: return HT_ERROR;
3132:
3133: /* do we support this protocol? */
1.7 cvs 3134: if (IsValidProtocol (urlName) == NO)
3135: {
3136: /* return error */
1.198 cvs 3137: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL), urlName);
1.7 cvs 3138: return HT_ERROR;
3139: }
1.116 cvs 3140:
1.228 cvs 3141: /* get the size of the file */
1.252 kahan 3142: if (!AM_GetFileSize (fileName, &file_size) || file_size == 0L)
1.7 cvs 3143: {
3144: /* file was empty */
3145: /*errmsg here */
3146: return (HT_ERROR);
3147: }
1.116 cvs 3148:
3149: /* prepare the request context */
1.4 cvs 3150: if (THD_TRACE)
1.228 cvs 3151: fprintf (stderr, "file size == %u\n", (unsigned) file_size);
1.4 cvs 3152:
3153: me = AHTReqContext_new (docid);
1.7 cvs 3154: if (me == NULL)
3155: {
1.168 cvs 3156: /* @@ need an error message here */
1.151 cvs 3157: TtaHandlePendingEvents ();
1.7 cvs 3158: return (HT_ERROR);
1.208 cvs 3159: }
3160:
3161: /*
3162: ** Set up the original URL name
3163: */
1.285 kahan 3164:
3165: /* are we using content-location? */
3166: if (DocumentMeta[docid]->content_location)
3167: resource_name = DocumentMeta[docid]->content_location;
3168: else
3169: resource_name = NULL;
3170:
3171: /* prepare the target URL */
3172: if (resource_name)
1.208 cvs 3173: {
1.285 kahan 3174: tmp = strstr (urlName, resource_name);
3175: if (!tmp)
1.208 cvs 3176: {
1.285 kahan 3177: /* urlName does not include the resource name */
3178: me->default_put_name = TtaGetMemory (strlen (urlName)
3179: + strlen (resource_name)
3180: + sizeof (URL_SEP)
3181: + 1);
3182: strcpy (me->default_put_name, urlName);
3183: tmp = strrchr (me->default_put_name, URL_SEP);
3184: if (tmp)
1.208 cvs 3185: {
1.285 kahan 3186: /* it is a URL finishing in /. Only add the resrouce name */
3187: tmp++;
3188: strcpy (tmp, resource_name);
3189: }
3190: else
3191: {
3192: strcat (me->default_put_name, URL_STR);
3193: strcat (me->default_put_name, resource_name);
1.208 cvs 3194: }
3195: }
1.7 cvs 3196: }
1.285 kahan 3197:
3198: if (!me->default_put_name)
1.271 kahan 3199: me->default_put_name = TtaStrdup (urlName);
3200:
1.285 kahan 3201: HTRequest_setDefaultPutName (me->request, me->default_put_name);
1.116 cvs 3202:
1.4 cvs 3203: me->mode = mode;
3204: me->incremental_cbf = (TIcbf *) NULL;
3205: me->context_icbf = (void *) NULL;
3206: me->terminate_cbf = terminate_cbf;
3207: me->context_tcbf = context_tcbf;
1.286 kahan 3208:
3209: /* normalize the URL */
3210: tmp = URLToUTF8 (urlName, docid);
3211: if (tmp)
3212: {
3213: esc_url = EscapeURL (tmp);
3214: TtaFreeMemory (tmp);
3215: }
3216: else
3217: esc_url = EscapeURL (urlName);
1.272 cvs 3218: me->urlName = TtaStrdup (esc_url);
1.206 cvs 3219: TtaFreeMemory (esc_url);
1.228 cvs 3220: me->block_size = file_size;
1.17 cvs 3221: /* select the parameters that distinguish a PUT from a GET/POST */
1.4 cvs 3222: me->method = METHOD_PUT;
3223: me->output = stdout;
1.17 cvs 3224: /* we are not expecting to receive any input from the server */
1.265 cvs 3225: me->outputfile = (char *) NULL;
1.4 cvs 3226:
1.134 cvs 3227: #ifdef _WINDOWS
3228: /* libwww's HTParse function doesn't take into account the drive name;
1.168 cvs 3229: so we sidestep it */
1.238 cvs 3230:
1.134 cvs 3231: fileURL = NULL;
3232: StrAllocCopy (fileURL, "file:");
1.273 cvs 3233: strcpy (file_name, fileName);
1.235 cvs 3234: StrAllocCat (fileURL, file_name);
1.134 cvs 3235: #else
1.116 cvs 3236: fileURL = HTParse (fileName, "file:/", PARSE_ALL);
1.134 cvs 3237: #endif /* _WINDOWS */
1.168 cvs 3238: me->source = HTAnchor_findAddress (fileURL);
1.116 cvs 3239: HT_FREE (fileURL);
1.273 cvs 3240: strcpy (url_name, me->urlName);
1.235 cvs 3241: me->dest = HTAnchor_findAddress (url_name);
1.169 cvs 3242: /* we memorize the anchor's parent @ as we use it a number of times
3243: in the following lines */
3244: dest_anc_parent = HTAnchor_parent (me->dest);
1.168 cvs 3245:
1.169 cvs 3246: /*
3247: ** Set the Content-Type of the file we are uploading
3248: */
3249: /* we try to use any content-type previosuly associated
3250: with the parent. If it doesn't exist, we try to guess it
3251: from the URL */
1.276 kahan 3252: /* @@ JK: trying to use the content type we stored */
3253: /*
1.235 cvs 3254: tmp2 = HTAtom_name (HTAnchor_format (dest_anc_parent));
1.276 kahan 3255: */
3256: tmp2 = NULL;
1.235 cvs 3257: if (!tmp2 || !strcmp (tmp2, "www/unknown"))
1.169 cvs 3258: {
1.260 kahan 3259: HTAtom *tmp_atom;
1.276 kahan 3260:
1.260 kahan 3261: tmp_atom = AHTGuessAtom_for (me->urlName, contentType);
1.276 kahan 3262: if (!tmp_atom || !strcmp (HTAtom_name (tmp_atom), "www/unknown"))
3263: {
3264: /* ask the user for a MIME type */
3265: }
1.260 kahan 3266: HTAnchor_setFormat (dest_anc_parent, tmp_atom);
1.235 cvs 3267: tmp2 = HTAtom_name (HTAnchor_format (dest_anc_parent));
1.169 cvs 3268: }
3269: /* .. and we give the same type to the source anchor */
3270: /* we go thru setOutputFormat, rather than change the parent's
3271: anchor, as that's the place that libwww expects it to be */
1.235 cvs 3272: HTAnchor_setFormat (HTAnchor_parent (me->source), HTAtom_for (tmp2));
1.219 cvs 3273:
1.235 cvs 3274: HTRequest_setOutputFormat (me->request, HTAtom_for (tmp2));
1.219 cvs 3275:
3276: /*
3277: ** Set the Charset of the file we are uploading
3278: */
1.240 cvs 3279: /* we set the charset as indicated in the document's metadata
3280: structure (and only if it exists) */
1.250 cvs 3281: charset = TtaGetDocumentCharset (docid);
3282: if (charset != UNDEFINED_CHARSET)
1.240 cvs 3283: {
1.250 cvs 3284: tmp = TtaGetCharsetName (charset);
1.272 cvs 3285: if (tmp && *tmp != EOS)
1.250 cvs 3286: {
1.273 cvs 3287: tmp2 = TtaStrdup (tmp);
1.250 cvs 3288: HTAnchor_setCharset (dest_anc_parent, HTAtom_for (tmp2));
3289: TtaFreeMemory (tmp2);
3290: tmp2 = HTAtom_name (HTAnchor_charset (dest_anc_parent));
3291: /* .. and we give the same charset to the source anchor */
3292: /* we go thru setCharSet, rather than change the parent's
3293: anchor, as that's the place that libwww expects it to be */
3294: HTAnchor_setCharset (HTAnchor_parent (me->source),
3295: HTAtom_for (tmp2));
3296: }
1.219 cvs 3297: }
3298:
3299: /*
3300: ** define other request characteristics
3301: */
1.116 cvs 3302: #ifdef _WINDOWS
1.136 cvs 3303: HTRequest_setPreemptive (me->request, NO);
1.116 cvs 3304: #else
1.134 cvs 3305: HTRequest_setPreemptive (me->request, NO);
1.116 cvs 3306: #endif /* _WINDOWS */
3307:
1.151 cvs 3308: /*
3309: ** Make sure that the first request is flushed immediately and not
3310: ** buffered in the output buffer
3311: */
3312: if (mode & AMAYA_FLUSH_REQUEST)
3313: HTRequest_setFlush(me->request, YES);
1.168 cvs 3314:
3315: /* Should we use preconditions? */
1.172 cvs 3316: if (lost_update_check)
1.168 cvs 3317: {
1.172 cvs 3318: if (UsePreconditions)
3319: etag = HTAnchor_etag (HTAnchor_parent (me->dest));
3320:
3321: if (etag)
3322: {
3323: HTRequest_setPreconditions(me->request, HT_MATCH_THIS);
3324: }
3325: else
3326: {
3327: HTRequest_setPreconditions(me->request, HT_NO_MATCH);
3328: HTRequest_addAfter(me->request, check_handler, NULL, NULL, HT_ALL,
3329: HT_FILTER_MIDDLE, YES);
1.175 cvs 3330: HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL,
3331: HT_NO_ACCESS, HT_FILTER_MIDDLE, YES);
3332: HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL,
3333: HT_REAUTH, HT_FILTER_MIDDLE, YES);
3334: HTRequest_addAfter (me->request, HTAuthInfoFilter, "http://*", NULL,
3335: HT_ALL, HT_FILTER_MIDDLE, YES);
3336: HTRequest_addAfter (me->request, HTUseProxyFilter, "http://*", NULL,
3337: HT_USE_PROXY, HT_FILTER_MIDDLE, YES);
1.172 cvs 3338: }
1.168 cvs 3339: }
3340: else
3341: {
1.172 cvs 3342: /* don't use preconditions */
1.168 cvs 3343: HTRequest_setPreconditions(me->request, HT_NO_MATCH);
3344: }
1.151 cvs 3345:
1.136 cvs 3346: /* don't use the cache while saving a document */
3347: HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
1.116 cvs 3348:
1.164 cvs 3349: /* Throw away any reponse body */
3350: /*
3351: HTRequest_setOutputStream (me->request, HTBlackHole());
3352: */
3353:
1.168 cvs 3354: /* prepare the URLname that will be displayed in the status bar */
1.74 cvs 3355: ChopURL (me->status_urlName, me->urlName);
1.233 cvs 3356: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING), me->status_urlName);
1.114 cvs 3357:
1.164 cvs 3358: /* make the request */
1.172 cvs 3359: if (lost_update_check && (!UsePreconditions || !etag))
1.168 cvs 3360: status = HTHeadAnchor (me->dest, me->request);
3361: else
3362: status = HTPutDocumentAnchor (HTAnchor_parent (me->source), me->dest, me->request);
1.4 cvs 3363:
1.130 cvs 3364: if (status == YES && me->reqStatus != HT_ERR)
1.7 cvs 3365: {
1.168 cvs 3366: /* part of the stop button handler */
3367: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
3368: status = LoopForStop (me);
1.15 cvs 3369: }
1.136 cvs 3370: if (!HTRequest_kill (me->request))
3371: AHTReqContext_delete (me);
1.168 cvs 3372:
1.116 cvs 3373: TtaHandlePendingEvents ();
1.90 cvs 3374:
1.130 cvs 3375: return (status == YES ? 0 : -1);
1.28 cvs 3376: }
1.4 cvs 3377:
1.5 cvs 3378: /*----------------------------------------------------------------------
1.138 cvs 3379: StopRequest
1.17 cvs 3380: stops (kills) all active requests associated with a docid
1.5 cvs 3381: ----------------------------------------------------------------------*/
1.4 cvs 3382: void StopRequest (int docid)
3383: {
1.140 cvs 3384: if (Amaya && CanDoStop ())
1.138 cvs 3385: {
1.139 cvs 3386: #if 0 /* for later */
1.140 cvs 3387: AHTDocId_Status *docid_status;
3388: /* verify if there are any requests at all associated with docid */
1.138 cvs 3389: docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
3390: Amaya->docid_status);
3391: if (docid_status == (AHTDocId_Status *) NULL)
3392: return;
1.139 cvs 3393: #endif /* 0 */
1.138 cvs 3394: /* temporary call to stop all requests, as libwww changed its API */
3395: StopAllRequests (docid);
3396: }
3397: }
3398:
1.190 cvs 3399: /* @@@ the docid parameter isn't used... clean it up */
1.138 cvs 3400: /*----------------------------------------------------------------------
3401: StopAllRequests
3402: stops (kills) all active requests. We use the docid
3403: ----------------------------------------------------------------------*/
3404: void StopAllRequests (int docid)
3405: {
1.4 cvs 3406: HTList *cur;
3407: AHTReqContext *me;
1.191 cvs 3408: static ThotBool lock_stop = 0;
3409: ThotBool async_flag;
1.232 cvs 3410: AHTReqStatus old_reqStatus;
1.116 cvs 3411:
1.138 cvs 3412: /* only do the stop if we're not being called while processing a
1.146 cvs 3413: request, and if we're not already dealing with a stop */
3414: if (Amaya && CanDoStop () && !lock_stop)
1.7 cvs 3415: {
1.100 cvs 3416: #ifdef DEBUG_LIBWWW
1.138 cvs 3417: fprintf (stderr, "StopRequest: number of Amaya requests "
3418: "before kill: %d\n", Amaya->open_requests);
1.100 cvs 3419: #endif /* DEBUG_LIBWWW */
1.150 cvs 3420: /* enter the critical section */
1.146 cvs 3421: lock_stop = TRUE;
1.214 cvs 3422: /* set a module global variable so that we can do special
3423: processing easier */
3424: UserAborted_flag = TRUE;
1.232 cvs 3425: /* abort all outstanding libwww UI dialogues */
1.265 cvs 3426: CallbackDialogue (BaseDialog + FormAnswer, STRING_DATA, NULL);
3427: CallbackDialogue (BaseDialog + ConfirmForm, INTEGER_DATA, NULL);
1.164 cvs 3428: /* expire all outstanding timers */
3429: HTTimer_expireAll ();
1.160 cvs 3430: /* HTNet_killAll (); */
1.245 kahan 3431: if (Amaya->open_requests)
1.116 cvs 3432: {
1.245 kahan 3433: cur = Amaya->reqlist;
3434: while ((me = (AHTReqContext *) HTList_nextObject (cur)))
1.160 cvs 3435: {
1.245 kahan 3436: if (AmayaIsAlive ())
3437: {
1.161 cvs 3438: #ifdef DEBUG_LIBWWW
1.245 kahan 3439: fprintf (stderr,"StopRequest: killing req %p, url %s, status %d\n", me, me->urlName, me->reqStatus);
1.161 cvs 3440: #endif /* DEBUG_LIBWWW */
1.245 kahan 3441:
3442: if (me->reqStatus != HT_END && me->reqStatus != HT_ABORT)
1.163 cvs 3443: {
1.245 kahan 3444: if ((me->mode & AMAYA_ASYNC)
3445: || (me->mode & AMAYA_IASYNC))
3446: async_flag = TRUE;
3447: else
3448: async_flag = FALSE;
3449:
3450: /* change the status to say that the request aborted */
3451: /* if the request was "busy", we just change a flag to say so and
3452: let the handler finish the processing itself */
3453: old_reqStatus = me->reqStatus;
3454: me->reqStatus = HT_ABORT;
3455: if (old_reqStatus == HT_BUSY)
3456: continue;
3457:
3458: /* kill the request, using the appropriate function */
3459: if (me->request->net)
3460: HTNet_killPipe (me->request->net);
3461: else
3462: {
3463: if (me->terminate_cbf)
3464: (*me->terminate_cbf) (me->docid, -1, me->urlName,
3465: me->outputfile,
3466: NULL,
3467: me->context_tcbf);
3468:
3469: if (async_flag)
3470: /* explicitly free the request context for async
3471: requests. The sync requests context is freed
3472: by LoopForStop */
3473: AHTReqContext_delete (me);
3474: }
3475: cur = Amaya->reqlist;
1.160 cvs 3476: }
1.142 cvs 3477: #ifndef _WINDOWS
3478: #ifdef WWW_XWINDOWS
1.245 kahan 3479: /* to be on the safe side, remove all outstanding
3480: X events */
1.160 cvs 3481: else
3482: RequestKillAllXtevents (me);
1.142 cvs 3483: #endif /* WWW_XWINDOWS */
3484: #endif /* !_WINDOWS */
1.245 kahan 3485: }
1.160 cvs 3486: }
1.245 kahan 3487: /* Delete remaining channels */
3488: HTChannel_safeDeleteAll ();
1.116 cvs 3489: }
1.214 cvs 3490: /* reset the stop status */
3491: UserAborted_flag = FALSE;
1.150 cvs 3492: /* exit the critical section */
1.148 cvs 3493: lock_stop = FALSE;
1.100 cvs 3494: #ifdef DEBUG_LIBWWW
1.138 cvs 3495: fprintf (stderr, "StopRequest: number of Amaya requests "
3496: "after kill: %d\n", Amaya->open_requests);
1.100 cvs 3497: #endif /* DEBUG_LIBWWW */
1.138 cvs 3498: }
3499: } /* StopAllRequests */
1.17 cvs 3500:
3501:
1.105 cvs 3502: /*----------------------------------------------------------------------
3503: AmayaIsAlive
1.140 cvs 3504: returns the value of the AmayaAlive_flag
1.105 cvs 3505: ----------------------------------------------------------------------*/
1.191 cvs 3506: ThotBool AmayaIsAlive (void)
1.105 cvs 3507: {
1.140 cvs 3508: return AmayaAlive_flag;
3509: }
3510:
3511: /*----------------------------------------------------------------------
3512: CanDoStop
3513: returns the value of the CanDoStop flag
3514: ----------------------------------------------------------------------*/
1.191 cvs 3515: ThotBool CanDoStop (void)
1.140 cvs 3516: {
3517: return CanDoStop_flag;
3518: }
3519:
3520: /*----------------------------------------------------------------------
3521: CanDoStop_set
3522: sets the value of the CanDoStop flag
3523: ----------------------------------------------------------------------*/
1.191 cvs 3524: void CanDoStop_set (ThotBool value)
1.140 cvs 3525: {
3526: CanDoStop_flag = value;
1.176 cvs 3527: }
3528:
1.265 cvs 3529: /*----------------------------------------------------------------------
3530: ----------------------------------------------------------------------*/
1.176 cvs 3531: void libwww_updateNetworkConf (int status)
3532: {
3533: /* @@@ the docid parameter isn't used... clean it up */
3534: int docid = 1;
3535:
3536: /* first, stop all current requests, as the network
3537: may make some changes */
3538: StopAllRequests (docid);
1.207 cvs 3539:
3540: if (status & AMAYA_SAFEPUT_RESTART)
3541: {
3542: SafePut_delete ();
3543: SafePut_init ();
3544: }
1.176 cvs 3545:
3546: if (status & AMAYA_PROXY_RESTART)
3547: {
3548: HTProxy_deleteAll ();
3549: ProxyInit ();
3550: }
3551:
3552: if (status & AMAYA_CACHE_RESTART)
3553: {
3554: clear_cachelock ();
3555: HTCacheTerminate ();
3556: HTCacheMode_setEnabled (NO);
3557: CacheInit ();
3558: }
1.193 cvs 3559:
3560: if (status & AMAYA_LANNEG_RESTART)
3561: {
3562: /* clear the current values */
3563: if (acceptLanguages)
3564: HTLanguage_deleteAll (acceptLanguages);
3565: /* read in the new ones */
3566: acceptLanguages = HTList_new ();
3567: AHTAcceptLanguagesInit (acceptLanguages);
3568: }
1.105 cvs 3569: }
1.246 kahan 3570:
3571: /****************************************************************************
3572: ** The following two functions allow to set and read the value of *
3573: ** the FTPURL_flag. If this flag is true, it means we can browse FTP URLs *
3574: ***************************************************************************/
3575:
3576: /*----------------------------------------------------------------------
3577: AHT_FTPURL_flag_set
3578: ----------------------------------------------------------------------*/
3579: void AHTFTPURL_flag_set (ThotBool value)
3580: {
3581: FTPURL_flag = value;
3582: }
3583:
3584: /*----------------------------------------------------------------------
3585: AHT_FTPURL_flag
3586: ----------------------------------------------------------------------*/
3587: ThotBool AHTFTPURL_flag (void)
3588: {
3589: return (FTPURL_flag);
3590: }
3591:
Webmaster