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

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.249     kahan    1162:        /* JK: removed it as the kludge doesn't seem useful anymore */
1.160     cvs      1163:        /* kludge for work around libwww problem */
1.249     kahan    1164:        /* || (status == HT_INTERRUPTED && me->method == METHOD_PUT) */
1.116     cvs      1165: #ifdef AMAYA_WWW_CACHE
                   1166:        /* what status to use to know we're downloading from a cache? */
1.238     cvs      1167:        || status ==  HT_NOT_MODIFIED
                   1168:        || status == HT_PARTIAL_CONTENT
1.116     cvs      1169: #endif /* AMAYA_WWW_CACHE */
1.238     cvs      1170:        || me->reqStatus == HT_ABORT)
1.74      cvs      1171:      error_flag = FALSE;
1.220     cvs      1172:      else
1.74      cvs      1173:      error_flag = TRUE;
1.130     cvs      1174: 
1.4       cvs      1175:    /* output any errors from the server */
1.130     cvs      1176: 
1.77      cvs      1177:    /*
1.74      cvs      1178:    ** me->output = output file which will receive an html file
                   1179:    ** me->error_html = yes, output HTML errors in the screen
                   1180:    ** request->error_stack == if there are any errors, they will be here
                   1181:    ** me->error_stream_size If it's != 0 means an error message has already
                   1182:    **                       been written to the stack
                   1183:    */
                   1184:    
1.4       cvs      1185:    /* First, we verify if there are any errors and if they are not
1.17      cvs      1186:    ** yet written to the error stack. If no, then let's try to write them
                   1187:    ** ourselves
                   1188:    */
1.74      cvs      1189:    
                   1190: #ifdef DEBUG_LIBWWW
                   1191:    fprintf (stderr, "terminate_handler: URL is "
                   1192:            "%s, closing FILE %p status is %d\n", me->urlName, me->output, 
                   1193:            status); 
1.69      cvs      1194: #endif
1.74      cvs      1195:    
1.69      cvs      1196:    if (me->output && me->output != stdout)
                   1197:      {
1.214     cvs      1198:        /* we are writing to a local file */
1.74      cvs      1199:        if (me->reqStatus != HT_ABORT)
                   1200:         {                      /* if the request was not aborted and */
1.80      cvs      1201:           if (error_flag &&
                   1202:               me->error_html == TRUE)
                   1203:               /* there were some errors and we want to print them */
                   1204:             {          
                   1205:               if (me->error_stream_size == 0)/* and the stream is empty */
1.210     cvs      1206:                 {
                   1207:                   /* if the transfer was interrupted, the file may not be
                   1208:                      empty. So, we erase it */
                   1209:                   fflush (me->output);
                   1210:                   rewind (me->output);
                   1211:                   AHTError_MemPrint (request); /* copy errors from 
                   1212:                                                **the error stack 
                   1213:                                                ** into the error stream */
                   1214:                 }
1.80      cvs      1215:               if (me->error_stream)
                   1216:                 {      /* if the stream is non-empty */
                   1217:                   fprintf (me->output, me->error_stream);/* output the errors */
1.85      cvs      1218:                   /* Clear the error context, so that we can deal with
                   1219:                      this answer as if it were a normal reply */
                   1220:                    HTError_deleteAll( HTRequest_error (request));
                   1221:                    HTRequest_setError (request, NULL);
                   1222:                    error_flag = 0;
1.74      cvs      1223:                 }
                   1224:             }                  /* if error_stack */
1.85      cvs      1225:         }
                   1226: 
                   1227:        /* if != HT_ABORT */
                   1228:        
                   1229: #ifdef DEBUG_LIBWWW       
                   1230:        fprintf (stderr, "terminate_handler: URL is  %s, closing "
                   1231:                "FILE %p\n", me->urlName, me->output); 
                   1232: #endif
1.74      cvs      1233:        fclose (me->output);
                   1234:        me->output = NULL;
1.69      cvs      1235:      }
1.80      cvs      1236: 
1.214     cvs      1237:    /* a very special handling so that we can put a message box
                   1238:       to tell the user WHAT he has just done... */
                   1239:    if (UserAborted_flag && me->method == METHOD_PUT)
                   1240:      {
                   1241:        HTRequest_addError (request, ERR_FATAL, NO, HTERR_INTERRUPTED,
                   1242:                           "Operation aborted by user", 0, NULL);
                   1243:      }
                   1244: 
1.80      cvs      1245:    if (error_flag)
                   1246:      me->reqStatus = HT_ERR;
1.214     cvs      1247:    else 
                   1248:      if (me->reqStatus != HT_ABORT)
                   1249:        me->reqStatus = HT_END;
1.154     cvs      1250: 
1.238     cvs      1251:    /* copy the headers in which the application is interested */
                   1252:    HTTP_headers_set (request, response, context, status);
1.114     cvs      1253: 
1.115     cvs      1254:    /* to avoid a hangup while downloading css files */
1.140     cvs      1255:    if (AmayaAlive_flag && (me->mode & AMAYA_LOAD_CSS))
1.233     cvs      1256:      TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_ELEMENT_LOADED), me->status_urlName);
1.116     cvs      1257:    
                   1258:   /* don't remove or Xt will hang up during the PUT */
                   1259:    if (AmayaIsAlive ()  && ((me->method == METHOD_POST) ||
                   1260:                            (me->method == METHOD_PUT)))
1.7       cvs      1261:      {
1.80      cvs      1262:        PrintTerminateStatus (me, status);
                   1263:      } 
1.114     cvs      1264: 
1.111     cvs      1265:    ProcessTerminateRequest (request, response, context, status);
1.116     cvs      1266:    
1.151     cvs      1267:    /* stop here */
1.150     cvs      1268:    return HT_ERROR;
1.4       cvs      1269: }
                   1270: 
1.5       cvs      1271: /*----------------------------------------------------------------------
1.17      cvs      1272:   AHTLoadTerminate_handler
1.74      cvs      1273:   this is an application "AFTER" Callback. It's called by the library
                   1274:   when a request has ended, so that we can setup the correct status.
1.5       cvs      1275:   ----------------------------------------------------------------------*/
1.4       cvs      1276: 
                   1277: #ifdef __STDC__
1.111     cvs      1278: int          AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs      1279: #else
1.111     cvs      1280: int          AHTLoadTerminate_handler (request, response, param, status)
1.4       cvs      1281: HTRequest          *request;
                   1282: HTResponse         *response;
                   1283: void               *param;
                   1284: int                 status;
1.69      cvs      1285: 
1.4       cvs      1286: #endif
                   1287: {
1.116     cvs      1288: 
                   1289:   /** @@@@ use this with printstatus ?? */
                   1290: 
1.4       cvs      1291:    AHTReqContext      *me = HTRequest_context (request);
                   1292:    HTAlertCallback    *cbf;
                   1293:    AHTDocId_Status    *docid_status;
                   1294: 
1.7       cvs      1295:    switch (status)
1.116     cvs      1296:      {
                   1297:         case HT_LOADED:
                   1298:           if (PROT_TRACE)
                   1299:             HTTrace ("Load End.... OK: `%s\' has been accessed\n",
                   1300:                      me->status_urlName);
1.4       cvs      1301: 
1.116     cvs      1302:           docid_status = GetDocIdStatus (me->docid,
                   1303:                                          Amaya->docid_status);
1.7       cvs      1304: 
1.116     cvs      1305:           if (docid_status != NULL && docid_status->counter > 1)
                   1306:             TtaSetStatus (me->docid, 1, 
                   1307:                           TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
1.233     cvs      1308:                           me->status_urlName);
1.7       cvs      1309:               break;
1.116     cvs      1310:               
                   1311:      case HT_NO_DATA:
                   1312:        if (PROT_TRACE)
                   1313:         HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n", 
                   1314:                  me->status_urlName);
                   1315:        TtaSetStatus (me->docid, 1, 
                   1316:                     TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
1.233     cvs      1317:                     me->status_urlName);
1.116     cvs      1318:        break;
                   1319:        
                   1320:      case HT_INTERRUPTED:
                   1321:        if (PROT_TRACE)
                   1322:         HTTrace ("Load End.... INTERRUPTED: `%s\'\n", 
                   1323:                  me->status_urlName);
                   1324:        TtaSetStatus (me->docid, 1, 
                   1325:                     TtaGetMessage (AMAYA, AM_LOAD_ABORT), 
                   1326:                     NULL);
                   1327:        break;
1.7       cvs      1328: 
1.116     cvs      1329:      case HT_RETRY:
                   1330:        if (PROT_TRACE)
                   1331:         HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
                   1332:                  HTResponse_retryTime (response));
                   1333:        TtaSetStatus (me->docid, 1, 
                   1334:                     TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
1.233     cvs      1335:                     me->status_urlName);
1.116     cvs      1336:        break;
1.7       cvs      1337: 
1.247     kahan    1338:      case HT_NOT_MODIFIED:
                   1339:        if (PROT_TRACE)
                   1340:         HTTrace ("Load End.... NOT MODIFIED (%s)", me->status_urlName ? me->status_urlName :" <UNKNOWN>");
                   1341:        break;
                   1342:        
1.116     cvs      1343:      case HT_ERROR:
                   1344:        cbf = HTAlert_find (HT_A_MESSAGE);
                   1345:        if (cbf)
                   1346:         (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                   1347:                 HTRequest_error (request), NULL);
1.247     kahan    1348:        if (PROT_TRACE)
                   1349:            HTTrace ("Load End.... ERROR: Can't access `%s\'\n", me->status_urlName ? me->status_urlName :"<UNKNOWN>"); 
                   1350:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD), me->status_urlName ? me->status_urlName : TEXT("<UNKNOWN>"));
1.116     cvs      1351:        break;
1.247     kahan    1352: 
                   1353:      case HTERR_TIMEOUT:
                   1354:        cbf = HTAlert_find (HT_A_MESSAGE);
                   1355:        if (cbf)
                   1356:         (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                   1357:                 HTRequest_error (request), NULL);
1.116     cvs      1358:        if (PROT_TRACE)
1.247     kahan    1359:            HTTrace ("Load End.... REQUEST TIMEOUT: Can't access `%s\'\n", me->status_urlName ? me->status_urlName :"<UNKNOWN>"); 
1.235     cvs      1360:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD), me->status_urlName ? me->status_urlName : TEXT("<UNKNOWN>"));
1.116     cvs      1361:        break;
1.247     kahan    1362: 
1.116     cvs      1363:      default:
                   1364:        if (PROT_TRACE)
                   1365:         HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
                   1366:        break;
                   1367:      }
                   1368:    
                   1369:    return HT_OK;
                   1370: }
1.7       cvs      1371: 
1.248     kahan    1372: /*----------------------------------------------------------------------
                   1373:   LineTrace
                   1374:   Makes a file copy of libwww's traces
                   1375:   ----------------------------------------------------------------------*/
1.116     cvs      1376: static  int LineTrace (const char * fmt, va_list pArgs)
                   1377: {
1.248     kahan    1378:   vfprintf (trace_fp, fmt, pArgs);
                   1379:   return (fflush (trace_fp));
1.4       cvs      1380: }
                   1381: 
1.27      cvs      1382: /*----------------------------------------------------------------------
                   1383:   AHTAcceptTypesInit
1.74      cvs      1384:   This function prepares the Accept header used by Amaya during
                   1385:   the HTTP content negotiation phase
1.27      cvs      1386:   ----------------------------------------------------------------------*/
                   1387: #ifdef __STDC__
                   1388: static void           AHTAcceptTypesInit (HTList *c)
                   1389: #else  /* __STDC__ */
                   1390: static void           AHTAcceptTypesInit (c)
                   1391: HTList             *c;
                   1392: #endif /* __STDC__ */
                   1393: {
1.223     cvs      1394:    if (c == (HTList *) NULL) 
1.27      cvs      1395:       return;
                   1396: 
1.223     cvs      1397:    /* define here all the mime types that Amaya can accept */
1.27      cvs      1398: 
1.223     cvs      1399:    HTConversion_add (c, "image/png",  "www/present", HTThroughLine, 1.0, 0.0, 0.0);
                   1400:    HTConversion_add (c, "image/jpeg", "www/present", HTThroughLine, 1.0, 0.0, 0.0);
                   1401:    HTConversion_add (c, "image/gif",  "www/present", HTThroughLine, 1.0, 0.0, 0.0);
                   1402:    HTConversion_add (c, "image/xbm",  "www/present", HTThroughLine, 1.0, 0.0, 0.0);
                   1403:    HTConversion_add (c, "image/xpm",  "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1.183     cvs      1404: 
1.27      cvs      1405:    /* Define here the equivalences between MIME types and file extensions for
1.223     cvs      1406:       the types that Amaya can display */
1.27      cvs      1407: 
1.176     cvs      1408:    /* Initialize suffix bindings for local files */
                   1409:    HTBind_init();
                   1410: 
1.27      cvs      1411:    /* Register the default set of file suffix bindings */
                   1412:    HTFileInit ();
                   1413: 
                   1414:    /* Don't do any case distinction */
                   1415:    HTBind_caseSensitive (FALSE);
                   1416: }
1.4       cvs      1417: 
1.5       cvs      1418: /*----------------------------------------------------------------------
1.193     cvs      1419:   AHTAcceptLanguagesInit
                   1420:   This function prepares the Accept header used by Amaya during
                   1421:   the HTTP content negotiation phase
                   1422:   ----------------------------------------------------------------------*/
                   1423: #ifdef __STDC__
1.196     cvs      1424: static void         AHTAcceptLanguagesInit (HTList *c)
1.193     cvs      1425: #else  /* __STDC__ */
1.196     cvs      1426: static void         AHTAcceptLanguagesInit (c)
1.193     cvs      1427: HTList             *c;
                   1428: #endif /* __STDC__ */
                   1429: {
1.233     cvs      1430:   CHAR_T*         ptr;
                   1431:   CHAR_T*         lang_list;
1.223     cvs      1432:   char              s[3];
1.196     cvs      1433:   int               count, lg;
                   1434:   double            quality;
                   1435:   ThotBool          still;
1.193     cvs      1436: 
                   1437:   if (c == (HTList *) NULL) 
                   1438:       return;
                   1439:   
1.203     cvs      1440:   lang_list = TtaGetEnvString ("ACCEPT_LANGUAGES");
1.197     cvs      1441:   s[2] = EOS;
1.234     cvs      1442:   if (lang_list && *lang_list != WC_EOS)
1.193     cvs      1443:     {
                   1444:       /* add the default language first  */
                   1445:       HTLanguage_add (c, "*", -1.0);
                   1446:       /* how many languages do we have? */
1.196     cvs      1447:       ptr = lang_list;
                   1448:       count = 0;
1.233     cvs      1449:       while (*ptr != WC_EOS) {
                   1450:             while (*ptr != WC_EOS && (*ptr < TEXT('A') || (*ptr > TEXT('Z') && *ptr < TEXT('a')) || *ptr > TEXT('z')))
1.223     cvs      1451:                   /* skip the whole separator */
                   1452:                   ptr++;
                   1453:             lg = 0;
1.233     cvs      1454:             while ((*ptr >= TEXT('A') && *ptr <= TEXT('Z')) || (*ptr >= TEXT('a') && *ptr <= TEXT('z')) || *ptr == TEXT('-')) {
1.223     cvs      1455:                   /* it's a new language */
                   1456:                   ptr++;
                   1457:                   lg++;
                   1458:                        }
                   1459:             if (lg >= 2)
                   1460:                count++;
1.233     cvs      1461:             if (*ptr != WC_EOS)
1.223     cvs      1462:                ptr++;
                   1463:          }
1.193     cvs      1464: 
1.196     cvs      1465:       if (count > 0)
1.223     cvs      1466:          quality = 1.1 - (double) count/10.0;
1.193     cvs      1467:       else
1.223     cvs      1468:           quality = 1.0;
1.193     cvs      1469: 
1.223     cvs      1470:      /* Read the languages from the registry, then inject them one, by one.
                   1471:         The first one is the one with the highest priority.
                   1472:         The libwww ask for the lowest priority first.
1.196     cvs      1473:       */
                   1474:       ptr--;
                   1475:       still = TRUE;
1.223     cvs      1476:       while (count) {
1.233     cvs      1477:             while (still && (*ptr < TEXT('A') || (*ptr > TEXT('Z') && *ptr < TEXT('a')) || *ptr > TEXT('z')))
1.223     cvs      1478:                   /* skip the whole separator */
                   1479:                   if (ptr > lang_list)
                   1480:                      ptr--;
                   1481:                   else
                   1482:                       still = FALSE;
                   1483:             lg = 0;
1.233     cvs      1484:             while (still && ((*ptr >= TEXT('A') && *ptr <= TEXT('Z')) || (*ptr >= TEXT('a') && *ptr <= TEXT('z')) || *ptr == TEXT('-'))) {
1.223     cvs      1485:                   /* it's a new language */
                   1486:                   if (ptr > lang_list)
                   1487:                      ptr--;
                   1488:                   else
                   1489:                       still = FALSE;
                   1490:                   lg++;
                   1491:                        }
                   1492:             if (lg >= 2) {
                   1493:                if (still)
1.234     cvs      1494:                   wc2iso_strncpy  (s, &ptr[1], 2);
1.223     cvs      1495:                else
1.234     cvs      1496:                    wc2iso_strncpy (s, lang_list, 2);
1.223     cvs      1497:                count--;
1.227     cvs      1498:                HTLanguage_add (c, s, quality);
1.223     cvs      1499:                quality += 0.1;
                   1500:                        }
                   1501:             ptr--;
                   1502:          }
                   1503:   }
1.193     cvs      1504: }
                   1505: 
                   1506: /*----------------------------------------------------------------------
1.17      cvs      1507:   AHTConverterInit
                   1508:   Bindings between a source media type and a destination media type
                   1509:   (conversion).
1.5       cvs      1510:   ----------------------------------------------------------------------*/
1.15      cvs      1511: #ifdef __STDC__
                   1512: static void         AHTConverterInit (HTList *c)
                   1513: #else  /* __STDC__ */
                   1514: static void         AHTConverterInit (c)
                   1515: HTList             *c;
                   1516: #endif /* __STDC__ */
1.4       cvs      1517: {
                   1518: 
                   1519:    /* Handler for custom http error messages */
1.7       cvs      1520:    HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4       cvs      1521: 
                   1522:    /*
                   1523:     ** These are converters that converts to something other than www/present,
                   1524:     ** that is not directly outputting someting to the user on the screen
                   1525:     */
                   1526: 
1.223     cvs      1527:    HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 1.0, 0.0, 0.0);
                   1528:    HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter, 1.0, 0.0, 0.0);
                   1529:    HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader, 1.0, 0.0, 0.0);
                   1530:    HTConversion_add(c,"message/x-rfc822-cont", "*/*", HTMIMEContinue, 1.0, 0.0, 0.0);
                   1531:    HTConversion_add(c,"message/x-rfc822-partial","*/*",        HTMIMEPartial, 1.0, 0.0, 0.0);
                   1532:    HTConversion_add (c, "multipart/*", "*/*", HTBoundary, 1.0, 0.0, 0.0);
                   1533:    HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML, 1.0, 0.0, 0.0);
