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

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__
1.196     cvs      1283: static void         AHTAcceptLanguagesInit (HTList *c)
1.193     cvs      1284: #else  /* __STDC__ */
1.196     cvs      1285: static void         AHTAcceptLanguagesInit (c)
1.193     cvs      1286: HTList             *c;
                   1287: #endif /* __STDC__ */
                   1288: {
1.196     cvs      1289:   STRING            ptr;
                   1290:   STRING            lang_list;
                   1291:   CHAR_T            s[3];
                   1292:   int               count, lg;
                   1293:   double            quality;
                   1294:   ThotBool          still;
1.193     cvs      1295: 
                   1296:   if (c == (HTList *) NULL) 
                   1297:       return;
                   1298:   
1.196     cvs      1299:   lang_list = TtaGetEnvString ("ACCEPT_LANGUAGES");
1.197   ! cvs      1300:   s[2] = EOS;
1.196     cvs      1301:   if (lang_list && *lang_list != EOS)
1.193     cvs      1302:     {
                   1303:       /* add the default language first  */
                   1304:       HTLanguage_add (c, "*", -1.0);
                   1305:       /* how many languages do we have? */
1.196     cvs      1306:       ptr = lang_list;
                   1307:       count = 0;
                   1308:       while (*ptr != EOS)
                   1309:        {
                   1310:          while (*ptr != EOS &&
                   1311:                 (*ptr < 'A' || (*ptr > 'Z' && *ptr < 'a') || *ptr > 'z'))
                   1312:            /* skip the whole separator */
                   1313:            ptr++;
                   1314:          lg = 0;
                   1315:          while ((*ptr >= 'A' && *ptr <= 'Z') || (*ptr >= 'a' && *ptr <= 'z') || *ptr == '-')
                   1316:            {
                   1317:              /* it's a new language */
                   1318:              ptr++;
                   1319:              lg++;
                   1320:            }
                   1321:          if (lg >= 2)
1.193     cvs      1322:            count++;
1.196     cvs      1323:          if (*ptr != EOS)
                   1324:            ptr++;
                   1325:        }
1.193     cvs      1326: 
1.196     cvs      1327:       if (count > 0)
1.193     cvs      1328:        quality = 1.1 - (double) count/10.0;
                   1329:       else
                   1330:        quality = 1.0;
                   1331: 
1.196     cvs      1332:       /*
                   1333:        Read the languages from the registry, then inject them one, by one.
                   1334:        The first one is the one with the highest priority.
                   1335:        The libwww ask for the lowest priority first.
                   1336:       */
                   1337:       ptr--;
                   1338:       still = TRUE;
                   1339:       while (count) 
1.193     cvs      1340:        {
1.196     cvs      1341:          while (still &&
                   1342:                 (*ptr < 'A' || (*ptr > 'Z' && *ptr < 'a') || *ptr > 'z'))
                   1343:            /* skip the whole separator */
                   1344:            if (ptr > lang_list)
                   1345:              ptr--;
                   1346:            else
                   1347:              still = FALSE;
                   1348:          lg = 0;
                   1349:          while (still &&
                   1350:                 ((*ptr >= 'A' && *ptr <= 'Z') || (*ptr >= 'a' && *ptr <= 'z') || *ptr == '-'))
                   1351:            {
                   1352:              /* it's a new language */
                   1353:              if (ptr > lang_list)
                   1354:                ptr--;
                   1355:              else
                   1356:                still = FALSE;
                   1357:              lg++;
                   1358:            }
                   1359:          if (lg >= 2)
                   1360:            {
                   1361:              if (still)
                   1362:                ustrncpy (s, &ptr[1], 2);
                   1363:              else
                   1364:                ustrncpy (s, lang_list, 2);
                   1365:              count--;
                   1366:              HTLanguage_add (c, s, quality);
                   1367:              quality += 0.1;
                   1368:            }
                   1369:          ptr--;
1.193     cvs      1370:        }
                   1371:     }
                   1372: }
                   1373: 
                   1374: /*----------------------------------------------------------------------
1.17      cvs      1375:   AHTConverterInit
                   1376:   Bindings between a source media type and a destination media type
                   1377:   (conversion).
1.5       cvs      1378:   ----------------------------------------------------------------------*/
1.15      cvs      1379: #ifdef __STDC__
                   1380: static void         AHTConverterInit (HTList *c)
                   1381: #else  /* __STDC__ */
                   1382: static void         AHTConverterInit (c)
                   1383: HTList             *c;
                   1384: #endif /* __STDC__ */
1.4       cvs      1385: {
                   1386: 
                   1387:    /* Handler for custom http error messages */
1.7       cvs      1388:    HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4       cvs      1389: 
                   1390:    /*
                   1391:     ** These are converters that converts to something other than www/present,
                   1392:     ** that is not directly outputting someting to the user on the screen
                   1393:     */
                   1394: 
1.116     cvs      1395:    HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 
                   1396:                     1.0, 0.0, 0.0);
1.4       cvs      1397:    HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter,
                   1398:                     1.0, 0.0, 0.0);
                   1399:    HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader,
                   1400:                     1.0, 0.0, 0.0);
1.116     cvs      1401:    HTConversion_add(c,"message/x-rfc822-cont", "*/*", HTMIMEContinue,  
                   1402:                    1.0, 0.0, 0.0);
                   1403:    HTConversion_add(c,"message/x-rfc822-partial","*/*",        HTMIMEPartial,
                   1404:                    1.0, 0.0, 0.0);
1.4       cvs      1405:    HTConversion_add (c, "multipart/*", "*/*", HTBoundary,
                   1406:                     1.0, 0.0, 0.0);
                   1407:    HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML,
                   1408:                     1.0, 0.0, 0.0);
                   1409: 
                   1410:    /*
1.116     cvs      1411:    ** The following conversions are converting ASCII output from various
                   1412:    ** protocols to HTML objects.
                   1413:    */
1.4       cvs      1414:    HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new,
                   1415:                     1.0, 0.0, 0.0);
                   1416: 
                   1417:    /*
1.116     cvs      1418:    ** We also register a special content type guess stream that can figure out
                   1419:    ** the content type by reading the first bytes of the stream
                   1420:    */
1.4       cvs      1421:    HTConversion_add (c, "www/unknown", "*/*", HTGuess_new,
                   1422:                     1.0, 0.0, 0.0);
                   1423: 
1.116     cvs      1424: #ifdef AMAYA_WWW_CACHE
1.4       cvs      1425:    /*
1.116     cvs      1426:    ** Register a persistent cache stream which can save an object to local
                   1427:    ** file
                   1428:    */
1.4       cvs      1429:    HTConversion_add (c, "www/cache", "*/*", HTCacheWriter,
                   1430:                     1.0, 0.0, 0.0);
1.116     cvs      1431:    HTConversion_add(c,"www/cache-append", "*/*", HTCacheAppend,
                   1432:                    1.0, 0.0, 0.0);
                   1433: #endif AMAYA_WWW_CACHE
1.4       cvs      1434: 
                   1435:    /*
1.116     cvs      1436:    ** This dumps all other formats to local disk without any further
                   1437:    ** action taken
                   1438:    */
1.4       cvs      1439:    HTConversion_add (c, "*/*", "www/present", HTSaveLocally,
1.190     cvs      1440:                     0.3, 0.0, 0.0);  
1.4       cvs      1441: }
                   1442: 
1.27      cvs      1443: 
1.15      cvs      1444: /*----------------------------------------------------------------------
1.17      cvs      1445:   AHTProtocolInit
                   1446:   Registers all amaya supported protocols.
1.15      cvs      1447:   ----------------------------------------------------------------------*/
1.4       cvs      1448: static void         AHTProtocolInit (void)
                   1449: {
1.116     cvs      1450:   char *strptr;
1.4       cvs      1451: 
1.116     cvs      1452:   /* 
                   1453:      NB. Preemptive == YES means Blocking requests
                   1454:      Non-preemptive == NO means Non-blocking requests
                   1455:      */
                   1456:   HTTransport_add("tcp", HT_TP_SINGLE, HTReader_new, HTWriter_new);
                   1457:   HTTransport_add("buffered_tcp", HT_TP_SINGLE, HTReader_new, 
                   1458:                  HTBufferWriter_new);
                   1459:   HTProtocol_add ("http", "buffered_tcp", HTTP_PORT, NO, HTLoadHTTP, NULL);
1.136     cvs      1460: #ifdef _WINDOWS
                   1461:   HTProtocol_add ("file", "local", 0, YES, HTLoadFile, NULL);
                   1462: #else
1.116     cvs      1463:   HTProtocol_add ("file", "local", 0, NO, HTLoadFile, NULL);
1.136     cvs      1464: #endif _WINDOWS
1.116     cvs      1465: #ifdef AMAYA_WWW_CACHE
                   1466:    HTProtocol_add("cache",  "local", 0, YES, HTLoadCache, NULL);
                   1467: #endif /* AMAYA_WWW_CACHE */
1.103     cvs      1468: #if 0 /* experimental code */
1.116     cvs      1469:    HTProtocol_add ("ftp", "tcp", FTP_PORT, NO, HTLoadFTP, NULL);
1.17      cvs      1470: #endif
1.116     cvs      1471: 
                   1472:    /* initialize pipelining */
1.168     cvs      1473:   strptr = (char *) TtaGetEnvString ("ENABLE_PIPELINING");
1.116     cvs      1474:   if (strptr && *strptr && strcasecmp (strptr,"yes" ))
                   1475:     HTTP_setConnectionMode (HTTP_11_NO_PIPELINING);
1.4       cvs      1476: }
                   1477: 
1.15      cvs      1478: /*----------------------------------------------------------------------
1.17      cvs      1479:   AHTNetInit
                   1480:   Reegisters "before" and "after" request filters.
1.15      cvs      1481:   ----------------------------------------------------------------------*/
1.4       cvs      1482: static void         AHTNetInit (void)
                   1483: {
                   1484: 
                   1485: /*      Register BEFORE filters
1.74      cvs      1486: **      The BEFORE filters handle proxies, caches, rule files etc.
                   1487: **      The filters are called in the order by which the are registered
                   1488: **      Not done automaticly - may be done by application!
                   1489: */
1.4       cvs      1490: 
1.116     cvs      1491:   HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, HT_FILTER_LATE);
                   1492:   HTNet_addBefore (HTProxyFilter, NULL, NULL, HT_FILTER_LATE);
1.85      cvs      1493:   HTHost_setActivateRequestCallback (AHTOpen_file);
1.4       cvs      1494: 
                   1495: /*      register AFTER filters
1.74      cvs      1496: **      The AFTER filters handle error messages, logging, redirection,
                   1497: **      authentication etc.
                   1498: **      The filters are called in the order by which the are registered
                   1499: **      Not done automaticly - may be done by application!
                   1500: */
1.4       cvs      1501: 
1.116     cvs      1502:   HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS,
                   1503:                  HT_FILTER_MIDDLE);
                   1504:   HTNet_addAfter(HTAuthFilter, "http://*", NULL, HT_REAUTH,
                   1505:                 HT_FILTER_MIDDLE);
                   1506:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT,
                   1507:                  HT_FILTER_MIDDLE);
                   1508:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_FOUND, 
                   1509:                  HT_FILTER_MIDDLE);
                   1510:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_SEE_OTHER,
                   1511:                  HT_FILTER_MIDDLE);
                   1512:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT,
                   1513:                  HT_FILTER_MIDDLE);
1.168     cvs      1514:   HTNet_addAfter(HTAuthInfoFilter,     "http://*", NULL, HT_ALL, 
                   1515:                 HT_FILTER_MIDDLE);
1.116     cvs      1516:   HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY,
                   1517:                  HT_FILTER_MIDDLE);
