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

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.116     cvs        19: /* defines to include elsewhere
                     20: *********************************/
                     21: 
1.124     cvs        22: #ifndef _WINDOWS
1.130     cvs        23: #endif /* !_WINDOWS */
1.116     cvs        24: #define AMAYA_WWW_CACHE
                     25: 
1.128     cvs        26: #define CACHE_DIR_NAME DIR_STR"libwww-cache"
1.116     cvs        27: #define DEFAULT_CACHE_SIZE 5
                     28: 
1.12      cvs        29: /* Amaya includes  */
1.25      cvs        30: #define THOT_EXPORT extern
1.4       cvs        31: #include "amaya.h"
1.130     cvs        32: #include <sys/types.h>
1.70      cvs        33: #include <fcntl.h>
1.7       cvs        34: 
1.4       cvs        35: #if defined(__svr4__)
                     36: #define CATCH_SIG
                     37: #endif
                     38: 
                     39: /* local structures coming from libwww and which are
1.17      cvs        40:    not found in any .h file
1.7       cvs        41:  */
1.68      cvs        42: 
1.7       cvs        43: struct _HTStream
                     44:   {
1.15      cvs        45:      const HTStreamClass *isa;
                     46:      FILE               *fp;
1.45      cvs        47:      BOOL                leave_open;   /* Close file when TtaFreeMemory? */
1.15      cvs        48:      char               *end_command;  /* Command to execute       */
                     49:      BOOL                remove_on_close;      /* Remove file?             */
                     50:      char               *filename;     /* Name of file             */
                     51:      HTRequest          *request;      /* saved for callback       */
                     52:      HTRequestCallback  *callback;
1.7       cvs        53:   };
                     54: 
1.15      cvs        55: 
1.7       cvs        56: struct _HTError
                     57:   {
                     58:      HTErrorElement      element;      /* Index number into HTError */
                     59:      HTSeverity          severity;     /* A la VMS */
                     60:      BOOL                ignore;       /* YES if msg should not go to user */
                     61:      void               *par;  /* Explanation, e.g. filename  */
                     62:      int                 length;       /* For copying by generic routine */
                     63:      char               *where;        /* Which function */
                     64:   };
                     65: 
1.4       cvs        66: /* Type definitions and global variables etc. local to this module */
                     67: 
1.17      cvs        68: /*----------------------------------------------------------------------*/
                     69: 
1.4       cvs        70: /*** private variables ***/
1.17      cvs        71: 
1.4       cvs        72: static HTList      *converters = NULL; /* List of global converters */
1.27      cvs        73: static HTList      *acceptTypes = NULL; /* List of types for the Accept header */
1.4       cvs        74: static HTList      *encodings = NULL;
1.16      cvs        75: static int          object_counter = 0;        /* loaded objects counter */
1.116     cvs        76: static  boolean     AmayaAlive; /* set to 1 if the application is active;
                     77:                                   0 if we have killed */
1.128     cvs        78: #ifdef  AMAYA_WWW_CACHE
1.130     cvs        79: static int          fd_cachelock; /* open handle to the .lock cache file */
                     80: #endif /* AMAYA_WWW_CACHE */
1.4       cvs        81: 
1.15      cvs        82: #include "answer_f.h"
                     83: #include "query_f.h"
                     84: #include "AHTURLTools_f.h"
                     85: #include "AHTBridge_f.h"
                     86: #include "AHTMemConv_f.h"
                     87: #include "AHTFWrite_f.h"
1.4       cvs        88: 
1.116     cvs        89: /* prototypes */
                     90: 
                     91: #ifdef __STDC__
1.128     cvs        92: static void RecCleanCache (char *dirname);
1.123     cvs        93: #ifdef _WINDOWS
1.70      cvs        94: int WIN_Activate_Request (HTRequest* , HTAlertOpcode, int, const char*, void*, HTAlertPar*);
1.116     cvs        95: #endif /* _WINDOWS */
1.70      cvs        96: #else
1.128     cvs        97: static void RecCleanCache (/* char *dirname */);
1.123     cvs        98: #ifdef _WINDOWS
1.128     cvs        99: int WIN_Activate_Request (/* HTRequest* , HTAlertOpcode, int, const char*, void*, HTAlertPar* */);
1.116     cvs       100: #endif /* _WINDOWS */
1.70      cvs       101: #endif /* __STDC__ */
1.116     cvs       102: 
1.15      cvs       103: 
1.130     cvs       104: #ifdef AMAYA_WWW_CACHE
                    105: /***************************************************************
                    106:  lock functions, used to avoid concurrent use of the cache
                    107:  Mostly based on W. Richard Stevens' APUE book.
                    108:  ***************************************************************/
                    109: /*----------------------------------------------------------------------
                    110:   set_cachelock
                    111:   sets a write lock on filename. 
                    112:   Returns -1 in case of failure, otherwise, it'll open a handle on
                    113:   filename and fd_cachelock will take its value.
                    114:   ----------------------------------------------------------------------*/
                    115: #ifdef __STDC__
                    116: static int set_cachelock (char *filename)
                    117: #else        
                    118: static int set_cachelock (filename)
                    119: char *filename;
                    120: #endif /* __STDC__ */
                    121: {
                    122: #ifdef _WINDOWS
                    123:   return 0;
                    124: #else
                    125:   int status;
                    126:   struct flock lock;
                    127:  
                    128:   lock.l_type = F_WRLCK;
                    129:   lock.l_start = 0;
                    130:   lock.l_whence = SEEK_SET;
                    131:   lock.l_len = 0;
                    132:   
                    133:   fd_cachelock = open (filename, O_WRONLY);
                    134:   
                    135:   if (fd_cachelock == -1)
                    136:     status = -1;
                    137:   else
                    138:     status = fcntl(fd_cachelock, F_SETLK, &lock);
                    139:   
                    140:   if (status == -1)
                    141:     fd_cachelock = 0;
                    142:   
                    143:   return (status);
                    144: #endif /* _WINDOWS */
                    145: }
                    146: 
                    147: /*----------------------------------------------------------------------
                    148:   clear_cachelock
                    149:   remove the write lock set on a filename.
                    150:   It'll close the fd handle on filename and reset *fd to 0.
                    151:   ----------------------------------------------------------------------*/
                    152: #ifdef __STDC__
                    153: static int clear_cachelock (void)
                    154: #else        
                    155: static int clear_cachelock ()
                    156: int *fd;
                    157: #endif /* __STDC__ */
                    158: {
                    159: #ifdef _WINDOWS
                    160:   return 0;
                    161: #else
                    162:   int status;
                    163:   struct flock lock;
                    164: 
                    165:   if (fd_cachelock)
                    166:     return (-1);
                    167:  
                    168:   lock.l_type = F_UNLCK;
                    169:   lock.l_start = 0;
                    170:   lock.l_whence = SEEK_SET;
                    171:   lock.l_len = 0;
                    172:   
                    173:   status = fcntl(fd_cachelock, F_SETLK, &lock);
                    174:   close (fd_cachelock);
                    175:   fd_cachelock = 0;
                    176: 
                    177:   return (status);
                    178: #endif /* _WINDOWS */
                    179: }
                    180: 
                    181: /*----------------------------------------------------------------------
                    182:   test_cachelock
                    183:   returns 0 if a fd is not locked by other process, otherwise 
                    184:   returns the pid of the process who has the lock
                    185:   ----------------------------------------------------------------------*/
                    186: #ifdef __STDC__
                    187: static int test_cachelock (char *filename)
                    188: #else
                    189: static int test_cachelock (filename)
                    190: char *filename;
                    191: #endif __STDC__
                    192: {
                    193: #ifdef _WINDOWS
                    194:  int fd;
                    195:   fd = open (filename, _O_WRONLY | _O_BINARY);
                    196:   if (fd != -1)
                    197:     {
                    198:       close (fd);
                    199:       return 0;
                    200:     }
                    201:   else
                    202:     return -1;
                    203: #else
                    204:   struct flock lock;
                    205:   int fd, status;
                    206: 
                    207:   lock.l_type = F_WRLCK;
                    208:   lock.l_start = 0;
                    209:   lock.l_whence = SEEK_SET;
                    210:   lock.l_len = 0;
                    211: 
                    212:   fd = open (filename, O_WRONLY);
                    213: 
                    214:   if (fd < 0)
                    215:     return (-1);
                    216:   /* is this file locked ? */
                    217:   status = fcntl (fd,  F_GETLK, &lock);
                    218:   close (fd);
                    219: 
                    220:   if (status < 0)
                    221:     return (-1);
                    222: 
                    223:   if (lock.l_type == F_UNLCK)
                    224:     return (0); /* false, region is not locked by another proc */
                    225:   return (lock.l_pid); /* true, return pid of lock owner */
                    226: #endif /* _WINDOWS */
                    227: }
                    228: 
                    229: #endif /* AMAYA_WWW_CACHE */
                    230: 
1.15      cvs       231: /*----------------------------------------------------------------------
1.17      cvs       232:   GetDocIdStatus
                    233:   gets the status associated to a docid                         
1.15      cvs       234:   ----------------------------------------------------------------------*/
                    235: #ifdef __STDC__
                    236: AHTDocId_Status    *GetDocIdStatus (int docid, HTList * documents)
                    237: #else
                    238: AHTDocID_Status    *GetDocIdStatus (docid, documents)
                    239: int                 docid;
                    240: HTList             *documents;
                    241: 
                    242: #endif
                    243: {
                    244:    AHTDocId_Status    *me;
                    245:    HTList             *cur;
                    246: 
                    247:    if (documents)
                    248:      {
                    249:        cur = documents;
                    250: 
                    251:        while ((me = (AHTDocId_Status *) HTList_nextObject (cur)))
                    252:          {
                    253:             if (me->docid == docid)
                    254:                return (me);
1.18      cvs       255:          }
                    256:      }
1.15      cvs       257:    return (AHTDocId_Status *) NULL;
                    258: 
                    259: }
                    260: 
1.5       cvs       261: /*----------------------------------------------------------------------
1.27      cvs       262:   AHTGuessAtom_for
                    263:   Converts an Amaya type descriptor into the equivalent MIME type.
                    264:   ----------------------------------------------------------------------*/
                    265: #ifdef __STDC__
                    266: static  HTAtom *AHTGuessAtom_for (char *urlName, PicType contentType)
                    267: #else
                    268: static  HTAtom *AHTGuessAtom_for (urlName, contentType)
                    269: char *urlName;
                    270: PicType contentType;
                    271: #endif
                    272: {
                    273:  HTAtom           *atom;
                    274:  char             *filename;
                    275:  HTEncoding        enc;
                    276:  HTEncoding        cte;
                    277:  HTLanguage        lang;
1.50      cvs       278:  double            quality = 1.0;
1.27      cvs       279: 
                    280:  switch (contentType)
                    281:    {
                    282:     case xbm_type:
                    283:       atom = HTAtom_for("image/xbm");
                    284:       break;
                    285:     case eps_type:
1.35      cvs       286:       atom = HTAtom_for("application/postscript");
1.27      cvs       287:       break;
                    288:    case xpm_type:
                    289:       atom = HTAtom_for("image/xpm");
                    290:      break;
                    291:     case gif_type:
                    292:       atom = HTAtom_for("image/gif");
                    293:       break;
                    294:     case jpeg_type:
                    295:       atom = HTAtom_for("image/jpeg");
                    296:       break;
                    297:     case png_type:
                    298:       atom = HTAtom_for("image/png");
                    299:       break;
                    300:    case unknown_type:
                    301:    default:
                    302:      /* 
                    303:      ** Amaya could not detect the type, so 
                    304:      ** we try to use the filename's suffix to do so.
                    305:      */
1.45      cvs       306:      filename = AmayaParseUrl (urlName, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.27      cvs       307:      HTBind_getFormat (filename, &atom, &enc, &cte, &lang, &quality);
1.45      cvs       308:      TtaFreeMemory (filename);
1.27      cvs       309:      if (atom ==  WWW_UNKNOWN)
                    310:         /*
                    311:         ** we could not identify the suffix, so we assign it
                    312:         ** a default type
                    313:         */
                    314:         atom = HTAtom_for ("text/html");
                    315:      break;
                    316:    }
                    317: 
                    318:  return atom;
                    319: }
                    320: 
                    321: /*----------------------------------------------------------------------
1.17      cvs       322:   AHTReqContext_new
                    323:   create a new Amaya Context Object and update the global Amaya
                    324:   request status.
1.5       cvs       325:   ----------------------------------------------------------------------*/
1.4       cvs       326: #ifdef __STDC__
                    327: static AHTReqContext *AHTReqContext_new (int docid)
                    328: #else
                    329: static AHTReqContext *AHTReqContext_new (docid)
                    330: int                 docid;
                    331: 
                    332: #endif
                    333: {
                    334:    AHTReqContext      *me;
                    335:    AHTDocId_Status    *docid_status;
                    336: 
                    337:    if ((me = (AHTReqContext *) TtaGetMemory (sizeof (AHTReqContext))) == NULL)
1.116     cvs       338:      outofmem (__FILE__, "AHTReqContext_new");
                    339: 
                    340:    /* clear the structure */
                    341:    memset ((void *) me, 0, sizeof (AHTReqContext));
1.4       cvs       342: 
1.81      cvs       343:    /* Bind the Context object together with the Request Object */
                    344:    me->request = HTRequest_new ();
                    345:   
1.116     cvs       346:    /*
                    347:    ** Make sure that the first request is flushed immediately and not
                    348:    ** buffered in the output buffer
                    349:    */
                    350:    HTRequest_setFlush(me->request, YES);
                    351: 
1.80      cvs       352:    /* clean the associated file structure) */
1.79      cvs       353:    HTRequest_setOutputStream (me->request, NULL);
1.81      cvs       354:  
1.4       cvs       355:    /* Initialize the other members of the structure */
1.17      cvs       356:    me->reqStatus = HT_NEW; /* initial status of a request */
1.4       cvs       357:    me->docid = docid;
                    358:    HTRequest_setMethod (me->request, METHOD_GET);
                    359:    HTRequest_setOutputFormat (me->request, WWW_SOURCE);
                    360:    HTRequest_setContext (me->request, me);
1.116     cvs       361: 
1.36      cvs       362:    /* experimental */
                    363:    me->read_sock = INVSOC;
                    364:    me->write_sock = INVSOC;
                    365:    me->except_sock = INVSOC;
1.4       cvs       366: 
                    367:    /* Update the global context */
                    368:    HTList_appendObject (Amaya->reqlist, (void *) me);
                    369: 
                    370:    docid_status = GetDocIdStatus (docid, Amaya->docid_status);
                    371: 
1.7       cvs       372:    if (docid_status == NULL)
                    373:      {
1.116     cvs       374:        docid_status = (AHTDocId_Status *) 
                    375:          TtaGetMemory (sizeof (AHTDocId_Status));
1.7       cvs       376:        docid_status->docid = docid;
                    377:        docid_status->counter = 1;
                    378:        HTList_addObject (Amaya->docid_status, (void *) docid_status);
                    379:      }
                    380:    else
1.4       cvs       381:       docid_status->counter++;
                    382: 
                    383:    Amaya->open_requests++;
                    384: 
1.74      cvs       385: #ifdef DEBUG_LIBWWW
                    386:    fprintf (stderr, "AHTReqContext_new: Created object %p\n", me);
                    387: #endif   
1.116     cvs       388: 
1.4       cvs       389:    return me;
                    390: }
                    391: 
1.5       cvs       392: /*----------------------------------------------------------------------
1.17      cvs       393:   AHTReqContext_delete
                    394:   Delete an Amaya Context Object and update the global Amaya request
                    395:   status.
1.5       cvs       396:   ----------------------------------------------------------------------*/
1.4       cvs       397: 
                    398: #ifdef __STDC__
1.15      cvs       399: boolean   AHTReqContext_delete (AHTReqContext * me)
1.4       cvs       400: #else
1.15      cvs       401: boolean   AHTReqContext_delete (me)
1.4       cvs       402: AHTReqContext      *me;
                    403: 
                    404: #endif
                    405: {
                    406:    AHTDocId_Status    *docid_status;
                    407: 
1.7       cvs       408:    if (me)
                    409:      {
1.4       cvs       410: 
1.74      cvs       411: #ifdef DEBUG_LIBWWW
                    412:         fprintf (stderr, "AHTReqContext_delete: Deleting object %p\n", me);
                    413: #endif   
                    414: 
1.7       cvs       415:        if (Amaya->reqlist)
                    416:           HTList_removeObject (Amaya->reqlist, (void *) me);
1.4       cvs       417: 
1.7       cvs       418:        docid_status = GetDocIdStatus (me->docid, Amaya->docid_status);
1.4       cvs       419: 
1.7       cvs       420:        if (docid_status)
                    421:          {
                    422:             docid_status->counter--;
                    423: 
                    424:             if (docid_status->counter == 0)
                    425:               {
                    426:                  HTList_removeObject (Amaya->docid_status, (void *) docid_status);
                    427:                  TtaFreeMemory ((void *) docid_status);
                    428:               }
                    429:          }
1.131     cvs       430:        if (me->method != METHOD_PUT && HTRequest_outputStream (me->request))
1.79      cvs       431:          AHTFWriter_FREE (me->request->output_stream);
1.7       cvs       432:        HTRequest_delete (me->request);
1.85      cvs       433: 
1.89      cvs       434:        if (me->output && me->output != stdout)
1.85      cvs       435:          {     
                    436: #ifdef DEBUG_LIBWWW       
                    437:            fprintf (stderr, "AHTReqContext_delete: URL is  %s, closing "
                    438:                     "FILE %p\n", me->urlName, me->output); 
                    439: #endif
                    440:            fclose (me->output);
                    441:            me->output = NULL;
                    442:          }
1.79      cvs       443:          
1.7       cvs       444:        if (me->error_stream != (char *) NULL)
1.70      cvs       445:          HT_FREE (me->error_stream);
                    446: #     ifndef _WINDOWS
                    447: #     ifdef WWW_XWINDOWS       
1.21      cvs       448:        if (me->read_xtinput_id || me->write_xtinput_id ||
                    449:             me->except_xtinput_id)
                    450:           RequestKillAllXtevents(me);
1.70      cvs       451: #     endif /* WWW_XWINDOWS */
                    452: #     endif /* !_WINDOWS */
1.74      cvs       453: 
1.99      cvs       454:     if (me->reqStatus == HT_ABORT)
                    455:       {
                    456:       if (me->outputfile && me->outputfile[0] != EOS)
                    457:        {
                    458:          TtaFileUnlink (me->outputfile);
                    459:          me->outputfile[0] = EOS;
                    460:        }
                    461:       }
                    462: 
1.130     cvs       463:     if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC))
                    464:       /* for the ASYNC mode, free the memory we allocated in GetObjectWWW
                    465:         or in PutObjectWWW */
                    466:       {
                    467:        if (me->urlName)
                    468:          TtaFreeMemory (me->urlName);
                    469:        if (me->outputfile)
                    470:          TtaFreeMemory (me->outputfile);
                    471:       }
                    472:     
                    473:     if (me->content_type)
                    474:       TtaFreeMemory (me->content_type);
1.131     cvs       475:     
1.130     cvs       476:     if (me->formdata)
                    477:       HTAssocList_delete (me->formdata);
                    478:     
                    479:     /* to trace bugs */
                    480:     memset ((void *) me, 0, sizeof (AHTReqContext));
                    481:     
                    482:     TtaFreeMemory ((void *) me);
                    483:     
                    484:     Amaya->open_requests--;
                    485:     
                    486:     return TRUE;
1.7       cvs       487:      }
1.15      cvs       488:    return FALSE;
1.4       cvs       489: }
                    490: 
                    491: 
