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

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: /*
1.83      cvs         9:  * query.c : contains all the functions for requesting and publishing
1.19      cvs        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
1.92      cvs        14:  *         J. Kahan/R. Guetari 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: 
1.81      cvs       211:    /* Bind the Context object together with the Request Object */
                    212:    me->request = HTRequest_new ();
                    213:   
1.80      cvs       214:    /* clean the associated file structure) */
1.79      cvs       215:    HTRequest_setOutputStream (me->request, NULL);
1.81      cvs       216:  
1.4       cvs       217:    /* Initialize the other members of the structure */
1.17      cvs       218:    me->reqStatus = HT_NEW; /* initial status of a request */
1.4       cvs       219:    me->output = NULL;
1.89      cvs       220:    me->content_type = NULL;
1.51      cvs       221: #  ifndef _WINDOWS
1.4       cvs       222:    me->read_xtinput_id = (XtInputId) NULL;
                    223:    me->write_xtinput_id = (XtInputId) NULL;
                    224:    me->except_xtinput_id = (XtInputId) NULL;
1.51      cvs       225: #  endif
1.4       cvs       226:    me->docid = docid;
                    227:    HTRequest_setMethod (me->request, METHOD_GET);
                    228:    HTRequest_setOutputFormat (me->request, WWW_SOURCE);
                    229:    HTRequest_setContext (me->request, me);
                    230:    me->read_ops = 0;
                    231:    me->write_ops = 0;
                    232:    me->except_ops = 0;
1.36      cvs       233:    /* experimental */
                    234:    me->read_sock = INVSOC;
                    235:    me->write_sock = INVSOC;
                    236:    me->except_sock = INVSOC;
1.4       cvs       237: 
                    238:    /* Update the global context */
                    239:    HTList_appendObject (Amaya->reqlist, (void *) me);
                    240: 
                    241:    docid_status = GetDocIdStatus (docid, Amaya->docid_status);
                    242: 
1.7       cvs       243:    if (docid_status == NULL)
                    244:      {
                    245:        docid_status = (AHTDocId_Status *) TtaGetMemory (sizeof (AHTDocId_Status));
                    246:        docid_status->docid = docid;
                    247:        docid_status->counter = 1;
                    248:        HTList_addObject (Amaya->docid_status, (void *) docid_status);
                    249:      }
                    250:    else
1.4       cvs       251:       docid_status->counter++;
                    252: 
                    253: 
                    254:    Amaya->open_requests++;
                    255: 
                    256:    /* error stream handling */
                    257:    me->error_stream = (char *) NULL;
                    258:    me->error_stream_size = 0;
1.74      cvs       259: 
                    260: #ifdef DEBUG_LIBWWW
                    261:    fprintf (stderr, "AHTReqContext_new: Created object %p\n", me);
                    262: #endif   
1.4       cvs       263:    return me;
1.74      cvs       264: 
1.4       cvs       265: }
                    266: 
1.5       cvs       267: /*----------------------------------------------------------------------
1.17      cvs       268:   AHTReqContext_delete
                    269:   Delete an Amaya Context Object and update the global Amaya request
                    270:   status.
1.5       cvs       271:   ----------------------------------------------------------------------*/
1.4       cvs       272: 
                    273: #ifdef __STDC__
1.15      cvs       274: boolean   AHTReqContext_delete (AHTReqContext * me)
1.4       cvs       275: #else
1.15      cvs       276: boolean   AHTReqContext_delete (me)
1.4       cvs       277: AHTReqContext      *me;
                    278: 
                    279: #endif
                    280: {
                    281:    AHTDocId_Status    *docid_status;
                    282: 
1.7       cvs       283:    if (me)
                    284:      {
1.4       cvs       285: 
1.74      cvs       286: #ifdef DEBUG_LIBWWW
                    287:         fprintf (stderr, "AHTReqContext_delete: Deleting object %p\n", me);
                    288: #endif   
                    289: 
1.7       cvs       290:        if (Amaya->reqlist)
                    291:           HTList_removeObject (Amaya->reqlist, (void *) me);
1.4       cvs       292: 
1.7       cvs       293:        docid_status = GetDocIdStatus (me->docid, Amaya->docid_status);
1.4       cvs       294: 
1.7       cvs       295:        if (docid_status)
                    296:          {
                    297:             docid_status->counter--;
                    298: 
                    299:             if (docid_status->counter == 0)
                    300:               {
                    301:                  HTList_removeObject (Amaya->docid_status, (void *) docid_status);
                    302:                  TtaFreeMemory ((void *) docid_status);
                    303:               }
                    304:          }
1.79      cvs       305: 
                    306:        if (HTRequest_outputStream (me->request))
                    307:          AHTFWriter_FREE (me->request->output_stream);
1.91      cvs       308: 
1.7       cvs       309:        HTRequest_delete (me->request);
1.85      cvs       310: 
1.89      cvs       311:        if (me->output && me->output != stdout)
1.85      cvs       312:          {     
                    313: #ifdef DEBUG_LIBWWW       
                    314:            fprintf (stderr, "AHTReqContext_delete: URL is  %s, closing "
                    315:                     "FILE %p\n", me->urlName, me->output); 
                    316: #endif
                    317:            fclose (me->output);
                    318:            me->output = NULL;
                    319:          }
1.79      cvs       320:          
1.7       cvs       321:        if (me->error_stream != (char *) NULL)
1.70      cvs       322:          HT_FREE (me->error_stream);
                    323: #     ifndef _WINDOWS
                    324: #     ifdef WWW_XWINDOWS       
1.21      cvs       325:        if (me->read_xtinput_id || me->write_xtinput_id ||
                    326:             me->except_xtinput_id)
                    327:           RequestKillAllXtevents(me);
1.70      cvs       328: #     endif /* WWW_XWINDOWS */
                    329: #     endif /* !_WINDOWS */
1.74      cvs       330: 
                    331:        if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC))
                    332:          /* for the ASYNC mode, free the memory we allocated in GetObjectWWW
                    333:             or in PutObjectWWW */
                    334:          {
1.79      cvs       335:             if (me->urlName)
                    336:               TtaFreeMemory (me->urlName);
                    337:             if (me->outputfile)
                    338:               TtaFreeMemory (me->outputfile);
1.74      cvs       339:          }
                    340:    
1.7       cvs       341:        TtaFreeMemory ((void *) me);
1.4       cvs       342: 
1.7       cvs       343:        Amaya->open_requests--;
1.4       cvs       344: 
1.15      cvs       345:        return TRUE;
1.4       cvs       346: 
1.7       cvs       347:      }
1.15      cvs       348:    return FALSE;
1.4       cvs       349: }
                    350: 
                    351: 
1.15      cvs       352: /*----------------------------------------------------------------------
                    353:   AHTUpload_callback
1.17      cvs       354:   callback handler for executing the PUT command
1.15      cvs       355:   ----------------------------------------------------------------------*/
1.4       cvs       356: #ifdef __STDC__
                    357: static int          AHTUpload_callback (HTRequest * request, HTStream * target)
                    358: #else
                    359: static int          AHTUpload_callback (request, target)
                    360: HTRequest          *request;
                    361: HTStream           *target;
                    362: 
                    363: #endif
                    364: {
                    365:    AHTReqContext      *me = HTRequest_context (request);
1.7       cvs       366:    HTParentAnchor     *entity = HTRequest_entityAnchor (request);
                    367:    int                 len = HTAnchor_length (entity);
1.4       cvs       368:    int                 status;
                    369: 
                    370:    /* Send the data down the pipe */
1.7       cvs       371: 
1.4       cvs       372:    status = (*target->isa->put_block) (target, me->mem_ptr, len);
                    373: 
1.7       cvs       374:    if (status == HT_LOADED || status == HT_OK)
                    375:      {
1.13      cvs       376:        if (PROT_TRACE)
                    377:         HTTrace ("Posting Data Target is SAVED\n");
                    378:        (*target->isa->flush) (target);
                    379:        return (HT_LOADED);
1.7       cvs       380:      }
                    381:    if (status == HT_WOULD_BLOCK)
                    382:      {
                    383:        if (PROT_TRACE)
                    384:           HTTrace ("Posting Data Target WOULD BLOCK\n");
1.74      cvs       385: #ifdef _WINDOWS
                    386:        return HT_CONTINUE;
                    387: #else
                    388:        return HT_WOULD_BLOCK;
                    389: #endif /* _WINDOWS */
                    390: 
1.7       cvs       391:        return HT_WOULD_BLOCK;
                    392:      }
                    393:    else if (status == HT_PAUSE)
                    394:      {
                    395:        if (PROT_TRACE)
                    396:           HTTrace ("Posting Data Target PAUSED\n");
                    397:        return HT_PAUSE;
                    398:      }
                    399:    else if (status > 0)
                    400:      {                         /* Stream specific return code */
                    401:        if (PROT_TRACE)
                    402:           HTTrace ("Posting Data. Target returns %d\n", status);
                    403:        return status;
                    404:      }
                    405:    else
                    406:      {                         /* we have a real error */
                    407:        if (PROT_TRACE)
                    408:           HTTrace ("Posting Data Target ERROR %d\n", status);
                    409:        return status;
                    410:      }
1.4       cvs       411: }
                    412: 
1.5       cvs       413: /*----------------------------------------------------------------------
1.17      cvs       414:   Thread_deleteAll
                    415:   this function deletes the whole list of active threads.           
1.5       cvs       416:   ----------------------------------------------------------------------*/
1.4       cvs       417: #ifdef __STDC__
                    418: static void         Thread_deleteAll (void)
                    419: #else
                    420: static void         Thread_deleteAll ()
                    421: #endif
                    422: {
1.21      cvs       423:   HTList             *cur;
                    424:   AHTReqContext      *me;
                    425:   AHTDocId_Status    *docid_status;
                    426: 
1.74      cvs       427:   if (Amaya && Amaya->reqlist)
                    428:     {
                    429:       if (Amaya->open_requests > 0)
                    430: #ifdef DEBUG_LIBWWW
                    431:       fprintf (stderr, "Thread_deleteAll: Killing %d outstanding "
                    432:                       "requests\n", Amaya->open_requests);
                    433: #endif   
                    434:        {
                    435:          cur = Amaya->reqlist;
                    436:          
                    437:          /* erase the requests */
                    438:          while ((me = (AHTReqContext *) HTList_removeLastObject (cur)))
                    439:            {
                    440:              if (me->request)
                    441:                {
1.70      cvs       442: #              ifndef _WINDOWS 
1.74      cvs       443:                  RequestKillAllXtevents (me);
1.70      cvs       444: #              endif /* !_WINDOWS */
1.92      cvs       445: 
                    446:                  if (me->request->net)
                    447:                    HTRequest_kill (me->request);
1.96      cvs       448: #ifndef _WINDOWS
                    449:                  AHTReqContext_delete (me);
                    450: #endif /* _WINDOWS */
1.74      cvs       451:                }
                    452:            }           /* while */
                    453:          
                    454:          /* erase the docid_status entities */
                    455:          while ((docid_status = (AHTDocId_Status *) HTList_removeLastObject ((void *) Amaya->docid_status)))
                    456:            TtaFreeMemory ((void *) docid_status);
                    457:          
                    458:        }                       /* if */
1.84      cvs       459:        
1.74      cvs       460:     }
1.4       cvs       461: }
                    462: 