1.4       cvs      1534: 
                   1535:    /*
1.116     cvs      1536:    ** The following conversions are converting ASCII output from various
                   1537:    ** protocols to HTML objects.
                   1538:    */
1.223     cvs      1539:    HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new, 1.0, 0.0, 0.0);
1.4       cvs      1540: 
                   1541:    /*
1.116     cvs      1542:    ** We also register a special content type guess stream that can figure out
                   1543:    ** the content type by reading the first bytes of the stream
                   1544:    */
1.223     cvs      1545:    HTConversion_add (c, "www/unknown", "*/*", HTGuess_new, 1.0, 0.0, 0.0);
1.4       cvs      1546: 
1.116     cvs      1547: #ifdef AMAYA_WWW_CACHE
1.4       cvs      1548:    /*
1.116     cvs      1549:    ** Register a persistent cache stream which can save an object to local
                   1550:    ** file
                   1551:    */
1.223     cvs      1552:    HTConversion_add (c, "www/cache", "*/*", HTCacheWriter, 1.0, 0.0, 0.0);
                   1553:    HTConversion_add(c,"www/cache-append", "*/*", HTCacheAppend, 1.0, 0.0, 0.0);
1.116     cvs      1554: #endif AMAYA_WWW_CACHE
1.4       cvs      1555: 
                   1556:    /*
1.116     cvs      1557:    ** This dumps all other formats to local disk without any further
                   1558:    ** action taken
                   1559:    */
1.223     cvs      1560:    HTConversion_add (c, "*/*", "www/present", HTSaveLocally, 0.3, 0.0, 0.0);  
1.4       cvs      1561: }
                   1562: 
1.27      cvs      1563: 
1.15      cvs      1564: /*----------------------------------------------------------------------
1.17      cvs      1565:   AHTProtocolInit
                   1566:   Registers all amaya supported protocols.
1.15      cvs      1567:   ----------------------------------------------------------------------*/
1.4       cvs      1568: static void         AHTProtocolInit (void)
                   1569: {
1.233     cvs      1570:   CHAR_T* strptr;
1.4       cvs      1571: 
1.116     cvs      1572:   /* 
                   1573:      NB. Preemptive == YES means Blocking requests
                   1574:      Non-preemptive == NO means Non-blocking requests
                   1575:      */
                   1576:   HTTransport_add("tcp", HT_TP_SINGLE, HTReader_new, HTWriter_new);
                   1577:   HTTransport_add("buffered_tcp", HT_TP_SINGLE, HTReader_new, 
                   1578:                  HTBufferWriter_new);
                   1579:   HTProtocol_add ("http", "buffered_tcp", HTTP_PORT, NO, HTLoadHTTP, NULL);
1.136     cvs      1580: #ifdef _WINDOWS
                   1581:   HTProtocol_add ("file", "local", 0, YES, HTLoadFile, NULL);
                   1582: #else
1.116     cvs      1583:   HTProtocol_add ("file", "local", 0, NO, HTLoadFile, NULL);
1.198     cvs      1584: #endif /* _WINDOWS */
1.116     cvs      1585: #ifdef AMAYA_WWW_CACHE
                   1586:    HTProtocol_add("cache",  "local", 0, YES, HTLoadCache, NULL);
                   1587: #endif /* AMAYA_WWW_CACHE */
1.103     cvs      1588: #if 0 /* experimental code */
1.245     kahan    1589: #endif
1.116     cvs      1590:    HTProtocol_add ("ftp", "tcp", FTP_PORT, NO, HTLoadFTP, NULL);
1.245     kahan    1591: 
1.116     cvs      1592: 
                   1593:    /* initialize pipelining */
1.223     cvs      1594:   strptr = TtaGetEnvString ("ENABLE_PIPELINING");
1.234     cvs      1595:   if (strptr && *strptr && ustrcasecmp (strptr, TEXT("yes")))
1.116     cvs      1596:     HTTP_setConnectionMode (HTTP_11_NO_PIPELINING);
1.4       cvs      1597: }
                   1598: 
1.15      cvs      1599: /*----------------------------------------------------------------------
1.17      cvs      1600:   AHTNetInit
                   1601:   Reegisters "before" and "after" request filters.
1.15      cvs      1602:   ----------------------------------------------------------------------*/
1.4       cvs      1603: static void         AHTNetInit (void)
                   1604: {
                   1605: 
                   1606: /*      Register BEFORE filters
1.74      cvs      1607: **      The BEFORE filters handle proxies, caches, rule files etc.
                   1608: **      The filters are called in the order by which the are registered
                   1609: **      Not done automaticly - may be done by application!
                   1610: */
1.4       cvs      1611: 
1.116     cvs      1612:   HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, HT_FILTER_LATE);
                   1613:   HTNet_addBefore (HTProxyFilter, NULL, NULL, HT_FILTER_LATE);
1.85      cvs      1614:   HTHost_setActivateRequestCallback (AHTOpen_file);
1.4       cvs      1615: 
                   1616: /*      register AFTER filters
1.74      cvs      1617: **      The AFTER filters handle error messages, logging, redirection,
                   1618: **      authentication etc.
                   1619: **      The filters are called in the order by which the are registered
                   1620: **      Not done automaticly - may be done by application!
                   1621: */
1.4       cvs      1622: 
1.223     cvs      1623:   HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS, HT_FILTER_MIDDLE);
                   1624:   HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_REAUTH, HT_FILTER_MIDDLE);
                   1625:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT, HT_FILTER_MIDDLE);
                   1626:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_FOUND, HT_FILTER_MIDDLE);
                   1627:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_SEE_OTHER, HT_FILTER_MIDDLE);
                   1628:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT, HT_FILTER_MIDDLE);
                   1629:   HTNet_addAfter (HTAuthInfoFilter,    "http://*", NULL, HT_ALL, HT_FILTER_MIDDLE);
                   1630:   HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY, HT_FILTER_MIDDLE);
1.151     cvs      1631: #ifdef AMAYA_LOST_UPDATE
1.223     cvs      1632:   HTNet_addAfter (precondition_handler, NULL, NULL, HT_PRECONDITION_FAILED, HT_FILTER_MIDDLE);
1.151     cvs      1633: #endif /* AMAYA_LOST_UPDATE */
1.111     cvs      1634: #ifndef _WINDOWS
1.223     cvs      1635:   HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);       
1.111     cvs      1636: #endif /* !_WINDOWS */
1.116     cvs      1637:    /**** for later ?? ****/
                   1638:    /*  HTNet_addAfter(HTInfoFilter,    NULL,           NULL, HT_ALL,           HT_FILTER_LATE); */
1.245     kahan    1639:   /* @@ JK: Filters for doing ftp authentication */
                   1640:   HTNet_addAfter (HTAuthFilter, "ftp://*", NULL, HT_NO_ACCESS, HT_FILTER_MIDDLE);
                   1641:   HTNet_addAfter (HTAuthFilter, "ftp://*", NULL, HT_REAUTH, HT_FILTER_MIDDLE);
1.51      cvs      1642:    /* handles all errors */
1.116     cvs      1643:   HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4       cvs      1644: }
                   1645: 
1.15      cvs      1646: /*----------------------------------------------------------------------
1.17      cvs      1647:   AHTAlertInit
                   1648:   Register alert messages and their callbacks.
1.15      cvs      1649:   ----------------------------------------------------------------------*/
                   1650: #ifdef __STDC__
                   1651: static void         AHTAlertInit (void)
                   1652: #else
                   1653: static void         AHTAlertInit ()
                   1654: #endif
                   1655: {
                   1656:    HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.120     cvs      1657: #ifdef __WINDOWS
1.70      cvs      1658:    HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
1.116     cvs      1659: #endif /* _WINDOWS */
1.15      cvs      1660:    HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48      cvs      1661:    HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG));       /* process all messages except debug ones*/
1.15      cvs      1662:    HTAlert_add (AHTConfirm, HT_A_CONFIRM);
                   1663:    HTAlert_add (AHTPrompt, HT_A_PROMPT);
                   1664:    HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
1.116     cvs      1665: }
                   1666: 
1.128     cvs      1667: /*----------------------------------------------------------------------
                   1668:   libwww_CleanCache
                   1669:   frontend to the recursive cache cleaning function
                   1670:   ----------------------------------------------------------------------*/
                   1671: #ifdef __STDC__
                   1672: void libwww_CleanCache (void)
                   1673: #else
                   1674: void libwww_CleanCache ()
                   1675: Document doc;
                   1676: View view;
                   1677: #endif /* __STDC__ */
                   1678: {
1.130     cvs      1679: #ifdef AMAYA_WWW_CACHE
1.235     cvs      1680:   CHAR_T*  real_dir;
                   1681:   char*    cache_dir;
                   1682:   char*    tmp;
                   1683:   int      cache_size;
                   1684:   int      cache_expire;
                   1685:   int      cache_disconnect;
1.191     cvs      1686:   ThotBool error;
1.235     cvs      1687:   CHAR_T*  ptr;
1.130     cvs      1688: 
1.128     cvs      1689:   if (!HTCacheMode_enabled ())
1.183     cvs      1690:     /* don't do anything if we're not using a cache */
                   1691:     return;
1.128     cvs      1692:   /* temporarily close down the cache, purge it, then restart */
1.228     cvs      1693:   tmp = HTCacheMode_getRoot ();
1.190     cvs      1694:   /* don't do anything if we don't have a valid cache dir */
                   1695:   if (!tmp || *tmp == EOS)
                   1696:          return;
1.233     cvs      1697:   cache_dir = TtaStrdup (tmp);
1.228     cvs      1698:   HT_FREE (tmp);
1.128     cvs      1699:   cache_size = HTCacheMode_maxSize ();
1.177     cvs      1700:   cache_expire = HTCacheMode_expires ();
                   1701:   cache_disconnect = HTCacheMode_disconnected ();
                   1702: 
1.185     cvs      1703:   /* get something we can work on :) */
1.233     cvs      1704:   tmp = HTWWWToLocal (cache_dir, "file:", NULL);
1.235     cvs      1705:   real_dir = TtaAllocString (strlen (tmp) + 20);
                   1706:   iso2wc_strcpy (real_dir, tmp);
1.190     cvs      1707:   HT_FREE (tmp);
1.185     cvs      1708: 
                   1709:   /* safeguard... abort the operation if cache_dir doesn't end with
                   1710:      CACHE_DIR_NAME */
                   1711:   error = TRUE;
1.198     cvs      1712:   ptr = ustrstr (real_dir, CACHE_DIR_NAME);  
                   1713:     if (ptr && *ptr && !ustrcasecmp (ptr, CACHE_DIR_NAME))
1.185     cvs      1714:       error = FALSE;
                   1715:   if (error)
                   1716:     return;  
                   1717:   
1.130     cvs      1718:   /* remove the concurrent cache lock */
                   1719: #ifdef DEBUG_LIBWWW
                   1720:   fprintf (stderr, "Clearing the cache lock\n");
                   1721: #endif /* DEBUG_LIBWWW */
                   1722:   clear_cachelock ();
1.128     cvs      1723:   HTCacheTerminate ();
1.185     cvs      1724:   HTCacheMode_setEnabled (FALSE);
                   1725:   
                   1726:   RecCleanCache (real_dir);
1.128     cvs      1727: 
1.177     cvs      1728:   HTCacheMode_setExpires (cache_expire);
                   1729:   HTCacheMode_setDisconnected (cache_disconnect);
1.233     cvs      1730:   HTCacheInit (cache_dir, cache_size);
1.130     cvs      1731:   /* set a new concurrent cache lock */
1.198     cvs      1732:   ustrcat (real_dir, TEXT(".lock"));
1.185     cvs      1733:   if (set_cachelock (real_dir) == -1)
1.130     cvs      1734:     /* couldn't open the .lock file, so, we close the cache to
                   1735:        be in the safe side */
                   1736:     {
                   1737: #ifdef DEBUG_LIBWWW
                   1738:       fprintf (stderr, "couldnt set the cache lock\n");
                   1739: #endif /* DEBUG_LIBWWW */
                   1740:       HTCacheTerminate ();
1.185     cvs      1741:       HTCacheMode_setEnabled (FALSE);
1.130     cvs      1742:     }
                   1743: #ifdef DEBUG_LIBWWW
                   1744:   fprintf (stderr, "set a cache lock\n");
                   1745: #endif /* DEBUG_LIBWWW */
1.185     cvs      1746:   TtaFreeMemory (real_dir);
1.128     cvs      1747:   TtaFreeMemory (cache_dir);
                   1748: #endif /* AMAYA_WWW_CACHE */
                   1749: }
                   1750: 
1.118     cvs      1751: #ifdef AMAYA_WWW_CACHE
                   1752: /*----------------------------------------------------------------------
1.128     cvs      1753:   RecCleanCache
1.118     cvs      1754:   Clears an existing cache directory
                   1755:   ----------------------------------------------------------------------*/
                   1756: #ifdef __STDC__
