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

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

Webmaster