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

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

Webmaster