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