1.15      cvs       492: /*----------------------------------------------------------------------
1.17      cvs       493:   Thread_deleteAll
                    494:   this function deletes the whole list of active threads.           
1.5       cvs       495:   ----------------------------------------------------------------------*/
1.4       cvs       496: #ifdef __STDC__
                    497: static void         Thread_deleteAll (void)
                    498: #else
                    499: static void         Thread_deleteAll ()
                    500: #endif
                    501: {
1.21      cvs       502:   HTList             *cur;
                    503:   AHTReqContext      *me;
                    504:   AHTDocId_Status    *docid_status;
                    505: 
1.74      cvs       506:   if (Amaya && Amaya->reqlist)
                    507:     {
                    508:       if (Amaya->open_requests > 0)
                    509: #ifdef DEBUG_LIBWWW
                    510:       fprintf (stderr, "Thread_deleteAll: Killing %d outstanding "
                    511:                       "requests\n", Amaya->open_requests);
                    512: #endif   
                    513:        {
                    514:          cur = Amaya->reqlist;
                    515:          
                    516:          /* erase the requests */
                    517:          while ((me = (AHTReqContext *) HTList_removeLastObject (cur)))
                    518:            {
                    519:              if (me->request)
                    520:                {
1.70      cvs       521: #              ifndef _WINDOWS 
1.74      cvs       522:                  RequestKillAllXtevents (me);
1.70      cvs       523: #              endif /* !_WINDOWS */
1.92      cvs       524: 
                    525:                  if (me->request->net)
                    526:                    HTRequest_kill (me->request);
1.96      cvs       527: #ifndef _WINDOWS
                    528:                  AHTReqContext_delete (me);
                    529: #endif /* _WINDOWS */
1.74      cvs       530:                }
                    531:            }           /* while */
                    532:          
                    533:          /* erase the docid_status entities */
                    534:          while ((docid_status = (AHTDocId_Status *) HTList_removeLastObject ((void *) Amaya->docid_status)))
                    535:            TtaFreeMemory ((void *) docid_status);
                    536:          
                    537:        }                       /* if */
1.84      cvs       538:        
1.74      cvs       539:     }
1.4       cvs       540: }
                    541: 
1.5       cvs       542: /*----------------------------------------------------------------------
1.83      cvs       543:   AHTOpen_file
                    544:   ----------------------------------------------------------------------*/
                    545: #ifdef __STDC__
1.85      cvs       546: int                 AHTOpen_file (HTRequest * request)
1.83      cvs       547: #else
1.85      cvs       548: int                 AHTOpen_file (request)
1.83      cvs       549: HTRequest           *request;
                    550: 
                    551: #endif /* __STDC__ */
                    552: {
                    553:   AHTReqContext      *me;      /* current request */
                    554: 
                    555:   me = HTRequest_context (request);
                    556: 
1.93      cvs       557:   if (!me)
                    558:       return HT_ERROR;
                    559: 
1.131     cvs       560:   if (me->method == METHOD_PUT)
                    561:     return HT_OK;
                    562: 
1.83      cvs       563: #ifdef DEBUG_LIBWWW
1.116     cvs       564:   fprintf(stderr, "AHTOpen_file: start for object : %p\n", me);
1.85      cvs       565: #endif /* DEBUG_LIBWWW */
                    566: 
                    567:   if (me->reqStatus == HT_ABORT) 
                    568:     {
                    569: #ifdef DEBUG_LIBWWW
                    570:       fprintf(stderr, "AHTOpen_file: caught an abort request, skipping it\n");
1.83      cvs       571: #endif /* DEBUG_LIBWWW */
                    572: 
1.85      cvs       573:       return HT_OK;
                    574:     }
                    575: 
                    576:   if (HTRequest_outputStream (me->request)) 
                    577:     {
                    578: 
                    579: #ifdef DEBUG_LIBWWW
                    580:       fprintf(stderr, "AHTOpen_file: output stream already existed for url %s\n", me->urlName);
                    581: #endif /* DEBUG_LIBWWW */      
                    582:       return HT_OK;
                    583:     }
                    584: 
                    585: #ifdef DEBUG_LIBWWW
                    586:       fprintf(stderr, "AHTOpen_file: opening output stream for url %s\n", me->urlName);
                    587: #endif /* DEBUG_LIBWWW */      
                    588: 
1.83      cvs       589:   if (!(me->output) && 
                    590:       (me->output != stdout) && 
                    591: #ifndef _WINDOWS
                    592:       (me->output = fopen (me->outputfile, "w")) == NULL)
                    593:     {
1.116     cvs       594: #else /* !_WINDOWS */
1.83      cvs       595:     (me->output = fopen (me->outputfile, "wb")) == NULL) 
                    596:     {
                    597: #endif /* !_WINDOWS */
                    598: 
1.131     cvs       599:       me->outputfile[0] = EOS; /* file could not be opened */
1.85      cvs       600: #ifdef DEBUG_LIBWWW
                    601:       fprintf(stderr, "AHTOpen_file: couldn't open output stream for url %s\n", me->urlName);
                    602: #endif
1.83      cvs       603:       TtaSetStatus (me->docid, 1, 
                    604:                    TtaGetMessage (AMAYA, AM_CANNOT_CREATE_FILE),
                    605:                    me->outputfile);
                    606:       me->reqStatus = HT_ERR;
                    607:       return (HT_ERROR);
                    608:     }
                    609:          
                    610:   HTRequest_setOutputStream (me->request,
                    611:                             AHTFWriter_new (me->request, 
                    612:                                             me->output, YES));
                    613:   me->reqStatus = HT_WAITING;
                    614:   
                    615:   return HT_OK;
                    616: }
                    617: 
                    618: /*----------------------------------------------------------------------
1.17      cvs       619:   redirection_handler
                    620:   this function is registered to handle permanent and temporary
                    621:   redirections.
                    622:   ----------------------------------------------------------------------*/
1.4       cvs       623: #ifdef __STDC__
1.7       cvs       624: static int          redirection_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs       625: #else
                    626: static int          redirection_handler (request, context, status)
                    627: HTRequest          *request;
                    628: HTResponse         *response;
                    629: void               *param;
                    630: int                 status;
                    631: 
                    632: #endif
                    633: {
                    634: 
                    635:    HTAnchor           *new_anchor = HTResponse_redirection (response);
1.7       cvs       636:    AHTReqContext      *me = HTRequest_context (request);
1.4       cvs       637:    HTMethod            method = HTRequest_method (request);
1.98      cvs       638:    char               *ref;
                    639:    char               *tmp;
1.4       cvs       640: 
1.7       cvs       641:    if (!new_anchor)
                    642:      {
                    643:        if (PROT_TRACE)
                    644:           HTTrace ("Redirection. No destination\n");
                    645:        return HT_OK;
                    646:      }
1.4       cvs       647: 
                    648:    /*
                    649:       ** Only do redirect on GET and HEAD
                    650:     */
1.7       cvs       651:    if (!HTMethod_isSafe (method))
                    652:      {
1.130     cvs       653:        /*
                    654:        ** If we got a 303 See Other then change the method to GET.
                    655:        ** Otherwise ask the user whether we should continue.
                    656:        */
                    657:        if (status == HT_SEE_OTHER) 
                    658:         {
                    659:           if (PROT_TRACE)
                    660:             HTTrace("Redirection. Changing method from %s to GET\n",
                    661:                     HTMethod_name(method));
                    662:           HTRequest_setMethod(request, METHOD_GET);
                    663:         }
                    664:        else 
                    665:         {
                    666:           HTAlertCallback    *prompt = HTAlert_find (HT_A_CONFIRM);
                    667:           if (prompt)
                    668:             {
                    669:               if ((*prompt) (request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
                    670:                              NULL, NULL, NULL) != YES)
                    671:                 return HT_ERROR;
                    672:             }
                    673:         }
1.7       cvs       674:      }
1.4       cvs       675:    /*
                    676:     **  Start new request with the redirect anchor found in the headers.
                    677:     **  Note that we reuse the same request object which means that we must
                    678:     **  keep this around until the redirected request has terminated. It also           
                    679:     **  allows us in an easy way to keep track of the number of redirections
                    680:     **  so that we can detect endless loops.
                    681:     */
1.17      cvs       682:    
1.7       cvs       683:    if (HTRequest_doRetry (request))
                    684:      {
                    685:        /* do we need to normalize the URL? */
                    686:        if (strncmp (new_anchor->parent->address, "http:", 5))
                    687:          {
                    688:             /* Yes, so we use the pre-redirection anchor as a base name */
1.74      cvs       689:             ref = AmayaParseUrl (new_anchor->parent->address, 
                    690:                                  me->urlName, AMAYA_PARSE_ALL);
1.7       cvs       691:             if (ref)
                    692:               {
1.98      cvs       693:                 HT_FREE (new_anchor->parent->address);
                    694:                 tmp = NULL;
                    695:                 HTSACopy (&tmp, ref);
                    696:                 new_anchor->parent->address = tmp;
                    697:                 TtaFreeMemory (ref);
1.7       cvs       698:               }
1.98      cvs       699:             else
                    700:               return HT_ERROR; /* We can't redirect anymore */
1.7       cvs       701:          }
                    702: 
                    703:        /* update the current file name */
1.130     cvs       704:        if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC)) 
1.7       cvs       705:          {
1.130     cvs       706:            TtaFreeMemory (me->urlName);
                    707:            me->urlName = TtaStrdup (new_anchor->parent->address);
1.7       cvs       708:          }
                    709:        else
1.130     cvs       710:          /* it's a SYNC mode, so we should keep the urlName */
                    711:          {
                    712:            strncpy (me->urlName, new_anchor->parent->address, 
                    713:                     MAX_LENGTH - 1);
                    714:            me->urlName[MAX_LENGTH - 1] = EOS;
                    715:          }
1.7       cvs       716: 
1.38      cvs       717:        ChopURL (me->status_urlName, me->urlName);
1.116     cvs       718: 
1.7       cvs       719:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_RED_FETCHING),
1.38      cvs       720:                      me->status_urlName);
1.7       cvs       721: 
                    722:        /* Start request with new credentials */
