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

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

Webmaster