1.151     cvs      1518: #ifdef AMAYA_LOST_UPDATE
                   1519:   HTNet_addAfter (precondition_handler, NULL, NULL, HT_PRECONDITION_FAILED,
                   1520:                  HT_FILTER_MIDDLE);
                   1521: #endif /* AMAYA_LOST_UPDATE */
1.111     cvs      1522: #ifndef _WINDOWS
1.116     cvs      1523:   HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, 
                   1524:                   HT_FILTER_LAST);     
1.111     cvs      1525: #endif /* !_WINDOWS */
1.116     cvs      1526:    /**** for later ?? ****/
                   1527:    /*  HTNet_addAfter(HTInfoFilter,    NULL,           NULL, HT_ALL,           HT_FILTER_LATE); */
1.51      cvs      1528:    /* handles all errors */
1.116     cvs      1529:   HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4       cvs      1530: }
                   1531: 
1.15      cvs      1532: /*----------------------------------------------------------------------
1.17      cvs      1533:   AHTAlertInit
                   1534:   Register alert messages and their callbacks.
1.15      cvs      1535:   ----------------------------------------------------------------------*/
                   1536: #ifdef __STDC__
                   1537: static void         AHTAlertInit (void)
                   1538: #else
                   1539: static void         AHTAlertInit ()
                   1540: #endif
                   1541: {
                   1542:    HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.120     cvs      1543: #ifdef __WINDOWS
1.70      cvs      1544:    HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
1.116     cvs      1545: #endif /* _WINDOWS */
1.15      cvs      1546:    HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48      cvs      1547:    HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG));       /* process all messages except debug ones*/
1.15      cvs      1548:    HTAlert_add (AHTConfirm, HT_A_CONFIRM);
                   1549:    HTAlert_add (AHTPrompt, HT_A_PROMPT);
                   1550:    HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
1.116     cvs      1551: }
                   1552: 
1.128     cvs      1553: /*----------------------------------------------------------------------
                   1554:   libwww_CleanCache
                   1555:   frontend to the recursive cache cleaning function
                   1556:   ----------------------------------------------------------------------*/
                   1557: #ifdef __STDC__
                   1558: void libwww_CleanCache (void)
                   1559: #else
                   1560: void libwww_CleanCache ()
                   1561: Document doc;
                   1562: View view;
                   1563: #endif /* __STDC__ */
                   1564: {
1.130     cvs      1565: #ifdef AMAYA_WWW_CACHE
1.185     cvs      1566:   char *real_dir;
                   1567:   char *cache_dir;
1.190     cvs      1568:   char *tmp;
1.128     cvs      1569:   int cache_size;
1.177     cvs      1570:   int cache_expire;
                   1571:   int cache_disconnect;
1.191     cvs      1572:   ThotBool error;
1.185     cvs      1573:   STRING ptr;
1.130     cvs      1574: 
1.128     cvs      1575:   if (!HTCacheMode_enabled ())
1.183     cvs      1576:     /* don't do anything if we're not using a cache */
                   1577:     return;
1.128     cvs      1578:   /* temporarily close down the cache, purge it, then restart */
1.190     cvs      1579:   tmp = (char *) HTCacheMode_getRoot ();
                   1580:   /* don't do anything if we don't have a valid cache dir */
                   1581:   if (!tmp || *tmp == EOS)
                   1582:          return;
                   1583:   cache_dir = TtaStrdup (tmp);
1.128     cvs      1584:   cache_size = HTCacheMode_maxSize ();
1.177     cvs      1585:   cache_expire = HTCacheMode_expires ();
                   1586:   cache_disconnect = HTCacheMode_disconnected ();
                   1587: 
1.185     cvs      1588:   /* get something we can work on :) */
1.190     cvs      1589:   tmp = HTWWWToLocal (cache_dir, "file:", NULL);
                   1590:   real_dir = TtaGetMemory (strlen (tmp) + 20);
                   1591:   strcpy (real_dir, tmp);
                   1592:   HT_FREE (tmp);
1.185     cvs      1593: 
                   1594:   /* safeguard... abort the operation if cache_dir doesn't end with
                   1595:      CACHE_DIR_NAME */
                   1596:   error = TRUE;
1.188     cvs      1597:   ptr = strstr (real_dir, CACHE_DIR_NAME);  
1.185     cvs      1598: #ifdef _WINDOWS
                   1599:   if (ptr && *ptr && !_stricmp (ptr, CACHE_DIR_NAME))
                   1600: #else
                   1601:     if (ptr && *ptr && !strcasecmp (ptr, CACHE_DIR_NAME))
                   1602: #endif /* _WINDOWS */
                   1603:       error = FALSE;
                   1604:   if (error)
                   1605:     return;  
                   1606:   
1.130     cvs      1607:   /* remove the concurrent cache lock */
                   1608: #ifdef DEBUG_LIBWWW
                   1609:   fprintf (stderr, "Clearing the cache lock\n");
                   1610: #endif /* DEBUG_LIBWWW */
                   1611:   clear_cachelock ();
1.128     cvs      1612:   HTCacheTerminate ();
1.185     cvs      1613:   HTCacheMode_setEnabled (FALSE);
                   1614:   
                   1615:   RecCleanCache (real_dir);
1.128     cvs      1616: 
1.177     cvs      1617:   HTCacheMode_setExpires (cache_expire);
                   1618:   HTCacheMode_setDisconnected (cache_disconnect);
1.183     cvs      1619:   HTCacheInit (cache_dir, cache_size);
1.130     cvs      1620:   /* set a new concurrent cache lock */
1.185     cvs      1621:   strcat (real_dir, ".lock");
                   1622:   if (set_cachelock (real_dir) == -1)
1.130     cvs      1623:     /* couldn't open the .lock file, so, we close the cache to
                   1624:        be in the safe side */
                   1625:     {
                   1626: #ifdef DEBUG_LIBWWW
                   1627:       fprintf (stderr, "couldnt set the cache lock\n");
                   1628: #endif /* DEBUG_LIBWWW */
                   1629:       HTCacheTerminate ();
1.185     cvs      1630:       HTCacheMode_setEnabled (FALSE);
1.130     cvs      1631:     }
                   1632: #ifdef DEBUG_LIBWWW
                   1633:   fprintf (stderr, "set a cache lock\n");
                   1634: #endif /* DEBUG_LIBWWW */
1.185     cvs      1635:   TtaFreeMemory (real_dir);
1.128     cvs      1636:   TtaFreeMemory (cache_dir);
                   1637: #endif /* AMAYA_WWW_CACHE */
                   1638: }
                   1639: 
1.118     cvs      1640: #ifdef AMAYA_WWW_CACHE
                   1641: /*----------------------------------------------------------------------
1.128     cvs      1642:   RecCleanCache
1.118     cvs      1643:   Clears an existing cache directory
                   1644:   ----------------------------------------------------------------------*/
                   1645: #ifdef __STDC__
1.128     cvs      1646: static void RecCleanCache (char *dirname)
1.118     cvs      1647: #else
1.128     cvs      1648: static void RecCleanCache (char *dirname)
                   1649: Document doc;
                   1650: View view;
1.118     cvs      1651: #endif /* __STDC__ */
1.128     cvs      1652: 
                   1653: #ifdef _WINDOWS
                   1654: {
                   1655:   HANDLE hFindFile;
1.191     cvs      1656:   ThotBool status;
1.128     cvs      1657:   WIN32_FIND_DATA ffd;
                   1658:   
                   1659:   char t_dir [MAX_LENGTH];
1.130     cvs      1660:   char *ptr;
1.128     cvs      1661: 
1.130     cvs      1662:   /* create a t_dir name to start searching for files */
1.128     cvs      1663:   if ((strlen (dirname) + 10) > MAX_LENGTH)
                   1664:     /* ERROR: directory name is too big */
                   1665:     return;
                   1666: 
                   1667:   strcpy (t_dir, dirname);
1.130     cvs      1668:   /* save the end of the dirname. We'll use it to make
                   1669:      a complete pathname when erasing files */
                   1670:   ptr = &t_dir[strlen (t_dir)];
                   1671:   strcat (t_dir, "*");
1.128     cvs      1672: 
1.130     cvs      1673:   hFindFile = FindFirstFile (t_dir, &ffd);
1.128     cvs      1674:     
                   1675:   if (hFindFile == INVALID_HANDLE_VALUE)
                   1676:     /* nothing to erase? */
                   1677:     return;
                   1678: 
                   1679:   status = TRUE;
1.130     cvs      1680:   while (status) 
                   1681:     {
                   1682:       if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1.133     cvs      1683:        {
                   1684:          /* it's a directory, erase it recursively */
                   1685:          if (strcmp (ffd.cFileName, "..") && strcmp (ffd.cFileName, "."))
                   1686:            {
                   1687:              strcpy (ptr, ffd.cFileName);
                   1688:              strcat (ptr, DIR_STR);
                   1689:              RecCleanCache (t_dir);
1.137     cvs      1690: #         ifdef _WINDOWS
                   1691:              _rmdir (t_dir);
                   1692: #         else /* !_WINDOWS */
1.133     cvs      1693:              rmdir (t_dir);
1.137     cvs      1694: #         endif /* _WINDOWS */
1.133     cvs      1695:            }
                   1696:        }
                   1697:        else
1.130     cvs      1698:          {
1.133     cvs      1699:            /* it's a file, erase it */
1.130     cvs      1700:            strcpy (ptr, ffd.cFileName);
1.133     cvs      1701:            TtaFileUnlink (t_dir);
1.130     cvs      1702:          }
                   1703:       status = FindNextFile (hFindFile, &ffd);
                   1704:     }
1.128     cvs      1705:   FindClose (hFindFile);
                   1706: }
                   1707: 
                   1708: #else /* _WINDOWS */
1.118     cvs      1709: {
                   1710:   DIR *dp;
                   1711:   struct stat st;
                   1712: #ifdef HAVE_DIRENT_H
                   1713:   struct dirent *d;
                   1714: #else
                   1715:   struct direct *d;
                   1716: #endif /* HAVE_DIRENT_H */
1.128     cvs      1717:   char filename[BUFSIZ+1];
1.118     cvs      1718: 
                   1719:   if ((dp = opendir (dirname)) == NULL) 
                   1720:     {
                   1721:       /* @@@ we couldn't open the directory ... we need some msg */
                   1722:       perror (dirname);
1.176     cvs      1723:       return;
1.118     cvs      1724:     }
                   1725:   
                   1726:   while ((d = readdir (dp)) != NULL)
                   1727:     {
                   1728:       /* skip the UNIX . and .. links */
                   1729:       if (!strcmp (d->d_name, "..")
                   1730:          || !strcmp (d->d_name, "."))
                   1731:        continue;
                   1732: 
1.188     cvs      1733:       sprintf (filename, "%s%c%s", dirname, DIR_SEP, d->d_name);
1.118     cvs      1734:       if  (lstat (filename, &st) < 0 ) 
                   1735:        {
                   1736:          /* @@2 need some error message */
                   1737:          perror (filename);
                   1738:          continue;
                   1739:        }
                   1740:       
                   1741:       switch (st.st_mode & S_IFMT)
                   1742:        {
                   1743:        case S_IFDIR:
                   1744:          /* if we find a directory, we erase it, recursively */
1.128     cvs      1745:          strcat (filename, DIR_STR);
                   1746:          RecCleanCache (filename);
                   1747:          rmdir (filename);
1.118     cvs      1748:          break;
                   1749:        case S_IFLNK:
                   1750:          /* skip any links we find */
                   1751:          continue;
                   1752:          break;
                   1753:        default:
                   1754:          /* erase the filename */
                   1755:          TtaFileUnlink (filename);
                   1756:          break;
                   1757:        }
                   1758:     }
                   1759:   closedir (dp);
                   1760: }
1.128     cvs      1761: #endif /* _WINDOWS */
1.118     cvs      1762: #endif /* AMAYA_WWW_CACHE */
                   1763: 