1.233     cvs      1757: static void RecCleanCache (CHAR_T* dirname)
1.118     cvs      1758: #else
1.198     cvs      1759: static void RecCleanCache (dirname)
1.233     cvs      1760: CHAR_T*   dirname;
1.118     cvs      1761: #endif /* __STDC__ */
1.128     cvs      1762: 
                   1763: #ifdef _WINDOWS
                   1764: {
1.223     cvs      1765:   HANDLE          hFindFile;
                   1766:   ThotBool        status;
1.128     cvs      1767:   WIN32_FIND_DATA ffd;
1.233     cvs      1768:   CHAR_T        t_dir [MAX_LENGTH];
                   1769:   CHAR_T*       ptr;
1.128     cvs      1770: 
1.130     cvs      1771:   /* create a t_dir name to start searching for files */
1.234     cvs      1772:   if ((ustrlen (dirname) + 10) > MAX_LENGTH)
1.128     cvs      1773:     /* ERROR: directory name is too big */
                   1774:     return;
                   1775: 
1.234     cvs      1776:   ustrcpy (t_dir, dirname);
1.130     cvs      1777:   /* save the end of the dirname. We'll use it to make
                   1778:      a complete pathname when erasing files */
1.234     cvs      1779:   ptr = &t_dir[ustrlen (t_dir)];
                   1780:   ustrcat (t_dir, TEXT("*"));
1.128     cvs      1781: 
1.130     cvs      1782:   hFindFile = FindFirstFile (t_dir, &ffd);
1.128     cvs      1783:     
                   1784:   if (hFindFile == INVALID_HANDLE_VALUE)
                   1785:     /* nothing to erase? */
                   1786:     return;
                   1787: 
                   1788:   status = TRUE;
1.130     cvs      1789:   while (status) 
                   1790:     {
                   1791:       if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1.133     cvs      1792:        {
                   1793:          /* it's a directory, erase it recursively */
1.233     cvs      1794:          if (ustrcmp (ffd.cFileName, TEXT("..")) && ustrcmp (ffd.cFileName, TEXT(".")))
1.133     cvs      1795:            {
1.233     cvs      1796:              ustrcpy (ptr, ffd.cFileName);
                   1797:              ustrcat (ptr, WC_DIR_STR);
1.133     cvs      1798:              RecCleanCache (t_dir);
1.198     cvs      1799:              urmdir (t_dir);
1.133     cvs      1800:            }
                   1801:        }
                   1802:        else
1.130     cvs      1803:          {
1.133     cvs      1804:            /* it's a file, erase it */
1.233     cvs      1805:            ustrcpy (ptr, ffd.cFileName);
1.133     cvs      1806:            TtaFileUnlink (t_dir);
1.130     cvs      1807:          }
                   1808:       status = FindNextFile (hFindFile, &ffd);
                   1809:     }
1.128     cvs      1810:   FindClose (hFindFile);
                   1811: }
                   1812: 
                   1813: #else /* _WINDOWS */
1.118     cvs      1814: {
                   1815:   DIR *dp;
                   1816:   struct stat st;
                   1817: #ifdef HAVE_DIRENT_H
                   1818:   struct dirent *d;
                   1819: #else
                   1820:   struct direct *d;
                   1821: #endif /* HAVE_DIRENT_H */
1.128     cvs      1822:   char filename[BUFSIZ+1];
1.118     cvs      1823: 
                   1824:   if ((dp = opendir (dirname)) == NULL) 
                   1825:     {
                   1826:       /* @@@ we couldn't open the directory ... we need some msg */
                   1827:       perror (dirname);
1.176     cvs      1828:       return;
1.118     cvs      1829:     }
                   1830:   
                   1831:   while ((d = readdir (dp)) != NULL)
                   1832:     {
                   1833:       /* skip the UNIX . and .. links */
                   1834:       if (!strcmp (d->d_name, "..")
                   1835:          || !strcmp (d->d_name, "."))
                   1836:        continue;
                   1837: 
1.188     cvs      1838:       sprintf (filename, "%s%c%s", dirname, DIR_SEP, d->d_name);
1.118     cvs      1839:       if  (lstat (filename, &st) < 0 ) 
                   1840:        {
                   1841:          /* @@2 need some error message */
                   1842:          perror (filename);
                   1843:          continue;
                   1844:        }
                   1845:       
                   1846:       switch (st.st_mode & S_IFMT)
                   1847:        {
                   1848:        case S_IFDIR:
                   1849:          /* if we find a directory, we erase it, recursively */
1.128     cvs      1850:          strcat (filename, DIR_STR);
                   1851:          RecCleanCache (filename);
                   1852:          rmdir (filename);
1.118     cvs      1853:          break;
                   1854:        case S_IFLNK:
                   1855:          /* skip any links we find */
                   1856:          continue;
                   1857:          break;
                   1858:        default:
                   1859:          /* erase the filename */
                   1860:          TtaFileUnlink (filename);
                   1861:          break;
                   1862:        }
                   1863:     }
                   1864:   closedir (dp);
                   1865: }
1.128     cvs      1866: #endif /* _WINDOWS */
1.118     cvs      1867: #endif /* AMAYA_WWW_CACHE */
                   1868: 
1.116     cvs      1869: /*----------------------------------------------------------------------
                   1870:   CacheInit
                   1871:   Reads the cache settings from the thot.ini file.
                   1872:   ----------------------------------------------------------------------*/
1.118     cvs      1873: #ifdef __STDC__
1.116     cvs      1874: static void CacheInit (void)
1.118     cvs      1875: #else
                   1876: static void Cacheinit ()
                   1877: #endif
                   1878: 
1.116     cvs      1879: {
                   1880: #ifndef AMAYA_WWW_CACHE
                   1881:    HTCacheMode_setEnabled (NO);
                   1882: 
                   1883: #else /* AMAYA_WWW_CACHE */
1.233     cvs      1884:   CHAR_T* strptr;
                   1885:   CHAR_T* real_dir = NULL;
                   1886:   CHAR_T* cache_lockfile;
1.223     cvs      1887:   char      www_realDir[MAX_LENGTH];
                   1888:   char*     cache_dir = NULL;
                   1889:   int       cache_size;
                   1890:   int       cache_entry_size;
                   1891:   ThotBool  cache_enabled;
                   1892:   ThotBool  cache_locked;
                   1893:   ThotBool  tmp_bool;
1.130     cvs      1894: 
1.185     cvs      1895: int i;
                   1896: 
1.116     cvs      1897:   /* activate cache? */
1.203     cvs      1898:   strptr = TtaGetEnvString ("ENABLE_CACHE");
1.234     cvs      1899:   if (strptr && *strptr && ustrcasecmp (strptr, TEXT("yes")))
1.130     cvs      1900:     cache_enabled = NO;
1.116     cvs      1901:   else
1.130     cvs      1902:     cache_enabled = YES;
1.116     cvs      1903: 
1.151     cvs      1904:   /* cache protected documents? */
1.203     cvs      1905:   strptr = TtaGetEnvString ("CACHE_PROTECTED_DOCS");
1.234     cvs      1906:   if (strptr && *strptr && !ustrcasecmp (strptr, TEXT("yes")))
1.151     cvs      1907:     HTCacheMode_setProtected (YES);
                   1908:   else
                   1909:     HTCacheMode_setProtected (NO);
                   1910: 
1.116     cvs      1911:   /* get the cache dir (or use a default one) */
1.203     cvs      1912:   strptr = TtaGetEnvString ("CACHE_DIR");
1.116     cvs      1913:   if (strptr && *strptr) 
                   1914:     {
1.234     cvs      1915:       real_dir = TtaAllocString (ustrlen (strptr) + ustrlen (CACHE_DIR_NAME) + 20);
                   1916:       ustrcpy (real_dir, strptr);
                   1917:          if (*(real_dir + ustrlen (real_dir) - 1) != WC_DIR_SEP)
                   1918:            ustrcat (real_dir, WC_DIR_STR);
1.116     cvs      1919:     }
                   1920:   else
                   1921:     {
1.234     cvs      1922:       real_dir = TtaAllocString (ustrlen (TempFileDirectory) + ustrlen (CACHE_DIR_NAME) + 20);
1.233     cvs      1923:       usprintf (real_dir, TEXT("%s%s"), TempFileDirectory, CACHE_DIR_NAME);
1.185     cvs      1924:     }
                   1925: 
                   1926:   /* compatiblity with previous versions of Amaya: does real_dir
                   1927:      include CACHE_DIR_NAME? If not, add it */
1.234     cvs      1928:   strptr = ustrstr (real_dir, CACHE_DIR_NAME);
1.185     cvs      1929:   if (!strptr)
1.234     cvs      1930:     ustrcat (real_dir, CACHE_DIR_NAME);
1.185     cvs      1931:   else
                   1932:     {
1.234     cvs      1933:       i = ustrlen (CACHE_DIR_NAME);
                   1934:          if (strptr[i] != WC_EOS)
                   1935:           ustrcat (real_dir, CACHE_DIR_NAME);
1.116     cvs      1936:     }
1.185     cvs      1937: 
1.211     cvs      1938:   /* convert the local cache dir into a file URL, as expected by
                   1939:      libwww */
1.223     cvs      1940:  
1.234     cvs      1941:   wc2iso_strcpy (www_realDir, real_dir);
1.223     cvs      1942: 
                   1943:   cache_dir = HTLocalToWWW (www_realDir, "file:");
1.116     cvs      1944: 
                   1945:   /* get the cache size (or use a default one) */
1.203     cvs      1946:   strptr = TtaGetEnvString ("CACHE_SIZE");
1.116     cvs      1947:   if (strptr && *strptr) 
1.233     cvs      1948:     cache_size = wctoi (strptr);
1.116     cvs      1949:   else
                   1950:     cache_size = DEFAULT_CACHE_SIZE;
                   1951: 
1.176     cvs      1952:   /* get the max cached file size (or use a default one) */
1.223     cvs      1953:   if (!TtaGetEnvInt ("MAX_CACHE_ENTRY_SIZE", &cache_entry_size))
1.176     cvs      1954:     cache_entry_size = DEFAULT_MAX_CACHE_ENTRY_SIZE;
                   1955: 
1.130     cvs      1956:   if (cache_enabled) 
1.116     cvs      1957:     {
                   1958:       /* how to remove the lock? force remove it? */
1.234     cvs      1959:       cache_lockfile = TtaAllocString (ustrlen (real_dir) + 20);
                   1960:       ustrcpy (cache_lockfile, real_dir);
                   1961:       ustrcat (cache_lockfile, TEXT(".lock"));
1.130     cvs      1962:       cache_locked = FALSE;
1.223     cvs      1963:       if (TtaFileExist (cache_lockfile) && !(cache_locked = test_cachelock (cache_lockfile)))
1.116     cvs      1964:        {
1.130     cvs      1965: #ifdef DEBUG_LIBWWW
                   1966:          fprintf (stderr, "found a stale cache, removing it\n");
                   1967: #endif /* DEBUG_LIBWWW */
                   1968:          /* remove the lock and clean the cache (the clean cache 
                   1969:             will remove all, making the following call unnecessary */
                   1970:          /* little trick to win some memory */
1.234     cvs      1971:          strptr = ustrrchr (cache_lockfile, TEXT('.'));
                   1972:          *strptr = WC_EOS;
1.130     cvs      1973:          RecCleanCache (cache_lockfile);
1.234     cvs      1974:          *strptr = TEXT('.');
1.116     cvs      1975:        }
                   1976: 
1.130     cvs      1977:       if (!cache_locked) 
                   1978:        {
                   1979:          /* initialize the cache if there's no other amaya
                   1980:             instance running */
1.177     cvs      1981:          HTCacheMode_setMaxCacheEntrySize (cache_entry_size);
1.223     cvs      1982:          if (TtaGetEnvBoolean ("CACHE_EXPIRE_IGNORE", &tmp_bool) && tmp_bool)
1.177     cvs      1983:            HTCacheMode_setExpires (HT_EXPIRES_IGNORE);
                   1984:          else
                   1985:            HTCacheMode_setExpires (HT_EXPIRES_AUTO);
1.223     cvs      1986:          TtaGetEnvBoolean ("CACHE_DISCONNECTED_MODE", &tmp_bool);
1.177     cvs      1987:          if (tmp_bool)
                   1988:            HTCacheMode_setDisconnected (HT_DISCONNECT_NORMAL);
                   1989:          else
                   1990:            HTCacheMode_setDisconnected (HT_DISCONNECT_NONE);
1.211     cvs      1991:          if (HTCacheInit (cache_dir, cache_size))
1.130     cvs      1992:            {
1.188     cvs      1993:              if (set_cachelock (cache_lockfile) == -1)
                   1994:                /* couldn't open the .lock file, so, we close the cache to
                   1995:                   be in the safe side */
                   1996:                {
                   1997:                  HTCacheTerminate ();
                   1998:                  HTCacheMode_setEnabled (FALSE);
                   1999: #ifdef DEBUG_LIBWWW
                   2000:                  fprintf (stderr, "couldnt set the cache lock\n");
                   2001: #endif /* DEBUG_LIBWWW */
                   2002:                }
1.130     cvs      2003: #ifdef DEBUG_LIBWWW
1.188     cvs      2004:              else
                   2005:                fprintf (stderr, "created the cache lock\n");
1.130     cvs      2006: #endif /* DEBUG_LIBWWW */
                   2007:            }
1.188     cvs      2008:          else
                   2009:            {
1.130     cvs      2010: #ifdef DEBUG_LIBWWW
1.188     cvs      2011:              fprintf (stderr, "couldn't create the cache\n");
                   2012: #endif /* DEBUG_LIBWWW */            
                   2013:              HTCacheTerminate ();
                   2014:            }
1.130     cvs      2015:        }
                   2016:       else 
                   2017:        {
1.185     cvs      2018:          HTCacheMode_setEnabled (FALSE);
1.130     cvs      2019: #ifdef DEBUG_LIBWWW
                   2020:          fprintf (stderr, "lock detected, starting libwww without a cache/n");
                   2021: #endif /* DEBUG_LIBWWW */
                   2022:        }
                   2023:       TtaFreeMemory (cache_lockfile);
1.116     cvs      2024:     }
                   2025:   else
1.185     cvs      2026:     {
                   2027:       HTCacheMode_setEnabled (FALSE);
                   2028:     }
1.116     cvs      2029:   if (cache_dir)
1.211     cvs      2030:     HT_FREE (cache_dir);
1.189     cvs      2031:   if (real_dir)
                   2032:     TtaFreeMemory (real_dir);
1.185     cvs      2033:   /* warn the user if the cache isn't active */
                   2034:   if (cache_enabled && !HTCacheMode_enabled ())
1.214     cvs      2035:       InitInfo (TEXT("Cache"), TtaGetMessage (AMAYA, AM_CANT_CREATE_CACHE));
1.116     cvs      2036: #endif /* AMAYA_WWW_CACHE */
1.15      cvs      2037: }
                   2038: 
                   2039: /*----------------------------------------------------------------------
1.97      cvs      2040:   ProxyInit
                   2041:   Reads any proxies settings which may be declared as environmental
                   2042:   variables or in the thot.ini file. The former overrides the latter.
                   2043:   ----------------------------------------------------------------------*/
1.118     cvs      2044: #ifdef __STDC__
1.97      cvs      2045: static void ProxyInit (void)
1.118     cvs      2046: #else
                   2047: static void ProxyInit ()
                   2048: #endif /* __STDC__ */
