Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

473 lines
11 KiB

  1. /*++
  2. Copyright (c) 1998-2002 Microsoft Corporation
  3. Module Name:
  4. cache.h
  5. Abstract:
  6. The public definition of response cache interfaces.
  7. Author:
  8. Michael Courage (mcourage) 17-May-1999
  9. Revision History:
  10. --*/
  11. #ifndef _CACHE_H_
  12. #define _CACHE_H_
  13. //
  14. // Forwards
  15. //
  16. typedef struct _UL_INTERNAL_RESPONSE *PUL_INTERNAL_RESPONSE;
  17. typedef struct _UL_INTERNAL_DATA_CHUNK *PUL_INTERNAL_DATA_CHUNK;
  18. //
  19. // Cache configuration
  20. //
  21. typedef struct _UL_URI_CACHE_CONFIG {
  22. BOOLEAN EnableCache;
  23. ULONG MaxCacheUriCount;
  24. ULONG MaxCacheMegabyteCount;
  25. ULONGLONG MaxCacheByteCount;
  26. ULONG ScavengerPeriod;
  27. ULONG MaxUriBytes;
  28. LONG HashTableBits;
  29. } UL_URI_CACHE_CONFIG, *PUL_URI_CACHE_CONFIG;
  30. extern UL_URI_CACHE_CONFIG g_UriCacheConfig;
  31. //
  32. // Cache statistics
  33. //
  34. typedef struct _UL_URI_CACHE_STATS {
  35. ULONG UriCount; // entries in hash table
  36. ULONG UriCountMax; // high water mark
  37. ULONGLONG UriAddedTotal; // total entries ever added
  38. ULONGLONG ByteCount; // memory used for cache
  39. ULONGLONG ByteCountMax; // high water
  40. ULONG ZombieCount; // length of zombie list
  41. ULONG ZombieCountMax; // high water
  42. ULONG HitCount; // table lookup succeeded
  43. ULONG MissTableCount; // entry not in table
  44. ULONG MissPreconditionCount; // request not cacheable
  45. ULONG MissConfigCount; // config invalidated
  46. ULONG UriTypeNotSpecifiedCount; // Uri's site binding is not applicable
  47. ULONG UriTypeIpBoundCount; // Uri's site binding is IP only
  48. ULONG UriTypeHostPlusIpBoundCount; // Uri's site binding is Host + IP
  49. ULONG UriTypeHostBoundCount; // Uri's site binding is Host only
  50. ULONG UriTypeWildCardCount; // Uri's site binding is wildcard
  51. } UL_URI_CACHE_STATS, *PUL_URI_CACHE_STATS;
  52. extern UL_URI_CACHE_STATS g_UriCacheStats;
  53. __inline
  54. ULONG
  55. UlGetIpBoundUriCacheCount()
  56. {
  57. return g_UriCacheStats.UriTypeIpBoundCount;
  58. }
  59. __inline
  60. ULONG
  61. UlGetHostPlusIpBoundUriCacheCount()
  62. {
  63. return g_UriCacheStats.UriTypeHostPlusIpBoundCount;
  64. }
  65. __inline
  66. ULONG
  67. UlGetHostBoundUriCacheCount()
  68. {
  69. return g_UriCacheStats.UriTypeHostBoundCount;
  70. }
  71. //
  72. // Structure of an HTTP cache table entry.
  73. //
  74. typedef enum _URI_KEY_TYPE
  75. {
  76. UriKeyTypeNormal = 0,
  77. UriKeyTypeExtended,
  78. UriKeyTypeMax
  79. } URI_KEY_TYPE, *PURI_KEY_TYPE;
  80. typedef struct URI_KEY
  81. {
  82. ULONG Hash;
  83. PWSTR pUri;
  84. ULONG Length;
  85. // Optional pointer which will point to
  86. // the AbsPath of the pUri. Only set when
  87. // the URI_KEY is used in the cache entry.
  88. PWSTR pPath;
  89. } URI_KEY, *PURI_KEY;
  90. typedef struct EX_URI_KEY
  91. {
  92. ULONG Hash;
  93. PWSTR pAbsPath;
  94. ULONG AbsPathLength;
  95. PWSTR pToken;
  96. ULONG TokenLength;
  97. } EX_URI_KEY, *PEX_URI_KEY;
  98. typedef struct _URI_SEARCH_KEY
  99. {
  100. URI_KEY_TYPE Type;
  101. union
  102. {
  103. URI_KEY Key;
  104. EX_URI_KEY ExKey;
  105. };
  106. } URI_SEARCH_KEY, *PURI_SEARCH_KEY;
  107. #define IS_VALID_URI_SEARCH_KEY(pKey) \
  108. ((pKey)->Type == UriKeyTypeNormal || (pKey)->Type == UriKeyTypeExtended)
  109. //
  110. // Structure for holding the split-up content type. Assumes that types and
  111. // subtypes will never be longer than MAX_TYPE_LEN.
  112. //
  113. #define MAX_TYPE_LENGTH 32
  114. #define MAX_SUBTYPE_LENGTH 64
  115. typedef struct _UL_CONTENT_TYPE
  116. {
  117. ULONG TypeLen;
  118. UCHAR Type[MAX_TYPE_LENGTH];
  119. ULONG SubTypeLen;
  120. UCHAR SubType[MAX_SUBTYPE_LENGTH];
  121. } UL_CONTENT_TYPE, *PUL_CONTENT_TYPE;
  122. #define IS_VALID_URI_CACHE_ENTRY(pEntry) \
  123. HAS_VALID_SIGNATURE(pEntry, UL_URI_CACHE_ENTRY_POOL_TAG)
  124. #define IS_RESPONSE_CACHE_ENTRY(pEntry) \
  125. (0 != (pEntry)->HeaderLength)
  126. #define IS_FRAGMENT_CACHE_ENTRY(pEntry) \
  127. (0 == (pEntry)->HeaderLength)
  128. typedef struct _UL_URI_CACHE_ENTRY // CacheEntry
  129. {
  130. //
  131. // PagedPool
  132. //
  133. ULONG Signature; // UL_URI_CACHE_ENTRY_POOL_TAG
  134. LONG ReferenceCount;
  135. //
  136. // cache info
  137. //
  138. SINGLE_LIST_ENTRY BucketEntry;
  139. URI_KEY UriKey;
  140. ULONG HitCount;
  141. LIST_ENTRY ZombieListEntry;
  142. BOOLEAN Zombie;
  143. BOOLEAN ZombieAddReffed;
  144. BOOLEAN Cached;
  145. BOOLEAN ContentLengthSpecified; // hack
  146. USHORT StatusCode;
  147. HTTP_VERB Verb;
  148. ULONG ScavengerTicks;
  149. HTTP_CACHE_POLICY CachePolicy;
  150. LARGE_INTEGER ExpirationTime;
  151. //
  152. // System time of Date that went out on original response
  153. //
  154. LARGE_INTEGER CreationTime;
  155. //
  156. // ETag of original response
  157. //
  158. ULONG ETagLength; // Including NULL
  159. PUCHAR pETag;
  160. //
  161. // Content-Encoding of original response
  162. //
  163. ULONG ContentEncodingLength; // Incl. NULL
  164. PUCHAR pContentEncoding;
  165. //
  166. // Content-Type of original response
  167. //
  168. UL_CONTENT_TYPE ContentType;
  169. //
  170. // config and process data for invalidation
  171. //
  172. UL_URL_CONFIG_GROUP_INFO ConfigInfo;
  173. PUL_APP_POOL_PROCESS pProcess;
  174. PUL_APP_POOL_OBJECT pAppPool;
  175. //
  176. // Response data
  177. //
  178. ULONG HeaderLength;
  179. ULONG ContentLength;
  180. PMDL pMdl; // including content + header
  181. ULONG_PTR NumPages; // # pages allocated in pMdl
  182. //
  183. // Logging Information. When enabled, logging info
  184. // follows the structure after etags.
  185. //
  186. BOOLEAN LoggingEnabled;
  187. BOOLEAN BinaryLogged;
  188. ULONG BinaryIndexWritten;
  189. USHORT UsedOffset1;
  190. USHORT UsedOffset2;
  191. ULONG LogDataLength;
  192. PUCHAR pLogData;
  193. //
  194. // Followings are allocated at the end of the structure.
  195. //
  196. // WSTR Uri[];
  197. // UCHAR ETag[];
  198. // UCHAR LogData[];
  199. } UL_URI_CACHE_ENTRY, *PUL_URI_CACHE_ENTRY;
  200. //
  201. // public functions
  202. //
  203. NTSTATUS
  204. UlInitializeUriCache(
  205. PUL_CONFIG pConfig
  206. );
  207. VOID
  208. UlTerminateUriCache(
  209. VOID
  210. );
  211. VOID
  212. UlInitCacheEntry(
  213. PUL_URI_CACHE_ENTRY pUriCacheEntry,
  214. ULONG Hash,
  215. ULONG Length,
  216. PCWSTR pUrl,
  217. PCWSTR pAbsPath,
  218. PCWSTR pRoutingToken,
  219. USHORT RoutingTokenLength
  220. );
  221. NTSTATUS
  222. UlAddCacheEntry(
  223. PUL_URI_CACHE_ENTRY pUriCacheEntry
  224. );
  225. PUL_URI_CACHE_ENTRY
  226. UlCheckoutUriCacheEntry(
  227. PURI_SEARCH_KEY pSearchKey
  228. );
  229. VOID
  230. UlCheckinUriCacheEntry(
  231. PUL_URI_CACHE_ENTRY pUriCacheEntry
  232. );
  233. VOID
  234. UlFlushCache(
  235. IN PUL_CONTROL_CHANNEL pControlChannel
  236. );
  237. VOID
  238. UlFlushCacheByProcess(
  239. PUL_APP_POOL_PROCESS pProcess
  240. );
  241. VOID
  242. UlFlushCacheByUri(
  243. IN PWSTR pUri,
  244. IN ULONG Length,
  245. IN ULONG Flags,
  246. PUL_APP_POOL_PROCESS pProcess
  247. );
  248. //
  249. // cachability test functions
  250. //
  251. BOOLEAN
  252. UlCheckCachePreconditions(
  253. PUL_INTERNAL_REQUEST pRequest,
  254. PUL_HTTP_CONNECTION pHttpConn
  255. );
  256. BOOLEAN
  257. UlCheckCacheResponseConditions(
  258. PUL_INTERNAL_REQUEST pRequest,
  259. PUL_INTERNAL_RESPONSE pResponse,
  260. ULONG Flags,
  261. HTTP_CACHE_POLICY CachePolicy
  262. );
  263. // reference counting
  264. LONG
  265. UlAddRefUriCacheEntry(
  266. IN PUL_URI_CACHE_ENTRY pUriCacheEntry,
  267. IN REFTRACE_ACTION Action
  268. REFERENCE_DEBUG_FORMAL_PARAMS
  269. );
  270. LONG
  271. UlReleaseUriCacheEntry(
  272. IN PUL_URI_CACHE_ENTRY pUriCacheEntry,
  273. IN REFTRACE_ACTION Action
  274. REFERENCE_DEBUG_FORMAL_PARAMS
  275. );
  276. #define REFERENCE_URI_CACHE_ENTRY( pEntry, Action ) \
  277. UlAddRefUriCacheEntry( \
  278. (pEntry), \
  279. (REF_ACTION_##Action##_URI_ENTRY) \
  280. REFERENCE_DEBUG_ACTUAL_PARAMS \
  281. )
  282. #define DEREFERENCE_URI_CACHE_ENTRY( pEntry, Action ) \
  283. UlReleaseUriCacheEntry( \
  284. (pEntry), \
  285. (REF_ACTION_##Action##_URI_ENTRY) \
  286. REFERENCE_DEBUG_ACTUAL_PARAMS \
  287. )
  288. // Periodic Scavenger
  289. VOID
  290. UlPeriodicCacheScavenger(
  291. ULONG Age
  292. );
  293. // Reclaim memory from cache
  294. VOID
  295. UlTrimCache(
  296. IN ULONG_PTR Pages,
  297. IN ULONG Age
  298. );
  299. // fragment cache
  300. NTSTATUS
  301. UlAddFragmentToCache(
  302. IN PUL_APP_POOL_PROCESS pProcess,
  303. IN PUNICODE_STRING pFullyQualifiedUrl,
  304. IN PHTTP_DATA_CHUNK pDataChunk,
  305. IN PHTTP_CACHE_POLICY pCachePolicy,
  306. IN KPROCESSOR_MODE RequestorMode
  307. );
  308. NTSTATUS
  309. UlReadFragmentFromCache(
  310. IN PUL_APP_POOL_PROCESS pProcess,
  311. IN PVOID pInputBuffer,
  312. IN ULONG InputBufferLength,
  313. OUT PVOID pOutputBuffer,
  314. IN ULONG OutputBufferLength,
  315. IN KPROCESSOR_MODE RequestorMode,
  316. OUT PULONG pBytesRead
  317. );
  318. VOID
  319. UlClearCentralizedLogged(
  320. IN PVOID pContext
  321. );
  322. //
  323. // Wrappers to memory allocate routines
  324. //
  325. PUL_URI_CACHE_ENTRY
  326. UlAllocateCacheEntry(
  327. ULONG SpaceLength,
  328. ULONG ResponseLength
  329. );
  330. VOID
  331. UlFreeCacheEntry(
  332. PUL_URI_CACHE_ENTRY pEntry
  333. );
  334. /***************************************************************************++
  335. Routine Description:
  336. Copy cache data to the specified entry starting from Offset.
  337. --***************************************************************************/
  338. __inline BOOLEAN
  339. UlCacheEntrySetData(
  340. IN PUL_URI_CACHE_ENTRY pEntry,
  341. IN PUCHAR pBuffer,
  342. IN ULONG Length,
  343. IN ULONG Offset
  344. )
  345. {
  346. ASSERT( IS_VALID_URI_CACHE_ENTRY(pEntry) );
  347. ASSERT(pEntry->pMdl != NULL);
  348. ASSERT(Offset <= pEntry->pMdl->ByteCount);
  349. ASSERT(Length <= (pEntry->pMdl->ByteCount - Offset));
  350. return UlLargeMemSetData( pEntry->pMdl, pBuffer, Length, Offset );
  351. } // UlCacheEntrySetData
  352. //
  353. // Enable/Disable cache at runtime. Used by scavenger.
  354. //
  355. VOID
  356. UlDisableCache(
  357. VOID
  358. );
  359. VOID
  360. UlEnableCache(
  361. VOID
  362. );
  363. #endif // _CACHE_H_