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