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

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

Webmaster