Annotation of Amaya/amaya/query.c, revision 1.90
1.14 cvs 1: /*
2: *
3: * (c) COPYRIGHT MIT and INRIA, 1996.
4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
1.4 cvs 7:
1.19 cvs 8: /*
1.83 cvs 9: * query.c : contains all the functions for requesting and publishing
1.19 cvs 10: * URLs via libwww. It handles any eventual HTTP error code
11: * (redirection, authentication needed, not found, etc.)
12: *
1.69 cvs 13: * Author: J. Kahan
1.75 cvs 14: * R. Guetari/J. Kahan Windows 95/NT routines
1.19 cvs 15: */
1.17 cvs 16:
1.44 cvs 17: #ifndef AMAYA_JAVA
18:
1.12 cvs 19: /* Amaya includes */
1.25 cvs 20: #define THOT_EXPORT extern
1.4 cvs 21: #include "amaya.h"
1.70 cvs 22: #ifdef _WINDOWS
23: #include <fcntl.h>
24: #endif /* _WINDOWS */
1.7 cvs 25:
1.4 cvs 26: #if defined(__svr4__)
27: #define CATCH_SIG
28: #endif
29:
30: /* local structures coming from libwww and which are
1.17 cvs 31: not found in any .h file
1.7 cvs 32: */
1.68 cvs 33:
1.7 cvs 34: struct _HTStream
35: {
1.15 cvs 36: const HTStreamClass *isa;
37: FILE *fp;
1.45 cvs 38: BOOL leave_open; /* Close file when TtaFreeMemory? */
1.15 cvs 39: char *end_command; /* Command to execute */
40: BOOL remove_on_close; /* Remove file? */
41: char *filename; /* Name of file */
42: HTRequest *request; /* saved for callback */
43: HTRequestCallback *callback;
1.7 cvs 44: };
45:
1.15 cvs 46:
1.7 cvs 47: struct _HTError
48: {
49: HTErrorElement element; /* Index number into HTError */
50: HTSeverity severity; /* A la VMS */
51: BOOL ignore; /* YES if msg should not go to user */
52: void *par; /* Explanation, e.g. filename */
53: int length; /* For copying by generic routine */
54: char *where; /* Which function */
55: };
56:
57:
58: struct _HTHost
59: {
60: char *hostname; /* name of host + optional port */
61: time_t ntime; /* Creation time */
62: char *type; /* Peer type */
63: int version; /* Peer version */
64: HTMethod methods; /* Public methods (bit-flag) */
65: char *server; /* Server name */
66: char *user_agent; /* User Agent */
67: char *range_units; /* Acceptable range units */
68: HTTransportMode mode; /* Supported mode */
69: HTChannel *channel; /* Persistent channel */
70: HTList *pipeline; /* Pipe line of net objects */
71: HTList *pending; /* List of pending Net objects */
72: time_t expires; /* Persistent channel expires time */
73: };
1.4 cvs 74:
75: /* Type definitions and global variables etc. local to this module */
76:
1.17 cvs 77: /*----------------------------------------------------------------------*/
78:
1.4 cvs 79: /*** private variables ***/
1.17 cvs 80:
1.4 cvs 81: static HTList *converters = NULL; /* List of global converters */
1.27 cvs 82: static HTList *acceptTypes = NULL; /* List of types for the Accept header */
1.4 cvs 83: static HTList *encodings = NULL;
1.16 cvs 84: static int object_counter = 0; /* loaded objects counter */
1.24 cvs 85: static boolean AmayaIsAlive;
1.4 cvs 86:
1.15 cvs 87: #include "answer_f.h"
88: #include "query_f.h"
89: #include "AHTURLTools_f.h"
90: #include "AHTBridge_f.h"
91: #include "AHTMemConv_f.h"
92: #include "AHTFWrite_f.h"
1.4 cvs 93:
1.70 cvs 94: #ifdef _WINDOWS
95: #ifdef __STDC__
96: int WIN_Activate_Request (HTRequest* , HTAlertOpcode, int, const char*, void*, HTAlertPar*);
97: #else
98: int WIN_Activate_Request ();
99: #endif /* __STDC__ */
100: #endif /* _WINDOWS */
1.15 cvs 101:
102: /*----------------------------------------------------------------------
1.17 cvs 103: GetDocIdStatus
104: gets the status associated to a docid
1.15 cvs 105: ----------------------------------------------------------------------*/
106: #ifdef __STDC__
107: AHTDocId_Status *GetDocIdStatus (int docid, HTList * documents)
108: #else
109: AHTDocID_Status *GetDocIdStatus (docid, documents)
110: int docid;
111: HTList *documents;
112:
113: #endif
114: {
115: AHTDocId_Status *me;
116: HTList *cur;
117:
118: if (documents)
119: {
120: cur = documents;
121:
122: while ((me = (AHTDocId_Status *) HTList_nextObject (cur)))
123: {
124: if (me->docid == docid)
125: return (me);
1.18 cvs 126: }
127: }
1.15 cvs 128: return (AHTDocId_Status *) NULL;
129:
130: }
131:
1.5 cvs 132: /*----------------------------------------------------------------------
1.27 cvs 133: AHTGuessAtom_for
134: Converts an Amaya type descriptor into the equivalent MIME type.
135: ----------------------------------------------------------------------*/
136: #ifdef __STDC__
137: static HTAtom *AHTGuessAtom_for (char *urlName, PicType contentType)
138: #else
139: static HTAtom *AHTGuessAtom_for (urlName, contentType)
140: char *urlName;
141: PicType contentType;
142: #endif
143: {
144: HTAtom *atom;
145: char *filename;
146: HTEncoding enc;
147: HTEncoding cte;
148: HTLanguage lang;
1.50 cvs 149: double quality = 1.0;
1.27 cvs 150:
151: switch (contentType)
152: {
153: case xbm_type:
154: atom = HTAtom_for("image/xbm");
155: break;
156: case eps_type:
1.35 cvs 157: atom = HTAtom_for("application/postscript");
1.27 cvs 158: break;
159: case xpm_type:
160: atom = HTAtom_for("image/xpm");
161: break;
162: case gif_type:
163: atom = HTAtom_for("image/gif");
164: break;
165: case jpeg_type:
166: atom = HTAtom_for("image/jpeg");
167: break;
168: case png_type:
169: atom = HTAtom_for("image/png");
170: break;
171: case unknown_type:
172: default:
173: /*
174: ** Amaya could not detect the type, so
175: ** we try to use the filename's suffix to do so.
176: */
1.45 cvs 177: filename = AmayaParseUrl (urlName, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.27 cvs 178: HTBind_getFormat (filename, &atom, &enc, &cte, &lang, &quality);
1.45 cvs 179: TtaFreeMemory (filename);
1.27 cvs 180: if (atom == WWW_UNKNOWN)
181: /*
182: ** we could not identify the suffix, so we assign it
183: ** a default type
184: */
185: atom = HTAtom_for ("text/html");
186: break;
187: }
188:
189: return atom;
190: }
191:
192: /*----------------------------------------------------------------------
1.17 cvs 193: AHTReqContext_new
194: create a new Amaya Context Object and update the global Amaya
195: request status.
1.5 cvs 196: ----------------------------------------------------------------------*/
1.4 cvs 197: #ifdef __STDC__
198: static AHTReqContext *AHTReqContext_new (int docid)
199: #else
200: static AHTReqContext *AHTReqContext_new (docid)
201: int docid;
202:
203: #endif
204: {
205: AHTReqContext *me;
206: AHTDocId_Status *docid_status;
207:
208: if ((me = (AHTReqContext *) TtaGetMemory (sizeof (AHTReqContext))) == NULL)
209: outofmem (__FILE__, "Context_new");
210:
1.81 cvs 211: /* Bind the Context object together with the Request Object */
212: me->request = HTRequest_new ();
213:
1.80 cvs 214: /* clean the associated file structure) */
1.79 cvs 215: HTRequest_setOutputStream (me->request, NULL);
1.81 cvs 216:
1.4 cvs 217: /* Initialize the other members of the structure */
1.17 cvs 218: me->reqStatus = HT_NEW; /* initial status of a request */
1.4 cvs 219: me->output = NULL;
1.89 cvs 220: me->content_type = NULL;
1.51 cvs 221: # ifndef _WINDOWS
1.4 cvs 222: me->read_xtinput_id = (XtInputId) NULL;
223: me->write_xtinput_id = (XtInputId) NULL;
224: me->except_xtinput_id = (XtInputId) NULL;
1.51 cvs 225: # endif
1.4 cvs 226: me->docid = docid;
227: HTRequest_setMethod (me->request, METHOD_GET);
228: HTRequest_setOutputFormat (me->request, WWW_SOURCE);
229: HTRequest_setContext (me->request, me);
230: me->read_ops = 0;
231: me->write_ops = 0;
232: me->except_ops = 0;
1.36 cvs 233: /* experimental */
234: me->read_sock = INVSOC;
235: me->write_sock = INVSOC;
236: me->except_sock = INVSOC;
1.4 cvs 237:
238: /* Update the global context */
239: HTList_appendObject (Amaya->reqlist, (void *) me);
240:
241: docid_status = GetDocIdStatus (docid, Amaya->docid_status);
242:
1.7 cvs 243: if (docid_status == NULL)
244: {
245: docid_status = (AHTDocId_Status *) TtaGetMemory (sizeof (AHTDocId_Status));
246: docid_status->docid = docid;
247: docid_status->counter = 1;
248: HTList_addObject (Amaya->docid_status, (void *) docid_status);
249: }
250: else
1.4 cvs 251: docid_status->counter++;
252:
253:
254: Amaya->open_requests++;
255:
256: /* error stream handling */
257: me->error_stream = (char *) NULL;
258: me->error_stream_size = 0;
1.74 cvs 259:
260: #ifdef DEBUG_LIBWWW
261: fprintf (stderr, "AHTReqContext_new: Created object %p\n", me);
262: #endif
1.4 cvs 263: return me;
1.74 cvs 264:
1.4 cvs 265: }
266:
1.5 cvs 267: /*----------------------------------------------------------------------
1.17 cvs 268: AHTReqContext_delete
269: Delete an Amaya Context Object and update the global Amaya request
270: status.
1.5 cvs 271: ----------------------------------------------------------------------*/
1.4 cvs 272:
273: #ifdef __STDC__
1.15 cvs 274: boolean AHTReqContext_delete (AHTReqContext * me)
1.4 cvs 275: #else
1.15 cvs 276: boolean AHTReqContext_delete (me)
1.4 cvs 277: AHTReqContext *me;
278:
279: #endif
280: {
281: AHTDocId_Status *docid_status;
282:
1.7 cvs 283: if (me)
284: {
1.4 cvs 285:
1.74 cvs 286: #ifdef DEBUG_LIBWWW
287: fprintf (stderr, "AHTReqContext_delete: Deleting object %p\n", me);
288: #endif
289:
1.7 cvs 290: if (Amaya->reqlist)
291: HTList_removeObject (Amaya->reqlist, (void *) me);
1.4 cvs 292:
1.7 cvs 293: docid_status = GetDocIdStatus (me->docid, Amaya->docid_status);
1.4 cvs 294:
1.7 cvs 295: if (docid_status)
296: {
297: docid_status->counter--;
298:
299: if (docid_status->counter == 0)
300: {
301: HTList_removeObject (Amaya->docid_status, (void *) docid_status);
302: TtaFreeMemory ((void *) docid_status);
303: }
304: }
1.79 cvs 305:
306: if (HTRequest_outputStream (me->request))
307: AHTFWriter_FREE (me->request->output_stream);
308:
1.7 cvs 309: HTRequest_delete (me->request);
1.85 cvs 310:
1.89 cvs 311: if (me->output && me->output != stdout)
1.85 cvs 312: {
313: #ifdef DEBUG_LIBWWW
314: fprintf (stderr, "AHTReqContext_delete: URL is %s, closing "
315: "FILE %p\n", me->urlName, me->output);
316: #endif
317: fclose (me->output);
318: me->output = NULL;
319: }
1.79 cvs 320:
1.7 cvs 321: if (me->error_stream != (char *) NULL)
1.70 cvs 322: HT_FREE (me->error_stream);
323: # ifndef _WINDOWS
324: # ifdef WWW_XWINDOWS
1.21 cvs 325: if (me->read_xtinput_id || me->write_xtinput_id ||
326: me->except_xtinput_id)
327: RequestKillAllXtevents(me);
1.70 cvs 328: # endif /* WWW_XWINDOWS */
329: # endif /* !_WINDOWS */
1.74 cvs 330:
331: if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC))
332: /* for the ASYNC mode, free the memory we allocated in GetObjectWWW
333: or in PutObjectWWW */
334: {
1.79 cvs 335: if (me->urlName)
336: TtaFreeMemory (me->urlName);
337: if (me->outputfile)
338: TtaFreeMemory (me->outputfile);
1.74 cvs 339: }
340:
1.7 cvs 341: TtaFreeMemory ((void *) me);
1.4 cvs 342:
1.7 cvs 343: Amaya->open_requests--;
1.4 cvs 344:
1.15 cvs 345: return TRUE;
1.4 cvs 346:
1.7 cvs 347: }
1.15 cvs 348: return FALSE;
1.4 cvs 349: }
350:
351:
1.15 cvs 352: /*----------------------------------------------------------------------
353: AHTUpload_callback
1.17 cvs 354: callback handler for executing the PUT command
1.15 cvs 355: ----------------------------------------------------------------------*/
1.4 cvs 356: #ifdef __STDC__
357: static int AHTUpload_callback (HTRequest * request, HTStream * target)
358: #else
359: static int AHTUpload_callback (request, target)
360: HTRequest *request;
361: HTStream *target;
362:
363: #endif
364: {
365: AHTReqContext *me = HTRequest_context (request);
1.7 cvs 366: HTParentAnchor *entity = HTRequest_entityAnchor (request);
367: int len = HTAnchor_length (entity);
1.4 cvs 368: int status;
369:
370: /* Send the data down the pipe */
1.7 cvs 371:
1.4 cvs 372: status = (*target->isa->put_block) (target, me->mem_ptr, len);
373:
1.7 cvs 374: if (status == HT_LOADED || status == HT_OK)
375: {
1.13 cvs 376: if (PROT_TRACE)
377: HTTrace ("Posting Data Target is SAVED\n");
378: (*target->isa->flush) (target);
379: return (HT_LOADED);
1.7 cvs 380: }
381: if (status == HT_WOULD_BLOCK)
382: {
383: if (PROT_TRACE)
384: HTTrace ("Posting Data Target WOULD BLOCK\n");
1.74 cvs 385: #ifdef _WINDOWS
386: return HT_CONTINUE;
387: #else
388: return HT_WOULD_BLOCK;
389: #endif /* _WINDOWS */
390:
1.7 cvs 391: return HT_WOULD_BLOCK;
392: }
393: else if (status == HT_PAUSE)
394: {
395: if (PROT_TRACE)
396: HTTrace ("Posting Data Target PAUSED\n");
397: return HT_PAUSE;
398: }
399: else if (status > 0)
400: { /* Stream specific return code */
401: if (PROT_TRACE)
402: HTTrace ("Posting Data. Target returns %d\n", status);
403: return status;
404: }
405: else
406: { /* we have a real error */
407: if (PROT_TRACE)
408: HTTrace ("Posting Data Target ERROR %d\n", status);
409: return status;
410: }
1.4 cvs 411: }
412:
1.5 cvs 413: /*----------------------------------------------------------------------
1.17 cvs 414: Thread_deleteAll
415: this function deletes the whole list of active threads.
1.5 cvs 416: ----------------------------------------------------------------------*/
1.4 cvs 417: #ifdef __STDC__
418: static void Thread_deleteAll (void)
419: #else
420: static void Thread_deleteAll ()
421: #endif
422: {
1.21 cvs 423: HTList *cur;
424: AHTReqContext *me;
425: AHTDocId_Status *docid_status;
426:
1.74 cvs 427: if (Amaya && Amaya->reqlist)
428: {
429: if (Amaya->open_requests > 0)
430: #ifdef DEBUG_LIBWWW
431: fprintf (stderr, "Thread_deleteAll: Killing %d outstanding "
432: "requests\n", Amaya->open_requests);
433: #endif
434: {
435: cur = Amaya->reqlist;
436:
437: /* erase the requests */
438: while ((me = (AHTReqContext *) HTList_removeLastObject (cur)))
439: {
440: if (me->request)
441: {
1.70 cvs 442: # ifndef _WINDOWS
1.74 cvs 443: RequestKillAllXtevents (me);
1.70 cvs 444: # endif /* !_WINDOWS */
1.87 cvs 445: HTRequest_kill (me->request);
1.74 cvs 446: AHTReqContext_delete (me);
447: }
448: } /* while */
449:
450: /* erase the docid_status entities */
451: while ((docid_status = (AHTDocId_Status *) HTList_removeLastObject ((void *) Amaya->docid_status)))
452: TtaFreeMemory ((void *) docid_status);
453:
454: } /* if */
1.84 cvs 455:
1.74 cvs 456: }
1.4 cvs 457: }
458:
1.5 cvs 459: /*----------------------------------------------------------------------
1.83 cvs 460: AHTOpen_file
461: ----------------------------------------------------------------------*/
462: #ifdef __STDC__
1.85 cvs 463: int AHTOpen_file (HTRequest * request)
1.83 cvs 464: #else
1.85 cvs 465: int AHTOpen_file (request)
1.83 cvs 466: HTRequest *request;
467:
468: #endif /* __STDC__ */
469: {
470: AHTReqContext *me; /* current request */
471:
472: me = HTRequest_context (request);
473:
474: #ifdef DEBUG_LIBWWW
1.85 cvs 475: fprintf(stderr, "AHTOpen_file: start\n");
476: #endif /* DEBUG_LIBWWW */
477:
478: if (me->reqStatus == HT_ABORT)
479: {
480: #ifdef DEBUG_LIBWWW
481: fprintf(stderr, "AHTOpen_file: caught an abort request, skipping it\n");
1.83 cvs 482: #endif /* DEBUG_LIBWWW */
483:
1.85 cvs 484: return HT_OK;
485: }
486:
487: if (HTRequest_outputStream (me->request))
488: {
489:
490: #ifdef DEBUG_LIBWWW
491: fprintf(stderr, "AHTOpen_file: output stream already existed for url %s\n", me->urlName);
492: #endif /* DEBUG_LIBWWW */
493: return HT_OK;
494: }
495:
496: #ifdef DEBUG_LIBWWW
497: fprintf(stderr, "AHTOpen_file: opening output stream for url %s\n", me->urlName);
498: #endif /* DEBUG_LIBWWW */
499:
1.83 cvs 500: if (!(me->output) &&
501: (me->output != stdout) &&
502: #ifndef _WINDOWS
503: (me->output = fopen (me->outputfile, "w")) == NULL)
504: {
505: #else
506: (me->output = fopen (me->outputfile, "wb")) == NULL)
507: {
508: #endif /* !_WINDOWS */
509:
510: me->outputfile[0] = '\0'; /* file could not be opened */
1.85 cvs 511: #ifdef DEBUG_LIBWWW
512: fprintf(stderr, "AHTOpen_file: couldn't open output stream for url %s\n", me->urlName);
513: #endif
1.83 cvs 514: TtaSetStatus (me->docid, 1,
515: TtaGetMessage (AMAYA, AM_CANNOT_CREATE_FILE),
516: me->outputfile);
517: me->reqStatus = HT_ERR;
518: return (HT_ERROR);
519: }
520:
521: HTRequest_setOutputStream (me->request,
522: AHTFWriter_new (me->request,
523: me->output, YES));
524: me->reqStatus = HT_WAITING;
525:
526: return HT_OK;
527: }
528:
529: /*----------------------------------------------------------------------
1.17 cvs 530: redirection_handler
531: this function is registered to handle permanent and temporary
532: redirections.
533: ----------------------------------------------------------------------*/
1.4 cvs 534: #ifdef __STDC__
1.7 cvs 535: static int redirection_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4 cvs 536: #else
537: static int redirection_handler (request, context, status)
538: HTRequest *request;
539: HTResponse *response;
540: void *param;
541: int status;
542:
543: #endif
544: {
545:
546: char *ref;
547: HTAnchor *new_anchor = HTResponse_redirection (response);
1.7 cvs 548: AHTReqContext *me = HTRequest_context (request);
1.4 cvs 549: HTMethod method = HTRequest_method (request);
550:
551:
1.7 cvs 552: if (!new_anchor)
553: {
554: if (PROT_TRACE)
555: HTTrace ("Redirection. No destination\n");
556: return HT_OK;
557: }
1.4 cvs 558:
559: /*
560: ** Only do redirect on GET and HEAD
561: */
1.7 cvs 562: if (!HTMethod_isSafe (method))
563: {
564: HTAlertCallback *prompt = HTAlert_find (HT_A_CONFIRM);
565:
566: if (prompt)
567: {
568: if ((*prompt) (request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
569: NULL, NULL, NULL) != YES)
570: return HT_ERROR;
571: }
572: }
1.4 cvs 573:
574: /*
575: ** Start new request with the redirect anchor found in the headers.
576: ** Note that we reuse the same request object which means that we must
577: ** keep this around until the redirected request has terminated. It also
578: ** allows us in an easy way to keep track of the number of redirections
579: ** so that we can detect endless loops.
580: */
1.17 cvs 581:
1.7 cvs 582: if (HTRequest_doRetry (request))
583: {
584: /* do we need to normalize the URL? */
585: if (strncmp (new_anchor->parent->address, "http:", 5))
586: {
587: /* Yes, so we use the pre-redirection anchor as a base name */
1.74 cvs 588: ref = AmayaParseUrl (new_anchor->parent->address,
589: me->urlName, AMAYA_PARSE_ALL);
1.7 cvs 590: if (ref)
591: {
1.74 cvs 592: TtaFreeMemory (new_anchor->parent->address);
593: new_anchor->parent->address = ref;
1.7 cvs 594: }
595: }
596:
597: /* update the current file name */
1.21 cvs 598: if (strlen (new_anchor->parent->address) > (MAX_LENGTH - 2))
1.7 cvs 599: {
1.74 cvs 600: strncpy (me->urlName, new_anchor->parent->address,
601: MAX_LENGTH - 1);
1.21 cvs 602: me->urlName[MAX_LENGTH - 1] = EOS;
1.7 cvs 603: }
604: else
1.74 cvs 605: strcpy (me->urlName, new_anchor->parent->address);
1.7 cvs 606:
1.38 cvs 607: ChopURL (me->status_urlName, me->urlName);
608:
1.7 cvs 609: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_RED_FETCHING),
1.38 cvs 610: me->status_urlName);
1.7 cvs 611:
612: /* Start request with new credentials */
1.64 cvs 613:
1.79 cvs 614: if (HTRequest_outputStream (me->request) != NULL) {
1.85 cvs 615: AHTFWriter_FREE (request->output_stream);
616: if (me->output != stdout) { /* Are we writing to a file? */
1.74 cvs 617: #ifdef DEBUG_LIBWWW
1.79 cvs 618: fprintf (stderr, "redirection_handler: New URL is %s, closing "
1.74 cvs 619: "FILE %p\n", me->urlName, me->output);
620: #endif
1.79 cvs 621: fclose (me->output);
622: me->output = NULL;
623: }
1.64 cvs 624: }
1.68 cvs 625:
1.69 cvs 626: me->reqStatus = HT_NEW; /* reset the status */
1.7 cvs 627: if (me->method == METHOD_PUT || me->method == METHOD_POST) /* PUT, POST etc. */
1.74 cvs 628: status = HTLoadAbsolute (me->urlName, request);
1.7 cvs 629: else
1.74 cvs 630: HTLoadAnchor (new_anchor, request);
1.7 cvs 631: }
632: else
1.68 cvs 633: {
1.74 cvs 634: HTRequest_addError (request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
635: NULL, 0, "HTRedirectFilter");
636: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REDIRECTIONS_LIMIT),
637: NULL);
638: if (me->error_html)
639: DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
640: /* so that we can show the error message */
1.68 cvs 641: }
642:
1.4 cvs 643: /*
1.74 cvs 644: ** By returning HT_ERROR we make sure that this is the last handler to be
645: ** called. We do this as we don't want any other filter to delete the
646: ** request object now when we have just started a new one ourselves
647: */
1.4 cvs 648: return HT_ERROR;
649: }
650:
1.5 cvs 651: /*----------------------------------------------------------------------
1.17 cvs 652: terminate_handler
653: this function is registered to handle the result of the request
1.5 cvs 654: ----------------------------------------------------------------------*/
1.4 cvs 655: #if __STDC__
1.7 cvs 656: static int terminate_handler (HTRequest * request, HTResponse * response, void *context, int status)
1.4 cvs 657: #else
658: static int terminate_handler (request, response, context, status)
659: HTRequest *request;
660: HTResponse *response;
661: void *context;
662: int status;
663: #endif
664: {
665: AHTReqContext *me = (AHTReqContext *) HTRequest_context (request);
1.13 cvs 666: boolean error_flag;
1.4 cvs 667:
668: if (!me)
1.74 cvs 669: return HT_OK; /* not an Amaya request */
670:
1.80 cvs 671: /* if Amaya was killed, treat with this request as if it were
672: issued by a Stop button event */
673: if (!AmayaIsAlive)
674: me->reqStatus = HT_ABORT;
1.74 cvs 675:
1.77 cvs 676: if (status == HT_LOADED ||
677: status == HT_CREATED ||
1.79 cvs 678: status == HT_NO_DATA ||
679: me->reqStatus == HT_ABORT)
1.74 cvs 680: error_flag = FALSE;
1.13 cvs 681: else
1.74 cvs 682: error_flag = TRUE;
683:
1.4 cvs 684: /* output any errors from the server */
1.74 cvs 685:
1.77 cvs 686: /*
1.74 cvs 687: ** me->output = output file which will receive an html file
688: ** me->error_html = yes, output HTML errors in the screen
689: ** request->error_stack == if there are any errors, they will be here
690: ** me->error_stream_size If it's != 0 means an error message has already
691: ** been written to the stack
692: */
693:
1.4 cvs 694: /* First, we verify if there are any errors and if they are not
1.17 cvs 695: ** yet written to the error stack. If no, then let's try to write them
696: ** ourselves
697: */
1.74 cvs 698:
699: #ifdef DEBUG_LIBWWW
700: fprintf (stderr, "terminate_handler: URL is "
701: "%s, closing FILE %p status is %d\n", me->urlName, me->output,
702: status);
1.69 cvs 703: #endif
1.74 cvs 704:
1.69 cvs 705: if (me->output && me->output != stdout)
706: {
1.74 cvs 707: /* we are writing to a file */
708: if (me->reqStatus != HT_ABORT)
709: { /* if the request was not aborted and */
1.80 cvs 710: if (error_flag &&
711: me->error_html == TRUE)
712: /* there were some errors and we want to print them */
713: {
714: if (me->error_stream_size == 0)/* and the stream is empty */
715: AHTError_MemPrint (request); /* copy errors from
1.74 cvs 716: **the error stack
1.77 cvs 717: ** into the error stream */
1.80 cvs 718: if (me->error_stream)
719: { /* if the stream is non-empty */
720: fprintf (me->output, me->error_stream);/* output the errors */
1.85 cvs 721: /* Clear the error context, so that we can deal with
722: this answer as if it were a normal reply */
723: HTError_deleteAll( HTRequest_error (request));
724: HTRequest_setError (request, NULL);
725: error_flag = 0;
1.74 cvs 726: }
727: } /* if error_stack */
1.85 cvs 728: }
729:
730: /* if != HT_ABORT */
731:
732: #ifdef DEBUG_LIBWWW
733: fprintf (stderr, "terminate_handler: URL is %s, closing "
734: "FILE %p\n", me->urlName, me->output);
735: #endif
1.74 cvs 736: fclose (me->output);
737: me->output = NULL;
1.69 cvs 738: }
1.80 cvs 739:
1.74 cvs 740:
1.80 cvs 741: if (error_flag)
742: me->reqStatus = HT_ERR;
743: else if (me->reqStatus != HT_ABORT)
744: me->reqStatus = HT_END;
745:
1.88 cvs 746: /* copy the content_type */
747:
748: {
749: char *content_type = request->anchor->content_type->name;
750:
751: if (content_type && content_type [0] && me->content_type)
752: {
753: /* libwww gives www/unknown when it gets an error. As this is
754: an HTML test, we force the type to text/html */
755: if (!strcmp (content_type, "www/unknown"))
756: {
757: strcpy (me->content_type, "text/html");
758: }
759: else
760: {
761: strncpy (me->content_type, content_type,
762: NAME_LENGTH -1);
763: me->content_type [NAME_LENGTH-1] = '\0';
764:
765: /* Content-Type can be specified by a server's admin. To be on
766: the safe side, we normalize its case */
767: ConvertToLowerCase (me->content_type);
768: }
769: #ifdef DEBUG_LIBWWW
770: fprintf (stderr, "content type is: %s\n", me->content_type);
771: #endif /* DEBUG_LIBWWW */
772: }
773: }
774:
1.80 cvs 775: /* don't remove or Xt will hang up during the PUT */
776:
1.28 cvs 777: if (AmayaIsAlive && ((me->method == METHOD_POST) ||
778: (me->method == METHOD_PUT)))
1.7 cvs 779: {
1.80 cvs 780: PrintTerminateStatus (me, status);
781:
782: }
783:
1.72 cvs 784: #ifdef _WINDOWS
1.80 cvs 785: /* Try to add this to AHTEventrg.c */
786: ProcessTerminateRequest (me);
787: #endif /* WINDOWS */
1.71 cvs 788:
789: return HT_OK;
1.4 cvs 790: }
791:
1.5 cvs 792: /*----------------------------------------------------------------------
1.17 cvs 793: AHTLoadTerminate_handler
1.74 cvs 794: this is an application "AFTER" Callback. It's called by the library
795: when a request has ended, so that we can setup the correct status.
1.5 cvs 796: ----------------------------------------------------------------------*/
1.4 cvs 797:
798: #ifdef __STDC__
1.7 cvs 799: static int AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4 cvs 800: #else
801: static int AHTLoadTerminate_handler (request, response, param, status)
802: HTRequest *request;
803: HTResponse *response;
804: void *param;
805: int status;
1.69 cvs 806:
1.4 cvs 807: #endif
808: {
809: AHTReqContext *me = HTRequest_context (request);
810: HTAlertCallback *cbf;
811: AHTDocId_Status *docid_status;
812:
1.7 cvs 813: switch (status)
814: {
815: case HT_LOADED:
816: if (PROT_TRACE)
817: HTTrace ("Load End.... OK: `%s\' has been accessed\n",
1.38 cvs 818: me->status_urlName);
1.4 cvs 819:
1.7 cvs 820: docid_status = GetDocIdStatus (me->docid,
821: Amaya->docid_status);
822:
823: if (docid_status != NULL && docid_status->counter > 1)
1.74 cvs 824: TtaSetStatus (me->docid, 1,
825: TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
826: me->status_urlName);
1.7 cvs 827: break;
828:
829: case HT_NO_DATA:
830: if (PROT_TRACE)
1.74 cvs 831: HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n",
832: me->status_urlName);
833: TtaSetStatus (me->docid, 1,
834: TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
1.38 cvs 835: me->status_urlName);
1.7 cvs 836: break;
837:
838: case HT_INTERRUPTED:
839: if (PROT_TRACE)
1.74 cvs 840: HTTrace ("Load End.... INTERRUPTED: `%s\'\n",
841: me->status_urlName);
842: TtaSetStatus (me->docid, 1,
843: TtaGetMessage (AMAYA, AM_LOAD_ABORT),
844: NULL);
1.7 cvs 845: break;
846:
847: case HT_RETRY:
848: if (PROT_TRACE)
849: HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
850: HTResponse_retryTime (response));
1.74 cvs 851: TtaSetStatus (me->docid, 1,
852: TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
1.38 cvs 853: me->status_urlName);
1.7 cvs 854: break;
855:
856: case HT_ERROR:
857:
858: cbf = HTAlert_find (HT_A_MESSAGE);
859: if (cbf)
860: (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
861: HTRequest_error (request), NULL);
862: break;
863:
864: if (PROT_TRACE)
865: HTTrace ("Load End.... ERROR: Can't access `%s\'\n",
1.74 cvs 866: me->status_urlName ? me->status_urlName :"<UNKNOWN>");
867: TtaSetStatus (me->docid, 1,
868: TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1.38 cvs 869: me->status_urlName ? me->status_urlName : "<UNKNOWN>");
1.7 cvs 870: break;
871: default:
872: if (PROT_TRACE)
873: HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
874: break;
875: }
876:
1.4 cvs 877: return HT_OK;
878: }
879:
1.27 cvs 880: /*----------------------------------------------------------------------
881: AHTAcceptTypesInit
1.74 cvs 882: This function prepares the Accept header used by Amaya during
883: the HTTP content negotiation phase
1.27 cvs 884: ----------------------------------------------------------------------*/
885: #ifdef __STDC__
886: static void AHTAcceptTypesInit (HTList *c)
887: #else /* __STDC__ */
888: static void AHTAcceptTypesInit (c)
889: HTList *c;
890: #endif /* __STDC__ */
891: {
892: if (c == (HTList *) NULL)
893: return;
894:
895: /* define here all the mime types that Amaya can accept */
896:
1.74 cvs 897: HTConversion_add (c, "image/png", "www/present",
898: HTThroughLine, 1.0, 0.0, 0.0);
899: HTConversion_add (c, "image/jpeg", "www/present",
900: HTThroughLine, 1.0, 0.0, 0.0);
901: HTConversion_add (c, "image/gif", "www/present",
902: HTThroughLine, 1.0, 0.0, 0.0);
903: HTConversion_add (c, "image/xbm", "www/present",
904: HTThroughLine, 1.0, 0.0, 0.0);
905: HTConversion_add (c, "image/xpm", "www/present",
906: HTThroughLine, 1.0, 0.0, 0.0);
907: HTConversion_add (c, "application/postscript",
908: "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1.4 cvs 909:
1.27 cvs 910: /* Define here the equivalences between MIME types and file extensions for
911: the types that Amaya can display */
912:
913: /* Register the default set of file suffix bindings */
914: HTFileInit ();
915:
916: /* Don't do any case distinction */
917: HTBind_caseSensitive (FALSE);
918: }
1.4 cvs 919:
1.5 cvs 920: /*----------------------------------------------------------------------
1.17 cvs 921: AHTConverterInit
922: Bindings between a source media type and a destination media type
923: (conversion).
1.5 cvs 924: ----------------------------------------------------------------------*/
1.15 cvs 925: #ifdef __STDC__
926: static void AHTConverterInit (HTList *c)
927: #else /* __STDC__ */
928: static void AHTConverterInit (c)
929: HTList *c;
930: #endif /* __STDC__ */
1.4 cvs 931: {
932:
933: /* Handler for custom http error messages */
1.7 cvs 934: HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4 cvs 935:
936: /*
937: ** These are converters that converts to something other than www/present,
938: ** that is not directly outputting someting to the user on the screen
939: */
940:
941: HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 1.0, 0.0, 0.0);
942: HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter,
943: 1.0, 0.0, 0.0);
944: HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader,
945: 1.0, 0.0, 0.0);
946: HTConversion_add (c, "multipart/*", "*/*", HTBoundary,
947: 1.0, 0.0, 0.0);
948: HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML,
949: 1.0, 0.0, 0.0);
950:
951:
952: /*
953: ** The following conversions are converting ASCII output from various
954: ** protocols to HTML objects.
955: */
956: HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new,
957: 1.0, 0.0, 0.0);
958: HTConversion_add (c, "text/x-nntp-list", "*/*", HTNewsList,
959: 1.0, 0.0, 0.0);
960: HTConversion_add (c, "text/x-nntp-over", "*/*", HTNewsGroup,
961: 1.0, 0.0, 0.0);
962:
963:
964: /*
965: ** We also register a special content type guess stream that can figure out
966: ** the content type by reading the first bytes of the stream
967: */
968: HTConversion_add (c, "www/unknown", "*/*", HTGuess_new,
969: 1.0, 0.0, 0.0);
970:
971: /*
972: ** Register a persistent cache stream which can save an object to local
973: ** file
974: */
975: HTConversion_add (c, "www/cache", "*/*", HTCacheWriter,
976: 1.0, 0.0, 0.0);
977:
978: /*
979: ** This dumps all other formats to local disk without any further
980: ** action taken
981: */
982: HTConversion_add (c, "*/*", "www/present", HTSaveLocally,
983: 0.3, 0.0, 0.0);
984:
985: }
986:
1.27 cvs 987:
1.15 cvs 988: /*----------------------------------------------------------------------
1.17 cvs 989: AHTProtocolInit
990: Registers all amaya supported protocols.
1.15 cvs 991: ----------------------------------------------------------------------*/
1.4 cvs 992: static void AHTProtocolInit (void)
993: {
994:
1.17 cvs 995: /*
1.74 cvs 996: NB. Preemptive == YES means Blocking requests
997: Non-preemptive == NO means Non-blocking requests
1.17 cvs 998: */
1.4 cvs 999:
1.63 cvs 1000: HTProtocol_add ("http", "buffered_tcp", NO, HTLoadHTTP, NULL);
1.4 cvs 1001: /* HTProtocol_add ("http", "tcp", NO, HTLoadHTTP, NULL); */
1002: HTProtocol_add ("file", "local", NO, HTLoadFile, NULL);
1003: HTProtocol_add ("cache", "local", NO, HTLoadCache, NULL);
1.36 cvs 1004: HTProtocol_add ("ftp", "tcp", NO, HTLoadFTP, NULL);
1.17 cvs 1005: #if 0 /* experimental code */
1.4 cvs 1006: HTProtocol_add ("telnet", "", YES, HTLoadTelnet, NULL);
1007: HTProtocol_add ("tn3270", "", YES, HTLoadTelnet, NULL);
1008: HTProtocol_add ("rlogin", "", YES, HTLoadTelnet, NULL);
1009: HTProtocol_add ("nntp", "tcp", NO, HTLoadNews, NULL);
1010: HTProtocol_add ("news", "tcp", NO, HTLoadNews, NULL);
1.17 cvs 1011: #endif
1.4 cvs 1012: }
1013:
1.15 cvs 1014: /*----------------------------------------------------------------------
1.17 cvs 1015: AHTNetInit
1016: Reegisters "before" and "after" request filters.
1.15 cvs 1017: ----------------------------------------------------------------------*/
1.4 cvs 1018: static void AHTNetInit (void)
1019: {
1020:
1021: /* Register BEFORE filters
1.74 cvs 1022: ** The BEFORE filters handle proxies, caches, rule files etc.
1023: ** The filters are called in the order by which the are registered
1024: ** Not done automaticly - may be done by application!
1025: */
1026:
1.4 cvs 1027:
1.74 cvs 1028: HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, 6);
1029: HTNet_addBefore (HTProxyFilter, NULL, NULL, 10);
1.85 cvs 1030: /* HTNet_addBefore (AHTOpen_file, NULL, NULL, 11); */
1031: HTHost_setActivateRequestCallback (AHTOpen_file);
1.4 cvs 1032:
1033: /* register AFTER filters
1.74 cvs 1034: ** The AFTER filters handle error messages, logging, redirection,
1035: ** authentication etc.
1036: ** The filters are called in the order by which the are registered
1037: ** Not done automaticly - may be done by application!
1038: */
1.4 cvs 1039:
1.82 cvs 1040: HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS, HT_FILTER_MIDDLE);
1041: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT, HT_FILTER_MIDDLE);
1042: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT, HT_FILTER_MIDDLE);
1043: HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY, HT_FILTER_MIDDLE);
1.51 cvs 1044: HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1045: /* handles all errors */
1.7 cvs 1046: HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4 cvs 1047: }
1048:
1.15 cvs 1049: /*----------------------------------------------------------------------
1.17 cvs 1050: AHTAlertInit
1051: Register alert messages and their callbacks.
1.15 cvs 1052: ----------------------------------------------------------------------*/
1053: #ifdef __STDC__
1054: static void AHTAlertInit (void)
1055: #else
1056: static void AHTAlertInit ()
1057: #endif
1058: {
1059: HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.70 cvs 1060: # ifndef _WINDOWS
1.83 cvs 1061: /* HTAlert_add ((HTAlertCallback *) Add_NewSocket_to_Loop, HT_PROG_CONNECT); */
1.70 cvs 1062: # else /* _WINDOWS */
1063: HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
1064: # endif /* _WINDOWS */
1.15 cvs 1065: HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48 cvs 1066: HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG)); /* process all messages except debug ones*/
1.15 cvs 1067: HTAlert_add (AHTConfirm, HT_A_CONFIRM);
1068: HTAlert_add (AHTPrompt, HT_A_PROMPT);
1069: HTAlert_add (AHTPromptPassword, HT_A_SECRET);
1070: HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
1071: }
1072:
1073: /*----------------------------------------------------------------------
1.17 cvs 1074: AHTProfile_newAmaya
1075: creates the Amaya client profile for libwww.
1.15 cvs 1076: ----------------------------------------------------------------------*/
1077: #ifdef __STDC__
1078: static void AHTProfile_newAmaya (char *AppName, char *AppVersion)
1079: #else /* __STDC__ */
1080: static void AHTProfile_newAmaya (AppName, AppVersion)
1081: char *AppName;
1082: char *AppVersion;
1083: #endif /* __STDC__ */
1.4 cvs 1084: {
1085: /* If the Library is not already initialized then do it */
1086: if (!HTLib_isInitialized ())
1087: HTLibInit (AppName, AppVersion);
1088:
1089: if (!converters)
1090: converters = HTList_new ();
1.27 cvs 1091: if (!acceptTypes)
1092: acceptTypes = HTList_new ();
1.4 cvs 1093: if (!encodings)
1094: encodings = HTList_new ();
1095:
1096: /* Register the default set of transport protocols */
1097: HTTransportInit ();
1098:
1099: /* Register the default set of application protocol modules */
1100: AHTProtocolInit ();
1101:
1102: /* Enable the persistent cache */
1103: /* HTCacheInit (NULL, 20); */
1104:
1105: /* Register the default set of BEFORE and AFTER filters */
1106: AHTNetInit ();
1107:
1108: /* Set up the default set of Authentication schemes */
1109: HTAAInit ();
1110:
1111: /* Get any proxy or gateway environment variables */
1112: HTProxy_getEnvVar ();
1113:
1114: /* Register the default set of converters */
1115: AHTConverterInit (converters);
1.27 cvs 1116: AHTAcceptTypesInit (acceptTypes);
1.4 cvs 1117: HTFormat_setConversion (converters);
1118:
1119: /* Register the default set of transfer encoders and decoders */
1120: HTEncoderInit (encodings); /* chunks ??? */
1121: HTFormat_setTransferCoding (encodings);
1122:
1123: /* Register the default set of MIME header parsers */
1.74 cvs 1124: HTMIMEInit (); /* must be called again for language selector */
1.4 cvs 1125:
1126: /* Register the default set of Icons for directory listings */
1.27 cvs 1127: /*HTIconInit(NULL); *//* experimental */
1.4 cvs 1128:
1129: /* Register the default set of messages and dialog functions */
1130: AHTAlertInit ();
1131: HTAlert_setInteractive (YES);
1132: }
1133:
1.5 cvs 1134: /*----------------------------------------------------------------------
1.17 cvs 1135: AHTProfile_delete
1136: deletes the Amaya client profile.
1.5 cvs 1137: ----------------------------------------------------------------------*/
1.4 cvs 1138: #ifdef __STDC__
1139: static void AHTProfile_delete (void)
1140: #else
1141: static void AHTProfile_delete ()
1.7 cvs 1142: #endif /* __STDC__ */
1.4 cvs 1143: {
1.22 cvs 1144:
1145: /* free the Amaya global context */
1.74 cvs 1146: if (!converters)
1147: HTConversion_deleteAll (converters);
1148: if (!acceptTypes)
1149: HTConversion_deleteAll (acceptTypes);
1150: if (!encodings)
1151: HTCoding_deleteAll (encodings);
1152:
1153: HTList_delete (Amaya->docid_status);
1154: HTList_delete (Amaya->reqlist);
1155: TtaFreeMemory (Amaya);
1156: {
1.61 cvs 1157:
1.74 cvs 1158: if (HTLib_isInitialized ())
1159:
1.61 cvs 1160: # ifdef _WINDOWS
1161: HTEventTerminate ();
1162: # endif _WINDOWS;
1.74 cvs 1163:
1164: /* Clean up the persistent cache (if any) */
1165: HTCacheTerminate ();
1166:
1167: /* Clean up all the global preferences */
1168: HTFormat_deleteAll ();
1169:
1170: /* Terminate libwww */
1171: HTLibTerminate ();
1172: }
1.4 cvs 1173: }
1174:
1.5 cvs 1175: /*----------------------------------------------------------------------
1.17 cvs 1176: QueryInit
1177: initializes the libwww interface
1.5 cvs 1178: ----------------------------------------------------------------------*/
1.4 cvs 1179: #ifdef __STDC__
1180: void QueryInit ()
1181: #else
1182: void QueryInit ()
1183: #endif
1184: {
1185:
1.24 cvs 1186: AmayaIsAlive = TRUE;
1.4 cvs 1187: AHTProfile_newAmaya (HTAppName, HTAppVersion);
1188:
1.69 cvs 1189: /* New AHTBridge stuff */
1.4 cvs 1190:
1.54 cvs 1191: # ifdef _WINDOWS
1.75 cvs 1192: AHTEventInit ();
1.54 cvs 1193: # endif _WINDOWS;
1.72 cvs 1194:
1.70 cvs 1195: HTEvent_setRegisterCallback (AHTEvent_register);
1.78 cvs 1196: /*** a effacer ***/
1197: HTEvent_setUnregisterCallback (AHTEvent_unregister);
1198: /*** ***/
1.4 cvs 1199:
1.72 cvs 1200: # ifndef _WINDOWS
1201: HTEvent_setUnregisterCallback (AHTEvent_unregister);
1202: # endif /* _WINDOWS */
1203:
1.74 cvs 1204: #ifdef DEBUG_LIBWWW
1205: WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | PROT_TRACE;
1206: #endif
1.4 cvs 1207:
1208: /* Trace activation (for debugging) */
1.7 cvs 1209: /*
1.4 cvs 1210: WWW_TraceFlag = SHOW_APP_TRACE | SHOW_UTIL_TRACE |
1211: SHOW_BIND_TRACE | SHOW_THREAD_TRACE |
1212: SHOW_STREAM_TRACE | SHOW_PROTOCOL_TRACE |
1213: SHOW_URI_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
1214: SHOW_CORE_TRACE;
1215:
1.7 cvs 1216: */
1.4 cvs 1217:
1218: /***
1219: WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
1220: SHOW_PROTOCOL_TRACE| SHOW_APP_TRACE | SHOW_UTIL_TRACE;
1221: ***/
1222:
1223: /* Setting up other user interfaces */
1224:
1225: /* Setting up different network parameters */
1.17 cvs 1226: /* Maximum number of simultaneous open sockets */
1.4 cvs 1227: HTNet_setMaxSocket (8);
1.75 cvs 1228: /* different network services timeouts */
1.4 cvs 1229: HTDNS_setTimeout (3600);
1.75 cvs 1230: #ifdef _WINDOWS
1231: /* under windows, the libwww persistent socket handling has
1232: ** some bugs. The following line inhibits idle socket reusal.
1233: ** this is a bit slower, but avoids crashes and gives us time
1234: ** to distribute Amaya before having to patch up libwww.
1235: */
1.76 cvs 1236: HTHost_setPersistTimeout (-1L);
1.75 cvs 1237: #else
1.76 cvs 1238: HTHost_setPersistTimeout (60L);
1.75 cvs 1239: #endif /* _WINDOWS */
1240:
1.17 cvs 1241: /* Cache is disabled in this version */
1.4 cvs 1242: HTCacheMode_setEnabled (0);
1243:
1244: /* Initialization of the global context */
1245: Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
1246: Amaya->reqlist = HTList_new ();
1247: Amaya->docid_status = HTList_new ();
1248: Amaya->open_requests = 0;
1.74 cvs 1249:
1.4 cvs 1250: #ifdef CATCH_SIG
1.18 cvs 1251: signal (SIGPIPE, SIG_IGN);
1.4 cvs 1252: #endif
1.15 cvs 1253: }
1254:
1.69 cvs 1255: #ifndef _WINDOWS
1.15 cvs 1256: /*----------------------------------------------------------------------
1.17 cvs 1257: LoopForStop
1258: a copy of the Thop event loop so we can handle the stop button.
1.69 cvs 1259: Not useful for windows code (Ramzi).
1.15 cvs 1260: ----------------------------------------------------------------------*/
1261: #ifdef __STDC__
1262: static int LoopForStop (AHTReqContext * me)
1263: #else
1264: static int LoopForStop (AHTReqContext * me)
1265: #endif
1266: {
1267:
1.25 cvs 1268: extern ThotAppContext app_cont;
1.51 cvs 1269: XEvent ev;
1270: XtInputMask status;
1.17 cvs 1271: int status_req = HT_OK;
1.15 cvs 1272:
1273: /* to test the async calls */
1.17 cvs 1274: /* Loop while waiting for new events, exists when the request is over */
1.15 cvs 1275: while (me->reqStatus != HT_ABORT &&
1276: me->reqStatus != HT_END &&
1.69 cvs 1277: me->reqStatus != HT_ERR) {
1278: if (!AmayaIsAlive)
1279: /* Amaya was killed by one of the callback handlers */
1280: exit (0);
1281:
1282: status = XtAppPending (app_cont);
1283: if (status & XtIMXEvent) {
1284: XtAppNextEvent (app_cont, &ev);
1285: TtaHandleOneEvent (&ev);
1286: } else if (status & (XtIMAll & (~XtIMXEvent))) {
1287: XtAppProcessEvent (app_cont, (XtIMAll & (~XtIMXEvent)));
1288: } else {
1289: XtAppNextEvent (app_cont, &ev);
1290: TtaHandleOneEvent (&ev);
1291: }
1292: }
1.4 cvs 1293:
1.69 cvs 1294: switch (me->reqStatus) {
1295: case HT_ERR:
1296: case HT_ABORT:
1.15 cvs 1297: status_req = HT_ERROR;
1298: break;
1299:
1.69 cvs 1300: case HT_END:
1.15 cvs 1301: status_req = HT_OK;
1302: break;
1303:
1.69 cvs 1304: default:
1.15 cvs 1305: break;
1.69 cvs 1306: }
1.15 cvs 1307: return (status_req);
1.4 cvs 1308: }
1.69 cvs 1309: #endif /* _WINDOWS */
1.4 cvs 1310:
1.5 cvs 1311: /*----------------------------------------------------------------------
1.15 cvs 1312: QueryClose
1.21 cvs 1313: closes all existing threads, frees all non-automatically deallocated
1314: memory and then ends libwww.
1.5 cvs 1315: ----------------------------------------------------------------------*/
1.4 cvs 1316: void QueryClose ()
1317: {
1.24 cvs 1318:
1319: AmayaIsAlive = FALSE;
1320:
1.21 cvs 1321: /* remove all the handlers and callbacks that may output a message to
1322: a non-existent Amaya window */
1323:
1324: HTNet_deleteAfter (AHTLoadTerminate_handler);
1325: HTNet_deleteAfter (redirection_handler);
1326: HTAlertCall_deleteAll (HTAlert_global () );
1.23 cvs 1327: HTAlert_setGlobal ((HTList *) NULL);
1.24 cvs 1328: HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
1.27 cvs 1329: HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
1.87 cvs 1330: HTHost_setActivateRequestCallback (NULL);
1.4 cvs 1331: Thread_deleteAll ();
1.21 cvs 1332:
1.4 cvs 1333: HTProxy_deleteAll ();
1334: HTNoProxy_deleteAll ();
1335: HTGateway_deleteAll ();
1336: AHTProfile_delete ();
1337: }
1338:
1.5 cvs 1339: /*----------------------------------------------------------------------
1.15 cvs 1340: GetObjectWWW
1.17 cvs 1341: this function requests a resource designated by a URLname into a
1342: temporary filename. The download can come from a simple GET operation,
1343: or can come from POSTING/GETTING a form. In the latter
1344: case, the function receives a query string to send to the server.
1345:
1.5 cvs 1346: 4 file retrieval modes are proposed:
1347: AMAYA_SYNC : blocking mode
1348: AMAYA_ISYNC : incremental, blocking mode
1349: AMAYA_ASYNC : non-blocking mode
1350: AMAYA_IASYNC : incremental, non-blocking mode
1351:
1352: In the incremental mode, each time a package arrives, it will be
1353: stored in the temporary file. In addition, if an
1354: incremental_callback function is defined, this function will be
1355: called and handled a copy of the newly received data package.
1356: Finally, if a terminate_callback function is defined, it will be
1357: invoked when the request terminates. The caller of this function
1.4 cvs 1358: can define two different contexts to be passed to the callback
1359: functions.
1360:
1361: When the function is called with the SYNC mode, the function will
1362: return only when the requested file has been loaded.
1363: The ASYNC mode will immediately return after setting up the
1364: call.
1365:
1366: Notes:
1367: At the end of a succesful request, the urlName string contains the
1368: name of the actually retrieved URL. As a URL can change over the time,
1369: (e.g., be redirected elsewhere), it is advised that the function
1.17 cvs 1370: caller verify the value of the urlName variable at the end of
1.4 cvs 1371: a request.
1372:
1373: Inputs:
1374: - docid Document identifier for the set of objects being
1375: retrieved.
1376: - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
1377: - outputfile A pointer to an empty string of MAX_URL_LENGTH.
1378: - mode The retrieval mode.
1379: - incremental_cbf
1380: - context_icbf
1381: Callback and context for the incremental modes
1382: - terminate_cbf
1383: - context_icbf
1384: Callback and context for a terminate handler
1.17 cvs 1385: -error_html if TRUE, then display any server error message as an
1386: HTML document.
1.88 cvs 1387: - content_type a string
1388:
1.4 cvs 1389: Outputs:
1390: - urlName The URL that was retrieved
1391: - outputfile The name of the temporary file which holds the
1392: retrieved data. (Only in case of success)
1.88 cvs 1393: - if content_type wasn't NULL, it will contain a copy of the parameter
1394: sent in the HTTP answer
1.4 cvs 1395: Returns:
1396: HT_ERROR
1397: HT_OK
1.5 cvs 1398:
1399: ----------------------------------------------------------------------*/
1.4 cvs 1400: #ifdef __STDC__
1.52 cvs 1401: int GetObjectWWW (int docid, char* urlName, char* postString, char* outputfile, int mode,
1402: TIcbf* incremental_cbf, void* context_icbf, TTcbf* terminate_cbf,
1.88 cvs 1403: void* context_tcbf, boolean error_html, char *content_type)
1.4 cvs 1404: #else
1.52 cvs 1405: int GetObjectWWW (docid, urlName, postString, outputfile, mode, incremental_cbf, context_icbf,
1.88 cvs 1406: terminate_cbf, context_tcbf, error_html, content_type)
1.73 cvs 1407: int docid;
1408: char *urlName;
1409: char *postString;
1410: char *outputfile;
1411: int mode;
1412: TIcbf *incremental_cbf;
1413: void *context_icbf;
1414: TTcbf *terminate_cbf;
1415: void *context_tcbf;
1416: boolean error_html;
1.88 cvs 1417: char *content_type;
1.4 cvs 1418: #endif
1419: {
1420: AHTReqContext *me;
1421: char *ref;
1422: int status;
1.7 cvs 1423:
1.69 cvs 1424: if (urlName == NULL || docid == 0 || outputfile == NULL) {
1425: /* no file to be loaded */
1426: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1427:
1428: if (error_html)
1429: /* so we can show the error message */
1430: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1431: return HT_ERROR;
1432:
1433: }
1.7 cvs 1434:
1.4 cvs 1435: /* do we support this protocol? */
1.69 cvs 1436: if (IsValidProtocol (urlName) == NO) {
1437: /* return error */
1438: outputfile[0] = EOS; /* file could not be opened */
1439: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL), urlName);
1440:
1441: if (error_html)
1442: /* so we can show the error message */
1443: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1444: return HT_ERROR;
1445: }
1.4 cvs 1446:
1.58 cvs 1447: /*create a tempfilename */
1.59 cvs 1448: sprintf (outputfile, "%s%c%d%c%04dAM", TempFileDirectory, DIR_SEP, docid, DIR_SEP, object_counter);
1.4 cvs 1449:
1450: /* update the object_counter */
1451: object_counter++;
1452: /* normalize the URL */
1.45 cvs 1453: ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4 cvs 1454: /* should we abort the request if we could not normalize the url? */
1.69 cvs 1455: if (ref == (char*) NULL || ref[0] == EOS) {
1456: /*error */
1457: outputfile[0] = EOS;
1458: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1459:
1460: if (error_html)
1461: /* so we can show the error message */
1462: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.4 cvs 1463:
1.69 cvs 1464: return HT_ERROR;
1465: }
1.4 cvs 1466: /* verify if that file name existed */
1.9 cvs 1467: if (TtaFileExist (outputfile))
1.77 cvs 1468: TtaFileUnlink (outputfile);
1.4 cvs 1469:
1470: /* Initialize the request structure */
1471: me = AHTReqContext_new (docid);
1.69 cvs 1472: if (me == NULL) {
1.77 cvs 1473: outputfile[0] = EOS;
1474: /* need an error message here */
1475: TtaFreeMemory (ref);
1476: return (HT_ERROR);
1.69 cvs 1477: }
1.77 cvs 1478:
1.4 cvs 1479: /* Specific initializations for POST and GET */
1.69 cvs 1480: if (mode & AMAYA_FORM_POST) {
1.77 cvs 1481: me->method = METHOD_POST;
1482: if (postString) {
1483: me->mem_ptr = postString;
1484: me->block_size = strlen (postString);
1485: } else {
1486: me->mem_ptr = "";
1487: me->block_size = 0;
1488: }
1489: HTRequest_setMethod (me->request, METHOD_POST);
1.69 cvs 1490: HTRequest_setPostCallback (me->request, AHTUpload_callback);
1491: } else {
1.77 cvs 1492: me->method = METHOD_GET;
1493: me->dest = (HTParentAnchor *) NULL; /*useful only for PUT and POST methods */
1494: if (!HasKnownFileSuffix (ref))
1495: HTRequest_setConversion(me->request, acceptTypes, TRUE);
1.69 cvs 1496: }
1.4 cvs 1497:
1498: /* Common initialization */
1499: me->mode = mode;
1500: me->error_html = error_html;
1501: me->incremental_cbf = incremental_cbf;
1502: me->context_icbf = context_icbf;
1503: me->terminate_cbf = terminate_cbf;
1504: me->context_tcbf = context_tcbf;
1.88 cvs 1505: me->content_type = content_type;
1.64 cvs 1506:
1.69 cvs 1507: /* for the async. request modes, we need to have our
1.4 cvs 1508: own copy of outputfile and urlname
1509: */
1510:
1.69 cvs 1511: if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) {
1.77 cvs 1512: char* tmp;
1513:
1.69 cvs 1514: tmp = TtaGetMemory (strlen (outputfile) + 1);
1515: strcpy (tmp, outputfile);
1516: me->outputfile = tmp;
1517:
1518: tmp = TtaGetMemory (MAX_LENGTH + 1);
1519: strncpy (tmp, urlName, MAX_LENGTH);
1520: tmp[MAX_LENGTH] = EOS;
1521: me->urlName = tmp;
1522: # ifdef _WINDOWS
1523: HTRequest_setPreemptive (me->request, NO);
1524: } else {
1.77 cvs 1525: me->outputfile = outputfile;
1526: me->urlName = urlName;
1527: HTRequest_setPreemptive (me->request, YES);
1.61 cvs 1528: }
1.69 cvs 1529: # else /* _WINDOWS */
1530: } else {
1.77 cvs 1531: me->outputfile = outputfile;
1532: me->urlName = urlName;
1.61 cvs 1533: }
1.77 cvs 1534: /***
1.57 cvs 1535: Change for taking into account the stop button:
1536: The requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1537: we will loop until the document has been received or a stop signal
1538: generated
1.77 cvs 1539: ****/
1540: HTRequest_setPreemptive (me->request, NO);
1.69 cvs 1541: # endif /* _WINDOWS */
1.61 cvs 1542:
1543: /* prepare the URLname that will be displayed in teh status bar */
1544: ChopURL (me->status_urlName, me->urlName);
1545:
1.77 cvs 1546: TtaSetStatus (me->docid, 1,
1547: TtaGetMessage (AMAYA, AM_FETCHING),
1548: me->status_urlName);
1.4 cvs 1549:
1550: me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45 cvs 1551: TtaFreeMemory (ref);
1.4 cvs 1552:
1.69 cvs 1553: if (mode & AMAYA_FORM_POST) {
1554: HTAnchor_setFormat ((HTParentAnchor *) me->anchor, HTAtom_for ("application/x-www-form-urlencoded"));
1555: HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1556: HTRequest_setEntityAnchor (me->request, me->anchor);
1557:
1558: status = HTLoadAbsolute (urlName, me->request);
1559: } else
1.77 cvs 1560: status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69 cvs 1561:
1.72 cvs 1562: #ifndef _WINDOWS
1.77 cvs 1563: if (status == HT_ERROR ||
1564: me->reqStatus == HT_END ||
1565: me->reqStatus == HT_ERR)
1566: {
1567: /* in case of error, free all allocated memory and exit */
1.82 cvs 1568: if (me->output) {
1.85 cvs 1569: #ifdef DEBUG_LIBWWW
1570: fprintf (stderr, "tGetObjectWWW:: URL is %s, closing "
1571: "FILE %p\n", me->urlName, me->output);
1572: #endif
1.77 cvs 1573: fclose (me->output);
1.82 cvs 1574: me->output = NULL;
1575: }
1576:
1.72 cvs 1577: if (me->reqStatus == HT_ERR) {
1.77 cvs 1578: status = HT_ERROR;
1579: /* show an error message on the status bar */
1580: DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
1581: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD), me->
1582: status_urlName);
1.72 cvs 1583: } else
1.77 cvs 1584: status = HT_OK;
1.72 cvs 1585:
1586: AHTReqContext_delete (me);
1.77 cvs 1587: } else {
1.72 cvs 1588: /* part of the stop button handler */
1589: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC)) {
1590: status = LoopForStop (me);
1591: AHTReqContext_delete (me);
1592: }
1593: }
1.79 cvs 1594: /* TtaHandlePendingEvents (); */
1.72 cvs 1595:
1596: #else /* !_WINDOWS */
1597:
1.90 ! cvs 1598: if (status == HT_ERROR ||
! 1599: me->reqStatus == HT_ERROR)
! 1600: {
! 1601: status = HT_ERROR;
1.71 cvs 1602:
1.90 ! cvs 1603: /* in case of error, close any open files, free all allocated
! 1604: memory and exit */
! 1605: if (me->output && me->output != stdout)
! 1606: {
1.85 cvs 1607: #ifdef DEBUG_LIBWWW
1.90 ! cvs 1608: fprintf (stderr, "GetObjectWWW: URL is %s, closing "
! 1609: "FILE %p\n", me->urlName, me->output);
1.85 cvs 1610: #endif
1.90 ! cvs 1611: fclose (me->output);
! 1612: me->output = NULL;
! 1613: }
1.85 cvs 1614:
1.90 ! cvs 1615: if (me->reqStatus == HT_ERR)
! 1616: {
! 1617: /* show an error message on the status bar */
! 1618: DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
! 1619: TtaSetStatus (me->docid, 1,
! 1620: TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
! 1621: me->status_urlName);
! 1622: }
! 1623: }
! 1624: else
! 1625: {
! 1626: status = HT_OK;
! 1627: }
1.69 cvs 1628:
1.77 cvs 1629: /* part of the stop button handler */
1630: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC)) {
1.90 ! cvs 1631: AHTReqContext_delete (me);
1.69 cvs 1632: }
1.90 ! cvs 1633:
1.4 cvs 1634:
1.72 cvs 1635: #endif /* !_WINDOWS */
1636:
1.4 cvs 1637: return (status);
1638: }
1639:
1.5 cvs 1640: /*----------------------------------------------------------------------
1.17 cvs 1641: PutObjectWWW
1642: frontend for uploading a resource to a URL. This function downloads
1643: a file to be uploaded into memory, it then calls UploadMemWWW to
1644: finish the job.
1645:
1.5 cvs 1646: 2 upload modes are proposed:
1647: AMAYA_SYNC : blocking mode
1648: AMAYA_ASYNC : non-blocking mode
1649:
1.4 cvs 1650: When the function is called with the SYNC mode, the function will
1651: return only when the file has been uploaded.
1652: The ASYNC mode will immediately return after setting up the
1653: call. Furthermore, at the end of an upload, the ASYNC mode will
1654: call back terminate_cbf, handling it the context defined in
1655: context_tcbf.
1656:
1657: Notes:
1658: At the end of a succesful request, the urlName string contains the
1659: name of the actually uploaded URL. As a URL can change over the time,
1660: (e.g., be redirected elsewhere), it is advised that the function
1661: caller verifies the value of the urlName variable at the end of
1662: a request.
1663:
1664: Inputs:
1665: - docid Document identifier for the set of objects being
1666: retrieved.
1667: - fileName A pointer to the local file to upload
1668: - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
1669: - mode The retrieval mode.
1670: - terminate_cbf
1671: - context_icbf
1672: Callback and context for a terminate handler
1673:
1674: Outputs:
1675: - urlName The URL that was uploaded
1676:
1677: Returns:
1678: HT_ERROR
1679: HT_OK
1.5 cvs 1680: ----------------------------------------------------------------------*/
1.4 cvs 1681: #ifdef __STDC__
1.27 cvs 1682: int PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4 cvs 1683: TTcbf * terminate_cbf, void *context_tcbf)
1684: #else
1.26 cvs 1685: int PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4 cvs 1686: ,terminate_cbf, context_tcbf)
1687: int docid;
1688: char *urlName;
1689: char *fileName;
1690: int mode;
1.27 cvs 1691: PicType contentType;
1.4 cvs 1692: TTcbf *terminate_cbf;
1693: void *context_tcbf;
1694:
1695: #endif
1696: {
1.7 cvs 1697: /*AHTReqContext *me; */
1.4 cvs 1698: int status;
1699:
1700: int fd;
1701: struct stat file_stat;
1702: char *mem_ptr;
1703: unsigned long block_size;
1704:
1.33 cvs 1705: AmayaLastHTTPErrorMsg [0] = EOS;
1706:
1.4 cvs 1707: if (urlName == NULL || docid == 0 || fileName == NULL ||
1.9 cvs 1708: !TtaFileExist (fileName))
1.4 cvs 1709: /* no file to be uploaded */
1710: return HT_ERROR;
1711:
1712: /* do we support this protocol? */
1.7 cvs 1713: if (IsValidProtocol (urlName) == NO)
1714: {
1715: /* return error */
1.77 cvs 1716: TtaSetStatus (docid, 1,
1717: TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7 cvs 1718: urlName);
1719: return HT_ERROR;
1720: }
1.4 cvs 1721: /* read the file into memory */
1.70 cvs 1722: # ifndef _WINDOWS
1.7 cvs 1723: if ((fd = open (fileName, O_RDONLY)) == -1)
1.70 cvs 1724: # else /* _WINDOWS */
1.74 cvs 1725: if ((fd = open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.70 cvs 1726: # endif /* _WINDOWS */
1.7 cvs 1727: {
1728: /* if we could not open the file, exit */
1729: /*error msg here */
1730: return (HT_ERROR);
1731: }
1.4 cvs 1732:
1733: fstat (fd, &file_stat);
1734:
1.7 cvs 1735: if (file_stat.st_size == 0)
1736: {
1737: /* file was empty */
1738: /*errmsg here */
1739: close (fd);
1740: return (HT_ERROR);
1741: }
1.4 cvs 1742: block_size = file_stat.st_size;
1743:
1744: if (THD_TRACE)
1745: fprintf (stderr, "file size == %u\n", (unsigned) block_size);
1746:
1747: mem_ptr = (char *) TtaGetMemory (block_size);
1748:
1.7 cvs 1749: if (mem_ptr == (char *) NULL)
1750: {
1751: /* could not allocate enough memory */
1752: /*errmsg here */
1753: close (fd);
1754: return (HT_ERROR);
1755: }
1.4 cvs 1756: read (fd, mem_ptr, block_size);
1757:
1758: close (fd);
1759:
1.27 cvs 1760: status = UploadMemWWW (docid, METHOD_PUT, urlName, contentType, mem_ptr,
1.4 cvs 1761: block_size, mode, terminate_cbf,
1762: context_tcbf, (char *) NULL);
1763:
1764: TtaFreeMemory (mem_ptr);
1.28 cvs 1765: TtaHandlePendingEvents ();
1.4 cvs 1766: return (status);
1767: }
1768:
1.5 cvs 1769: /*----------------------------------------------------------------------
1.17 cvs 1770: UploadMemWWW
1771: low level interface function to libwww for uploading a block of
1772: memory to a URL.
1.5 cvs 1773: ----------------------------------------------------------------------*/
1.4 cvs 1774: #ifdef __STDC__
1775: int UploadMemWWW (int docid, HTMethod method,
1.27 cvs 1776: char *urlName, PicType contentType, char *mem_ptr, unsigned long block_size,
1.4 cvs 1777: int mode, TTcbf * terminate_cbf, void *context_tcbf,
1778: char *outputfile)
1779: #else
1.27 cvs 1780: int UploadMemWWW (docid, method, urlName, contentType, mem_ptr, block_size, mode,
1.4 cvs 1781: terminate_cbf, context_tcbf, outputfile)
1782: int docid;
1783: HTMethod method;
1784: char *urlName;
1.27 cvs 1785: PicType contentType;
1.4 cvs 1786: char *mem_ptr;
1787: usigned long block_size;
1788: int mode;
1789: TTcbf *terminate_cbf;
1790: void *context_tcbf;
1791: char *outputfile;
1.7 cvs 1792:
1.4 cvs 1793: #endif
1794: {
1795: AHTReqContext *me;
1796: int status;
1797:
1798: if (mem_ptr == (char *) NULL ||
1799: block_size == 0 ||
1800: docid == 0 ||
1.7 cvs 1801: urlName == (char *) NULL)
1802: {
1803: /* nothing to be uploaded */
1804: return HT_ERROR;
1805: }
1.4 cvs 1806:
1807: /* Initialize the request structure */
1808: me = AHTReqContext_new (docid);
1809:
1.7 cvs 1810: if (me == NULL)
1811: {
1812: /* need an error message here */
1813: TtaHandlePendingEvents ();
1814: return (HT_ERROR);
1815: }
1.4 cvs 1816: me->mode = mode;
1817:
1818: me->incremental_cbf = (TIcbf *) NULL;
1819: me->context_icbf = (void *) NULL;
1820: me->terminate_cbf = terminate_cbf;
1821: me->context_tcbf = context_tcbf;
1822:
1823: me->output = stdout;
1824: me->outputfile = (char *) NULL;
1825: me->urlName = urlName;
1826:
1.70 cvs 1827: #ifdef _WINDOWS
1828: HTRequest_setPreemptive (me->request, YES);
1829: #else
1.4 cvs 1830: HTRequest_setPreemptive (me->request, NO);
1.70 cvs 1831: #endif /* _WINDOWS */
1.74 cvs 1832:
1.17 cvs 1833: /* select the parameters that distinguish a PUT from a GET/POST */
1.4 cvs 1834: me->method = METHOD_PUT;
1835: HTRequest_setMethod (me->request, METHOD_PUT);
1836: me->output = stdout;
1.17 cvs 1837: /* we are not expecting to receive any input from the server */
1838: me->outputfile = (char *) NULL;
1.4 cvs 1839:
1840: me->mem_ptr = mem_ptr;
1841: me->block_size = block_size;
1.17 cvs 1842:
1843: /* set the callback which will actually copy data into the
1844: output stream */
1845:
1.4 cvs 1846: HTRequest_setPostCallback (me->request, AHTUpload_callback);
1.72 cvs 1847:
1.4 cvs 1848: me->anchor = (HTParentAnchor *) HTAnchor_findAddress (urlName);
1.7 cvs 1849:
1.28 cvs 1850: /* Set the Content-Type of the file we are uploading */
1.77 cvs 1851: HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
1852: AHTGuessAtom_for (me->urlName, contentType));
1.17 cvs 1853:
1.7 cvs 1854: HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1.4 cvs 1855: HTRequest_setEntityAnchor (me->request, me->anchor);
1.74 cvs 1856: /* prepare the URLname that will be displayed in teh status bar */
1857: ChopURL (me->status_urlName, me->urlName);
1858: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
1859: me->status_urlName);
1.4 cvs 1860: status = HTLoadAbsolute (urlName, me->request);
1861:
1.74 cvs 1862: #ifndef _WINDOWS
1.77 cvs 1863: if (status == HT_ERROR ||
1864: me->reqStatus == HT_END ||
1865: me->reqStatus == HT_ERR ||
1866: HTError_hasSeverity (HTRequest_error (me->request), ERR_INFO))
1.7 cvs 1867: {
1.17 cvs 1868: status = HT_ERROR;
1.28 cvs 1869: }
1.7 cvs 1870: else
1871: {
1872: /* part of the stop button handler */
1.4 cvs 1873:
1.7 cvs 1874: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
1875: {
1876: status = LoopForStop (me);
1877: }
1.15 cvs 1878: }
1.90 ! cvs 1879: #else /* _WINDOWS */
! 1880: if (status == HT_ERROR ||
! 1881: me->reqStatus == HT_ERR) /* || Error_hasSeverity (HTRequest_error (me->request), ERR_INFO)) */
! 1882: status = HT_ERROR;
! 1883: else
! 1884: status = HT_OK;
! 1885: #endif /* _WINDOWS */
! 1886:
! 1887: AHTReqContext_delete (me);
1.4 cvs 1888: return (status);
1.28 cvs 1889: }
1.4 cvs 1890:
1891:
1892:
1.5 cvs 1893: /*----------------------------------------------------------------------
1.17 cvs 1894: Stop Request
1895: stops (kills) all active requests associated with a docid
1.5 cvs 1896: ----------------------------------------------------------------------*/
1.4 cvs 1897: #ifdef __STDC__
1898: void StopRequest (int docid)
1899: #else
1900: void StopRequest (docid)
1901: int docid;
1902: #endif
1903: {
1904: HTList *cur;
1905: AHTDocId_Status *docid_status;
1906: AHTReqContext *me;
1907: int open_requests;
1908:
1.84 cvs 1909: HTNet *reqNet;
1910: HTHost *reqHost;
1.85 cvs 1911: HTChannel *reqChannel;
1912: int reqSock;
1913:
1.7 cvs 1914: if (Amaya)
1915: {
1916: docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
1.4 cvs 1917: Amaya->docid_status);
1.7 cvs 1918: /* verify if there are any requests at all associated with docid */
1.4 cvs 1919:
1.7 cvs 1920: if (docid_status == (AHTDocId_Status *) NULL)
1921: return;
1.4 cvs 1922:
1.7 cvs 1923: open_requests = docid_status->counter;
1.4 cvs 1924:
1.85 cvs 1925: /* First, kill all pending requests */
1926: /* We first inhibit the activation of pending requests */
1927: HTHost_disable_PendingReqLaunch ();
1928: cur = Amaya->reqlist;
1929: while ((me = (AHTReqContext *) HTList_nextObject (cur)))
1930: {
1931: if (me->docid == docid && me->reqStatus == HT_NEW)
1932: {
1933: reqNet = HTRequest_net (me->request);
1934: reqSock = HTNet_socket (reqNet);
1935: reqChannel = HTChannel_find(reqSock);
1936: reqHost = HTChannel_host (reqChannel);
1937:
1938:
1939: HTRequest_kill (me->request);
1940: /* HTRequest_setNet (me->request, NULL);
1941: */
1942:
1943: if ((me->mode & AMAYA_ASYNC) ||
1944: (me->mode & AMAYA_IASYNC))
1945: {
1946: AHTReqContext_delete (me);
1947: }
1948:
1949: if (HTHost_isIdle (reqHost) ) {
1950: #ifdef DEBUG_LIBWWW
1951: fprintf (stderr, "Host is idle, killing socket %d\n",
1952: reqSock);;
1953: #endif /* DEBUG_LIBWWW */
1954:
1955: HTEvent_unregister (reqSock, FD_ALL);
1956: HTEvent_register(reqSock, NULL, (SockOps) FD_READ,
1957: HTHost_catchClose, HT_PRIORITY_MAX);
1958: close (reqSock);
1959: /*
1960: if (reqChannel && reqHost)
1961: HTHost_clearChannel(reqHost, HT_OK);
1962: HTHost_catchClose (reqSock, NULL, FD_CLOSE);
1963: */
1964: }
1965:
1966: cur = Amaya->reqlist;
1967: open_requests--;
1968: }
1969: }
1970: /* enable the activation of pending requests */
1971: HTHost_enable_PendingReqLaunch ();
1972:
1973: cur = Amaya->reqlist;
1.7 cvs 1974: while ((me = (AHTReqContext *) HTList_nextObject (cur)))
1975: {
1976: if (me->docid == docid)
1977: {
1978: /* kill this request */
1.4 cvs 1979:
1.7 cvs 1980: switch (me->reqStatus)
1981: {
1982: case HT_ABORT:
1983: break;
1.4 cvs 1984:
1.7 cvs 1985: case HT_BUSY:
1986: me->reqStatus = HT_ABORT;
1987: break;
1988: case HT_NEW_PENDING:
1989: case HT_WAITING:
1990: default:
1.74 cvs 1991: me->reqStatus = HT_ABORT;
1.85 cvs 1992:
1.70 cvs 1993: # ifndef _WINDOWS
1.7 cvs 1994: RequestKillAllXtevents (me);
1.74 cvs 1995: # endif _WINDOWS
1.85 cvs 1996:
1.84 cvs 1997: reqNet = HTRequest_net (me->request);
1998: reqSock = HTNet_socket (reqNet);
1.85 cvs 1999: reqChannel = HTChannel_find(reqSock);
2000: reqHost = HTChannel_host (reqChannel);
2001:
2002: HTRequest_kill (me->request);
2003: /* HTRequest_setNet (me->request, NULL); */
1.84 cvs 2004:
1.71 cvs 2005: if ((me->mode & AMAYA_ASYNC) ||
2006: (me->mode & AMAYA_IASYNC))
1.7 cvs 2007: {
2008: AHTReqContext_delete (me);
1.84 cvs 2009:
1.7 cvs 2010: }
1.84 cvs 2011:
1.85 cvs 2012: /* if there are no more requests, then close
2013: the connection */
2014:
1.84 cvs 2015: if (HTHost_isIdle (reqHost) ) {
1.85 cvs 2016: #ifdef DEBUG_LIBWWW
2017: fprintf (stderr, "Host is idle, "
2018: "killing socket %d\n",
2019: reqSock);
2020: #endif /* DEBUG_LIBWWW */
2021: HTEvent_unregister (reqSock, FD_ALL);
2022: HTEvent_register(reqSock,
2023: NULL,
2024: (SockOps) FD_READ,
2025: HTHost_catchClose,
2026: HT_PRIORITY_MAX);
2027: close (reqSock);
2028: HTHost_clearChannel (reqHost, HT_ERROR);
2029: /*
2030: if (reqChannel && reqHost)
2031: HTHost_clearChannel(reqHost, HT_OK);
2032: HTHost_catchClose (reqSock, NULL, FD_CLOSE);
2033: */
2034: #ifdef DEBUG_LIBWWW
2035: fprintf (stderr, "After killing, "
2036: "HTHost_isIdle gives %d\n",
2037: HTHost_isIdle (reqHost));
2038: #endif /* DEBUG_LIBWWW */
1.84 cvs 2039: }
2040:
1.74 cvs 2041: cur = Amaya->reqlist;
1.7 cvs 2042: open_requests--;
1.4 cvs 2043:
1.7 cvs 2044: break;
1.4 cvs 2045:
1.7 cvs 2046: } /* switch */
2047: } /* if me docid */
2048: } /* while */
2049: } /* if amaya open requests */
1.85 cvs 2050:
2051: } /* StopRequest */
1.17 cvs 2052:
2053: /*
2054: end of Module query.c
2055: */
2056:
1.69 cvs 2057: #endif /* AMAYA_JAVA */
1.77 cvs 2058:
2059:
1.17 cvs 2060:
2061:
Webmaster