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

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

Webmaster