Annotation of Amaya/amaya/query.c, revision 1.6

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

Webmaster