1.5       cvs       463: /*----------------------------------------------------------------------
1.83      cvs       464:   AHTOpen_file
                    465:   ----------------------------------------------------------------------*/
                    466: #ifdef __STDC__
1.85      cvs       467: int                 AHTOpen_file (HTRequest * request)
1.83      cvs       468: #else
1.85      cvs       469: int                 AHTOpen_file (request)
1.83      cvs       470: HTRequest           *request;
                    471: 
                    472: #endif /* __STDC__ */
                    473: {
                    474:   AHTReqContext      *me;      /* current request */
                    475: 
                    476:   me = HTRequest_context (request);
                    477: 
1.93      cvs       478: 
                    479:   if (!me)
                    480:       return HT_ERROR;
                    481: 
1.83      cvs       482: #ifdef DEBUG_LIBWWW
1.85      cvs       483:   fprintf(stderr, "AHTOpen_file: start\n");
                    484: #endif /* DEBUG_LIBWWW */
                    485: 
                    486:   if (me->reqStatus == HT_ABORT) 
                    487:     {
                    488: #ifdef DEBUG_LIBWWW
                    489:       fprintf(stderr, "AHTOpen_file: caught an abort request, skipping it\n");
1.83      cvs       490: #endif /* DEBUG_LIBWWW */
                    491: 
1.85      cvs       492:       return HT_OK;
                    493:     }
                    494: 
                    495:   if (HTRequest_outputStream (me->request)) 
                    496:     {
                    497: 
                    498: #ifdef DEBUG_LIBWWW
                    499:       fprintf(stderr, "AHTOpen_file: output stream already existed for url %s\n", me->urlName);
                    500: #endif /* DEBUG_LIBWWW */      
                    501:       return HT_OK;
                    502:     }
                    503: 
                    504: #ifdef DEBUG_LIBWWW
                    505:       fprintf(stderr, "AHTOpen_file: opening output stream for url %s\n", me->urlName);
                    506: #endif /* DEBUG_LIBWWW */      
                    507: 
1.83      cvs       508:   if (!(me->output) && 
                    509:       (me->output != stdout) && 
                    510: #ifndef _WINDOWS
                    511:       (me->output = fopen (me->outputfile, "w")) == NULL)
                    512:     {
                    513: #else
                    514:     (me->output = fopen (me->outputfile, "wb")) == NULL) 
                    515:     {
                    516: #endif /* !_WINDOWS */
                    517: 
                    518:       me->outputfile[0] = '\0';        /* file could not be opened */
1.85      cvs       519: #ifdef DEBUG_LIBWWW
                    520:       fprintf(stderr, "AHTOpen_file: couldn't open output stream for url %s\n", me->urlName);
                    521: #endif
1.83      cvs       522:       TtaSetStatus (me->docid, 1, 
                    523:                    TtaGetMessage (AMAYA, AM_CANNOT_CREATE_FILE),
                    524:                    me->outputfile);
                    525:       me->reqStatus = HT_ERR;
                    526:       return (HT_ERROR);
                    527:     }
                    528:          
                    529:   HTRequest_setOutputStream (me->request,
                    530:                             AHTFWriter_new (me->request, 
                    531:                                             me->output, YES));
                    532:   me->reqStatus = HT_WAITING;
                    533:   
                    534:   return HT_OK;
                    535: }
                    536: 
                    537: /*----------------------------------------------------------------------
1.17      cvs       538:   redirection_handler
                    539:   this function is registered to handle permanent and temporary
                    540:   redirections.
                    541:   ----------------------------------------------------------------------*/
1.4       cvs       542: #ifdef __STDC__
1.7       cvs       543: static int          redirection_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs       544: #else
                    545: static int          redirection_handler (request, context, status)
                    546: HTRequest          *request;
                    547: HTResponse         *response;
                    548: void               *param;
                    549: int                 status;
                    550: 
                    551: #endif
                    552: {
                    553: 
                    554:    HTAnchor           *new_anchor = HTResponse_redirection (response);
1.7       cvs       555:    AHTReqContext      *me = HTRequest_context (request);
1.4       cvs       556:    HTMethod            method = HTRequest_method (request);
1.98    ! cvs       557:    char               *ref;
        !           558:    char               *tmp;
1.4       cvs       559: 
1.7       cvs       560:    if (!new_anchor)
                    561:      {
                    562:        if (PROT_TRACE)
                    563:           HTTrace ("Redirection. No destination\n");
                    564:        return HT_OK;
                    565:      }
1.4       cvs       566: 
                    567:    /*
                    568:       ** Only do redirect on GET and HEAD
                    569:     */
1.7       cvs       570:    if (!HTMethod_isSafe (method))
                    571:      {
                    572:        HTAlertCallback    *prompt = HTAlert_find (HT_A_CONFIRM);
                    573:        if (prompt)
                    574:          {
                    575:             if ((*prompt) (request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
                    576:                            NULL, NULL, NULL) != YES)
                    577:                return HT_ERROR;
                    578:          }
                    579:      }
1.4       cvs       580: 
                    581:    /*
                    582:     **  Start new request with the redirect anchor found in the headers.
                    583:     **  Note that we reuse the same request object which means that we must
                    584:     **  keep this around until the redirected request has terminated. It also           
                    585:     **  allows us in an easy way to keep track of the number of redirections
                    586:     **  so that we can detect endless loops.
                    587:     */
1.17      cvs       588:    
1.7       cvs       589:    if (HTRequest_doRetry (request))
                    590:      {
                    591:        /* do we need to normalize the URL? */
                    592:        if (strncmp (new_anchor->parent->address, "http:", 5))
                    593:          {
                    594:             /* Yes, so we use the pre-redirection anchor as a base name */
1.74      cvs       595:             ref = AmayaParseUrl (new_anchor->parent->address, 
                    596:                                  me->urlName, AMAYA_PARSE_ALL);
1.7       cvs       597:             if (ref)
                    598:               {
1.98    ! cvs       599:                 HT_FREE (new_anchor->parent->address);
        !           600:                 tmp = NULL;
        !           601:                 HTSACopy (&tmp, ref);
        !           602:                 new_anchor->parent->address = tmp;
        !           603:                 TtaFreeMemory (ref);
1.7       cvs       604:               }
1.98    ! cvs       605:             else
        !           606:               return HT_ERROR; /* We can't redirect anymore */
1.7       cvs       607:          }
                    608: 
                    609:        /* update the current file name */
1.21      cvs       610:        if (strlen (new_anchor->parent->address) > (MAX_LENGTH - 2))
1.7       cvs       611:          {
1.74      cvs       612:             strncpy (me->urlName, new_anchor->parent->address, 
                    613:                      MAX_LENGTH - 1);
1.21      cvs       614:             me->urlName[MAX_LENGTH - 1] = EOS;
1.7       cvs       615:          }
                    616:        else
1.74      cvs       617:          strcpy (me->urlName, new_anchor->parent->address);
1.7       cvs       618: 
1.38      cvs       619:        ChopURL (me->status_urlName, me->urlName);
                    620: 
1.7       cvs       621:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_RED_FETCHING),
1.38      cvs       622:                      me->status_urlName);
1.7       cvs       623: 
                    624:        /* Start request with new credentials */
1.64      cvs       625: 
1.79      cvs       626:        if (HTRequest_outputStream (me->request) != NULL) {
1.85      cvs       627:          AHTFWriter_FREE (request->output_stream);
                    628:          if (me->output != stdout) { /* Are we writing to a file? */
1.74      cvs       629: #ifdef DEBUG_LIBWWW
1.79      cvs       630:          fprintf (stderr, "redirection_handler: New URL is  %s, closing "
1.74      cvs       631:                             "FILE %p\n", me->urlName, me->output); 
                    632: #endif 
1.79      cvs       633:            fclose (me->output);
                    634:            me->output = NULL;
                    635:          }
1.64      cvs       636:        }
1.68      cvs       637: 
1.69      cvs       638:        me->reqStatus = HT_NEW; /* reset the status */
1.7       cvs       639:        if (me->method == METHOD_PUT || me->method == METHOD_POST)      /* PUT, POST etc. */
1.74      cvs       640:          status = HTLoadAbsolute (me->urlName, request);
1.7       cvs       641:        else
1.74      cvs       642:          HTLoadAnchor (new_anchor, request);
1.7       cvs       643:      }
                    644:    else
1.68      cvs       645:      {
1.74      cvs       646:        HTRequest_addError (request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    647:                           NULL, 0, "HTRedirectFilter");
                    648:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REDIRECTIONS_LIMIT),
                    649:                     NULL);
                    650:        if (me->error_html)
                    651:         DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR; 
                    652:        /* so that we can show the error message */
1.68      cvs       653:      }
                    654: 
1.4       cvs       655:    /*
1.74      cvs       656:    **  By returning HT_ERROR we make sure that this is the last handler to be
                    657:    **  called. We do this as we don't want any other filter to delete the 
                    658:    **  request object now when we have just started a new one ourselves
                    659:    */
1.4       cvs       660:    return HT_ERROR;
                    661: }
                    662: 