1.79      cvs       723:        if (HTRequest_outputStream (me->request) != NULL) {
1.85      cvs       724:          AHTFWriter_FREE (request->output_stream);
                    725:          if (me->output != stdout) { /* Are we writing to a file? */
1.74      cvs       726: #ifdef DEBUG_LIBWWW
1.130     cvs       727:            fprintf (stderr, "redirection_handler: New URL is  %s, closing "
                    728:                     "FILE %p\n", me->urlName, me->output); 
1.74      cvs       729: #endif 
1.79      cvs       730:            fclose (me->output);
                    731:            me->output = NULL;
                    732:          }
1.64      cvs       733:        }
1.68      cvs       734: 
1.105     cvs       735:        /* reset the status */
1.116     cvs       736:        me->reqStatus = HT_NEW; 
1.105     cvs       737:        /* clear the errors */
1.116     cvs       738:        HTError_deleteAll (HTRequest_error (request));
1.105     cvs       739:        HTRequest_setError (request, NULL);
1.116     cvs       740: 
1.130     cvs       741:        if (me->method == METHOD_PUT 
                    742:            || me->method == METHOD_POST)       /* PUT, POST etc. */
1.74      cvs       743:          status = HTLoadAbsolute (me->urlName, request);
1.7       cvs       744:        else
1.74      cvs       745:          HTLoadAnchor (new_anchor, request);
1.7       cvs       746:      }
                    747:    else
1.68      cvs       748:      {
1.74      cvs       749:        HTRequest_addError (request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    750:                           NULL, 0, "HTRedirectFilter");
                    751:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REDIRECTIONS_LIMIT),
                    752:                     NULL);
                    753:        if (me->error_html)
                    754:         DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR; 
                    755:        /* so that we can show the error message */
1.68      cvs       756:      }
                    757: 
1.4       cvs       758:    /*
1.74      cvs       759:    **  By returning HT_ERROR we make sure that this is the last handler to be
                    760:    **  called. We do this as we don't want any other filter to delete the 
                    761:    **  request object now when we have just started a new one ourselves
                    762:    */
1.4       cvs       763:    return HT_ERROR;
                    764: }
                    765: 
1.5       cvs       766: /*----------------------------------------------------------------------
1.17      cvs       767:   terminate_handler
                    768:   this function is registered to handle the result of the request
1.5       cvs       769:   ----------------------------------------------------------------------*/
1.4       cvs       770: #if __STDC__
1.7       cvs       771: static int          terminate_handler (HTRequest * request, HTResponse * response, void *context, int status)
1.4       cvs       772: #else
                    773: static int          terminate_handler (request, response, context, status)
                    774: HTRequest          *request;
                    775: HTResponse         *response;
                    776: void               *context;
                    777: int                 status;
1.123     cvs       778: #endif /* __STDC__ */
1.4       cvs       779: {
1.116     cvs       780:   AHTReqContext      *me = (AHTReqContext *) HTRequest_context (request);
                    781:   boolean             error_flag;
                    782:   char               *content_type;
                    783: 
                    784:   if (!me)
                    785:     return HT_OK;              /* not an Amaya request */
                    786: 
                    787:   if (me->reqStatus == HT_END)
                    788:     /* we have already processed this request! */
                    789:     return HT_OK;
                    790: 
                    791:   /* if Amaya was killed, treat with this request as if it were
                    792:      issued by a Stop button event */
                    793: 
                    794:    if (!AmayaIsAlive ())           
1.80      cvs       795:       me->reqStatus = HT_ABORT; 
1.74      cvs       796:    
1.77      cvs       797:    if (status == HT_LOADED || 
                    798:        status == HT_CREATED || 
1.79      cvs       799:        status == HT_NO_DATA ||
1.116     cvs       800: #ifdef AMAYA_WWW_CACHE
                    801:        /* what status to use to know we're downloading from a cache? */
                    802:        status ==  HT_NOT_MODIFIED ||
                    803: #endif /* AMAYA_WWW_CACHE */
1.79      cvs       804:        me->reqStatus == HT_ABORT)
1.74      cvs       805:      error_flag = FALSE;
1.13      cvs       806:    else
1.74      cvs       807:      error_flag = TRUE;
1.130     cvs       808: 
1.4       cvs       809:    /* output any errors from the server */
1.130     cvs       810: 
1.77      cvs       811:    /*
1.74      cvs       812:    ** me->output = output file which will receive an html file
                    813:    ** me->error_html = yes, output HTML errors in the screen
                    814:    ** request->error_stack == if there are any errors, they will be here
                    815:    ** me->error_stream_size If it's != 0 means an error message has already
                    816:    **                       been written to the stack
                    817:    */
                    818:    
1.4       cvs       819:    /* First, we verify if there are any errors and if they are not
1.17      cvs       820:    ** yet written to the error stack. If no, then let's try to write them
                    821:    ** ourselves
                    822:    */
1.74      cvs       823:    
                    824: #ifdef DEBUG_LIBWWW
                    825:    fprintf (stderr, "terminate_handler: URL is "
                    826:            "%s, closing FILE %p status is %d\n", me->urlName, me->output, 
                    827:            status); 
1.69      cvs       828: #endif
1.74      cvs       829:    
1.69      cvs       830:    if (me->output && me->output != stdout)
                    831:      {
1.74      cvs       832:        /* we are writing to a file */
                    833:        if (me->reqStatus != HT_ABORT)
                    834:         {                      /* if the request was not aborted and */
1.80      cvs       835:           if (error_flag &&
                    836:               me->error_html == TRUE)
                    837:               /* there were some errors and we want to print them */
                    838:             {          
                    839:               if (me->error_stream_size == 0)/* and the stream is empty */
                    840:                 AHTError_MemPrint (request); /* copy errors from 
1.74      cvs       841:                                                  **the error stack 
1.77      cvs       842:                                                  ** into the error stream */
1.80      cvs       843:               if (me->error_stream)
                    844:                 {      /* if the stream is non-empty */
                    845:                   fprintf (me->output, me->error_stream);/* output the errors */
1.85      cvs       846:                   /* Clear the error context, so that we can deal with
                    847:                      this answer as if it were a normal reply */
                    848:                    HTError_deleteAll( HTRequest_error (request));
                    849:                    HTRequest_setError (request, NULL);
                    850:                    error_flag = 0;
1.74      cvs       851:                 }
                    852:             }                  /* if error_stack */
1.85      cvs       853:         }
                    854: 
                    855:        /* if != HT_ABORT */
                    856:        
                    857: #ifdef DEBUG_LIBWWW       
                    858:        fprintf (stderr, "terminate_handler: URL is  %s, closing "
                    859:                "FILE %p\n", me->urlName, me->output); 
                    860: #endif
1.74      cvs       861:        fclose (me->output);
                    862:        me->output = NULL;
1.69      cvs       863:      }
1.80      cvs       864: 
                    865:    if (error_flag)
                    866:      me->reqStatus = HT_ERR;
                    867:    else if (me->reqStatus != HT_ABORT)
                    868:      me->reqStatus = HT_END;
1.116     cvs       869:    
                    870:    /* copy the content_type */
                    871:    content_type = request->anchor->content_type->name;
1.114     cvs       872:    if (content_type && content_type [0] != EOS)
1.88      cvs       873:      {
1.114     cvs       874:        /* libwww gives www/unknown when it gets an error. As this is 
                    875:          an HTML test, we force the type to text/html */
                    876:        if (!strcmp (content_type, "www/unknown"))
                    877:         me->content_type = TtaStrdup ("text/html");
                    878:        else
                    879:         me->content_type = TtaStrdup (content_type);
                    880: 
                    881:        /* Content-Type can be specified by an httpd  server's admin. To be on
                    882:          the safe side, we normalize its case */
                    883:        ConvertToLowerCase (me->content_type);
                    884: 
1.88      cvs       885: #ifdef DEBUG_LIBWWW
                    886:         fprintf (stderr, "content type is: %s\n", me->content_type);
                    887: #endif /* DEBUG_LIBWWW */
1.114     cvs       888:      } 
                    889: 
1.115     cvs       890:    /* to avoid a hangup while downloading css files */
                    891:    if (AmayaAlive && (me->mode & AMAYA_LOAD_CSS))
1.116     cvs       892:      TtaSetStatus (me->docid, 1, 
                    893:                   TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
                    894:                   me->status_urlName);
                    895:    
                    896:   /* don't remove or Xt will hang up during the PUT */
                    897:    if (AmayaIsAlive ()  && ((me->method == METHOD_POST) ||
                    898:                            (me->method == METHOD_PUT)))
1.7       cvs       899:      {
1.80      cvs       900:        PrintTerminateStatus (me, status);
1.116     cvs       901:        
1.80      cvs       902:      } 
1.114     cvs       903: 
1.111     cvs       904:    ProcessTerminateRequest (request, response, context, status);
1.116     cvs       905:    
1.111     cvs       906:    return HT_OK;
1.4       cvs       907: }
                    908: 
1.5       cvs       909: /*----------------------------------------------------------------------
1.17      cvs       910:   AHTLoadTerminate_handler
1.74      cvs       911:   this is an application "AFTER" Callback. It's called by the library
                    912:   when a request has ended, so that we can setup the correct status.
1.5       cvs       913:   ----------------------------------------------------------------------*/
1.4       cvs       914: 
                    915: #ifdef __STDC__
1.111     cvs       916: int          AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs       917: #else
1.111     cvs       918: int          AHTLoadTerminate_handler (request, response, param, status)
1.4       cvs       919: HTRequest          *request;
                    920: HTResponse         *response;
                    921: void               *param;
                    922: int                 status;
1.69      cvs       923: 
1.4       cvs       924: #endif
                    925: {
1.116     cvs       926: 
                    927:   /** @@@@ use this with printstatus ?? */
                    928: 
1.4       cvs       929:    AHTReqContext      *me = HTRequest_context (request);
                    930:    HTAlertCallback    *cbf;
                    931:    AHTDocId_Status    *docid_status;
                    932: 
1.123     cvs       933: #ifdef _WINDOWS
                    934:    /* @@@ I have problems with the trace variables under windows */
1.124     cvs       935:    return HT_OK;
1.123     cvs       936: #endif /* _WINDOWS */
                    937: 
1.7       cvs       938:    switch (status)
1.116     cvs       939:      {
                    940:         case HT_LOADED:
                    941:           if (PROT_TRACE)
                    942:             HTTrace ("Load End.... OK: `%s\' has been accessed\n",
                    943:                      me->status_urlName);
1.4       cvs       944: 
1.116     cvs       945:           docid_status = GetDocIdStatus (me->docid,
                    946:                                          Amaya->docid_status);
1.7       cvs       947: 
1.116     cvs       948:           if (docid_status != NULL && docid_status->counter > 1)
                    949:             TtaSetStatus (me->docid, 1, 
                    950:                           TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
1.74      cvs       951:                           me->status_urlName);
1.7       cvs       952:               break;
1.116     cvs       953:               
                    954:      case HT_NO_DATA:
                    955:        if (PROT_TRACE)
                    956:         HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n", 
                    957:                  me->status_urlName);
                    958:        TtaSetStatus (me->docid, 1, 
                    959:                     TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
                    960:                     me->status_urlName);
                    961:        break;
                    962:        
                    963:      case HT_INTERRUPTED:
                    964:        if (PROT_TRACE)
                    965:         HTTrace ("Load End.... INTERRUPTED: `%s\'\n", 
                    966:                  me->status_urlName);
                    967:        TtaSetStatus (me->docid, 1, 
                    968:                     TtaGetMessage (AMAYA, AM_LOAD_ABORT), 
                    969:                     NULL);
                    970:        break;
1.7       cvs       971: 
1.116     cvs       972:      case HT_RETRY:
                    973:        if (PROT_TRACE)
                    974:         HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
                    975:                  HTResponse_retryTime (response));
                    976:        TtaSetStatus (me->docid, 1, 
                    977:                     TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
                    978:                     me->status_urlName);
                    979:        break;
1.7       cvs       980: 
1.116     cvs       981:      case HT_ERROR:
                    982:        cbf = HTAlert_find (HT_A_MESSAGE);
                    983:        if (cbf)
                    984:         (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    985:                 HTRequest_error (request), NULL);
                    986:        break;
                    987:        
                    988:        if (PROT_TRACE)
                    989:         HTTrace ("Load End.... ERROR: Can't access `%s\'\n",
                    990:                  me->status_urlName ? me->status_urlName :"<UNKNOWN>");
                    991:        TtaSetStatus (me->docid, 1,
                    992:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                    993:                     me->status_urlName ? me->status_urlName : "<UNKNOWN>");
                    994:        break;
                    995:      default:
                    996:        if (PROT_TRACE)
                    997:         HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
                    998:        break;
                    999:      }
                   1000:    
                   1001:    return HT_OK;
                   1002: }
1.7       cvs      1003: 
1.116     cvs      1004: #ifdef DEBUG_LIBWWW
                   1005: static  int LineTrace (const char * fmt, va_list pArgs)
                   1006: {
                   1007:     return (vfprintf(stderr, fmt, pArgs));
1.4       cvs      1008: }
1.116     cvs      1009: #endif DEBUG_LIBWWW
1.4       cvs      1010: 
1.27      cvs      1011: /*----------------------------------------------------------------------
                   1012:   AHTAcceptTypesInit
1.74      cvs      1013:   This function prepares the Accept header used by Amaya during
                   1014:   the HTTP content negotiation phase
1.27      cvs      1015:   ----------------------------------------------------------------------*/
                   1016: #ifdef __STDC__
                   1017: static void           AHTAcceptTypesInit (HTList *c)
                   1018: #else  /* __STDC__ */
                   1019: static void           AHTAcceptTypesInit (c)
                   1020: HTList             *c;
                   1021: #endif /* __STDC__ */
                   1022: {
                   1023:   if (c == (HTList *) NULL) 
                   1024:       return;
                   1025: 
                   1026:       /* define here all the mime types that Amaya can accept */
                   1027: 
1.74      cvs      1028:       HTConversion_add (c, "image/png",  "www/present", 
                   1029:                        HTThroughLine, 1.0, 0.0, 0.0);
                   1030:       HTConversion_add (c, "image/jpeg", "www/present", 
                   1031:                        HTThroughLine, 1.0, 0.0, 0.0);
                   1032:       HTConversion_add (c, "image/gif",  "www/present", 
                   1033:                        HTThroughLine, 1.0, 0.0, 0.0);
                   1034:       HTConversion_add (c, "image/xbm",  "www/present", 
                   1035:                        HTThroughLine, 1.0, 0.0, 0.0);
                   1036:       HTConversion_add (c, "image/xpm",  "www/present", 
                   1037:                        HTThroughLine, 1.0, 0.0, 0.0);
                   1038:       HTConversion_add (c, "application/postscript",  
                   1039:                        "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1.4       cvs      1040: 
1.27      cvs      1041:    /* Define here the equivalences between MIME types and file extensions for
                   1042:     the types that Amaya can display */
                   1043: 
                   1044:    /* Register the default set of file suffix bindings */
                   1045:    HTFileInit ();
                   1046: 
                   1047:    /* Don't do any case distinction */
                   1048:    HTBind_caseSensitive (FALSE);
                   1049: }
1.4       cvs      1050: 
1.5       cvs      1051: /*----------------------------------------------------------------------
1.17      cvs      1052:   AHTConverterInit
                   1053:   Bindings between a source media type and a destination media type
                   1054:   (conversion).
1.5       cvs      1055:   ----------------------------------------------------------------------*/
1.15      cvs      1056: #ifdef __STDC__
                   1057: static void         AHTConverterInit (HTList *c)
                   1058: #else  /* __STDC__ */
                   1059: static void         AHTConverterInit (c)
                   1060: HTList             *c;
                   1061: #endif /* __STDC__ */
1.4       cvs      1062: {
                   1063: 
                   1064:    /* Handler for custom http error messages */
1.7       cvs      1065:    HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4       cvs      1066: 
                   1067:    /*
                   1068:     ** These are converters that converts to something other than www/present,
                   1069:     ** that is not directly outputting someting to the user on the screen
                   1070:     */
                   1071: 
1.116     cvs      1072:    HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 
                   1073:                     1.0, 0.0, 0.0);
1.4       cvs      1074:    HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter,
                   1075:                     1.0, 0.0, 0.0);
                   1076:    HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader,
                   1077:                     1.0, 0.0, 0.0);
