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

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

Webmaster