1.5       cvs       663: /*----------------------------------------------------------------------
1.17      cvs       664:   terminate_handler
                    665:   this function is registered to handle the result of the request
1.5       cvs       666:   ----------------------------------------------------------------------*/
1.4       cvs       667: #if __STDC__
1.7       cvs       668: static int          terminate_handler (HTRequest * request, HTResponse * response, void *context, int status)
1.4       cvs       669: #else
                    670: static int          terminate_handler (request, response, context, status)
                    671: HTRequest          *request;
                    672: HTResponse         *response;
                    673: void               *context;
                    674: int                 status;
                    675: #endif
                    676: {
                    677:    AHTReqContext      *me = (AHTReqContext *) HTRequest_context (request);
1.13      cvs       678:    boolean             error_flag;
1.4       cvs       679: 
                    680:    if (!me)
1.74      cvs       681:      return HT_OK;             /* not an Amaya request */
                    682:    
1.80      cvs       683:    /* if Amaya was killed, treat with this request as if it were
                    684:       issued by a Stop button event */
                    685:    if (!AmayaIsAlive)           
                    686:       me->reqStatus = HT_ABORT; 
1.74      cvs       687:    
1.77      cvs       688:    if (status == HT_LOADED || 
                    689:        status == HT_CREATED || 
1.79      cvs       690:        status == HT_NO_DATA ||
                    691:        me->reqStatus == HT_ABORT)
1.74      cvs       692:      error_flag = FALSE;
1.13      cvs       693:    else
1.74      cvs       694:      error_flag = TRUE;
                    695:    
1.4       cvs       696:    /* output any errors from the server */
1.74      cvs       697:    
1.77      cvs       698:    /*
1.74      cvs       699:    ** me->output = output file which will receive an html file
                    700:    ** me->error_html = yes, output HTML errors in the screen
                    701:    ** request->error_stack == if there are any errors, they will be here
                    702:    ** me->error_stream_size If it's != 0 means an error message has already
                    703:    **                       been written to the stack
                    704:    */
                    705:    
1.4       cvs       706:    /* First, we verify if there are any errors and if they are not
1.17      cvs       707:    ** yet written to the error stack. If no, then let's try to write them
                    708:    ** ourselves
                    709:    */
1.74      cvs       710:    
                    711: #ifdef DEBUG_LIBWWW
                    712:    fprintf (stderr, "terminate_handler: URL is "
                    713:            "%s, closing FILE %p status is %d\n", me->urlName, me->output, 
                    714:            status); 
1.69      cvs       715: #endif
1.74      cvs       716:    
1.69      cvs       717:    if (me->output && me->output != stdout)
                    718:      {
1.74      cvs       719:        /* we are writing to a file */
                    720:        if (me->reqStatus != HT_ABORT)
                    721:         {                      /* if the request was not aborted and */
1.80      cvs       722:           if (error_flag &&
                    723:               me->error_html == TRUE)
                    724:               /* there were some errors and we want to print them */
                    725:             {          
                    726:               if (me->error_stream_size == 0)/* and the stream is empty */
                    727:                 AHTError_MemPrint (request); /* copy errors from 
1.74      cvs       728:                                                  **the error stack 
1.77      cvs       729:                                                  ** into the error stream */
1.80      cvs       730:               if (me->error_stream)
                    731:                 {      /* if the stream is non-empty */
                    732:                   fprintf (me->output, me->error_stream);/* output the errors */
1.85      cvs       733:                   /* Clear the error context, so that we can deal with
                    734:                      this answer as if it were a normal reply */
                    735:                    HTError_deleteAll( HTRequest_error (request));
                    736:                    HTRequest_setError (request, NULL);
                    737:                    error_flag = 0;
1.74      cvs       738:                 }
                    739:             }                  /* if error_stack */
1.85      cvs       740:         }
                    741: 
                    742:        /* if != HT_ABORT */
                    743:        
                    744: #ifdef DEBUG_LIBWWW       
                    745:        fprintf (stderr, "terminate_handler: URL is  %s, closing "
                    746:                "FILE %p\n", me->urlName, me->output); 
                    747: #endif
1.74      cvs       748:        fclose (me->output);
                    749:        me->output = NULL;
1.69      cvs       750:      }
1.80      cvs       751: 
1.74      cvs       752:    
1.80      cvs       753:    if (error_flag)
                    754:      me->reqStatus = HT_ERR;
                    755:    else if (me->reqStatus != HT_ABORT)
                    756:      me->reqStatus = HT_END;
                    757: 
1.88      cvs       758:    /* copy the content_type */
                    759:   
                    760:    {
                    761:     char *content_type = request->anchor->content_type->name;
                    762:  
                    763:    if (content_type && content_type [0] && me->content_type)  
                    764:      {
                    765:         /* libwww gives www/unknown when it gets an error. As this is 
                    766:            an HTML test, we force the type to text/html */
                    767:        if (!strcmp (content_type, "www/unknown"))
                    768:           {
                    769:            strcpy (me->content_type, "text/html");
                    770:           }
                    771:         else 
                    772:           { 
                    773:           strncpy (me->content_type, content_type, 
                    774:                    NAME_LENGTH -1);
                    775:            me->content_type [NAME_LENGTH-1] = '\0';
                    776:            
                    777:            /* Content-Type can be specified by a server's admin. To be on
                    778:               the safe side, we normalize its case */
                    779:            ConvertToLowerCase (me->content_type);
                    780:           } 
                    781: #ifdef DEBUG_LIBWWW
                    782:         fprintf (stderr, "content type is: %s\n", me->content_type);
                    783: #endif /* DEBUG_LIBWWW */
                    784:      }
                    785:    }
                    786:  
1.80      cvs       787:   /* don't remove or Xt will hang up during the PUT */
                    788: 
1.28      cvs       789:    if (AmayaIsAlive  && ((me->method == METHOD_POST) ||
                    790:                         (me->method == METHOD_PUT)))
1.7       cvs       791:      {
1.80      cvs       792:        PrintTerminateStatus (me, status);
                    793: 
                    794:      } 
                    795: 
1.72      cvs       796: #ifdef _WINDOWS
1.80      cvs       797:    /* Try to add this to AHTEventrg.c */
1.91      cvs       798:   ProcessTerminateRequest (me);
1.92      cvs       799:   /* kill the windows request here, of course */
1.80      cvs       800: #endif /* WINDOWS */
1.71      cvs       801: 
                    802:   return HT_OK;
1.4       cvs       803: }
                    804: 
1.5       cvs       805: /*----------------------------------------------------------------------
1.17      cvs       806:   AHTLoadTerminate_handler
1.74      cvs       807:   this is an application "AFTER" Callback. It's called by the library
                    808:   when a request has ended, so that we can setup the correct status.
1.5       cvs       809:   ----------------------------------------------------------------------*/
1.4       cvs       810: 
                    811: #ifdef __STDC__
1.7       cvs       812: static int          AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs       813: #else
                    814: static int          AHTLoadTerminate_handler (request, response, param, status)
                    815: HTRequest          *request;
                    816: HTResponse         *response;
                    817: void               *param;
                    818: int                 status;
1.69      cvs       819: 
1.4       cvs       820: #endif
                    821: {
                    822:    AHTReqContext      *me = HTRequest_context (request);
                    823:    HTAlertCallback    *cbf;
                    824:    AHTDocId_Status    *docid_status;
                    825: 
1.7       cvs       826:    switch (status)
                    827:         {
                    828:            case HT_LOADED:
                    829:               if (PROT_TRACE)
                    830:                  HTTrace ("Load End.... OK: `%s\' has been accessed\n",
1.38      cvs       831:                           me->status_urlName);
1.4       cvs       832: 
1.7       cvs       833:               docid_status = GetDocIdStatus (me->docid,
                    834:                                              Amaya->docid_status);
                    835: 
                    836:               if (docid_status != NULL && docid_status->counter > 1)
1.74      cvs       837:                  TtaSetStatus (me->docid, 1, 
                    838:                                TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
                    839:                                me->status_urlName);
1.7       cvs       840:               break;
                    841: 
                    842:            case HT_NO_DATA:
                    843:               if (PROT_TRACE)
1.74      cvs       844:                  HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n", 
                    845:                           me->status_urlName);
                    846:               TtaSetStatus (me->docid, 1, 
                    847:                             TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
1.38      cvs       848:                             me->status_urlName);
1.7       cvs       849:               break;
                    850: 
                    851:            case HT_INTERRUPTED:
                    852:               if (PROT_TRACE)
1.74      cvs       853:                  HTTrace ("Load End.... INTERRUPTED: `%s\'\n", 
                    854:                           me->status_urlName);
                    855:               TtaSetStatus (me->docid, 1, 
                    856:                             TtaGetMessage (AMAYA, AM_LOAD_ABORT), 
                    857:                             NULL);
1.7       cvs       858:               break;
                    859: 
                    860:            case HT_RETRY:
                    861:               if (PROT_TRACE)
                    862:                  HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
                    863:                           HTResponse_retryTime (response));
1.74      cvs       864:               TtaSetStatus (me->docid, 1, 
                    865:                             TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
1.38      cvs       866:                             me->status_urlName);
1.7       cvs       867:               break;
                    868: 
                    869:            case HT_ERROR:
                    870: 
                    871:               cbf = HTAlert_find (HT_A_MESSAGE);
                    872:               if (cbf)
                    873:                  (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    874:                          HTRequest_error (request), NULL);
                    875:               break;
                    876: 
                    877:               if (PROT_TRACE)
                    878:                  HTTrace ("Load End.... ERROR: Can't access `%s\'\n",
1.74      cvs       879:                           me->status_urlName ? me->status_urlName :"<UNKNOWN>");
                    880:               TtaSetStatus (me->docid, 1,
                    881:                             TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1.38      cvs       882:                             me->status_urlName ? me->status_urlName : "<UNKNOWN>");
1.7       cvs       883:               break;
                    884:            default:
                    885:               if (PROT_TRACE)
                    886:                  HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
                    887:               break;
                    888:         }
                    889: 
1.4       cvs       890:    return HT_OK;
                    891: }
                    892: 
1.27      cvs       893: /*----------------------------------------------------------------------
                    894:   AHTAcceptTypesInit
1.74      cvs       895:   This function prepares the Accept header used by Amaya during
                    896:   the HTTP content negotiation phase
1.27      cvs       897:   ----------------------------------------------------------------------*/
                    898: #ifdef __STDC__
                    899: static void           AHTAcceptTypesInit (HTList *c)
                    900: #else  /* __STDC__ */
                    901: static void           AHTAcceptTypesInit (c)
                    902: HTList             *c;
                    903: #endif /* __STDC__ */
                    904: {
                    905:   if (c == (HTList *) NULL) 
                    906:       return;
                    907: 
                    908:       /* define here all the mime types that Amaya can accept */
                    909: 
1.74      cvs       910:       HTConversion_add (c, "image/png",  "www/present", 
                    911:                        HTThroughLine, 1.0, 0.0, 0.0);
                    912:       HTConversion_add (c, "image/jpeg", "www/present", 
                    913:                        HTThroughLine, 1.0, 0.0, 0.0);
                    914:       HTConversion_add (c, "image/gif",  "www/present", 
                    915:                        HTThroughLine, 1.0, 0.0, 0.0);
                    916:       HTConversion_add (c, "image/xbm",  "www/present", 
                    917:                        HTThroughLine, 1.0, 0.0, 0.0);
                    918:       HTConversion_add (c, "image/xpm",  "www/present", 
                    919:                        HTThroughLine, 1.0, 0.0, 0.0);
                    920:       HTConversion_add (c, "application/postscript",  
                    921:                        "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1.4       cvs       922: 
1.27      cvs       923:    /* Define here the equivalences between MIME types and file extensions for
                    924:     the types that Amaya can display */
                    925: 
                    926:    /* Register the default set of file suffix bindings */
                    927:    HTFileInit ();
                    928: 
                    929:    /* Don't do any case distinction */
                    930:    HTBind_caseSensitive (FALSE);
                    931: }
1.4       cvs       932: 
1.5       cvs       933: /*----------------------------------------------------------------------
1.17      cvs       934:   AHTConverterInit
                    935:   Bindings between a source media type and a destination media type
                    936:   (conversion).
1.5       cvs       937:   ----------------------------------------------------------------------*/
1.15      cvs       938: #ifdef __STDC__
                    939: static void         AHTConverterInit (HTList *c)
                    940: #else  /* __STDC__ */
                    941: static void         AHTConverterInit (c)
                    942: HTList             *c;
                    943: #endif /* __STDC__ */
1.4       cvs       944: {
                    945: 
                    946:    /* Handler for custom http error messages */
1.7       cvs       947:    HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4       cvs       948: 
                    949:    /*
                    950:     ** These are converters that converts to something other than www/present,
                    951:     ** that is not directly outputting someting to the user on the screen
                    952:     */
                    953: 
                    954:    HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 1.0, 0.0, 0.0);
                    955:    HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter,
                    956:                     1.0, 0.0, 0.0);
                    957:    HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader,
                    958:                     1.0, 0.0, 0.0);
                    959:    HTConversion_add (c, "multipart/*", "*/*", HTBoundary,
                    960:                     1.0, 0.0, 0.0);
                    961:    HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML,
                    962:                     1.0, 0.0, 0.0);
                    963: 
                    964: 
                    965:    /*
                    966:       ** The following conversions are converting ASCII output from various
                    967:       ** protocols to HTML objects.
                    968:     */
                    969:    HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new,
                    970:                     1.0, 0.0, 0.0);
                    971:    HTConversion_add (c, "text/x-nntp-list", "*/*", HTNewsList,
                    972:                     1.0, 0.0, 0.0);
                    973:    HTConversion_add (c, "text/x-nntp-over", "*/*", HTNewsGroup,
                    974:                     1.0, 0.0, 0.0);
                    975: 
                    976: 
                    977:    /*
                    978:     ** We also register a special content type guess stream that can figure out
                    979:     ** the content type by reading the first bytes of the stream
                    980:     */
                    981:    HTConversion_add (c, "www/unknown", "*/*", HTGuess_new,
                    982:                     1.0, 0.0, 0.0);
                    983: 
                    984:    /*
                    985:       ** Register a persistent cache stream which can save an object to local
                    986:       ** file
                    987:     */
                    988:    HTConversion_add (c, "www/cache", "*/*", HTCacheWriter,
                    989:                     1.0, 0.0, 0.0);
                    990: 
                    991:    /*
                    992:       ** This dumps all other formats to local disk without any further
                    993:       ** action taken
                    994:     */
                    995:    HTConversion_add (c, "*/*", "www/present", HTSaveLocally,
                    996:                     0.3, 0.0, 0.0);
                    997: 
                    998: }
                    999: 
