Annotation of Amaya/amaya/query.c, revision 1.112
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.107 cvs 631: strncpy (me->urlName, new_anchor->parent->address, MAX_LENGTH - 1);
1.21 cvs 632: me->urlName[MAX_LENGTH - 1] = EOS;
1.7 cvs 633: }
634: else
1.74 cvs 635: strcpy (me->urlName, new_anchor->parent->address);
1.7 cvs 636:
1.38 cvs 637: ChopURL (me->status_urlName, me->urlName);
1.7 cvs 638: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_RED_FETCHING),
1.38 cvs 639: me->status_urlName);
1.7 cvs 640:
641: /* Start request with new credentials */
1.64 cvs 642:
1.79 cvs 643: if (HTRequest_outputStream (me->request) != NULL) {
1.85 cvs 644: AHTFWriter_FREE (request->output_stream);
645: if (me->output != stdout) { /* Are we writing to a file? */
1.74 cvs 646: #ifdef DEBUG_LIBWWW
1.79 cvs 647: fprintf (stderr, "redirection_handler: New URL is %s, closing "
1.74 cvs 648: "FILE %p\n", me->urlName, me->output);
649: #endif
1.79 cvs 650: fclose (me->output);
651: me->output = NULL;
652: }
1.64 cvs 653: }
1.68 cvs 654:
1.105 cvs 655: /* reset the status */
656: me->reqStatus = HT_NEW;
657: /* clear the errors */
658: HTError_deleteAll( HTRequest_error (request));
659: HTRequest_setError (request, NULL);
1.7 cvs 660: if (me->method == METHOD_PUT || me->method == METHOD_POST) /* PUT, POST etc. */
1.74 cvs 661: status = HTLoadAbsolute (me->urlName, request);
1.7 cvs 662: else
1.74 cvs 663: HTLoadAnchor (new_anchor, request);
1.7 cvs 664: }
665: else
1.68 cvs 666: {
1.74 cvs 667: HTRequest_addError (request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
668: NULL, 0, "HTRedirectFilter");
669: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REDIRECTIONS_LIMIT),
670: NULL);
671: if (me->error_html)
672: DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
673: /* so that we can show the error message */
1.68 cvs 674: }
675:
1.4 cvs 676: /*
1.74 cvs 677: ** By returning HT_ERROR we make sure that this is the last handler to be
678: ** called. We do this as we don't want any other filter to delete the
679: ** request object now when we have just started a new one ourselves
680: */
1.4 cvs 681: return HT_ERROR;
682: }
683:
1.5 cvs 684: /*----------------------------------------------------------------------
1.17 cvs 685: terminate_handler
686: this function is registered to handle the result of the request
1.5 cvs 687: ----------------------------------------------------------------------*/
1.4 cvs 688: #if __STDC__
1.7 cvs 689: static int terminate_handler (HTRequest * request, HTResponse * response, void *context, int status)
1.4 cvs 690: #else
691: static int terminate_handler (request, response, context, status)
692: HTRequest *request;
693: HTResponse *response;
694: void *context;
695: int status;
696: #endif
697: {
698: AHTReqContext *me = (AHTReqContext *) HTRequest_context (request);
1.106 cvs 699: char *content_type;
1.13 cvs 700: boolean error_flag;
1.4 cvs 701:
702: if (!me)
1.74 cvs 703: return HT_OK; /* not an Amaya request */
704:
1.80 cvs 705: /* if Amaya was killed, treat with this request as if it were
706: issued by a Stop button event */
1.105 cvs 707: if (!AmayaAlive)
1.80 cvs 708: me->reqStatus = HT_ABORT;
1.74 cvs 709:
1.77 cvs 710: if (status == HT_LOADED ||
711: status == HT_CREATED ||
1.79 cvs 712: status == HT_NO_DATA ||
713: me->reqStatus == HT_ABORT)
1.74 cvs 714: error_flag = FALSE;
1.13 cvs 715: else
1.74 cvs 716: error_flag = TRUE;
717:
1.4 cvs 718: /* output any errors from the server */
1.74 cvs 719:
1.77 cvs 720: /*
1.74 cvs 721: ** me->output = output file which will receive an html file
722: ** me->error_html = yes, output HTML errors in the screen
723: ** request->error_stack == if there are any errors, they will be here
724: ** me->error_stream_size If it's != 0 means an error message has already
725: ** been written to the stack
726: */
727:
1.4 cvs 728: /* First, we verify if there are any errors and if they are not
1.17 cvs 729: ** yet written to the error stack. If no, then let's try to write them
730: ** ourselves
731: */
1.74 cvs 732:
733: #ifdef DEBUG_LIBWWW
734: fprintf (stderr, "terminate_handler: URL is "
735: "%s, closing FILE %p status is %d\n", me->urlName, me->output,
736: status);
1.69 cvs 737: #endif
1.74 cvs 738:
1.69 cvs 739: if (me->output && me->output != stdout)
740: {
1.74 cvs 741: /* we are writing to a file */
742: if (me->reqStatus != HT_ABORT)
743: { /* if the request was not aborted and */
1.80 cvs 744: if (error_flag &&
745: me->error_html == TRUE)
746: /* there were some errors and we want to print them */
747: {
748: if (me->error_stream_size == 0)/* and the stream is empty */
749: AHTError_MemPrint (request); /* copy errors from
1.74 cvs 750: **the error stack
1.77 cvs 751: ** into the error stream */
1.80 cvs 752: if (me->error_stream)
753: { /* if the stream is non-empty */
754: fprintf (me->output, me->error_stream);/* output the errors */
1.85 cvs 755: /* Clear the error context, so that we can deal with
756: this answer as if it were a normal reply */
757: HTError_deleteAll( HTRequest_error (request));
758: HTRequest_setError (request, NULL);
759: error_flag = 0;
1.74 cvs 760: }
761: } /* if error_stack */
1.85 cvs 762: }
763:
764: /* if != HT_ABORT */
765:
766: #ifdef DEBUG_LIBWWW
767: fprintf (stderr, "terminate_handler: URL is %s, closing "
768: "FILE %p\n", me->urlName, me->output);
769: #endif
1.74 cvs 770: fclose (me->output);
771: me->output = NULL;
1.69 cvs 772: }
1.80 cvs 773:
1.74 cvs 774:
1.80 cvs 775: if (error_flag)
776: me->reqStatus = HT_ERR;
777: else if (me->reqStatus != HT_ABORT)
778: me->reqStatus = HT_END;
779:
1.106 cvs 780: /* copy the content_type */
781: content_type = request->anchor->content_type->name;
782: if (content_type && content_type [0] != EOS)
1.88 cvs 783: {
784: /* libwww gives www/unknown when it gets an error. As this is
785: an HTML test, we force the type to text/html */
786: if (!strcmp (content_type, "www/unknown"))
787: {
1.106 cvs 788: if (me->content_type == NULL)
789: me->content_type = TtaStrdup ("text/html");
790: else
791: strcpy (me->content_type, "text/html");
1.88 cvs 792: }
793: else
1.106 cvs 794: {
795: if (me->content_type == NULL)
796: me->content_type = TtaStrdup (content_type);
797: else
798: {
799: strncpy (me->content_type, content_type, NAME_LENGTH -1);
800: me->content_type [NAME_LENGTH-1] = '\0';
801: }
1.88 cvs 802:
803: /* Content-Type can be specified by a server's admin. To be on
804: the safe side, we normalize its case */
805: ConvertToLowerCase (me->content_type);
806: }
807: #ifdef DEBUG_LIBWWW
808: fprintf (stderr, "content type is: %s\n", me->content_type);
809: #endif /* DEBUG_LIBWWW */
810: }
811:
1.111 cvs 812: /* don't remove or Xt will hang up during the PUT */
1.105 cvs 813: if (AmayaAlive && ((me->method == METHOD_POST) ||
1.111 cvs 814: (me->method == METHOD_PUT)))
1.7 cvs 815: {
1.80 cvs 816: PrintTerminateStatus (me, status);
817: }
1.111 cvs 818:
819: ProcessTerminateRequest (request, response, context, status);
820: return HT_OK;
1.4 cvs 821: }
822:
1.5 cvs 823: /*----------------------------------------------------------------------
1.17 cvs 824: AHTLoadTerminate_handler
1.74 cvs 825: this is an application "AFTER" Callback. It's called by the library
826: when a request has ended, so that we can setup the correct status.
1.5 cvs 827: ----------------------------------------------------------------------*/
1.4 cvs 828:
829: #ifdef __STDC__
1.111 cvs 830: int AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4 cvs 831: #else
1.111 cvs 832: int AHTLoadTerminate_handler (request, response, param, status)
1.4 cvs 833: HTRequest *request;
834: HTResponse *response;
835: void *param;
836: int status;
1.69 cvs 837:
1.4 cvs 838: #endif
839: {
840: AHTReqContext *me = HTRequest_context (request);
841: HTAlertCallback *cbf;
842: AHTDocId_Status *docid_status;
843:
1.7 cvs 844: switch (status)
845: {
846: case HT_LOADED:
847: if (PROT_TRACE)
848: HTTrace ("Load End.... OK: `%s\' has been accessed\n",
1.38 cvs 849: me->status_urlName);
1.4 cvs 850:
1.7 cvs 851: docid_status = GetDocIdStatus (me->docid,
852: Amaya->docid_status);
853:
854: if (docid_status != NULL && docid_status->counter > 1)
1.74 cvs 855: TtaSetStatus (me->docid, 1,
856: TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
857: me->status_urlName);
1.7 cvs 858: break;
859:
860: case HT_NO_DATA:
861: if (PROT_TRACE)
1.74 cvs 862: HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n",
863: me->status_urlName);
864: TtaSetStatus (me->docid, 1,
865: TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
1.38 cvs 866: me->status_urlName);
1.7 cvs 867: break;
868:
869: case HT_INTERRUPTED:
870: if (PROT_TRACE)
1.74 cvs 871: HTTrace ("Load End.... INTERRUPTED: `%s\'\n",
872: me->status_urlName);
873: TtaSetStatus (me->docid, 1,
874: TtaGetMessage (AMAYA, AM_LOAD_ABORT),
875: NULL);
1.7 cvs 876: break;
877:
878: case HT_RETRY:
879: if (PROT_TRACE)
880: HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
881: HTResponse_retryTime (response));
1.74 cvs 882: TtaSetStatus (me->docid, 1,
883: TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
1.38 cvs 884: me->status_urlName);
1.7 cvs 885: break;
886:
887: case HT_ERROR:
888:
889: cbf = HTAlert_find (HT_A_MESSAGE);
890: if (cbf)
891: (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
892: HTRequest_error (request), NULL);
893: break;
894:
895: if (PROT_TRACE)
896: HTTrace ("Load End.... ERROR: Can't access `%s\'\n",
1.74 cvs 897: me->status_urlName ? me->status_urlName :"<UNKNOWN>");
898: TtaSetStatus (me->docid, 1,
899: TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1.38 cvs 900: me->status_urlName ? me->status_urlName : "<UNKNOWN>");
1.7 cvs 901: break;
902: default:
903: if (PROT_TRACE)
904: HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
905: break;
906: }
907:
1.4 cvs 908: return HT_OK;
909: }
910:
1.27 cvs 911: /*----------------------------------------------------------------------
912: AHTAcceptTypesInit
1.74 cvs 913: This function prepares the Accept header used by Amaya during
914: the HTTP content negotiation phase
1.27 cvs 915: ----------------------------------------------------------------------*/
916: #ifdef __STDC__
917: static void AHTAcceptTypesInit (HTList *c)
918: #else /* __STDC__ */
919: static void AHTAcceptTypesInit (c)
920: HTList *c;
921: #endif /* __STDC__ */
922: {
923: if (c == (HTList *) NULL)
924: return;
925:
926: /* define here all the mime types that Amaya can accept */
927:
1.74 cvs 928: HTConversion_add (c, "image/png", "www/present",
929: HTThroughLine, 1.0, 0.0, 0.0);
930: HTConversion_add (c, "image/jpeg", "www/present",
931: HTThroughLine, 1.0, 0.0, 0.0);
932: HTConversion_add (c, "image/gif", "www/present",
933: HTThroughLine, 1.0, 0.0, 0.0);
934: HTConversion_add (c, "image/xbm", "www/present",
935: HTThroughLine, 1.0, 0.0, 0.0);
936: HTConversion_add (c, "image/xpm", "www/present",
937: HTThroughLine, 1.0, 0.0, 0.0);
938: HTConversion_add (c, "application/postscript",
939: "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1.4 cvs 940:
1.27 cvs 941: /* Define here the equivalences between MIME types and file extensions for
942: the types that Amaya can display */
943:
944: /* Register the default set of file suffix bindings */
945: HTFileInit ();
946:
947: /* Don't do any case distinction */
948: HTBind_caseSensitive (FALSE);
949: }
1.4 cvs 950:
1.5 cvs 951: /*----------------------------------------------------------------------
1.17 cvs 952: AHTConverterInit
953: Bindings between a source media type and a destination media type
954: (conversion).
1.5 cvs 955: ----------------------------------------------------------------------*/
1.15 cvs 956: #ifdef __STDC__
957: static void AHTConverterInit (HTList *c)
958: #else /* __STDC__ */
959: static void AHTConverterInit (c)
960: HTList *c;
961: #endif /* __STDC__ */
1.4 cvs 962: {
963:
964: /* Handler for custom http error messages */
1.7 cvs 965: HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4 cvs 966:
967: /*
968: ** These are converters that converts to something other than www/present,
969: ** that is not directly outputting someting to the user on the screen
970: */
971:
972: HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 1.0, 0.0, 0.0);
973: HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter,
974: 1.0, 0.0, 0.0);
975: HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader,
976: 1.0, 0.0, 0.0);
977: HTConversion_add (c, "multipart/*", "*/*", HTBoundary,
978: 1.0, 0.0, 0.0);
979: HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML,
980: 1.0, 0.0, 0.0);
981:
982:
983: /*
984: ** The following conversions are converting ASCII output from various
985: ** protocols to HTML objects.
986: */
987: HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new,
988: 1.0, 0.0, 0.0);
989:
990: /*
991: ** We also register a special content type guess stream that can figure out
992: ** the content type by reading the first bytes of the stream
993: */
994: HTConversion_add (c, "www/unknown", "*/*", HTGuess_new,
995: 1.0, 0.0, 0.0);
996:
997: /*
998: ** Register a persistent cache stream which can save an object to local
999: ** file
1000: */
1.102 cvs 1001: #if 0
1.4 cvs 1002: HTConversion_add (c, "www/cache", "*/*", HTCacheWriter,
1003: 1.0, 0.0, 0.0);
1.102 cvs 1004: #endif
1.4 cvs 1005:
1006: /*
1007: ** This dumps all other formats to local disk without any further
1008: ** action taken
1009: */
1010: HTConversion_add (c, "*/*", "www/present", HTSaveLocally,
1011: 0.3, 0.0, 0.0);
1012:
1013: }
1014:
1.27 cvs 1015:
1.15 cvs 1016: /*----------------------------------------------------------------------
1.17 cvs 1017: AHTProtocolInit
1018: Registers all amaya supported protocols.
1.15 cvs 1019: ----------------------------------------------------------------------*/
1.4 cvs 1020: static void AHTProtocolInit (void)
1021: {
1022:
1.17 cvs 1023: /*
1.74 cvs 1024: NB. Preemptive == YES means Blocking requests
1025: Non-preemptive == NO means Non-blocking requests
1.17 cvs 1026: */
1.4 cvs 1027:
1.63 cvs 1028: HTProtocol_add ("http", "buffered_tcp", NO, HTLoadHTTP, NULL);
1.4 cvs 1029: /* HTProtocol_add ("http", "tcp", NO, HTLoadHTTP, NULL); */
1030: HTProtocol_add ("file", "local", NO, HTLoadFile, NULL);
1.103 cvs 1031: #if 0 /* experimental code */
1032: HTProtocol_add ("cache", "local", NO, HTLoadCache, NULL);
1.36 cvs 1033: HTProtocol_add ("ftp", "tcp", NO, HTLoadFTP, NULL);
1.4 cvs 1034: HTProtocol_add ("telnet", "", YES, HTLoadTelnet, NULL);
1035: HTProtocol_add ("tn3270", "", YES, HTLoadTelnet, NULL);
1036: HTProtocol_add ("rlogin", "", YES, HTLoadTelnet, NULL);
1037: HTProtocol_add ("nntp", "tcp", NO, HTLoadNews, NULL);
1038: HTProtocol_add ("news", "tcp", NO, HTLoadNews, NULL);
1.17 cvs 1039: #endif
1.4 cvs 1040: }
1041:
1.15 cvs 1042: /*----------------------------------------------------------------------
1.17 cvs 1043: AHTNetInit
1044: Reegisters "before" and "after" request filters.
1.15 cvs 1045: ----------------------------------------------------------------------*/
1.4 cvs 1046: static void AHTNetInit (void)
1047: {
1048:
1049: /* Register BEFORE filters
1.74 cvs 1050: ** The BEFORE filters handle proxies, caches, rule files etc.
1051: ** The filters are called in the order by which the are registered
1052: ** Not done automaticly - may be done by application!
1053: */
1054:
1.4 cvs 1055:
1.74 cvs 1056: HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, 6);
1057: HTNet_addBefore (HTProxyFilter, NULL, NULL, 10);
1.85 cvs 1058: /* HTNet_addBefore (AHTOpen_file, NULL, NULL, 11); */
1059: HTHost_setActivateRequestCallback (AHTOpen_file);
1.4 cvs 1060:
1061: /* register AFTER filters
1.74 cvs 1062: ** The AFTER filters handle error messages, logging, redirection,
1063: ** authentication etc.
1064: ** The filters are called in the order by which the are registered
1065: ** Not done automaticly - may be done by application!
1066: */
1.4 cvs 1067:
1.82 cvs 1068: HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS, HT_FILTER_MIDDLE);
1069: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT, HT_FILTER_MIDDLE);
1070: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT, HT_FILTER_MIDDLE);
1071: HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY, HT_FILTER_MIDDLE);
1.111 cvs 1072: #ifndef _WINDOWS
1.51 cvs 1073: HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.111 cvs 1074: #endif /* !_WINDOWS */
1.51 cvs 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.109 cvs 1299: HTDNS_setTimeout (60);
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
1.111 cvs 1418: void InvokeGetObjectWWW_callback (int docid, char *urlName, char *outputfile, TTcbf *terminate_cbf, void *context_cbf, int status)
1.99 cvs 1419: #else
1.111 cvs 1420: void InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf, context_tcbf, status)
1.99 cvs 1421: int docid;
1422: char *urlName;
1423: char *outputfile;
1424: TTcbf *terminate_cbf;
1425: void *context_tcbf;
1.111 cvs 1426: int status;
1.99 cvs 1427: #endif /* _STDC */
1428: {
1429: if (!terminate_cbf)
1430: return;
1431:
1432: (*terminate_cbf) (docid, -1, urlName, outputfile,
1433: NULL, context_tcbf);
1434: }
1435:
1436:
1437:
1.5 cvs 1438: /*----------------------------------------------------------------------
1.15 cvs 1439: GetObjectWWW
1.17 cvs 1440: this function requests a resource designated by a URLname into a
1441: temporary filename. The download can come from a simple GET operation,
1442: or can come from POSTING/GETTING a form. In the latter
1443: case, the function receives a query string to send to the server.
1444:
1.5 cvs 1445: 4 file retrieval modes are proposed:
1446: AMAYA_SYNC : blocking mode
1447: AMAYA_ISYNC : incremental, blocking mode
1448: AMAYA_ASYNC : non-blocking mode
1449: AMAYA_IASYNC : incremental, non-blocking mode
1450:
1451: In the incremental mode, each time a package arrives, it will be
1452: stored in the temporary file. In addition, if an
1453: incremental_callback function is defined, this function will be
1454: called and handled a copy of the newly received data package.
1455: Finally, if a terminate_callback function is defined, it will be
1456: invoked when the request terminates. The caller of this function
1.4 cvs 1457: can define two different contexts to be passed to the callback
1458: functions.
1459:
1460: When the function is called with the SYNC mode, the function will
1461: return only when the requested file has been loaded.
1462: The ASYNC mode will immediately return after setting up the
1463: call.
1464:
1465: Notes:
1466: At the end of a succesful request, the urlName string contains the
1467: name of the actually retrieved URL. As a URL can change over the time,
1468: (e.g., be redirected elsewhere), it is advised that the function
1.17 cvs 1469: caller verify the value of the urlName variable at the end of
1.4 cvs 1470: a request.
1471:
1472: Inputs:
1473: - docid Document identifier for the set of objects being
1474: retrieved.
1475: - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
1476: - outputfile A pointer to an empty string of MAX_URL_LENGTH.
1477: - mode The retrieval mode.
1478: - incremental_cbf
1479: - context_icbf
1480: Callback and context for the incremental modes
1481: - terminate_cbf
1482: - context_icbf
1483: Callback and context for a terminate handler
1.17 cvs 1484: -error_html if TRUE, then display any server error message as an
1485: HTML document.
1.88 cvs 1486: - content_type a string
1487:
1.4 cvs 1488: Outputs:
1489: - urlName The URL that was retrieved
1490: - outputfile The name of the temporary file which holds the
1491: retrieved data. (Only in case of success)
1.88 cvs 1492: - if content_type wasn't NULL, it will contain a copy of the parameter
1493: sent in the HTTP answer
1.4 cvs 1494: Returns:
1495: HT_ERROR
1496: HT_OK
1.5 cvs 1497:
1498: ----------------------------------------------------------------------*/
1.4 cvs 1499: #ifdef __STDC__
1.52 cvs 1500: int GetObjectWWW (int docid, char* urlName, char* postString, char* outputfile, int mode,
1501: TIcbf* incremental_cbf, void* context_icbf, TTcbf* terminate_cbf,
1.88 cvs 1502: void* context_tcbf, boolean error_html, char *content_type)
1.4 cvs 1503: #else
1.52 cvs 1504: int GetObjectWWW (docid, urlName, postString, outputfile, mode, incremental_cbf, context_icbf,
1.88 cvs 1505: terminate_cbf, context_tcbf, error_html, content_type)
1.73 cvs 1506: int docid;
1507: char *urlName;
1508: char *postString;
1509: char *outputfile;
1510: int mode;
1511: TIcbf *incremental_cbf;
1512: void *context_icbf;
1513: TTcbf *terminate_cbf;
1514: void *context_tcbf;
1515: boolean error_html;
1.88 cvs 1516: char *content_type;
1.4 cvs 1517: #endif
1518: {
1519: AHTReqContext *me;
1520: char *ref;
1.107 cvs 1521: int status, l;
1.7 cvs 1522:
1.112 ! cvs 1523: if (urlName == NULL || outputfile == NULL) {
1.69 cvs 1524: /* no file to be loaded */
1525: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1526:
1527: if (error_html)
1528: /* so we can show the error message */
1529: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99 cvs 1530: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.111 cvs 1531: context_tcbf, -1);
1.69 cvs 1532: return HT_ERROR;
1533: }
1.7 cvs 1534:
1.4 cvs 1535: /* do we support this protocol? */
1.69 cvs 1536: if (IsValidProtocol (urlName) == NO) {
1537: /* return error */
1538: outputfile[0] = EOS; /* file could not be opened */
1539: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL), urlName);
1540:
1541: if (error_html)
1542: /* so we can show the error message */
1543: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99 cvs 1544: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.111 cvs 1545: context_tcbf, -1);
1.69 cvs 1546: return HT_ERROR;
1547: }
1.4 cvs 1548:
1.58 cvs 1549: /*create a tempfilename */
1.59 cvs 1550: sprintf (outputfile, "%s%c%d%c%04dAM", TempFileDirectory, DIR_SEP, docid, DIR_SEP, object_counter);
1.4 cvs 1551:
1552: /* update the object_counter */
1553: object_counter++;
1554: /* normalize the URL */
1.45 cvs 1555: ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4 cvs 1556: /* should we abort the request if we could not normalize the url? */
1.69 cvs 1557: if (ref == (char*) NULL || ref[0] == EOS) {
1558: /*error */
1559: outputfile[0] = EOS;
1560: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1561:
1562: if (error_html)
1563: /* so we can show the error message */
1564: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99 cvs 1565: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.111 cvs 1566: context_tcbf, -1);
1.69 cvs 1567: return HT_ERROR;
1568: }
1.4 cvs 1569: /* verify if that file name existed */
1.9 cvs 1570: if (TtaFileExist (outputfile))
1.77 cvs 1571: TtaFileUnlink (outputfile);
1.4 cvs 1572:
1573: /* Initialize the request structure */
1574: me = AHTReqContext_new (docid);
1.69 cvs 1575: if (me == NULL) {
1.77 cvs 1576: outputfile[0] = EOS;
1577: /* need an error message here */
1578: TtaFreeMemory (ref);
1.99 cvs 1579: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.111 cvs 1580: context_tcbf, -1);
1.99 cvs 1581: return HT_ERROR;
1.69 cvs 1582: }
1.77 cvs 1583:
1.4 cvs 1584: /* Specific initializations for POST and GET */
1.69 cvs 1585: if (mode & AMAYA_FORM_POST) {
1.77 cvs 1586: me->method = METHOD_POST;
1587: if (postString) {
1588: me->block_size = strlen (postString);
1.99 cvs 1589: me->mem_ptr = TtaStrdup (postString);
1.77 cvs 1590: } else {
1591: me->mem_ptr = "";
1592: me->block_size = 0;
1593: }
1594: HTRequest_setMethod (me->request, METHOD_POST);
1.69 cvs 1595: HTRequest_setPostCallback (me->request, AHTUpload_callback);
1596: } else {
1.77 cvs 1597: me->method = METHOD_GET;
1598: me->dest = (HTParentAnchor *) NULL; /*useful only for PUT and POST methods */
1599: if (!HasKnownFileSuffix (ref))
1600: HTRequest_setConversion(me->request, acceptTypes, TRUE);
1.69 cvs 1601: }
1.93 cvs 1602:
1.4 cvs 1603: /* Common initialization */
1604: me->mode = mode;
1605: me->error_html = error_html;
1606: me->incremental_cbf = incremental_cbf;
1607: me->context_icbf = context_icbf;
1608: me->terminate_cbf = terminate_cbf;
1609: me->context_tcbf = context_tcbf;
1.64 cvs 1610:
1.69 cvs 1611: /* for the async. request modes, we need to have our
1.4 cvs 1612: own copy of outputfile and urlname
1613: */
1614:
1.69 cvs 1615: if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) {
1.107 cvs 1616: l = strlen (outputfile);
1617: if (l > MAX_LENGTH)
1618: me->outputfile = TtaGetMemory (l + 2);
1619: else
1620: me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
1.104 cvs 1621: strcpy (me->outputfile, outputfile);
1.107 cvs 1622: l = strlen (urlName);
1623: if (l > MAX_LENGTH)
1624: me->urlName = TtaGetMemory (l + 2);
1625: else
1626: me->urlName = TtaGetMemory (MAX_LENGTH + 2);
1.104 cvs 1627: strcpy (me->urlName, urlName);
1.69 cvs 1628: # ifdef _WINDOWS
1629: HTRequest_setPreemptive (me->request, NO);
1630: } else {
1.77 cvs 1631: me->outputfile = outputfile;
1632: me->urlName = urlName;
1633: HTRequest_setPreemptive (me->request, YES);
1.61 cvs 1634: }
1.99 cvs 1635: # else /* !_WINDOWS */
1.69 cvs 1636: } else {
1.77 cvs 1637: me->outputfile = outputfile;
1638: me->urlName = urlName;
1.99 cvs 1639: me->content_type = content_type;
1.61 cvs 1640: }
1.77 cvs 1641: /***
1.57 cvs 1642: Change for taking into account the stop button:
1643: The requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1644: we will loop until the document has been received or a stop signal
1645: generated
1.77 cvs 1646: ****/
1647: HTRequest_setPreemptive (me->request, NO);
1.69 cvs 1648: # endif /* _WINDOWS */
1.61 cvs 1649:
1650: /* prepare the URLname that will be displayed in teh status bar */
1651: ChopURL (me->status_urlName, me->urlName);
1652:
1.77 cvs 1653: TtaSetStatus (me->docid, 1,
1654: TtaGetMessage (AMAYA, AM_FETCHING),
1655: me->status_urlName);
1.4 cvs 1656:
1657: me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45 cvs 1658: TtaFreeMemory (ref);
1.4 cvs 1659:
1.69 cvs 1660: if (mode & AMAYA_FORM_POST) {
1661: HTAnchor_setFormat ((HTParentAnchor *) me->anchor, HTAtom_for ("application/x-www-form-urlencoded"));
1662: HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1663: HTRequest_setEntityAnchor (me->request, me->anchor);
1664:
1665: status = HTLoadAbsolute (urlName, me->request);
1666: } else
1.77 cvs 1667: status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.99 cvs 1668:
1669: if (status == HT_ERROR && me->reqStatus == HT_NEW)
1.100 cvs 1670: {
1671: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.111 cvs 1672: context_tcbf, -1);
1.100 cvs 1673: }
1.69 cvs 1674:
1.72 cvs 1675: #ifndef _WINDOWS
1.77 cvs 1676: if (status == HT_ERROR ||
1677: me->reqStatus == HT_END ||
1678: me->reqStatus == HT_ERR)
1679: {
1680: /* in case of error, free all allocated memory and exit */
1.96 cvs 1681: if (me->output && me->output != stdout) {
1.85 cvs 1682: #ifdef DEBUG_LIBWWW
1.96 cvs 1683: fprintf (stderr, "GetObjectWWW:: URL is %s, closing "
1.85 cvs 1684: "FILE %p\n", me->urlName, me->output);
1685: #endif
1.77 cvs 1686: fclose (me->output);
1.82 cvs 1687: me->output = NULL;
1688: }
1689:
1.99 cvs 1690: if (me->reqStatus == HT_ERR) {
1.77 cvs 1691: status = HT_ERROR;
1692: /* show an error message on the status bar */
1693: DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
1694: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD), me->
1695: status_urlName);
1.72 cvs 1696: } else
1.77 cvs 1697: status = HT_OK;
1.72 cvs 1698:
1699: AHTReqContext_delete (me);
1.77 cvs 1700: } else {
1.72 cvs 1701: /* part of the stop button handler */
1702: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC)) {
1703: status = LoopForStop (me);
1704: AHTReqContext_delete (me);
1705: }
1706: }
1707: #else /* !_WINDOWS */
1708:
1.90 cvs 1709: if (status == HT_ERROR ||
1710: me->reqStatus == HT_ERROR)
1711: {
1712: status = HT_ERROR;
1.71 cvs 1713:
1.90 cvs 1714: /* in case of error, close any open files, free all allocated
1715: memory and exit */
1716: if (me->output && me->output != stdout)
1717: {
1.85 cvs 1718: #ifdef DEBUG_LIBWWW
1.90 cvs 1719: fprintf (stderr, "GetObjectWWW: URL is %s, closing "
1720: "FILE %p\n", me->urlName, me->output);
1.85 cvs 1721: #endif
1.90 cvs 1722: fclose (me->output);
1723: me->output = NULL;
1724: }
1.85 cvs 1725:
1.90 cvs 1726: if (me->reqStatus == HT_ERR)
1727: {
1728: /* show an error message on the status bar */
1729: DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
1730: TtaSetStatus (me->docid, 1,
1731: TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1732: me->status_urlName);
1733: }
1734: }
1735: else
1736: {
1737: status = HT_OK;
1738: }
1739:
1.93 cvs 1740: /* part of the stop button handler */
1741: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
1742: {
1743: if (HTRequest_net (me->request))
1744: HTRequest_kill (me->request);
1745: else
1746: AHTReqContext_delete (me);
1747: }
1.4 cvs 1748:
1.72 cvs 1749: #endif /* !_WINDOWS */
1750:
1.4 cvs 1751: return (status);
1752: }
1753:
1.5 cvs 1754: /*----------------------------------------------------------------------
1.17 cvs 1755: PutObjectWWW
1756: frontend for uploading a resource to a URL. This function downloads
1757: a file to be uploaded into memory, it then calls UploadMemWWW to
1758: finish the job.
1759:
1.5 cvs 1760: 2 upload modes are proposed:
1761: AMAYA_SYNC : blocking mode
1762: AMAYA_ASYNC : non-blocking mode
1763:
1.4 cvs 1764: When the function is called with the SYNC mode, the function will
1765: return only when the file has been uploaded.
1766: The ASYNC mode will immediately return after setting up the
1767: call. Furthermore, at the end of an upload, the ASYNC mode will
1768: call back terminate_cbf, handling it the context defined in
1769: context_tcbf.
1770:
1771: Notes:
1772: At the end of a succesful request, the urlName string contains the
1773: name of the actually uploaded URL. As a URL can change over the time,
1774: (e.g., be redirected elsewhere), it is advised that the function
1775: caller verifies the value of the urlName variable at the end of
1776: a request.
1777:
1778: Inputs:
1779: - docid Document identifier for the set of objects being
1780: retrieved.
1781: - fileName A pointer to the local file to upload
1782: - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
1783: - mode The retrieval mode.
1784: - terminate_cbf
1785: - context_icbf
1786: Callback and context for a terminate handler
1787:
1788: Outputs:
1789: - urlName The URL that was uploaded
1790:
1791: Returns:
1792: HT_ERROR
1793: HT_OK
1.5 cvs 1794: ----------------------------------------------------------------------*/
1.4 cvs 1795: #ifdef __STDC__
1.27 cvs 1796: int PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4 cvs 1797: TTcbf * terminate_cbf, void *context_tcbf)
1798: #else
1.26 cvs 1799: int PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4 cvs 1800: ,terminate_cbf, context_tcbf)
1801: int docid;
1802: char *urlName;
1803: char *fileName;
1804: int mode;
1.27 cvs 1805: PicType contentType;
1.4 cvs 1806: TTcbf *terminate_cbf;
1807: void *context_tcbf;
1808:
1809: #endif
1810: {
1.7 cvs 1811: /*AHTReqContext *me; */
1.4 cvs 1812: int status;
1813:
1814: int fd;
1815: struct stat file_stat;
1816: char *mem_ptr;
1817: unsigned long block_size;
1818:
1.33 cvs 1819: AmayaLastHTTPErrorMsg [0] = EOS;
1820:
1.4 cvs 1821: if (urlName == NULL || docid == 0 || fileName == NULL ||
1.9 cvs 1822: !TtaFileExist (fileName))
1.4 cvs 1823: /* no file to be uploaded */
1824: return HT_ERROR;
1825:
1826: /* do we support this protocol? */
1.7 cvs 1827: if (IsValidProtocol (urlName) == NO)
1828: {
1829: /* return error */
1.77 cvs 1830: TtaSetStatus (docid, 1,
1831: TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7 cvs 1832: urlName);
1833: return HT_ERROR;
1834: }
1.4 cvs 1835: /* read the file into memory */
1.70 cvs 1836: # ifndef _WINDOWS
1.7 cvs 1837: if ((fd = open (fileName, O_RDONLY)) == -1)
1.70 cvs 1838: # else /* _WINDOWS */
1.74 cvs 1839: if ((fd = open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.70 cvs 1840: # endif /* _WINDOWS */
1.7 cvs 1841: {
1842: /* if we could not open the file, exit */
1843: /*error msg here */
1844: return (HT_ERROR);
1845: }
1.4 cvs 1846:
1847: fstat (fd, &file_stat);
1848:
1.7 cvs 1849: if (file_stat.st_size == 0)
1850: {
1851: /* file was empty */
1852: /*errmsg here */
1853: close (fd);
1854: return (HT_ERROR);
1855: }
1.4 cvs 1856: block_size = file_stat.st_size;
1857:
1858: if (THD_TRACE)
1859: fprintf (stderr, "file size == %u\n", (unsigned) block_size);
1860:
1861: mem_ptr = (char *) TtaGetMemory (block_size);
1862:
1.7 cvs 1863: if (mem_ptr == (char *) NULL)
1864: {
1865: /* could not allocate enough memory */
1866: /*errmsg here */
1867: close (fd);
1868: return (HT_ERROR);
1869: }
1.4 cvs 1870: read (fd, mem_ptr, block_size);
1871:
1872: close (fd);
1873:
1.27 cvs 1874: status = UploadMemWWW (docid, METHOD_PUT, urlName, contentType, mem_ptr,
1.4 cvs 1875: block_size, mode, terminate_cbf,
1876: context_tcbf, (char *) NULL);
1877:
1878: TtaFreeMemory (mem_ptr);
1.28 cvs 1879: TtaHandlePendingEvents ();
1.4 cvs 1880: return (status);
1881: }
1882:
1.5 cvs 1883: /*----------------------------------------------------------------------
1.17 cvs 1884: UploadMemWWW
1885: low level interface function to libwww for uploading a block of
1886: memory to a URL.
1.5 cvs 1887: ----------------------------------------------------------------------*/
1.4 cvs 1888: #ifdef __STDC__
1889: int UploadMemWWW (int docid, HTMethod method,
1.27 cvs 1890: char *urlName, PicType contentType, char *mem_ptr, unsigned long block_size,
1.4 cvs 1891: int mode, TTcbf * terminate_cbf, void *context_tcbf,
1892: char *outputfile)
1893: #else
1.27 cvs 1894: int UploadMemWWW (docid, method, urlName, contentType, mem_ptr, block_size, mode,
1.4 cvs 1895: terminate_cbf, context_tcbf, outputfile)
1896: int docid;
1897: HTMethod method;
1898: char *urlName;
1.27 cvs 1899: PicType contentType;
1.4 cvs 1900: char *mem_ptr;
1901: usigned long block_size;
1902: int mode;
1903: TTcbf *terminate_cbf;
1904: void *context_tcbf;
1905: char *outputfile;
1.7 cvs 1906:
1.4 cvs 1907: #endif
1908: {
1909: AHTReqContext *me;
1910: int status;
1911:
1912: if (mem_ptr == (char *) NULL ||
1913: block_size == 0 ||
1914: docid == 0 ||
1.7 cvs 1915: urlName == (char *) NULL)
1916: {
1917: /* nothing to be uploaded */
1918: return HT_ERROR;
1919: }
1.4 cvs 1920:
1921: /* Initialize the request structure */
1922: me = AHTReqContext_new (docid);
1923:
1.7 cvs 1924: if (me == NULL)
1925: {
1926: /* need an error message here */
1927: TtaHandlePendingEvents ();
1928: return (HT_ERROR);
1929: }
1.4 cvs 1930: me->mode = mode;
1931:
1932: me->incremental_cbf = (TIcbf *) NULL;
1933: me->context_icbf = (void *) NULL;
1934: me->terminate_cbf = terminate_cbf;
1935: me->context_tcbf = context_tcbf;
1936:
1937: me->output = stdout;
1938: me->outputfile = (char *) NULL;
1939: me->urlName = urlName;
1940:
1.70 cvs 1941: #ifdef _WINDOWS
1942: HTRequest_setPreemptive (me->request, YES);
1943: #else
1.4 cvs 1944: HTRequest_setPreemptive (me->request, NO);
1.70 cvs 1945: #endif /* _WINDOWS */
1.74 cvs 1946:
1.17 cvs 1947: /* select the parameters that distinguish a PUT from a GET/POST */
1.4 cvs 1948: me->method = METHOD_PUT;
1949: HTRequest_setMethod (me->request, METHOD_PUT);
1950: me->output = stdout;
1.17 cvs 1951: /* we are not expecting to receive any input from the server */
1952: me->outputfile = (char *) NULL;
1.4 cvs 1953:
1954: me->mem_ptr = mem_ptr;
1955: me->block_size = block_size;
1.17 cvs 1956:
1957: /* set the callback which will actually copy data into the
1958: output stream */
1959:
1.4 cvs 1960: HTRequest_setPostCallback (me->request, AHTUpload_callback);
1.72 cvs 1961:
1.4 cvs 1962: me->anchor = (HTParentAnchor *) HTAnchor_findAddress (urlName);
1.7 cvs 1963:
1.28 cvs 1964: /* Set the Content-Type of the file we are uploading */
1.77 cvs 1965: HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
1966: AHTGuessAtom_for (me->urlName, contentType));
1.17 cvs 1967:
1.7 cvs 1968: HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1.4 cvs 1969: HTRequest_setEntityAnchor (me->request, me->anchor);
1.74 cvs 1970: /* prepare the URLname that will be displayed in teh status bar */
1971: ChopURL (me->status_urlName, me->urlName);
1972: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
1973: me->status_urlName);
1.4 cvs 1974: status = HTLoadAbsolute (urlName, me->request);
1975:
1.74 cvs 1976: #ifndef _WINDOWS
1.77 cvs 1977: if (status == HT_ERROR ||
1978: me->reqStatus == HT_END ||
1979: me->reqStatus == HT_ERR ||
1980: HTError_hasSeverity (HTRequest_error (me->request), ERR_INFO))
1.7 cvs 1981: {
1.17 cvs 1982: status = HT_ERROR;
1.28 cvs 1983: }
1.7 cvs 1984: else
1985: {
1986: /* part of the stop button handler */
1.4 cvs 1987:
1.7 cvs 1988: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
1989: {
1990: status = LoopForStop (me);
1991: }
1.15 cvs 1992: }
1.90 cvs 1993: #else /* _WINDOWS */
1994: if (status == HT_ERROR ||
1995: me->reqStatus == HT_ERR) /* || Error_hasSeverity (HTRequest_error (me->request), ERR_INFO)) */
1996: status = HT_ERROR;
1997: else
1998: status = HT_OK;
1999: #endif /* _WINDOWS */
2000:
1.92 cvs 2001: if (HTRequest_net (me->request))
2002: {
2003: HTRequest_kill (me->request);
2004: }
1.93 cvs 2005: else
2006: AHTReqContext_delete (me);
1.4 cvs 2007: return (status);
1.28 cvs 2008: }
1.4 cvs 2009:
2010:
2011:
1.5 cvs 2012: /*----------------------------------------------------------------------
1.17 cvs 2013: Stop Request
2014: stops (kills) all active requests associated with a docid
1.5 cvs 2015: ----------------------------------------------------------------------*/
1.4 cvs 2016: #ifdef __STDC__
2017: void StopRequest (int docid)
2018: #else
2019: void StopRequest (docid)
2020: int docid;
2021: #endif
2022: {
2023: HTList *cur;
2024: AHTDocId_Status *docid_status;
2025: AHTReqContext *me;
1.84 cvs 2026: HTNet *reqNet;
2027: HTHost *reqHost;
1.85 cvs 2028: HTChannel *reqChannel;
2029: int reqSock;
1.108 cvs 2030: #ifdef DEBUG_LIBWWW
2031: int open_requests;
2032: #endif /* DEBUG_LIBWWW */
1.100 cvs 2033:
1.7 cvs 2034: if (Amaya)
2035: {
1.100 cvs 2036: #ifdef DEBUG_LIBWWW
2037: fprintf (stderr, "StopRequest: number of Amaya requests : %d\n", Amaya->open_requests);
2038: #endif /* DEBUG_LIBWWW */
2039:
1.7 cvs 2040: docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
1.4 cvs 2041: Amaya->docid_status);
1.7 cvs 2042: /* verify if there are any requests at all associated with docid */
1.4 cvs 2043:
1.7 cvs 2044: if (docid_status == (AHTDocId_Status *) NULL)
2045: return;
1.4 cvs 2046:
1.108 cvs 2047: #ifdef DEBUG_LIBWWW
1.7 cvs 2048: open_requests = docid_status->counter;
1.108 cvs 2049: #endif /* DEBUG_LIBWWW */
1.4 cvs 2050:
1.85 cvs 2051: /* First, kill all pending requests */
2052: /* We first inhibit the activation of pending requests */
2053: HTHost_disable_PendingReqLaunch ();
2054: cur = Amaya->reqlist;
2055: while ((me = (AHTReqContext *) HTList_nextObject (cur)))
2056: {
2057: if (me->docid == docid && me->reqStatus == HT_NEW)
2058: {
2059: reqNet = HTRequest_net (me->request);
2060: reqSock = HTNet_socket (reqNet);
2061: reqChannel = HTChannel_find(reqSock);
2062: reqHost = HTChannel_host (reqChannel);
1.95 cvs 2063:
2064: /* If we have an ASYNC request, we kill it.
2065: ** If it's a SYNC request, we just mark it as aborted
2066: */
2067: me->reqStatus = HT_ABORT;
1.110 cvs 2068: if (((me->mode & AMAYA_IASYNC)
2069: || (me->mode & AMAYA_ASYNC))
1.108 cvs 2070: && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
2071: {
2072: if (HTRequest_net (me->request))
2073: /* delete the libwww request context */
2074: HTRequest_kill (me->request);
1.96 cvs 2075: #ifndef _WINDOWS
2076: /* delete the Amaya request context */
2077: AHTReqContext_delete (me);
1.108 cvs 2078: #endif /* !_WINDOWS */
1.96 cvs 2079: cur = Amaya->reqlist;
1.108 cvs 2080: #ifdef DEBUG_LIBWWW
2081: /* update the number of open requests */
2082: open_requests--;
2083: #endif /* DEBUG_LIBWWW */
1.85 cvs 2084: }
1.108 cvs 2085:
1.85 cvs 2086: if (HTHost_isIdle (reqHost) ) {
2087: #ifdef DEBUG_LIBWWW
2088: fprintf (stderr, "Host is idle, killing socket %d\n",
1.95 cvs 2089: reqSock);
1.85 cvs 2090: #endif /* DEBUG_LIBWWW */
2091:
2092: HTEvent_unregister (reqSock, FD_ALL);
2093: HTEvent_register(reqSock, NULL, (SockOps) FD_READ,
2094: HTHost_catchClose, HT_PRIORITY_MAX);
1.108 cvs 2095: NETCLOSE (reqSock);
1.85 cvs 2096: /*
2097: if (reqChannel && reqHost)
2098: HTHost_clearChannel(reqHost, HT_OK);
2099: HTHost_catchClose (reqSock, NULL, FD_CLOSE);
2100: */
2101: }
2102: }
2103: }
2104: /* enable the activation of pending requests */
2105: HTHost_enable_PendingReqLaunch ();
2106:
2107: cur = Amaya->reqlist;
1.7 cvs 2108: while ((me = (AHTReqContext *) HTList_nextObject (cur)))
2109: {
2110: if (me->docid == docid)
2111: {
1.93 cvs 2112: /* kill this request */
1.85 cvs 2113:
1.93 cvs 2114: switch (me->reqStatus)
2115: {
2116: case HT_ABORT:
1.108 cvs 2117: #ifdef DEBUG_LIBWWW
2118: fprintf (stderr, "Stop: url %s says abort", me->urlName);
2119: #endif /* DEBUG_LIBWWW */
1.93 cvs 2120: break;
1.100 cvs 2121:
2122: case HT_END:
1.108 cvs 2123: #ifdef DEBUG_LIBWWW
2124: fprintf (stderr, "Stop: url %s says end", me->urlName);
2125: #endif /* DEBUG_LIBWWW */
1.100 cvs 2126: break;
2127:
1.93 cvs 2128: case HT_BUSY:
2129: me->reqStatus = HT_ABORT;
1.108 cvs 2130: #ifdef DEBUG_LIBWWW
2131: fprintf (stderr, "Stop: url %s going from busy to abort\n", me->urlName);
2132: #endif /* DEBUG_LIBWWW */
1.93 cvs 2133: break;
1.96 cvs 2134:
1.93 cvs 2135: case HT_NEW_PENDING:
2136: case HT_WAITING:
2137: default:
1.108 cvs 2138: #ifdef DEBUG_LIBWWW
2139: fprintf (stderr, "Stop: url %s says NEW_PENDING, WAITING", me->urlName);
2140: #endif /* DEBUG_LIBWWW */
1.93 cvs 2141: me->reqStatus = HT_ABORT;
2142:
1.70 cvs 2143: # ifndef _WINDOWS
1.93 cvs 2144: RequestKillAllXtevents (me);
1.74 cvs 2145: # endif _WINDOWS
1.96 cvs 2146:
1.93 cvs 2147: reqNet = HTRequest_net (me->request);
2148: reqSock = HTNet_socket (reqNet);
2149: reqChannel = HTChannel_find(reqSock);
2150: reqHost = HTChannel_host (reqChannel);
1.96 cvs 2151:
1.110 cvs 2152: if (((me->mode & AMAYA_IASYNC)
2153: || (me->mode & AMAYA_ASYNC))
1.108 cvs 2154: && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
2155: {
1.96 cvs 2156: if (HTRequest_net (me->request))
1.108 cvs 2157: /* delete the libwww request context */
1.96 cvs 2158: HTRequest_kill (me->request);
2159: #ifndef _WINDOWS
1.108 cvs 2160: /* delete the Amaya request context */
2161: AHTReqContext_delete (me);
2162: #endif /* !_WINDOWS */
1.96 cvs 2163: cur = Amaya->reqlist;
1.108 cvs 2164: #ifdef DEBUG_LIBWWW
2165: /* update the number of open requests */
2166: open_requests--;
2167: #endif /* DEBUG_LIBWWW */
2168: }
1.95 cvs 2169:
1.93 cvs 2170: /* if there are no more requests, then close
2171: the connection */
2172:
2173: if (HTHost_isIdle (reqHost) ) {
1.85 cvs 2174: #ifdef DEBUG_LIBWWW
1.93 cvs 2175: fprintf (stderr, "Host is idle, "
2176: "killing socket %d\n",
2177: reqSock);
1.85 cvs 2178: #endif /* DEBUG_LIBWWW */
1.93 cvs 2179: HTEvent_unregister (reqSock, FD_ALL);
2180: HTEvent_register(reqSock,
2181: NULL,
2182: (SockOps) FD_READ,
2183: HTHost_catchClose,
2184: HT_PRIORITY_MAX);
1.108 cvs 2185: NETCLOSE (reqSock);
1.93 cvs 2186: HTHost_clearChannel (reqHost, HT_ERROR);
2187: /*
2188: if (reqChannel && reqHost)
2189: HTHost_clearChannel(reqHost, HT_OK);
2190: HTHost_catchClose (reqSock, NULL, FD_CLOSE);
2191: */
1.85 cvs 2192: #ifdef DEBUG_LIBWWW
1.93 cvs 2193: fprintf (stderr, "After killing, "
2194: "HTHost_isIdle gives %d\n",
2195: HTHost_isIdle (reqHost));
1.85 cvs 2196: #endif /* DEBUG_LIBWWW */
1.93 cvs 2197: }
1.108 cvs 2198: #ifdef DEBUG_LIBWWW
1.93 cvs 2199: open_requests--;
1.108 cvs 2200: #endif /* DEBUG_LIBWWW */
1.93 cvs 2201: break;
2202:
2203: } /* switch */
1.7 cvs 2204: } /* if me docid */
2205: } /* while */
1.100 cvs 2206:
2207: #ifdef DEBUG_LIBWWW
2208: fprintf (stderr, "StopRequest: number of Amaya requests : %d\n", Amaya->open_requests);
2209: #endif /* DEBUG_LIBWWW */
2210:
1.7 cvs 2211: } /* if amaya open requests */
1.93 cvs 2212:
1.85 cvs 2213: } /* StopRequest */
1.17 cvs 2214:
2215: /*
2216: end of Module query.c
2217: */
2218:
1.105 cvs 2219: /*----------------------------------------------------------------------
2220: AmayaIsAlive
2221: returns the status of the AmayaAlive flag
2222: ----------------------------------------------------------------------*/
2223: #ifdef __STDC__
2224: boolean AmayaIsAlive (void)
2225: #else
2226: boolean AmayaIsAlive ()
2227: #endif /* _STDC_ */
2228: {
2229: return AmayaAlive;
2230: }
1.69 cvs 2231: #endif /* AMAYA_JAVA */
1.77 cvs 2232:
2233:
1.17 cvs 2234:
2235:
Webmaster