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

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

Webmaster