1.27      cvs      1000: 
1.15      cvs      1001: /*----------------------------------------------------------------------
1.17      cvs      1002:   AHTProtocolInit
                   1003:   Registers all amaya supported protocols.
1.15      cvs      1004:   ----------------------------------------------------------------------*/
1.4       cvs      1005: static void         AHTProtocolInit (void)
                   1006: {
                   1007: 
1.17      cvs      1008:    /* 
1.74      cvs      1009:       NB. Preemptive == YES means Blocking requests
                   1010:       Non-preemptive == NO means Non-blocking requests
1.17      cvs      1011:    */
1.4       cvs      1012: 
1.63      cvs      1013:    HTProtocol_add ("http", "buffered_tcp", NO, HTLoadHTTP, NULL);
1.4       cvs      1014:    /*   HTProtocol_add ("http", "tcp", NO, HTLoadHTTP, NULL); */
                   1015:    HTProtocol_add ("file", "local", NO, HTLoadFile, NULL);
                   1016:    HTProtocol_add ("cache", "local", NO, HTLoadCache, NULL);
1.36      cvs      1017:    HTProtocol_add ("ftp", "tcp", NO, HTLoadFTP, NULL);
1.17      cvs      1018: #if 0 /* experimental code */
1.4       cvs      1019:    HTProtocol_add ("telnet", "", YES, HTLoadTelnet, NULL);
                   1020:    HTProtocol_add ("tn3270", "", YES, HTLoadTelnet, NULL);
                   1021:    HTProtocol_add ("rlogin", "", YES, HTLoadTelnet, NULL);
                   1022:    HTProtocol_add ("nntp", "tcp", NO, HTLoadNews, NULL);
                   1023:    HTProtocol_add ("news", "tcp", NO, HTLoadNews, NULL);
1.17      cvs      1024: #endif
1.4       cvs      1025: }
                   1026: 
1.15      cvs      1027: /*----------------------------------------------------------------------
1.17      cvs      1028:   AHTNetInit
                   1029:   Reegisters "before" and "after" request filters.
1.15      cvs      1030:   ----------------------------------------------------------------------*/
1.4       cvs      1031: static void         AHTNetInit (void)
                   1032: {
                   1033: 
                   1034: /*      Register BEFORE filters
1.74      cvs      1035: **      The BEFORE filters handle proxies, caches, rule files etc.
                   1036: **      The filters are called in the order by which the are registered
                   1037: **      Not done automaticly - may be done by application!
                   1038: */
                   1039:   
1.4       cvs      1040: 
1.74      cvs      1041:   HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, 6);
                   1042:   HTNet_addBefore (HTProxyFilter, NULL, NULL, 10);
1.85      cvs      1043:   /*  HTNet_addBefore (AHTOpen_file, NULL, NULL, 11); */
                   1044:   HTHost_setActivateRequestCallback (AHTOpen_file);
1.4       cvs      1045: 
                   1046: /*      register AFTER filters
1.74      cvs      1047: **      The AFTER filters handle error messages, logging, redirection,
                   1048: **      authentication etc.
                   1049: **      The filters are called in the order by which the are registered
                   1050: **      Not done automaticly - may be done by application!
                   1051: */
1.4       cvs      1052: 
1.82      cvs      1053:    HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS, HT_FILTER_MIDDLE);
                   1054:    HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT, HT_FILTER_MIDDLE);
                   1055:    HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT, HT_FILTER_MIDDLE);
                   1056:    HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY, HT_FILTER_MIDDLE);
1.51      cvs      1057:    HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);      
                   1058:    /* handles all errors */
1.7       cvs      1059:    HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4       cvs      1060: }
                   1061: 
1.15      cvs      1062: /*----------------------------------------------------------------------
1.17      cvs      1063:   AHTAlertInit
                   1064:   Register alert messages and their callbacks.
1.15      cvs      1065:   ----------------------------------------------------------------------*/
                   1066: #ifdef __STDC__
                   1067: static void         AHTAlertInit (void)
                   1068: #else
                   1069: static void         AHTAlertInit ()
                   1070: #endif
                   1071: {
                   1072:    HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.70      cvs      1073: #  ifndef _WINDOWS
1.83      cvs      1074:    /*   HTAlert_add ((HTAlertCallback *) Add_NewSocket_to_Loop, HT_PROG_CONNECT); */
1.70      cvs      1075: #  else  /* _WINDOWS */
                   1076:    HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
                   1077: #  endif /* _WINDOWS */
1.15      cvs      1078:    HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48      cvs      1079:    HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG));       /* process all messages except debug ones*/
1.15      cvs      1080:    HTAlert_add (AHTConfirm, HT_A_CONFIRM);
                   1081:    HTAlert_add (AHTPrompt, HT_A_PROMPT);
                   1082:    HTAlert_add (AHTPromptPassword, HT_A_SECRET);
                   1083:    HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
                   1084: }
                   1085: 
                   1086: /*----------------------------------------------------------------------
1.97      cvs      1087:   ProxyInit
                   1088:   Reads any proxies settings which may be declared as environmental
                   1089:   variables or in the thot.ini file. The former overrides the latter.
                   1090:   ----------------------------------------------------------------------*/
                   1091: static void ProxyInit (void)
                   1092: {
                   1093:   char *strptr;
                   1094:   char *str = NULL;
                   1095:   char *name;
                   1096: 
                   1097:   /* get the proxy settings from the thot.ini file */
                   1098:   strptr = (char *) TtaGetEnvString ("HTTP_PROXY");
                   1099:   if (strptr && *strptr)
                   1100:     HTProxy_add ("http", strptr);
                   1101:   /* get the no_proxy settings from the thot.ini file */
                   1102:   strptr = (char *) TtaGetEnvString ("NO_PROXY");
                   1103:   if (strptr && *strptr) 
                   1104:     {
                   1105:       str = TtaStrdup (strptr);          /* Get copy we can mutilate */
                   1106:       strptr = str;
                   1107:       while ((name = HTNextField (&strptr)) != NULL) {
                   1108:        char *portstr = strchr (name, ':');
                   1109:        unsigned port=0;
                   1110:        if (portstr) {
                   1111:          *portstr++ = '\0';
                   1112:          if (*portstr) port = (unsigned) atoi(portstr);
                   1113:        }
                   1114:        /* Register it for all access methods */
                   1115:        HTNoProxy_add (name, NULL, port);
                   1116:       }
                   1117:       TtaFreeMemory (str);
                   1118:     }
                   1119:   
                   1120:   /* use libw3's routine to get all proxy settings from the environment */
                   1121:    HTProxy_getEnvVar ();
                   1122: 
                   1123: }
                   1124: 
                   1125: 
                   1126: /*----------------------------------------------------------------------
1.17      cvs      1127:   AHTProfile_newAmaya
                   1128:   creates the Amaya client profile for libwww.
1.15      cvs      1129:   ----------------------------------------------------------------------*/
                   1130: #ifdef __STDC__
                   1131: static void         AHTProfile_newAmaya (char *AppName, char *AppVersion)
                   1132: #else  /* __STDC__ */
                   1133: static void         AHTProfile_newAmaya (AppName, AppVersion)
                   1134: char               *AppName;
                   1135: char               *AppVersion;
                   1136: #endif /* __STDC__ */
1.4       cvs      1137: {
                   1138:    /* If the Library is not already initialized then do it */
                   1139:    if (!HTLib_isInitialized ())
                   1140:       HTLibInit (AppName, AppVersion);
                   1141: 
                   1142:    if (!converters)
                   1143:       converters = HTList_new ();
1.27      cvs      1144:    if (!acceptTypes)
                   1145:       acceptTypes = HTList_new ();
1.4       cvs      1146:    if (!encodings)
                   1147:       encodings = HTList_new ();
                   1148: 
                   1149:    /* Register the default set of transport protocols */
                   1150:    HTTransportInit ();
                   1151: 
                   1152:    /* Register the default set of application protocol modules */
                   1153:    AHTProtocolInit ();
                   1154: 
                   1155:    /* Enable the persistent cache */
                   1156:    /*   HTCacheInit (NULL, 20); */
                   1157: 
                   1158:    /* Register the default set of BEFORE and AFTER filters */
                   1159:    AHTNetInit ();
                   1160: 
                   1161:    /* Set up the default set of Authentication schemes */
                   1162:    HTAAInit ();
                   1163: 
1.97      cvs      1164:    /* Get any proxy settings */
                   1165:    ProxyInit ();
1.4       cvs      1166: 
                   1167:    /* Register the default set of converters */
                   1168:    AHTConverterInit (converters);
1.27      cvs      1169:    AHTAcceptTypesInit (acceptTypes);
1.4       cvs      1170:    HTFormat_setConversion (converters);
                   1171: 
                   1172:    /* Register the default set of transfer encoders and decoders */
                   1173:    HTEncoderInit (encodings);  /* chunks ??? */
                   1174:    HTFormat_setTransferCoding (encodings);
                   1175: 
                   1176:    /* Register the default set of MIME header parsers */
1.74      cvs      1177:    HTMIMEInit ();   /* must be called again for language selector */
1.4       cvs      1178: 
                   1179:    /* Register the default set of Icons for directory listings */
1.27      cvs      1180:    /*HTIconInit(NULL); *//* experimental */
1.4       cvs      1181: 
                   1182:    /* Register the default set of messages and dialog functions */
                   1183:    AHTAlertInit ();
                   1184:    HTAlert_setInteractive (YES);
                   1185: }
                   1186: 
