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