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

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

Webmaster