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

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

Webmaster