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