1.97      cvs      2049: {
1.233     cvs      2050:   CHAR_T*   strptr;
1.223     cvs      2051:   char*     name;
                   2052:   ThotBool  proxy_is_onlyproxy;
1.228     cvs      2053:   char*     tmp = NULL;
1.223     cvs      2054:   char*     strptrA;
1.97      cvs      2055: 
                   2056:   /* get the proxy settings from the thot.ini file */
1.203     cvs      2057:   strptr = TtaGetEnvString ("HTTP_PROXY");
1.228     cvs      2058:   if (strptr && *strptr)
                   2059:     {
1.234     cvs      2060:       tmp = (char*) TtaGetMemory (ustrlen (strptr) + 1);
                   2061:       wc2iso_strcpy (tmp, strptr);
1.223     cvs      2062: 
1.228     cvs      2063:       /* does the proxy env string has an "http://" prefix? */
1.234     cvs      2064:       if (!ustrncasecmp (strptr, TEXT("http://"), 7)) 
1.223     cvs      2065:         HTProxy_add ("http", tmp);
1.228     cvs      2066:       else 
                   2067:        {
                   2068:          strptrA = (char*) TtaGetMemory (ustrlen (strptr) + 9);
                   2069:          strcpy (strptrA, "http://");
                   2070:          strcat (strptrA, tmp);
                   2071:          HTProxy_add ("http", strptrA);
                   2072:          TtaFreeMemory (strptrA);
                   2073:        }
                   2074:       TtaFreeMemory (tmp);
                   2075:     }
1.216     cvs      2076: 
1.97      cvs      2077:   /* get the no_proxy settings from the thot.ini file */
1.212     cvs      2078:   strptr = TtaGetEnvString ("PROXYDOMAIN");
1.228     cvs      2079:   if (strptr && *strptr) 
                   2080:     {
1.234     cvs      2081:       strptrA = (char*) TtaGetMemory (ustrlen (strptr) + 1);
                   2082:       wc2iso_strcpy (strptrA, strptr);
1.228     cvs      2083:       /* as HTNextField changes the ptr we pass as an argument, we'll
                   2084:         work with another variable, so that we can free the strptrA
                   2085:         block later on */
                   2086:       tmp = strptrA;
                   2087:       while ((name = HTNextField (&tmp)) != NULL) 
                   2088:        {
                   2089:          char* portstr = strchr (name, ':');
                   2090:          unsigned port=0;
                   2091:          if (portstr) { 
                   2092:            *portstr++ = EOS;
                   2093:            if (*portstr) 
                   2094:              port = (unsigned) atoi (portstr);
                   2095:          }
                   2096:          /* Register it for all access methods */
                   2097:          HTNoProxy_add (name, NULL, port);
                   2098:        }
                   2099:       TtaFreeMemory (strptrA);
                   2100:     }
                   2101:   
1.212     cvs      2102:   /* how should we interpret the proxy domain list? */
                   2103:   TtaGetEnvBoolean ("PROXYDOMAIN_IS_ONLYPROXY", &proxy_is_onlyproxy);
                   2104:   HTProxy_setNoProxyIsOnlyProxy (proxy_is_onlyproxy);
                   2105: 
                   2106:   /* use libwww's routine to get all proxy settings from the environment */
1.97      cvs      2107:    HTProxy_getEnvVar ();
                   2108: }
                   2109: 
1.207     cvs      2110: /*----------------------------------------------------------------------
                   2111:   SafePut_init
                   2112:   Sets up the domains which are authorized to make a redirect on 
                   2113:   a PUT.
                   2114:   ----------------------------------------------------------------------*/
                   2115: #ifdef __STDC__
                   2116: static void SafePut_init (void)
                   2117: #else
                   2118: static void SafePut_init ()
                   2119: #endif /* __STDC__ */
                   2120: {
1.233     cvs      2121:   CHAR_T* strptr;
                   2122:   CHAR_T* str = NULL;
1.226     cvs      2123:   char*     ptr, *strptrA, *ptr2;
1.223     cvs      2124:   char*     domain;
1.207     cvs      2125: 
                   2126:   /* get the proxy settings from the thot.ini file */
                   2127:   strptr = TtaGetEnvString ("SAFE_PUT_REDIRECT");
                   2128:   if (strptr && *strptr)
                   2129:     {
                   2130:       /* Get copy we can mutilate */
1.234     cvs      2131:       strptrA = (char*) TtaGetMemory (ustrlen (strptr) + 1);
                   2132:       wc2iso_strcpy (strptrA, strptr);
1.226     cvs      2133:       ptr2 = strptrA;
1.207     cvs      2134:       /* convert to lowercase */
                   2135:       ptr = strptrA;
                   2136:       while (*ptr) 
                   2137:        {
                   2138:          *ptr = tolower (*ptr);
                   2139:          ptr++;
                   2140:        }
                   2141:       
                   2142:       /* create the list container */
                   2143:       safeput_list = HTList_new ();   
                   2144:       /* store the domain list */
1.227     cvs      2145: 
                   2146:       ptr = strptrA;
1.207     cvs      2147:       while ((domain = HTNextField (&strptrA)) != NULL)
                   2148:          HTList_addObject (safeput_list, TtaStrdup (domain)); 
                   2149: 
                   2150:       TtaFreeMemory (str);
1.226     cvs      2151:       TtaFreeMemory (ptr2);
1.207     cvs      2152:     }
                   2153: }
                   2154: 
                   2155: /*----------------------------------------------------------------------
                   2156:   SafePut_delete
                   2157:   Deletes the safeput_list variable
                   2158:   ----------------------------------------------------------------------*/
                   2159: static void SafePut_delete (void)
                   2160: {
                   2161:   HTList *cur = safeput_list;
                   2162:   char *domain;
                   2163: 
                   2164:   if (!safeput_list) 
                   2165:     return;
                   2166: 
                   2167:   while ((domain = (char *) HTList_nextObject (cur))) 
                   2168:       TtaFreeMemory (domain);
                   2169:    HTList_delete (safeput_list);
                   2170:    safeput_list = NULL;
                   2171: }
                   2172: 
                   2173: /*----------------------------------------------------------------------
                   2174:   SafePut_query
                   2175:   returns true if the domain to which belongs the URL accepts an automatic
                   2176:   PUT redirect.
                   2177:   ----------------------------------------------------------------------*/
1.235     cvs      2178: static ThotBool SafePut_query (CHAR_T* url)
1.207     cvs      2179: {
1.235     cvs      2180:   HTList*  cur;
                   2181:   char*    me;
1.207     cvs      2182:   ThotBool found;
1.235     cvs      2183:   CHAR_T   tmp[MAX_LENGTH];
1.207     cvs      2184: 
                   2185:   /* extract the domain path of the url and normalize it */
                   2186:   /* domain = url; */
                   2187:   cur = safeput_list;
                   2188:   found = FALSE;
1.235     cvs      2189: 
                   2190:   while ((me = (char*) HTList_nextObject (cur))) {
                   2191:         iso2wc_strcpy (tmp, me);
                   2192:         if (ustrstr (url, tmp)) {
                   2193:            found = TRUE;
                   2194:            break;
                   2195:                } 
                   2196:   } 
1.207     cvs      2197: 
                   2198:   return (found);
                   2199: }
1.97      cvs      2200: 
                   2201: /*----------------------------------------------------------------------
1.17      cvs      2202:   AHTProfile_newAmaya
                   2203:   creates the Amaya client profile for libwww.
1.15      cvs      2204:   ----------------------------------------------------------------------*/
                   2205: #ifdef __STDC__
1.234     cvs      2206: static void         AHTProfile_newAmaya (CHAR_T* AppName, CHAR_T* AppVersion)
1.15      cvs      2207: #else  /* __STDC__ */
                   2208: static void         AHTProfile_newAmaya (AppName, AppVersion)
1.223     cvs      2209: char* AppName;
                   2210: char* AppVersion;
1.15      cvs      2211: #endif /* __STDC__ */
1.4       cvs      2212: {
1.233     cvs      2213:    CHAR_T* strptr;
1.234     cvs      2214: #  ifdef _I18N_
                   2215:    unsigned char mbAppName[MAX_LENGTH], mbAppVersion[MAX_LENGTH];
                   2216: #  else  /* !_I18N_ */
                   2217:    char* mbAppName    = AppName;
                   2218:    char* mbAppVersion = AppVersion;
                   2219: #  endif /* !_I18N_ */
1.158     cvs      2220: 
1.4       cvs      2221:    /* If the Library is not already initialized then do it */
1.234     cvs      2222:    if (!HTLib_isInitialized ()) {
                   2223: #     ifdef _I18N_
                   2224:       /* Here we suppose that libwww works with multibyte character string (MBCS).
                   2225:          AppName and AppVersion are wide character strings (WCS). The following 
                   2226:                 code transform each of AppName and AppVersion (WCSs) int mbAppName and
                   2227:          mbAppName (MBCSs).
                   2228:          If the libwww will support WCSs, than you have to remove the code related 
                   2229:          to _I18N_ (rounded by #ifdef _I18N_ #endif) and pass to HTLibInit
                   2230:          AppName instead of mbAppName and AppVersion instead of mbAppVersion */
                   2231:       wcstombs (mbAppName, AppName, MAX_LENGTH);
                   2232:       wcstombs (mbAppVersion, AppVersion, MAX_LENGTH);
                   2233: #     endif /* _I18N_ */
                   2234:       HTLibInit (mbAppName, mbAppVersion);
                   2235:    } 
1.4       cvs      2236: 
                   2237:    if (!converters)
                   2238:       converters = HTList_new ();
1.27      cvs      2239:    if (!acceptTypes)
                   2240:       acceptTypes = HTList_new ();
1.193     cvs      2241:    if (!acceptLanguages)
                   2242:       acceptLanguages = HTList_new ();
1.151     cvs      2243:    if (!transfer_encodings)
                   2244:       transfer_encodings = HTList_new ();
                   2245:    if (!content_encodings)
                   2246:       content_encodings = HTList_new ();
1.4       cvs      2247: 
1.169     cvs      2248:    /* inhibits libwww's automatic file_suffix_binding */
                   2249:    HTFile_doFileSuffixBinding (FALSE);
                   2250: 
1.4       cvs      2251:    /* Register the default set of transport protocols */
                   2252:    HTTransportInit ();
                   2253: 
                   2254:    /* Register the default set of application protocol modules */
                   2255:    AHTProtocolInit ();
                   2256: 
1.116     cvs      2257:    /* Register the default set of messages and dialog functions */
                   2258:    AHTAlertInit ();
                   2259:    HTAlert_setInteractive (YES);
                   2260: 
                   2261: #ifdef AMAYA_WWW_CACHE
                   2262:    /* Enable the persistent cache  */
                   2263:    CacheInit ();
                   2264: #else
                   2265:    HTCacheMode_setEnabled (NO);
                   2266: #endif /* AMAYA_WWW_CACHE */
1.4       cvs      2267: 
                   2268:    /* Register the default set of BEFORE and AFTER filters */
                   2269:    AHTNetInit ();
                   2270: 
                   2271:    /* Set up the default set of Authentication schemes */
1.223     cvs      2272:    HTAA_newModule ("basic", HTBasic_generate, HTBasic_parse, NULL, HTBasic_delete);
1.158     cvs      2273:    /* activate MDA by defaul */
1.203     cvs      2274:    strptr = TtaGetEnvString ("ENABLE_MDA");
1.234     cvs      2275:    if (!strptr || (strptr && *strptr && ustrcasecmp (strptr, TEXT("no"))))
1.223     cvs      2276:      HTAA_newModule ("digest", HTDigest_generate, HTDigest_parse, HTDigest_updateInfo, HTDigest_delete);
1.4       cvs      2277: 
1.97      cvs      2278:    /* Get any proxy settings */
                   2279:    ProxyInit ();
1.4       cvs      2280: 
1.207     cvs      2281:    /* Set up the domains where we accept a redirect on PUT */
                   2282:    SafePut_init ();
                   2283: 
1.4       cvs      2284:    /* Register the default set of converters */
                   2285:    AHTConverterInit (converters);
1.151     cvs      2286:    HTFormat_setConversion (converters);
1.27      cvs      2287:    AHTAcceptTypesInit (acceptTypes);
1.193     cvs      2288:    AHTAcceptLanguagesInit (acceptLanguages);
1.4       cvs      2289: 
                   2290:    /* Register the default set of transfer encoders and decoders */
1.151     cvs      2291:    HTTransferEncoderInit (transfer_encodings);
1.190     cvs      2292:    HTFormat_setTransferCoding (transfer_encodings);
                   2293:    /* Register the default set of content encoders and decoders */
                   2294:    HTContentEncoderInit (content_encodings);
                   2295:    /* ignore all other encoding formats (or libwww will send them 
1.181     cvs      2296:       thru a blackhole otherwise */
                   2297:    HTCoding_add (content_encodings, "*", NULL, HTIdentityCoding, 1.0);
1.151     cvs      2298:    if (HTList_count(content_encodings) > 0)
                   2299:      HTFormat_setContentCoding(content_encodings);
                   2300:    else 
                   2301:      {
                   2302:        HTList_delete(content_encodings);
                   2303:        content_encodings = NULL;
                   2304:      }
1.4       cvs      2305: 
                   2306:    /* Register the default set of MIME header parsers */
1.74      cvs      2307:    HTMIMEInit ();   /* must be called again for language selector */
1.4       cvs      2308: 
                   2309:    /* Register the default set of Icons for directory listings */
1.27      cvs      2310:    /*HTIconInit(NULL); *//* experimental */
1.4       cvs      2311: }
                   2312: 
1.5       cvs      2313: /*----------------------------------------------------------------------
1.17      cvs      2314:   AHTProfile_delete
                   2315:   deletes the Amaya client profile.
1.5       cvs      2316:   ----------------------------------------------------------------------*/
1.4       cvs      2317: #ifdef __STDC__
                   2318: static void         AHTProfile_delete (void)
                   2319: #else
                   2320: static void         AHTProfile_delete ()
1.7       cvs      2321: #endif                         /* __STDC__ */
1.4       cvs      2322: {
1.22      cvs      2323:  
                   2324:   /* free the Amaya global context */
1.151     cvs      2325: 
                   2326:   /* Clean up all the registred converters */
                   2327:   HTFormat_deleteAll ();
                   2328:   if (acceptTypes)
1.74      cvs      2329:     HTConversion_deleteAll (acceptTypes);
1.193     cvs      2330:   if (acceptLanguages)
                   2331:     HTLanguage_deleteAll (acceptLanguages);
1.151     cvs      2332: 
1.209     cvs      2333:   StopAllRequests (1);
1.74      cvs      2334:   HTList_delete (Amaya->docid_status);
                   2335:   HTList_delete (Amaya->reqlist);
                   2336:   TtaFreeMemory (Amaya);
1.61      cvs      2337: 
1.120     cvs      2338: #ifdef _WINDOWS
1.209     cvs      2339:   if (HTLib_isInitialized ())      
1.151     cvs      2340:     HTEventTerminate ();
1.198     cvs      2341: #endif /* _WINDOWS; */         
1.74      cvs      2342:     
1.151     cvs      2343:   /* Clean up the persistent cache (if any) */
1.116     cvs      2344: #ifdef AMAYA_WWW_CACHE
1.151     cvs      2345:   clear_cachelock ();
                   2346:   HTCacheTerminate ();
1.116     cvs      2347: #endif /* AMAYA_WWW_CACHE */
1.217     cvs      2348:   
1.218     cvs      2349:   /* call remove functions that are not called automatically by libwww */
                   2350:   HTNetCall_deleteBeforeAll (HTNet_before ());
                   2351:   HTNetCall_deleteAfterAll (HTNet_after ());
                   2352:   HTAA_deleteAllModules ();
                   2353:   HTAlertCall_deleteAll (HTAlert_global () );
                   2354:   HTAlert_setGlobal ((HTList *) NULL);
1.249     kahan    2355:   HTEventList_unregisterAll ();
1.218     cvs      2356:   /* these two functions are broken, so we can't call them right now 
                   2357:   HTHeader_deleteAll ();
                   2358:   HTTimer_deleteAll ();
                   2359:   */
                   2360:   HTTransport_deleteAll ();
1.217     cvs      2361:   /* remove bindings between suffixes, media types*/
                   2362:   HTBind_deleteAll ();
1.151     cvs      2363:   /* Terminate libwww */
                   2364:   HTLibTerminate ();
1.4       cvs      2365: }
                   2366: 
