mod_cache.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file mod_cache.h
  18. * @brief Main include file for the Apache Transparent Cache
  19. *
  20. * @defgroup MOD_CACHE mod_cache
  21. * @ingroup APACHE_MODS
  22. * @{
  23. */
  24. #ifndef MOD_CACHE_H
  25. #define MOD_CACHE_H
  26. #include "httpd.h"
  27. #include "apr_date.h"
  28. #include "apr_optional.h"
  29. #include "apr_hooks.h"
  30. #include "cache_common.h"
  31. /* Create a set of CACHE_DECLARE(type), CACHE_DECLARE_NONSTD(type) and
  32. * CACHE_DECLARE_DATA with appropriate export and import tags for the platform
  33. */
  34. #if !defined(WIN32)
  35. #define CACHE_DECLARE(type) type
  36. #define CACHE_DECLARE_NONSTD(type) type
  37. #define CACHE_DECLARE_DATA
  38. #elif defined(CACHE_DECLARE_STATIC)
  39. #define CACHE_DECLARE(type) type __stdcall
  40. #define CACHE_DECLARE_NONSTD(type) type
  41. #define CACHE_DECLARE_DATA
  42. #elif defined(CACHE_DECLARE_EXPORT)
  43. #define CACHE_DECLARE(type) __declspec(dllexport) type __stdcall
  44. #define CACHE_DECLARE_NONSTD(type) __declspec(dllexport) type
  45. #define CACHE_DECLARE_DATA __declspec(dllexport)
  46. #else
  47. #define CACHE_DECLARE(type) __declspec(dllimport) type __stdcall
  48. #define CACHE_DECLARE_NONSTD(type) __declspec(dllimport) type
  49. #define CACHE_DECLARE_DATA __declspec(dllimport)
  50. #endif
  51. /* cache info information */
  52. typedef struct cache_info cache_info;
  53. struct cache_info {
  54. /**
  55. * the original time corresponding to the 'Date:' header of the request
  56. * served
  57. */
  58. apr_time_t date;
  59. /** a time when the cached entity is due to expire */
  60. apr_time_t expire;
  61. /** r->request_time from the same request */
  62. apr_time_t request_time;
  63. /** apr_time_now() at the time the entity was actually cached */
  64. apr_time_t response_time;
  65. /**
  66. * HTTP status code of the cached entity. Though not necessarily the
  67. * status code finally issued to the request.
  68. */
  69. int status;
  70. /* cached cache-control */
  71. cache_control_t control;
  72. };
  73. /* cache handle information */
  74. typedef struct cache_object cache_object_t;
  75. struct cache_object {
  76. const char *key;
  77. cache_object_t *next;
  78. cache_info info;
  79. /* Opaque portion (specific to the implementation) of the cache object */
  80. void *vobj;
  81. };
  82. typedef struct cache_handle cache_handle_t;
  83. struct cache_handle {
  84. cache_object_t *cache_obj;
  85. apr_table_t *req_hdrs; /* cached request headers */
  86. apr_table_t *resp_hdrs; /* cached response headers */
  87. };
  88. #define CACHE_PROVIDER_GROUP "cache"
  89. typedef struct {
  90. int (*remove_entity) (cache_handle_t *h);
  91. apr_status_t (*store_headers)(cache_handle_t *h, request_rec *r, cache_info *i);
  92. apr_status_t (*store_body)(cache_handle_t *h, request_rec *r, apr_bucket_brigade *in,
  93. apr_bucket_brigade *out);
  94. apr_status_t (*recall_headers) (cache_handle_t *h, request_rec *r);
  95. apr_status_t (*recall_body) (cache_handle_t *h, apr_pool_t *p, apr_bucket_brigade *bb);
  96. int (*create_entity) (cache_handle_t *h, request_rec *r,
  97. const char *urlkey, apr_off_t len, apr_bucket_brigade *bb);
  98. int (*open_entity) (cache_handle_t *h, request_rec *r,
  99. const char *urlkey);
  100. int (*remove_url) (cache_handle_t *h, request_rec *r);
  101. apr_status_t (*commit_entity)(cache_handle_t *h, request_rec *r);
  102. apr_status_t (*invalidate_entity)(cache_handle_t *h, request_rec *r);
  103. } cache_provider;
  104. typedef enum {
  105. AP_CACHE_HIT,
  106. AP_CACHE_REVALIDATE,
  107. AP_CACHE_MISS,
  108. AP_CACHE_INVALIDATE
  109. } ap_cache_status_e;
  110. #define AP_CACHE_HIT_ENV "cache-hit"
  111. #define AP_CACHE_REVALIDATE_ENV "cache-revalidate"
  112. #define AP_CACHE_MISS_ENV "cache-miss"
  113. #define AP_CACHE_INVALIDATE_ENV "cache-invalidate"
  114. #define AP_CACHE_STATUS_ENV "cache-status"
  115. /* cache_util.c */
  116. /* do a HTTP/1.1 age calculation */
  117. CACHE_DECLARE(apr_time_t) ap_cache_current_age(cache_info *info, const apr_time_t age_value,
  118. apr_time_t now);
  119. CACHE_DECLARE(apr_time_t) ap_cache_hex2usec(const char *x);
  120. CACHE_DECLARE(void) ap_cache_usec2hex(apr_time_t j, char *y);
  121. CACHE_DECLARE(char *) ap_cache_generate_name(apr_pool_t *p, int dirlevels,
  122. int dirlength,
  123. const char *name);
  124. CACHE_DECLARE(const char *)ap_cache_tokstr(apr_pool_t *p, const char *list, const char **str);
  125. /* Create a new table consisting of those elements from an
  126. * headers table that are allowed to be stored in a cache.
  127. */
  128. CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_headers(apr_pool_t *pool,
  129. apr_table_t *t,
  130. server_rec *s);
  131. /* Create a new table consisting of those elements from an input
  132. * headers table that are allowed to be stored in a cache.
  133. */
  134. CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_headers_in(request_rec *r);
  135. /* Create a new table consisting of those elements from an output
  136. * headers table that are allowed to be stored in a cache;
  137. * ensure there is a content type and capture any errors.
  138. */
  139. CACHE_DECLARE(apr_table_t *)ap_cache_cacheable_headers_out(request_rec *r);
  140. /**
  141. * Parse the Cache-Control and Pragma headers in one go, marking
  142. * which tokens appear within the header. Populate the structure
  143. * passed in.
  144. */
  145. int ap_cache_control(request_rec *r, cache_control_t *cc, const char *cc_header,
  146. const char *pragma_header, apr_table_t *headers);
  147. /* hooks */
  148. /**
  149. * Cache status hook.
  150. * This hook is called as soon as the cache has made a decision as to whether
  151. * an entity should be served from cache (hit), should be served from cache
  152. * after a successful validation (revalidate), or served from the backend
  153. * and potentially cached (miss).
  154. *
  155. * A basic implementation of this hook exists in mod_cache which writes this
  156. * information to the subprocess environment, and optionally to request
  157. * headers. Further implementations may add hooks as appropriate to perform
  158. * more advanced processing, or to store statistics about the cache behaviour.
  159. */
  160. APR_DECLARE_EXTERNAL_HOOK(cache, CACHE, int, cache_status, (cache_handle_t *h,
  161. request_rec *r, apr_table_t *headers, ap_cache_status_e status,
  162. const char *reason))
  163. APR_DECLARE_OPTIONAL_FN(apr_status_t,
  164. ap_cache_generate_key,
  165. (request_rec *r, apr_pool_t*p, const char **key));
  166. #endif /*MOD_CACHE_H*/
  167. /** @} */