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

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

Webmaster