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

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

Webmaster