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