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