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

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

Webmaster