1.5       cvs      1187: /*----------------------------------------------------------------------
1.17      cvs      1188:   AHTProfile_delete
                   1189:   deletes the Amaya client profile.
1.5       cvs      1190:   ----------------------------------------------------------------------*/
1.4       cvs      1191: #ifdef __STDC__
                   1192: static void         AHTProfile_delete (void)
                   1193: #else
                   1194: static void         AHTProfile_delete ()
1.7       cvs      1195: #endif                         /* __STDC__ */
1.4       cvs      1196: {
1.22      cvs      1197:  
                   1198:   /* free the Amaya global context */
1.74      cvs      1199:   if (!converters)
                   1200:     HTConversion_deleteAll (converters);
                   1201:   if (!acceptTypes)
                   1202:     HTConversion_deleteAll (acceptTypes);
                   1203:   if (!encodings)
                   1204:     HTCoding_deleteAll (encodings);
                   1205:   
                   1206:   HTList_delete (Amaya->docid_status);
                   1207:   HTList_delete (Amaya->reqlist);
                   1208:   TtaFreeMemory (Amaya);
                   1209:   {
1.61      cvs      1210: 
1.74      cvs      1211:     if (HTLib_isInitialized ())
                   1212:       
1.61      cvs      1213: #  ifdef _WINDOWS
                   1214:       HTEventTerminate ();
                   1215: #  endif _WINDOWS;             
1.74      cvs      1216:     
                   1217:     /* Clean up the persistent cache (if any) */
                   1218:     HTCacheTerminate ();
                   1219:     
                   1220:     /* Clean up all the global preferences */
                   1221:     HTFormat_deleteAll ();
                   1222:     
                   1223:     /* Terminate libwww */
                   1224:     HTLibTerminate ();
                   1225:   }
1.4       cvs      1226: }
                   1227: 
1.5       cvs      1228: /*----------------------------------------------------------------------
1.17      cvs      1229:   QueryInit
                   1230:   initializes the libwww interface 
1.5       cvs      1231:   ----------------------------------------------------------------------*/
1.4       cvs      1232: #ifdef __STDC__
                   1233: void                QueryInit ()
                   1234: #else
                   1235: void                QueryInit ()
                   1236: #endif
                   1237: {
                   1238: 
1.24      cvs      1239:    AmayaIsAlive = TRUE;
1.4       cvs      1240:    AHTProfile_newAmaya (HTAppName, HTAppVersion);
                   1241: 
1.69      cvs      1242:    /* New AHTBridge stuff */
1.4       cvs      1243: 
1.54      cvs      1244: #  ifdef _WINDOWS
1.75      cvs      1245:    AHTEventInit ();
1.54      cvs      1246: #  endif _WINDOWS;
1.72      cvs      1247: 
1.70      cvs      1248:    HTEvent_setRegisterCallback (AHTEvent_register);
1.78      cvs      1249:    /*** a effacer ***/
                   1250:    HTEvent_setUnregisterCallback (AHTEvent_unregister);
                   1251:        /***  ***/
1.4       cvs      1252: 
1.72      cvs      1253: #  ifndef _WINDOWS
                   1254:    HTEvent_setUnregisterCallback (AHTEvent_unregister);
                   1255: #  endif /* _WINDOWS */
                   1256: 
1.74      cvs      1257: #ifdef DEBUG_LIBWWW
                   1258:   WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | PROT_TRACE;
                   1259: #endif
1.4       cvs      1260: 
                   1261:    /* Trace activation (for debugging) */
1.7       cvs      1262:    /*
1.4       cvs      1263:       WWW_TraceFlag = SHOW_APP_TRACE | SHOW_UTIL_TRACE |
                   1264:       SHOW_BIND_TRACE | SHOW_THREAD_TRACE |
                   1265:       SHOW_STREAM_TRACE | SHOW_PROTOCOL_TRACE |
                   1266:       SHOW_URI_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1267:       SHOW_CORE_TRACE;
                   1268: 
1.7       cvs      1269:     */
1.4       cvs      1270: 
                   1271:    /***
                   1272:      WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1273:      SHOW_PROTOCOL_TRACE| SHOW_APP_TRACE | SHOW_UTIL_TRACE;
                   1274:      ***/
                   1275: 
                   1276:    /* Setting up other user interfaces */
                   1277: 
                   1278:    /* Setting up different network parameters */
1.17      cvs      1279:    /* Maximum number of simultaneous open sockets */
1.4       cvs      1280:    HTNet_setMaxSocket (8);
1.75      cvs      1281:    /* different network services timeouts */
1.4       cvs      1282:    HTDNS_setTimeout (3600);
1.75      cvs      1283: #ifdef _WINDOWS
                   1284:    /* under windows, the libwww persistent socket handling has
                   1285:    ** some bugs. The following line inhibits idle socket reusal.
                   1286:    ** this is a bit slower, but avoids crashes and gives us time
                   1287:    ** to distribute Amaya before having to patch up libwww.
                   1288:    */
1.76      cvs      1289:    HTHost_setPersistTimeout (-1L);
1.75      cvs      1290: #else
1.76      cvs      1291:    HTHost_setPersistTimeout (60L);
1.75      cvs      1292: #endif /* _WINDOWS */
                   1293: 
1.17      cvs      1294:    /* Cache is disabled in this version */
1.4       cvs      1295:    HTCacheMode_setEnabled (0);
                   1296: 
                   1297:    /* Initialization of the global context */
                   1298:    Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
                   1299:    Amaya->reqlist = HTList_new ();
                   1300:    Amaya->docid_status = HTList_new ();
                   1301:    Amaya->open_requests = 0;
1.74      cvs      1302:    
1.4       cvs      1303: #ifdef CATCH_SIG
1.18      cvs      1304:    signal (SIGPIPE, SIG_IGN);
1.4       cvs      1305: #endif
1.94      cvs      1306: 
                   1307: #if !defined(AMAYA_JAVA) && !defined(AMAYA_ILU)        && defined (_WINDOWS)
                   1308:   /* forces libwww to do sync request if true and if in MakeBook mode */
                   1309:   WinMakeBookFlag = FALSE;
                   1310: #endif /* _WINDOWS */
1.15      cvs      1311: }
                   1312: 
1.69      cvs      1313: #ifndef _WINDOWS
1.15      cvs      1314: /*----------------------------------------------------------------------
1.17      cvs      1315:   LoopForStop
                   1316:   a copy of the Thop event loop so we can handle the stop button.
1.69      cvs      1317:   Not useful for windows code (Ramzi).
1.15      cvs      1318:   ----------------------------------------------------------------------*/
                   1319: #ifdef __STDC__
                   1320: static int          LoopForStop (AHTReqContext * me)
                   1321: #else
                   1322: static int          LoopForStop (AHTReqContext * me)
                   1323: #endif
                   1324: {
                   1325: 
1.25      cvs      1326:    extern ThotAppContext app_cont;
1.51      cvs      1327:    XEvent                ev;
                   1328:    XtInputMask           status;
1.17      cvs      1329:    int                 status_req = HT_OK;
1.15      cvs      1330: 
                   1331:    /* to test the async calls  */
1.17      cvs      1332:    /* Loop while waiting for new events, exists when the request is over */
1.15      cvs      1333:    while (me->reqStatus != HT_ABORT &&
                   1334:          me->reqStatus != HT_END &&
1.69      cvs      1335:          me->reqStatus != HT_ERR) {
                   1336:         if (!AmayaIsAlive)
                   1337:            /* Amaya was killed by one of the callback handlers */
                   1338:            exit (0);
                   1339: 
                   1340:         status = XtAppPending (app_cont);
                   1341:         if (status & XtIMXEvent) {
                   1342:             XtAppNextEvent (app_cont, &ev);
                   1343:            TtaHandleOneEvent (&ev);
                   1344:         } else if (status & (XtIMAll & (~XtIMXEvent))) {
                   1345:                 XtAppProcessEvent (app_cont, (XtIMAll & (~XtIMXEvent)));
                   1346:         } else {
                   1347:                XtAppNextEvent (app_cont, &ev);
                   1348:               TtaHandleOneEvent (&ev);
                   1349:         }
                   1350:    }
1.4       cvs      1351: 
1.69      cvs      1352:    switch (me->reqStatus) {
                   1353:          case HT_ERR:
                   1354:           case HT_ABORT:
1.15      cvs      1355:               status_req = HT_ERROR;
                   1356:               break;
                   1357: 
1.69      cvs      1358:          case HT_END:
1.15      cvs      1359:               status_req = HT_OK;
                   1360:               break;
                   1361: 
1.69      cvs      1362:          default:
1.15      cvs      1363:               break;
1.69      cvs      1364:    }
1.15      cvs      1365:    return (status_req);
1.4       cvs      1366: }
1.69      cvs      1367: #endif /* _WINDOWS */
1.4       cvs      1368: 
1.5       cvs      1369: /*----------------------------------------------------------------------
1.15      cvs      1370:   QueryClose
1.21      cvs      1371:   closes all existing threads, frees all non-automatically deallocated
                   1372:   memory and then ends libwww.
1.5       cvs      1373:   ----------------------------------------------------------------------*/
1.4       cvs      1374: void                QueryClose ()
                   1375: {
1.24      cvs      1376: 
                   1377:    AmayaIsAlive = FALSE;
                   1378: 
1.21      cvs      1379:    /* remove all the handlers and callbacks that may output a message to
                   1380:       a non-existent Amaya window */
                   1381: 
                   1382:    HTNet_deleteAfter (AHTLoadTerminate_handler);
                   1383:    HTNet_deleteAfter (redirection_handler);
                   1384:    HTAlertCall_deleteAll (HTAlert_global () );
1.23      cvs      1385:    HTAlert_setGlobal ((HTList *) NULL);
1.24      cvs      1386:    HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
1.27      cvs      1387:    HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
1.87      cvs      1388:     HTHost_setActivateRequestCallback (NULL);
1.4       cvs      1389:    Thread_deleteAll ();
1.21      cvs      1390:  
1.4       cvs      1391:    HTProxy_deleteAll ();
                   1392:    HTNoProxy_deleteAll ();
                   1393:    HTGateway_deleteAll ();
                   1394:    AHTProfile_delete ();
                   1395: }
                   1396: 
