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