1.5       cvs      2367: /*----------------------------------------------------------------------
1.116     cvs      2368:   AmayacontextInit
                   2369:   initializes an internal Amaya context for our libwww interface 
                   2370:   ----------------------------------------------------------------------*/
                   2371: #ifdef __STDC__
                   2372: static void                AmayaContextInit ()
                   2373: #else
                   2374: static void                AmayaContextInit ()
                   2375: #endif
                   2376: 
                   2377: {
1.140     cvs      2378:   AmayaAlive_flag = TRUE;
1.116     cvs      2379:   /* Initialization of the global context */
                   2380:   Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
                   2381:   Amaya->reqlist = HTList_new ();
                   2382:   Amaya->docid_status = HTList_new ();
                   2383:   Amaya->open_requests = 0;
                   2384: }
                   2385: 
                   2386: /*----------------------------------------------------------------------
1.17      cvs      2387:   QueryInit
                   2388:   initializes the libwww interface 
1.5       cvs      2389:   ----------------------------------------------------------------------*/
1.4       cvs      2390: #ifdef __STDC__
                   2391: void                QueryInit ()
                   2392: #else
                   2393: void                QueryInit ()
                   2394: #endif
                   2395: {
1.233     cvs      2396:   CHAR_T* strptr;
1.151     cvs      2397:   int tmp_i;
                   2398:   long tmp_l;
1.4       cvs      2399: 
1.116     cvs      2400:    AmayaContextInit ();
1.4       cvs      2401:    AHTProfile_newAmaya (HTAppName, HTAppVersion);
1.140     cvs      2402:    CanDoStop_set (TRUE);
1.214     cvs      2403:    UserAborted_flag = FALSE;
1.4       cvs      2404: 
1.116     cvs      2405: #ifdef _WINDOWS
1.125     cvs      2406:    HTEventInit ();
1.116     cvs      2407: #endif /* _WINDOWS */
1.72      cvs      2408: 
1.123     cvs      2409: #ifndef _WINDOWS
1.116     cvs      2410:    HTEvent_setRegisterCallback ((void *) AHTEvent_register);
                   2411:    HTEvent_setUnregisterCallback ((void *) AHTEvent_unregister);
1.202     cvs      2412: #ifndef _GTK
1.120     cvs      2413:    HTTimer_registerSetTimerCallback ((void *) AMAYA_SetTimer);
                   2414:    HTTimer_registerDeleteTimerCallback ((void *) AMAYA_DeleteTimer);
1.202     cvs      2415: #endif /* _GTK */
1.116     cvs      2416: #endif /* !_WINDOWS */
1.190     cvs      2417: 
1.213     cvs      2418: #ifdef HTDEBUG
1.247     kahan    2419:    /* an undocumented option for being able to generate an HTTP protocol
                   2420:       trace.  The flag can take values from 1-10, which are interpreted as
                   2421:       different kinds of debug traces. Value 99 means a complete trace.
                   2422:       0 or other values means No Debug.1 No Debug (default), up to Full debug, respectively. */
1.238     cvs      2423:    if (TtaGetEnvInt ("ENABLE_LIBWWW_DEBUG", &tmp_i))
1.247     kahan    2424:      {
                   2425:        switch (tmp_i)
                   2426:         {
                   2427:         case 1:
                   2428:           WWW_TraceFlag = SHOW_URI_TRACE;
                   2429:           break;
                   2430:         case 2:
                   2431:           WWW_TraceFlag = SHOW_BIND_TRACE;
                   2432:           break;
                   2433:         case 3:
                   2434:           WWW_TraceFlag = SHOW_THREAD_TRACE;
                   2435:           break;
                   2436:         case 4:
                   2437:           WWW_TraceFlag = SHOW_STREAM_TRACE;
                   2438:           break;
                   2439:         case 5:
                   2440:           WWW_TraceFlag = SHOW_PROTOCOL_TRACE;
                   2441:           break;
                   2442:         case 6:
                   2443:           WWW_TraceFlag = SHOW_MEM_TRACE;
                   2444:           break;
                   2445:         case 7:
                   2446:           WWW_TraceFlag = SHOW_URI_TRACE;
                   2447:           break;
                   2448:         case 8:
                   2449:           WWW_TraceFlag = SHOW_AUTH_TRACE;
                   2450:           break;
                   2451:         case 9:
                   2452:           WWW_TraceFlag = SHOW_ANCHOR_TRACE;
                   2453:           break;
                   2454:         case 10 :
                   2455:           WWW_TraceFlag = SHOW_CORE_TRACE; 
                   2456:           break;
1.251     kahan    2457:         case 11 :
                   2458:           WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_ANCHOR_TRACE | SHOW_AUTH_TRACE | SHOW_URI_TRACE | SHOW_THREAD_TRACE | SHOW_STREAM_TRACE;
                   2459:           break;
1.247     kahan    2460:         case 99 :
                   2461:           WWW_TraceFlag = SHOW_ALL_TRACE;
                   2462:           break;
                   2463:         default:
                   2464:           WWW_TraceFlag = 0;
                   2465:           break;
                   2466:         }
1.248     kahan    2467:        if (WWW_TraceFlag)
                   2468:         {
                   2469:           /* Trace activation (for debugging) */
                   2470:           CHAR_T *s, *tmp;
                   2471:           s = TtaGetEnvString ("APP_TMPDIR");
                   2472:           tmp = TtaGetMemory (ustrlen (s) + sizeof (TEXT("/libwww.log")) + 1);
                   2473:           ustrcpy (tmp, s);
                   2474:           ustrcat (tmp, TEXT("/libwww.log"));
                   2475:           trace_fp = ufopen (tmp, TEXT("ab"));
                   2476:           TtaFreeMemory (tmp);
                   2477:           if (trace_fp)
                   2478:             HTTrace_setCallback(LineTrace);
                   2479:         }
1.247     kahan    2480:      }
1.208     cvs      2481:    else
                   2482:      WWW_TraceFlag = 0;
1.213     cvs      2483: #endif /* HTDEBUG */
1.208     cvs      2484: 
1.74      cvs      2485: #ifdef DEBUG_LIBWWW
1.116     cvs      2486:   /* forwards error messages to our own function */
                   2487:    WWW_TraceFlag = THD_TRACE;
1.138     cvs      2488:    HTTrace_setCallback(LineTrace);
1.248     kahan    2489: 
1.148     cvs      2490:    /***
1.138     cvs      2491:     WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | SHOW_PROTOCOL_TRACE;
                   2492:     ***/
1.152     cvs      2493: #endif
1.247     kahan    2494: 
                   2495:    TtaGetEnvBoolean ("ENABLE_FTP", &FTPURL_flag);
1.152     cvs      2496: 
1.148     cvs      2497:    /* Setting up different network parameters */
1.151     cvs      2498: 
1.148     cvs      2499:    /* Maximum number of simultaneous open sockets */
1.203     cvs      2500:    strptr = TtaGetEnvString ("MAX_SOCKET");
1.151     cvs      2501:    if (strptr && *strptr) 
1.233     cvs      2502:      tmp_i = wctoi (strptr);
1.151     cvs      2503:    else
                   2504:      tmp_i = DEFAULT_MAX_SOCKET;
                   2505:    HTNet_setMaxSocket (tmp_i);
                   2506: 
1.148     cvs      2507:    /* different network services timeouts */
1.151     cvs      2508:    /* dns timeout */
1.203     cvs      2509:    strptr = TtaGetEnvString ("DNS_TIMEOUT");
1.151     cvs      2510:    if (strptr && *strptr) 
1.233     cvs      2511:      tmp_i = wctoi (strptr);
1.151     cvs      2512:    else
                   2513:      tmp_i = DEFAULT_DNS_TIMEOUT;
                   2514:    HTDNS_setTimeout (tmp_i);
                   2515: 
                   2516:    /* persistent connections timeout */
1.203     cvs      2517:    strptr = TtaGetEnvString ("PERSIST_CX_TIMEOUT");
1.151     cvs      2518:    if (strptr && *strptr) 
1.242     cvs      2519:      tmp_l = uatol (strptr); 
1.151     cvs      2520:    else
                   2521:      tmp_l = DEFAULT_PERSIST_TIMEOUT;
                   2522:    HTHost_setPersistTimeout (tmp_l);
                   2523: 
1.148     cvs      2524:    /* default timeout in ms */
1.203     cvs      2525:    strptr = TtaGetEnvString ("NET_EVENT_TIMEOUT");
1.151     cvs      2526:    if (strptr && *strptr) 
1.233     cvs      2527:      tmp_i = wctoi (strptr);
1.151     cvs      2528:    else
                   2529:      tmp_i = DEFAULT_NET_EVENT_TIMEOUT;
                   2530:    HTHost_setEventTimeout (tmp_i);
                   2531: 
1.173     cvs      2532:    HTRequest_setMaxRetry (8);
1.4       cvs      2533: #ifdef CATCH_SIG
1.148     cvs      2534:    signal (SIGPIPE, SIG_IGN);
1.4       cvs      2535: #endif
1.15      cvs      2536: }
                   2537: 
                   2538: /*----------------------------------------------------------------------
1.17      cvs      2539:   LoopForStop
1.136     cvs      2540:   a copy of the Thop event loop so we can handle the stop button in Unix
                   2541:   and preemptive requests under Windows
1.15      cvs      2542:   ----------------------------------------------------------------------*/
                   2543: #ifdef __STDC__
                   2544: static int          LoopForStop (AHTReqContext * me)
                   2545: #else
                   2546: static int          LoopForStop (AHTReqContext * me)
                   2547: #endif
                   2548: {
1.136     cvs      2549: #ifdef _WINDOWS
                   2550:   MSG msg;
                   2551:   unsigned long libwww_msg;
                   2552:   HWND old_active_window, libwww_window;
                   2553:   int  status_req = HT_OK;
                   2554: 
                   2555:   old_active_window = GetActiveWindow ();
                   2556:   libwww_window = HTEventList_getWinHandle (&libwww_msg);
1.137     cvs      2557:  
1.149     cvs      2558:   while (me->reqStatus != HT_END && me->reqStatus != HT_ERR
1.253   ! cvs      2559:         && me->reqStatus != HT_ABORT && AmayaIsAlive () &&
        !          2560:         GetMessage (&msg, NULL, 0, 0))
        !          2561:     {
        !          2562:       if (msg.message != WM_QUIT)
        !          2563:        TtaHandleOneEvent (&msg);
        !          2564:       else
        !          2565:        break;      
        !          2566:     }
1.136     cvs      2567:   if (!AmayaIsAlive ())
                   2568:     /* Amaya was killed by one of the callback handlers */
                   2569:     exit (0);
                   2570: #else /* _WINDOWS */
1.25      cvs      2571:    extern ThotAppContext app_cont;
1.51      cvs      2572:    XEvent                ev;
                   2573:    XtInputMask           status;
1.17      cvs      2574:    int                 status_req = HT_OK;
1.15      cvs      2575: 
                   2576:    /* to test the async calls  */
1.17      cvs      2577:    /* Loop while waiting for new events, exists when the request is over */
1.15      cvs      2578:    while (me->reqStatus != HT_ABORT &&
                   2579:          me->reqStatus != HT_END &&
1.69      cvs      2580:          me->reqStatus != HT_ERR) {
1.116     cvs      2581:         if (!AmayaIsAlive ())
1.69      cvs      2582:            /* Amaya was killed by one of the callback handlers */
                   2583:            exit (0);
                   2584: 
                   2585:         status = XtAppPending (app_cont);
1.144     cvs      2586:         if (status & XtIMXEvent)
                   2587:           {
                   2588:             XtAppNextEvent (app_cont, &ev);
                   2589:             TtaHandleOneEvent (&ev);
                   2590:           } 
                   2591:         else if (status != 0) 
                   2592:           XtAppProcessEvent (app_cont, XtIMAll);
1.69      cvs      2593:    }
1.136     cvs      2594: #endif /* _WINDOWS */
1.69      cvs      2595:    switch (me->reqStatus) {
                   2596:          case HT_ERR:
                   2597:           case HT_ABORT:
1.130     cvs      2598:               status_req = NO;
1.15      cvs      2599:               break;
                   2600: 
1.69      cvs      2601:          case HT_END:
1.130     cvs      2602:               status_req = YES;
1.15      cvs      2603:               break;
                   2604: 
1.69      cvs      2605:          default:
1.15      cvs      2606:               break;
1.69      cvs      2607:    }
1.15      cvs      2608:    return (status_req);
1.4       cvs      2609: }
                   2610: 
1.5       cvs      2611: /*----------------------------------------------------------------------
1.15      cvs      2612:   QueryClose
1.21      cvs      2613:   closes all existing threads, frees all non-automatically deallocated
                   2614:   memory and then ends libwww.
1.5       cvs      2615:   ----------------------------------------------------------------------*/
1.116     cvs      2616: void QueryClose ()
1.4       cvs      2617: {
1.24      cvs      2618: 
1.140     cvs      2619:   AmayaAlive_flag = FALSE;
1.24      cvs      2620: 
1.116     cvs      2621:   /* remove all the handlers and callbacks that may output a message to
                   2622:      a non-existent Amaya window */
                   2623:   HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
                   2624:   HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
                   2625: #ifndef _WINDOWS
                   2626:   /** need to erase all existing timers too **/
                   2627:    HTTimer_registerSetTimerCallback (NULL);
                   2628:    HTTimer_registerDeleteTimerCallback (NULL);
                   2629: #endif /* !_WINDOWS */
                   2630:   HTHost_setActivateRequestCallback (NULL);
                   2631:   Thread_deleteAll ();
1.21      cvs      2632:  
1.116     cvs      2633:   HTProxy_deleteAll ();
                   2634:   HTNoProxy_deleteAll ();
1.207     cvs      2635:   SafePut_delete ();
1.116     cvs      2636:   HTGateway_deleteAll ();
                   2637:   AHTProfile_delete ();
1.248     kahan    2638: 
                   2639:   /* close the trace file (if it exists) */
                   2640:   if (trace_fp)
                   2641:     fclose (trace_fp);
                   2642: 
1.116     cvs      2643: }
                   2644: 
                   2645: /*----------------------------------------------------------------------
                   2646:   NextNameValue
                   2647:   ---------------------------------------------------------------------*/
                   2648: #ifdef __STDC__
                   2649: static char * NextNameValue (char ** pstr, char **name, char **value)
                   2650: #else
                   2651: static char * NextNameValue (pstr, name, value);
                   2652: char ** pstr;
                   2653: char **name;
                   2654: char **value;
                   2655: #endif /* __STDC__ */
                   2656: {
                   2657:   char * p = *pstr;
                   2658:   char * start = NULL;
                   2659:   if (!pstr || !*pstr) return NULL;
                   2660:   
                   2661:     if (!*p) {
                   2662:       *pstr = p;
                   2663:       *name = NULL;
                   2664:       *value = NULL;
                   2665:       return NULL;                                      /* No field */
                   2666:     }
                   2667:     
                   2668:     /* Now search for the next '&' and ';' delimitators */
                   2669:     start = p;
                   2670:     while (*p && *p != '&' && *p != ';') p++;
                   2671:     if (*p) 
                   2672:       *p++ = '\0';
                   2673:     *pstr = p;
                   2674: 
                   2675:     /* Search for the name and value */
                   2676:     *name = start;
                   2677:     p = start;
                   2678:     
                   2679:     while(*p && *p != '=') 
                   2680:       p++;
                   2681:     if (*p) 
                   2682:       *p++ = '\0';
                   2683:     *value = p;
                   2684: 
                   2685:     return start;
                   2686: }
                   2687: 
                   2688: /*----------------------------------------------------------------------
                   2689:   PrepareFormdata
                   2690:   ---------------------------------------------------------------------*/
                   2691: #ifdef __STDC__
1.236     cvs      2692: static   HTAssocList * PrepareFormdata (CHAR_T* string)
1.116     cvs      2693: #else
1.236     cvs      2694: static   HTAssocList * PrepareFormdata (string)
                   2695: CHAR_T*  string;
1.116     cvs      2696: #endif /* __STDC__ */
                   2697: {
1.198     cvs      2698:   char*        tmp_string, *tmp_string_ptr;
                   2699:   char*        name;
                   2700:   char*        value;
                   2701:   HTAssocList* formdata;
1.116     cvs      2702: 
                   2703:   if (!string)
                   2704:     return NULL;
                   2705: 
                   2706:   /* store the ptr in another variable, as the original address will
                   2707:      change
                   2708:      */
1.236     cvs      2709:   
                   2710:   tmp_string_ptr = TtaGetMemory (ustrlen (string) + 1);
                   2711:   tmp_string = tmp_string_ptr;
                   2712:   wc2iso_strcpy (tmp_string_ptr, string);
1.116     cvs      2713:   formdata = HTAssocList_new();
                   2714:   
                   2715:   while (*tmp_string)
                   2716:     {
                   2717:       NextNameValue (&tmp_string, &name, &value);
                   2718:       HTAssocList_addObject(formdata,
                   2719:                            name, value);
                   2720:     }
                   2721: 
                   2722:   TtaFreeMemory (tmp_string_ptr);
                   2723:   return formdata;
1.4       cvs      2724: }
                   2725: 
1.236     cvs      2726: /*----------------------------------------------------------------------
                   2727:   ---------------------------------------------------------------------*/