1.116     cvs      1764: /*----------------------------------------------------------------------
                   1765:   CacheInit
                   1766:   Reads the cache settings from the thot.ini file.
                   1767:   ----------------------------------------------------------------------*/
1.118     cvs      1768: #ifdef __STDC__
1.116     cvs      1769: static void CacheInit (void)
1.118     cvs      1770: #else
                   1771: static void Cacheinit ()
                   1772: #endif
                   1773: 
1.116     cvs      1774: {
                   1775: #ifndef AMAYA_WWW_CACHE
                   1776:    HTCacheMode_setEnabled (NO);
                   1777: 
                   1778: #else /* AMAYA_WWW_CACHE */
                   1779:   char *strptr;
                   1780:   char *cache_dir = NULL;
1.185     cvs      1781:   char *real_dir = NULL;
1.130     cvs      1782:   char *cache_lockfile;
                   1783:   int cache_size;
1.176     cvs      1784:   int cache_entry_size;
1.191     cvs      1785:   ThotBool cache_enabled;
                   1786:   ThotBool cache_locked;
                   1787:   ThotBool tmp_bool;
1.130     cvs      1788: 
1.185     cvs      1789: int i;
                   1790: 
1.116     cvs      1791:   /* activate cache? */
1.165     cvs      1792:   strptr = (char *) TtaGetEnvString ("ENABLE_CACHE");
1.152     cvs      1793:   if (strptr && *strptr && strcasecmp (strptr, "yes" ))
1.130     cvs      1794:     cache_enabled = NO;
1.116     cvs      1795:   else
1.130     cvs      1796:     cache_enabled = YES;
1.116     cvs      1797: 
1.151     cvs      1798:   /* cache protected documents? */
1.152     cvs      1799:   strptr = (char *) TtaGetEnvString ("CACHE_PROTECTED_DOCS");
                   1800:   if (strptr && *strptr && !strcasecmp (strptr, "yes" ))
1.151     cvs      1801:     HTCacheMode_setProtected (YES);
                   1802:   else
                   1803:     HTCacheMode_setProtected (NO);
                   1804: 
1.116     cvs      1805:   /* get the cache dir (or use a default one) */
                   1806:   strptr = (char *) TtaGetEnvString ("CACHE_DIR");
                   1807:   if (strptr && *strptr) 
                   1808:     {
1.185     cvs      1809:       real_dir = TtaGetMemory (strlen (strptr) + strlen (CACHE_DIR_NAME) + 20);
                   1810:       strcpy (real_dir, strptr);
                   1811:          if (*(real_dir + strlen (real_dir) - 1) != DIR_SEP)
                   1812:            strcat (real_dir, DIR_STR);
1.116     cvs      1813:     }
                   1814:   else
                   1815:     {
1.185     cvs      1816:       real_dir = TtaGetMemory (strlen (TempFileDirectory)
                   1817:                                + strlen (CACHE_DIR_NAME) + 20);
1.186     cvs      1818:       sprintf (real_dir, "%s%s", TempFileDirectory, CACHE_DIR_NAME);
1.185     cvs      1819:     }
                   1820: 
                   1821:   /* compatiblity with previous versions of Amaya: does real_dir
                   1822:      include CACHE_DIR_NAME? If not, add it */
                   1823:   strptr = strstr (real_dir, CACHE_DIR_NAME);
                   1824:   if (!strptr)
                   1825:   {
                   1826:     strcat (real_dir, CACHE_DIR_NAME);
                   1827:   }
                   1828:   else
                   1829:     {
                   1830:       i = strlen (CACHE_DIR_NAME);
                   1831:          if (strptr[i] != EOS)
                   1832:           strcat (real_dir, CACHE_DIR_NAME);
1.116     cvs      1833:     }
1.185     cvs      1834: 
                   1835:  
                   1836:   cache_dir = TtaGetMemory (strlen (real_dir) + 10);
1.190     cvs      1837:   sprintf (cache_dir, "file:%s", real_dir);
1.116     cvs      1838: 
                   1839:   /* get the cache size (or use a default one) */
1.185     cvs      1840:   strptr = (char *) TtaGetEnvString ("CACHE_SIZE");
1.116     cvs      1841:   if (strptr && *strptr) 
1.185     cvs      1842:     cache_size = atoi (strptr);
1.116     cvs      1843:   else
                   1844:     cache_size = DEFAULT_CACHE_SIZE;
                   1845: 
1.176     cvs      1846:   /* get the max cached file size (or use a default one) */
                   1847:   if (!TtaGetEnvInt ("MAX_CACHE_ENTRY_SIZE", &cache_entry_size))
                   1848:     cache_entry_size = DEFAULT_MAX_CACHE_ENTRY_SIZE;
                   1849: 
1.130     cvs      1850:   if (cache_enabled) 
1.116     cvs      1851:     {
                   1852:       /* how to remove the lock? force remove it? */
1.185     cvs      1853:       cache_lockfile = TtaGetMemory (strlen (real_dir) + 20);
                   1854:       strcpy (cache_lockfile, real_dir);
1.186     cvs      1855:       strcat (cache_lockfile, ".lock");
1.130     cvs      1856:       cache_locked = FALSE;
                   1857:       if (TtaFileExist (cache_lockfile)
                   1858:          && !(cache_locked = test_cachelock (cache_lockfile)))
1.116     cvs      1859:        {
1.130     cvs      1860: #ifdef DEBUG_LIBWWW
                   1861:          fprintf (stderr, "found a stale cache, removing it\n");
                   1862: #endif /* DEBUG_LIBWWW */
                   1863:          /* remove the lock and clean the cache (the clean cache 
                   1864:             will remove all, making the following call unnecessary */
                   1865:          /* little trick to win some memory */
                   1866:          strptr = strrchr (cache_lockfile, '.');
                   1867:          *strptr = EOS;
                   1868:          RecCleanCache (cache_lockfile);
                   1869:          *strptr = '.';
1.116     cvs      1870:        }
                   1871: 
1.130     cvs      1872:       if (!cache_locked) 
                   1873:        {
                   1874:          /* initialize the cache if there's no other amaya
                   1875:             instance running */
1.177     cvs      1876:          HTCacheMode_setMaxCacheEntrySize (cache_entry_size);
                   1877:          if (TtaGetEnvBoolean ("CACHE_EXPIRE_IGNORE", &tmp_bool) 
                   1878:              && tmp_bool)
                   1879:            HTCacheMode_setExpires (HT_EXPIRES_IGNORE);
                   1880:          else
                   1881:            HTCacheMode_setExpires (HT_EXPIRES_AUTO);
                   1882:          TtaGetEnvBoolean ("CACHE_DISCONNECTED_MODE", &tmp_bool);
                   1883:          if (tmp_bool)
                   1884:            HTCacheMode_setDisconnected (HT_DISCONNECT_NORMAL);
                   1885:          else
                   1886:            HTCacheMode_setDisconnected (HT_DISCONNECT_NONE);
1.188     cvs      1887:          if (HTCacheInit (cache_dir, cache_size))
1.130     cvs      1888:            {
1.188     cvs      1889:              if (set_cachelock (cache_lockfile) == -1)
                   1890:                /* couldn't open the .lock file, so, we close the cache to
                   1891:                   be in the safe side */
                   1892:                {
                   1893:                  HTCacheTerminate ();
                   1894:                  HTCacheMode_setEnabled (FALSE);
                   1895: #ifdef DEBUG_LIBWWW
                   1896:                  fprintf (stderr, "couldnt set the cache lock\n");
                   1897: #endif /* DEBUG_LIBWWW */
                   1898:                }
1.130     cvs      1899: #ifdef DEBUG_LIBWWW
1.188     cvs      1900:              else
                   1901:                fprintf (stderr, "created the cache lock\n");
1.130     cvs      1902: #endif /* DEBUG_LIBWWW */
                   1903:            }
1.188     cvs      1904:          else
                   1905:            {
1.130     cvs      1906: #ifdef DEBUG_LIBWWW
1.188     cvs      1907:              fprintf (stderr, "couldn't create the cache\n");
                   1908: #endif /* DEBUG_LIBWWW */            
                   1909:              HTCacheTerminate ();
                   1910:            }
1.130     cvs      1911:        }
                   1912:       else 
                   1913:        {
1.185     cvs      1914:          HTCacheMode_setEnabled (FALSE);
1.130     cvs      1915: #ifdef DEBUG_LIBWWW
                   1916:          fprintf (stderr, "lock detected, starting libwww without a cache/n");
                   1917: #endif /* DEBUG_LIBWWW */
                   1918:        }
                   1919:       TtaFreeMemory (cache_lockfile);
1.116     cvs      1920:     }
                   1921:   else
1.185     cvs      1922:     {
                   1923:       HTCacheMode_setEnabled (FALSE);
                   1924:     }
1.116     cvs      1925:   if (cache_dir)
                   1926:     TtaFreeMemory (cache_dir);
1.189     cvs      1927:   if (real_dir)
                   1928:     TtaFreeMemory (real_dir);
1.185     cvs      1929:   /* warn the user if the cache isn't active */
                   1930:   if (cache_enabled && !HTCacheMode_enabled ())
                   1931:     {
                   1932: #ifdef _WINDOWS   
                   1933:       MessageBox (NULL, TtaGetMessage (AMAYA, AM_CANT_CREATE_CACHE), 
1.187     cvs      1934:                  "Cache", MB_OK);
1.185     cvs      1935: #else /* !_WINDOWS */
                   1936:       TtaDisplayMessage (CONFIRM, TtaGetMessage (AMAYA, AM_CANT_CREATE_CACHE),
                   1937:                         NULL);
                   1938: #endif /* _WINDOWS */
                   1939:     }
1.116     cvs      1940: #endif /* AMAYA_WWW_CACHE */
1.15      cvs      1941: }
                   1942: 
                   1943: /*----------------------------------------------------------------------
1.97      cvs      1944:   ProxyInit
                   1945:   Reads any proxies settings which may be declared as environmental
                   1946:   variables or in the thot.ini file. The former overrides the latter.
                   1947:   ----------------------------------------------------------------------*/
1.118     cvs      1948: #ifdef __STDC__
1.97      cvs      1949: static void ProxyInit (void)
1.118     cvs      1950: #else
                   1951: static void ProxyInit ()
                   1952: #endif /* __STDC__ */
1.97      cvs      1953: {
                   1954:   char *strptr;
                   1955:   char *str = NULL;
                   1956:   char *name;
                   1957: 
                   1958:   /* get the proxy settings from the thot.ini file */
                   1959:   strptr = (char *) TtaGetEnvString ("HTTP_PROXY");
                   1960:   if (strptr && *strptr)
                   1961:     HTProxy_add ("http", strptr);
                   1962:   /* get the no_proxy settings from the thot.ini file */
                   1963:   strptr = (char *) TtaGetEnvString ("NO_PROXY");
                   1964:   if (strptr && *strptr) 
                   1965:     {
                   1966:       str = TtaStrdup (strptr);          /* Get copy we can mutilate */
                   1967:       strptr = str;
                   1968:       while ((name = HTNextField (&strptr)) != NULL) {
                   1969:        char *portstr = strchr (name, ':');
                   1970:        unsigned port=0;
                   1971:        if (portstr) {
                   1972:          *portstr++ = '\0';
1.116     cvs      1973:          if (*portstr) port = (unsigned) atoi (portstr);
1.97      cvs      1974:        }
                   1975:        /* Register it for all access methods */
                   1976:        HTNoProxy_add (name, NULL, port);
                   1977:       }
                   1978:       TtaFreeMemory (str);
                   1979:     }
                   1980:   
                   1981:   /* use libw3's routine to get all proxy settings from the environment */
                   1982:    HTProxy_getEnvVar ();
                   1983: }
                   1984: 
                   1985: 
                   1986: /*----------------------------------------------------------------------
1.17      cvs      1987:   AHTProfile_newAmaya
                   1988:   creates the Amaya client profile for libwww.
1.15      cvs      1989:   ----------------------------------------------------------------------*/
                   1990: #ifdef __STDC__
                   1991: static void         AHTProfile_newAmaya (char *AppName, char *AppVersion)
                   1992: #else  /* __STDC__ */
                   1993: static void         AHTProfile_newAmaya (AppName, AppVersion)
                   1994: char               *AppName;
                   1995: char               *AppVersion;
                   1996: #endif /* __STDC__ */
