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