Annotation of Amaya/amaya/query.c, revision 1.15
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.12 cvs 8: /* Amaya includes */
1.15 ! cvs 9: #define EXPORT extern
1.4 cvs 10: #include "amaya.h"
1.7 cvs 11:
1.4 cvs 12: #if defined(__svr4__)
13: #define CATCH_SIG
14: #endif
15:
16: /* local structures coming from libwww and which are
1.7 cvs 17: ** not found in any .h file
18: */
1.4 cvs 19:
1.7 cvs 20: struct _HTStream
21: {
1.15 ! cvs 22: const HTStreamClass *isa;
! 23: FILE *fp;
! 24: BOOL leave_open; /* Close file when HT_FREE? */
! 25: char *end_command; /* Command to execute */
! 26: BOOL remove_on_close; /* Remove file? */
! 27: char *filename; /* Name of file */
! 28: HTRequest *request; /* saved for callback */
! 29: HTRequestCallback *callback;
1.7 cvs 30: };
31:
1.15 ! cvs 32:
1.7 cvs 33: struct _HTError
34: {
35: HTErrorElement element; /* Index number into HTError */
36: HTSeverity severity; /* A la VMS */
37: BOOL ignore; /* YES if msg should not go to user */
38: void *par; /* Explanation, e.g. filename */
39: int length; /* For copying by generic routine */
40: char *where; /* Which function */
41: };
42:
43:
44: struct _HTHost
45: {
46: char *hostname; /* name of host + optional port */
47: time_t ntime; /* Creation time */
48: char *type; /* Peer type */
49: int version; /* Peer version */
50: HTMethod methods; /* Public methods (bit-flag) */
51: char *server; /* Server name */
52: char *user_agent; /* User Agent */
53: char *range_units; /* Acceptable range units */
54: HTTransportMode mode; /* Supported mode */
55: HTChannel *channel; /* Persistent channel */
56: HTList *pipeline; /* Pipe line of net objects */
57: HTList *pending; /* List of pending Net objects */
58: time_t expires; /* Persistent channel expires time */
59: };
1.4 cvs 60:
61: /* Type definitions and global variables etc. local to this module */
62:
63: /*** private variables ***/
64: static HTList *converters = NULL; /* List of global converters */
65: static HTList *encodings = NULL;
66:
1.15 ! cvs 67: #include "answer_f.h"
! 68: #include "query_f.h"
! 69: #include "AHTURLTools_f.h"
! 70: #include "AHTBridge_f.h"
! 71: #include "AHTMemConv_f.h"
! 72: #include "AHTFWrite_f.h"
1.4 cvs 73:
74: #ifdef CATCH_SIG
75:
1.5 cvs 76: /*----------------------------------------------------------------------
77: SetSignal: This function sets up signal handlers. This might not
78: be necessary to call if the application has its own handlers.
79: ----------------------------------------------------------------------*/
1.4 cvs 80: #ifdef __STDC__
81: static void SetSignal (void)
82: #else
83: static void SetSignal ()
84: #endif
85: {
86: /* On some systems (SYSV) it is necessary to catch the SIGPIPE signal
87: ** when attemting to connect to a remote host where you normally should
88: ** get `connection refused' back
89: */
1.7 cvs 90: if (signal (SIGPIPE, SIG_IGN) == SIG_ERR)
91: {
92: if (PROT_TRACE)
93: HTTrace ("HTSignal.... Can't catch SIGPIPE\n");
94: }
95: else
96: {
97: if (PROT_TRACE)
98: HTTrace ("HTSignal.... Ignoring SIGPIPE\n");
99: }
1.4 cvs 100: }
101: #endif /* CATCH_SIG */
102:
1.15 ! cvs 103:
! 104: /*----------------------------------------------------------------------
! 105: Gets the status associated to a docid
! 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);
! 127: } /* while */
! 128: } /* if */
! 129: return (AHTDocId_Status *) NULL;
! 130:
! 131: }
! 132:
1.5 cvs 133: /*----------------------------------------------------------------------
134: Create a new Amaya Context Object
135: ----------------------------------------------------------------------*/
1.4 cvs 136: #ifdef __STDC__
137: static AHTReqContext *AHTReqContext_new (int docid)
138: #else
139: static AHTReqContext *AHTReqContext_new (docid)
140: int docid;
141:
142: #endif
143: {
144: AHTReqContext *me;
145: AHTDocId_Status *docid_status;
146:
147: if ((me = (AHTReqContext *) TtaGetMemory (sizeof (AHTReqContext))) == NULL)
148: outofmem (__FILE__, "Context_new");
149:
150: /* Bind the Context object together with the Request Object */
151:
152: me->request = HTRequest_new ();
153:
154: /*** This is for correcting the keep-alive bug. Is this interfering with */
155: /**** libwww v5? */
156:
157: /* test
158: me->request->GenMask &= ~((int) HT_G_CONNECTION);
1.7 cvs 159: */
1.4 cvs 160:
161: /* Initialize the other members of the structure */
162: me->reqStatus = HT_NEW;
163: me->output = NULL;
164: #ifdef WWW_XWINDOWS
165: me->read_xtinput_id = (XtInputId) NULL;
166: me->write_xtinput_id = (XtInputId) NULL;
167: me->except_xtinput_id = (XtInputId) NULL;
168: #endif
169: me->docid = docid;
170: HTRequest_setConversion (me->request, converters, YES);
171: HTRequest_setMethod (me->request, METHOD_GET);
172: HTRequest_setOutputFormat (me->request, WWW_SOURCE);
173: HTRequest_setContext (me->request, me);
174:
175: /* to interface with Eric's new routines */
176: me->read_ops = 0;
177: me->write_ops = 0;
178: me->except_ops = 0;
179:
180: /* Update the global context */
181: HTList_appendObject (Amaya->reqlist, (void *) me);
182:
183: docid_status = GetDocIdStatus (docid, Amaya->docid_status);
184:
1.7 cvs 185: if (docid_status == NULL)
186: {
187: docid_status = (AHTDocId_Status *) TtaGetMemory (sizeof (AHTDocId_Status));
188: docid_status->docid = docid;
189: docid_status->counter = 1;
190: HTList_addObject (Amaya->docid_status, (void *) docid_status);
191: }
192: else
1.4 cvs 193: docid_status->counter++;
194:
195:
196: Amaya->open_requests++;
197:
198: /* error stream handling */
199: me->error_stream = (char *) NULL;
200: me->error_stream_size = 0;
201:
202: return me;
203: }
204:
1.5 cvs 205: /*----------------------------------------------------------------------
206: Delete an Amaya Context Object
207: ----------------------------------------------------------------------*/
1.4 cvs 208:
209: #ifdef __STDC__
1.15 ! cvs 210: boolean AHTReqContext_delete (AHTReqContext * me)
1.4 cvs 211: #else
1.15 ! cvs 212: boolean AHTReqContext_delete (me)
1.4 cvs 213: AHTReqContext *me;
214:
215: #endif
216: {
217: AHTDocId_Status *docid_status;
218:
1.7 cvs 219: if (me)
220: {
1.4 cvs 221:
1.7 cvs 222: if (Amaya->reqlist)
223: HTList_removeObject (Amaya->reqlist, (void *) me);
1.4 cvs 224:
1.7 cvs 225: docid_status = GetDocIdStatus (me->docid, Amaya->docid_status);
1.4 cvs 226:
1.7 cvs 227: if (docid_status)
228: {
229: docid_status->counter--;
230:
231: if (docid_status->counter == 0)
232: {
233: HTList_removeObject (Amaya->docid_status, (void *) docid_status);
234: TtaFreeMemory ((void *) docid_status);
235: }
236: }
237: HTRequest_delete (me->request);
1.4 cvs 238:
1.7 cvs 239: if (me->error_stream != (char *) NULL)
240: HT_FREE (me->error_stream);
1.4 cvs 241:
1.7 cvs 242: TtaFreeMemory ((void *) me);
1.4 cvs 243:
1.7 cvs 244: Amaya->open_requests--;
1.4 cvs 245:
1.15 ! cvs 246: return TRUE;
1.4 cvs 247:
1.7 cvs 248: }
1.15 ! cvs 249: return FALSE;
1.4 cvs 250: }
251:
252:
1.15 ! cvs 253: /*----------------------------------------------------------------------
! 254: AHTUpload_callback
! 255: ----------------------------------------------------------------------*/
1.4 cvs 256: #ifdef __STDC__
257: static int AHTUpload_callback (HTRequest * request, HTStream * target)
258: #else
259: static int AHTUpload_callback (request, target)
260: HTRequest *request;
261: HTStream *target;
262:
263: #endif
264: {
265: AHTReqContext *me = HTRequest_context (request);
1.7 cvs 266: HTParentAnchor *entity = HTRequest_entityAnchor (request);
267: int len = HTAnchor_length (entity);
1.4 cvs 268: int status;
269:
270: /* Send the data down the pipe */
1.7 cvs 271:
1.4 cvs 272: status = (*target->isa->put_block) (target, me->mem_ptr, len);
273:
1.7 cvs 274: if (status == HT_LOADED || status == HT_OK)
275: {
1.13 cvs 276: if (PROT_TRACE)
277: HTTrace ("Posting Data Target is SAVED\n");
278: (*target->isa->flush) (target);
279: return (HT_LOADED);
1.7 cvs 280: }
281: if (status == HT_WOULD_BLOCK)
282: {
283: if (PROT_TRACE)
284: HTTrace ("Posting Data Target WOULD BLOCK\n");
285: return HT_WOULD_BLOCK;
286: }
287: else if (status == HT_PAUSE)
288: {
289: if (PROT_TRACE)
290: HTTrace ("Posting Data Target PAUSED\n");
291: return HT_PAUSE;
292: }
293: else if (status > 0)
294: { /* Stream specific return code */
295: if (PROT_TRACE)
296: HTTrace ("Posting Data. Target returns %d\n", status);
297: return status;
298: }
299: else
300: { /* we have a real error */
301: if (PROT_TRACE)
302: HTTrace ("Posting Data Target ERROR %d\n", status);
303: return status;
304: }
1.4 cvs 305: }
306:
1.5 cvs 307: /*----------------------------------------------------------------------
308: This function deletes the whole list of active threads.
309: ----------------------------------------------------------------------*/
1.4 cvs 310: #ifdef __STDC__
311: static void Thread_deleteAll (void)
312: #else
313: static void Thread_deleteAll ()
314: #endif
315: {
1.7 cvs 316: if (Amaya && Amaya->reqlist)
317: {
318: if (Amaya->open_requests > 0)
319: {
320: HTList *cur = Amaya->reqlist;
321: AHTReqContext *me;
322: AHTDocId_Status *docid_status;
323:
324: HTNet_killAll ();
325: /* erase the requests */
326: while ((me = (AHTReqContext *) HTList_nextObject (cur)))
327: {
328: if (me->request)
329: {
1.4 cvs 330: #ifdef WWW_XWINDOWS
1.7 cvs 331: RequestKillAllXtevents (me);
1.4 cvs 332: #endif /* WWW_XWINDOWS */
1.7 cvs 333: /* HTRequest_kill(me->request);
334: if(me->output)
335: fclose(me->output);
336: */
337: AHTReqContext_delete (me);
338: }
339: } /* while */
340: HTList_delete (Amaya->reqlist);
341: /* erase the docid_status entities */
342: while ((docid_status = (AHTDocId_Status *) HTList_removeLastObject ((void *) Amaya->docid_status)))
343: TtaFreeMemory ((void *) docid_status);
344:
345: HTList_delete (Amaya->reqlist);
346: } /* if */
347: TtaFreeMemory ((void *) Amaya);
348: }
1.4 cvs 349: }
350:
1.5 cvs 351: /*----------------------------------------------------------------------
352: redirection_handler
1.15 ! cvs 353: This function is registered to handle permanent and temporary
! 354: redirections.
! 355: ----------------------------------------------------------------------*/
1.4 cvs 356: #ifdef __STDC__
1.7 cvs 357: static int redirection_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4 cvs 358: #else
359: static int redirection_handler (request, context, status)
360: HTRequest *request;
361: HTResponse *response;
362: void *param;
363: int status;
364:
365: #endif
366: {
367:
368: char *ref;
369: HTAnchor *new_anchor = HTResponse_redirection (response);
1.7 cvs 370: AHTReqContext *me = HTRequest_context (request);
1.4 cvs 371: HTMethod method = HTRequest_method (request);
372:
373:
1.7 cvs 374: if (!new_anchor)
375: {
376: if (PROT_TRACE)
377: HTTrace ("Redirection. No destination\n");
378: return HT_OK;
379: }
1.4 cvs 380:
381: /*
382: ** Only do redirect on GET and HEAD
383: */
1.7 cvs 384: if (!HTMethod_isSafe (method))
385: {
386: HTAlertCallback *prompt = HTAlert_find (HT_A_CONFIRM);
387:
388: if (prompt)
389: {
390: if ((*prompt) (request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
391: NULL, NULL, NULL) != YES)
392: return HT_ERROR;
393: }
394: }
1.4 cvs 395:
396: /*
397: ** Start new request with the redirect anchor found in the headers.
398: ** Note that we reuse the same request object which means that we must
399: ** keep this around until the redirected request has terminated. It also
400: ** allows us in an easy way to keep track of the number of redirections
401: ** so that we can detect endless loops.
402: */
1.7 cvs 403: if (HTRequest_doRetry (request))
404: {
405:
406: /* Verify if this is not redundant */
407:
408: /* do we need to normalize the URL? */
409: if (strncmp (new_anchor->parent->address, "http:", 5))
410: {
411: /* Yes, so we use the pre-redirection anchor as a base name */
412: ref = HTParse (new_anchor->parent->address, me->urlName, PARSE_ALL);
413: if (ref)
414: {
415: HT_FREE (new_anchor->parent->address);
416: new_anchor->parent->address = ref;
417: }
418: }
419:
420: /* update the current file name */
421: if (strlen (new_anchor->parent->address) > (MAX_LENGTH - 1))
422: {
423: /*
424: ** copy MAX_LENGTH cars. The error will be detected later on and shown on the
425: ** screen. This code will be fixed up later on
426: */
427: strncpy (me->urlName, new_anchor->parent->address, MAX_LENGTH);
428: me->urlName[MAX_LENGTH] = '\0';
429: }
430: else
431: strcpy (me->urlName, new_anchor->parent->address);
432:
433: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_RED_FETCHING),
434: me->urlName);
435:
436: /* Start request with new credentials */
1.4 cvs 437:
1.7 cvs 438: me->reqStatus = HT_NEW;
439: if (me->method == METHOD_PUT || me->method == METHOD_POST) /* PUT, POST etc. */
440: /* HTCopyAnchor((HTAnchor *) HTRequest_anchor(request), request); */
441: status = HTLoadAbsolute (me->urlName, request);
442: else
443: HTLoadAnchor (new_anchor, request);
444: }
445: else
446: {
447: HTRequest_addError (request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
448: NULL, 0, "HTRedirectFilter");
449: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REDIRECTIONS_LIMIT),
450: NULL);
451: if (me->error_html)
452: FilesLoading[me->docid] = 2; /* so we can show the error message */
453: }
1.4 cvs 454:
455: /*
1.7 cvs 456: ** By returning HT_ERROR we make sure that this is the last handler to be
457: ** called. We do this as we don't want any other filter to delete the
458: ** request object now when we have just started a new one ourselves
459: */
1.4 cvs 460: return HT_ERROR;
461: }
462:
1.5 cvs 463: /*----------------------------------------------------------------------
464: terminate_handler
1.4 cvs 465: ** This function is registered to handle the result of the request
1.5 cvs 466:
467: ----------------------------------------------------------------------*/
1.4 cvs 468: #if __STDC__
1.7 cvs 469: static int terminate_handler (HTRequest * request, HTResponse * response, void *context, int status)
1.4 cvs 470: #else
471: static int terminate_handler (request, response, context, status)
472: HTRequest *request;
473: HTResponse *response;
474: void *context;
475: int status;
476:
477: #endif
478: {
479: AHTReqContext *me = (AHTReqContext *) HTRequest_context (request);
1.13 cvs 480: boolean error_flag;
1.4 cvs 481:
482: if (!me)
483: return HT_OK; /* not an Amaya request */
484:
1.13 cvs 485: if (status == HT_LOADED || status == HT_CREATED || status == HT_NO_DATA)
486: error_flag = FALSE;
487: else
488: error_flag = TRUE;
489:
1.4 cvs 490: /* output any errors from the server */
491:
1.5 cvs 492: /***
1.4 cvs 493: ** me->output = output file which will receive an html file
494: ** me->error_html = yes, output HTML errors in the screen
495: ** request->error_stack == if there are any errors, they will be here
496: ** me->error_stream_size If it's != 0 means an error message has already
497: ** been written to the stack
498: */
499:
500: /* First, we verify if there are any errors and if they are not
501: ** yet written to the error stack. If no, then let's try to write them
502: ** ourselves
503: */
504:
1.7 cvs 505: if (me->output && me->output != stdout)
506: {
507: /* we are writing to a file */
508: if (me->reqStatus != HT_ABORT)
509: { /* if the request was not aborted and */
1.13 cvs 510: if (error_flag)
511: { /* there were some errors */
1.15 ! cvs 512: if (me->error_html == TRUE)
1.13 cvs 513: { /* and we want to print errors */
514: if (me->error_stream_size == 0) /* and the stream is empty */
1.7 cvs 515: AHTError_MemPrint (request); /* copy errors from the error stack
516: ** into a data structure */
517: if (me->error_stream)
518: { /* if the stream is non-empty */
519: fprintf (me->output, me->error_stream); /* output the errors */
1.13 cvs 520: error_flag = FALSE; /* show it in the HTML window */
1.7 cvs 521: }
522: else
523: me->reqStatus = HT_ERR; /* we did not get an error msg,
524: ** so just
525: ** mark error
526: */
527: }
528: else
529: me->reqStatus = HT_ERR; /* we don't want to print the error */
530: } /* if error_stack */
1.13 cvs 531: } /* if != HT_ABORT */
1.7 cvs 532: fclose (me->output);
533: }
534: else
535: {
536: /* We must be doing a PUT. Verify if there was an error */
1.13 cvs 537: if (error_flag)
538: me->reqStatus = HT_ERR;
1.7 cvs 539: } /* if me-output */
540:
1.4 cvs 541: /* setup the request status and invoke the cbf */
1.7 cvs 542:
1.4 cvs 543: /* work to be done: verify if we can put join all the terminate cbf in
544: only one call after the following lines */
1.7 cvs 545:
1.13 cvs 546: if (!error_flag && me->reqStatus != HT_ERR
1.7 cvs 547: && me->reqStatus != HT_ABORT)
548: {
549: me->reqStatus = HT_END; /* no errors */
550: if (me->terminate_cbf)
551: (*me->terminate_cbf) ((AHTReqContext *) me,
552: HT_LOADED);
553: }
554: else if (me->reqStatus == HT_ABORT)
555: {
556: if (me->terminate_cbf)
557: (*me->terminate_cbf) ((AHTReqContext *) me,
558: HT_ERROR);
559: if (me->outputfile && me->outputfile[0] != EOS)
560: {
1.9 cvs 561: TtaFileUnlink (me->outputfile);
1.7 cvs 562: me->outputfile[0] = EOS;
563: }
564: }
565: else if (me->reqStatus == HT_ERR)
566: {
567: /* there was an error */
568: if (me->terminate_cbf)
569: (*me->terminate_cbf) ((AHTReqContext *) me,
570: HT_ERROR);
571:
572: if (me->outputfile && me->outputfile[0] != EOS)
573: {
1.9 cvs 574: TtaFileUnlink (me->outputfile);
1.7 cvs 575: me->outputfile[0] = EOS;
576: }
1.4 cvs 577: }
1.13 cvs 578: else if (error_flag &&
1.7 cvs 579: (me->reqStatus == HT_BUSY || me->reqStatus == HT_WAITING))
580: {
581: /* there was an error */
582: if (me->terminate_cbf)
583: (*me->terminate_cbf) ((AHTReqContext *) me,
584: HT_ERROR);
585:
586: if (me->outputfile && me->outputfile[0] != EOS)
587: {
1.9 cvs 588: TtaFileUnlink (me->outputfile);
1.7 cvs 589: me->outputfile[0] = EOS;
590: me->reqStatus = HT_ERR;
591: }
592: } /* if-else HT_END, HT_ABORT, HT_ERROR */
1.4 cvs 593: if (HTLog_isOpen ())
1.7 cvs 594: HTLog_add (request, status);
1.4 cvs 595:
1.7 cvs 596: if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC))
597: {
598: TtaFreeMemory (me->urlName);
599: TtaFreeMemory (me->outputfile);
600: /* AHTReqContext_delete(me); */
601: }
1.4 cvs 602: /* don't remove or Xt will hang up during the put */
603:
1.7 cvs 604: if (me->method == METHOD_PUT || me->method == METHOD_POST)
605: {
606: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_PROG_WRITE),
607: me->urlName);
1.4 cvs 608:
1.7 cvs 609: }
1.4 cvs 610: return HT_OK;
611: }
612:
1.5 cvs 613: /*----------------------------------------------------------------------
614: Application "AFTER" Callback
615: This function uses all the functionaly that the app part of
616: the Library gives for handling AFTER a request.
617: ----------------------------------------------------------------------*/
1.4 cvs 618:
619: #ifdef __STDC__
1.7 cvs 620: static int AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4 cvs 621: #else
622: static int AHTLoadTerminate_handler (request, response, param, status)
623: HTRequest *request;
624: HTResponse *response;
625: void *param;
626: int status;
627:
628: #endif
629: {
630: char *uri = HTAnchor_address ((HTAnchor *) request->anchor);
631: AHTReqContext *me = HTRequest_context (request);
632: HTAlertCallback *cbf;
633: AHTDocId_Status *docid_status;
634:
1.7 cvs 635: switch (status)
636: {
637: case HT_LOADED:
638: if (PROT_TRACE)
639: HTTrace ("Load End.... OK: `%s\' has been accessed\n",
1.4 cvs 640: uri);
641:
1.7 cvs 642: docid_status = GetDocIdStatus (me->docid,
643: Amaya->docid_status);
644:
645: if (docid_status != NULL && docid_status->counter > 1)
646: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA,
647: AM_ELEMENT_LOADED), uri);
648:
649: break;
650:
651: case HT_NO_DATA:
652: if (PROT_TRACE)
653: HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n", uri);
654: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
655: uri);
656: break;
657:
658: case HT_INTERRUPTED:
659: if (PROT_TRACE)
660: HTTrace ("Load End.... INTERRUPTED: `%s\'\n", uri);
661: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_LOAD_ABORT), NULL);
662:
663: break;
664:
665: case HT_RETRY:
666: if (PROT_TRACE)
667: HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
668: HTResponse_retryTime (response));
669: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
670: uri);
671: break;
672:
673: case HT_ERROR:
674:
675: cbf = HTAlert_find (HT_A_MESSAGE);
676: if (cbf)
677: (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
678: HTRequest_error (request), NULL);
679: break;
680:
681: if (PROT_TRACE)
682: HTTrace ("Load End.... ERROR: Can't access `%s\'\n",
683: uri ? uri : "<UNKNOWN>");
684: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
685: uri ? uri : "<UNKNOWN>");
686: break;
687: default:
688: if (PROT_TRACE)
689: HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
690: break;
691: }
692:
1.4 cvs 693: /* Should we do logging? */
694: if (HTLog_isOpen ())
1.7 cvs 695: HTLog_add (request, status);
1.4 cvs 696: HT_FREE (uri);
1.7 cvs 697:
1.4 cvs 698: return HT_OK;
699: }
700:
701: /*
702: ** After filter for handling PUT of document. We should now have the
703: */
1.7 cvs 704: static int AHTSaveFilter (HTRequest * request, HTResponse * response, void *param, int status)
1.4 cvs 705: {
706: AHTReqContext *me = HTRequest_context (request);
707:
708: if (APP_TRACE)
1.7 cvs 709: HTTrace ("Save Filter\n");
1.4 cvs 710:
711: /*
1.7 cvs 712: ** Just ignore authentication in the hope that some other filter will
713: ** handle this.
1.4 cvs 714: */
1.7 cvs 715:
716: if (status == HT_NO_ACCESS || status == HT_NO_PROXY_ACCESS)
717: {
718: if (APP_TRACE)
719: HTTrace ("Save Filter. Waiting for authentication\n");
720: status = HT_OK;
721: }
722: else if (status == HT_TEMP_REDIRECT || status == HT_PERM_REDIRECT)
723: {
1.4 cvs 724: /*
1.7 cvs 725: ** The destination entity has been redirected. Ask the user
726: ** what to do. If there is no user confirmation to go ahead then stop
727: */
728: /* put here the equiv. code of redirection handler, and a prompt
729: for user */
730: HTAlertCallback *prompt = HTAlert_find (HT_A_CONFIRM);
731: HTAnchor *redirection = HTResponse_redirection (response);
732:
733: if (prompt && redirection)
734: {
735: if ((*prompt) (request, HT_A_CONFIRM, HT_MSG_SOURCE_MOVED,
736: NULL, NULL, NULL) == YES)
737: {
738: return HT_OK;
739: }
740: else
741: {
742: /*
743: ** Make sure that the operation stops
744: */
745: me->reqStatus = HT_ABORT;
1.4 cvs 746:
1.7 cvs 747: /* add a message? */
1.4 cvs 748:
1.7 cvs 749: status = HT_ERROR;
750: }
751: }
752: status = HT_OK;
753: }
754: else if (status == HT_ERROR)
755: {
756: if (me->reqStatus != HT_ABORT && me->reqStatus != HT_END)
757: me->reqStatus = HT_ERR;
758: }
759: else
760: {
761: /*
762: ** No authentication or redirection was detected ... let's erase this filter
763: **
764: */
765: HTRequest_deleteAfter (request, AHTSaveFilter);
1.4 cvs 766: status = HT_OK;
1.7 cvs 767: }
1.4 cvs 768:
1.7 cvs 769: return status;
1.4 cvs 770:
771: }
772:
773:
1.5 cvs 774: /*----------------------------------------------------------------------
775: BINDINGS BETWEEN A SOURCE MEDIA TYPE AND A DEST MEDIA TYPE (CONVERSION)
776: ----------------------------------------------------------------------*/
1.15 ! cvs 777: #ifdef __STDC__
! 778: static void AHTConverterInit (HTList *c)
! 779: #else /* __STDC__ */
! 780: static void AHTConverterInit (c)
! 781: HTList *c;
! 782: #endif /* __STDC__ */
1.4 cvs 783: {
784:
785: /* Handler for custom http error messages */
1.7 cvs 786: HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4 cvs 787:
788: /*
789: ** These are converters that converts to something other than www/present,
790: ** that is not directly outputting someting to the user on the screen
791: */
792:
793: HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 1.0, 0.0, 0.0);
794: HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter,
795: 1.0, 0.0, 0.0);
796: HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader,
797: 1.0, 0.0, 0.0);
798: HTConversion_add (c, "multipart/*", "*/*", HTBoundary,
799: 1.0, 0.0, 0.0);
800: HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML,
801: 1.0, 0.0, 0.0);
802:
803:
804: /*
805: ** The following conversions are converting ASCII output from various
806: ** protocols to HTML objects.
807: */
808: HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new,
809: 1.0, 0.0, 0.0);
810: HTConversion_add (c, "text/x-nntp-list", "*/*", HTNewsList,
811: 1.0, 0.0, 0.0);
812: HTConversion_add (c, "text/x-nntp-over", "*/*", HTNewsGroup,
813: 1.0, 0.0, 0.0);
814:
815:
816: /*
817: ** We also register a special content type guess stream that can figure out
818: ** the content type by reading the first bytes of the stream
819: */
820: HTConversion_add (c, "www/unknown", "*/*", HTGuess_new,
821: 1.0, 0.0, 0.0);
822:
823: /*
824: ** Register a persistent cache stream which can save an object to local
825: ** file
826: */
827: HTConversion_add (c, "www/cache", "*/*", HTCacheWriter,
828: 1.0, 0.0, 0.0);
829:
830: /*
831: ** This dumps all other formats to local disk without any further
832: ** action taken
833: */
834: HTConversion_add (c, "*/*", "www/present", HTSaveLocally,
835: 0.3, 0.0, 0.0);
836:
837: }
838:
1.15 ! cvs 839: /*----------------------------------------------------------------------
! 840: REGISTER BEFORE AND AFTER FILTERS
! 841: We register a commonly used set of BEFORE and AFTER filters.
! 842: Not done automaticly - may be done by application!
! 843: REGISTER ALL AMAYA SUPPORTED PROTOCOLS
! 844: ----------------------------------------------------------------------*/
1.4 cvs 845: static void AHTProtocolInit (void)
846: {
847:
848: /* NB. Preemptive == YES = Blocking request
849: ** Non-preemptive == NO = Non-blocking request
850: */
851:
852: HTProtocol_add ("http", "buffered_tcp", NO, HTLoadHTTP, NULL);
853: /* HTProtocol_add ("http", "tcp", NO, HTLoadHTTP, NULL); */
854: HTProtocol_add ("file", "local", NO, HTLoadFile, NULL);
855: HTProtocol_add ("cache", "local", NO, HTLoadCache, NULL);
856: HTProtocol_add ("telnet", "", YES, HTLoadTelnet, NULL);
857: HTProtocol_add ("tn3270", "", YES, HTLoadTelnet, NULL);
858: HTProtocol_add ("rlogin", "", YES, HTLoadTelnet, NULL);
859:
860: HTProtocol_add ("ftp", "tcp", NO, HTLoadFTP, NULL);
861: HTProtocol_add ("nntp", "tcp", NO, HTLoadNews, NULL);
862: HTProtocol_add ("news", "tcp", NO, HTLoadNews, NULL);
863: }
864:
1.15 ! cvs 865: /*----------------------------------------------------------------------
! 866: ----------------------------------------------------------------------*/
1.4 cvs 867: static void AHTNetInit (void)
868: {
869:
870: /* Register BEFORE filters
871: ** The BEFORE filters handle proxies, caches, rule files etc.
872: ** The filters are called in the order by which the are registered
873: ** Not done automaticly - may be done by application!
874: */
875:
876:
1.7 cvs 877: /*#ifndef HACK_WWW */
878: HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, 6);
879: HTNet_addBefore (HTProxyFilter, NULL, NULL, 10);
1.4 cvs 880:
1.7 cvs 881: /*#endif */
1.4 cvs 882:
883: /* register AFTER filters
884: ** The AFTER filters handle error messages, logging, redirection,
885: ** authentication etc.
886: ** The filters are called in the order by which the are registered
887: ** Not done automaticly - may be done by application!
888: */
889:
890: #ifndef HACK_WWW
891:
1.7 cvs 892: HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS,
893: 5);
1.4 cvs 894:
1.7 cvs 895: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT,
896: 5);
897: HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT,
898: 5);
899: HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY,
900: 5);
1.4 cvs 901:
1.7 cvs 902: HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* handles all errors */
903: HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4 cvs 904: #endif
905: }
906:
1.15 ! cvs 907: /* REGISTER CALLBACKS FOR THE ALERT MANAGER
! 908: ** We register a set of alert messages
! 909: */
! 910:
! 911: /*----------------------------------------------------------------------
! 912: ----------------------------------------------------------------------*/
! 913: #ifdef __STDC__
! 914: static void AHTAlertInit (void)
! 915: #else
! 916: static void AHTAlertInit ()
! 917: #endif
! 918: {
! 919:
! 920: HTAlert_add (AHTProgress, HT_A_PROGRESS);
! 921: HTAlert_add ((HTAlertCallback *) Add_NewSocket_to_Loop, HT_PROG_CONNECT);
! 922: HTAlert_add (AHTError_print, HT_A_MESSAGE);
! 923: HTError_setShow (0xFF); /* process all messages */
! 924: /* HTAlert_add (HTError_print, HT_A_MESSAGE); */
! 925: HTAlert_add (AHTConfirm, HT_A_CONFIRM);
! 926: HTAlert_add (AHTPrompt, HT_A_PROMPT);
! 927: HTAlert_add (AHTPromptPassword, HT_A_SECRET);
! 928: HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
! 929: /* Setup a global call to input XtEvents for new sockets */
! 930: /* should not be necessary anymore */
! 931:
! 932: }
! 933:
! 934: /*----------------------------------------------------------------------
! 935: ----------------------------------------------------------------------*/
! 936: #ifdef __STDC__
! 937: static void AHTProfile_newAmaya (char *AppName, char *AppVersion)
! 938: #else /* __STDC__ */
! 939: static void AHTProfile_newAmaya (AppName, AppVersion)
! 940: char *AppName;
! 941: char *AppVersion;
! 942: #endif /* __STDC__ */
1.4 cvs 943: {
944: /* If the Library is not already initialized then do it */
945: if (!HTLib_isInitialized ())
946: HTLibInit (AppName, AppVersion);
947:
948: if (!converters)
949: converters = HTList_new ();
950: if (!encodings)
951: encodings = HTList_new ();
952:
953: /* Register the default set of transport protocols */
954: HTTransportInit ();
955:
956: /* Register the default set of application protocol modules */
957: #ifndef HACK_WWW
958: AHTProtocolInit ();
959: #endif
960:
961: /* Enable the persistent cache */
962: /* HTCacheInit (NULL, 20); */
963:
964: /* Register the default set of BEFORE and AFTER filters */
965: AHTNetInit ();
966:
967: /* Set up the default set of Authentication schemes */
968: HTAAInit ();
969:
970: /* Get any proxy or gateway environment variables */
971: HTProxy_getEnvVar ();
972:
973: /* Register the default set of converters */
974: AHTConverterInit (converters);
975: HTFormat_setConversion (converters);
976:
977: /* Register the default set of transfer encoders and decoders */
978: HTEncoderInit (encodings); /* chunks ??? */
979: HTFormat_setTransferCoding (encodings);
980:
981: /* Register the default set of MIME header parsers */
982: HTMIMEInit (); /* must be called again for language selector */
983:
984: /* Register the default set of file suffix bindings */
985: HTFileInit ();
986:
987: /* Register the default set of Icons for directory listings */
988: /*HTIconInit(NULL); *//*is this useful ? */
989:
990: /* Register the default set of messages and dialog functions */
991: AHTAlertInit ();
992: HTAlert_setInteractive (YES);
993: }
994:
1.5 cvs 995: /*----------------------------------------------------------------------
996: ----------------------------------------------------------------------*/
1.4 cvs 997: #ifdef __STDC__
998: static void AHTProfile_delete (void)
999: #else
1000: static void AHTProfile_delete ()
1.7 cvs 1001: #endif /* __STDC__ */
1.4 cvs 1002: {
1.7 cvs 1003: if (HTLib_isInitialized ())
1004: {
1.4 cvs 1005:
1.7 cvs 1006: /* Clean up the persistent cache (if any) */
1007: HTCacheTerminate ();
1.4 cvs 1008:
1.7 cvs 1009: /* Clean up all the global preferences */
1010: HTFormat_deleteAll ();
1.4 cvs 1011:
1.7 cvs 1012: HTLibTerminate ();
1013: /* Terminate libwww */
1014: }
1.4 cvs 1015: }
1016:
1.5 cvs 1017: /*----------------------------------------------------------------------
1018: ----------------------------------------------------------------------*/
1.4 cvs 1019: #ifdef __STDC__
1020: void QueryInit ()
1021: #else
1022: void QueryInit ()
1023: #endif
1024: {
1025:
1026: AHTProfile_newAmaya (HTAppName, HTAppVersion);
1027:
1028: /* New AHTBridge stuff */
1029:
1030: HTEvent_setRegisterCallback (AHTEvent_register);
1031: HTEvent_setUnregisterCallback (AHTEvent_unregister);
1032:
1033: /* Setup authentication manager */
1034: /***
1035: HTAuthCall_add("basic", HTBasic_parse, HTBasic_generate, HTBasic_delete);
1036: ****/
1037:
1038: /* Trace activation (for debugging) */
1.7 cvs 1039: /*
1.4 cvs 1040: WWW_TraceFlag = SHOW_APP_TRACE | SHOW_UTIL_TRACE |
1041: SHOW_BIND_TRACE | SHOW_THREAD_TRACE |
1042: SHOW_STREAM_TRACE | SHOW_PROTOCOL_TRACE |
1043: SHOW_URI_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
1044: SHOW_CORE_TRACE;
1045:
1.7 cvs 1046: */
1.4 cvs 1047:
1048: /***
1049: WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
1050: SHOW_PROTOCOL_TRACE| SHOW_APP_TRACE | SHOW_UTIL_TRACE;
1051: ***/
1052:
1053: HTBind_caseSensitive (FALSE);
1054: HTBind_addType ("html", "text/html", 0.9);
1055: HTBind_addType ("htm", "text/html", 0.9);
1056: HTBind_addType ("gif", "image/gif", 0.9);
1057: HTBind_addType ("png", "image/png", 0.9);
1058: HTBind_addType ("jpg", "image/jpeg", 0.9);
1059: HTBind_addType ("txt", "text/plain", 0.9);
1060:
1061: /* Setting up other user interfaces */
1062: /* needs a little bit more work */
1063:
1064: /* Setting up handlers */
1065: #ifndef HACK_WWW
1066: /* HTNetCall_addBefore(HTLoadStart, NULL, 0); */
1067: #endif
1068:
1069: /* Setting up different network parameters */
1070: HTNet_setMaxSocket (8);
1071: HTDNS_setTimeout (3600);
1072: HTCacheMode_setEnabled (0);
1073:
1074: /* Initialization of the global context */
1075: Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
1076: Amaya->reqlist = HTList_new ();
1077: Amaya->docid_status = HTList_new ();
1078: Amaya->open_requests = 0;
1079:
1080: #ifdef CATCH_SIG
1081: SetSignal ();
1082: #endif
1.15 ! cvs 1083: }
! 1084:
! 1085:
! 1086: /*----------------------------------------------------------------------
! 1087: ----------------------------------------------------------------------*/
! 1088: #ifdef __STDC__
! 1089: static int LoopForStop (AHTReqContext * me)
! 1090: #else
! 1091: static int LoopForStop (AHTReqContext * me)
! 1092: #endif
! 1093: {
! 1094:
! 1095: #ifdef WWW_XWINDOWS
! 1096: extern XtAppContext app_cont;
! 1097: XEvent ev;
! 1098: XtInputMask status;
! 1099:
! 1100: #endif /* WWW_XWINDOWS */
! 1101: int status_req;
! 1102:
! 1103: /* to test the async calls */
! 1104: /* Boucle d'attente des evenements */
! 1105:
! 1106: while (me->reqStatus != HT_ABORT &&
! 1107: me->reqStatus != HT_END &&
! 1108: me->reqStatus != HT_ERR)
! 1109: {
! 1110:
! 1111: #ifdef WWW_XWINDOWS
! 1112: status = XtAppPending (app_cont);
! 1113: if (status & XtIMXEvent)
! 1114: {
! 1115: XtAppNextEvent (app_cont, &ev);
! 1116: TtaHandleOneEvent (&ev);
! 1117: }
! 1118: else if (status & (XtIMAll & (~XtIMXEvent)))
! 1119: {
! 1120: XtAppProcessEvent (app_cont,
! 1121: (XtIMAll & (~XtIMXEvent)));
! 1122: }
! 1123: else
! 1124: {
! 1125: XtAppNextEvent (app_cont, &ev);
! 1126: TtaHandleOneEvent (&ev);
! 1127: }
! 1128:
! 1129: #endif /* WWW_XWINDOWS */
! 1130: }
! 1131:
! 1132: switch (me->reqStatus)
! 1133: {
1.4 cvs 1134:
1.15 ! cvs 1135: case HT_ERR:
! 1136: case HT_ABORT:
! 1137: status_req = HT_ERROR;
! 1138: break;
! 1139:
! 1140: case HT_END:
! 1141: status_req = HT_OK;
! 1142: break;
! 1143:
! 1144: default:
! 1145: break;
! 1146: }
1.12 cvs 1147:
1.15 ! cvs 1148: return (status_req);
1.4 cvs 1149: }
1150:
1151:
1.5 cvs 1152: /*----------------------------------------------------------------------
1.15 ! cvs 1153: QueryClose
1.5 cvs 1154: ----------------------------------------------------------------------*/
1.4 cvs 1155: void QueryClose ()
1156: {
1157: /** HTConversion_deleteAll(converters); **/
1158: /* Later, the following call should use all the active docids. For the mome
1159: nt, it stops everything */
1160: /* StopRequest (); */
1161: Thread_deleteAll ();
1162:
1163: #ifndef HACK_WWW
1164: /** HTAuthInfo_deleteAll (); **/
1165: #endif
1166: HTProxy_deleteAll ();
1167: HTNoProxy_deleteAll ();
1168: HTGateway_deleteAll ();
1169: AHTProfile_delete ();
1170: }
1171:
1172:
1.5 cvs 1173: /*----------------------------------------------------------------------
1.15 ! cvs 1174: GetObjectWWW
! 1175: loads the file designated by urlName in a temporary file.
1.5 cvs 1176: 4 file retrieval modes are proposed:
1177: AMAYA_SYNC : blocking mode
1178: AMAYA_ISYNC : incremental, blocking mode
1179: AMAYA_ASYNC : non-blocking mode
1180: AMAYA_IASYNC : incremental, non-blocking mode
1181:
1182: In the incremental mode, each time a package arrives, it will be
1183: stored in the temporary file. In addition, if an
1184: incremental_callback function is defined, this function will be
1185: called and handled a copy of the newly received data package.
1186: Finally, if a terminate_callback function is defined, it will be
1187: invoked when the request terminates. The caller of this function
1.4 cvs 1188: can define two different contexts to be passed to the callback
1189: functions.
1190:
1191: When the function is called with the SYNC mode, the function will
1192: return only when the requested file has been loaded.
1193: The ASYNC mode will immediately return after setting up the
1194: call.
1195:
1196: Notes:
1197: At the end of a succesful request, the urlName string contains the
1198: name of the actually retrieved URL. As a URL can change over the time,
1199: (e.g., be redirected elsewhere), it is advised that the function
1200: caller verifies the value of the urlName variable at the end of
1201: a request.
1202:
1203: Inputs:
1204: - docid Document identifier for the set of objects being
1205: retrieved.
1206: - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
1207: - outputfile A pointer to an empty string of MAX_URL_LENGTH.
1208: - mode The retrieval mode.
1209: - incremental_cbf
1210: - context_icbf
1211: Callback and context for the incremental modes
1212: - terminate_cbf
1213: - context_icbf
1214: Callback and context for a terminate handler
1215:
1216: Outputs:
1217: - urlName The URL that was retrieved
1218: - outputfile The name of the temporary file which holds the
1219: retrieved data. (Only in case of success)
1220: Returns:
1221: HT_ERROR
1222: HT_OK
1.5 cvs 1223:
1224: ----------------------------------------------------------------------*/
1.4 cvs 1225: #ifdef __STDC__
1226: int GetObjectWWW (int docid, char *urlName, char *postString,
1227: char *outputfile, int mode,
1228: TIcbf * incremental_cbf, void *context_icbf,
1.15 ! cvs 1229: TTcbf * terminate_cbf, void *context_tcbf, boolean error_html)
1.4 cvs 1230: #else
1231: int GetObjectWWW (docid, urlName, postString, outputfile, mode,
1232: incremental_cbf, context_icbf, terminate_cbf, context_tcbf,
1233: error_html)
1234: int docid;
1235: char *urlName;
1236: char *postString;
1237: char *outputfile;
1238: int mode;
1239: TIcbf *incremental_cbf;
1240: void *context_icbf;
1241: TTcbf *terminate_cbf;
1242: void *context_tcbf;
1.15 ! cvs 1243: boolean error_html;
1.4 cvs 1244: #endif
1245: {
1246: AHTReqContext *me;
1247:
1248: FILE *tmp_fp;
1249: char *tmp_dir;
1250: char *ref;
1251: int status;
1252:
1253: static int object_counter = 0; /* loaded objects counter */
1254:
1255: HTList *cur, *pending;
1256:
1257: #if 0
1258: char *test;
1259:
1.7 cvs 1260: /*** test URL*/
1261: test = AHTMakeRelativeName ("http://www.w3.org/", "http://www.w3.org/pub/Amaya");
1262: HT_FREE (test);
1263: test = AHTMakeRelativeName ("http://www.w3.org", "http://www.w3.org/pub/Amaya");
1264: HT_FREE (test);
1265: test = AHTMakeRelativeName ("http://www.w3.org/pub/Amaya", "http://www.w3.org");
1266: HT_FREE (test);
1267: #endif
1268:
1269: if (urlName == NULL || docid == 0 || outputfile == NULL)
1270: {
1271:
1272: /* no file to be loaded */
1273: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL),
1274: urlName);
1275:
1276: if (error_html)
1277: FilesLoading[docid] = 2; /* so we can show the error message */
1278: return HT_ERROR;
1.4 cvs 1279:
1.7 cvs 1280: }
1.4 cvs 1281: /* do we support this protocol? */
1.7 cvs 1282: if (IsValidProtocol (urlName) == NO)
1283: {
1284: /* return error */
1285: outputfile[0] = EOS; /* file could not be opened */
1286: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
1287: urlName);
1288:
1289: if (error_html)
1290: FilesLoading[docid] = 2; /* so we can show the error message */
1291: return HT_ERROR;
1292: }
1.4 cvs 1293:
1294: /* verify if a docid directory exists */
1295:
1296: tmp_dir = TtaGetMemory (strlen (TempFileDirectory) + 5 + 1);
1297: sprintf (tmp_dir, "%s/%d", TempFileDirectory, docid);
1298:
1299: tmp_fp = fopen (tmp_dir, "r");
1.7 cvs 1300: if (tmp_fp == 0)
1301: {
1302: /*directory did not exist */
1303: if (mkdir (tmp_dir, S_IRWXU) == -1)
1304: {
1305: /*error */
1306: outputfile[0] = EOS;
1307: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_CACHE_ERROR),
1308: urlName);
1309:
1310: if (error_html)
1311: FilesLoading[docid] = 2; /* so we can show the error message */
1312:
1313: return HT_ERROR;
1314: }
1315: }
1316: else
1.4 cvs 1317: fclose (tmp_fp);
1318:
1319: /*create a tempfilename */
1320:
1321: sprintf (outputfile, "%s/%04dAM", tmp_dir, object_counter);
1322:
1323: TtaFreeMemory (tmp_dir);
1324:
1325: /* update the object_counter */
1326: object_counter++;
1327:
1328: /* normalize the URL */
1329: ref = HTParse (urlName, "", PARSE_ALL);
1330:
1331: /* should we abort the request if we could not normalize the url? */
1332:
1.7 cvs 1333: if (ref == (char *) NULL || ref[0] == EOS)
1334: {
1335: /*error */
1336: outputfile[0] = EOS;
1337: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL),
1338: urlName);
1.4 cvs 1339:
1.7 cvs 1340: if (error_html)
1341: FilesLoading[docid] = 2; /* so we can show the error message */
1.4 cvs 1342:
1.7 cvs 1343: return HT_ERROR;
1344: }
1.4 cvs 1345: /* verify if that file name existed */
1.9 cvs 1346: if (TtaFileExist (outputfile))
1.7 cvs 1347: {
1.9 cvs 1348: TtaFileUnlink (outputfile);
1.7 cvs 1349: }
1.4 cvs 1350:
1351: /* try to open the outputfile */
1352:
1.7 cvs 1353: if ((tmp_fp = fopen (outputfile, "w")) == NULL)
1354: {
1355: outputfile[0] = EOS; /* file could not be opened */
1356: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_CREATE_FILE),
1357: outputfile);
1358: HT_FREE (ref);
1359:
1360: if (error_html)
1361: FilesLoading[docid] = 2; /* so we can show the error message */
1362: return (HT_ERROR);
1363: }
1.4 cvs 1364:
1365: /* the terminate_handler closes the above open fp */
1366: /* Not anymore, we do that in the AHTCallback_bridge, as all
1367: requests are now asynchronous */
1368:
1369: /* Initialize the request structure */
1370:
1371: me = AHTReqContext_new (docid);
1372:
1.7 cvs 1373: if (me == NULL)
1374: {
1375: fclose (tmp_fp);
1376: outputfile[0] = EOS;
1377: /* need an error message here */
1378: HT_FREE (ref);
1379: return (HT_ERROR);
1380: }
1.4 cvs 1381:
1382:
1383: /* Specific initializations for POST and GET */
1.7 cvs 1384: if (mode & AMAYA_FORM_POST)
1385: {
1386: me->method = METHOD_POST;
1387: me->mem_ptr = postString;
1388: me->block_size = strlen (postString);
1389: HTRequest_setMethod (me->request, METHOD_POST);
1390: HTRequest_setPostCallback (me->request, AHTUpload_callback);
1391: }
1392: else
1393: {
1394: me->method = METHOD_GET;
1395: me->dest = (HTParentAnchor *) NULL; /*useful only for PUT and POST methods */
1396: }
1.4 cvs 1397:
1398: /* Common initialization */
1399:
1400: me->mode = mode;
1401: me->error_html = error_html;
1402: me->incremental_cbf = incremental_cbf;
1403: me->context_icbf = context_icbf;
1404: me->terminate_cbf = terminate_cbf;
1405: me->context_tcbf = context_tcbf;
1406: me->output = tmp_fp;
1407:
1408: HTRequest_setOutputStream (me->request,
1409: AHTFWriter_new (me->request, me->output, YES));
1.7 cvs 1410:
1.4 cvs 1411:
1412: /*for the async. request modes, we need to have our
1413: own copy of outputfile and urlname
1414: */
1415:
1.7 cvs 1416: if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC))
1417: {
1418: char *tmp;
1419:
1420: tmp = TtaGetMemory (strlen (outputfile) + 1);
1421: strcpy (tmp, outputfile);
1422: me->outputfile = tmp;
1423:
1424: tmp = TtaGetMemory (MAX_LENGTH);
1425: strcpy (tmp, urlName);
1426: me->urlName = tmp;
1.4 cvs 1427:
1.7 cvs 1428: }
1429: else
1430: {
1431: me->outputfile = outputfile;
1432: me->urlName = urlName;
1433: }
1.4 cvs 1434:
1435: /***
1436: Change for taking into account the stop button:
1437: The requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1438: we will loop until the document has been received or a stop signal
1439: generated
1440: ****/
1441:
1442: HTRequest_setPreemptive (me->request, NO);
1443: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_FETCHING),
1444: me->urlName);
1445:
1446: me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1447:
1448: HT_FREE (ref);
1449:
1.7 cvs 1450: if (mode & AMAYA_FORM_POST)
1451: {
1452: HTAnchor_setFormat ((HTParentAnchor *) me->anchor, HTAtom_for ("application/x-www-form-urlencoded"));
1453: HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1454: HTRequest_setEntityAnchor (me->request, me->anchor);
1.4 cvs 1455:
1.7 cvs 1456: status = HTLoadAbsolute (urlName, me->request);
1457: }
1458: else
1.4 cvs 1459: status = HTLoadAnchor ((HTAnchor *) me->anchor,
1460: me->request);
1461:
1462: if (status == HT_ERROR || me->reqStatus == HT_END
1.7 cvs 1463: || me->reqStatus == HT_ERR)
1464: {
1.4 cvs 1465:
1.7 cvs 1466: /* in case of error, free all allocated memory and exit */
1467:
1468: if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC))
1469: {
1470: TtaFreeMemory (me->outputfile);
1471: TtaFreeMemory (me->urlName);
1472: }
1473: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1474: me->urlName);
1.4 cvs 1475:
1.7 cvs 1476: status = (me->reqStatus == HT_END) ? HT_OK : HT_ERROR;
1477:
1478: AHTReqContext_delete (me);
1479:
1480: }
1481: else
1482: {
1483:
1484: /* part of the stop button handler */
1485:
1486: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
1487: {
1488: status = LoopForStop (me);
1489: AHTReqContext_delete (me);
1490: }
1491: else
1492: {
1493:
1494: /* ASYNC MODE */
1495: /* if the request went to the pending events queue, then we close
1496: ** the file. It'll be open by AddEventLoop upon liberation of the
1497: ** queue
1.4 cvs 1498: */
1.7 cvs 1499:
1500: /* verify if this request went to the pending request queue */
1501:
1502: if ((cur = (HTList *) me->request->net->host->pending))
1503: while ((pending = HTList_nextObject (cur)))
1504: {
1505: if (me->request->net == (HTNet *) pending)
1506: {
1507: /* To correct: see if we can fine tune this request */
1508: if (me->reqStatus == HT_WAITING)
1509: break;
1510: me->reqStatus = HT_NEW_PENDING;
1511: /* the request went to the pending queue, so we close the fd to
1512: **avoid having many of them open without being used
1513: */
1514: if (THD_TRACE)
1515: fprintf (stderr, "GetObjectWWW: %s is pending. Closing fd %d\n", me->urlName, (int) me->output);
1516: fclose (me->output);
1517: me->output = NULL;
1518: break;
1519: }
1520: }
1521:
1.4 cvs 1522: }
1.7 cvs 1523: }
1.4 cvs 1524: TtaHandlePendingEvents ();
1525:
1526: return (status);
1527: }
1528:
1.5 cvs 1529: /*----------------------------------------------------------------------
1530: PutObjectWWW uploads a file into a URL
1531:
1532: 2 upload modes are proposed:
1533: AMAYA_SYNC : blocking mode
1534: AMAYA_ASYNC : non-blocking mode
1535:
1536:
1.4 cvs 1537: When the function is called with the SYNC mode, the function will
1538: return only when the file has been uploaded.
1539: The ASYNC mode will immediately return after setting up the
1540: call. Furthermore, at the end of an upload, the ASYNC mode will
1541: call back terminate_cbf, handling it the context defined in
1542: context_tcbf.
1543:
1544: Notes:
1545: At the end of a succesful request, the urlName string contains the
1546: name of the actually uploaded URL. As a URL can change over the time,
1547: (e.g., be redirected elsewhere), it is advised that the function
1548: caller verifies the value of the urlName variable at the end of
1549: a request.
1550:
1551: Inputs:
1552: - docid Document identifier for the set of objects being
1553: retrieved.
1554: - fileName A pointer to the local file to upload
1555: - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
1556: - mode The retrieval mode.
1557:
1558: - terminate_cbf
1559: - context_icbf
1560: Callback and context for a terminate handler
1561:
1562: Outputs:
1563: - urlName The URL that was uploaded
1564:
1565: Returns:
1566: HT_ERROR
1567: HT_OK
1.5 cvs 1568: ----------------------------------------------------------------------*/
1.4 cvs 1569: #ifdef __STDC__
1570: int PutObjectWWW (int docid, char *fileName, char *urlName, int mode,
1571: TTcbf * terminate_cbf, void *context_tcbf)
1572: #else
1573: int PutObjectWWW (docid, urlName, fileName, mode,
1574: ,terminate_cbf, context_tcbf)
1575: int docid;
1576: char *urlName;
1577: char *fileName;
1578: int mode;
1579: TTcbf *terminate_cbf;
1580: void *context_tcbf;
1581:
1582: #endif
1583: {
1.7 cvs 1584: /*AHTReqContext *me; */
1.4 cvs 1585: int status;
1586:
1587: #ifdef WWW_XWINDOWS
1588: /*** Temporary patch (I hope) ****/
1589: int fd;
1590: struct stat file_stat;
1591: char *mem_ptr;
1592: unsigned long block_size;
1593:
1594: if (urlName == NULL || docid == 0 || fileName == NULL ||
1.9 cvs 1595: !TtaFileExist (fileName))
1.4 cvs 1596: /* no file to be uploaded */
1597: return HT_ERROR;
1598:
1599: /* do we support this protocol? */
1.7 cvs 1600: if (IsValidProtocol (urlName) == NO)
1601: {
1602: /* return error */
1603: TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1604: urlName);
1605: return HT_ERROR;
1606: }
1.4 cvs 1607: /* read the file into memory */
1608:
1.7 cvs 1609: if ((fd = open (fileName, O_RDONLY)) == -1)
1610: {
1.4 cvs 1611:
1.7 cvs 1612: /* if we could not open the file, exit */
1.4 cvs 1613:
1.7 cvs 1614: /*error msg here */
1.4 cvs 1615:
1.7 cvs 1616: return (HT_ERROR);
1617: }
1.4 cvs 1618:
1619: fstat (fd, &file_stat);
1620:
1.7 cvs 1621: if (file_stat.st_size == 0)
1622: {
1.4 cvs 1623:
1.7 cvs 1624: /* file was empty */
1.4 cvs 1625:
1.7 cvs 1626: /*errmsg here */
1.4 cvs 1627:
1.7 cvs 1628: close (fd);
1629: return (HT_ERROR);
1630: }
1.4 cvs 1631: block_size = file_stat.st_size;
1632:
1633: if (THD_TRACE)
1634: fprintf (stderr, "file size == %u\n", (unsigned) block_size);
1635:
1636: mem_ptr = (char *) TtaGetMemory (block_size);
1637:
1.7 cvs 1638: if (mem_ptr == (char *) NULL)
1639: {
1.4 cvs 1640:
1.7 cvs 1641: /* could not allocate enough memory */
1.4 cvs 1642:
1.7 cvs 1643: /*errmsg here */
1.4 cvs 1644:
1.7 cvs 1645: close (fd);
1646: return (HT_ERROR);
1647: }
1.4 cvs 1648: read (fd, mem_ptr, block_size);
1649:
1650: close (fd);
1651:
1652: status = UploadMemWWW (docid, METHOD_PUT, urlName, mem_ptr,
1653: block_size, mode, terminate_cbf,
1654: context_tcbf, (char *) NULL);
1655:
1656: TtaFreeMemory (mem_ptr);
1657:
1658: #endif /*WWW_XWINDOWS */
1659:
1660: return (status);
1661: /*** End of temporary patch */
1662:
1663: }
1664:
1665:
1.5 cvs 1666: /*----------------------------------------------------------------------
1667: ----------------------------------------------------------------------*/
1.4 cvs 1668: #ifdef __STDC__
1669: int UploadMemWWW (int docid, HTMethod method,
1670: char *urlName, char *mem_ptr, unsigned long block_size,
1671: int mode, TTcbf * terminate_cbf, void *context_tcbf,
1672: char *outputfile)
1673: #else
1674: int UploadMemWWW (docid, method, urlName, mem_ptr, block_size, mode,
1675: terminate_cbf, context_tcbf, outputfile)
1676: int docid;
1677: HTMethod method;
1678: char *urlName;
1679: char *mem_ptr;
1680: usigned long block_size;
1681: int mode;
1682: TTcbf *terminate_cbf;
1683: void *context_tcbf;
1684: char *outputfile;
1.7 cvs 1685:
1.4 cvs 1686: #endif
1687: {
1688: AHTReqContext *me;
1689: int status;
1690:
1691: /****
1692: int basename_flag;
1693: ****/
1694:
1695: if (mem_ptr == (char *) NULL ||
1696: block_size == 0 ||
1697: docid == 0 ||
1.7 cvs 1698: urlName == (char *) NULL)
1699: {
1700: /* nothing to be uploaded */
1701: return HT_ERROR;
1702: }
1.4 cvs 1703: /* if we are doing a post, try to open the destination file */
1704:
1705: /* Initialize the request structure */
1706: me = AHTReqContext_new (docid);
1707:
1.7 cvs 1708: if (me == NULL)
1709: {
1710: /* need an error message here */
1711: TtaHandlePendingEvents ();
1712: return (HT_ERROR);
1713: }
1.4 cvs 1714: me->mode = mode;
1715:
1716: me->incremental_cbf = (TIcbf *) NULL;
1717: me->context_icbf = (void *) NULL;
1718: me->terminate_cbf = terminate_cbf;
1719: me->context_tcbf = context_tcbf;
1720:
1721: me->output = stdout;
1722: me->outputfile = (char *) NULL;
1723: me->urlName = urlName;
1724:
1725: HTRequest_setPreemptive (me->request, NO);
1726:
1727: me->method = METHOD_PUT;
1728: HTRequest_setMethod (me->request, METHOD_PUT);
1729: me->output = stdout;
1730: me->outputfile = (char *) NULL;
1731:
1732: me->mem_ptr = mem_ptr;
1733: me->block_size = block_size;
1734: HTRequest_setPostCallback (me->request, AHTUpload_callback);
1735:
1736: HTRequest_setOutputStream (me->request,
1737: HTFWriter_new (me->request, me->output, YES));
1738:
1739: me->anchor = (HTParentAnchor *) HTAnchor_findAddress (urlName);
1.7 cvs 1740:
1741: HTAnchor_setFormat ((HTParentAnchor *) me->anchor, HTAtom_for ("text/html")); /* test */
1742: HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1.4 cvs 1743: HTRequest_setEntityAnchor (me->request, me->anchor);
1.5 cvs 1744: /*
1.7 cvs 1745: HTRequest_addAfter (me->request, AHTSaveFilter, NULL, NULL, HT_ALL,
1746: HT_FILTER_FIRST, NO);
1747: */
1.4 cvs 1748: status = HTLoadAbsolute (urlName, me->request);
1749:
1750: if (status == HT_ERROR || me->reqStatus == HT_END
1.7 cvs 1751: || me->reqStatus == HT_ERR || HTError_hasSeverity (HTRequest_error (me->request), ERR_NON_FATAL))
1752: {
1753: /* just in case ... :-) <-- because it's the case sometimes */
1.4 cvs 1754:
1755: #ifdef WWW_XWINDOWS
1756: /***
1757: if(me->xtinput_id != (XtInputId) NULL) {
1758: if (THD_TRACE)
1759: fprintf(stderr, "Query: Removing Xtinput: %lu\n",
1760: me->xtinput_id);
1761: XtRemoveInput(me->xtinput_id);
1762: }
1763: ***/
1764: #endif /* WWW_XWINDOWS */
1765:
1.7 cvs 1766: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_SAVE),
1767: me->urlName);
1.4 cvs 1768:
1.7 cvs 1769: status = HT_ERROR;
1770: AHTReqContext_delete (me);
1.4 cvs 1771:
1.7 cvs 1772: }
1773: else
1774: {
1.4 cvs 1775:
1.7 cvs 1776: TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
1777: me->urlName);
1.4 cvs 1778:
1.7 cvs 1779: /* part of the stop button handler */
1.4 cvs 1780:
1.7 cvs 1781: if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
1782: {
1783: status = LoopForStop (me);
1784: AHTReqContext_delete (me);
1785: }
1.15 ! cvs 1786: }
1.4 cvs 1787:
1788: return (status);
1789:
1790: }
1791:
1792:
1.5 cvs 1793: /*----------------------------------------------------------------------
1794: Stop Request stops (kills) all active requests associated with a docid
1795: ----------------------------------------------------------------------*/
1.4 cvs 1796: #ifdef __STDC__
1797: void StopRequest (int docid)
1798: #else
1799: void StopRequest (docid)
1800: int docid;
1801: #endif
1802: {
1803: HTList *cur;
1804: AHTDocId_Status *docid_status;
1805: AHTReqContext *me;
1806: int open_requests;
1807:
1.7 cvs 1808: if (Amaya)
1809: {
1.4 cvs 1810:
1.7 cvs 1811: cur = Amaya->reqlist;
1812: docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
1.4 cvs 1813: Amaya->docid_status);
1814:
1.7 cvs 1815: /* verify if there are any requests at all associated with docid */
1.4 cvs 1816:
1.7 cvs 1817: if (docid_status == (AHTDocId_Status *) NULL)
1818: return;
1.4 cvs 1819:
1.7 cvs 1820: open_requests = docid_status->counter;
1.4 cvs 1821:
1.7 cvs 1822: while ((me = (AHTReqContext *) HTList_nextObject (cur)))
1823: {
1.4 cvs 1824:
1.7 cvs 1825: if (me->docid == docid)
1826: {
1.4 cvs 1827:
1.7 cvs 1828: /* kill this request */
1.4 cvs 1829:
1.7 cvs 1830: switch (me->reqStatus)
1831: {
1.4 cvs 1832:
1.7 cvs 1833: case HT_ABORT:
1834: break;
1.4 cvs 1835:
1.7 cvs 1836: case HT_BUSY:
1837: me->reqStatus = HT_ABORT;
1838: break;
1839: case HT_NEW_PENDING:
1840: case HT_WAITING:
1841: default:
1.4 cvs 1842: #ifdef WWW_XWINDOWS
1.7 cvs 1843: RequestKillAllXtevents (me);
1.4 cvs 1844: #endif
1.7 cvs 1845: me->reqStatus = HT_ABORT;
1.4 cvs 1846: /***
1847: HTNet_kill(me->request);
1848: ***/
1849:
1.7 cvs 1850: HTRequest_kill (me->request);
1.4 cvs 1851:
1.7 cvs 1852: if (me->mode == AMAYA_ASYNC ||
1853: me->mode == AMAYA_IASYNC)
1854: {
1.4 cvs 1855:
1.7 cvs 1856: AHTReqContext_delete (me);
1857: }
1858: cur = Amaya->reqlist;
1.4 cvs 1859:
1.7 cvs 1860: open_requests--;
1.4 cvs 1861:
1.7 cvs 1862: break;
1.4 cvs 1863:
1.7 cvs 1864: } /* switch */
1865: } /* if me docid */
1866: } /* while */
1867: } /* if amaya open requests */
1.4 cvs 1868: } /* StopRequest */
Webmaster