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