1.116     cvs      1078:    HTConversion_add(c,"message/x-rfc822-cont", "*/*", HTMIMEContinue,  
                   1079:                    1.0, 0.0, 0.0);
                   1080:    HTConversion_add(c,"message/x-rfc822-partial","*/*",        HTMIMEPartial,
                   1081:                    1.0, 0.0, 0.0);
1.4       cvs      1082:    HTConversion_add (c, "multipart/*", "*/*", HTBoundary,
                   1083:                     1.0, 0.0, 0.0);
                   1084:    HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML,
                   1085:                     1.0, 0.0, 0.0);
                   1086: 
                   1087:    /*
1.116     cvs      1088:    ** The following conversions are converting ASCII output from various
                   1089:    ** protocols to HTML objects.
                   1090:    */
1.4       cvs      1091:    HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new,
                   1092:                     1.0, 0.0, 0.0);
                   1093: 
                   1094:    /*
1.116     cvs      1095:    ** We also register a special content type guess stream that can figure out
                   1096:    ** the content type by reading the first bytes of the stream
                   1097:    */
1.4       cvs      1098:    HTConversion_add (c, "www/unknown", "*/*", HTGuess_new,
                   1099:                     1.0, 0.0, 0.0);
                   1100: 
1.116     cvs      1101: #ifdef AMAYA_WWW_CACHE
1.4       cvs      1102:    /*
1.116     cvs      1103:    ** Register a persistent cache stream which can save an object to local
                   1104:    ** file
                   1105:    */
1.4       cvs      1106:    HTConversion_add (c, "www/cache", "*/*", HTCacheWriter,
                   1107:                     1.0, 0.0, 0.0);
1.116     cvs      1108:    HTConversion_add(c,"www/cache-append", "*/*", HTCacheAppend,
                   1109:                    1.0, 0.0, 0.0);
                   1110: #endif AMAYA_WWW_CACHE
1.4       cvs      1111: 
                   1112:    /*
1.116     cvs      1113:    ** This dumps all other formats to local disk without any further
                   1114:    ** action taken
                   1115:    */
1.4       cvs      1116:    HTConversion_add (c, "*/*", "www/present", HTSaveLocally,
                   1117:                     0.3, 0.0, 0.0);
1.116     cvs      1118:    
1.4       cvs      1119: }
                   1120: 
1.27      cvs      1121: 
1.15      cvs      1122: /*----------------------------------------------------------------------
1.17      cvs      1123:   AHTProtocolInit
                   1124:   Registers all amaya supported protocols.
1.15      cvs      1125:   ----------------------------------------------------------------------*/
1.4       cvs      1126: static void         AHTProtocolInit (void)
                   1127: {
1.116     cvs      1128:   char *strptr;
1.4       cvs      1129: 
1.116     cvs      1130:   /* 
                   1131:      NB. Preemptive == YES means Blocking requests
                   1132:      Non-preemptive == NO means Non-blocking requests
                   1133:      */
                   1134:   HTTransport_add("tcp", HT_TP_SINGLE, HTReader_new, HTWriter_new);
                   1135:   HTTransport_add("buffered_tcp", HT_TP_SINGLE, HTReader_new, 
                   1136:                  HTBufferWriter_new);
                   1137:   HTProtocol_add ("http", "buffered_tcp", HTTP_PORT, NO, HTLoadHTTP, NULL);
                   1138:   HTProtocol_add ("file", "local", 0, NO, HTLoadFile, NULL);
                   1139: #ifdef AMAYA_WWW_CACHE
                   1140:    HTProtocol_add("cache",  "local", 0, YES, HTLoadCache, NULL);
                   1141: #endif /* AMAYA_WWW_CACHE */
1.103     cvs      1142: #if 0 /* experimental code */
1.116     cvs      1143:    HTProtocol_add ("ftp", "tcp", FTP_PORT, NO, HTLoadFTP, NULL);
1.17      cvs      1144: #endif
1.116     cvs      1145: 
                   1146:    /* initialize pipelining */
1.121     cvs      1147:   strptr = (char *) TtaGetEnvString ("ENABLE_PIPELINING");
1.116     cvs      1148:   if (strptr && *strptr && strcasecmp (strptr,"yes" ))
                   1149:     HTTP_setConnectionMode (HTTP_11_NO_PIPELINING);
1.4       cvs      1150: }
                   1151: 
1.15      cvs      1152: /*----------------------------------------------------------------------
1.17      cvs      1153:   AHTNetInit
                   1154:   Reegisters "before" and "after" request filters.
1.15      cvs      1155:   ----------------------------------------------------------------------*/
1.4       cvs      1156: static void         AHTNetInit (void)
                   1157: {
                   1158: 
                   1159: /*      Register BEFORE filters
1.74      cvs      1160: **      The BEFORE filters handle proxies, caches, rule files etc.
                   1161: **      The filters are called in the order by which the are registered
                   1162: **      Not done automaticly - may be done by application!
                   1163: */
1.4       cvs      1164: 
1.116     cvs      1165: #ifdef AMAYA_WWW_CACHE  
                   1166:   HTNet_addBefore (HTCacheFilter, "http://*", NULL, HT_FILTER_MIDDLE);
                   1167: #endif /* AMAYA_WWW_CACHE */
                   1168:   HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, HT_FILTER_LATE);
                   1169:   HTNet_addBefore (HTProxyFilter, NULL, NULL, HT_FILTER_LATE);
1.85      cvs      1170:   HTHost_setActivateRequestCallback (AHTOpen_file);
1.4       cvs      1171: 
                   1172: /*      register AFTER filters
1.74      cvs      1173: **      The AFTER filters handle error messages, logging, redirection,
                   1174: **      authentication etc.
                   1175: **      The filters are called in the order by which the are registered
                   1176: **      Not done automaticly - may be done by application!
                   1177: */
1.4       cvs      1178: 
1.116     cvs      1179:   HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS,
                   1180:                  HT_FILTER_MIDDLE);
                   1181:   HTNet_addAfter(HTAuthFilter, "http://*", NULL, HT_REAUTH,
                   1182:                 HT_FILTER_MIDDLE);
                   1183:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT,
                   1184:                  HT_FILTER_MIDDLE);
                   1185:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_FOUND, 
                   1186:                  HT_FILTER_MIDDLE);
                   1187:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_SEE_OTHER,
                   1188:                  HT_FILTER_MIDDLE);
                   1189:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT,
                   1190:                  HT_FILTER_MIDDLE);
                   1191:   HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY,
                   1192:                  HT_FILTER_MIDDLE);
                   1193: #ifdef AMAYA_WWW_CACHE
                   1194:   HTNet_addAfter (HTCacheUpdateFilter, "http://*", NULL, HT_NOT_MODIFIED, 
                   1195:                  HT_FILTER_MIDDLE);
                   1196: #endif /* AMAYA_WWW_CACHE */
1.111     cvs      1197: #ifndef _WINDOWS
1.116     cvs      1198:   HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, 
                   1199:                   HT_FILTER_LAST);     
1.111     cvs      1200: #endif /* !_WINDOWS */
1.116     cvs      1201:    /**** for later ?? ****/
                   1202:    /*  HTNet_addAfter(HTInfoFilter,    NULL,           NULL, HT_ALL,           HT_FILTER_LATE); */
1.51      cvs      1203:    /* handles all errors */
1.116     cvs      1204:   HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4       cvs      1205: }
                   1206: 
1.15      cvs      1207: /*----------------------------------------------------------------------
1.17      cvs      1208:   AHTAlertInit
                   1209:   Register alert messages and their callbacks.
1.15      cvs      1210:   ----------------------------------------------------------------------*/
                   1211: #ifdef __STDC__
                   1212: static void         AHTAlertInit (void)
                   1213: #else
                   1214: static void         AHTAlertInit ()
                   1215: #endif
                   1216: {
                   1217:    HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.120     cvs      1218: #ifdef __WINDOWS
1.70      cvs      1219:    HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
1.116     cvs      1220: #endif /* _WINDOWS */
1.15      cvs      1221:    HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48      cvs      1222:    HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG));       /* process all messages except debug ones*/
1.15      cvs      1223:    HTAlert_add (AHTConfirm, HT_A_CONFIRM);
                   1224:    HTAlert_add (AHTPrompt, HT_A_PROMPT);
                   1225:    HTAlert_add (AHTPromptPassword, HT_A_SECRET);
                   1226:    HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
1.116     cvs      1227: }
                   1228: 
1.128     cvs      1229: /*----------------------------------------------------------------------
                   1230:   libwww_CleanCache
                   1231:   frontend to the recursive cache cleaning function
                   1232:   ----------------------------------------------------------------------*/
                   1233: #ifdef __STDC__
                   1234: void libwww_CleanCache (void)
                   1235: #else
                   1236: void libwww_CleanCache ()
                   1237: Document doc;
                   1238: View view;
                   1239: #endif /* __STDC__ */
                   1240: {
1.130     cvs      1241: #ifdef AMAYA_WWW_CACHE
1.128     cvs      1242:   char *strptr;
                   1243:   char *cache_dir;
                   1244:   int cache_size;
1.130     cvs      1245: 
1.128     cvs      1246:   if (!HTCacheMode_enabled ())
                   1247:     /* don't do anything if we're not using a cache */
                   1248:     return;
                   1249:   /* temporarily close down the cache, purge it, then restart */
1.130     cvs      1250:   cache_dir = TtaStrdup ( (char *) HTCacheMode_getRoot ());
1.128     cvs      1251:   cache_size = HTCacheMode_maxSize ();
1.130     cvs      1252:   /* remove the concurrent cache lock */
                   1253: #ifdef DEBUG_LIBWWW
                   1254:   fprintf (stderr, "Clearing the cache lock\n");
                   1255: #endif /* DEBUG_LIBWWW */
                   1256:   clear_cachelock ();
1.128     cvs      1257:   HTCacheTerminate ();
1.130     cvs      1258:   HTCacheMode_setEnabled (NO);
                   1259:   strptr = TtaGetMemory (strlen (cache_dir) + 20);
1.128     cvs      1260:   strcpy (strptr, cache_dir);
                   1261:   strcat (strptr, DIR_STR);
                   1262: 
                   1263:   RecCleanCache (strptr);
                   1264: 
                   1265:   HTCacheMode_setExpires(HT_EXPIRES_AUTO);
                   1266:   HTCacheInit (cache_dir, cache_size);
1.130     cvs      1267:   /* set a new concurrent cache lock */
1.132     cvs      1268:   strcat (strptr, ".lock");
1.130     cvs      1269:   if (set_cachelock (strptr) == -1)
                   1270:     /* couldn't open the .lock file, so, we close the cache to
                   1271:        be in the safe side */
                   1272:     {
                   1273: #ifdef DEBUG_LIBWWW
                   1274:       fprintf (stderr, "couldnt set the cache lock\n");
                   1275: #endif /* DEBUG_LIBWWW */
                   1276:       HTCacheTerminate ();
                   1277:       HTCacheMode_setEnabled (NO);
                   1278:     }
                   1279: #ifdef DEBUG_LIBWWW
                   1280:   fprintf (stderr, "set a cache lock\n");
                   1281: #endif /* DEBUG_LIBWWW */
                   1282:   TtaFreeMemory (strptr);
1.128     cvs      1283:   TtaFreeMemory (cache_dir);
                   1284: #endif /* AMAYA_WWW_CACHE */
                   1285: }
                   1286: 
1.118     cvs      1287: #ifdef AMAYA_WWW_CACHE
                   1288: /*----------------------------------------------------------------------
1.128     cvs      1289:   RecCleanCache
1.118     cvs      1290:   Clears an existing cache directory
                   1291:   ----------------------------------------------------------------------*/
                   1292: #ifdef __STDC__
1.128     cvs      1293: static void RecCleanCache (char *dirname)
1.118     cvs      1294: #else
1.128     cvs      1295: static void RecCleanCache (char *dirname)
                   1296: Document doc;
                   1297: View view;
1.118     cvs      1298: #endif /* __STDC__ */
1.128     cvs      1299: 
                   1300: #ifdef _WINDOWS
                   1301: {
                   1302:   HANDLE hFindFile;
                   1303:   boolean status;
                   1304:   WIN32_FIND_DATA ffd;
                   1305:   
                   1306:   char t_dir [MAX_LENGTH];
1.130     cvs      1307:   char *ptr;
1.128     cvs      1308: 
1.130     cvs      1309:   /* create a t_dir name to start searching for files */
1.128     cvs      1310:   if ((strlen (dirname) + 10) > MAX_LENGTH)
                   1311:     /* ERROR: directory name is too big */
                   1312:     return;
                   1313: 
                   1314:   strcpy (t_dir, dirname);
1.130     cvs      1315:   /* save the end of the dirname. We'll use it to make
                   1316:      a complete pathname when erasing files */
                   1317:   ptr = &t_dir[strlen (t_dir)];
                   1318:   strcat (t_dir, "*");
1.128     cvs      1319: 
1.130     cvs      1320:   hFindFile = FindFirstFile (t_dir, &ffd);
1.128     cvs      1321:     
                   1322:   if (hFindFile == INVALID_HANDLE_VALUE)
                   1323:     /* nothing to erase? */
                   1324:     return;
                   1325: 
                   1326:   status = TRUE;
1.130     cvs      1327:   while (status) 
                   1328:     {
                   1329:       if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1.133     cvs      1330:        {
                   1331:          /* it's a directory, erase it recursively */
                   1332:          if (strcmp (ffd.cFileName, "..") && strcmp (ffd.cFileName, "."))
                   1333:            {
                   1334:              strcpy (ptr, ffd.cFileName);
                   1335:              strcat (ptr, DIR_STR);
                   1336:              RecCleanCache (t_dir);
                   1337:              rmdir (t_dir);
                   1338:            }
                   1339:        }
                   1340:        else
1.130     cvs      1341:          {
1.133     cvs      1342:            /* it's a file, erase it */
1.130     cvs      1343:            strcpy (ptr, ffd.cFileName);
1.133     cvs      1344:            TtaFileUnlink (t_dir);
1.130     cvs      1345:          }
                   1346:       status = FindNextFile (hFindFile, &ffd);
                   1347:     }
1.128     cvs      1348:   FindClose (hFindFile);
                   1349: }
                   1350: 
                   1351: #else /* _WINDOWS */
