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