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

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

Webmaster