Persistant Cache

So I guess the cache should be really really easy to implement into a
program, from what I've seen, shouldn't THIS do it?  
This is a simple, useless sample program I have whose only pupose in life
is to try and load something and hopefully fill up the cache a bit.  You
pass it an absolute url and it loads it, after initializing and stuff.
It creates the cache directory, and it seems to succeed in reading the
page off of the web server (my parser my_handler() gets called and passed
the correct token/value pair)  but no data is stored in the cache
directory.  
The thing is, the cache works fine when I take out the parser my_handler, 
which makes me think I'm messing something up rather basic here,
something conceptual. Perhaps someone could shed some light on this.  
Thanks a bunch!  The code follows.

-Chris

// Chris Mulhearn,  cb.cc   A quick test of the cache.
#include <iostream.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <assert.h>
#include <errno.h>

#include <sys/socket.h>
#include <sys/types.h>          
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/wait.h>

#include <netinet/in.h>

#include <w3c-libwww/wwwsys.h>
#include <w3c-libwww/WWWLib.h>
#include <w3c-libwww/WWWHTTP.h>
#include <w3c-libwww/WWWInit.h>

int my_handler(HTRequest *request, HTResponse */*response*/, const char *token, const char *val) {
   cerr << "I got called! (this out to stderr) token [" << token << "] value [" << val << "]" << endl;
}

int main(int argc, char * argv[])
{
   if(argc != 2) {
      cout << "usage: cb <url>" << endl;
      exit(-1);
   }
   char * url = new char[strlen(argv[1])];
   char * str = new char[65536];
   strcpy(url, argv[1]);
   HTTransportInit();
   // Set up http and cache protocols.
   HTProtocol_add("http", "buffered_tcp", HTTP_PORT, YES, HTLoadHTTP, NULL);
   HTProtocol_add("cache", "local", 0, YES, HTLoadCache, NULL);
   
   HTNetInit();
   HTAAInit();
   
   HTList * _conv = HTList_new();
   HTConverterInit(_conv);
   HTFormat_setConversion(_conv);
   
   HTList *transfer_encodings = HTList_new();
   HTTransferEncoderInit(transfer_encodings);
   HTFormat_setTransferCoding(transfer_encodings);
   
   HTMIMEInit();
   HTError_setShow(HT_ERR_SHOW_DEBUG);
 
   
   //Setup Cache!
   if(HTCacheInit("file:/devel/CACHE", 20) == YES) { 
      cerr << "Cache initialized!" << endl;
   }
   
   //HTCacheMode_setEnabled(TRUE);

   //   if(HTCacheIndex_write("file:/devel/CACHE") == YES) {
   //      cerr << "Wrote cache index!" << endl;
   //   }
   //-----------------
   
   HTCache_flushAll();
   
// If I comment out this parser, the cache gets filled.  If not, its
// left totally empty.

   HTHeader_addParser("Content-Length", NO, my_handler);
  

 
   // This tries to read the url given as an argument.
   FILE * stream;
   HTRequest *_request = HTRequest_new();
   HTRequest_setOutputFormat(_request, WWW_SOURCE);
   HTRequest_setOutputStream(_request, HTFWriter_new(_request, stream, YES));
   HTRequest_setReloadMode(_request, HT_CACHE_VALIDATE);
   int status = HTLoadAbsolute(url, _request); 
   if(status == YES) {
      cout << "looks good.[stdout]" << endl;
   }
   HTCacheTerminate();
   return 0;
}

Received on Thursday, 29 July 1999 11:03:49 UTC