1.5       cvs      1397: /*----------------------------------------------------------------------
1.15      cvs      1398:    GetObjectWWW
1.17      cvs      1399:    this function requests a resource designated by a URLname into a
                   1400:    temporary filename. The download can come from a simple GET operation,
                   1401:    or can come from POSTING/GETTING a form. In the latter
                   1402:    case, the function receives a query string to send to the server.
                   1403: 
1.5       cvs      1404:    4  file retrieval modes are proposed:                              
                   1405:    AMAYA_SYNC : blocking mode                            
                   1406:    AMAYA_ISYNC : incremental, blocking mode              
                   1407:    AMAYA_ASYNC : non-blocking mode                       
                   1408:    AMAYA_IASYNC : incremental, non-blocking mode         
                   1409:    
                   1410:    In the incremental mode, each time a package arrives, it will be   
                   1411:    stored in the temporary file. In addition, if an                   
                   1412:    incremental_callback function is defined, this function will be    
                   1413:    called and handled a copy of the newly received data package.      
                   1414:    Finally, if a terminate_callback function is defined, it will be   
                   1415:    invoked when the request terminates. The caller of this function
1.4       cvs      1416:    can define two different contexts to be passed to the callback
                   1417:    functions.
                   1418: 
                   1419:    When the function is called with the SYNC mode, the function will
                   1420:    return only when the requested file has been loaded.
                   1421:    The ASYNC mode will immediately return after setting up the
                   1422:    call.
                   1423: 
                   1424:    Notes:
                   1425:    At the end of a succesful request, the urlName string contains the
                   1426:    name of the actually retrieved URL. As a URL can change over the time,
                   1427:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      1428:    caller verify the value of the urlName variable at the end of
1.4       cvs      1429:    a request.
                   1430: 
                   1431:    Inputs:
                   1432:    - docid  Document identifier for the set of objects being
                   1433:    retrieved.
                   1434:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   1435:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   1436:    - mode The retrieval mode.
                   1437:    - incremental_cbf 
                   1438:    - context_icbf
                   1439:    Callback and context for the incremental modes
                   1440:    - terminate_cbf 
                   1441:    - context_icbf
                   1442:    Callback and context for a terminate handler
1.17      cvs      1443:    -error_html if TRUE, then display any server error message as an
                   1444:    HTML document.
1.88      cvs      1445:    - content_type a string
                   1446:  
1.4       cvs      1447:    Outputs:
                   1448:    - urlName The URL that was retrieved
                   1449:    - outputfile The name of the temporary file which holds the
                   1450:    retrieved data. (Only in case of success)
1.88      cvs      1451:    - if content_type wasn't NULL, it will contain a copy of the parameter
                   1452:      sent in the HTTP answer
1.4       cvs      1453:    Returns:
                   1454:    HT_ERROR
                   1455:    HT_OK
1.5       cvs      1456:  
                   1457:   ----------------------------------------------------------------------*/
1.4       cvs      1458: #ifdef __STDC__
1.52      cvs      1459: int GetObjectWWW (int docid, char* urlName, char* postString, char* outputfile, int mode,
                   1460:                  TIcbf* incremental_cbf, void* context_icbf, TTcbf* terminate_cbf, 
1.88      cvs      1461:                  void* context_tcbf, boolean error_html, char *content_type)
1.4       cvs      1462: #else
1.52      cvs      1463: int GetObjectWWW (docid, urlName, postString, outputfile, mode, incremental_cbf, context_icbf, 
1.88      cvs      1464:                  terminate_cbf, context_tcbf, error_html, content_type)
1.73      cvs      1465: int           docid;
                   1466: char         *urlName;
                   1467: char         *postString;
                   1468: char         *outputfile;
                   1469: int           mode;
                   1470: TIcbf        *incremental_cbf;
                   1471: void         *context_icbf;
                   1472: TTcbf        *terminate_cbf;
                   1473: void         *context_tcbf;
                   1474: boolean       error_html;
1.88      cvs      1475: char        *content_type;
1.4       cvs      1476: #endif
                   1477: {
                   1478:    AHTReqContext      *me;
                   1479:    char               *ref;
                   1480:    int                 status;
1.7       cvs      1481: 
1.69      cvs      1482:    if (urlName == NULL || docid == 0 || outputfile == NULL) {
                   1483:       /* no file to be loaded */
                   1484:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   1485:        
                   1486:       if (error_html)
                   1487:         /* so we can show the error message */
                   1488:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   1489:       return HT_ERROR;
                   1490:       
                   1491:    }
1.7       cvs      1492: 
1.4       cvs      1493:    /* do we support this protocol? */
1.69      cvs      1494:    if (IsValidProtocol (urlName) == NO) {
                   1495:       /* return error */
                   1496:       outputfile[0] = EOS;     /* file could not be opened */
                   1497:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL), urlName);
                   1498:       
                   1499:       if (error_html)
                   1500:         /* so we can show the error message */
                   1501:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   1502:       return HT_ERROR;
                   1503:    }
1.4       cvs      1504: 
1.58      cvs      1505:    /*create a tempfilename */
1.59      cvs      1506:    sprintf (outputfile, "%s%c%d%c%04dAM", TempFileDirectory, DIR_SEP, docid, DIR_SEP, object_counter);
1.4       cvs      1507: 
                   1508:    /* update the object_counter */
                   1509:    object_counter++;
                   1510:    /* normalize the URL */
1.45      cvs      1511:    ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4       cvs      1512:    /* should we abort the request if we could not normalize the url? */
1.69      cvs      1513:    if (ref == (char*) NULL || ref[0] == EOS) {
                   1514:       /*error */
                   1515:       outputfile[0] = EOS;
                   1516:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   1517:       
                   1518:       if (error_html)
                   1519:         /* so we can show the error message */
                   1520:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.4       cvs      1521: 
1.69      cvs      1522:       return HT_ERROR;
                   1523:    }
1.4       cvs      1524:    /* verify if that file name existed */
1.9       cvs      1525:    if (TtaFileExist (outputfile))
1.77      cvs      1526:      TtaFileUnlink (outputfile);
1.4       cvs      1527: 
                   1528:    /* Initialize the request structure */
                   1529:    me = AHTReqContext_new (docid);
1.69      cvs      1530:    if (me == NULL) {
1.77      cvs      1531:      outputfile[0] = EOS;
                   1532:      /* need an error message here */
                   1533:      TtaFreeMemory (ref);
                   1534:      return (HT_ERROR);
1.69      cvs      1535:    }
1.77      cvs      1536:    
1.4       cvs      1537:    /* Specific initializations for POST and GET */
1.69      cvs      1538:    if (mode & AMAYA_FORM_POST) {
1.77      cvs      1539:      me->method = METHOD_POST;
                   1540:      if (postString) {
                   1541:        me->mem_ptr = postString;
                   1542:        me->block_size = strlen (postString);
                   1543:      } else {
                   1544:        me->mem_ptr = "";
                   1545:        me->block_size = 0;
                   1546:      }
                   1547:      HTRequest_setMethod (me->request, METHOD_POST);
1.69      cvs      1548:       HTRequest_setPostCallback (me->request, AHTUpload_callback);
                   1549:    } else {
1.77      cvs      1550:      me->method = METHOD_GET;
                   1551:      me->dest = (HTParentAnchor *) NULL;       /*useful only for PUT and POST methods */
                   1552:      if (!HasKnownFileSuffix (ref))
                   1553:        HTRequest_setConversion(me->request, acceptTypes, TRUE);
1.69      cvs      1554:    }
1.93      cvs      1555: 
                   1556: #if !defined(AMAYA_JAVA) && !defined(AMAYA_ILU)        && defined (_WINDOWS)
                   1557:    /* forces libwww to do sync request if in MakeBook mode */
                   1558:    if (WinMakeBookFlag)
                   1559:           mode = AMAYA_SYNC;
                   1560: #endif /* _WINDOWS */
                   1561: 
1.4       cvs      1562:    /* Common initialization */
                   1563:    me->mode = mode;
                   1564:    me->error_html = error_html;
                   1565:    me->incremental_cbf = incremental_cbf;
                   1566:    me->context_icbf = context_icbf;
                   1567:    me->terminate_cbf = terminate_cbf;
                   1568:    me->context_tcbf = context_tcbf;
1.88      cvs      1569:    me->content_type = content_type;
1.64      cvs      1570: 
1.69      cvs      1571:    /* for the async. request modes, we need to have our
1.4       cvs      1572:       own copy of outputfile and urlname
                   1573:     */
                   1574: 
1.69      cvs      1575:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) {
1.77      cvs      1576:      char* tmp;
                   1577:      
1.69      cvs      1578:       tmp = TtaGetMemory (strlen (outputfile) + 1);
                   1579:       strcpy (tmp, outputfile);
                   1580:       me->outputfile = tmp;
                   1581:       
                   1582:       tmp = TtaGetMemory (MAX_LENGTH + 1);
                   1583:       strncpy (tmp, urlName, MAX_LENGTH);
                   1584:       tmp[MAX_LENGTH] = EOS;
                   1585:       me->urlName = tmp;
                   1586: #  ifdef _WINDOWS
                   1587:       HTRequest_setPreemptive (me->request, NO);
                   1588:    } else {
1.77      cvs      1589:      me->outputfile = outputfile;
                   1590:      me->urlName = urlName;
                   1591:      HTRequest_setPreemptive (me->request, YES);
1.61      cvs      1592:    }
1.69      cvs      1593: #  else /* _WINDOWS */
                   1594:    } else {
1.77      cvs      1595:      me->outputfile = outputfile;
                   1596:      me->urlName = urlName;
1.61      cvs      1597:    }
1.77      cvs      1598:      /***
1.57      cvs      1599:      Change for taking into account the stop button:
                   1600:      The requests will be always asynchronous, however, if mode=AMAYA_SYNC,
                   1601:      we will loop until the document has been received or a stop signal
                   1602:      generated
1.77      cvs      1603:      ****/
                   1604:      HTRequest_setPreemptive (me->request, NO);
1.69      cvs      1605: #  endif /* _WINDOWS */
1.61      cvs      1606: 
                   1607:    /* prepare the URLname that will be displayed in teh status bar */
                   1608:    ChopURL (me->status_urlName, me->urlName);
                   1609: 
1.77      cvs      1610:    TtaSetStatus (me->docid, 1, 
                   1611:                 TtaGetMessage (AMAYA, AM_FETCHING),
                   1612:                 me->status_urlName);
1.4       cvs      1613: 
                   1614:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45      cvs      1615:    TtaFreeMemory (ref);
1.4       cvs      1616: 
1.69      cvs      1617:    if (mode & AMAYA_FORM_POST) {
                   1618:       HTAnchor_setFormat ((HTParentAnchor *) me->anchor, HTAtom_for ("application/x-www-form-urlencoded"));
                   1619:       HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
                   1620:       HTRequest_setEntityAnchor (me->request, me->anchor);
                   1621:       
                   1622:       status = HTLoadAbsolute (urlName, me->request);
                   1623:    } else
1.77      cvs      1624:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      1625: 
1.72      cvs      1626: #ifndef _WINDOWS
1.77      cvs      1627:    if (status == HT_ERROR ||
                   1628:     me->reqStatus == HT_END ||
                   1629:     me->reqStatus == HT_ERR)
                   1630:    {
                   1631:      /* in case of error, free all allocated memory and exit */
1.96      cvs      1632:      if (me->output && me->output != stdout) {
1.85      cvs      1633: #ifdef DEBUG_LIBWWW      
1.96      cvs      1634:        fprintf (stderr, "GetObjectWWW:: URL is  %s, closing "
1.85      cvs      1635:                "FILE %p\n", me->urlName, me->output); 
                   1636: #endif
1.77      cvs      1637:        fclose (me->output);
1.82      cvs      1638:        me->output = NULL;
                   1639:      }
                   1640:      
1.72      cvs      1641:       if (me->reqStatus == HT_ERR) {
1.77      cvs      1642:        status = HT_ERROR;
                   1643:        /* show an error message on the status bar */
                   1644:        DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
                   1645:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD), me->
                   1646:                      status_urlName);
