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

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

Webmaster