home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / extcache.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.0 KB  |  277 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef EXT_CACHE_H
  20. #define EXT_CACHE_H
  21.  
  22. #ifndef EXT_DB_ROUTINES
  23. #include "mcom_db.h"
  24. #endif
  25.  
  26. #ifdef EXT_DB_ROUTINES
  27. #define Bool char
  28. #define uint32 unsigned int
  29. #define int32  int
  30. #define XP_NEW(structure)  ((structure *) malloc(sizeof(structure)))
  31. #define XP_ALLOC   (void *) malloc
  32. #define XP_MEMCPY  memcpy
  33. #define XP_MEMSET  memset
  34. #define TRACEMSG(x) printf x
  35. #define FREEIF(x)  do { if(x) free(x); } while(0)
  36. #define FREE free
  37. #define XP_STRLEN  strlen
  38. #define XP_STRCHR  strchr
  39. #define XP_STRCMP  strcmp
  40. #define XP_ASSERT  assert
  41. #define MODULE_PRIVATE
  42. #define PRIVATE static
  43. #define TRUE  !0
  44. #define FALSE 0
  45.  
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <db.h>
  49. #endif 
  50.  
  51. #ifndef EXT_DB_ROUTINES
  52. #include "mkutils.h"
  53.  
  54. #ifndef NSPR20
  55. #include "prosdep.h"  /* for IS_LITTLE_ENDIAN / IS_BIG_ENDIAN */
  56. #else
  57. #include "prtypes.h"
  58. #endif
  59.  
  60. #endif /* EXT_DB_ROUTINES */
  61.  
  62. #if !defined(IS_LITTLE_ENDIAN) && !defined(IS_BIG_ENDIAN)
  63. ERROR! Must have a byte order
  64. #endif
  65.  
  66. #ifdef IS_LITTLE_ENDIAN
  67. #define COPY_INT32(_a,_b)  XP_MEMCPY(_a, _b, sizeof(int32));
  68. #else
  69. #define COPY_INT32(_a,_b)  /* swap */                   \
  70.     do {                                                \
  71.     ((char *)(_a))[0] = ((char *)(_b))[3];              \
  72.     ((char *)(_a))[1] = ((char *)(_b))[2];              \
  73.     ((char *)(_a))[2] = ((char *)(_b))[1];              \
  74.     ((char *)(_a))[3] = ((char *)(_b))[0];              \
  75.     } while(0)
  76. #endif
  77.  
  78. #define EXT_CACHE_NAME_STRING "INT_ExternalCacheNameString"
  79.  
  80. /* Internal WARNING!!  Some slots of this structure 
  81.  * are shared with URL_Struct and
  82.  * History_entry.  If you add a slot, decide whether it needs to be shared
  83.  * as well.
  84.  */
  85. typedef struct _net_CacheObject {
  86.     time_t  last_modified;
  87.     time_t  last_accessed;
  88.     time_t  expires;
  89.     Bool    is_netsite;
  90.     uint32  content_length;
  91.  
  92.     char    * filename;                 /* cache file name */
  93.     int32     filename_len;             /* optimization */
  94.     Bool      is_relative_path;         /* is the path relative? */
  95.  
  96.     /* Security information */
  97.     int32   security_on;                /* is security on? */
  98.     char   *key_cipher;                 /* cipher being used */
  99.     int32   key_size;                   /* size of the total key (in bits) */
  100.     int32   key_secret_size;            /* secret portion (in bits) */
  101.     struct  CERTCertificateStr *certificate;  /* server's certificate */
  102.  
  103.     time_t  lock_date;                  /* the file is locked if this
  104.                                          * is non-zero.  The date
  105.                                          * represents the time the
  106.                                          * lock was put in place.
  107.                                          * Locks are only valid for
  108.                                          * one session
  109.                                          */
  110.  
  111.     int32       method;
  112.     char    * address;
  113.     uint32    post_data_size;
  114.     char    * post_data;
  115.     char    * post_headers;
  116.     char    * content_type;
  117.     char    * content_encoding;
  118.     char    * charset;
  119.  
  120.     Bool      incomplete_file;          /* means that the whole
  121.                                          * file is not there.
  122.                                          * This can only be true
  123.                                          * if the server supports byteranges
  124.                                          */
  125.     uint32    real_content_length;      /* the whole content length
  126.                                          * i.e. the server size of a truncated 
  127.                                          * client file
  128.                                          */
  129.     char    * page_services_url;
  130.     char    * etag;            /* HTTP/1.1 Etag */
  131.  
  132. } net_CacheObject;
  133.  
  134. /* this is the version number of the cache database entry.
  135.  * It should be incremented in integer ingrements up
  136.  * to MAXINT32
  137.  */
  138. #define CACHE_FORMAT_VERSION 4
  139.  
  140. /* these defines specify the exact byte position
  141.  * of the first 4 elements in the DBT data struct
  142.  * Change these if you change the order of entry into
  143.  * the DBT
  144.  */
  145. #define LAST_MODIFIED_BYTE_POSITION   \
  146.                 sizeof(int32)+sizeof(int32)
  147. #define LAST_ACCESSED_BYTE_POSITION   \
  148.                 sizeof(int32)+sizeof(int32)+sizeof(time_t)
  149. #define EXPIRES_BYTE_POSITION         \
  150.                 sizeof(int32)+sizeof(int32)+sizeof(time_t)+sizeof(time_t)
  151. #define CONTENT_LENGTH_BYTE_POSITION  \
  152.                 sizeof(int32)+sizeof(int32)+sizeof(time_t)+sizeof(time_t) \
  153.                 +sizeof(time_t)
  154. #define IS_NETSITE_BYTE_POSITION  \
  155.                 sizeof(int32)+sizeof(int32)+sizeof(time_t)+sizeof(time_t) \
  156.                 +sizeof(time_t)+sizeof(int32)
  157.  
  158. #define LOCK_DATE_BYTE_POSITION  \
  159.                 sizeof(int32)+sizeof(int32)+sizeof(time_t)+sizeof(time_t) \
  160.                 +sizeof(time_t)+sizeof(int32)+sizeof(char)
  161.  
  162. #define FILENAME_SIZE_BYTE_POSITION   \
  163.                 sizeof(int32)+sizeof(int32)+sizeof(time_t)+sizeof(time_t) \
  164.                 +sizeof(time_t)+sizeof(uint32)+sizeof(char)+sizeof(time_t)
  165. #define FILENAME_BYTE_POSITION        \
  166.                 sizeof(int32)+sizeof(int32)+sizeof(time_t)+sizeof(time_t) \
  167.                 +sizeof(time_t)+sizeof(uint32)+sizeof(char)+sizeof(time_t) \
  168.                 +sizeof(int32)
  169.  
  170. /* generates a key for use in the cache database
  171.  * from a CacheObject struct
  172.  *
  173.  * Key is based on the address and the post_data
  174.  */
  175. extern DBT *
  176. net_GenCacheDBKey(char *address, char *post_data, int32 post_data_size);
  177.  
  178. /* returns a static string that contains the
  179.  * URL->address of the key
  180.  *
  181.  * returns NULL on error
  182.  */
  183. extern char *
  184. net_GetAddressFromCacheKey(DBT *key);
  185.  
  186.  
  187. /* allocs and copies a new DBT from an existing DBT
  188.  */
  189. extern DBT * net_CacheDBTDup(DBT *obj);
  190.  
  191. /* free the cache object
  192.  */
  193. extern void net_freeCacheObj (net_CacheObject * cache_obj);
  194.  
  195. /* takes a cache object and returns a malloc'd
  196.  * (void *) suitible for passing in as a database
  197.  * data storage object
  198.  */
  199. extern DBT * net_CacheStructToDBData(net_CacheObject * old_obj);
  200.  
  201. /* takes a database storage object and returns a malloc'd
  202.  * cache data object.  The cache object needs all of
  203.  * it's parts free'd.
  204.  *
  205.  * returns NULL on parse error
  206.  */
  207. extern net_CacheObject * net_DBDataToCacheStruct(DBT * db_obj);
  208.  
  209. /* checks a date within a DBT struct so
  210.  * that we don't have to convert it into a CacheObject
  211.  *
  212.  * This works because of the fixed length record format
  213.  * of the first part of the specific DBT format I'm
  214.  * using
  215.  *
  216.  * returns 0 on error
  217.  */
  218. extern time_t net_GetTimeInCacheDBT(DBT *data, int byte_position);
  219.  
  220. /* Sets a date within a DBT struct so
  221.  * that we don't have to convert it into a CacheObject
  222.  *
  223.  * This works because of the fixed length record format
  224.  * of the first part of the specific DBT format I'm
  225.  * using
  226.  *
  227.  * returns 0 on error
  228.  */
  229. extern void net_SetTimeInCacheDBT(DBT *data, int byte_position, time_t date);
  230.  
  231. /* Gets the filename within a cache DBT struct so
  232.  * that we don't have to convert it into a CacheObject
  233.  *
  234.  * This works because of the fixed length record format
  235.  * of the first part of the specific DBT format I'm
  236.  * using
  237.  *
  238.  * returns NULL on error
  239.  */
  240. extern char * net_GetFilenameInCacheDBT(DBT *data);
  241.  
  242. /* Gets a int32 within a DBT struct so
  243.  * that we don't have to convert it into a CacheObject
  244.  *
  245.  * This works because of the fixed length record format
  246.  * of the first part of the specific DBT format I'm
  247.  * using
  248.  *
  249.  * returns 0 on error
  250.  */
  251. extern time_t net_GetInt32InCacheDBT(DBT *data, int byte_position);
  252.  
  253. /* free's a DBT struct
  254.  */
  255. extern void net_FreeCacheDBTdata(DBT *stuff);
  256.  
  257. /* stores a cache object in the DBM database
  258.  */
  259. extern void net_ExtCacheStore(DB *database, net_CacheObject * obj);
  260.  
  261. /* takes a database storage object and returns an un-malloc'd
  262.  * cache data object. The structure returned has pointers
  263.  * directly into the database memory and are only valid
  264.  * until the next call to any database function
  265.  *
  266.  * do not free anything returned by this structure
  267.  */
  268. extern net_CacheObject * net_Fast_DBDataToCacheStruct(DBT *obj);
  269.  
  270. /* returns true if this DBT looks like a valid
  271.  * entry.  It looks at the checksum and the
  272.  * version number to see if it's valid
  273.  */
  274. extern Bool net_IsValidCacheDBT(DBT *obj);
  275.  
  276. #endif /* EXT_CACHE_H */
  277.