1.118     cvs      1352: {
                   1353:   DIR *dp;
                   1354:   struct stat st;
                   1355: #ifdef HAVE_DIRENT_H
                   1356:   struct dirent *d;
                   1357: #else
                   1358:   struct direct *d;
                   1359: #endif /* HAVE_DIRENT_H */
1.128     cvs      1360:   char filename[BUFSIZ+1];
1.118     cvs      1361: 
                   1362:   if ((dp = opendir (dirname)) == NULL) 
                   1363:     {
                   1364:       /* @@@ we couldn't open the directory ... we need some msg */
                   1365:       perror (dirname);
                   1366:       exit;
                   1367:     }
                   1368:   
                   1369:   while ((d = readdir (dp)) != NULL)
                   1370:     {
                   1371:       /* skip the UNIX . and .. links */
                   1372:       if (!strcmp (d->d_name, "..")
                   1373:          || !strcmp (d->d_name, "."))
                   1374:        continue;
                   1375: 
1.128     cvs      1376:       sprintf (filename, "%s"DIR_STR"%s", dirname, d->d_name);
1.118     cvs      1377:       if  (lstat (filename, &st) < 0 ) 
                   1378:        {
                   1379:          /* @@2 need some error message */
                   1380:          perror (filename);
                   1381:          continue;
                   1382:        }
                   1383:       
                   1384:       switch (st.st_mode & S_IFMT)
                   1385:        {
                   1386:        case S_IFDIR:
                   1387:          /* if we find a directory, we erase it, recursively */
1.128     cvs      1388:          strcat (filename, DIR_STR);
                   1389:          RecCleanCache (filename);
                   1390:          rmdir (filename);
1.118     cvs      1391:          break;
                   1392:        case S_IFLNK:
                   1393:          /* skip any links we find */
                   1394:          continue;
                   1395:          break;
                   1396:        default:
                   1397:          /* erase the filename */
                   1398:          TtaFileUnlink (filename);
                   1399:          break;
                   1400:        }
                   1401:     }
                   1402:   closedir (dp);
                   1403: }
1.128     cvs      1404: #endif /* _WINDOWS */
1.118     cvs      1405: #endif /* AMAYA_WWW_CACHE */
                   1406: 
1.116     cvs      1407: /*----------------------------------------------------------------------
                   1408:   CacheInit
                   1409:   Reads the cache settings from the thot.ini file.
                   1410:   ----------------------------------------------------------------------*/
1.118     cvs      1411: #ifdef __STDC__
1.116     cvs      1412: static void CacheInit (void)
1.118     cvs      1413: #else
                   1414: static void Cacheinit ()
                   1415: #endif
                   1416: 
1.116     cvs      1417: {
                   1418: #ifndef AMAYA_WWW_CACHE
                   1419:    HTCacheMode_setEnabled (NO);
                   1420: 
                   1421: #else /* AMAYA_WWW_CACHE */
                   1422:   char *strptr;
                   1423:   char *cache_dir = NULL;
1.130     cvs      1424:   char *cache_lockfile;
                   1425:   int cache_size;
                   1426:   boolean cache_enabled;
                   1427:   boolean cache_locked;
                   1428: 
1.116     cvs      1429:   /* activate cache? */
                   1430:   strptr = (char *) TtaGetEnvString ("ENABLE_CACHE");
                   1431:   if (strptr && *strptr && strcasecmp (strptr,"yes" ))
1.130     cvs      1432:     cache_enabled = NO;
1.116     cvs      1433:   else
1.130     cvs      1434:     cache_enabled = YES;
1.116     cvs      1435: 
                   1436:   /* get the cache dir (or use a default one) */
                   1437:   strptr = (char *) TtaGetEnvString ("CACHE_DIR");
                   1438:   if (strptr && *strptr) 
                   1439:     {
                   1440:       cache_dir = TtaGetMemory (strlen (strptr) + strlen (CACHE_DIR_NAME) + 1);
                   1441:       strcpy (cache_dir, strptr);
                   1442:     }
                   1443:   else
                   1444:     {
1.122     cvs      1445:       cache_dir = TtaGetMemory (strlen (TempFileDirectory) 
1.116     cvs      1446:                                + strlen (CACHE_DIR_NAME) + 1);
1.122     cvs      1447:       strcpy (cache_dir, TempFileDirectory);
1.116     cvs      1448:     }
                   1449:   strcat (cache_dir, CACHE_DIR_NAME);
                   1450: 
                   1451:   /* get the cache size (or use a default one) */
                   1452:     strptr = (char *) TtaGetEnvString ("CACHE_SIZE");
                   1453:   if (strptr && *strptr) 
                   1454:       cache_size = atoi (strptr);
                   1455:   else
                   1456:     cache_size = DEFAULT_CACHE_SIZE;
                   1457: 
1.130     cvs      1458:   if (cache_enabled) 
1.116     cvs      1459:     {
                   1460:       /* how to remove the lock? force remove it? */
1.130     cvs      1461:       cache_lockfile = TtaGetMemory (strlen (cache_dir) + 20);
                   1462:       strcpy (cache_lockfile, cache_dir);
                   1463:       strcat (cache_lockfile, DIR_STR".lock");
                   1464:       cache_locked = FALSE;
                   1465:       if (TtaFileExist (cache_lockfile)
                   1466:          && !(cache_locked = test_cachelock (cache_lockfile)))
1.116     cvs      1467:        {
1.130     cvs      1468: #ifdef DEBUG_LIBWWW
                   1469:          fprintf (stderr, "found a stale cache, removing it\n");
                   1470: #endif /* DEBUG_LIBWWW */
                   1471:          /* remove the lock and clean the cache (the clean cache 
                   1472:             will remove all, making the following call unnecessary */
                   1473:          /* little trick to win some memory */
                   1474:          strptr = strrchr (cache_lockfile, '.');
                   1475:          *strptr = EOS;
                   1476:          RecCleanCache (cache_lockfile);
                   1477:          *strptr = '.';
1.116     cvs      1478:        }
                   1479: 
1.130     cvs      1480:       if (!cache_locked) 
                   1481:        {
                   1482:          /* initialize the cache if there's no other amaya
                   1483:             instance running */
                   1484:          HTCacheInit (cache_dir, cache_size);
                   1485:          HTCacheMode_setExpires(HT_EXPIRES_AUTO);
                   1486:          if (set_cachelock (cache_lockfile) == -1)
                   1487:            /* couldn't open the .lock file, so, we close the cache to
                   1488:               be in the safe side */
                   1489:            {
                   1490:              HTCacheTerminate ();
                   1491:              HTCacheMode_setEnabled (NO);
                   1492: #ifdef DEBUG_LIBWWW
                   1493:              fprintf (stderr, "couldnt set the cache lock\n");
                   1494: #endif /* DEBUG_LIBWWW */
                   1495:            }
                   1496: #ifdef DEBUG_LIBWWW
                   1497:          else
                   1498: 
                   1499:            fprintf (stderr, "created the cache lock\n");
                   1500: #endif /* DEBUG_LIBWWW */
                   1501:        }
                   1502:       else 
                   1503:        {
                   1504:          HTCacheMode_setEnabled (NO);
                   1505: #ifdef DEBUG_LIBWWW
                   1506:          fprintf (stderr, "lock detected, starting libwww without a cache/n");
                   1507: #endif /* DEBUG_LIBWWW */
                   1508:        }
                   1509:       TtaFreeMemory (cache_lockfile);
1.116     cvs      1510:     }
                   1511:   else
                   1512:       HTCacheMode_setEnabled (NO);
                   1513: 
                   1514:   if (cache_dir)
                   1515:     TtaFreeMemory (cache_dir);
                   1516: #endif /* AMAYA_WWW_CACHE */
1.15      cvs      1517: }
                   1518: 
                   1519: /*----------------------------------------------------------------------
1.97      cvs      1520:   ProxyInit
                   1521:   Reads any proxies settings which may be declared as environmental
                   1522:   variables or in the thot.ini file. The former overrides the latter.
                   1523:   ----------------------------------------------------------------------*/
1.118     cvs      1524: #ifdef __STDC__
1.97      cvs      1525: static void ProxyInit (void)
1.118     cvs      1526: #else
                   1527: static void ProxyInit ()
                   1528: #endif /* __STDC__ */
1.97      cvs      1529: {
                   1530:   char *strptr;
                   1531:   char *str = NULL;
                   1532:   char *name;
                   1533: 
                   1534:   /* get the proxy settings from the thot.ini file */
                   1535:   strptr = (char *) TtaGetEnvString ("HTTP_PROXY");
                   1536:   if (strptr && *strptr)
                   1537:     HTProxy_add ("http", strptr);
                   1538:   /* get the no_proxy settings from the thot.ini file */
                   1539:   strptr = (char *) TtaGetEnvString ("NO_PROXY");
                   1540:   if (strptr && *strptr) 
                   1541:     {
                   1542:       str = TtaStrdup (strptr);          /* Get copy we can mutilate */
                   1543:       strptr = str;
                   1544:       while ((name = HTNextField (&strptr)) != NULL) {
                   1545:        char *portstr = strchr (name, ':');
                   1546:        unsigned port=0;
                   1547:        if (portstr) {
                   1548:          *portstr++ = '\0';
1.116     cvs      1549:          if (*portstr) port = (unsigned) atoi (portstr);
1.97      cvs      1550:        }
                   1551:        /* Register it for all access methods */
                   1552:        HTNoProxy_add (name, NULL, port);
                   1553:       }
                   1554:       TtaFreeMemory (str);
                   1555:     }
                   1556:   
                   1557:   /* use libw3's routine to get all proxy settings from the environment */
                   1558:    HTProxy_getEnvVar ();
                   1559: }
                   1560: 
                   1561: 
                   1562: /*----------------------------------------------------------------------
1.17      cvs      1563:   AHTProfile_newAmaya
                   1564:   creates the Amaya client profile for libwww.
1.15      cvs      1565:   ----------------------------------------------------------------------*/
                   1566: #ifdef __STDC__
                   1567: static void         AHTProfile_newAmaya (char *AppName, char *AppVersion)
                   1568: #else  /* __STDC__ */
                   1569: static void         AHTProfile_newAmaya (AppName, AppVersion)
                   1570: char               *AppName;
                   1571: char               *AppVersion;
                   1572: #endif /* __STDC__ */
1.4       cvs      1573: {
                   1574:    /* If the Library is not already initialized then do it */
                   1575:    if (!HTLib_isInitialized ())
                   1576:       HTLibInit (AppName, AppVersion);
                   1577: 
                   1578:    if (!converters)
                   1579:       converters = HTList_new ();
1.27      cvs      1580:    if (!acceptTypes)
                   1581:       acceptTypes = HTList_new ();
1.4       cvs      1582:    if (!encodings)
                   1583:       encodings = HTList_new ();
                   1584: 
                   1585:    /* Register the default set of transport protocols */
                   1586:    HTTransportInit ();
                   1587: 
                   1588:    /* Register the default set of application protocol modules */
                   1589:    AHTProtocolInit ();
                   1590: 
1.116     cvs      1591:    /* Register the default set of messages and dialog functions */
                   1592:    AHTAlertInit ();
                   1593:    HTAlert_setInteractive (YES);
                   1594: 
                   1595: #ifdef AMAYA_WWW_CACHE
                   1596:    /* Enable the persistent cache  */
                   1597:    CacheInit ();
                   1598: #else
                   1599:    HTCacheMode_setEnabled (NO);
                   1600: #endif /* AMAYA_WWW_CACHE */
1.4       cvs      1601: 
                   1602:    /* Register the default set of BEFORE and AFTER filters */
                   1603:    AHTNetInit ();
                   1604: 
                   1605:    /* Set up the default set of Authentication schemes */
                   1606:    HTAAInit ();
                   1607: 
1.97      cvs      1608:    /* Get any proxy settings */
                   1609:    ProxyInit ();
1.4       cvs      1610: 
                   1611:    /* Register the default set of converters */
                   1612:    AHTConverterInit (converters);
1.27      cvs      1613:    AHTAcceptTypesInit (acceptTypes);
1.4       cvs      1614:    HTFormat_setConversion (converters);
                   1615: 
                   1616:    /* Register the default set of transfer encoders and decoders */
1.116     cvs      1617:    HTTransferEncoderInit (encodings);  /* chunks ??? */
1.4       cvs      1618:    HTFormat_setTransferCoding (encodings);
                   1619: 
                   1620:    /* Register the default set of MIME header parsers */
1.74      cvs      1621:    HTMIMEInit ();   /* must be called again for language selector */
1.4       cvs      1622: 
                   1623:    /* Register the default set of Icons for directory listings */
1.27      cvs      1624:    /*HTIconInit(NULL); *//* experimental */
1.4       cvs      1625: 
                   1626: }
                   1627: 
1.5       cvs      1628: /*----------------------------------------------------------------------
1.17      cvs      1629:   AHTProfile_delete
                   1630:   deletes the Amaya client profile.
1.5       cvs      1631:   ----------------------------------------------------------------------*/
1.4       cvs      1632: #ifdef __STDC__
                   1633: static void         AHTProfile_delete (void)
                   1634: #else
                   1635: static void         AHTProfile_delete ()
1.7       cvs      1636: #endif                         /* __STDC__ */
1.4       cvs      1637: {
1.22      cvs      1638:  
                   1639:   /* free the Amaya global context */
1.74      cvs      1640:   if (!converters)
                   1641:     HTConversion_deleteAll (converters);
                   1642:   if (!acceptTypes)
                   1643:     HTConversion_deleteAll (acceptTypes);
                   1644:   if (!encodings)
                   1645:     HTCoding_deleteAll (encodings);
                   1646:   
                   1647:   HTList_delete (Amaya->docid_status);
                   1648:   HTList_delete (Amaya->reqlist);
                   1649:   TtaFreeMemory (Amaya);
                   1650:   {
1.61      cvs      1651: 
1.74      cvs      1652:     if (HTLib_isInitialized ())
                   1653:       
1.120     cvs      1654: #ifdef _WINDOWS
1.61      cvs      1655:       HTEventTerminate ();
1.120     cvs      1656: #endif _WINDOWS;               
1.74      cvs      1657:     
                   1658:     /* Clean up the persistent cache (if any) */
1.116     cvs      1659: #ifdef AMAYA_WWW_CACHE
1.130     cvs      1660:     clear_cachelock ();
1.116     cvs      1661:     HTCacheTerminate ();
                   1662: #endif /* AMAYA_WWW_CACHE */
1.74      cvs      1663:     
                   1664:     /* Clean up all the global preferences */
                   1665:     HTFormat_deleteAll ();
                   1666:     
                   1667:     /* Terminate libwww */
                   1668:     HTLibTerminate ();
                   1669:   }
1.4       cvs      1670: }
                   1671: 
