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

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

Webmaster