Annotation of Amaya/amaya/query.c, revision 1.109
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.80 cvs 812: /* don't remove or Xt will hang up during the PUT */
1.105 cvs 813: if (AmayaAlive && ((me->method == METHOD_POST) ||
1.28 cvs 814: (me->method == METHOD_PUT)))
1.7 cvs 815: {
1.80 cvs 816: PrintTerminateStatus (me, status);
817: }
818:
1.91 cvs 819: ProcessTerminateRequest (me);
1.71 cvs 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.7 cvs 830: static int AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4 cvs 831: #else
832: static int AHTLoadTerminate_handler (request, response, param, status)
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.51 cvs 1072: HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1073: /* handles all errors */
1.7 cvs 1074: HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4 cvs 1075: }
1076:
1.15 cvs 1077: /*----------------------------------------------------------------------
1.17 cvs 1078: AHTAlertInit
1079: Register alert messages and their callbacks.
1.15 cvs 1080: ----------------------------------------------------------------------*/
1081: #ifdef __STDC__
1082: static void AHTAlertInit (void)
1083: #else
1084: static void AHTAlertInit ()
1085: #endif
1086: {
1087: HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.70 cvs 1088: # ifndef _WINDOWS
1.83 cvs 1089: /* HTAlert_add ((HTAlertCallback *) Add_NewSocket_to_Loop, HT_PROG_CONNECT); */
1.70 cvs 1090: # else /* _WINDOWS */
1091: HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
1092: # endif /* _WINDOWS */
1.15 cvs 1093: HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48 cvs 1094: HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG)); /* process all messages except debug ones*/
1.15 cvs 1095: HTAlert_add (AHTConfirm, HT_A_CONFIRM);
1096: HTAlert_add (AHTPrompt, HT_A_PROMPT);
1097: HTAlert_add (AHTPromptPassword, HT_A_SECRET);
1098: HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
1099: }
1100:
1101: /*----------------------------------------------------------------------
1.97 cvs 1102: ProxyInit
1103: Reads any proxies settings which may be declared as environmental
1104: variables or in the thot.ini file. The former overrides the latter.
1105: ----------------------------------------------------------------------*/
1106: static void ProxyInit (void)
1107: {
1108: char *strptr;
1109: char *str = NULL;
1110: char *name;
1111:
1112: /* get the proxy settings from the thot.ini file */
1113: strptr = (char *) TtaGetEnvString ("HTTP_PROXY");
1114: if (strptr && *strptr)
1115: HTProxy_add ("http", strptr);
1116: /* get the no_proxy settings from the thot.ini file */
1117: strptr = (char *) TtaGetEnvString ("NO_PROXY");
1118: if (strptr && *strptr)
1119: {
1120: str = TtaStrdup (strptr); /* Get copy we can mutilate */
1121: strptr = str;
1122: while ((name = HTNextField (&strptr)) != NULL) {
1123: char *portstr = strchr (name, ':');
1124: unsigned port=0;
1125: if (portstr) {
1126: *portstr++ = '\0';
1127: if (*portstr) port = (unsigned) atoi(portstr);
1128: }
1129: /* Register it for all access methods */
1130: HTNoProxy_add (name, NULL, port);
1131: }
1132: TtaFreeMemory (str);
1133: }
1134:
1135: /* use libw3's routine to get all proxy settings from the environment */
1136: HTProxy_getEnvVar ();
1137:
1138: }
1139:
1140:
1141: /*----------------------------------------------------------------------
1.17 cvs 1142: AHTProfile_newAmaya
1143: creates the Amaya client profile for libwww.
1.15 cvs 1144: ----------------------------------------------------------------------*/
1145: #ifdef __STDC__
1146: static void AHTProfile_newAmaya (char *AppName, char *AppVersion)
1147: #else /* __STDC__ */
1148: static void AHTProfile_newAmaya (AppName, AppVersion)
1149: char *AppName;
1150: char *AppVersion;
1151: #endif /* __STDC__ */
1.4 cvs 1152: {
1153: /* If the Library is not already initialized then do it */
1154: if (!HTLib_isInitialized ())
1155: HTLibInit (AppName, AppVersion);
1156:
1157: if (!converters)
1158: converters = HTList_new ();
1.27 cvs 1159: if (!acceptTypes)
1160: acceptTypes = HTList_new ();
1.4 cvs 1161: if (!encodings)
1162: encodings = HTList_new ();
1163:
1164: /* Register the default set of transport protocols */
1165: HTTransportInit ();
1166:
1167: /* Register the default set of application protocol modules */
1168: AHTProtocolInit ();
1169:
1170: /* Enable the persistent cache */
1171: /* HTCacheInit (NULL, 20); */
1172:
1173: /* Register the default set of BEFORE and AFTER filters */
1174: AHTNetInit ();
1175:
1176: /* Set up the default set of Authentication schemes */
1177: HTAAInit ();
1178:
1.97 cvs 1179: /* Get any proxy settings */
1180: ProxyInit ();
1.4 cvs 1181:
1182: /* Register the default set of converters */
1183: AHTConverterInit (converters);
1.27 cvs 1184: AHTAcceptTypesInit (acceptTypes);
1.4 cvs 1185: HTFormat_setConversion (converters);
1186:
1187: /* Register the default set of transfer encoders and decoders */
1188: HTEncoderInit (encodings); /* chunks ??? */
1189: HTFormat_setTransferCoding (encodings);
1190:
1191: /* Register the default set of MIME header parsers */
1.74 cvs 1192: HTMIMEInit (); /* must be called again for language selector */
1.4 cvs 1193:
1194: /* Register the default set of Icons for directory listings */
1.27 cvs 1195: /*HTIconInit(NULL); *//* experimental */
1.4 cvs 1196:
1197: /* Register the default set of messages and dialog functions */
1198: AHTAlertInit ();
1199: HTAlert_setInteractive (YES);
1200: }
1201:
1.5 cvs 1202: /*----------------------------------------------------------------------
1.17 cvs 1203: AHTProfile_delete
1204: deletes the Amaya client profile.
1.5 cvs 1205: ----------------------------------------------------------------------*/
1.4 cvs 1206: #ifdef __STDC__
1207: static void AHTProfile_delete (void)
1208: #else
1209: static void AHTProfile_delete ()
1.7 cvs 1210: #endif /* __STDC__ */
1.4 cvs 1211: {
1.22 cvs 1212:
1213: /* free the Amaya global context */
1.74 cvs 1214: if (!converters)
1215: HTConversion_deleteAll (converters);
1216: if (!acceptTypes)
1217: HTConversion_deleteAll (acceptTypes);
1218: if (!encodings)
1219: HTCoding_deleteAll (encodings);
1220:
1221: HTList_delete (Amaya->docid_status);
1222: HTList_delete (Amaya->reqlist);
1223: TtaFreeMemory (Amaya);
1224: {
1.61 cvs 1225:
1.74 cvs 1226: if (HTLib_isInitialized ())
1227:
1.61 cvs 1228: # ifdef _WINDOWS
1229: HTEventTerminate ();
1230: # endif _WINDOWS;
1.74 cvs 1231:
1232: /* Clean up the persistent cache (if any) */
1.102 cvs 1233: /* HTCacheTerminate (); */
1.74 cvs 1234:
1235: /* Clean up all the global preferences */
1236: HTFormat_deleteAll ();
1237:
1238: /* Terminate libwww */
1239: HTLibTerminate ();
1240: }
1.4 cvs 1241: }
1242:
1.5 cvs 1243: /*----------------------------------------------------------------------
1.17 cvs 1244: QueryInit
1245: initializes the libwww interface
1.5 cvs 1246: ----------------------------------------------------------------------*/
1.4 cvs 1247: #ifdef __STDC__
1248: void QueryInit ()
1249: #else
1250: void QueryInit ()
1251: #endif
1252: {
1253:
1.105 cvs 1254: AmayaAlive = TRUE;
1.4 cvs 1255: AHTProfile_newAmaya (HTAppName, HTAppVersion);
1256:
1.69 cvs 1257: /* New AHTBridge stuff */
1.4 cvs 1258:
1.54 cvs 1259: # ifdef _WINDOWS
1.75 cvs 1260: AHTEventInit ();
1.54 cvs 1261: # endif _WINDOWS;
1.72 cvs 1262:
1.70 cvs 1263: HTEvent_setRegisterCallback (AHTEvent_register);
1.78 cvs 1264: /*** a effacer ***/
1265: HTEvent_setUnregisterCallback (AHTEvent_unregister);
1266: /*** ***/
1.4 cvs 1267:
1.72 cvs 1268: # ifndef _WINDOWS
1269: HTEvent_setUnregisterCallback (AHTEvent_unregister);
1270: # endif /* _WINDOWS */
1271:
1.74 cvs 1272: #ifdef DEBUG_LIBWWW
1273: WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | PROT_TRACE;
1274: #endif
1.4 cvs 1275:
1276: /* Trace activation (for debugging) */
1.7 cvs 1277: /*
1.4 cvs 1278: WWW_TraceFlag = SHOW_APP_TRACE | SHOW_UTIL_TRACE |
1279: SHOW_BIND_TRACE | SHOW_THREAD_TRACE |
1280: SHOW_STREAM_TRACE | SHOW_PROTOCOL_TRACE |
1281: SHOW_URI_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
1282: SHOW_CORE_TRACE;
1283:
1.7 cvs 1284: */
1.4 cvs 1285:
1286: /***
1287: WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
1288: SHOW_PROTOCOL_TRACE| SHOW_APP_TRACE | SHOW_UTIL_TRACE;
1289: ***/
1290:
1291: /* Setting up other user interfaces */
1292:
1293: /* Setting up different network parameters */
1.17 cvs 1294: /* Maximum number of simultaneous open sockets */
1.4 cvs 1295: HTNet_setMaxSocket (8);
1.75 cvs 1296: /* different network services timeouts */
1.109 ! cvs 1297: HTDNS_setTimeout (60);
1.75 cvs 1298: #ifdef _WINDOWS
1299: /* under windows, the libwww persistent socket handling has
1300: ** some bugs. The following line inhibits idle socket reusal.
1301: ** this is a bit slower, but avoids crashes and gives us time
1302: ** to distribute Amaya before having to patch up libwww.
1303: */
1.76 cvs 1304: HTHost_setPersistTimeout (-1L);
1.75 cvs 1305: #else
1.76 cvs 1306: HTHost_setPersistTimeout (60L);
1.75 cvs 1307: #endif /* _WINDOWS */
1308:
1.17 cvs 1309: /* Cache is disabled in this version */
1.4 cvs 1310: HTCacheMode_setEnabled (0);
1311:
1312: /* Initialization of the global context */
1313: Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
1314: Amaya->reqlist = HTList_new ();
1315: Amaya->docid_status = HTList_new ();
1316: Amaya->open_requests = 0;
1.74 cvs 1317:
1.4 cvs 1318: #ifdef CATCH_SIG
1.18 cvs 1319: signal (SIGPIPE, SIG_IGN);
1.4 cvs 1320: #endif
1.94 cvs 1321:
1.15 cvs 1322: }
1323:
1.69 cvs 1324: #ifndef _WINDOWS
1.15 cvs 1325: /*----------------------------------------------------------------------
1.17 cvs 1326: LoopForStop
1327: a copy of the Thop event loop so we can handle the stop button.
1.69 cvs 1328: Not useful for windows code (Ramzi).
1.15 cvs 1329: ----------------------------------------------------------------------*/
1330: #ifdef __STDC__
1331: static int LoopForStop (AHTReqContext * me)
1332: #else
1333: static int LoopForStop (AHTReqContext * me)
1334: #endif
1335: {
1336:
1.25 cvs 1337: extern ThotAppContext app_cont;
1.51 cvs 1338: XEvent ev;
1339: XtInputMask status;
1.17 cvs 1340: int status_req = HT_OK;
1.15 cvs 1341:
1342: /* to test the async calls */
1.17 cvs 1343: /* Loop while waiting for new events, exists when the request is over */
1.15 cvs 1344: while (me->reqStatus != HT_ABORT &&
1345: me->reqStatus != HT_END &&
1.69 cvs 1346: me->reqStatus != HT_ERR) {
1.105 cvs 1347: if (!AmayaAlive)
1.69 cvs 1348: /* Amaya was killed by one of the callback handlers */
1349: exit (0);
1350:
1351: status = XtAppPending (app_cont);
1352: if (status & XtIMXEvent) {
1353: XtAppNextEvent (app_cont, &ev);
1354: TtaHandleOneEvent (&ev);
1355: } else if (status & (XtIMAll & (~XtIMXEvent))) {
1356: XtAppProcessEvent (app_cont, (XtIMAll & (~XtIMXEvent)));
1357: } else {
1358: XtAppNextEvent (app_cont, &ev);
1359: TtaHandleOneEvent (&ev);
1360: }
1361: }
1.4 cvs 1362:
1.69 cvs 1363: switch (me->reqStatus) {
1364: case HT_ERR:
1365: case HT_ABORT:
1.15 cvs 1366: status_req = HT_ERROR;
1367: break;
1368:
1.69 cvs 1369: case HT_END:
1.15 cvs 1370: status_req = HT_OK;
1371: break;
1372:
1.69 cvs 1373: default:
1.15 cvs 1374: break;
1.69 cvs 1375: }
1.15 cvs 1376: return (status_req);
1.4 cvs 1377: }
1.69 cvs 1378: #endif /* _WINDOWS */
1.4 cvs 1379:
1.5 cvs 1380: /*----------------------------------------------------------------------
1.15 cvs 1381: QueryClose
1.21 cvs 1382: closes all existing threads, frees all non-automatically deallocated
1383: memory and then ends libwww.
1.5 cvs 1384: ----------------------------------------------------------------------*/
1.4 cvs 1385: void QueryClose ()
1386: {
1.24 cvs 1387:
1.105 cvs 1388: AmayaAlive = FALSE;
1.24 cvs 1389:
1.21 cvs 1390: /* remove all the handlers and callbacks that may output a message to
1391: a non-existent Amaya window */
1392:
1393: HTNet_deleteAfter (AHTLoadTerminate_handler);
1394: HTNet_deleteAfter (redirection_handler);
1395: HTAlertCall_deleteAll (HTAlert_global () );
1.23 cvs 1396: HTAlert_setGlobal ((HTList *) NULL);
1.24 cvs 1397: HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
1.27 cvs 1398: HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
1.87 cvs 1399: HTHost_setActivateRequestCallback (NULL);
1.4 cvs 1400: Thread_deleteAll ();
1.21 cvs 1401:
1.4 cvs 1402: HTProxy_deleteAll ();
1403: HTNoProxy_deleteAll ();
1404: HTGateway_deleteAll ();
1405: AHTProfile_delete ();
1406: }
1407:
1.99 cvs 1408:
1409: /*----------------------------------------------------------------------
1410: InvokeGetObjectWWW_callback
1411: A simple function to invoke a callback function whenever there's an error
1412: in GetObjectWWW
1413: ---------------------------------------------------------------------*/
1414:
1415: #ifdef _STDC
1416: void InvokeGetObjectWWW_callback (int docid, char *urlName, char *outputfile, TTcbf *terminate_cbf, void *context_cbf)
1417: #else
1418: void InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf, context_tcbf)
1419: int docid;
1420: char *urlName;
1421: char *outputfile;
1422: TTcbf *terminate_cbf;
1423: void *context_tcbf;
1424: #endif /* _STDC */
1425: {
1426: if (!terminate_cbf)
1427: return;
1428:
1429: (*terminate_cbf) (docid, -1, urlName, outputfile,
1430: NULL, context_tcbf);
1431: }
1432:
1433:
1434:
1.5 cvs 1435: /*----------------------------------------------------------------------
1.15 cvs 1436: GetObjectWWW
1.17 cvs 1437: this function requests a resource designated by a URLname into a
1438: temporary filename. The download can come from a simple GET operation,
1439: or can come from POSTING/GETTING a form. In the latter
1440: case, the function receives a query string to send to the server.
1441:
1.5 cvs 1442: 4 file retrieval modes are proposed:
1443: AMAYA_SYNC : blocking mode
1444: AMAYA_ISYNC : incremental, blocking mode
1445: AMAYA_ASYNC : non-blocking mode
1446: AMAYA_IASYNC : incremental, non-blocking mode
1447:
1448: In the incremental mode, each time a package arrives, it will be
1449: stored in the temporary file. In addition, if an
1450: incremental_callback function is defined, this function will be
1451: called and handled a copy of the newly received data package.
1452: Finally, if a terminate_callback function is defined, it will be
1453: invoked when the request terminates. The caller of this function
1.4 cvs 1454: can define two different contexts to be passed to the callback
1455: functions.
1456:
1457: When the function is called with the SYNC mode, the function will
1458: return only when the requested file has been loaded.
1459: The ASYNC mode will immediately return after setting up the
1460: call.
1461:
1462: Notes:
1463: At the end of a succesful request, the urlName string contains the
1464: name of the actually retrieved URL. As a URL can change over the time,
1465: (e.g., be redirected elsewhere), it is advised that the function
1.17 cvs 1466: caller verify the value of the urlName variable at the end of
1.4 cvs 1467: a request.
1468:
1469: Inputs:
1470: - docid Document identifier for the set of objects being
1471: retrieved.
1472: - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
1473: - outputfile A pointer to an empty string of MAX_URL_LENGTH.
1474: - mode The retrieval mode.
1475: - incremental_cbf
1476: - context_icbf
1477: Callback and context for the incremental modes
1478: - terminate_cbf
1479: - context_icbf
1480: Callback and context for a terminate handler
1.17 cvs 1481: -error_html if TRUE, then display any server error message as an
1482: HTML document.
1.88 cvs 1483: - content_type a string
1484:
1.4 cvs 1485: Outputs:
1486: - urlName The URL that was retrieved
1487: - outputfile The name of the temporary file which holds the
1488: retrieved data. (Only in case of success)
1.88 cvs 1489: - if content_type wasn't NULL, it will contain a copy of the parameter
1490: sent in the HTTP answer
1.4 cvs 1491: Returns:
1492: HT_ERROR
1493: HT_OK
1.5 cvs 1494:
1495: ----------------------------------------------------------------------*/
1.4 cvs 1496: #ifdef __STDC__
1.52 cvs 1497: int GetObjectWWW (int docid, char* urlName, char* postString, char* outputfile, int mode,
1498: TIcbf* incremental_cbf, void* context_icbf, TTcbf* terminate_cbf,
1.88 cvs 1499: void* context_tcbf, boolean error_html, char *content_type)
1.4 cvs 1500: #else
1.52 cvs 1501: int GetObjectWWW (docid, urlName, postString, outputfile, mode, incremental_cbf, context_icbf,
1.88 cvs 1502: terminate_cbf, context_tcbf, error_html, content_type)
1.73 cvs 1503: int docid;
1504: char *urlName;
1505: char *postString;
1506: char *outputfile;
1507: int mode;
1508: TIcbf *incremental_cbf;
1509: void *context_icbf;
1510: TTcbf *terminate_cbf;
1511: void *context_tcbf;
1512: boolean error_html;
1.88 cvs 1513: char *content_type;
1.4 cvs 1514: #endif
1515: {
1516: AHTReqContext *me;
1517: char *ref;
1.107 cvs 1518: int status, l;
1.7 cvs 1519:
1.69 cvs 1520: if (urlName == NULL || docid == 0 || outputfile == NULL) {
1521: /* no file to be loaded */
1522: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1523:
1524: if (error_html)
1525: /* so we can show the error message */
1526: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99 cvs 1527: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1528: context_tcbf);
1.69 cvs 1529: return HT_ERROR;
1530: }
1.7 cvs 1531:
1.4 cvs 1532: /* do we support this protocol? */
1.69 cvs 1533: if (IsValidProtocol (urlName) == NO) {
1534: /* return error */
1535: outputfile[0] = EOS; /* file could not be opened */
1536: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL), urlName);
1537:
1538: if (error_html)
1539: /* so we can show the error message */
1540: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99 cvs 1541: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1542: context_tcbf);
1.69 cvs 1543: return HT_ERROR;
1544: }
1.4 cvs 1545:
1.58 cvs 1546: /*create a tempfilename */
1.59 cvs 1547: sprintf (outputfile, "%s%c%d%c%04dAM", TempFileDirectory, DIR_SEP, docid, DIR_SEP, object_counter);
1.4 cvs 1548:
1549: /* update the object_counter */
1550: object_counter++;
1551: /* normalize the URL */
1.45 cvs 1552: ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4 cvs 1553: /* should we abort the request if we could not normalize the url? */
1.69 cvs 1554: if (ref == (char*) NULL || ref[0] == EOS) {
1555: /*error */
1556: outputfile[0] = EOS;
1557: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1558:
1559: if (error_html)
1560: /* so we can show the error message */
1561: DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99 cvs 1562: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1563: context_tcbf);
1.69 cvs 1564: return HT_ERROR;
1565: }
1.4 cvs 1566: /* verify if that file name existed */
1.9 cvs 1567: if (TtaFileExist (outputfile))
1.77 cvs 1568: TtaFileUnlink (outputfile);
1.4 cvs 1569:
1570: /* Initialize the request structure */
1571: me = AHTReqContext_new (docid);
1.69 cvs 1572: if (me == NULL) {
1.77 cvs 1573: outputfile[0] = EOS;
1574: /* need an error message here */
1575: TtaFreeMemory (ref);
1.99 cvs 1576: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1577: context_tcbf);
1578: return HT_ERROR;
1.69 cvs 1579: }
1.77 cvs 1580:
1.4 cvs 1581: /* Specific initializations for POST and GET */
1.69 cvs 1582: if (mode & AMAYA_FORM_POST) {
1.77 cvs 1583: me->method = METHOD_POST;
1584: if (postString) {
1585: me->block_size = strlen (postString);
1.99 cvs 1586: me->mem_ptr = TtaStrdup (postString);
1.77 cvs 1587: } else {
1588: me->mem_ptr = "";
1589: me->block_size = 0;
1590: }
1591: HTRequest_setMethod (me->request, METHOD_POST);
1.69 cvs 1592: HTRequest_setPostCallback (me->request, AHTUpload_callback);
1593: } else {
1.77 cvs 1594: me->method = METHOD_GET;
1595: me->dest = (HTParentAnchor *) NULL; /*useful only for PUT and POST methods */
1596: if (!HasKnownFileSuffix (ref))
1597: HTRequest_setConversion(me->request, acceptTypes, TRUE);
1.69 cvs 1598: }
1.93 cvs 1599:
1.4 cvs 1600: /* Common initialization */
1601: me->mode = mode;
1602: me->error_html = error_html;
1603: me->incremental_cbf = incremental_cbf;
1604: me->context_icbf = context_icbf;
1605: me->terminate_cbf = terminate_cbf;
1606: me->context_tcbf = context_tcbf;
1.64 cvs 1607:
1.69 cvs 1608: /* for the async. request modes, we need to have our
1.4 cvs 1609: own copy of outputfile and urlname
1610: */
1611:
1.69 cvs 1612: if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) {
1.107 cvs 1613: l = strlen (outputfile);
1614: if (l > MAX_LENGTH)
1615: me->outputfile = TtaGetMemory (l + 2);
1616: else
1617: me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
1.104 cvs 1618: strcpy (me->outputfile, outputfile);
1.107 cvs 1619: l = strlen (urlName);
1620: if (l > MAX_LENGTH)
1621: me->urlName = TtaGetMemory (l + 2);
1622: else
1623: me->urlName = TtaGetMemory (MAX_LENGTH + 2);
1.104 cvs 1624: strcpy (me->urlName, urlName);
1.69 cvs 1625: # ifdef _WINDOWS
1626: HTRequest_setPreemptive (me->request, NO);
1627: } else {
1.77 cvs 1628: me->outputfile = outputfile;
1629: me->urlName = urlName;
1630: HTRequest_setPreemptive (me->request, YES);
1.61 cvs 1631: }
1.99 cvs 1632: # else /* !_WINDOWS */
1.69 cvs 1633: } else {
1.77 cvs 1634: me->outputfile = outputfile;
1635: me->urlName = urlName;
1.99 cvs 1636: me->content_type = content_type;
1.61 cvs 1637: }
1.77 cvs 1638: /***
1.57 cvs 1639: Change for taking into account the stop button:
1640: The requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1641: we will loop until the document has been received or a stop signal
1642: generated
1.77 cvs 1643: ****/
1644: HTRequest_setPreemptive (me->request, NO);
1.69 cvs 1645: # endif /* _WINDOWS */
1.61 cvs 1646:
1647: /* prepare the URLname that will be displayed in teh status bar */
1648: ChopURL (me->status_urlName, me->urlName);
1649:
1.77 cvs 1650: TtaSetStatus (me->docid, 1,
1651: TtaGetMessage (AMAYA, AM_FETCHING),
1652: me->status_urlName);
1.4 cvs 1653:
1654: me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45 cvs 1655: TtaFreeMemory (ref);
1.4 cvs 1656:
1.69 cvs 1657: if (mode & AMAYA_FORM_POST) {
1658: HTAnchor_setFormat ((HTParentAnchor *) me->anchor, HTAtom_for ("application/x-www-form-urlencoded"));
1659: HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1660: HTRequest_setEntityAnchor (me->request, me->anchor);
1661:
1662: status = HTLoadAbsolute (urlName, me->request);
1663: } else
1.77 cvs 1664: status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.99 cvs 1665:
1666: if (status == HT_ERROR && me->reqStatus == HT_NEW)
1.100 cvs 1667: {
1668: InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1669: context_tcbf);
1670: }
1.69 cvs 1671:
1.72 cvs 1672: #ifndef _WINDOWS
1.77 cvs 1673: if (status == HT_ERROR ||
1674: me->reqStatus == HT_END ||
1675: me->reqStatus == HT_ERR)
1676: {
1677: /* in case of error, free all allocated memory and exit */
1.96 cvs 1678: if (me->output && me->output != stdout) {
1.85 cvs 1679: #ifdef DEBUG_LIBWWW
1.96 cvs 1680: fprintf (stderr, "GetObjectWWW:: URL is %s, closing "
1.85 cvs 1681: "FILE %p\n", me->urlName, me->output);
1682: #endif
1.77 cvs 1683: fclose (me->output);
1.82 cvs 1684: me->output = NULL;
1685: }
1686:
1.99 cvs 1687: if (me->reqStatus == HT_ERR) {
1.77 cvs 1688: status = HT_ERROR;
1689: /* show an error message on the status bar */
1690: DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
1691: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD), me->
1692: status_urlName);
1.72 cvs 1693: } else
1.77 cvs 1694: status = HT_OK;
1.72 cvs 1695:
1696: AHTReqContext_delete (me);
1.77 cvs 1697: } else {
1.72 cvs 1698: /* part of the stop button handler */
1699: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC)) {
1700: status = LoopForStop (me);
1701: AHTReqContext_delete (me);
1702: }
1703: }
1704: #else /* !_WINDOWS */
1705:
1.90 cvs 1706: if (status == HT_ERROR ||
1707: me->reqStatus == HT_ERROR)
1708: {
1709: status = HT_ERROR;
1.71 cvs 1710:
1.90 cvs 1711: /* in case of error, close any open files, free all allocated
1712: memory and exit */
1713: if (me->output && me->output != stdout)
1714: {
1.85 cvs 1715: #ifdef DEBUG_LIBWWW
1.90 cvs 1716: fprintf (stderr, "GetObjectWWW: URL is %s, closing "
1717: "FILE %p\n", me->urlName, me->output);
1.85 cvs 1718: #endif
1.90 cvs 1719: fclose (me->output);
1720: me->output = NULL;
1721: }
1.85 cvs 1722:
1.90 cvs 1723: if (me->reqStatus == HT_ERR)
1724: {
1725: /* show an error message on the status bar */
1726: DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
1727: TtaSetStatus (me->docid, 1,
1728: TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1729: me->status_urlName);
1730: }
1731: }
1732: else
1733: {
1734: status = HT_OK;
1735: }
1736:
1.93 cvs 1737: /* part of the stop button handler */
1738: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
1739: {
1740: if (HTRequest_net (me->request))
1741: HTRequest_kill (me->request);
1742: else
1743: AHTReqContext_delete (me);
1744: }
1.4 cvs 1745:
1.72 cvs 1746: #endif /* !_WINDOWS */
1747:
1.4 cvs 1748: return (status);
1749: }
1750:
1.5 cvs 1751: /*----------------------------------------------------------------------
1.17 cvs 1752: PutObjectWWW
1753: frontend for uploading a resource to a URL. This function downloads
1754: a file to be uploaded into memory, it then calls UploadMemWWW to
1755: finish the job.
1756:
1.5 cvs 1757: 2 upload modes are proposed:
1758: AMAYA_SYNC : blocking mode
1759: AMAYA_ASYNC : non-blocking mode
1760:
1.4 cvs 1761: When the function is called with the SYNC mode, the function will
1762: return only when the file has been uploaded.
1763: The ASYNC mode will immediately return after setting up the
1764: call. Furthermore, at the end of an upload, the ASYNC mode will
1765: call back terminate_cbf, handling it the context defined in
1766: context_tcbf.
1767:
1768: Notes:
1769: At the end of a succesful request, the urlName string contains the
1770: name of the actually uploaded URL. As a URL can change over the time,
1771: (e.g., be redirected elsewhere), it is advised that the function
1772: caller verifies the value of the urlName variable at the end of
1773: a request.
1774:
1775: Inputs:
1776: - docid Document identifier for the set of objects being
1777: retrieved.
1778: - fileName A pointer to the local file to upload
1779: - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
1780: - mode The retrieval mode.
1781: - terminate_cbf
1782: - context_icbf
1783: Callback and context for a terminate handler
1784:
1785: Outputs:
1786: - urlName The URL that was uploaded
1787:
1788: Returns:
1789: HT_ERROR
1790: HT_OK
1.5 cvs 1791: ----------------------------------------------------------------------*/
1.4 cvs 1792: #ifdef __STDC__
1.27 cvs 1793: int PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4 cvs 1794: TTcbf * terminate_cbf, void *context_tcbf)
1795: #else
1.26 cvs 1796: int PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4 cvs 1797: ,terminate_cbf, context_tcbf)
1798: int docid;
1799: char *urlName;
1800: char *fileName;
1801: int mode;
1.27 cvs 1802: PicType contentType;
1.4 cvs 1803: TTcbf *terminate_cbf;
1804: void *context_tcbf;
1805:
1806: #endif
1807: {
1.7 cvs 1808: /*AHTReqContext *me; */
1.4 cvs 1809: int status;
1810:
1811: int fd;
1812: struct stat file_stat;
1813: char *mem_ptr;
1814: unsigned long block_size;
1815:
1.33 cvs 1816: AmayaLastHTTPErrorMsg [0] = EOS;
1817:
1.4 cvs 1818: if (urlName == NULL || docid == 0 || fileName == NULL ||
1.9 cvs 1819: !TtaFileExist (fileName))
1.4 cvs 1820: /* no file to be uploaded */
1821: return HT_ERROR;
1822:
1823: /* do we support this protocol? */
1.7 cvs 1824: if (IsValidProtocol (urlName) == NO)
1825: {
1826: /* return error */
1.77 cvs 1827: TtaSetStatus (docid, 1,
1828: TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7 cvs 1829: urlName);
1830: return HT_ERROR;
1831: }
1.4 cvs 1832: /* read the file into memory */
1.70 cvs 1833: # ifndef _WINDOWS
1.7 cvs 1834: if ((fd = open (fileName, O_RDONLY)) == -1)
1.70 cvs 1835: # else /* _WINDOWS */
1.74 cvs 1836: if ((fd = open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.70 cvs 1837: # endif /* _WINDOWS */
1.7 cvs 1838: {
1839: /* if we could not open the file, exit */
1840: /*error msg here */
1841: return (HT_ERROR);
1842: }
1.4 cvs 1843:
1844: fstat (fd, &file_stat);
1845:
1.7 cvs 1846: if (file_stat.st_size == 0)
1847: {
1848: /* file was empty */
1849: /*errmsg here */
1850: close (fd);
1851: return (HT_ERROR);
1852: }
1.4 cvs 1853: block_size = file_stat.st_size;
1854:
1855: if (THD_TRACE)
1856: fprintf (stderr, "file size == %u\n", (unsigned) block_size);
1857:
1858: mem_ptr = (char *) TtaGetMemory (block_size);
1859:
1.7 cvs 1860: if (mem_ptr == (char *) NULL)
1861: {
1862: /* could not allocate enough memory */
1863: /*errmsg here */
1864: close (fd);
1865: return (HT_ERROR);
1866: }
1.4 cvs 1867: read (fd, mem_ptr, block_size);
1868:
1869: close (fd);
1870:
1.27 cvs 1871: status = UploadMemWWW (docid, METHOD_PUT, urlName, contentType, mem_ptr,
1.4 cvs 1872: block_size, mode, terminate_cbf,
1873: context_tcbf, (char *) NULL);
1874:
1875: TtaFreeMemory (mem_ptr);
1.28 cvs 1876: TtaHandlePendingEvents ();
1.4 cvs 1877: return (status);
1878: }
1879:
1.5 cvs 1880: /*----------------------------------------------------------------------
1.17 cvs 1881: UploadMemWWW
1882: low level interface function to libwww for uploading a block of
1883: memory to a URL.
1.5 cvs 1884: ----------------------------------------------------------------------*/
1.4 cvs 1885: #ifdef __STDC__
1886: int UploadMemWWW (int docid, HTMethod method,
1.27 cvs 1887: char *urlName, PicType contentType, char *mem_ptr, unsigned long block_size,
1.4 cvs 1888: int mode, TTcbf * terminate_cbf, void *context_tcbf,
1889: char *outputfile)
1890: #else
1.27 cvs 1891: int UploadMemWWW (docid, method, urlName, contentType, mem_ptr, block_size, mode,
1.4 cvs 1892: terminate_cbf, context_tcbf, outputfile)
1893: int docid;
1894: HTMethod method;
1895: char *urlName;
1.27 cvs 1896: PicType contentType;
1.4 cvs 1897: char *mem_ptr;
1898: usigned long block_size;
1899: int mode;
1900: TTcbf *terminate_cbf;
1901: void *context_tcbf;
1902: char *outputfile;
1.7 cvs 1903:
1.4 cvs 1904: #endif
1905: {
1906: AHTReqContext *me;
1907: int status;
1908:
1909: if (mem_ptr == (char *) NULL ||
1910: block_size == 0 ||
1911: docid == 0 ||
1.7 cvs 1912: urlName == (char *) NULL)
1913: {
1914: /* nothing to be uploaded */
1915: return HT_ERROR;
1916: }
1.4 cvs 1917:
1918: /* Initialize the request structure */
1919: me = AHTReqContext_new (docid);
1920:
1.7 cvs 1921: if (me == NULL)
1922: {
1923: /* need an error message here */
1924: TtaHandlePendingEvents ();
1925: return (HT_ERROR);
1926: }
1.4 cvs 1927: me->mode = mode;
1928:
1929: me->incremental_cbf = (TIcbf *) NULL;
1930: me->context_icbf = (void *) NULL;
1931: me->terminate_cbf = terminate_cbf;
1932: me->context_tcbf = context_tcbf;
1933:
1934: me->output = stdout;
1935: me->outputfile = (char *) NULL;
1936: me->urlName = urlName;
1937:
1.70 cvs 1938: #ifdef _WINDOWS
1939: HTRequest_setPreemptive (me->request, YES);
1940: #else
1.4 cvs 1941: HTRequest_setPreemptive (me->request, NO);
1.70 cvs 1942: #endif /* _WINDOWS */
1.74 cvs 1943:
1.17 cvs 1944: /* select the parameters that distinguish a PUT from a GET/POST */
1.4 cvs 1945: me->method = METHOD_PUT;
1946: HTRequest_setMethod (me->request, METHOD_PUT);
1947: me->output = stdout;
1.17 cvs 1948: /* we are not expecting to receive any input from the server */
1949: me->outputfile = (char *) NULL;
1.4 cvs 1950:
1951: me->mem_ptr = mem_ptr;
1952: me->block_size = block_size;
1.17 cvs 1953:
1954: /* set the callback which will actually copy data into the
1955: output stream */
1956:
1.4 cvs 1957: HTRequest_setPostCallback (me->request, AHTUpload_callback);
1.72 cvs 1958:
1.4 cvs 1959: me->anchor = (HTParentAnchor *) HTAnchor_findAddress (urlName);
1.7 cvs 1960:
1.28 cvs 1961: /* Set the Content-Type of the file we are uploading */
1.77 cvs 1962: HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
1963: AHTGuessAtom_for (me->urlName, contentType));
1.17 cvs 1964:
1.7 cvs 1965: HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1.4 cvs 1966: HTRequest_setEntityAnchor (me->request, me->anchor);
1.74 cvs 1967: /* prepare the URLname that will be displayed in teh status bar */
1968: ChopURL (me->status_urlName, me->urlName);
1969: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
1970: me->status_urlName);
1.4 cvs 1971: status = HTLoadAbsolute (urlName, me->request);
1972:
1.74 cvs 1973: #ifndef _WINDOWS
1.77 cvs 1974: if (status == HT_ERROR ||
1975: me->reqStatus == HT_END ||
1976: me->reqStatus == HT_ERR ||
1977: HTError_hasSeverity (HTRequest_error (me->request), ERR_INFO))
1.7 cvs 1978: {
1.17 cvs 1979: status = HT_ERROR;
1.28 cvs 1980: }
1.7 cvs 1981: else
1982: {
1983: /* part of the stop button handler */
1.4 cvs 1984:
1.7 cvs 1985: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
1986: {
1987: status = LoopForStop (me);
1988: }
1.15 cvs 1989: }
1.90 cvs 1990: #else /* _WINDOWS */
1991: if (status == HT_ERROR ||
1992: me->reqStatus == HT_ERR) /* || Error_hasSeverity (HTRequest_error (me->request), ERR_INFO)) */
1993: status = HT_ERROR;
1994: else
1995: status = HT_OK;
1996: #endif /* _WINDOWS */
1997:
1.92 cvs 1998: if (HTRequest_net (me->request))
1999: {
2000: HTRequest_kill (me->request);
2001: }
1.93 cvs 2002: else
2003: AHTReqContext_delete (me);
1.4 cvs 2004: return (status);
1.28 cvs 2005: }
1.4 cvs 2006:
2007:
2008:
1.5 cvs 2009: /*----------------------------------------------------------------------
1.17 cvs 2010: Stop Request
2011: stops (kills) all active requests associated with a docid
1.5 cvs 2012: ----------------------------------------------------------------------*/
1.4 cvs 2013: #ifdef __STDC__
2014: void StopRequest (int docid)
2015: #else
2016: void StopRequest (docid)
2017: int docid;
2018: #endif
2019: {
2020: HTList *cur;
2021: AHTDocId_Status *docid_status;
2022: AHTReqContext *me;
1.84 cvs 2023: HTNet *reqNet;
2024: HTHost *reqHost;
1.85 cvs 2025: HTChannel *reqChannel;
2026: int reqSock;
1.108 cvs 2027: #ifdef DEBUG_LIBWWW
2028: int open_requests;
2029: #endif /* DEBUG_LIBWWW */
1.100 cvs 2030:
1.7 cvs 2031: if (Amaya)
2032: {
1.100 cvs 2033: #ifdef DEBUG_LIBWWW
2034: fprintf (stderr, "StopRequest: number of Amaya requests : %d\n", Amaya->open_requests);
2035: #endif /* DEBUG_LIBWWW */
2036:
1.7 cvs 2037: docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
1.4 cvs 2038: Amaya->docid_status);
1.7 cvs 2039: /* verify if there are any requests at all associated with docid */
1.4 cvs 2040:
1.7 cvs 2041: if (docid_status == (AHTDocId_Status *) NULL)
2042: return;
1.4 cvs 2043:
1.108 cvs 2044: #ifdef DEBUG_LIBWWW
1.7 cvs 2045: open_requests = docid_status->counter;
1.108 cvs 2046: #endif /* DEBUG_LIBWWW */
1.4 cvs 2047:
1.85 cvs 2048: /* First, kill all pending requests */
2049: /* We first inhibit the activation of pending requests */
2050: HTHost_disable_PendingReqLaunch ();
2051: cur = Amaya->reqlist;
2052: while ((me = (AHTReqContext *) HTList_nextObject (cur)))
2053: {
2054: if (me->docid == docid && me->reqStatus == HT_NEW)
2055: {
2056: reqNet = HTRequest_net (me->request);
2057: reqSock = HTNet_socket (reqNet);
2058: reqChannel = HTChannel_find(reqSock);
2059: reqHost = HTChannel_host (reqChannel);
1.95 cvs 2060:
2061: /* If we have an ASYNC request, we kill it.
2062: ** If it's a SYNC request, we just mark it as aborted
2063: */
2064: me->reqStatus = HT_ABORT;
1.108 cvs 2065: if ((me->mode & AMAYA_IASYNC)
2066: || (me->mode & AMAYA_ASYNC)
2067: && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
2068: {
2069: if (HTRequest_net (me->request))
2070: /* delete the libwww request context */
2071: HTRequest_kill (me->request);
1.96 cvs 2072: #ifndef _WINDOWS
2073: /* delete the Amaya request context */
2074: AHTReqContext_delete (me);
1.108 cvs 2075: #endif /* !_WINDOWS */
1.96 cvs 2076: cur = Amaya->reqlist;
1.108 cvs 2077: #ifdef DEBUG_LIBWWW
2078: /* update the number of open requests */
2079: open_requests--;
2080: #endif /* DEBUG_LIBWWW */
1.85 cvs 2081: }
1.108 cvs 2082:
1.85 cvs 2083: if (HTHost_isIdle (reqHost) ) {
2084: #ifdef DEBUG_LIBWWW
2085: fprintf (stderr, "Host is idle, killing socket %d\n",
1.95 cvs 2086: reqSock);
1.85 cvs 2087: #endif /* DEBUG_LIBWWW */
2088:
2089: HTEvent_unregister (reqSock, FD_ALL);
2090: HTEvent_register(reqSock, NULL, (SockOps) FD_READ,
2091: HTHost_catchClose, HT_PRIORITY_MAX);
1.108 cvs 2092: NETCLOSE (reqSock);
1.85 cvs 2093: /*
2094: if (reqChannel && reqHost)
2095: HTHost_clearChannel(reqHost, HT_OK);
2096: HTHost_catchClose (reqSock, NULL, FD_CLOSE);
2097: */
2098: }
2099: }
2100: }
2101: /* enable the activation of pending requests */
2102: HTHost_enable_PendingReqLaunch ();
2103:
2104: cur = Amaya->reqlist;
1.7 cvs 2105: while ((me = (AHTReqContext *) HTList_nextObject (cur)))
2106: {
2107: if (me->docid == docid)
2108: {
1.93 cvs 2109: /* kill this request */
1.85 cvs 2110:
1.93 cvs 2111: switch (me->reqStatus)
2112: {
2113: case HT_ABORT:
1.108 cvs 2114: #ifdef DEBUG_LIBWWW
2115: fprintf (stderr, "Stop: url %s says abort", me->urlName);
2116: #endif /* DEBUG_LIBWWW */
1.93 cvs 2117: break;
1.100 cvs 2118:
2119: case HT_END:
1.108 cvs 2120: #ifdef DEBUG_LIBWWW
2121: fprintf (stderr, "Stop: url %s says end", me->urlName);
2122: #endif /* DEBUG_LIBWWW */
1.100 cvs 2123: break;
2124:
1.93 cvs 2125: case HT_BUSY:
2126: me->reqStatus = HT_ABORT;
1.108 cvs 2127: #ifdef DEBUG_LIBWWW
2128: fprintf (stderr, "Stop: url %s going from busy to abort\n", me->urlName);
2129: #endif /* DEBUG_LIBWWW */
1.93 cvs 2130: break;
1.96 cvs 2131:
1.93 cvs 2132: case HT_NEW_PENDING:
2133: case HT_WAITING:
2134: default:
1.108 cvs 2135: #ifdef DEBUG_LIBWWW
2136: fprintf (stderr, "Stop: url %s says NEW_PENDING, WAITING", me->urlName);
2137: #endif /* DEBUG_LIBWWW */
1.93 cvs 2138: me->reqStatus = HT_ABORT;
2139:
1.70 cvs 2140: # ifndef _WINDOWS
1.93 cvs 2141: RequestKillAllXtevents (me);
1.74 cvs 2142: # endif _WINDOWS
1.96 cvs 2143:
1.93 cvs 2144: reqNet = HTRequest_net (me->request);
2145: reqSock = HTNet_socket (reqNet);
2146: reqChannel = HTChannel_find(reqSock);
2147: reqHost = HTChannel_host (reqChannel);
1.96 cvs 2148:
1.108 cvs 2149: if ((me->mode & AMAYA_IASYNC)
2150: || (me->mode & AMAYA_ASYNC)
2151: && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
2152: {
1.96 cvs 2153: if (HTRequest_net (me->request))
1.108 cvs 2154: /* delete the libwww request context */
1.96 cvs 2155: HTRequest_kill (me->request);
2156: #ifndef _WINDOWS
1.108 cvs 2157: /* delete the Amaya request context */
2158: AHTReqContext_delete (me);
2159: #endif /* !_WINDOWS */
1.96 cvs 2160: cur = Amaya->reqlist;
1.108 cvs 2161: #ifdef DEBUG_LIBWWW
2162: /* update the number of open requests */
2163: open_requests--;
2164: #endif /* DEBUG_LIBWWW */
2165: }
1.95 cvs 2166:
1.93 cvs 2167: /* if there are no more requests, then close
2168: the connection */
2169:
2170: if (HTHost_isIdle (reqHost) ) {
1.85 cvs 2171: #ifdef DEBUG_LIBWWW
1.93 cvs 2172: fprintf (stderr, "Host is idle, "
2173: "killing socket %d\n",
2174: reqSock);
1.85 cvs 2175: #endif /* DEBUG_LIBWWW */
1.93 cvs 2176: HTEvent_unregister (reqSock, FD_ALL);
2177: HTEvent_register(reqSock,
2178: NULL,
2179: (SockOps) FD_READ,
2180: HTHost_catchClose,
2181: HT_PRIORITY_MAX);
1.108 cvs 2182: NETCLOSE (reqSock);
1.93 cvs 2183: HTHost_clearChannel (reqHost, HT_ERROR);
2184: /*
2185: if (reqChannel && reqHost)
2186: HTHost_clearChannel(reqHost, HT_OK);
2187: HTHost_catchClose (reqSock, NULL, FD_CLOSE);
2188: */
1.85 cvs 2189: #ifdef DEBUG_LIBWWW
1.93 cvs 2190: fprintf (stderr, "After killing, "
2191: "HTHost_isIdle gives %d\n",
2192: HTHost_isIdle (reqHost));
1.85 cvs 2193: #endif /* DEBUG_LIBWWW */
1.93 cvs 2194: }
1.108 cvs 2195: #ifdef DEBUG_LIBWWW
1.93 cvs 2196: open_requests--;
1.108 cvs 2197: #endif /* DEBUG_LIBWWW */
1.93 cvs 2198: break;
2199:
2200: } /* switch */
1.7 cvs 2201: } /* if me docid */
2202: } /* while */
1.100 cvs 2203:
2204: #ifdef DEBUG_LIBWWW
2205: fprintf (stderr, "StopRequest: number of Amaya requests : %d\n", Amaya->open_requests);
2206: #endif /* DEBUG_LIBWWW */
2207:
1.7 cvs 2208: } /* if amaya open requests */
1.93 cvs 2209:
1.85 cvs 2210: } /* StopRequest */
1.17 cvs 2211:
2212: /*
2213: end of Module query.c
2214: */
2215:
1.105 cvs 2216: /*----------------------------------------------------------------------
2217: AmayaIsAlive
2218: returns the status of the AmayaAlive flag
2219: ----------------------------------------------------------------------*/
2220: #ifdef __STDC__
2221: boolean AmayaIsAlive (void)
2222: #else
2223: boolean AmayaIsAlive ()
2224: #endif /* _STDC_ */
2225: {
2226: return AmayaAlive;
2227: }
1.69 cvs 2228: #endif /* AMAYA_JAVA */
1.77 cvs 2229:
2230:
1.17 cvs 2231:
2232:
Webmaster