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