1.72      cvs      1647:       } else
1.77      cvs      1648:        status = HT_OK;
1.72      cvs      1649: 
                   1650:       AHTReqContext_delete (me);
1.77      cvs      1651:  } else {
1.72      cvs      1652:          /* part of the stop button handler */
                   1653:          if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC)) {
                   1654:             status = LoopForStop (me);
                   1655:            AHTReqContext_delete (me);
                   1656:          }
                   1657:    }
                   1658: 
                   1659: #else  /* !_WINDOWS */
                   1660: 
1.90      cvs      1661:    if (status == HT_ERROR ||
                   1662:           me->reqStatus == HT_ERROR)
                   1663:      {
                   1664:           status = HT_ERROR;
1.71      cvs      1665: 
1.90      cvs      1666:        /* in case of error, close any open files, free all allocated
                   1667:              memory and exit */
                   1668:        if (me->output && me->output != stdout)
                   1669:             {
1.85      cvs      1670: #ifdef DEBUG_LIBWWW      
1.90      cvs      1671:            fprintf (stderr, "GetObjectWWW: URL is  %s, closing "
                   1672:                    "FILE %p\n", me->urlName, me->output); 
1.85      cvs      1673: #endif
1.90      cvs      1674:            fclose (me->output);
                   1675:            me->output = NULL;
                   1676:          }
1.85      cvs      1677:      
1.90      cvs      1678:        if (me->reqStatus == HT_ERR) 
                   1679:             {
                   1680:              /* show an error message on the status bar */
                   1681:              DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
                   1682:              TtaSetStatus (me->docid, 1, 
                   1683:                  TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                   1684:                  me->status_urlName);
                   1685:          }
                   1686:      } 
                   1687:        else 
                   1688:          {
                   1689:            status = HT_OK;
                   1690:       }
                   1691:  
1.93      cvs      1692:    /* part of the stop button handler */
                   1693:      if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   1694:        {
                   1695:         if (HTRequest_net (me->request))
                   1696:            HTRequest_kill (me->request);
                   1697:         else
                   1698:            AHTReqContext_delete (me);
                   1699:        }
1.4       cvs      1700: 
1.72      cvs      1701: #endif /* !_WINDOWS */
                   1702: 
1.4       cvs      1703:    return (status);
                   1704: }
                   1705: 
1.5       cvs      1706: /*----------------------------------------------------------------------
1.17      cvs      1707:    PutObjectWWW
                   1708:    frontend for uploading a resource to a URL. This function downloads
                   1709:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   1710:    finish the job.
                   1711: 
1.5       cvs      1712:    2 upload modes are proposed:                                       
                   1713:    AMAYA_SYNC : blocking mode                            
                   1714:    AMAYA_ASYNC : non-blocking mode                       
                   1715:    
1.4       cvs      1716:    When the function is called with the SYNC mode, the function will
                   1717:    return only when the file has been uploaded.
                   1718:    The ASYNC mode will immediately return after setting up the
                   1719:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   1720:    call back terminate_cbf, handling it the context defined in
                   1721:    context_tcbf.
                   1722: 
                   1723:    Notes:
                   1724:    At the end of a succesful request, the urlName string contains the
                   1725:    name of the actually uploaded URL. As a URL can change over the time,
                   1726:    (e.g., be redirected elsewhere), it is advised that the function
                   1727:    caller verifies the value of the urlName variable at the end of
                   1728:    a request.
                   1729: 
                   1730:    Inputs:
                   1731:    - docid  Document identifier for the set of objects being
                   1732:    retrieved.
                   1733:    - fileName A pointer to the local file to upload
                   1734:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   1735:    - mode The retrieval mode.
                   1736:    - terminate_cbf 
                   1737:    - context_icbf
                   1738:    Callback and context for a terminate handler
                   1739: 
                   1740:    Outputs:
                   1741:    - urlName The URL that was uploaded
                   1742: 
                   1743:    Returns:
                   1744:    HT_ERROR
                   1745:    HT_OK
1.5       cvs      1746:   ----------------------------------------------------------------------*/
1.4       cvs      1747: #ifdef __STDC__
1.27      cvs      1748: int                 PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4       cvs      1749:                                  TTcbf * terminate_cbf, void *context_tcbf)
                   1750: #else
1.26      cvs      1751: int                 PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4       cvs      1752:                                  ,terminate_cbf, context_tcbf)
                   1753: int                 docid;
                   1754: char               *urlName;
                   1755: char               *fileName;
                   1756: int                 mode;
1.27      cvs      1757: PicType             contentType;
1.4       cvs      1758: TTcbf              *terminate_cbf;
                   1759: void               *context_tcbf;
                   1760: 
                   1761: #endif
                   1762: {
1.7       cvs      1763:    /*AHTReqContext      *me; */
1.4       cvs      1764:    int                 status;
                   1765: 
                   1766:    int                 fd;
                   1767:    struct stat         file_stat;
                   1768:    char               *mem_ptr;
                   1769:    unsigned long       block_size;
                   1770: 
1.33      cvs      1771:    AmayaLastHTTPErrorMsg [0] = EOS;
                   1772:    
1.4       cvs      1773:    if (urlName == NULL || docid == 0 || fileName == NULL ||
1.9       cvs      1774:        !TtaFileExist (fileName))
1.4       cvs      1775:       /* no file to be uploaded */
                   1776:       return HT_ERROR;
                   1777: 
                   1778:    /* do we support this protocol? */
1.7       cvs      1779:    if (IsValidProtocol (urlName) == NO)
                   1780:      {
                   1781:        /* return error */
1.77      cvs      1782:        TtaSetStatus (docid, 1, 
                   1783:                       TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7       cvs      1784:                      urlName);
                   1785:        return HT_ERROR;
                   1786:      }
1.4       cvs      1787:    /* read the file into memory */
1.70      cvs      1788: #  ifndef _WINDOWS
1.7       cvs      1789:    if ((fd = open (fileName, O_RDONLY)) == -1)
1.70      cvs      1790: #  else /* _WINDOWS */
1.74      cvs      1791:    if ((fd = open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.70      cvs      1792: #  endif /* _WINDOWS */
1.7       cvs      1793:      {
                   1794:        /* if we could not open the file, exit */
                   1795:        /*error msg here */
                   1796:        return (HT_ERROR);
                   1797:      }
1.4       cvs      1798: 
                   1799:    fstat (fd, &file_stat);
                   1800: 
1.7       cvs      1801:    if (file_stat.st_size == 0)
                   1802:      {
                   1803:        /* file was empty */
                   1804:        /*errmsg here */
                   1805:        close (fd);
                   1806:        return (HT_ERROR);
                   1807:      }
1.4       cvs      1808:    block_size = file_stat.st_size;
                   1809: 
                   1810:    if (THD_TRACE)
                   1811:       fprintf (stderr, "file size == %u\n", (unsigned) block_size);
                   1812: 
                   1813:    mem_ptr = (char *) TtaGetMemory (block_size);
                   1814: 
1.7       cvs      1815:    if (mem_ptr == (char *) NULL)
                   1816:      {
                   1817:        /* could not allocate enough memory */
                   1818:        /*errmsg here */
                   1819:        close (fd);
                   1820:        return (HT_ERROR);
                   1821:      }
1.4       cvs      1822:    read (fd, mem_ptr, block_size);
                   1823: 
                   1824:    close (fd);
                   1825: 
1.27      cvs      1826:    status = UploadMemWWW (docid, METHOD_PUT, urlName, contentType, mem_ptr,
1.4       cvs      1827:                          block_size, mode, terminate_cbf,
                   1828:                          context_tcbf, (char *) NULL);
                   1829: 
                   1830:    TtaFreeMemory (mem_ptr);
1.28      cvs      1831:    TtaHandlePendingEvents ();
1.4       cvs      1832:    return (status);
                   1833: }
                   1834: 
1.5       cvs      1835: /*----------------------------------------------------------------------
1.17      cvs      1836:   UploadMemWWW
                   1837:   low level interface function to libwww for uploading a block of
                   1838:   memory to a URL.
1.5       cvs      1839:   ----------------------------------------------------------------------*/
1.4       cvs      1840: #ifdef __STDC__
                   1841: int                 UploadMemWWW (int docid, HTMethod method,
1.27      cvs      1842:                     char *urlName, PicType contentType, char *mem_ptr, unsigned long block_size,
1.4       cvs      1843:                        int mode, TTcbf * terminate_cbf, void *context_tcbf,
                   1844:                                  char *outputfile)
                   1845: #else
1.27      cvs      1846: int                 UploadMemWWW (docid, method, urlName, contentType, mem_ptr, block_size, mode,
1.4       cvs      1847:                                  terminate_cbf, context_tcbf, outputfile)
                   1848: int                 docid;
                   1849: HTMethod            method;
                   1850: char               *urlName;
1.27      cvs      1851: PicType             contentType;
1.4       cvs      1852: char               *mem_ptr;
                   1853: usigned long        block_size;
                   1854: int                 mode;
                   1855: TTcbf              *terminate_cbf;
                   1856: void               *context_tcbf;
                   1857: char               *outputfile;
1.7       cvs      1858: 
1.4       cvs      1859: #endif
                   1860: {
                   1861:    AHTReqContext      *me;
                   1862:    int                 status;
                   1863: 
                   1864:    if (mem_ptr == (char *) NULL ||
                   1865:        block_size == 0 ||
                   1866:        docid == 0 ||
1.7       cvs      1867:        urlName == (char *) NULL)
                   1868:      {
                   1869:        /* nothing to be uploaded */
                   1870:        return HT_ERROR;
                   1871:      }
1.4       cvs      1872: 
                   1873:    /* Initialize the request structure */
                   1874:    me = AHTReqContext_new (docid);
                   1875: 
1.7       cvs      1876:    if (me == NULL)
                   1877:      {
                   1878:        /* need an error message here */
                   1879:        TtaHandlePendingEvents ();
                   1880:        return (HT_ERROR);
                   1881:      }
1.4       cvs      1882:    me->mode = mode;
                   1883: 
                   1884:    me->incremental_cbf = (TIcbf *) NULL;
                   1885:    me->context_icbf = (void *) NULL;
                   1886:    me->terminate_cbf = terminate_cbf;
                   1887:    me->context_tcbf = context_tcbf;
                   1888: 
                   1889:    me->output = stdout;
                   1890:    me->outputfile = (char *) NULL;
                   1891:    me->urlName = urlName;
                   1892: 
1.70      cvs      1893: #ifdef _WINDOWS
                   1894:    HTRequest_setPreemptive (me->request, YES);
                   1895: #else
1.4       cvs      1896:    HTRequest_setPreemptive (me->request, NO);
1.70      cvs      1897: #endif /* _WINDOWS */
1.74      cvs      1898: 
1.17      cvs      1899:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      1900:    me->method = METHOD_PUT;
                   1901:    HTRequest_setMethod (me->request, METHOD_PUT);
                   1902:    me->output = stdout;
1.17      cvs      1903:    /* we are not expecting to receive any input from the server */
                   1904:    me->outputfile = (char *) NULL; 
1.4       cvs      1905: 
                   1906:    me->mem_ptr = mem_ptr;
                   1907:    me->block_size = block_size;
1.17      cvs      1908: 
                   1909:    /* set the callback which will actually copy data into the
                   1910:       output stream */
                   1911: 
1.4       cvs      1912:    HTRequest_setPostCallback (me->request, AHTUpload_callback);
1.72      cvs      1913: 
1.4       cvs      1914:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (urlName);
1.7       cvs      1915: 
1.28      cvs      1916:    /* Set the Content-Type of the file we are uploading */
1.77      cvs      1917:    HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
                   1918:                       AHTGuessAtom_for (me->urlName, contentType));
1.17      cvs      1919: 
1.7       cvs      1920:    HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1.4       cvs      1921:    HTRequest_setEntityAnchor (me->request, me->anchor);
1.74      cvs      1922:    /* prepare the URLname that will be displayed in teh status bar */
                   1923:    ChopURL (me->status_urlName, me->urlName);
                   1924:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
                   1925:                     me->status_urlName);
1.4       cvs      1926:    status = HTLoadAbsolute (urlName, me->request);
                   1927: 
1.74      cvs      1928: #ifndef _WINDOWS
1.77      cvs      1929:    if (status == HT_ERROR || 
                   1930:        me->reqStatus == HT_END || 
                   1931:        me->reqStatus == HT_ERR || 
                   1932:        HTError_hasSeverity (HTRequest_error (me->request), ERR_INFO))
1.7       cvs      1933:      {
1.17      cvs      1934:        status = HT_ERROR;
1.28      cvs      1935:      }     
1.7       cvs      1936:    else
                   1937:      {
                   1938:        /* part of the stop button handler */
1.4       cvs      1939: 
1.7       cvs      1940:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   1941:          {
                   1942:             status = LoopForStop (me);
                   1943:          }
1.15      cvs      1944:      }
1.90      cvs      1945: #else /* _WINDOWS */
                   1946:   if (status == HT_ERROR || 
                   1947:          me->reqStatus == HT_ERR) /* || Error_hasSeverity (HTRequest_error (me->request), ERR_INFO)) */
                   1948:          status = HT_ERROR;
                   1949:   else
                   1950:          status = HT_OK;
                   1951: #endif /* _WINDOWS */
                   1952: 
1.92      cvs      1953:     if (HTRequest_net (me->request))
                   1954:        {
                   1955:         HTRequest_kill (me->request);
                   1956:        }
1.93      cvs      1957:     else 
                   1958:         AHTReqContext_delete (me);
1.4       cvs      1959:    return (status);
1.28      cvs      1960: }
1.4       cvs      1961: 
                   1962: 
                   1963: 