1.4       cvs      1997: {
1.158     cvs      1998:    char * strptr;
                   1999: 
1.4       cvs      2000:    /* If the Library is not already initialized then do it */
                   2001:    if (!HTLib_isInitialized ())
                   2002:       HTLibInit (AppName, AppVersion);
                   2003: 
                   2004:    if (!converters)
                   2005:       converters = HTList_new ();
1.27      cvs      2006:    if (!acceptTypes)
                   2007:       acceptTypes = HTList_new ();
1.193     cvs      2008:    if (!acceptLanguages)
                   2009:       acceptLanguages = HTList_new ();
1.151     cvs      2010:    if (!transfer_encodings)
                   2011:       transfer_encodings = HTList_new ();
                   2012:    if (!content_encodings)
                   2013:       content_encodings = HTList_new ();
1.4       cvs      2014: 
1.169     cvs      2015:    /* inhibits libwww's automatic file_suffix_binding */
                   2016:    HTFile_doFileSuffixBinding (FALSE);
                   2017: 
1.4       cvs      2018:    /* Register the default set of transport protocols */
                   2019:    HTTransportInit ();
                   2020: 
                   2021:    /* Register the default set of application protocol modules */
                   2022:    AHTProtocolInit ();
                   2023: 
1.116     cvs      2024:    /* Register the default set of messages and dialog functions */
                   2025:    AHTAlertInit ();
                   2026:    HTAlert_setInteractive (YES);
                   2027: 
                   2028: #ifdef AMAYA_WWW_CACHE
                   2029:    /* Enable the persistent cache  */
                   2030:    CacheInit ();
                   2031: #else
                   2032:    HTCacheMode_setEnabled (NO);
                   2033: #endif /* AMAYA_WWW_CACHE */
1.4       cvs      2034: 
                   2035:    /* Register the default set of BEFORE and AFTER filters */
                   2036:    AHTNetInit ();
                   2037: 
                   2038:    /* Set up the default set of Authentication schemes */
1.167     cvs      2039:    HTAA_newModule ("basic", HTBasic_generate, HTBasic_parse, NULL,
                   2040:                    HTBasic_delete);
1.158     cvs      2041:    /* activate MDA by defaul */
                   2042:    strptr = (char *) TtaGetEnvString ("ENABLE_MDA");
                   2043:    if (!strptr || (strptr && *strptr && strcasecmp (strptr, "no" )))
                   2044:      HTAA_newModule ("digest", HTDigest_generate, HTDigest_parse, 
1.167     cvs      2045:                     HTDigest_updateInfo, HTDigest_delete);
1.4       cvs      2046: 
1.97      cvs      2047:    /* Get any proxy settings */
                   2048:    ProxyInit ();
1.4       cvs      2049: 
                   2050:    /* Register the default set of converters */
                   2051:    AHTConverterInit (converters);
1.151     cvs      2052:    HTFormat_setConversion (converters);
1.27      cvs      2053:    AHTAcceptTypesInit (acceptTypes);
1.193     cvs      2054:    AHTAcceptLanguagesInit (acceptLanguages);
1.4       cvs      2055: 
                   2056:    /* Register the default set of transfer encoders and decoders */
1.151     cvs      2057:    HTTransferEncoderInit (transfer_encodings);
1.190     cvs      2058:    HTFormat_setTransferCoding (transfer_encodings);
                   2059:    /* Register the default set of content encoders and decoders */
                   2060:    HTContentEncoderInit (content_encodings);
                   2061:    /* ignore all other encoding formats (or libwww will send them 
1.181     cvs      2062:       thru a blackhole otherwise */
                   2063:    HTCoding_add (content_encodings, "*", NULL, HTIdentityCoding, 1.0);
1.151     cvs      2064:    if (HTList_count(content_encodings) > 0)
                   2065:      HTFormat_setContentCoding(content_encodings);
                   2066:    else 
                   2067:      {
                   2068:        HTList_delete(content_encodings);
                   2069:        content_encodings = NULL;
                   2070:      }
1.4       cvs      2071: 
                   2072:    /* Register the default set of MIME header parsers */
1.74      cvs      2073:    HTMIMEInit ();   /* must be called again for language selector */
1.4       cvs      2074: 
                   2075:    /* Register the default set of Icons for directory listings */
1.27      cvs      2076:    /*HTIconInit(NULL); *//* experimental */
1.4       cvs      2077: }
                   2078: 
1.5       cvs      2079: /*----------------------------------------------------------------------
1.17      cvs      2080:   AHTProfile_delete
                   2081:   deletes the Amaya client profile.
1.5       cvs      2082:   ----------------------------------------------------------------------*/
1.4       cvs      2083: #ifdef __STDC__
                   2084: static void         AHTProfile_delete (void)
                   2085: #else
                   2086: static void         AHTProfile_delete ()
1.7       cvs      2087: #endif                         /* __STDC__ */
1.4       cvs      2088: {
1.22      cvs      2089:  
                   2090:   /* free the Amaya global context */
1.151     cvs      2091: 
                   2092:   /* Clean up all the registred converters */
                   2093:   HTFormat_deleteAll ();
                   2094:   if (acceptTypes)
1.74      cvs      2095:     HTConversion_deleteAll (acceptTypes);
1.193     cvs      2096:   if (acceptLanguages)
                   2097:     HTLanguage_deleteAll (acceptLanguages);
1.151     cvs      2098: 
1.74      cvs      2099:   HTList_delete (Amaya->docid_status);
                   2100:   HTList_delete (Amaya->reqlist);
                   2101:   TtaFreeMemory (Amaya);
1.61      cvs      2102: 
1.151     cvs      2103:   if (HTLib_isInitialized ())
1.74      cvs      2104:       
1.120     cvs      2105: #ifdef _WINDOWS
1.151     cvs      2106:     HTEventTerminate ();
1.120     cvs      2107: #endif _WINDOWS;               
1.74      cvs      2108:     
1.151     cvs      2109:   /* Clean up the persistent cache (if any) */
1.116     cvs      2110: #ifdef AMAYA_WWW_CACHE
1.151     cvs      2111:   clear_cachelock ();
                   2112:   HTCacheTerminate ();
1.116     cvs      2113: #endif /* AMAYA_WWW_CACHE */
1.74      cvs      2114:     
1.151     cvs      2115:   /* Terminate libwww */
                   2116:   HTLibTerminate ();
1.4       cvs      2117: }
                   2118: 
1.5       cvs      2119: /*----------------------------------------------------------------------
1.116     cvs      2120:   AmayacontextInit
                   2121:   initializes an internal Amaya context for our libwww interface 
                   2122:   ----------------------------------------------------------------------*/
                   2123: #ifdef __STDC__
                   2124: static void                AmayaContextInit ()
                   2125: #else
                   2126: static void                AmayaContextInit ()
                   2127: #endif
                   2128: 
                   2129: {
1.140     cvs      2130:   AmayaAlive_flag = TRUE;
1.116     cvs      2131:   /* Initialization of the global context */
                   2132:   Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
                   2133:   Amaya->reqlist = HTList_new ();
                   2134:   Amaya->docid_status = HTList_new ();
                   2135:   Amaya->open_requests = 0;
                   2136: }
                   2137: 
                   2138: /*----------------------------------------------------------------------
1.17      cvs      2139:   QueryInit
                   2140:   initializes the libwww interface 
1.5       cvs      2141:   ----------------------------------------------------------------------*/
1.4       cvs      2142: #ifdef __STDC__
                   2143: void                QueryInit ()
                   2144: #else
                   2145: void                QueryInit ()
                   2146: #endif
                   2147: {
1.151     cvs      2148:   char *strptr;
                   2149:   int tmp_i;
                   2150:   long tmp_l;
1.4       cvs      2151: 
1.116     cvs      2152:    AmayaContextInit ();
1.4       cvs      2153:    AHTProfile_newAmaya (HTAppName, HTAppVersion);
1.140     cvs      2154:    CanDoStop_set (TRUE);
1.4       cvs      2155: 
1.116     cvs      2156: #ifdef _WINDOWS
1.125     cvs      2157:    HTEventInit ();
1.116     cvs      2158: #endif /* _WINDOWS */
1.72      cvs      2159: 
1.123     cvs      2160: #ifndef _WINDOWS
1.116     cvs      2161:    HTEvent_setRegisterCallback ((void *) AHTEvent_register);
                   2162:    HTEvent_setUnregisterCallback ((void *) AHTEvent_unregister);
1.120     cvs      2163:    HTTimer_registerSetTimerCallback ((void *) AMAYA_SetTimer);
                   2164:    HTTimer_registerDeleteTimerCallback ((void *) AMAYA_DeleteTimer);
1.116     cvs      2165: #endif /* !_WINDOWS */
1.190     cvs      2166: 
                   2167:    /*** @@@@
1.176     cvs      2168:    WWW_TraceFlag = 0;
1.190     cvs      2169:    ***/
1.74      cvs      2170: #ifdef DEBUG_LIBWWW
1.116     cvs      2171:   /* forwards error messages to our own function */
                   2172:    WWW_TraceFlag = THD_TRACE;
1.138     cvs      2173:    HTTrace_setCallback(LineTrace);
1.4       cvs      2174:    /* Trace activation (for debugging) */
1.148     cvs      2175:    /***
1.138     cvs      2176:     WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | SHOW_PROTOCOL_TRACE;
                   2177:     WWW_TraceFlag |= 0xFFFFFFFFl;
                   2178:     ***/
1.152     cvs      2179: #endif
                   2180: 
1.148     cvs      2181:    /* Setting up different network parameters */
1.151     cvs      2182: 
1.148     cvs      2183:    /* Maximum number of simultaneous open sockets */
1.151     cvs      2184:    strptr = (char *) TtaGetEnvString ("MAX_SOCKET");
                   2185:    if (strptr && *strptr) 
                   2186:      tmp_i = atoi (strptr);
                   2187:    else
                   2188:      tmp_i = DEFAULT_MAX_SOCKET;
                   2189:    HTNet_setMaxSocket (tmp_i);
                   2190: 
1.148     cvs      2191:    /* different network services timeouts */
1.151     cvs      2192:    /* dns timeout */
                   2193:    strptr = (char *) TtaGetEnvString ("DNS_TIMEOUT");
                   2194:    if (strptr && *strptr) 
                   2195:      tmp_i = atoi (strptr);
                   2196:    else
                   2197:      tmp_i = DEFAULT_DNS_TIMEOUT;
                   2198:    HTDNS_setTimeout (tmp_i);
                   2199: 
                   2200:    /* persistent connections timeout */
1.152     cvs      2201:    strptr = (char *) TtaGetEnvString ("PERSIST_CX_TIMEOUT");
1.151     cvs      2202:    if (strptr && *strptr) 
                   2203:      tmp_l = atol (strptr);
                   2204:    else
                   2205:      tmp_l = DEFAULT_PERSIST_TIMEOUT;
                   2206:    HTHost_setPersistTimeout (tmp_l);
                   2207: 
1.148     cvs      2208:    /* default timeout in ms */
1.151     cvs      2209:    strptr = (char *) TtaGetEnvString ("NET_EVENT_TIMEOUT");
                   2210:    if (strptr && *strptr) 
                   2211:      tmp_i = atoi (strptr);
                   2212:    else
                   2213:      tmp_i = DEFAULT_NET_EVENT_TIMEOUT;
                   2214:    HTHost_setEventTimeout (tmp_i);
                   2215: 
1.173     cvs      2216:    HTRequest_setMaxRetry (8);
1.4       cvs      2217: #ifdef CATCH_SIG
1.148     cvs      2218:    signal (SIGPIPE, SIG_IGN);
1.4       cvs      2219: #endif
1.15      cvs      2220: }
                   2221: 
                   2222: /*----------------------------------------------------------------------
1.17      cvs      2223:   LoopForStop
1.136     cvs      2224:   a copy of the Thop event loop so we can handle the stop button in Unix
                   2225:   and preemptive requests under Windows
1.15      cvs      2226:   ----------------------------------------------------------------------*/
                   2227: #ifdef __STDC__
                   2228: static int          LoopForStop (AHTReqContext * me)
                   2229: #else
                   2230: static int          LoopForStop (AHTReqContext * me)
                   2231: #endif
                   2232: {
1.136     cvs      2233: #ifdef _WINDOWS
                   2234:   MSG msg;
                   2235:   unsigned long libwww_msg;
                   2236:   HWND old_active_window, libwww_window;
                   2237:   int  status_req = HT_OK;
                   2238: 
                   2239:   old_active_window = GetActiveWindow ();
                   2240:   libwww_window = HTEventList_getWinHandle (&libwww_msg);
1.137     cvs      2241:  
1.149     cvs      2242:   while (me->reqStatus != HT_END && me->reqStatus != HT_ERR
                   2243:             && me->reqStatus != HT_ABORT && AmayaIsAlive () &&
                   2244:             GetMessage (&msg, NULL, 0, 0))
                   2245:                {
                   2246:          if (msg.message != WM_QUIT)
                   2247:                        {
                   2248:                      TranslateMessage (&msg);
                   2249:                      DispatchMessage (&msg);
                   2250:                        }
                   2251:          else
                   2252:                break;      
                   2253:                }
1.136     cvs      2254:   if (!AmayaIsAlive ())
                   2255:     /* Amaya was killed by one of the callback handlers */
                   2256:     exit (0);
                   2257: #else /* _WINDOWS */
1.25      cvs      2258:    extern ThotAppContext app_cont;
1.51      cvs      2259:    XEvent                ev;
                   2260:    XtInputMask           status;
1.17      cvs      2261:    int                 status_req = HT_OK;
1.15      cvs      2262: 
                   2263:    /* to test the async calls  */
1.17      cvs      2264:    /* Loop while waiting for new events, exists when the request is over */
1.15      cvs      2265:    while (me->reqStatus != HT_ABORT &&
                   2266:          me->reqStatus != HT_END &&
1.69      cvs      2267:          me->reqStatus != HT_ERR) {
1.116     cvs      2268:         if (!AmayaIsAlive ())
1.69      cvs      2269:            /* Amaya was killed by one of the callback handlers */
                   2270:            exit (0);
                   2271: 
                   2272:         status = XtAppPending (app_cont);
1.144     cvs      2273:         if (status & XtIMXEvent)
                   2274:           {
                   2275:             XtAppNextEvent (app_cont, &ev);
                   2276:             TtaHandleOneEvent (&ev);
                   2277:           } 
                   2278:         else if (status != 0) 
                   2279:           XtAppProcessEvent (app_cont, XtIMAll);
1.69      cvs      2280:    }
1.136     cvs      2281: #endif /* _WINDOWS */
1.69      cvs      2282:    switch (me->reqStatus) {
                   2283:          case HT_ERR:
                   2284:           case HT_ABORT:
1.130     cvs      2285:               status_req = NO;
1.15      cvs      2286:               break;
                   2287: 
1.69      cvs      2288:          case HT_END:
1.130     cvs      2289:               status_req = YES;
1.15      cvs      2290:               break;
                   2291: 
1.69      cvs      2292:          default:
1.15      cvs      2293:               break;
1.69      cvs      2294:    }
1.15      cvs      2295:    return (status_req);
1.4       cvs      2296: }
                   2297: 
