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

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

Webmaster