1.216     cvs      2728: #ifdef __STDC__
                   2729: void AHTRequest_setCustomAcceptHeader (HTRequest *request, char *value)
                   2730: #else
                   2731: void AHTRequest_setCustomAcceptHeader (request, value)
                   2732: HTRequest *request;
                   2733: char *value;
                   2734: #endif /* __STDC__ */
                   2735: {                              
                   2736:   HTRqHd rqhd = HTRequest_rqHd (request);
                   2737:   rqhd = rqhd & (~HT_C_ACCEPT_TYPE);
                   2738:   HTRequest_setRqHd (request, rqhd);
1.221     cvs      2739:   HTRequest_addExtraHeader (request, "Accept", value);
1.216     cvs      2740: }
                   2741: 
1.228     cvs      2742: /*----------------------------------------------------------------------
1.99      cvs      2743:   InvokeGetObjectWWW_callback
                   2744:   A simple function to invoke a callback function whenever there's an error
                   2745:   in GetObjectWWW
                   2746:   ---------------------------------------------------------------------*/
                   2747: 
1.116     cvs      2748: #ifdef __STDC__
1.198     cvs      2749: void      InvokeGetObjectWWW_callback (int docid, STRING urlName, STRING outputfile, TTcbf *terminate_cbf, void *context_tcbf, int status)
1.99      cvs      2750: #else
1.111     cvs      2751: void      InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf, context_tcbf, status)
1.99      cvs      2752: int docid;
1.198     cvs      2753: STRING urlName;
                   2754: STRING outputfile;
1.99      cvs      2755: TTcbf *terminate_cbf;
                   2756: void *context_tcbf;
1.116     cvs      2757: #endif /* __STDC__ */
1.99      cvs      2758: {
                   2759:   if (!terminate_cbf)
                   2760:     return;
                   2761:   
1.116     cvs      2762:   (*terminate_cbf) (docid, status, urlName, outputfile,
1.99      cvs      2763:                    NULL, context_tcbf);  
                   2764: }
                   2765: 
                   2766: 
                   2767: 
1.5       cvs      2768: /*----------------------------------------------------------------------
1.15      cvs      2769:    GetObjectWWW
1.17      cvs      2770:    this function requests a resource designated by a URLname into a
                   2771:    temporary filename. The download can come from a simple GET operation,
                   2772:    or can come from POSTING/GETTING a form. In the latter
                   2773:    case, the function receives a query string to send to the server.
                   2774: 
1.5       cvs      2775:    4  file retrieval modes are proposed:                              
                   2776:    AMAYA_SYNC : blocking mode                            
                   2777:    AMAYA_ISYNC : incremental, blocking mode              
                   2778:    AMAYA_ASYNC : non-blocking mode                       
                   2779:    AMAYA_IASYNC : incremental, non-blocking mode         
                   2780:    
                   2781:    In the incremental mode, each time a package arrives, it will be   
                   2782:    stored in the temporary file. In addition, if an                   
                   2783:    incremental_callback function is defined, this function will be    
                   2784:    called and handled a copy of the newly received data package.      
                   2785:    Finally, if a terminate_callback function is defined, it will be   
                   2786:    invoked when the request terminates. The caller of this function
1.4       cvs      2787:    can define two different contexts to be passed to the callback
                   2788:    functions.
                   2789: 
                   2790:    When the function is called with the SYNC mode, the function will
                   2791:    return only when the requested file has been loaded.
                   2792:    The ASYNC mode will immediately return after setting up the
                   2793:    call.
                   2794: 
                   2795:    Notes:
                   2796:    At the end of a succesful request, the urlName string contains the
                   2797:    name of the actually retrieved URL. As a URL can change over the time,
                   2798:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      2799:    caller verify the value of the urlName variable at the end of
1.4       cvs      2800:    a request.
                   2801: 
                   2802:    Inputs:
                   2803:    - docid  Document identifier for the set of objects being
                   2804:    retrieved.
                   2805:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   2806:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   2807:    - mode The retrieval mode.
                   2808:    - incremental_cbf 
                   2809:    - context_icbf
                   2810:    Callback and context for the incremental modes
                   2811:    - terminate_cbf 
                   2812:    - context_icbf
                   2813:    Callback and context for a terminate handler
1.17      cvs      2814:    -error_html if TRUE, then display any server error message as an
                   2815:    HTML document.
1.88      cvs      2816:    - content_type a string
                   2817:  
1.4       cvs      2818:    Outputs:
                   2819:    - urlName The URL that was retrieved
                   2820:    - outputfile The name of the temporary file which holds the
                   2821:    retrieved data. (Only in case of success)
1.88      cvs      2822:    - if content_type wasn't NULL, it will contain a copy of the parameter
                   2823:      sent in the HTTP answer
1.4       cvs      2824:    Returns:
                   2825:    HT_ERROR
                   2826:    HT_OK
1.5       cvs      2827:  
                   2828:   ----------------------------------------------------------------------*/
1.4       cvs      2829: #ifdef __STDC__
1.198     cvs      2830: int GetObjectWWW (int docid, STRING urlName, STRING formdata,
                   2831:                  STRING outputfile, int mode, TIcbf* incremental_cbf, 
1.116     cvs      2832:                  void* context_icbf, TTcbf* terminate_cbf, 
1.198     cvs      2833:                  void* context_tcbf, ThotBool error_html, STRING content_type)
1.4       cvs      2834: #else
1.127     cvs      2835: int GetObjectWWW (docid, urlName, formdata, outputfile, mode, 
1.116     cvs      2836:                  incremental_cbf, context_icbf, 
1.88      cvs      2837:                  terminate_cbf, context_tcbf, error_html, content_type)
1.73      cvs      2838: int           docid;
1.235     cvs      2839: CHAR_T*       urlName;
                   2840: CHAR_T*       formdata;
                   2841: CHAR_T*       outputfile;
1.73      cvs      2842: int           mode;
                   2843: TIcbf        *incremental_cbf;
                   2844: void         *context_icbf;
                   2845: TTcbf        *terminate_cbf;
                   2846: void         *context_tcbf;
1.191     cvs      2847: ThotBool      error_html;
1.235     cvs      2848: CHAR_T*       content_type;
1.4       cvs      2849: #endif
                   2850: {
                   2851:    AHTReqContext      *me;
1.235     cvs      2852:    CHAR_T*             ref;
                   2853:    CHAR_T*             esc_url;
                   2854:    char                urlRef[MAX_LENGTH];
1.107     cvs      2855:    int                 status, l;
1.114     cvs      2856:    int                 tempsubdir;
1.191     cvs      2857:    ThotBool            bool_tmp;
1.7       cvs      2858: 
1.116     cvs      2859:    if (urlName == NULL || docid == 0 || outputfile == NULL) 
                   2860:      {
                   2861:        /* no file to be loaded */
                   2862:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.69      cvs      2863:        
1.116     cvs      2864:        if (error_html)
1.69      cvs      2865:         /* so we can show the error message */
                   2866:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2867:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2868:                                    context_tcbf, HT_ERROR);
                   2869:        return HT_ERROR;
                   2870:      }
1.7       cvs      2871: 
1.159     cvs      2872:    /* if it's a 'docImage', we have already downloaded it */
1.198     cvs      2873:    if (!ustrncmp (TEXT("internal:"), urlName, 9)) 
1.159     cvs      2874:      {
1.198     cvs      2875:        ustrcpy (outputfile, urlName);
1.159     cvs      2876:        InvokeGetObjectWWW_callback (docid, urlName, outputfile,
                   2877:                                    terminate_cbf, context_tcbf, HT_OK);
                   2878:        return HT_OK;
                   2879:      }
                   2880: 
1.4       cvs      2881:    /* do we support this protocol? */
1.116     cvs      2882:    if (IsValidProtocol (urlName) == NO) 
                   2883:      {
                   2884:        /* return error */
                   2885:        outputfile[0] = EOS;    /* file could not be opened */
                   2886:        TtaSetStatus (docid, 1, 
                   2887:                     TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
                   2888:                     urlName);
                   2889: 
                   2890:        if (error_html)
1.69      cvs      2891:         /* so we can show the error message */
                   2892:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2893:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2894:                                    context_tcbf, HT_ERROR);
                   2895:        return HT_ERROR;
                   2896:      }
1.4       cvs      2897: 
1.114     cvs      2898:    /* we store CSS in subdir named 0; all the other files go to a subidr
                   2899:       named after their own docid */
                   2900:    
                   2901:    tempsubdir = (mode & AMAYA_LOAD_CSS) ? 0 : docid;
                   2902: 
1.116     cvs      2903:    /* create a tempfilename */
1.242     cvs      2904:    usprintf (outputfile, TEXT("%s%c%d%c%04dAM"), TempFileDirectory, WC_DIR_SEP, tempsubdir, WC_DIR_SEP, object_counter);
1.116     cvs      2905:    /* update the object_counter (used for the tempfilename) */
1.4       cvs      2906:    object_counter++;
1.116     cvs      2907:    
1.4       cvs      2908:    /* normalize the URL */
1.206     cvs      2909:    esc_url = EscapeURL (urlName);
                   2910:    if (esc_url) 
                   2911:      {
1.233     cvs      2912:        ref = AmayaParseUrl (esc_url, TEXT(""), AMAYA_PARSE_ALL);
1.206     cvs      2913:        TtaFreeMemory (esc_url);
                   2914:      }
                   2915:    else
                   2916:      ref = NULL;
                   2917: 
1.4       cvs      2918:    /* should we abort the request if we could not normalize the url? */
1.198     cvs      2919:    if (ref == NULL || ref[0] == EOS) {
1.69      cvs      2920:       /*error */
                   2921:       outputfile[0] = EOS;
                   2922:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.214     cvs      2923:       if (ref)
                   2924:        TtaFreeMemory (ref); 
1.69      cvs      2925:       if (error_html)
1.116     cvs      2926:        /* so we can show the error message */
                   2927:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99      cvs      2928:       InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.116     cvs      2929:                                   context_tcbf, HT_ERROR);
1.69      cvs      2930:       return HT_ERROR;
                   2931:    }
1.116     cvs      2932: 
1.4       cvs      2933:    /* verify if that file name existed */
1.9       cvs      2934:    if (TtaFileExist (outputfile))
1.77      cvs      2935:      TtaFileUnlink (outputfile);
1.116     cvs      2936:    
1.4       cvs      2937:    /* Initialize the request structure */
                   2938:    me = AHTReqContext_new (docid);
1.116     cvs      2939:    if (me == NULL) 
                   2940:      {
                   2941:        outputfile[0] = EOS;
                   2942:        /* need an error message here */
                   2943:        TtaFreeMemory (ref);
                   2944:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2945:                                   context_tcbf, HT_ERROR);
                   2946:        return HT_ERROR;
                   2947:      }
                   2948: 
1.4       cvs      2949:    /* Specific initializations for POST and GET */
1.222     cvs      2950:    if (mode & AMAYA_FORM_POST
1.228     cvs      2951:        || mode & AMAYA_FILE_POST)
1.116     cvs      2952:      {
                   2953:        me->method = METHOD_POST;
                   2954:        HTRequest_setMethod (me->request, METHOD_POST);
                   2955:      }
                   2956:    else 
                   2957:      {
                   2958:        me->method = METHOD_GET;
                   2959:        if (!HasKnownFileSuffix (ref))
1.216     cvs      2960:         {
                   2961:           /* try to adjust the Accept header in an netwise economical way */
                   2962:           if (mode & AMAYA_LOAD_IMAGE)
                   2963:             AHTRequest_setCustomAcceptHeader (me->request, IMAGE_ACCEPT_NEGOTIATION);
                   2964:           else if (mode & AMAYA_LOAD_CSS)
                   2965:             AHTRequest_setCustomAcceptHeader (me->request, "*/*;q=0.1,css/*");
1.244     kahan    2966: #ifdef ANNOTATIONS
                   2967:           else if (content_type && content_type[0] != WC_EOS)
                   2968:             /* use the custom sent content_type */
                   2969:             AHTRequest_setCustomAcceptHeader (me->request, content_type);
                   2970: #endif /* ANNOTATIONS */
1.216     cvs      2971:           /*
                   2972:           HTRequest_setConversion(me->request, acceptTypes, TRUE);
                   2973:           */
                   2974:         }
                   2975:           HTRequest_setLanguage (me->request, acceptLanguages, TRUE);
1.116     cvs      2976:      }
1.93      cvs      2977: 
1.116     cvs      2978:    /* Common initialization for all HTML methods */
1.4       cvs      2979:    me->mode = mode;
                   2980:    me->error_html = error_html;
                   2981:    me->incremental_cbf = incremental_cbf;
                   2982:    me->context_icbf = context_icbf;
                   2983:    me->terminate_cbf = terminate_cbf;
                   2984:    me->context_tcbf = context_tcbf;
1.64      cvs      2985: 
1.69      cvs      2986:    /* for the async. request modes, we need to have our
1.4       cvs      2987:       own copy of outputfile and urlname
1.116     cvs      2988:       */
1.4       cvs      2989: 
1.116     cvs      2990:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) 
                   2991:      {
1.198     cvs      2992:        l = ustrlen (outputfile);
1.116     cvs      2993:        if (l > MAX_LENGTH)
1.237     cvs      2994:         me->outputfile = TtaAllocString (l + 2);
1.116     cvs      2995:        else
1.237     cvs      2996:         me->outputfile = TtaAllocString (MAX_LENGTH + 2);
1.234     cvs      2997:        ustrcpy (me->outputfile, outputfile);
1.198     cvs      2998:        l = ustrlen (urlName);
1.116     cvs      2999:        if (l > MAX_LENGTH)
1.237     cvs      3000:         me->urlName = TtaAllocString (l + 2);
1.116     cvs      3001:        else
1.237     cvs      3002:         me->urlName = TtaAllocString (MAX_LENGTH + 2);
1.234     cvs      3003:        ustrcpy (me->urlName, urlName);
1.116     cvs      3004: #ifdef _WINDOWS
                   3005:      /* force windows ASYNC requests to always be non preemptive */
                   3006:      HTRequest_setPreemptive (me->request, NO);
                   3007: #endif /*_WINDOWS */
                   3008:      } /* AMAYA_ASYNC mode */ 
                   3009:    else 
                   3010: #ifdef _WINDOWS
                   3011:      {
1.233     cvs      3012:        me->outputfile = outputfile;
                   3013:        me->urlName = urlName;
1.116     cvs      3014:        /* force windows SYNC requests to always be non preemptive */
                   3015:        HTRequest_setPreemptive (me->request, YES);
                   3016:      }
                   3017: #else /* !_WINDOWS */
                   3018:      {
                   3019:        me->outputfile = outputfile;
                   3020:        me->urlName = urlName;
                   3021:      }
                   3022:    /***
1.136     cvs      3023:      In order to take into account the stop button, 
                   3024:      the requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1.57      cvs      3025:      we will loop until the document has been received or a stop signal
                   3026:      generated
1.77      cvs      3027:      ****/
1.116     cvs      3028:    HTRequest_setPreemptive (me->request, NO);
                   3029: #endif /* _WINDOWS */
1.61      cvs      3030: 
1.151     cvs      3031:    /*
                   3032:    ** Make sure that the first request is flushed immediately and not
                   3033:    ** buffered in the output buffer
                   3034:    */
                   3035:    if (mode & AMAYA_FLUSH_REQUEST)
                   3036:      HTRequest_setFlush(me->request, YES);
                   3037:    HTRequest_setFlush(me->request, YES);
                   3038: 
1.61      cvs      3039:    /* prepare the URLname that will be displayed in teh status bar */
                   3040:    ChopURL (me->status_urlName, me->urlName);
1.77      cvs      3041:    TtaSetStatus (me->docid, 1, 
                   3042:                 TtaGetMessage (AMAYA, AM_FETCHING),
1.233     cvs      3043:                 me->status_urlName);
1.4       cvs      3044: 
1.235     cvs      3045:    wc2iso_strcpy (urlRef, ref);
                   3046:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (urlRef);
1.45      cvs      3047:    TtaFreeMemory (ref);
1.177     cvs      3048:    
1.223     cvs      3049:    TtaGetEnvBoolean ("CACHE_DISCONNECTED_MODE", &bool_tmp);
1.177     cvs      3050:    if (!bool_tmp && (mode & AMAYA_NOCACHE))
1.116     cvs      3051:       HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
                   3052: 
                   3053:    /* prepare the query string and format for POST */
                   3054:    if (mode & AMAYA_FORM_POST)