1.5       cvs      2298: /*----------------------------------------------------------------------
1.15      cvs      2299:   QueryClose
1.21      cvs      2300:   closes all existing threads, frees all non-automatically deallocated
                   2301:   memory and then ends libwww.
1.5       cvs      2302:   ----------------------------------------------------------------------*/
1.116     cvs      2303: void QueryClose ()
1.4       cvs      2304: {
1.24      cvs      2305: 
1.140     cvs      2306:   AmayaAlive_flag = FALSE;
1.24      cvs      2307: 
1.116     cvs      2308:   /* remove all the handlers and callbacks that may output a message to
                   2309:      a non-existent Amaya window */
1.130     cvs      2310: #ifndef _WINDOWS
1.116     cvs      2311:   HTNet_deleteAfter (AHTLoadTerminate_handler);
1.130     cvs      2312: #endif _WINDOWS
1.116     cvs      2313:   HTNet_deleteAfter (redirection_handler);
                   2314:   HTAlertCall_deleteAll (HTAlert_global () );
                   2315:   HTAlert_setGlobal ((HTList *) NULL);
                   2316:   HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
                   2317:   HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
                   2318: #ifndef _WINDOWS
                   2319:   /** need to erase all existing timers too **/
                   2320:    HTTimer_registerSetTimerCallback (NULL);
                   2321:    HTTimer_registerDeleteTimerCallback (NULL);
                   2322: #endif /* !_WINDOWS */
                   2323:   HTHost_setActivateRequestCallback (NULL);
                   2324:   Thread_deleteAll ();
1.21      cvs      2325:  
1.116     cvs      2326:   HTProxy_deleteAll ();
                   2327:   HTNoProxy_deleteAll ();
                   2328:   HTGateway_deleteAll ();
                   2329:   AHTProfile_delete ();
                   2330: }
                   2331: 
                   2332: /*----------------------------------------------------------------------
                   2333:   NextNameValue
                   2334:   ---------------------------------------------------------------------*/
                   2335: #ifdef __STDC__
                   2336: static char * NextNameValue (char ** pstr, char **name, char **value)
                   2337: #else
                   2338: static char * NextNameValue (pstr, name, value);
                   2339: char ** pstr;
                   2340: char **name;
                   2341: char **value;
                   2342: #endif /* __STDC__ */
                   2343: {
                   2344:   char * p = *pstr;
                   2345:   char * start = NULL;
                   2346:   if (!pstr || !*pstr) return NULL;
                   2347:   
                   2348:     if (!*p) {
                   2349:       *pstr = p;
                   2350:       *name = NULL;
                   2351:       *value = NULL;
                   2352:       return NULL;                                      /* No field */
                   2353:     }
                   2354:     
                   2355:     /* Now search for the next '&' and ';' delimitators */
                   2356:     start = p;
                   2357:     while (*p && *p != '&' && *p != ';') p++;
                   2358:     if (*p) 
                   2359:       *p++ = '\0';
                   2360:     *pstr = p;
                   2361: 
                   2362:     /* Search for the name and value */
                   2363:     *name = start;
                   2364:     p = start;
                   2365:     
                   2366:     while(*p && *p != '=') 
                   2367:       p++;
                   2368:     if (*p) 
                   2369:       *p++ = '\0';
                   2370:     *value = p;
                   2371: 
                   2372:     return start;
                   2373: }
                   2374: 
                   2375: /*----------------------------------------------------------------------
                   2376:   PrepareFormdata
                   2377:   ---------------------------------------------------------------------*/
                   2378: #ifdef __STDC__
                   2379: static HTAssocList * PrepareFormdata (char *string)
                   2380: #else
                   2381: static HTAssocList * PrepareFormdata (string)
                   2382: char *string;
                   2383: #endif /* __STDC__ */
                   2384: {
                   2385:   char * tmp_string, * tmp_string_ptr;
                   2386:   char * name;
                   2387:   char * value;
                   2388:   HTAssocList * formdata;
                   2389: 
                   2390:   if (!string)
                   2391:     return NULL;
                   2392: 
                   2393:   /* store the ptr in another variable, as the original address will
                   2394:      change
                   2395:      */
                   2396: 
                   2397:   tmp_string_ptr = tmp_string = TtaStrdup (string);
                   2398:   formdata = HTAssocList_new();
                   2399:   
                   2400:   while (*tmp_string)
                   2401:     {
                   2402:       NextNameValue (&tmp_string, &name, &value);
                   2403:       HTAssocList_addObject(formdata,
                   2404:                            name, value);
                   2405:     }
                   2406: 
                   2407:   TtaFreeMemory (tmp_string_ptr);
                   2408:   return formdata;
1.4       cvs      2409: }
                   2410: 
1.99      cvs      2411: 
                   2412: /*----------------------------------------------------------------------
                   2413:   InvokeGetObjectWWW_callback
                   2414:   A simple function to invoke a callback function whenever there's an error
                   2415:   in GetObjectWWW
                   2416:   ---------------------------------------------------------------------*/
                   2417: 
1.116     cvs      2418: #ifdef __STDC__
                   2419: void      InvokeGetObjectWWW_callback (int docid, char *urlName, char *outputfile, TTcbf *terminate_cbf, void *context_tcbf, int status)
1.99      cvs      2420: #else
1.111     cvs      2421: void      InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf, context_tcbf, status)
1.99      cvs      2422: int docid;
                   2423: char *urlName;
                   2424: char *outputfile;
                   2425: TTcbf *terminate_cbf;
                   2426: void *context_tcbf;
1.116     cvs      2427: #endif /* __STDC__ */
1.99      cvs      2428: {
                   2429:   if (!terminate_cbf)
                   2430:     return;
                   2431:   
1.116     cvs      2432:   (*terminate_cbf) (docid, status, urlName, outputfile,
1.99      cvs      2433:                    NULL, context_tcbf);  
                   2434: }
                   2435: 
                   2436: 
                   2437: 
1.5       cvs      2438: /*----------------------------------------------------------------------
1.15      cvs      2439:    GetObjectWWW
1.17      cvs      2440:    this function requests a resource designated by a URLname into a
                   2441:    temporary filename. The download can come from a simple GET operation,
                   2442:    or can come from POSTING/GETTING a form. In the latter
                   2443:    case, the function receives a query string to send to the server.
                   2444: 
1.5       cvs      2445:    4  file retrieval modes are proposed:                              
                   2446:    AMAYA_SYNC : blocking mode                            
                   2447:    AMAYA_ISYNC : incremental, blocking mode              
                   2448:    AMAYA_ASYNC : non-blocking mode                       
                   2449:    AMAYA_IASYNC : incremental, non-blocking mode         
                   2450:    
                   2451:    In the incremental mode, each time a package arrives, it will be   
                   2452:    stored in the temporary file. In addition, if an                   
                   2453:    incremental_callback function is defined, this function will be    
                   2454:    called and handled a copy of the newly received data package.      
                   2455:    Finally, if a terminate_callback function is defined, it will be   
                   2456:    invoked when the request terminates. The caller of this function
1.4       cvs      2457:    can define two different contexts to be passed to the callback
                   2458:    functions.
                   2459: 
                   2460:    When the function is called with the SYNC mode, the function will
                   2461:    return only when the requested file has been loaded.
                   2462:    The ASYNC mode will immediately return after setting up the
                   2463:    call.
                   2464: 
                   2465:    Notes:
                   2466:    At the end of a succesful request, the urlName string contains the
                   2467:    name of the actually retrieved URL. As a URL can change over the time,
                   2468:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      2469:    caller verify the value of the urlName variable at the end of
1.4       cvs      2470:    a request.
                   2471: 
                   2472:    Inputs:
                   2473:    - docid  Document identifier for the set of objects being
                   2474:    retrieved.
                   2475:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   2476:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   2477:    - mode The retrieval mode.
                   2478:    - incremental_cbf 
                   2479:    - context_icbf
                   2480:    Callback and context for the incremental modes
                   2481:    - terminate_cbf 
                   2482:    - context_icbf
                   2483:    Callback and context for a terminate handler
1.17      cvs      2484:    -error_html if TRUE, then display any server error message as an
                   2485:    HTML document.
1.88      cvs      2486:    - content_type a string
                   2487:  
1.4       cvs      2488:    Outputs:
                   2489:    - urlName The URL that was retrieved
                   2490:    - outputfile The name of the temporary file which holds the
                   2491:    retrieved data. (Only in case of success)
1.88      cvs      2492:    - if content_type wasn't NULL, it will contain a copy of the parameter
                   2493:      sent in the HTTP answer
1.4       cvs      2494:    Returns:
                   2495:    HT_ERROR
                   2496:    HT_OK
1.5       cvs      2497:  
                   2498:   ----------------------------------------------------------------------*/
