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