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

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

Webmaster