1.5       cvs      1672: /*----------------------------------------------------------------------
1.116     cvs      1673:   AmayacontextInit
                   1674:   initializes an internal Amaya context for our libwww interface 
                   1675:   ----------------------------------------------------------------------*/
                   1676: #ifdef __STDC__
                   1677: static void                AmayaContextInit ()
                   1678: #else
                   1679: static void                AmayaContextInit ()
                   1680: #endif
                   1681: 
                   1682: {
                   1683:   AmayaAlive = TRUE;
                   1684:   /* Initialization of the global context */
                   1685:   Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
                   1686:   Amaya->reqlist = HTList_new ();
                   1687:   Amaya->docid_status = HTList_new ();
                   1688:   Amaya->open_requests = 0;
                   1689: }
                   1690: 
                   1691: /*----------------------------------------------------------------------
1.17      cvs      1692:   QueryInit
                   1693:   initializes the libwww interface 
1.5       cvs      1694:   ----------------------------------------------------------------------*/
1.4       cvs      1695: #ifdef __STDC__
                   1696: void                QueryInit ()
                   1697: #else
                   1698: void                QueryInit ()
                   1699: #endif
                   1700: {
                   1701: 
1.116     cvs      1702:    AmayaContextInit ();
                   1703:    
1.4       cvs      1704:    AHTProfile_newAmaya (HTAppName, HTAppVersion);
1.116     cvs      1705:    
1.69      cvs      1706:    /* New AHTBridge stuff */
1.4       cvs      1707: 
1.116     cvs      1708: #ifdef _WINDOWS
1.120     cvs      1709:    /*** AHTEventInit (); this was the call to my AHTEvent module HTEvtLst today***/
1.125     cvs      1710:    HTEventInit ();
1.116     cvs      1711: #endif /* _WINDOWS */
1.72      cvs      1712: 
1.123     cvs      1713: #ifndef _WINDOWS
1.116     cvs      1714:    HTEvent_setRegisterCallback ((void *) AHTEvent_register);
                   1715:    HTEvent_setUnregisterCallback ((void *) AHTEvent_unregister);
1.120     cvs      1716:    HTTimer_registerSetTimerCallback ((void *) AMAYA_SetTimer);
                   1717:    HTTimer_registerDeleteTimerCallback ((void *) AMAYA_DeleteTimer);
1.116     cvs      1718: #endif /* !_WINDOWS */
                   1719: 
                   1720: #ifdef AMAYA_WWW_CACHE
                   1721:    /*** 
                   1722:        WWW_TraceFlag = CACHE_TRACE;
                   1723:    ***/
                   1724: #endif
1.72      cvs      1725: 
1.74      cvs      1726: #ifdef DEBUG_LIBWWW
1.116     cvs      1727:   /* forwards error messages to our own function */
                   1728:    WWW_TraceFlag = THD_TRACE;
                   1729:   HTTrace_setCallback(LineTrace);
1.74      cvs      1730: #endif
1.4       cvs      1731: 
                   1732:    /* Trace activation (for debugging) */
1.7       cvs      1733:    /*
1.116     cvs      1734:      WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | PROT_TRACE;
                   1735: 
1.4       cvs      1736:       WWW_TraceFlag = SHOW_APP_TRACE | SHOW_UTIL_TRACE |
                   1737:       SHOW_BIND_TRACE | SHOW_THREAD_TRACE |
                   1738:       SHOW_STREAM_TRACE | SHOW_PROTOCOL_TRACE |
                   1739:       SHOW_URI_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1740:       SHOW_CORE_TRACE;
                   1741: 
1.7       cvs      1742:     */
1.4       cvs      1743: 
                   1744:    /***
                   1745:      WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1746:      SHOW_PROTOCOL_TRACE| SHOW_APP_TRACE | SHOW_UTIL_TRACE;
                   1747:      ***/
                   1748: 
                   1749:    /* Setting up other user interfaces */
                   1750: 
                   1751:    /* Setting up different network parameters */
1.17      cvs      1752:    /* Maximum number of simultaneous open sockets */
1.4       cvs      1753:    HTNet_setMaxSocket (8);
1.75      cvs      1754:    /* different network services timeouts */
1.109     cvs      1755:    HTDNS_setTimeout (60);
1.76      cvs      1756:    HTHost_setPersistTimeout (60L);
1.130     cvs      1757:    /* default timeout in ms */
                   1758:    HTHost_setEventTimeout (20000);
1.75      cvs      1759: 
1.4       cvs      1760: #ifdef CATCH_SIG
1.18      cvs      1761:    signal (SIGPIPE, SIG_IGN);
1.4       cvs      1762: #endif
1.15      cvs      1763: }
                   1764: 
1.69      cvs      1765: #ifndef _WINDOWS
1.15      cvs      1766: /*----------------------------------------------------------------------
1.17      cvs      1767:   LoopForStop
1.130     cvs      1768:   a copy of the Thop event loop so we can handle the stop button in Unix.
1.15      cvs      1769:   ----------------------------------------------------------------------*/
                   1770: #ifdef __STDC__
                   1771: static int          LoopForStop (AHTReqContext * me)
                   1772: #else
                   1773: static int          LoopForStop (AHTReqContext * me)
                   1774: #endif
                   1775: {
                   1776: 
1.25      cvs      1777:    extern ThotAppContext app_cont;
1.51      cvs      1778:    XEvent                ev;
                   1779:    XtInputMask           status;
1.17      cvs      1780:    int                 status_req = HT_OK;
1.15      cvs      1781: 
                   1782:    /* to test the async calls  */
1.17      cvs      1783:    /* Loop while waiting for new events, exists when the request is over */
1.15      cvs      1784:    while (me->reqStatus != HT_ABORT &&
                   1785:          me->reqStatus != HT_END &&
1.69      cvs      1786:          me->reqStatus != HT_ERR) {
1.116     cvs      1787:         if (!AmayaIsAlive ())
1.69      cvs      1788:            /* Amaya was killed by one of the callback handlers */
                   1789:            exit (0);
                   1790: 
                   1791:         status = XtAppPending (app_cont);
                   1792:         if (status & XtIMXEvent) {
                   1793:             XtAppNextEvent (app_cont, &ev);
                   1794:            TtaHandleOneEvent (&ev);
                   1795:         } else if (status & (XtIMAll & (~XtIMXEvent))) {
                   1796:                 XtAppProcessEvent (app_cont, (XtIMAll & (~XtIMXEvent)));
                   1797:         } else {
                   1798:                XtAppNextEvent (app_cont, &ev);
                   1799:               TtaHandleOneEvent (&ev);
                   1800:         }
                   1801:    }
1.4       cvs      1802: 
1.69      cvs      1803:    switch (me->reqStatus) {
                   1804:          case HT_ERR:
                   1805:           case HT_ABORT:
1.130     cvs      1806:               status_req = NO;
1.15      cvs      1807:               break;
                   1808: 
1.69      cvs      1809:          case HT_END:
1.130     cvs      1810:               status_req = YES;
1.15      cvs      1811:               break;
                   1812: 
1.69      cvs      1813:          default:
1.15      cvs      1814:               break;
1.69      cvs      1815:    }
1.15      cvs      1816:    return (status_req);
1.4       cvs      1817: }
1.69      cvs      1818: #endif /* _WINDOWS */
1.4       cvs      1819: 
1.5       cvs      1820: /*----------------------------------------------------------------------
1.15      cvs      1821:   QueryClose
1.21      cvs      1822:   closes all existing threads, frees all non-automatically deallocated
                   1823:   memory and then ends libwww.
1.5       cvs      1824:   ----------------------------------------------------------------------*/
1.116     cvs      1825: void QueryClose ()
1.4       cvs      1826: {
1.24      cvs      1827: 
1.116     cvs      1828:   AmayaAlive = FALSE;
1.24      cvs      1829: 
1.116     cvs      1830:   /* remove all the handlers and callbacks that may output a message to
                   1831:      a non-existent Amaya window */
1.130     cvs      1832: #ifndef _WINDOWS
1.116     cvs      1833:   HTNet_deleteAfter (AHTLoadTerminate_handler);
1.130     cvs      1834: #endif _WINDOWS
1.116     cvs      1835:   HTNet_deleteAfter (redirection_handler);
                   1836:   HTAlertCall_deleteAll (HTAlert_global () );
                   1837:   HTAlert_setGlobal ((HTList *) NULL);
                   1838:   HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
                   1839:   HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
                   1840: #ifndef _WINDOWS
                   1841:   /** need to erase all existing timers too **/
                   1842:    HTTimer_registerSetTimerCallback (NULL);
                   1843:    HTTimer_registerDeleteTimerCallback (NULL);
                   1844: #endif /* !_WINDOWS */
                   1845:   HTHost_setActivateRequestCallback (NULL);
                   1846:   Thread_deleteAll ();
1.21      cvs      1847:  
1.116     cvs      1848:   HTProxy_deleteAll ();
                   1849:   HTNoProxy_deleteAll ();
                   1850:   HTGateway_deleteAll ();
                   1851:   AHTProfile_delete ();
                   1852: }
                   1853: 
                   1854: /*----------------------------------------------------------------------
                   1855:   NextNameValue
                   1856:   ---------------------------------------------------------------------*/
                   1857: #ifdef __STDC__
                   1858: static char * NextNameValue (char ** pstr, char **name, char **value)
                   1859: #else
                   1860: static char * NextNameValue (pstr, name, value);
                   1861: char ** pstr;
                   1862: char **name;
                   1863: char **value;
                   1864: #endif /* __STDC__ */
                   1865: {
                   1866:   char * p = *pstr;
                   1867:   char * start = NULL;
                   1868:   if (!pstr || !*pstr) return NULL;
                   1869:   
                   1870:     if (!*p) {
                   1871:       *pstr = p;
                   1872:       *name = NULL;
                   1873:       *value = NULL;
                   1874:       return NULL;                                      /* No field */
                   1875:     }
                   1876:     
                   1877:     /* Now search for the next '&' and ';' delimitators */
                   1878:     start = p;
                   1879:     while (*p && *p != '&' && *p != ';') p++;
                   1880:     if (*p) 
                   1881:       *p++ = '\0';
                   1882:     *pstr = p;
                   1883: 
                   1884:     /* Search for the name and value */
                   1885:     *name = start;
                   1886:     p = start;
                   1887:     
                   1888:     while(*p && *p != '=') 
                   1889:       p++;
                   1890:     if (*p) 
                   1891:       *p++ = '\0';
                   1892:     *value = p;
                   1893: 
                   1894:     return start;
                   1895: }
                   1896: 
                   1897: /*----------------------------------------------------------------------
                   1898:   PrepareFormdata
                   1899:   ---------------------------------------------------------------------*/
                   1900: #ifdef __STDC__
                   1901: static HTAssocList * PrepareFormdata (char *string)
                   1902: #else
                   1903: static HTAssocList * PrepareFormdata (string)
                   1904: char *string;
                   1905: #endif /* __STDC__ */
                   1906: {
                   1907:   char * tmp_string, * tmp_string_ptr;
                   1908:   char * name;
                   1909:   char * value;
                   1910:   HTAssocList * formdata;
                   1911: 
                   1912:   if (!string)
                   1913:     return NULL;
                   1914: 
                   1915:   /* store the ptr in another variable, as the original address will
                   1916:      change
                   1917:      */
                   1918: 
                   1919:   tmp_string_ptr = tmp_string = TtaStrdup (string);
                   1920:   formdata = HTAssocList_new();
                   1921:   
                   1922:   while (*tmp_string)
                   1923:     {
                   1924:       NextNameValue (&tmp_string, &name, &value);
                   1925:       HTAssocList_addObject(formdata,
                   1926:                            name, value);
                   1927:     }
                   1928: 
                   1929:   TtaFreeMemory (tmp_string_ptr);
                   1930:   return formdata;
1.4       cvs      1931: }
                   1932: 
1.99      cvs      1933: 
                   1934: /*----------------------------------------------------------------------
                   1935:   InvokeGetObjectWWW_callback
                   1936:   A simple function to invoke a callback function whenever there's an error
                   1937:   in GetObjectWWW
                   1938:   ---------------------------------------------------------------------*/
                   1939: 
1.116     cvs      1940: #ifdef __STDC__
                   1941: void      InvokeGetObjectWWW_callback (int docid, char *urlName, char *outputfile, TTcbf *terminate_cbf, void *context_tcbf, int status)
1.99      cvs      1942: #else
1.111     cvs      1943: void      InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf, context_tcbf, status)
1.99      cvs      1944: int docid;
                   1945: char *urlName;
                   1946: char *outputfile;
                   1947: TTcbf *terminate_cbf;
                   1948: void *context_tcbf;
1.116     cvs      1949: #endif /* __STDC__ */
1.99      cvs      1950: {
                   1951:   if (!terminate_cbf)
                   1952:     return;
                   1953:   
1.116     cvs      1954:   (*terminate_cbf) (docid, status, urlName, outputfile,
1.99      cvs      1955:                    NULL, context_tcbf);  
                   1956: }
                   1957: 
                   1958: 
                   1959: 
1.5       cvs      1960: /*----------------------------------------------------------------------
1.15      cvs      1961:    GetObjectWWW
1.17      cvs      1962:    this function requests a resource designated by a URLname into a
                   1963:    temporary filename. The download can come from a simple GET operation,
                   1964:    or can come from POSTING/GETTING a form. In the latter
                   1965:    case, the function receives a query string to send to the server.
                   1966: 
1.5       cvs      1967:    4  file retrieval modes are proposed:                              
                   1968:    AMAYA_SYNC : blocking mode                            
                   1969:    AMAYA_ISYNC : incremental, blocking mode              
                   1970:    AMAYA_ASYNC : non-blocking mode                       
                   1971:    AMAYA_IASYNC : incremental, non-blocking mode         
                   1972:    
                   1973:    In the incremental mode, each time a package arrives, it will be   
                   1974:    stored in the temporary file. In addition, if an                   
                   1975:    incremental_callback function is defined, this function will be    
                   1976:    called and handled a copy of the newly received data package.      
                   1977:    Finally, if a terminate_callback function is defined, it will be   
                   1978:    invoked when the request terminates. The caller of this function
1.4       cvs      1979:    can define two different contexts to be passed to the callback
                   1980:    functions.
                   1981: 
                   1982:    When the function is called with the SYNC mode, the function will
                   1983:    return only when the requested file has been loaded.
                   1984:    The ASYNC mode will immediately return after setting up the
                   1985:    call.
                   1986: 
                   1987:    Notes:
                   1988:    At the end of a succesful request, the urlName string contains the
                   1989:    name of the actually retrieved URL. As a URL can change over the time,
                   1990:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      1991:    caller verify the value of the urlName variable at the end of
1.4       cvs      1992:    a request.
                   1993: 
                   1994:    Inputs:
                   1995:    - docid  Document identifier for the set of objects being
                   1996:    retrieved.
                   1997:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   1998:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   1999:    - mode The retrieval mode.
                   2000:    - incremental_cbf 
                   2001:    - context_icbf
                   2002:    Callback and context for the incremental modes
                   2003:    - terminate_cbf 
                   2004:    - context_icbf
                   2005:    Callback and context for a terminate handler
1.17      cvs      2006:    -error_html if TRUE, then display any server error message as an
                   2007:    HTML document.
1.88      cvs      2008:    - content_type a string
                   2009:  
1.4       cvs      2010:    Outputs:
                   2011:    - urlName The URL that was retrieved
                   2012:    - outputfile The name of the temporary file which holds the
                   2013:    retrieved data. (Only in case of success)
1.88      cvs      2014:    - if content_type wasn't NULL, it will contain a copy of the parameter
                   2015:      sent in the HTTP answer
1.4       cvs      2016:    Returns:
                   2017:    HT_ERROR
                   2018:    HT_OK
1.5       cvs      2019:  
                   2020:   ----------------------------------------------------------------------*/
