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

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

Webmaster