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