1.4       cvs      2021: #ifdef __STDC__
1.127     cvs      2022: int GetObjectWWW (int docid, char* urlName, char* formdata,
1.116     cvs      2023:                  char* outputfile, int mode, TIcbf* incremental_cbf, 
                   2024:                  void* context_icbf, TTcbf* terminate_cbf, 
1.88      cvs      2025:                  void* context_tcbf, boolean error_html, char *content_type)
1.4       cvs      2026: #else
1.127     cvs      2027: int GetObjectWWW (docid, urlName, formdata, outputfile, mode, 
1.116     cvs      2028:                  incremental_cbf, context_icbf, 
1.88      cvs      2029:                  terminate_cbf, context_tcbf, error_html, content_type)
1.73      cvs      2030: int           docid;
                   2031: char         *urlName;
1.127     cvs      2032: char         *formdata;
1.73      cvs      2033: char         *outputfile;
                   2034: int           mode;
                   2035: TIcbf        *incremental_cbf;
                   2036: void         *context_icbf;
                   2037: TTcbf        *terminate_cbf;
                   2038: void         *context_tcbf;
                   2039: boolean       error_html;
1.88      cvs      2040: char        *content_type;
1.4       cvs      2041: #endif
                   2042: {
                   2043:    AHTReqContext      *me;
                   2044:    char               *ref;
1.107     cvs      2045:    int                 status, l;
1.114     cvs      2046:    int                 tempsubdir;
1.7       cvs      2047: 
1.116     cvs      2048:    if (urlName == NULL || docid == 0 || outputfile == NULL) 
                   2049:      {
                   2050:        /* no file to be loaded */
                   2051:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.69      cvs      2052:        
1.116     cvs      2053:        if (error_html)
1.69      cvs      2054:         /* so we can show the error message */
                   2055:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2056:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2057:                                    context_tcbf, HT_ERROR);
                   2058:        return HT_ERROR;
                   2059:      }
1.7       cvs      2060: 
1.4       cvs      2061:    /* do we support this protocol? */
1.116     cvs      2062:    if (IsValidProtocol (urlName) == NO) 
                   2063:      {
                   2064:        /* return error */
                   2065:        outputfile[0] = EOS;    /* file could not be opened */
                   2066:        TtaSetStatus (docid, 1, 
                   2067:                     TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
                   2068:                     urlName);
                   2069: 
                   2070:        if (error_html)
1.69      cvs      2071:         /* so we can show the error message */
                   2072:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2073:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2074:                                    context_tcbf, HT_ERROR);
                   2075:        return HT_ERROR;
                   2076:      }
1.4       cvs      2077: 
1.114     cvs      2078:    /* we store CSS in subdir named 0; all the other files go to a subidr
                   2079:       named after their own docid */
                   2080:    
                   2081:    tempsubdir = (mode & AMAYA_LOAD_CSS) ? 0 : docid;
                   2082: 
1.116     cvs      2083:    /* create a tempfilename */
                   2084:    sprintf (outputfile, "%s%c%d%c%04dAM", 
                   2085:            TempFileDirectory, DIR_SEP, tempsubdir, DIR_SEP, object_counter);
                   2086:    /* update the object_counter (used for the tempfilename) */
1.4       cvs      2087:    object_counter++;
1.116     cvs      2088:    
1.4       cvs      2089:    /* normalize the URL */
1.45      cvs      2090:    ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4       cvs      2091:    /* should we abort the request if we could not normalize the url? */
1.69      cvs      2092:    if (ref == (char*) NULL || ref[0] == EOS) {
                   2093:       /*error */
                   2094:       outputfile[0] = EOS;
                   2095:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   2096:       
                   2097:       if (error_html)
1.116     cvs      2098:        /* so we can show the error message */
                   2099:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99      cvs      2100:       InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.116     cvs      2101:                                   context_tcbf, HT_ERROR);
1.69      cvs      2102:       return HT_ERROR;
                   2103:    }
1.116     cvs      2104: 
1.4       cvs      2105:    /* verify if that file name existed */
1.9       cvs      2106:    if (TtaFileExist (outputfile))
1.77      cvs      2107:      TtaFileUnlink (outputfile);
1.116     cvs      2108:    
1.4       cvs      2109:    /* Initialize the request structure */
                   2110:    me = AHTReqContext_new (docid);
1.116     cvs      2111:    if (me == NULL) 
                   2112:      {
                   2113:        outputfile[0] = EOS;
                   2114:        /* need an error message here */
                   2115:        TtaFreeMemory (ref);
                   2116:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2117:                                   context_tcbf, HT_ERROR);
                   2118:        return HT_ERROR;
                   2119:      }
                   2120: 
1.4       cvs      2121:    /* Specific initializations for POST and GET */
1.116     cvs      2122:    if (mode & AMAYA_FORM_POST)
                   2123:      {
                   2124:        me->method = METHOD_POST;
                   2125:        HTRequest_setMethod (me->request, METHOD_POST);
                   2126:      }
                   2127:    else 
                   2128:      {
                   2129:        me->method = METHOD_GET;
                   2130:        if (!HasKnownFileSuffix (ref))
                   2131:         HTRequest_setConversion(me->request, acceptTypes, TRUE);
                   2132:      }
1.93      cvs      2133: 
1.116     cvs      2134:    /* Common initialization for all HTML methods */
1.4       cvs      2135:    me->mode = mode;
                   2136:    me->error_html = error_html;
                   2137:    me->incremental_cbf = incremental_cbf;
                   2138:    me->context_icbf = context_icbf;
                   2139:    me->terminate_cbf = terminate_cbf;
                   2140:    me->context_tcbf = context_tcbf;
1.64      cvs      2141: 
1.69      cvs      2142:    /* for the async. request modes, we need to have our
1.4       cvs      2143:       own copy of outputfile and urlname
1.116     cvs      2144:       */
1.4       cvs      2145: 
1.116     cvs      2146:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) 
                   2147:      {
                   2148:        l = strlen (outputfile);
                   2149:        if (l > MAX_LENGTH)
                   2150:         me->outputfile = TtaGetMemory (l + 2);
                   2151:        else
                   2152:         me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
                   2153:        strcpy (me->outputfile, outputfile);
                   2154:        l = strlen (urlName);
                   2155:        if (l > MAX_LENGTH)
                   2156:         me->urlName = TtaGetMemory (l + 2);
                   2157:        else
                   2158:         me->urlName = TtaGetMemory (MAX_LENGTH + 2);
                   2159:        strcpy (me->urlName, urlName);
                   2160: #ifdef _WINDOWS
                   2161:      /* force windows ASYNC requests to always be non preemptive */
                   2162:      HTRequest_setPreemptive (me->request, NO);
                   2163: #endif /*_WINDOWS */
                   2164:      } /* AMAYA_ASYNC mode */ 
                   2165:    else 
                   2166: #ifdef _WINDOWS
                   2167:      {
                   2168:        me->outputfile = outputfile;
                   2169:        me->urlName = urlName;
                   2170:        /* force windows SYNC requests to always be non preemptive */
1.123     cvs      2171:        /* @@@ verify this */
1.116     cvs      2172:        HTRequest_setPreemptive (me->request, YES);
                   2173:      }
                   2174: #else /* !_WINDOWS */
                   2175:      {
                   2176:        me->outputfile = outputfile;
                   2177:        me->urlName = urlName;
                   2178:      }
                   2179:    /***
1.57      cvs      2180:      Change for taking into account the stop button:
                   2181:      The requests will be always asynchronous, however, if mode=AMAYA_SYNC,
                   2182:      we will loop until the document has been received or a stop signal
                   2183:      generated
1.77      cvs      2184:      ****/
1.116     cvs      2185:    HTRequest_setPreemptive (me->request, NO);
                   2186: #endif /* _WINDOWS */
1.61      cvs      2187: 
                   2188:    /* prepare the URLname that will be displayed in teh status bar */
                   2189:    ChopURL (me->status_urlName, me->urlName);
1.77      cvs      2190:    TtaSetStatus (me->docid, 1, 
                   2191:                 TtaGetMessage (AMAYA, AM_FETCHING),
                   2192:                 me->status_urlName);
1.4       cvs      2193: 
                   2194:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45      cvs      2195:    TtaFreeMemory (ref);
1.4       cvs      2196: 
1.114     cvs      2197:    if (mode & AMAYA_NOCACHE) 
1.116     cvs      2198:       HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
                   2199: 
                   2200:    /* prepare the query string and format for POST */
                   2201:    if (mode & AMAYA_FORM_POST)
1.114     cvs      2202:      {
1.116     cvs      2203:        HTAnchor_setFormat ((HTParentAnchor *) me->anchor, 
                   2204:                           HTAtom_for ("application/x-www-form-urlencoded"));
                   2205:        HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
                   2206:        HTRequest_setEntityAnchor (me->request, me->anchor);
                   2207:      } 
1.114     cvs      2208: 
1.127     cvs      2209:    /* create the formdata element for libwww */
                   2210:    if (formdata)
                   2211:      me->formdata = PrepareFormdata (formdata);
                   2212: 
1.116     cvs      2213:    /* do the request */
                   2214:    if (mode & AMAYA_FORM_POST)
1.119     cvs      2215:      {
                   2216:        /* this call doesn't give back a boolean */
                   2217:        HTParentAnchor * posted = NULL;
                   2218: 
                   2219:        posted = HTPostFormAnchor (me->formdata, (HTAnchor *) me->anchor, 
                   2220:                                    me->request);
                   2221:        status = posted ? YES : NO; 
                   2222:      }
1.127     cvs      2223:    else if (formdata)
                   2224:      status = HTGetFormAnchor(me->formdata, (HTAnchor *) me->anchor,
                   2225:                              me->request);
1.116     cvs      2226:    else
1.77      cvs      2227:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      2228: 
1.123     cvs      2229:    /* @@@ may need some special windows error msg here */
1.116     cvs      2230:    /* control the errors */
1.130     cvs      2231:    /* @@@test the effect of HTRequest_kill () */
                   2232: #if 0
                   2233:    /* doesn't look necessary anymore */
1.129     cvs      2234:    if (status == NO
1.116     cvs      2235:        && HTError_hasSeverity (HTRequest_error (me->request), ERR_NON_FATAL))
                   2236:      status = HT_ERROR;
1.130     cvs      2237: 
1.116     cvs      2238:      /** *this should go to term_d @@@@ */
                   2239:      if (me->reqStatus == HT_CACHE)
1.93      cvs      2240:        {
1.116     cvs      2241:         AHTPrintPendingRequestStatus (me->docid, YES);
                   2242:         /* free the memory allocated for async requests */
                   2243:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
                   2244:                                      terminate_cbf, context_tcbf, HT_OK);
                   2245:         AHTReqContext_delete (me);
                   2246:         return HT_OK;
1.93      cvs      2247:        }
1.116     cvs      2248: #endif 
1.4       cvs      2249: 
1.130     cvs      2250:     if (status == NO)
1.116     cvs      2251:      /* the request invocation failed */
                   2252:      {
                   2253:        /* show an error message on the status bar */
                   2254:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   2255:        TtaSetStatus (docid, 1, 
                   2256:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                   2257:                     urlName);
                   2258:        if (me->reqStatus == HT_NEW)
                   2259:         /* manually invoke the last processing that usually gets done
                   2260:            in a succesful request */
                   2261:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
                   2262:                                      terminate_cbf, context_tcbf, HT_ERROR);
                   2263:        /* terminate_handler wasn't called */
                   2264:        if (mode & AMAYA_SYNC || mode & AMAYA_ISYNC)
                   2265:         AHTReqContext_delete (me);
                   2266:      }
                   2267:    else
                   2268:    /* end treatment for SYNC requests */
1.120     cvs      2269:      if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   2270:        {
1.116     cvs      2271: #ifndef _WINDOWS
1.120     cvs      2272:         /* part of the UNIX stop button handler */
1.130     cvs      2273:         status = LoopForStop (me);
1.116     cvs      2274: #endif /* _!WINDOWS */
1.130     cvs      2275:         /* if status returns HT_ERROR, maybe we should invoke the callback
                   2276:            too */
1.120     cvs      2277:         /* @@@ this doesn't seem correct ... me->request may not exist ... */
                   2278:         if (!HTRequest_kill (me->request))
                   2279:           AHTReqContext_delete (me);
                   2280:        }
1.130     cvs      2281: 
                   2282:    /* an interface problem!!! */
                   2283:    return (status == YES ? 0 : -1);
1.4       cvs      2284: }
                   2285: 
1.5       cvs      2286: /*----------------------------------------------------------------------
1.17      cvs      2287:    PutObjectWWW
                   2288:    frontend for uploading a resource to a URL. This function downloads
                   2289:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   2290:    finish the job.
                   2291: 
1.5       cvs      2292:    2 upload modes are proposed:                                       
                   2293:    AMAYA_SYNC : blocking mode                            
                   2294:    AMAYA_ASYNC : non-blocking mode                       
                   2295:    
1.4       cvs      2296:    When the function is called with the SYNC mode, the function will
                   2297:    return only when the file has been uploaded.
                   2298:    The ASYNC mode will immediately return after setting up the
                   2299:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   2300:    call back terminate_cbf, handling it the context defined in
                   2301:    context_tcbf.
                   2302: 
                   2303:    Notes:
                   2304:    At the end of a succesful request, the urlName string contains the
                   2305:    name of the actually uploaded URL. As a URL can change over the time,
                   2306:    (e.g., be redirected elsewhere), it is advised that the function
                   2307:    caller verifies the value of the urlName variable at the end of
                   2308:    a request.
                   2309: 
                   2310:    Inputs:
                   2311:    - docid  Document identifier for the set of objects being
                   2312:    retrieved.
                   2313:    - fileName A pointer to the local file to upload
                   2314:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   2315:    - mode The retrieval mode.
                   2316:    - terminate_cbf 
                   2317:    - context_icbf
                   2318:    Callback and context for a terminate handler
                   2319: 
                   2320:    Outputs:
                   2321:    - urlName The URL that was uploaded
                   2322: 
                   2323:    Returns:
                   2324:    HT_ERROR
                   2325:    HT_OK
1.5       cvs      2326:   ----------------------------------------------------------------------*/
1.4       cvs      2327: #ifdef __STDC__
1.27      cvs      2328: int                 PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4       cvs      2329:                                  TTcbf * terminate_cbf, void *context_tcbf)
                   2330: #else
