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

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

Webmaster