1.4       cvs      2499: #ifdef __STDC__
1.127     cvs      2500: int GetObjectWWW (int docid, char* urlName, char* formdata,
1.116     cvs      2501:                  char* outputfile, int mode, TIcbf* incremental_cbf, 
                   2502:                  void* context_icbf, TTcbf* terminate_cbf, 
1.191     cvs      2503:                  void* context_tcbf, ThotBool error_html, char *content_type)
1.4       cvs      2504: #else
1.127     cvs      2505: int GetObjectWWW (docid, urlName, formdata, outputfile, mode, 
1.116     cvs      2506:                  incremental_cbf, context_icbf, 
1.88      cvs      2507:                  terminate_cbf, context_tcbf, error_html, content_type)
1.73      cvs      2508: int           docid;
                   2509: char         *urlName;
1.127     cvs      2510: char         *formdata;
1.73      cvs      2511: char         *outputfile;
                   2512: int           mode;
                   2513: TIcbf        *incremental_cbf;
                   2514: void         *context_icbf;
                   2515: TTcbf        *terminate_cbf;
                   2516: void         *context_tcbf;
1.191     cvs      2517: ThotBool      error_html;
1.88      cvs      2518: char        *content_type;
1.4       cvs      2519: #endif
                   2520: {
                   2521:    AHTReqContext      *me;
                   2522:    char               *ref;
1.107     cvs      2523:    int                 status, l;
1.114     cvs      2524:    int                 tempsubdir;
1.191     cvs      2525:    ThotBool            bool_tmp;
1.7       cvs      2526: 
1.116     cvs      2527:    if (urlName == NULL || docid == 0 || outputfile == NULL) 
                   2528:      {
                   2529:        /* no file to be loaded */
                   2530:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.69      cvs      2531:        
1.116     cvs      2532:        if (error_html)
1.69      cvs      2533:         /* so we can show the error message */
                   2534:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2535:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2536:                                    context_tcbf, HT_ERROR);
                   2537:        return HT_ERROR;
                   2538:      }
1.7       cvs      2539: 
1.159     cvs      2540:    /* if it's a 'docImage', we have already downloaded it */
                   2541:    if (!strncmp ("internal:", urlName, 9)) 
                   2542:      {
                   2543:        strcpy (outputfile, urlName);
                   2544:        InvokeGetObjectWWW_callback (docid, urlName, outputfile,
                   2545:                                    terminate_cbf, context_tcbf, HT_OK);
                   2546:        return HT_OK;
                   2547:      }
                   2548: 
1.4       cvs      2549:    /* do we support this protocol? */
1.116     cvs      2550:    if (IsValidProtocol (urlName) == NO) 
                   2551:      {
                   2552:        /* return error */
                   2553:        outputfile[0] = EOS;    /* file could not be opened */
                   2554:        TtaSetStatus (docid, 1, 
                   2555:                     TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
                   2556:                     urlName);
                   2557: 
                   2558:        if (error_html)
1.69      cvs      2559:         /* so we can show the error message */
                   2560:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2561:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2562:                                    context_tcbf, HT_ERROR);
                   2563:        return HT_ERROR;
                   2564:      }
1.4       cvs      2565: 
1.114     cvs      2566:    /* we store CSS in subdir named 0; all the other files go to a subidr
                   2567:       named after their own docid */
                   2568:    
                   2569:    tempsubdir = (mode & AMAYA_LOAD_CSS) ? 0 : docid;
                   2570: 
1.116     cvs      2571:    /* create a tempfilename */
                   2572:    sprintf (outputfile, "%s%c%d%c%04dAM", 
                   2573:            TempFileDirectory, DIR_SEP, tempsubdir, DIR_SEP, object_counter);
                   2574:    /* update the object_counter (used for the tempfilename) */
1.4       cvs      2575:    object_counter++;
1.116     cvs      2576:    
1.4       cvs      2577:    /* normalize the URL */
1.45      cvs      2578:    ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4       cvs      2579:    /* should we abort the request if we could not normalize the url? */
1.69      cvs      2580:    if (ref == (char*) NULL || ref[0] == EOS) {
                   2581:       /*error */
                   2582:       outputfile[0] = EOS;
                   2583:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   2584:       
                   2585:       if (error_html)
1.116     cvs      2586:        /* so we can show the error message */
                   2587:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99      cvs      2588:       InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.116     cvs      2589:                                   context_tcbf, HT_ERROR);
1.69      cvs      2590:       return HT_ERROR;
                   2591:    }
1.116     cvs      2592: 
1.4       cvs      2593:    /* verify if that file name existed */
1.9       cvs      2594:    if (TtaFileExist (outputfile))
1.77      cvs      2595:      TtaFileUnlink (outputfile);
1.116     cvs      2596:    
1.4       cvs      2597:    /* Initialize the request structure */
                   2598:    me = AHTReqContext_new (docid);
1.116     cvs      2599:    if (me == NULL) 
                   2600:      {
                   2601:        outputfile[0] = EOS;
                   2602:        /* need an error message here */
                   2603:        TtaFreeMemory (ref);
                   2604:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2605:                                   context_tcbf, HT_ERROR);
                   2606:        return HT_ERROR;
                   2607:      }
                   2608: 
1.4       cvs      2609:    /* Specific initializations for POST and GET */
1.116     cvs      2610:    if (mode & AMAYA_FORM_POST)
                   2611:      {
                   2612:        me->method = METHOD_POST;
                   2613:        HTRequest_setMethod (me->request, METHOD_POST);
                   2614:      }
                   2615:    else 
                   2616:      {
                   2617:        me->method = METHOD_GET;
                   2618:        if (!HasKnownFileSuffix (ref))
                   2619:         HTRequest_setConversion(me->request, acceptTypes, TRUE);
1.193     cvs      2620:        HTRequest_setLanguage (me->request, acceptLanguages, TRUE);
1.116     cvs      2621:      }
1.93      cvs      2622: 
1.116     cvs      2623:    /* Common initialization for all HTML methods */
1.4       cvs      2624:    me->mode = mode;
                   2625:    me->error_html = error_html;
                   2626:    me->incremental_cbf = incremental_cbf;
                   2627:    me->context_icbf = context_icbf;
                   2628:    me->terminate_cbf = terminate_cbf;
                   2629:    me->context_tcbf = context_tcbf;
1.64      cvs      2630: 
1.69      cvs      2631:    /* for the async. request modes, we need to have our
1.4       cvs      2632:       own copy of outputfile and urlname
1.116     cvs      2633:       */
1.4       cvs      2634: 
1.116     cvs      2635:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) 
                   2636:      {
                   2637:        l = strlen (outputfile);
                   2638:        if (l > MAX_LENGTH)
                   2639:         me->outputfile = TtaGetMemory (l + 2);
                   2640:        else
                   2641:         me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
                   2642:        strcpy (me->outputfile, outputfile);
                   2643:        l = strlen (urlName);
                   2644:        if (l > MAX_LENGTH)
                   2645:         me->urlName = TtaGetMemory (l + 2);
                   2646:        else
                   2647:         me->urlName = TtaGetMemory (MAX_LENGTH + 2);
                   2648:        strcpy (me->urlName, urlName);
                   2649: #ifdef _WINDOWS
                   2650:      /* force windows ASYNC requests to always be non preemptive */
                   2651:      HTRequest_setPreemptive (me->request, NO);
                   2652: #endif /*_WINDOWS */
                   2653:      } /* AMAYA_ASYNC mode */ 
                   2654:    else 
                   2655: #ifdef _WINDOWS
                   2656:      {
                   2657:        me->outputfile = outputfile;
                   2658:        me->urlName = urlName;
                   2659:        /* force windows SYNC requests to always be non preemptive */
                   2660:        HTRequest_setPreemptive (me->request, YES);
                   2661:      }
                   2662: #else /* !_WINDOWS */
                   2663:      {
                   2664:        me->outputfile = outputfile;
                   2665:        me->urlName = urlName;
                   2666:      }
                   2667:    /***
1.136     cvs      2668:      In order to take into account the stop button, 
                   2669:      the requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1.57      cvs      2670:      we will loop until the document has been received or a stop signal
                   2671:      generated
1.77      cvs      2672:      ****/
1.116     cvs      2673:    HTRequest_setPreemptive (me->request, NO);
                   2674: #endif /* _WINDOWS */
1.61      cvs      2675: 
1.151     cvs      2676:    /*
                   2677:    ** Make sure that the first request is flushed immediately and not
                   2678:    ** buffered in the output buffer
                   2679:    */
                   2680:    if (mode & AMAYA_FLUSH_REQUEST)
                   2681:      HTRequest_setFlush(me->request, YES);
                   2682:    HTRequest_setFlush(me->request, YES);
                   2683: 
1.61      cvs      2684:    /* prepare the URLname that will be displayed in teh status bar */
                   2685:    ChopURL (me->status_urlName, me->urlName);
1.77      cvs      2686:    TtaSetStatus (me->docid, 1, 
                   2687:                 TtaGetMessage (AMAYA, AM_FETCHING),
                   2688:                 me->status_urlName);
1.4       cvs      2689: 
                   2690:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45      cvs      2691:    TtaFreeMemory (ref);
1.177     cvs      2692:    
                   2693:    TtaGetEnvBoolean ("CACHE_DISCONNECTED_MODE", &bool_tmp);
                   2694:    if (!bool_tmp && (mode & AMAYA_NOCACHE))
1.116     cvs      2695:       HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
                   2696: 
                   2697:    /* prepare the query string and format for POST */
                   2698:    if (mode & AMAYA_FORM_POST)
1.114     cvs      2699:      {
1.116     cvs      2700:        HTAnchor_setFormat ((HTParentAnchor *) me->anchor, 
                   2701:                           HTAtom_for ("application/x-www-form-urlencoded"));
                   2702:        HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
                   2703:        HTRequest_setEntityAnchor (me->request, me->anchor);
                   2704:      } 
1.114     cvs      2705: 
1.127     cvs      2706:    /* create the formdata element for libwww */
                   2707:    if (formdata)
                   2708:      me->formdata = PrepareFormdata (formdata);
                   2709: 
1.116     cvs      2710:    /* do the request */
                   2711:    if (mode & AMAYA_FORM_POST)
1.119     cvs      2712:      {
1.191     cvs      2713:        /* this call doesn't give back a ThotBool */
1.119     cvs      2714:        HTParentAnchor * posted = NULL;
                   2715: 
                   2716:        posted = HTPostFormAnchor (me->formdata, (HTAnchor *) me->anchor, 
                   2717:                                    me->request);
                   2718:        status = posted ? YES : NO; 
                   2719:      }
1.127     cvs      2720:    else if (formdata)
                   2721:      status = HTGetFormAnchor(me->formdata, (HTAnchor *) me->anchor,
                   2722:                              me->request);