1.26      cvs      2331: int                 PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4       cvs      2332:                                  ,terminate_cbf, context_tcbf)
                   2333: int                 docid;
                   2334: char               *urlName;
                   2335: char               *fileName;
                   2336: int                 mode;
1.27      cvs      2337: PicType             contentType;
1.4       cvs      2338: TTcbf              *terminate_cbf;
                   2339: void               *context_tcbf;
                   2340: 
1.116     cvs      2341: #endif /* __STDC__ */
1.4       cvs      2342: {
1.116     cvs      2343:    AHTReqContext      *me;
1.4       cvs      2344:    int                 status;
                   2345:    int                 fd;
                   2346:    struct stat         file_stat;
1.116     cvs      2347:    char               *fileURL;
1.4       cvs      2348: 
1.33      cvs      2349:    AmayaLastHTTPErrorMsg [0] = EOS;
                   2350:    
1.116     cvs      2351:    if (urlName == NULL || docid == 0 || fileName == NULL 
                   2352:        || !TtaFileExist (fileName))
1.4       cvs      2353:       /* no file to be uploaded */
                   2354:       return HT_ERROR;
                   2355: 
                   2356:    /* do we support this protocol? */
1.7       cvs      2357:    if (IsValidProtocol (urlName) == NO)
                   2358:      {
                   2359:        /* return error */
1.77      cvs      2360:        TtaSetStatus (docid, 1, 
                   2361:                       TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7       cvs      2362:                      urlName);
                   2363:        return HT_ERROR;
                   2364:      }
1.116     cvs      2365: 
                   2366:    /* verify the file's size */
                   2367: #ifndef _WINDOWS
1.7       cvs      2368:    if ((fd = open (fileName, O_RDONLY)) == -1)
1.116     cvs      2369: #else 
1.74      cvs      2370:    if ((fd = open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.116     cvs      2371: #endif /* _WINDOWS */
1.7       cvs      2372:      {
                   2373:        /* if we could not open the file, exit */
                   2374:        /*error msg here */
                   2375:        return (HT_ERROR);
                   2376:      }
1.4       cvs      2377: 
                   2378:    fstat (fd, &file_stat);
1.116     cvs      2379:    close (fd);
1.4       cvs      2380: 
1.7       cvs      2381:    if (file_stat.st_size == 0)
                   2382:      {
                   2383:        /* file was empty */
                   2384:        /*errmsg here */
                   2385:        return (HT_ERROR);
                   2386:      }
1.116     cvs      2387: 
                   2388:    /* prepare the request context */
1.4       cvs      2389: 
                   2390:    if (THD_TRACE)
1.116     cvs      2391:       fprintf (stderr, "file size == %u\n", (unsigned) file_stat.st_size);
1.4       cvs      2392: 
                   2393:    me = AHTReqContext_new (docid);
                   2394: 
1.7       cvs      2395:    if (me == NULL)
                   2396:      {
1.131     cvs      2397:        /* @@ need an error message here */
1.7       cvs      2398:        TtaHandlePendingEvents ();
                   2399:        return (HT_ERROR);
                   2400:      }
1.116     cvs      2401: 
1.4       cvs      2402:    me->mode = mode;
                   2403:    me->incremental_cbf = (TIcbf *) NULL;
                   2404:    me->context_icbf = (void *) NULL;
                   2405:    me->terminate_cbf = terminate_cbf;
                   2406:    me->context_tcbf = context_tcbf;
                   2407:    me->urlName = urlName;
1.116     cvs      2408:    me->block_size =  file_stat.st_size;
1.17      cvs      2409:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      2410:    me->method = METHOD_PUT;
                   2411:    HTRequest_setMethod (me->request, METHOD_PUT);
                   2412:    me->output = stdout;
1.17      cvs      2413:    /* we are not expecting to receive any input from the server */
                   2414:    me->outputfile = (char *) NULL; 
1.4       cvs      2415: 
1.134   ! cvs      2416: #ifdef _WINDOWS
        !          2417:    /* libwww's HTParse function doesn't take into account the drive name;
        !          2418:    so we sidestep it */
        !          2419:    fileURL = NULL;
        !          2420:    StrAllocCopy (fileURL, "file:");
        !          2421:    StrAllocCat (fileURL, fileName);
        !          2422: #else
1.116     cvs      2423:    fileURL = HTParse (fileName, "file:/", PARSE_ALL);
1.134   ! cvs      2424: #endif /* _WINDOWS */
1.116     cvs      2425:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (fileURL);
                   2426:    HT_FREE (fileURL);
1.114     cvs      2427: 
1.28      cvs      2428:    /* Set the Content-Type of the file we are uploading */
1.77      cvs      2429:    HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
                   2430:                       AHTGuessAtom_for (me->urlName, contentType));
1.17      cvs      2431: 
1.134   ! cvs      2432:    /* associate the anchor to the request */
1.4       cvs      2433:    HTRequest_setEntityAnchor (me->request, me->anchor);
1.134   ! cvs      2434: 
        !          2435:    /* define other request characteristics */
1.116     cvs      2436: #ifdef _WINDOWS
                   2437:    HTRequest_setPreemptive (me->request, YES);
                   2438: #else
1.134   ! cvs      2439:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      2440: #endif /* _WINDOWS */
                   2441: 
                   2442:    if (mode & AMAYA_NOCACHE)
                   2443:      HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
                   2444: 
1.74      cvs      2445:    /* prepare the URLname that will be displayed in teh status bar */
                   2446:    ChopURL (me->status_urlName, me->urlName);
                   2447:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
                   2448:                     me->status_urlName);
1.114     cvs      2449: 
1.116     cvs      2450:    status = HTPutDocumentAbsolute (me->anchor, urlName, me->request);
1.4       cvs      2451: 
1.130     cvs      2452:    if (status == YES && me->reqStatus != HT_ERR)
1.7       cvs      2453:      {
                   2454:        /* part of the stop button handler */
                   2455:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   2456:          {
1.120     cvs      2457: #ifndef _WINDOWS
1.7       cvs      2458:             status = LoopForStop (me);
1.120     cvs      2459: #endif /* _WINDOWS */
                   2460:             if (!HTRequest_kill (me->request))
                   2461:               AHTReqContext_delete (me);
1.7       cvs      2462:          }
1.15      cvs      2463:      }
1.116     cvs      2464:    else
                   2465:      {
                   2466:       if (! HTRequest_kill (me->request))
                   2467:         AHTReqContext_delete (me);
                   2468:      }
                   2469:  
                   2470:    TtaHandlePendingEvents ();
1.90      cvs      2471: 
1.130     cvs      2472:    return (status == YES ? 0 : -1);
1.28      cvs      2473: }
1.4       cvs      2474: 
1.5       cvs      2475: /*----------------------------------------------------------------------
1.17      cvs      2476:   Stop Request
                   2477:   stops (kills) all active requests associated with a docid 
1.5       cvs      2478:   ----------------------------------------------------------------------*/
1.4       cvs      2479: #ifdef __STDC__
                   2480: void                StopRequest (int docid)
                   2481: #else
                   2482: void                StopRequest (docid)
                   2483: int                 docid;
                   2484: #endif
                   2485: {
                   2486:    HTList             *cur;
                   2487:    AHTDocId_Status    *docid_status;
                   2488:    AHTReqContext      *me;
1.108     cvs      2489: #ifdef DEBUG_LIBWWW
                   2490:    int                 open_requests;
                   2491: #endif /* DEBUG_LIBWWW */
1.116     cvs      2492: 
                   2493:  
                   2494:    if (Amaya && libDoStop)
1.7       cvs      2495:      {
1.100     cvs      2496: #ifdef DEBUG_LIBWWW
1.116     cvs      2497:        fprintf (stderr, "StopRequest: number of Amaya requests : %d\n", 
                   2498:                Amaya->open_requests);
1.100     cvs      2499: #endif /* DEBUG_LIBWWW */
                   2500: 
1.116     cvs      2501:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
                   2502:                                                          Amaya->docid_status);
                   2503:        /* verify if there are any requests at all associated with docid */
                   2504:        
                   2505:        if (docid_status == (AHTDocId_Status *) NULL)
                   2506:         return;
1.108     cvs      2507: #ifdef DEBUG_LIBWWW
1.116     cvs      2508:        open_requests = docid_status->counter;
1.108     cvs      2509: #endif /* DEBUG_LIBWWW */
1.4       cvs      2510: 
1.116     cvs      2511:        /* First, kill all pending requests */
                   2512:        /* We first inhibit the activation of pending requests */
                   2513:        HTHost_disable_PendingReqLaunch ();
                   2514:        cur = Amaya->reqlist;
                   2515:        while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
                   2516:         {
                   2517:           if (me->docid == docid && me->reqStatus == HT_NEW)
                   2518:             {
                   2519:               /* If we have an ASYNC request, we kill it.
                   2520:               ** If it's a SYNC request, we just mark it as aborted
                   2521:               */
                   2522:               me->reqStatus = HT_ABORT;
                   2523:               if (((me->mode & AMAYA_IASYNC)
                   2524:                    || (me->mode & AMAYA_ASYNC))
                   2525:                   && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
                   2526:                 {
                   2527:                   /* delete the amaya/libwww request context */
                   2528:                   if (!HTRequest_kill (me->request))
                   2529:                     /* if the above function returns NO (0), it means
                   2530:                     ** that there was no network context and that 
                   2531:                     ** terminate_handler wasn't called. So, we erase
                   2532:                     ** this context ourselves
                   2533:                     */
1.96      cvs      2534:                     AHTReqContext_delete (me);
1.116     cvs      2535:                   
                   2536:                   cur = Amaya->reqlist;
1.108     cvs      2537: #ifdef DEBUG_LIBWWW
1.116     cvs      2538:                   /* update the number of open requests */
                   2539:                   open_requests--;                
1.108     cvs      2540: #endif /* DEBUG_LIBWWW */
1.116     cvs      2541:                 }
                   2542:             }
                   2543:         }
1.108     cvs      2544: 
1.85      cvs      2545:        /* enable the activation of pending requests */
1.116     cvs      2546:        HTHost_enable_PendingReqLaunch ();
1.85      cvs      2547: 
1.116     cvs      2548:        cur = Amaya->reqlist;
                   2549:        while ((me = (AHTReqContext *) HTList_nextObject (cur)))
                   2550:         {
                   2551:           if (me->docid == docid)
                   2552:             {
                   2553:               /* kill this request */
                   2554:               
                   2555:               switch (me->reqStatus)
                   2556:                 {
                   2557:                 case HT_ABORT:
1.108     cvs      2558: #ifdef DEBUG_LIBWWW
1.116     cvs      2559:                   fprintf (stderr, "Stop: url %s says abort", me->urlName);
1.108     cvs      2560: #endif /* DEBUG_LIBWWW */
1.116     cvs      2561:                   break;
1.100     cvs      2562:                  
1.116     cvs      2563:                 case HT_END:
                   2564: #ifdef DEBUG_LIBWWW
                   2565:                   fprintf (stderr, "Stop: url %s says end", me->urlName);
                   2566: #endif /* DEBUG_LIBWWW */
                   2567:                   break;
                   2568: 
                   2569:                 case HT_BUSY:
                   2570:                   me->reqStatus = HT_ABORT;
1.108     cvs      2571: #ifdef DEBUG_LIBWWW
1.116     cvs      2572:                   fprintf (stderr, "Stop: url %s going from busy to abort\n",
                   2573:                            me->urlName);
1.108     cvs      2574: #endif /* DEBUG_LIBWWW */
1.116     cvs      2575:                   break;
1.100     cvs      2576: 
1.116     cvs      2577:                 case HT_NEW_PENDING:
                   2578:                 case HT_WAITING:
                   2579:                 default:
1.108     cvs      2580: #ifdef DEBUG_LIBWWW
1.116     cvs      2581:                   fprintf (stderr, "Stop: url %s says NEW_PENDING, WAITING",
                   2582:                            me->urlName);
1.108     cvs      2583: #endif /* DEBUG_LIBWWW */
1.116     cvs      2584:                   me->reqStatus = HT_ABORT;
                   2585: 
                   2586:                   if (((me->mode & AMAYA_IASYNC)
                   2587:                        || (me->mode & AMAYA_ASYNC))
                   2588:                       && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
                   2589:                     {
                   2590:                       /* delete the amaya/libwww request context */
                   2591:                       if (!HTRequest_kill (me->request))
                   2592:                         /* if the above function returns NO (0), it means
                   2593:                         ** that there was no network context and that 
                   2594:                         ** terminate_handler wasn't called. So, we erase
                   2595:                         ** this context ourselves
                   2596:                         */
                   2597:                         AHTReqContext_delete (me);
1.96      cvs      2598: 
1.116     cvs      2599:                       cur = Amaya->reqlist;
1.108     cvs      2600: #ifdef DEBUG_LIBWWW
1.116     cvs      2601:                       /* update the number of open requests */
                   2602:                       open_requests--;            
1.108     cvs      2603: #endif /* DEBUG_LIBWWW */
1.116     cvs      2604:                     }
                   2605: 
1.108     cvs      2606: #ifdef DEBUG_LIBWWW
1.116     cvs      2607:                   if (HTHost_isIdle (reqHost))
                   2608:                     fprintf (stderr, "StopRequst: After killing, Host is "
                   2609:                              "idle\n");
                   2610:                   else
                   2611:                     fprintf (stderr, "StopRequest: After killing, Host isn't "
                   2612:                              "idle\n");
1.108     cvs      2613: #endif /* DEBUG_LIBWWW */
1.95      cvs      2614: 
1.108     cvs      2615: #ifdef DEBUG_LIBWWW                 
1.116     cvs      2616:               open_requests--;
1.108     cvs      2617: #endif /* DEBUG_LIBWWW */                   
1.93      cvs      2618:                     break;
                   2619:                     
1.116     cvs      2620:                 }      /* switch */
                   2621:             }          /* if me docid */
                   2622:         }                      /* while */
                   2623:        
1.100     cvs      2624: #ifdef DEBUG_LIBWWW
1.116     cvs      2625:        fprintf (stderr, "StopRequest: number of Amaya requests : "
                   2626:                "%d\n", Amaya->open_requests);
1.100     cvs      2627: #endif /* DEBUG_LIBWWW */
1.7       cvs      2628:      }                         /* if amaya open requests */
1.93      cvs      2629:    
1.85      cvs      2630: } /* StopRequest */
1.17      cvs      2631: 
                   2632: 
1.105     cvs      2633: /*----------------------------------------------------------------------
                   2634:   AmayaIsAlive
                   2635:   returns the status of the AmayaAlive flag
                   2636:   ----------------------------------------------------------------------*/
                   2637: #ifdef __STDC__
                   2638: boolean AmayaIsAlive (void)
                   2639: #else
                   2640: boolean AmayaIsAlive ()
                   2641: #endif /* _STDC_ */
                   2642: {
                   2643:   return AmayaAlive;
                   2644: }
1.116     cvs      2645: 
1.69      cvs      2646: #endif /* AMAYA_JAVA */
1.77      cvs      2647: 
1.116     cvs      2648: /*
                   2649:   end of Module query.c
                   2650: */
1.17      cvs      2651: 
                   2652: 

Webmaster