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