1.116     cvs      2723:    else
1.77      cvs      2724:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      2725: 
1.123     cvs      2726:    /* @@@ may need some special windows error msg here */
1.116     cvs      2727:    /* control the errors */
1.4       cvs      2728: 
1.130     cvs      2729:     if (status == NO)
1.116     cvs      2730:      /* the request invocation failed */
                   2731:      {
                   2732:        /* show an error message on the status bar */
                   2733:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   2734:        TtaSetStatus (docid, 1, 
                   2735:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                   2736:                     urlName);
                   2737:        if (me->reqStatus == HT_NEW)
                   2738:         /* manually invoke the last processing that usually gets done
                   2739:            in a succesful request */
                   2740:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
                   2741:                                      terminate_cbf, context_tcbf, HT_ERROR);
                   2742:        /* terminate_handler wasn't called */
1.136     cvs      2743:        AHTReqContext_delete (me);
1.116     cvs      2744:      }
                   2745:    else
1.136     cvs      2746:      /* end treatment for SYNC requests */
1.120     cvs      2747:      if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   2748:        {
1.136     cvs      2749:         /* wait here untilt the asynchronous request finishes */
1.130     cvs      2750:         status = LoopForStop (me);
1.136     cvs      2751:         /* if status returns HT_ERROR, should we invoke the callback? */
1.120     cvs      2752:         if (!HTRequest_kill (me->request))
                   2753:           AHTReqContext_delete (me);
                   2754:        }
1.130     cvs      2755: 
1.136     cvs      2756:     /* an interface problem!!! */
                   2757:     return (status == YES ? 0 : -1);
1.4       cvs      2758: }
                   2759: 
1.5       cvs      2760: /*----------------------------------------------------------------------
1.17      cvs      2761:    PutObjectWWW
                   2762:    frontend for uploading a resource to a URL. This function downloads
                   2763:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   2764:    finish the job.
                   2765: 
1.5       cvs      2766:    2 upload modes are proposed:                                       
                   2767:    AMAYA_SYNC : blocking mode                            
                   2768:    AMAYA_ASYNC : non-blocking mode                       
                   2769:    
1.4       cvs      2770:    When the function is called with the SYNC mode, the function will
                   2771:    return only when the file has been uploaded.
                   2772:    The ASYNC mode will immediately return after setting up the
                   2773:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   2774:    call back terminate_cbf, handling it the context defined in
                   2775:    context_tcbf.
                   2776: 
                   2777:    Notes:
                   2778:    At the end of a succesful request, the urlName string contains the
                   2779:    name of the actually uploaded URL. As a URL can change over the time,
                   2780:    (e.g., be redirected elsewhere), it is advised that the function
                   2781:    caller verifies the value of the urlName variable at the end of
                   2782:    a request.
                   2783: 
                   2784:    Inputs:
                   2785:    - docid  Document identifier for the set of objects being
                   2786:    retrieved.
                   2787:    - fileName A pointer to the local file to upload
                   2788:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   2789:    - mode The retrieval mode.
                   2790:    - terminate_cbf 
                   2791:    - context_icbf
                   2792:    Callback and context for a terminate handler
                   2793: 
                   2794:    Outputs:
                   2795:    - urlName The URL that was uploaded
                   2796: 
                   2797:    Returns:
                   2798:    HT_ERROR
                   2799:    HT_OK
1.5       cvs      2800:   ----------------------------------------------------------------------*/
1.4       cvs      2801: #ifdef __STDC__
1.27      cvs      2802: int                 PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4       cvs      2803:                                  TTcbf * terminate_cbf, void *context_tcbf)
                   2804: #else
1.26      cvs      2805: int                 PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4       cvs      2806:                                  ,terminate_cbf, context_tcbf)
                   2807: int                 docid;
                   2808: char               *urlName;
                   2809: char               *fileName;
                   2810: int                 mode;
1.27      cvs      2811: PicType             contentType;
1.4       cvs      2812: TTcbf              *terminate_cbf;
                   2813: void               *context_tcbf;
                   2814: 
1.116     cvs      2815: #endif /* __STDC__ */
1.4       cvs      2816: {
1.116     cvs      2817:    AHTReqContext      *me;
1.4       cvs      2818:    int                 status;
                   2819:    int                 fd;
                   2820:    struct stat         file_stat;
1.116     cvs      2821:    char               *fileURL;
1.168     cvs      2822:    char               *etag = NULL;
1.169     cvs      2823:    HTParentAnchor     *dest_anc_parent;
                   2824:    char               *tmp;
1.168     cvs      2825:    int                 UsePreconditions;
1.191     cvs      2826:    ThotBool            lost_update_check = TRUE;
1.172     cvs      2827: 
                   2828:    /* should we protect the PUT against lost updates? */
                   2829:    tmp = (char *) TtaGetEnvString ("ENABLE_LOST_UPDATE_CHECK");
                   2830:    if (tmp && *tmp && strcasecmp (tmp, "yes" ))
                   2831:      lost_update_check = FALSE;
1.168     cvs      2832: 
                   2833:    UsePreconditions = mode & AMAYA_USE_PRECONDITIONS;
1.4       cvs      2834: 
1.33      cvs      2835:    AmayaLastHTTPErrorMsg [0] = EOS;
                   2836:    
1.116     cvs      2837:    if (urlName == NULL || docid == 0 || fileName == NULL 
                   2838:        || !TtaFileExist (fileName))
1.4       cvs      2839:       /* no file to be uploaded */
                   2840:       return HT_ERROR;
                   2841: 
                   2842:    /* do we support this protocol? */
1.7       cvs      2843:    if (IsValidProtocol (urlName) == NO)
                   2844:      {
                   2845:        /* return error */
1.77      cvs      2846:        TtaSetStatus (docid, 1, 
                   2847:                       TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7       cvs      2848:                      urlName);
                   2849:        return HT_ERROR;
                   2850:      }
1.116     cvs      2851: 
                   2852:    /* verify the file's size */
                   2853: #ifndef _WINDOWS
1.7       cvs      2854:    if ((fd = open (fileName, O_RDONLY)) == -1)
1.116     cvs      2855: #else 
1.137     cvs      2856:    if ((fd = _open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.116     cvs      2857: #endif /* _WINDOWS */
1.7       cvs      2858:      {
                   2859:        /* if we could not open the file, exit */
                   2860:        /*error msg here */
                   2861:        return (HT_ERROR);
                   2862:      }
1.4       cvs      2863: 
                   2864:    fstat (fd, &file_stat);
1.137     cvs      2865: #  ifdef _WINDOWS
                   2866:    _close (fd);
                   2867: #  else /* _WINDOWS */
1.116     cvs      2868:    close (fd);
1.137     cvs      2869: #  endif /* _WINDOWS */
1.4       cvs      2870: 
1.7       cvs      2871:    if (file_stat.st_size == 0)
                   2872:      {
                   2873:        /* file was empty */
                   2874:        /*errmsg here */
                   2875:        return (HT_ERROR);
                   2876:      }
1.116     cvs      2877: 
                   2878:    /* prepare the request context */
1.4       cvs      2879:    if (THD_TRACE)
1.116     cvs      2880:       fprintf (stderr, "file size == %u\n", (unsigned) file_stat.st_size);
1.4       cvs      2881: 
                   2882:    me = AHTReqContext_new (docid);
1.7       cvs      2883:    if (me == NULL)
                   2884:      {
1.168     cvs      2885:        /* @@ need an error message here */
1.151     cvs      2886:        TtaHandlePendingEvents (); 
1.7       cvs      2887:        return (HT_ERROR);
                   2888:      }
1.116     cvs      2889: 
1.4       cvs      2890:    me->mode = mode;
                   2891:    me->incremental_cbf = (TIcbf *) NULL;
                   2892:    me->context_icbf = (void *) NULL;
                   2893:    me->terminate_cbf = terminate_cbf;
                   2894:    me->context_tcbf = context_tcbf;
                   2895:    me->urlName = urlName;
1.116     cvs      2896:    me->block_size =  file_stat.st_size;
1.17      cvs      2897:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      2898:    me->method = METHOD_PUT;
                   2899:    me->output = stdout;
1.17      cvs      2900:    /* we are not expecting to receive any input from the server */
                   2901:    me->outputfile = (char *) NULL; 
1.4       cvs      2902: 
1.134     cvs      2903: #ifdef _WINDOWS
                   2904:    /* libwww's HTParse function doesn't take into account the drive name;
1.168     cvs      2905:       so we sidestep it */
1.134     cvs      2906:    fileURL = NULL;
                   2907:    StrAllocCopy (fileURL, "file:");
                   2908:    StrAllocCat (fileURL, fileName);
                   2909: #else
1.116     cvs      2910:    fileURL = HTParse (fileName, "file:/", PARSE_ALL);
1.134     cvs      2911: #endif /* _WINDOWS */
1.168     cvs      2912:    me->source = HTAnchor_findAddress (fileURL);
1.116     cvs      2913:    HT_FREE (fileURL);
1.168     cvs      2914:    me->dest = HTAnchor_findAddress (urlName);
1.169     cvs      2915:    /* we memorize the anchor's parent @ as we use it a number of times
                   2916:       in the following lines */
                   2917:    dest_anc_parent = HTAnchor_parent (me->dest);
1.168     cvs      2918: 
1.169     cvs      2919:    /*
                   2920:    **  Set the Content-Type of the file we are uploading 
                   2921:    */
                   2922:    /* we try to use any content-type previosuly associated
                   2923:       with the parent. If it doesn't exist, we try to guess it
                   2924:       from the URL */
                   2925:    tmp = HTAtom_name (HTAnchor_format (dest_anc_parent));
                   2926:    if (!tmp || !strcmp (tmp, "www/unknown"))
                   2927:      {
                   2928:        HTAnchor_setFormat (dest_anc_parent,
                   2929:                           AHTGuessAtom_for (me->urlName, contentType));
                   2930:        tmp = HTAtom_name (HTAnchor_format (dest_anc_parent));
                   2931:      }
                   2932:    /* .. and we give the same type to the source anchor */
                   2933:    /* we go thru setOutputFormat, rather than change the parent's
                   2934:       anchor, as that's the place that libwww expects it to be */
                   2935: 
1.170     cvs      2936:    HTAnchor_setFormat (HTAnchor_parent (me->source),
1.169     cvs      2937:                       HTAtom_for (tmp));
                   2938:    HTRequest_setOutputFormat (me->request,
                   2939:                              HTAtom_for (tmp));
1.134     cvs      2940:    /* define other request characteristics */
1.116     cvs      2941: #ifdef _WINDOWS
1.136     cvs      2942:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      2943: #else
1.134     cvs      2944:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      2945: #endif /* _WINDOWS */
                   2946: 
1.151     cvs      2947:    /*
                   2948:    ** Make sure that the first request is flushed immediately and not
                   2949:    ** buffered in the output buffer
                   2950:    */
                   2951:    if (mode & AMAYA_FLUSH_REQUEST)
                   2952:      HTRequest_setFlush(me->request, YES);
1.168     cvs      2953:    
                   2954:    /* Should we use preconditions? */
1.172     cvs      2955:    if (lost_update_check)
1.168     cvs      2956:      {
1.172     cvs      2957:        if (UsePreconditions) 
                   2958:         etag = HTAnchor_etag (HTAnchor_parent (me->dest));
                   2959:        
                   2960:        if (etag) 
                   2961:         {
                   2962:           HTRequest_setPreconditions(me->request, HT_MATCH_THIS);
                   2963:         }
                   2964:        else
                   2965:         {
                   2966:           HTRequest_setPreconditions(me->request, HT_NO_MATCH);
                   2967:           HTRequest_addAfter(me->request, check_handler, NULL, NULL, HT_ALL,
                   2968:                              HT_FILTER_MIDDLE, YES);
1.175     cvs      2969:           HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL, 
                   2970:                               HT_NO_ACCESS, HT_FILTER_MIDDLE, YES);
                   2971:           HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL,
                   2972:                               HT_REAUTH, HT_FILTER_MIDDLE, YES);
                   2973:           HTRequest_addAfter (me->request, HTAuthInfoFilter, "http://*", NULL,
                   2974:                               HT_ALL, HT_FILTER_MIDDLE, YES);
                   2975:           HTRequest_addAfter (me->request, HTUseProxyFilter, "http://*", NULL,
                   2976:                               HT_USE_PROXY, HT_FILTER_MIDDLE, YES);
1.172     cvs      2977:         }
1.168     cvs      2978:      }
                   2979:    else
                   2980:      {
1.172     cvs      2981:        /* don't use preconditions */
1.168     cvs      2982:        HTRequest_setPreconditions(me->request, HT_NO_MATCH);
                   2983:      }
1.151     cvs      2984:    
1.136     cvs      2985:    /* don't use the cache while saving a document */
                   2986:    HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
1.116     cvs      2987: 
1.164     cvs      2988:    /* Throw away any reponse body */
                   2989:    /*
                   2990:    HTRequest_setOutputStream (me->request, HTBlackHole());        
                   2991:    */
                   2992: 
1.168     cvs      2993:    /* prepare the URLname that will be displayed in the status bar */
1.74      cvs      2994:    ChopURL (me->status_urlName, me->urlName);
                   2995:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
1.168     cvs      2996:                 me->status_urlName);
1.114     cvs      2997: 
1.164     cvs      2998:    /* make the request */
1.172     cvs      2999:    if (lost_update_check && (!UsePreconditions || !etag))
1.168     cvs      3000:      status = HTHeadAnchor (me->dest, me->request);
                   3001:    else
                   3002:      status = HTPutDocumentAnchor (HTAnchor_parent (me->source), me->dest, me->request);
