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

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

Webmaster