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