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

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

Webmaster