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

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

Webmaster