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

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

Webmaster