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

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

Webmaster