1.4       cvs      3003: 
1.130     cvs      3004:    if (status == YES && me->reqStatus != HT_ERR)
1.7       cvs      3005:      {
1.168     cvs      3006:        /* part of the stop button handler */
                   3007:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   3008:         status = LoopForStop (me);
1.15      cvs      3009:      }
1.136     cvs      3010:    if (!HTRequest_kill (me->request))
                   3011:      AHTReqContext_delete (me);
1.168     cvs      3012:    
1.116     cvs      3013:    TtaHandlePendingEvents ();
1.90      cvs      3014: 
1.130     cvs      3015:    return (status == YES ? 0 : -1);
1.28      cvs      3016: }
1.4       cvs      3017: 
1.5       cvs      3018: /*----------------------------------------------------------------------
1.138     cvs      3019:   StopRequest
1.17      cvs      3020:   stops (kills) all active requests associated with a docid 
1.5       cvs      3021:   ----------------------------------------------------------------------*/
1.4       cvs      3022: #ifdef __STDC__
                   3023: void                StopRequest (int docid)
                   3024: #else
                   3025: void                StopRequest (docid)
                   3026: int                 docid;
                   3027: #endif
                   3028: {
1.140     cvs      3029:    if (Amaya && CanDoStop ())
1.138     cvs      3030:      { 
1.139     cvs      3031: #if 0 /* for later */
1.140     cvs      3032:        AHTDocId_Status    *docid_status;
                   3033:         /* verify if there are any requests at all associated with docid */
1.138     cvs      3034:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
                   3035:                                                          Amaya->docid_status);
                   3036:        if (docid_status == (AHTDocId_Status *) NULL)
                   3037:         return;
1.139     cvs      3038: #endif /* 0 */
1.138     cvs      3039:        /* temporary call to stop all requests, as libwww changed its API */
                   3040:        StopAllRequests (docid);
                   3041:      }
                   3042: }
                   3043: 
1.190     cvs      3044: /* @@@ the docid parameter isn't used... clean it up */
1.138     cvs      3045: /*----------------------------------------------------------------------
                   3046:   StopAllRequests
                   3047:   stops (kills) all active requests. We use the docid 
                   3048:   ----------------------------------------------------------------------*/
                   3049: #ifdef __STDC__
                   3050: void                StopAllRequests (int docid)
                   3051: #else
1.140     cvs      3052: void                StopAllRequests (docid)
1.138     cvs      3053: int                 docid;
                   3054: #endif
                   3055: {
1.4       cvs      3056:    HTList             *cur;
                   3057:    AHTReqContext      *me;
1.191     cvs      3058:    static ThotBool     lock_stop = 0;
                   3059:    ThotBool            async_flag;
1.116     cvs      3060: 
1.138     cvs      3061:    /* only do the stop if we're not being called while processing a 
1.146     cvs      3062:       request, and if we're not already dealing with a stop */
                   3063:    if (Amaya && CanDoStop () && !lock_stop)
1.7       cvs      3064:      {
1.100     cvs      3065: #ifdef DEBUG_LIBWWW
1.138     cvs      3066:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   3067:                "before kill: %d\n", Amaya->open_requests);
1.100     cvs      3068: #endif /* DEBUG_LIBWWW */
1.150     cvs      3069:        /* enter the critical section */
1.146     cvs      3070:        lock_stop = TRUE; 
1.164     cvs      3071:        /* expire all outstanding timers */
                   3072:        HTTimer_expireAll ();
1.160     cvs      3073:        /* HTNet_killAll (); */
1.116     cvs      3074:        cur = Amaya->reqlist;
                   3075:        while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
                   3076:         {
1.140     cvs      3077:           if (AmayaIsAlive ())
1.160     cvs      3078:             {
1.161     cvs      3079: #ifdef DEBUG_LIBWWW
1.163     cvs      3080:               fprintf (stderr,"StopRequest: killing req %p, url %s, status %d\n", me, me->urlName, me->reqStatus);
1.161     cvs      3081: #endif /* DEBUG_LIBWWW */
                   3082: 
1.165     cvs      3083:               if (me->reqStatus != HT_END && me->reqStatus != HT_ABORT)
1.163     cvs      3084:                 {
                   3085:                   if ((me->mode & AMAYA_ASYNC)
                   3086:                       || (me->mode & AMAYA_IASYNC))
                   3087:                     async_flag = TRUE;
                   3088:                   else
                   3089:                     async_flag = FALSE;
                   3090: 
1.165     cvs      3091:                   /* change the status to say that the request aborted */
                   3092:                   me->reqStatus = HT_ABORT;
                   3093: 
1.163     cvs      3094:                   /* kill the request, using the appropriate function */
                   3095:                   if (me->request->net)
                   3096:                       HTNet_killPipe (me->request->net);
                   3097:                   else
                   3098:                     {
1.160     cvs      3099:                       if (me->terminate_cbf)
                   3100:                         (*me->terminate_cbf) (me->docid, -1, me->urlName,
                   3101:                                               me->outputfile,
                   3102:                                               me->content_type, 
                   3103:                                               me->context_tcbf);
1.164     cvs      3104: 
                   3105:                       if (async_flag) 
1.165     cvs      3106:                         /* explicitly free the request context for async
                   3107:                          requests. The sync requests context is freed by LoopForStop */
1.164     cvs      3108:                           AHTReqContext_delete (me);
1.160     cvs      3109:                     }
1.163     cvs      3110:                   cur = Amaya->reqlist;
                   3111:                 }
1.142     cvs      3112: #ifndef _WINDOWS
                   3113: #ifdef WWW_XWINDOWS
1.140     cvs      3114:           /* to be on the safe side, remove all outstanding X events */
1.160     cvs      3115:                   else 
                   3116:                     RequestKillAllXtevents (me);
1.142     cvs      3117: #endif /* WWW_XWINDOWS */
                   3118: #endif /* !_WINDOWS */
1.160     cvs      3119:             }
1.116     cvs      3120:         }
1.150     cvs      3121:        /* Delete remaining channels */
                   3122:        HTChannel_safeDeleteAll ();
                   3123:        /* exit the critical section */
1.148     cvs      3124:        lock_stop = FALSE; 
1.100     cvs      3125: #ifdef DEBUG_LIBWWW
1.138     cvs      3126:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   3127:                "after kill: %d\n", Amaya->open_requests);
1.100     cvs      3128: #endif /* DEBUG_LIBWWW */
1.138     cvs      3129:      }
                   3130: } /* StopAllRequests */
1.17      cvs      3131: 
                   3132: 
1.105     cvs      3133: /*----------------------------------------------------------------------
                   3134:   AmayaIsAlive
1.140     cvs      3135:   returns the value of the AmayaAlive_flag
1.105     cvs      3136:   ----------------------------------------------------------------------*/
                   3137: #ifdef __STDC__
1.191     cvs      3138: ThotBool AmayaIsAlive (void)
1.105     cvs      3139: #else
1.191     cvs      3140: ThotBool AmayaIsAlive ()
1.105     cvs      3141: #endif /* _STDC_ */
                   3142: {
1.140     cvs      3143:   return AmayaAlive_flag;
                   3144: }
                   3145: 
                   3146: /*----------------------------------------------------------------------
                   3147:   CanDoStop
                   3148:   returns the value of the CanDoStop flag
                   3149:   ----------------------------------------------------------------------*/
                   3150: #ifdef __STDC__
1.191     cvs      3151: ThotBool CanDoStop (void)
1.140     cvs      3152: #else
1.191     cvs      3153: ThotBool CanDoStop ()
1.140     cvs      3154: #endif /* _STDC_ */
                   3155: {
                   3156:   return CanDoStop_flag;
                   3157: }
                   3158: 
                   3159: /*----------------------------------------------------------------------
                   3160:   CanDoStop_set
                   3161:   sets the value of the CanDoStop flag
                   3162:   ----------------------------------------------------------------------*/
                   3163: #ifdef __STDC__
1.191     cvs      3164: void CanDoStop_set (ThotBool value)
1.140     cvs      3165: #else
                   3166: void CanDoStop (value)
1.191     cvs      3167: ThotBool value;
1.140     cvs      3168: #endif /* _STDC_ */
                   3169: {
                   3170:   CanDoStop_flag = value;
1.176     cvs      3171: }
                   3172: 
                   3173: #ifdef __STDC__
                   3174: void libwww_updateNetworkConf (int status)
                   3175: #else
                   3176: void libwww_updateNetworkConf (status)
                   3177: int status;
                   3178: #endif /*__STDC__*/
                   3179: {
                   3180:   /* @@@ the docid parameter isn't used... clean it up */
                   3181:   int docid = 1;
                   3182: 
                   3183:   /* first, stop all current requests, as the network
                   3184:    may make some changes */
                   3185:   StopAllRequests (docid);
                   3186: 
                   3187:   if (status & AMAYA_PROXY_RESTART)
                   3188:     {
                   3189:       HTProxy_deleteAll ();
                   3190:       ProxyInit ();
                   3191:     }
                   3192: 
                   3193:   if (status & AMAYA_CACHE_RESTART)
                   3194:     {
                   3195:       clear_cachelock ();
                   3196:       HTCacheTerminate ();
                   3197:       HTCacheMode_setEnabled (NO);
                   3198:       CacheInit ();
                   3199:     }
1.193     cvs      3200: 
                   3201:   if (status & AMAYA_LANNEG_RESTART)
                   3202:     {
                   3203:       /* clear the current values */
                   3204:       if (acceptLanguages)
                   3205:        HTLanguage_deleteAll (acceptLanguages);
                   3206:       /* read in the new ones */
                   3207:       acceptLanguages = HTList_new ();
                   3208:       AHTAcceptLanguagesInit (acceptLanguages);
                   3209:     }
1.105     cvs      3210: }
1.116     cvs      3211: 
1.69      cvs      3212: #endif /* AMAYA_JAVA */
1.77      cvs      3213: 
1.116     cvs      3214: /*
                   3215:   end of Module query.c
                   3216: */

Webmaster