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

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

Webmaster