1.5       cvs      1964: /*----------------------------------------------------------------------
1.17      cvs      1965:   Stop Request
                   1966:   stops (kills) all active requests associated with a docid 
1.5       cvs      1967:   ----------------------------------------------------------------------*/
1.4       cvs      1968: #ifdef __STDC__
                   1969: void                StopRequest (int docid)
                   1970: #else
                   1971: void                StopRequest (docid)
                   1972: int                 docid;
                   1973: #endif
                   1974: {
                   1975:    HTList             *cur;
                   1976:    AHTDocId_Status    *docid_status;
                   1977:    AHTReqContext      *me;
                   1978:    int                 open_requests;
                   1979: 
1.84      cvs      1980:    HTNet              *reqNet;
                   1981:    HTHost             *reqHost;
1.85      cvs      1982:    HTChannel          *reqChannel;
                   1983:    int                 reqSock;
                   1984:    
1.7       cvs      1985:    if (Amaya)
                   1986:      {
                   1987:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
1.4       cvs      1988:                                                       Amaya->docid_status);
1.7       cvs      1989:        /* verify if there are any requests at all associated with docid */
1.4       cvs      1990: 
1.7       cvs      1991:        if (docid_status == (AHTDocId_Status *) NULL)
                   1992:           return;
1.4       cvs      1993: 
1.7       cvs      1994:        open_requests = docid_status->counter;
1.4       cvs      1995: 
1.85      cvs      1996:        /* First, kill all pending requests */
                   1997:        /* We first inhibit the activation of pending requests */
                   1998:        HTHost_disable_PendingReqLaunch ();
                   1999:        cur = Amaya->reqlist;
                   2000:        while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
                   2001:          {
                   2002:             if (me->docid == docid && me->reqStatus == HT_NEW)
                   2003:               {
                   2004:                 reqNet = HTRequest_net (me->request);
                   2005:                 reqSock = HTNet_socket (reqNet);
                   2006:                 reqChannel = HTChannel_find(reqSock);
                   2007:                 reqHost = HTChannel_host (reqChannel);
1.95      cvs      2008: 
                   2009:                 /* If we have an ASYNC request, we kill it.
                   2010:                 ** If it's a SYNC request, we just mark it as aborted
                   2011:                 */
                   2012:                 me->reqStatus = HT_ABORT;
1.96      cvs      2013:                 if (HTRequest_net (me->request))
                   2014:                   /* delete the libwww request context */
                   2015:                   HTRequest_kill (me->request);
                   2016: 
                   2017: #ifndef _WINDOWS
1.95      cvs      2018:                 if ((me->mode & AMAYA_IASYNC) || (me->mode & AMAYA_ASYNC))
1.85      cvs      2019:                   {
1.96      cvs      2020:                     /* delete the Amaya request context */
                   2021:                     AHTReqContext_delete (me);
                   2022:                     cur = Amaya->reqlist;
1.85      cvs      2023:                   }
1.96      cvs      2024: #else /* _WINDOWS */
                   2025:                 cur = Amaya->reqlist;
                   2026: #endif /* _WINDOWS */
1.95      cvs      2027: 
                   2028:                 /* update the number of open requests */
                   2029:                 open_requests--;                  
1.85      cvs      2030:                 
                   2031:                 if (HTHost_isIdle (reqHost) ) {
                   2032: #ifdef DEBUG_LIBWWW
                   2033:                   fprintf (stderr, "Host is idle, killing socket %d\n",
1.95      cvs      2034:                            reqSock);
1.85      cvs      2035: #endif /* DEBUG_LIBWWW */
                   2036: 
                   2037:                   HTEvent_unregister (reqSock, FD_ALL);
                   2038:                   HTEvent_register(reqSock, NULL, (SockOps) FD_READ,
                   2039:                                    HTHost_catchClose,  HT_PRIORITY_MAX);
                   2040:                   close (reqSock);
                   2041:                   /*   
                   2042:                   if (reqChannel && reqHost)
                   2043:                   HTHost_clearChannel(reqHost, HT_OK);
                   2044:                   HTHost_catchClose (reqSock, NULL, FD_CLOSE);
                   2045:                   */
                   2046:                 }
                   2047:               }
                   2048:          }
                   2049:        /* enable the activation of pending requests */
                   2050:        HTHost_enable_PendingReqLaunch ();
                   2051: 
                   2052:        cur = Amaya->reqlist;
1.7       cvs      2053:        while ((me = (AHTReqContext *) HTList_nextObject (cur)))
                   2054:          {
                   2055:             if (me->docid == docid)
                   2056:               {
1.93      cvs      2057:                 /* kill this request */
1.85      cvs      2058: 
1.93      cvs      2059:                 switch (me->reqStatus)
                   2060:                   {
                   2061:                   case HT_ABORT:
                   2062:                     break;
                   2063:                     
                   2064:                   case HT_BUSY:
                   2065:                     me->reqStatus = HT_ABORT;
                   2066:                     break;
1.96      cvs      2067: 
1.93      cvs      2068:                   case HT_NEW_PENDING:
                   2069:                   case HT_WAITING:
                   2070:                   default:
                   2071:                     me->reqStatus = HT_ABORT;
                   2072:                     
1.70      cvs      2073: #                 ifndef _WINDOWS
1.93      cvs      2074:                     RequestKillAllXtevents (me);
1.74      cvs      2075: #                 endif _WINDOWS
1.96      cvs      2076: 
1.93      cvs      2077:                     reqNet = HTRequest_net (me->request);
                   2078:                     reqSock = HTNet_socket (reqNet);
                   2079:                     reqChannel = HTChannel_find(reqSock);
                   2080:                     reqHost = HTChannel_host (reqChannel);
1.96      cvs      2081: 
                   2082:                     if (HTRequest_net (me->request))
                   2083:                       HTRequest_kill (me->request);
                   2084: 
                   2085: #ifndef _WINDOWS
1.95      cvs      2086:                     if ((me->mode & AMAYA_IASYNC) || (me->mode & AMAYA_ASYNC))
1.93      cvs      2087:                       {
1.96      cvs      2088:                         AHTReqContext_delete (me);                    
                   2089:                         cur = Amaya->reqlist;
1.93      cvs      2090:                       }
1.96      cvs      2091: #else /* _WINDOWS */
                   2092:                     cur = Amaya->reqlist;
                   2093: #endif /* _WINDOWS */
1.95      cvs      2094: 
1.93      cvs      2095:                     /* if there are no more requests, then close
                   2096:                        the connection */
                   2097:                     
                   2098:                     if (HTHost_isIdle (reqHost) ) {
1.85      cvs      2099: #ifdef                        DEBUG_LIBWWW
1.93      cvs      2100:                       fprintf (stderr, "Host is idle, "
                   2101:                                "killing socket %d\n",
                   2102:                                reqSock);
1.85      cvs      2103: #endif                        /* DEBUG_LIBWWW */
1.93      cvs      2104:                       HTEvent_unregister (reqSock, FD_ALL);
                   2105:                       HTEvent_register(reqSock,
                   2106:                                        NULL,
                   2107:                                        (SockOps) FD_READ,
                   2108:                                        HTHost_catchClose,
                   2109:                                        HT_PRIORITY_MAX);
                   2110:                       close (reqSock);                         
                   2111:                       HTHost_clearChannel (reqHost, HT_ERROR);
                   2112:                       /*
                   2113:                         if (reqChannel && reqHost)
                   2114:                         HTHost_clearChannel(reqHost, HT_OK);
                   2115:                         HTHost_catchClose (reqSock, NULL, FD_CLOSE);
                   2116:                         */
1.85      cvs      2117: #ifdef                          DEBUG_LIBWWW                           
1.93      cvs      2118:                       fprintf (stderr, "After killing, "
                   2119:                                "HTHost_isIdle gives %d\n",
                   2120:                                HTHost_isIdle (reqHost));
1.85      cvs      2121: #endif                          /* DEBUG_LIBWWW */
1.93      cvs      2122:                     }
                   2123:                     
                   2124:                     open_requests--;
                   2125:                     
                   2126:                     break;
                   2127:                     
                   2128:                   }    /* switch */
1.7       cvs      2129:               }                /* if me docid */
                   2130:          }                     /* while */
                   2131:      }                         /* if amaya open requests */
1.93      cvs      2132:    
1.85      cvs      2133: } /* StopRequest */
1.17      cvs      2134: 
                   2135: /*
                   2136:   end of Module query.c
                   2137: */
                   2138: 
1.69      cvs      2139: #endif /* AMAYA_JAVA */
1.77      cvs      2140: 
                   2141: 
1.17      cvs      2142: 
                   2143: 

Webmaster