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

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

Webmaster