1.114     cvs      3055:      {
1.116     cvs      3056:        HTAnchor_setFormat ((HTParentAnchor *) me->anchor, 
                   3057:                           HTAtom_for ("application/x-www-form-urlencoded"));
                   3058:        HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
                   3059:        HTRequest_setEntityAnchor (me->request, me->anchor);
                   3060:      } 
1.114     cvs      3061: 
1.127     cvs      3062:    /* create the formdata element for libwww */
1.236     cvs      3063:    if (formdata && ! (mode & AMAYA_FILE_POST))
                   3064:       me->formdata = PrepareFormdata (formdata);
1.127     cvs      3065: 
1.116     cvs      3066:    /* do the request */
                   3067:    if (mode & AMAYA_FORM_POST)
1.119     cvs      3068:      {
1.191     cvs      3069:        /* this call doesn't give back a ThotBool */
1.119     cvs      3070:        HTParentAnchor * posted = NULL;
                   3071: 
                   3072:        posted = HTPostFormAnchor (me->formdata, (HTAnchor *) me->anchor, 
                   3073:                                    me->request);
                   3074:        status = posted ? YES : NO; 
                   3075:      }
1.222     cvs      3076: #ifdef ANNOTATIONS
1.251     kahan    3077:    else if (mode & AMAYA_FILE_POST)
1.222     cvs      3078:      {
1.228     cvs      3079:        unsigned long filesize;
                   3080:        char *fileURL;
                   3081: 
                   3082:        /* @@@ a very ugly patch :)))
                   3083:        I'm copying here some of the functionality I use in the PUT
                   3084:        I need to put the common parts in another module */
1.252     kahan    3085:        AM_GetFileSize (formdata, &filesize);
1.228     cvs      3086:        filesize = filesize + strlen ("w3c_annotate=");
                   3087:        me->block_size = filesize;
                   3088: 
1.233     cvs      3089:        fileURL = HTParse (formdata, "file:/", PARSE_ALL);
1.228     cvs      3090:        me->source = HTAnchor_findAddress (fileURL);
                   3091:        HT_FREE (fileURL);
1.222     cvs      3092: 
1.228     cvs      3093:        /* hardcoded ... */
                   3094:        AHTRequest_setCustomAcceptHeader (me->request, "application/xml");
1.222     cvs      3095:        HTAnchor_setFormat (HTAnchor_parent (me->source),
                   3096:                           HTAtom_for ("application/xml"));
1.228     cvs      3097:        HTAnchor_setFormat (me->anchor,
                   3098:                           HTAtom_for ("application/xml"));
                   3099:        HTAnchor_setLength ((HTParentAnchor *) me->source, me->block_size);
                   3100:        /* @@ here I need to actually read the file and put it in document,
                   3101:          then, when I kill the request, I need to kill it */
                   3102:        {
                   3103:         FILE *fp;
                   3104:         int i;
                   3105:         char c;
                   3106: 
                   3107:         me->document = TtaGetMemory (me->block_size 
                   3108:                                      + strlen ("w3c_annotate=") + 1);
1.233     cvs      3109:         fp = fopen (formdata, "r");
1.244     kahan    3110:         i = 0; 
1.228     cvs      3111:         strcpy (me->document, "w3c_annotate=");
                   3112:         c = getc (fp);
                   3113:         i = strlen(me->document);
                   3114:         while (!feof (fp))
                   3115:           {
                   3116:             me->document[i++] = c;
                   3117:             c = getc (fp);
                   3118:           }
                   3119:         me->document[i] = '\0';
                   3120:         fclose (fp);
                   3121:        }
                   3122:        HTAnchor_setDocument ( (HTParentAnchor *) me->source,
                   3123:                              (void * ) me->document);
                   3124:        HTRequest_setEntityAnchor (me->request, HTAnchor_parent (me->source));
                   3125:  
                   3126:        status = HTPostAnchor (HTAnchor_parent (me->source), 
1.222     cvs      3127:                              (HTAnchor *) me->anchor, 
                   3128:                              me->request);
                   3129:      }
                   3130: #endif /* ANNOTATIONS */
1.127     cvs      3131:    else if (formdata)
                   3132:      status = HTGetFormAnchor(me->formdata, (HTAnchor *) me->anchor,
                   3133:                              me->request);
1.116     cvs      3134:    else
1.77      cvs      3135:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      3136: 
1.123     cvs      3137:    /* @@@ may need some special windows error msg here */
1.116     cvs      3138:    /* control the errors */
1.4       cvs      3139: 
1.130     cvs      3140:     if (status == NO)
1.116     cvs      3141:      /* the request invocation failed */
                   3142:      {
                   3143:        /* show an error message on the status bar */
                   3144:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   3145:        TtaSetStatus (docid, 1, 
                   3146:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                   3147:                     urlName);
                   3148:        if (me->reqStatus == HT_NEW)
                   3149:         /* manually invoke the last processing that usually gets done
                   3150:            in a succesful request */
                   3151:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
                   3152:                                      terminate_cbf, context_tcbf, HT_ERROR);
                   3153:        /* terminate_handler wasn't called */
1.136     cvs      3154:        AHTReqContext_delete (me);
1.116     cvs      3155:      }
                   3156:    else
1.136     cvs      3157:      /* end treatment for SYNC requests */
1.120     cvs      3158:      if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   3159:        {
1.136     cvs      3160:         /* wait here untilt the asynchronous request finishes */
1.130     cvs      3161:         status = LoopForStop (me);
1.136     cvs      3162:         /* if status returns HT_ERROR, should we invoke the callback? */
1.120     cvs      3163:         if (!HTRequest_kill (me->request))
                   3164:           AHTReqContext_delete (me);
                   3165:        }
1.130     cvs      3166: 
1.136     cvs      3167:     /* an interface problem!!! */
                   3168:     return (status == YES ? 0 : -1);
1.4       cvs      3169: }
                   3170: 
1.5       cvs      3171: /*----------------------------------------------------------------------
1.17      cvs      3172:    PutObjectWWW
                   3173:    frontend for uploading a resource to a URL. This function downloads
                   3174:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   3175:    finish the job.
                   3176: 
1.5       cvs      3177:    2 upload modes are proposed:                                       
                   3178:    AMAYA_SYNC : blocking mode                            
                   3179:    AMAYA_ASYNC : non-blocking mode                       
                   3180:    
1.4       cvs      3181:    When the function is called with the SYNC mode, the function will
                   3182:    return only when the file has been uploaded.
                   3183:    The ASYNC mode will immediately return after setting up the
                   3184:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   3185:    call back terminate_cbf, handling it the context defined in
                   3186:    context_tcbf.
                   3187: 
                   3188:    Notes:
                   3189:    At the end of a succesful request, the urlName string contains the
                   3190:    name of the actually uploaded URL. As a URL can change over the time,
                   3191:    (e.g., be redirected elsewhere), it is advised that the function
                   3192:    caller verifies the value of the urlName variable at the end of
                   3193:    a request.
                   3194: 
                   3195:    Inputs:
                   3196:    - docid  Document identifier for the set of objects being
                   3197:    retrieved.
                   3198:    - fileName A pointer to the local file to upload
                   3199:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   3200:    - mode The retrieval mode.
                   3201:    - terminate_cbf 
                   3202:    - context_icbf
                   3203:    Callback and context for a terminate handler
                   3204: 
                   3205:    Outputs:
                   3206:    - urlName The URL that was uploaded
                   3207: 
                   3208:    Returns:
                   3209:    HT_ERROR
                   3210:    HT_OK
1.5       cvs      3211:   ----------------------------------------------------------------------*/
1.4       cvs      3212: #ifdef __STDC__
1.198     cvs      3213: int                 PutObjectWWW (int docid, STRING fileName, STRING urlName, int mode, PicType contentType,
1.4       cvs      3214:                                  TTcbf * terminate_cbf, void *context_tcbf)
                   3215: #else
1.26      cvs      3216: int                 PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4       cvs      3217:                                  ,terminate_cbf, context_tcbf)
                   3218: int                 docid;
1.198     cvs      3219: STRING              urlName;
                   3220: STRING              fileName;
1.4       cvs      3221: int                 mode;
1.27      cvs      3222: PicType             contentType;
1.4       cvs      3223: TTcbf              *terminate_cbf;
                   3224: void               *context_tcbf;
                   3225: 
1.116     cvs      3226: #endif /* __STDC__ */
1.4       cvs      3227: {
1.116     cvs      3228:    AHTReqContext      *me;
1.250     cvs      3229:    CHARSET             charset;
1.4       cvs      3230:    int                 status;
1.250     cvs      3231:    unsigned long       file_size;
1.116     cvs      3232:    char               *fileURL;
1.168     cvs      3233:    char               *etag = NULL;
1.169     cvs      3234:    HTParentAnchor     *dest_anc_parent;
1.235     cvs      3235:    CHAR_T*             tmp;
                   3236:    CHAR_T*             esc_url;
1.168     cvs      3237:    int                 UsePreconditions;
1.191     cvs      3238:    ThotBool            lost_update_check = TRUE;
1.235     cvs      3239:    char                url_name[MAX_LENGTH];
                   3240:    char*               tmp2;
1.239     cvs      3241: #ifdef _WINDOWS
                   3242:    char                file_name[MAX_LENGTH];
                   3243: #endif /* _WINDOWS */
1.172     cvs      3244: 
                   3245:    /* should we protect the PUT against lost updates? */
1.203     cvs      3246:    tmp = TtaGetEnvString ("ENABLE_LOST_UPDATE_CHECK");
1.198     cvs      3247:    if (tmp && *tmp && ustrcasecmp (tmp, TEXT("yes")))
1.172     cvs      3248:      lost_update_check = FALSE;
1.168     cvs      3249: 
                   3250:    UsePreconditions = mode & AMAYA_USE_PRECONDITIONS;
1.4       cvs      3251: 
1.33      cvs      3252:    AmayaLastHTTPErrorMsg [0] = EOS;
                   3253:    
1.116     cvs      3254:    if (urlName == NULL || docid == 0 || fileName == NULL 
                   3255:        || !TtaFileExist (fileName))
1.4       cvs      3256:       /* no file to be uploaded */
                   3257:       return HT_ERROR;
                   3258: 
                   3259:    /* do we support this protocol? */
1.7       cvs      3260:    if (IsValidProtocol (urlName) == NO)
                   3261:      {
                   3262:        /* return error */
1.198     cvs      3263:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL), urlName);
1.7       cvs      3264:        return HT_ERROR;
                   3265:      }
1.116     cvs      3266: 
1.228     cvs      3267:    /* get the size of the file */
1.252     kahan    3268:    if (!AM_GetFileSize (fileName, &file_size) || file_size == 0L)
1.7       cvs      3269:      {
                   3270:        /* file was empty */
                   3271:        /*errmsg here */
                   3272:        return (HT_ERROR);
                   3273:      }
1.116     cvs      3274: 
                   3275:    /* prepare the request context */
1.4       cvs      3276:    if (THD_TRACE)
1.228     cvs      3277:       fprintf (stderr, "file size == %u\n", (unsigned) file_size);
1.4       cvs      3278: 
                   3279:    me = AHTReqContext_new (docid);
1.7       cvs      3280:    if (me == NULL)
                   3281:      {
1.168     cvs      3282:        /* @@ need an error message here */
1.151     cvs      3283:        TtaHandlePendingEvents (); 
1.7       cvs      3284:        return (HT_ERROR);
1.208     cvs      3285:      }
                   3286: 
                   3287:    /*
                   3288:    ** Set up the original URL name
                   3289:    */
                   3290:    if (DocumentMeta[docid]->put_default_name)
                   3291:      {
1.233     cvs      3292:        CHAR_T *ptr1, *ptr2;
1.208     cvs      3293:        ptr1 = TtaGetEnvString ("DEFAULTNAME");
                   3294:        if (ptr1 && *ptr1) 
                   3295:         {
1.234     cvs      3296:           ptr2 = ustrstr (urlName, ptr1);
1.208     cvs      3297:           if (ptr2) 
                   3298:             {
1.235     cvs      3299:            wc2iso_strcpy (url_name, urlName);
                   3300:               me->default_put_name = TtaStrdup (url_name);
                   3301:               me->default_put_name[strlen (me->default_put_name) - ustrlen (ptr1)] = EOS;
1.208     cvs      3302:               HTRequest_setDefaultPutName (me->request, me->default_put_name);
                   3303:             }
                   3304:         }
1.7       cvs      3305:      }
1.116     cvs      3306: 
1.4       cvs      3307:    me->mode = mode;
                   3308:    me->incremental_cbf = (TIcbf *) NULL;
                   3309:    me->context_icbf = (void *) NULL;
                   3310:    me->terminate_cbf = terminate_cbf;
                   3311:    me->context_tcbf = context_tcbf;
1.206     cvs      3312:    esc_url = EscapeURL (urlName);
1.235     cvs      3313:    me->urlName = TtaWCSdup (esc_url);
1.206     cvs      3314:    TtaFreeMemory (esc_url);
1.228     cvs      3315:    me->block_size =  file_size;
1.17      cvs      3316:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      3317:    me->method = METHOD_PUT;
                   3318:    me->output = stdout;
1.17      cvs      3319:    /* we are not expecting to receive any input from the server */
1.235     cvs      3320:    me->outputfile = (CHAR_T*) NULL; 
1.4       cvs      3321: 
1.134     cvs      3322: #ifdef _WINDOWS
                   3323:    /* libwww's HTParse function doesn't take into account the drive name;
1.168     cvs      3324:       so we sidestep it */
1.238     cvs      3325: 
1.134     cvs      3326:    fileURL = NULL;
                   3327:    StrAllocCopy (fileURL, "file:");
1.235     cvs      3328:    wc2iso_strcpy (file_name, fileName);
                   3329:    StrAllocCat (fileURL, file_name);
1.134     cvs      3330: #else
1.116     cvs      3331:    fileURL = HTParse (fileName, "file:/", PARSE_ALL);
1.134     cvs      3332: #endif /* _WINDOWS */
1.168     cvs      3333:    me->source = HTAnchor_findAddress (fileURL);
1.116     cvs      3334:    HT_FREE (fileURL);
1.235     cvs      3335:    wc2iso_strcpy (url_name, me->urlName);
                   3336:    me->dest = HTAnchor_findAddress (url_name);
1.169     cvs      3337:    /* we memorize the anchor's parent @ as we use it a number of times
                   3338:       in the following lines */
                   3339:    dest_anc_parent = HTAnchor_parent (me->dest);
1.168     cvs      3340: 
1.169     cvs      3341:    /*
                   3342:    **  Set the Content-Type of the file we are uploading 
                   3343:    */
                   3344:    /* we try to use any content-type previosuly associated
                   3345:       with the parent. If it doesn't exist, we try to guess it
                   3346:       from the URL */
1.235     cvs      3347:    tmp2 = HTAtom_name (HTAnchor_format (dest_anc_parent));
                   3348:    if (!tmp2 || !strcmp (tmp2, "www/unknown"))
1.169     cvs      3349:      {
1.233     cvs      3350:        HTAnchor_setFormat (dest_anc_parent, AHTGuessAtom_for (me->urlName, contentType));
1.235     cvs      3351:        tmp2 = HTAtom_name (HTAnchor_format (dest_anc_parent));
1.169     cvs      3352:      }
                   3353:    /* .. and we give the same type to the source anchor */
                   3354:    /* we go thru setOutputFormat, rather than change the parent's
                   3355:       anchor, as that's the place that libwww expects it to be */
1.235     cvs      3356:    HTAnchor_setFormat (HTAnchor_parent (me->source), HTAtom_for (tmp2));
1.219     cvs      3357: 
1.235     cvs      3358:    HTRequest_setOutputFormat (me->request, HTAtom_for (tmp2));
1.219     cvs      3359: 
                   3360:    /*
                   3361:    **  Set the Charset of the file we are uploading 
                   3362:    */
1.240     cvs      3363:    /* we set the charset as indicated in the document's metadata
                   3364:       structure (and only if it exists) */
1.250     cvs      3365:    charset = TtaGetDocumentCharset (docid);
                   3366:    if (charset != UNDEFINED_CHARSET)
