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

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

Webmaster