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