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

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

Webmaster