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

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

Webmaster