1.240     cvs      3367:      {
1.250     cvs      3368:        tmp =  TtaGetCharsetName (charset);
                   3369:        if (tmp && *tmp != WC_EOS)
                   3370:         {
                   3371:           tmp2 = TtaWC2ISOdup (tmp);
                   3372:           HTAnchor_setCharset (dest_anc_parent, HTAtom_for (tmp2));
                   3373:           TtaFreeMemory (tmp2);
                   3374:           tmp2 = HTAtom_name (HTAnchor_charset (dest_anc_parent));
                   3375:           /* .. and we give the same charset to the source anchor */
                   3376:           /* we go thru setCharSet, rather than change the parent's
                   3377:              anchor, as that's the place that libwww expects it to be */
                   3378:           HTAnchor_setCharset (HTAnchor_parent (me->source),
                   3379:                                HTAtom_for (tmp2));
                   3380:         }
1.219     cvs      3381:      }
                   3382: 
                   3383:    /*
                   3384:    ** define other request characteristics
                   3385:    */
1.116     cvs      3386: #ifdef _WINDOWS
1.136     cvs      3387:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      3388: #else
1.134     cvs      3389:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      3390: #endif /* _WINDOWS */
                   3391: 
1.151     cvs      3392:    /*
                   3393:    ** Make sure that the first request is flushed immediately and not
                   3394:    ** buffered in the output buffer
                   3395:    */
                   3396:    if (mode & AMAYA_FLUSH_REQUEST)
                   3397:      HTRequest_setFlush(me->request, YES);
1.168     cvs      3398:    
                   3399:    /* Should we use preconditions? */
1.172     cvs      3400:    if (lost_update_check)
1.168     cvs      3401:      {
1.172     cvs      3402:        if (UsePreconditions) 
                   3403:         etag = HTAnchor_etag (HTAnchor_parent (me->dest));
                   3404:        
                   3405:        if (etag) 
                   3406:         {
                   3407:           HTRequest_setPreconditions(me->request, HT_MATCH_THIS);
                   3408:         }
                   3409:        else
                   3410:         {
                   3411:           HTRequest_setPreconditions(me->request, HT_NO_MATCH);
                   3412:           HTRequest_addAfter(me->request, check_handler, NULL, NULL, HT_ALL,
                   3413:                              HT_FILTER_MIDDLE, YES);
1.175     cvs      3414:           HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL, 
                   3415:                               HT_NO_ACCESS, HT_FILTER_MIDDLE, YES);
                   3416:           HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL,
                   3417:                               HT_REAUTH, HT_FILTER_MIDDLE, YES);
                   3418:           HTRequest_addAfter (me->request, HTAuthInfoFilter, "http://*", NULL,
                   3419:                               HT_ALL, HT_FILTER_MIDDLE, YES);
                   3420:           HTRequest_addAfter (me->request, HTUseProxyFilter, "http://*", NULL,
                   3421:                               HT_USE_PROXY, HT_FILTER_MIDDLE, YES);
1.172     cvs      3422:         }
1.168     cvs      3423:      }
                   3424:    else
                   3425:      {
1.172     cvs      3426:        /* don't use preconditions */
1.168     cvs      3427:        HTRequest_setPreconditions(me->request, HT_NO_MATCH);
                   3428:      }
1.151     cvs      3429:    
1.136     cvs      3430:    /* don't use the cache while saving a document */
                   3431:    HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
1.116     cvs      3432: 
1.164     cvs      3433:    /* Throw away any reponse body */
                   3434:    /*
                   3435:    HTRequest_setOutputStream (me->request, HTBlackHole());        
                   3436:    */
                   3437: 
1.168     cvs      3438:    /* prepare the URLname that will be displayed in the status bar */
1.74      cvs      3439:    ChopURL (me->status_urlName, me->urlName);
1.233     cvs      3440:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING), me->status_urlName);
1.114     cvs      3441: 
1.164     cvs      3442:    /* make the request */
1.172     cvs      3443:    if (lost_update_check && (!UsePreconditions || !etag))
1.168     cvs      3444:      status = HTHeadAnchor (me->dest, me->request);
                   3445:    else
                   3446:      status = HTPutDocumentAnchor (HTAnchor_parent (me->source), me->dest, me->request);
1.4       cvs      3447: 
1.130     cvs      3448:    if (status == YES && me->reqStatus != HT_ERR)
1.7       cvs      3449:      {
1.168     cvs      3450:        /* part of the stop button handler */
                   3451:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   3452:         status = LoopForStop (me);
1.15      cvs      3453:      }
1.136     cvs      3454:    if (!HTRequest_kill (me->request))
                   3455:      AHTReqContext_delete (me);
1.168     cvs      3456:    
1.116     cvs      3457:    TtaHandlePendingEvents ();
1.90      cvs      3458: 
1.130     cvs      3459:    return (status == YES ? 0 : -1);
1.28      cvs      3460: }
1.4       cvs      3461: 
1.5       cvs      3462: /*----------------------------------------------------------------------
1.138     cvs      3463:   StopRequest
1.17      cvs      3464:   stops (kills) all active requests associated with a docid 
1.5       cvs      3465:   ----------------------------------------------------------------------*/
1.4       cvs      3466: #ifdef __STDC__
                   3467: void                StopRequest (int docid)
                   3468: #else
                   3469: void                StopRequest (docid)
                   3470: int                 docid;
                   3471: #endif
                   3472: {
1.140     cvs      3473:    if (Amaya && CanDoStop ())
1.138     cvs      3474:      { 
1.139     cvs      3475: #if 0 /* for later */
1.140     cvs      3476:        AHTDocId_Status    *docid_status;
                   3477:         /* verify if there are any requests at all associated with docid */
1.138     cvs      3478:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
                   3479:                                                          Amaya->docid_status);
                   3480:        if (docid_status == (AHTDocId_Status *) NULL)
                   3481:         return;
1.139     cvs      3482: #endif /* 0 */
1.138     cvs      3483:        /* temporary call to stop all requests, as libwww changed its API */
                   3484:        StopAllRequests (docid);
                   3485:      }
                   3486: }
                   3487: 
1.190     cvs      3488: /* @@@ the docid parameter isn't used... clean it up */
1.138     cvs      3489: /*----------------------------------------------------------------------
                   3490:   StopAllRequests
                   3491:   stops (kills) all active requests. We use the docid 
                   3492:   ----------------------------------------------------------------------*/
                   3493: #ifdef __STDC__
                   3494: void                StopAllRequests (int docid)
                   3495: #else
1.140     cvs      3496: void                StopAllRequests (docid)
1.138     cvs      3497: int                 docid;
                   3498: #endif
                   3499: {
1.4       cvs      3500:    HTList             *cur;
                   3501:    AHTReqContext      *me;
1.191     cvs      3502:    static ThotBool     lock_stop = 0;
                   3503:    ThotBool            async_flag;
1.232     cvs      3504:    AHTReqStatus        old_reqStatus;
1.116     cvs      3505: 
1.138     cvs      3506:    /* only do the stop if we're not being called while processing a 
1.146     cvs      3507:       request, and if we're not already dealing with a stop */
                   3508:    if (Amaya && CanDoStop () && !lock_stop)
1.7       cvs      3509:      {
1.100     cvs      3510: #ifdef DEBUG_LIBWWW
1.138     cvs      3511:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   3512:                "before kill: %d\n", Amaya->open_requests);
1.100     cvs      3513: #endif /* DEBUG_LIBWWW */
1.150     cvs      3514:        /* enter the critical section */
1.146     cvs      3515:        lock_stop = TRUE; 
1.214     cvs      3516:        /* set a module global variable so that we can do special
                   3517:          processing easier */
                   3518:        UserAborted_flag = TRUE;
1.232     cvs      3519:        /* abort all outstanding libwww UI dialogues */
1.233     cvs      3520:        CallbackDialogue (BaseDialog + FormAnswer,  STRING_DATA, (CHAR_T*) 0);
                   3521:        CallbackDialogue (BaseDialog + ConfirmForm, INTEGER_DATA, (CHAR_T*) 0);
1.164     cvs      3522:        /* expire all outstanding timers */
                   3523:        HTTimer_expireAll ();
1.160     cvs      3524:        /* HTNet_killAll (); */
1.245     kahan    3525:        if (Amaya->open_requests)
1.116     cvs      3526:         {
1.245     kahan    3527:           cur = Amaya->reqlist;
                   3528:           while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
1.160     cvs      3529:             {
1.245     kahan    3530:               if (AmayaIsAlive ())
                   3531:                 {
1.161     cvs      3532: #ifdef DEBUG_LIBWWW
1.245     kahan    3533:                   fprintf (stderr,"StopRequest: killing req %p, url %s, status %d\n", me, me->urlName, me->reqStatus);
1.161     cvs      3534: #endif /* DEBUG_LIBWWW */
1.245     kahan    3535:                   
                   3536:                   if (me->reqStatus != HT_END && me->reqStatus != HT_ABORT)
1.163     cvs      3537:                     {
1.245     kahan    3538:                       if ((me->mode & AMAYA_ASYNC)
                   3539:                           || (me->mode & AMAYA_IASYNC))
                   3540:                         async_flag = TRUE;
                   3541:                       else
                   3542:                         async_flag = FALSE;
                   3543:                       
                   3544:                       /* change the status to say that the request aborted */
                   3545:                       /* if the request was "busy", we just change a flag to say so and
                   3546:                          let the handler finish the processing itself */
                   3547:                       old_reqStatus = me->reqStatus;
                   3548:                       me->reqStatus = HT_ABORT;
                   3549:                       if (old_reqStatus == HT_BUSY)
                   3550:                         continue;
                   3551:                       
                   3552:                       /* kill the request, using the appropriate function */
                   3553:                       if (me->request->net)
                   3554:                         HTNet_killPipe (me->request->net);
                   3555:                       else
                   3556:                         {
                   3557:                           if (me->terminate_cbf)
                   3558:                             (*me->terminate_cbf) (me->docid, -1, me->urlName,
                   3559:                                                   me->outputfile,
                   3560:                                                   NULL,
                   3561:                                                   me->context_tcbf);
                   3562:                           
                   3563:                           if (async_flag) 
                   3564:                             /* explicitly free the request context for async
                   3565:                                requests. The sync requests context is freed
                   3566:                                by LoopForStop */
                   3567:                             AHTReqContext_delete (me);
                   3568:                         }
                   3569:                       cur = Amaya->reqlist;
1.160     cvs      3570:                     }
1.142     cvs      3571: #ifndef _WINDOWS
                   3572: #ifdef WWW_XWINDOWS
1.245     kahan    3573:                   /* to be on the safe side, remove all outstanding 
                   3574:                      X events */
1.160     cvs      3575:                   else 
                   3576:                     RequestKillAllXtevents (me);
1.142     cvs      3577: #endif /* WWW_XWINDOWS */
                   3578: #endif /* !_WINDOWS */
1.245     kahan    3579:                 }
1.160     cvs      3580:             }
1.245     kahan    3581:           /* Delete remaining channels */
                   3582:           HTChannel_safeDeleteAll ();
1.116     cvs      3583:         }
1.214     cvs      3584:        /* reset the stop status */
                   3585:        UserAborted_flag = FALSE;
1.150     cvs      3586:        /* exit the critical section */
1.148     cvs      3587:        lock_stop = FALSE; 
1.100     cvs      3588: #ifdef DEBUG_LIBWWW
1.138     cvs      3589:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   3590:                "after kill: %d\n", Amaya->open_requests);
1.100     cvs      3591: #endif /* DEBUG_LIBWWW */
1.138     cvs      3592:      }
                   3593: } /* StopAllRequests */
1.17      cvs      3594: 
                   3595: 
1.105     cvs      3596: /*----------------------------------------------------------------------
                   3597:   AmayaIsAlive
1.140     cvs      3598:   returns the value of the AmayaAlive_flag
1.105     cvs      3599:   ----------------------------------------------------------------------*/
                   3600: #ifdef __STDC__
1.191     cvs      3601: ThotBool AmayaIsAlive (void)
1.105     cvs      3602: #else
1.191     cvs      3603: ThotBool AmayaIsAlive ()
1.105     cvs      3604: #endif /* _STDC_ */
                   3605: {
1.140     cvs      3606:   return AmayaAlive_flag;
                   3607: }
                   3608: 
                   3609: /*----------------------------------------------------------------------
                   3610:   CanDoStop
                   3611:   returns the value of the CanDoStop flag
                   3612:   ----------------------------------------------------------------------*/
                   3613: #ifdef __STDC__
1.191     cvs      3614: ThotBool CanDoStop (void)
1.140     cvs      3615: #else
1.191     cvs      3616: ThotBool CanDoStop ()
1.140     cvs      3617: #endif /* _STDC_ */
                   3618: {
                   3619:   return CanDoStop_flag;
                   3620: }
                   3621: 
                   3622: /*----------------------------------------------------------------------
                   3623:   CanDoStop_set
                   3624:   sets the value of the CanDoStop flag
                   3625:   ----------------------------------------------------------------------*/
                   3626: #ifdef __STDC__
1.191     cvs      3627: void CanDoStop_set (ThotBool value)
1.140     cvs      3628: #else
                   3629: void CanDoStop (value)
1.191     cvs      3630: ThotBool value;
1.140     cvs      3631: #endif /* _STDC_ */
                   3632: {
                   3633:   CanDoStop_flag = value;
1.176     cvs      3634: }
                   3635: 
                   3636: #ifdef __STDC__
                   3637: void libwww_updateNetworkConf (int status)
                   3638: #else
                   3639: void libwww_updateNetworkConf (status)
                   3640: int status;
                   3641: #endif /*__STDC__*/
                   3642: {
                   3643:   /* @@@ the docid parameter isn't used... clean it up */
                   3644:   int docid = 1;
                   3645: 
                   3646:   /* first, stop all current requests, as the network
                   3647:    may make some changes */
                   3648:   StopAllRequests (docid);
1.207     cvs      3649: 
                   3650:   if (status & AMAYA_SAFEPUT_RESTART)
                   3651:     { 
                   3652:       SafePut_delete ();
                   3653:       SafePut_init ();
                   3654:     }
1.176     cvs      3655: 
                   3656:   if (status & AMAYA_PROXY_RESTART)
                   3657:     {
                   3658:       HTProxy_deleteAll ();
                   3659:       ProxyInit ();
                   3660:     }
                   3661: 
                   3662:   if (status & AMAYA_CACHE_RESTART)
                   3663:     {
                   3664:       clear_cachelock ();
                   3665:       HTCacheTerminate ();
                   3666:       HTCacheMode_setEnabled (NO);
                   3667:       CacheInit ();
                   3668:     }
1.193     cvs      3669: 
                   3670:   if (status & AMAYA_LANNEG_RESTART)
                   3671:     {
                   3672:       /* clear the current values */
                   3673:       if (acceptLanguages)
                   3674:        HTLanguage_deleteAll (acceptLanguages);
                   3675:       /* read in the new ones */
                   3676:       acceptLanguages = HTList_new ();
                   3677:       AHTAcceptLanguagesInit (acceptLanguages);
                   3678:     }
1.105     cvs      3679: }
1.246     kahan    3680: 
                   3681: /****************************************************************************
                   3682:  ** The following two functions allow to set and read the value of          *
                   3683:  ** the  FTPURL_flag. If this flag is true, it means we can browse FTP URLs *
                   3684:  ***************************************************************************/
                   3685: 
                   3686: /*----------------------------------------------------------------------
                   3687:   AHT_FTPURL_flag_set
                   3688:   ----------------------------------------------------------------------*/
                   3689: #ifdef __STDC__
                   3690: void AHTFTPURL_flag_set (ThotBool value)
                   3691: #else
                   3692: void AHTFTPURL_flag_set (value)
                   3693: ThotBool value;
                   3694: #endif /* __STDC__ */
                   3695: {
                   3696:   FTPURL_flag = value;
                   3697: }
                   3698: 
                   3699: /*----------------------------------------------------------------------
                   3700:   AHT_FTPURL_flag
                   3701:   ----------------------------------------------------------------------*/
                   3702: #ifdef __STDC__
                   3703: ThotBool AHTFTPURL_flag (void)
                   3704: #else
                   3705: ThotBool AHTFTPURL_flag (void)
                   3706: #endif /* __STDC__ */
                   3707: {
                   3708:   return (FTPURL_flag);
                   3709: }
                   3710: 

Webmaster