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

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

Webmaster