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

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

Webmaster