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

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

Webmaster