William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Cache management |
| 3 | * |
| 4 | * Copyright 2017 HAProxy Technologies |
| 5 | * William Lallemand <wlallemand@haproxy.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 13 | #include <import/eb32tree.h> |
| 14 | #include <import/sha1.h> |
| 15 | |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 16 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 18 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 19 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 20 | #include <haproxy/cli.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 21 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 22 | #include <haproxy/filters.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 23 | #include <haproxy/hash.h> |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 24 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 25 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 26 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 27 | #include <haproxy/http_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/htx.h> |
| 29 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 30 | #include <haproxy/proxy.h> |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 31 | #include <haproxy/sample.h> |
Willy Tarreau | 334099c | 2020-06-03 18:38:48 +0200 | [diff] [blame] | 32 | #include <haproxy/shctx.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 33 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 34 | #include <haproxy/stream_interface.h> |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 35 | |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 36 | #define CACHE_FLT_F_IMPLICIT_DECL 0x00000001 /* The cache filtre was implicitly declared (ie without |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 37 | * the filter keyword) */ |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 38 | #define CACHE_FLT_INIT 0x00000002 /* Whether the cache name was freed. */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 39 | |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 40 | const char *cache_store_flt_id = "cache store filter"; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 41 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 42 | extern struct applet http_cache_applet; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 43 | |
| 44 | struct flt_ops cache_ops; |
| 45 | |
| 46 | struct cache { |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 47 | struct list list; /* cache linked list */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 48 | struct eb_root entries; /* head of cache entries based on keys */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 49 | unsigned int maxage; /* max-age */ |
| 50 | unsigned int maxblocks; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 51 | unsigned int maxobjsz; /* max-object-size (in bytes) */ |
Willy Tarreau | fd5efb5 | 2017-11-26 08:54:31 +0100 | [diff] [blame] | 52 | char id[33]; /* cache name */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 55 | /* cache config for filters */ |
| 56 | struct cache_flt_conf { |
| 57 | union { |
| 58 | struct cache *cache; /* cache used by the filter */ |
| 59 | char *name; /* cache name used during conf parsing */ |
| 60 | } c; |
| 61 | unsigned int flags; /* CACHE_FLT_F_* */ |
| 62 | }; |
| 63 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 64 | /* |
| 65 | * cache ctx for filters |
| 66 | */ |
| 67 | struct cache_st { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 68 | struct shared_block *first_block; |
| 69 | }; |
| 70 | |
| 71 | struct cache_entry { |
| 72 | unsigned int latest_validation; /* latest validation date */ |
| 73 | unsigned int expire; /* expiration date */ |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 74 | unsigned int age; /* Origin server "Age" header value */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 75 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 76 | struct eb32_node eb; /* ebtree node used to hold the cache object */ |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 77 | char hash[20]; |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 78 | |
| 79 | unsigned int etag_length; /* Length of the ETag value (if one was found in the response). */ |
| 80 | unsigned int etag_offset; /* Offset of the ETag value in the data buffer. */ |
| 81 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 82 | time_t last_modified; /* Origin server "Last-Modified" header value converted in |
| 83 | * seconds since epoch. If no "Last-Modified" |
| 84 | * header is found, use "Date" header value, |
| 85 | * otherwise use reception time. This field will |
| 86 | * be used in case of an "If-Modified-Since"-based |
| 87 | * conditional request. */ |
| 88 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 89 | unsigned char data[0]; |
| 90 | }; |
| 91 | |
| 92 | #define CACHE_BLOCKSIZE 1024 |
Willy Tarreau | 96062a1 | 2018-11-11 14:00:28 +0100 | [diff] [blame] | 93 | #define CACHE_ENTRY_MAX_AGE 2147483648U |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 94 | |
| 95 | static struct list caches = LIST_HEAD_INIT(caches); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 96 | static struct list caches_config = LIST_HEAD_INIT(caches_config); /* cache config to init */ |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 97 | static struct cache *tmp_cache_config = NULL; |
| 98 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 99 | DECLARE_STATIC_POOL(pool_head_cache_st, "cache_st", sizeof(struct cache_st)); |
| 100 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 101 | struct cache_entry *entry_exist(struct cache *cache, char *hash) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 102 | { |
| 103 | struct eb32_node *node; |
| 104 | struct cache_entry *entry; |
| 105 | |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 106 | node = eb32_lookup(&cache->entries, read_u32(hash)); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 107 | if (!node) |
| 108 | return NULL; |
| 109 | |
| 110 | entry = eb32_entry(node, struct cache_entry, eb); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 111 | |
| 112 | /* if that's not the right node */ |
| 113 | if (memcmp(entry->hash, hash, sizeof(entry->hash))) |
| 114 | return NULL; |
| 115 | |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 116 | if (entry->expire > now.tv_sec) { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 117 | return entry; |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 118 | } else { |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 119 | eb32_delete(node); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 120 | entry->eb.key = 0; |
| 121 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 122 | return NULL; |
| 123 | |
| 124 | } |
| 125 | |
| 126 | static inline struct shared_context *shctx_ptr(struct cache *cache) |
| 127 | { |
| 128 | return (struct shared_context *)((unsigned char *)cache - ((struct shared_context *)NULL)->data); |
| 129 | } |
| 130 | |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 131 | static inline struct shared_block *block_ptr(struct cache_entry *entry) |
| 132 | { |
| 133 | return (struct shared_block *)((unsigned char *)entry - ((struct shared_block *)NULL)->data); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 138 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 139 | cache_store_init(struct proxy *px, struct flt_conf *fconf) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 140 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 141 | fconf->flags |= FLT_CFG_FL_HTX; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 145 | static void |
| 146 | cache_store_deinit(struct proxy *px, struct flt_conf *fconf) |
| 147 | { |
| 148 | struct cache_flt_conf *cconf = fconf->conf; |
| 149 | |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 150 | if (!(cconf->flags & CACHE_FLT_INIT)) |
| 151 | free(cconf->c.name); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 152 | free(cconf); |
| 153 | } |
| 154 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 155 | static int |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 156 | cache_store_check(struct proxy *px, struct flt_conf *fconf) |
| 157 | { |
| 158 | struct cache_flt_conf *cconf = fconf->conf; |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 159 | struct flt_conf *f; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 160 | struct cache *cache; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 161 | int comp = 0; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 162 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 163 | /* Find the cache corresponding to the name in the filter config. The |
| 164 | * cache will not be referenced now in the filter config because it is |
| 165 | * not fully allocated. This step will be performed during the cache |
| 166 | * post_check. |
| 167 | */ |
| 168 | list_for_each_entry(cache, &caches_config, list) { |
| 169 | if (!strcmp(cache->id, cconf->c.name)) |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 170 | goto found; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | ha_alert("config: %s '%s': unable to find the cache '%s' referenced by the filter 'cache'.\n", |
| 174 | proxy_type_str(px), px->id, (char *)cconf->c.name); |
| 175 | return 1; |
| 176 | |
| 177 | found: |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 178 | /* Here <cache> points on the cache the filter must use and <cconf> |
| 179 | * points on the cache filter configuration. */ |
| 180 | |
| 181 | /* Check all filters for proxy <px> to know if the compression is |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 182 | * enabled and if it is after the cache. When the compression is before |
| 183 | * the cache, an error is returned. Also check if the cache filter must |
| 184 | * be explicitly declaired or not. */ |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 185 | list_for_each_entry(f, &px->filter_configs, list) { |
| 186 | if (f == fconf) { |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 187 | /* The compression filter must be evaluated after the cache. */ |
| 188 | if (comp) { |
| 189 | ha_alert("config: %s '%s': unable to enable the compression filter before " |
| 190 | "the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 191 | return 1; |
| 192 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 193 | } |
Christopher Faulet | 8f7fe1c | 2019-07-15 15:08:25 +0200 | [diff] [blame] | 194 | else if (f->id == http_comp_flt_id) |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 195 | comp = 1; |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 196 | else if (f->id == fcgi_flt_id) |
| 197 | continue; |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 198 | else if ((f->id != fconf->id) && (cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 199 | /* Implicit declaration is only allowed with the |
Christopher Faulet | 78fbb9f | 2019-08-11 23:11:03 +0200 | [diff] [blame] | 200 | * compression and fcgi. For other filters, an implicit |
Christopher Faulet | 27d93c3 | 2018-12-15 22:32:02 +0100 | [diff] [blame] | 201 | * declaration is required. */ |
| 202 | ha_alert("config: %s '%s': require an explicit filter declaration " |
| 203 | "to use the cache '%s'.\n", proxy_type_str(px), px->id, cache->id); |
| 204 | return 1; |
| 205 | } |
| 206 | |
Christopher Faulet | afd819c | 2018-12-11 08:57:45 +0100 | [diff] [blame] | 207 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 212 | cache_store_strm_init(struct stream *s, struct filter *filter) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 213 | { |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 214 | struct cache_st *st; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 215 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 216 | st = pool_alloc_dirty(pool_head_cache_st); |
| 217 | if (st == NULL) |
| 218 | return -1; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 219 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 220 | st->first_block = NULL; |
| 221 | filter->ctx = st; |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 222 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 223 | /* Register post-analyzer on AN_RES_WAIT_HTTP */ |
| 224 | filter->post_analyzers |= AN_RES_WAIT_HTTP; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 228 | static void |
| 229 | cache_store_strm_deinit(struct stream *s, struct filter *filter) |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 230 | { |
| 231 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 232 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 233 | struct cache *cache = cconf->c.cache; |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 234 | struct shared_context *shctx = shctx_ptr(cache); |
| 235 | |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 236 | /* Everything should be released in the http_end filter, but we need to do it |
| 237 | * there too, in case of errors */ |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 238 | if (st && st->first_block) { |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 239 | shctx_lock(shctx); |
| 240 | shctx_row_dec_hot(shctx, st->first_block); |
| 241 | shctx_unlock(shctx); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 242 | } |
| 243 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 244 | pool_free(pool_head_cache_st, st); |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 245 | filter->ctx = NULL; |
| 246 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 249 | static int |
| 250 | cache_store_post_analyze(struct stream *s, struct filter *filter, struct channel *chn, |
| 251 | unsigned an_bit) |
| 252 | { |
| 253 | struct http_txn *txn = s->txn; |
| 254 | struct http_msg *msg = &txn->rsp; |
| 255 | struct cache_st *st = filter->ctx; |
| 256 | |
| 257 | if (an_bit != AN_RES_WAIT_HTTP) |
| 258 | goto end; |
| 259 | |
| 260 | /* Here we need to check if any compression filter precedes the cache |
| 261 | * filter. This is only possible when the compression is configured in |
| 262 | * the frontend while the cache filter is configured on the |
| 263 | * backend. This case cannot be detected during HAProxy startup. So in |
| 264 | * such cases, the cache is disabled. |
| 265 | */ |
| 266 | if (st && (msg->flags & HTTP_MSGF_COMPRESSING)) { |
| 267 | pool_free(pool_head_cache_st, st); |
| 268 | filter->ctx = NULL; |
| 269 | } |
| 270 | |
| 271 | end: |
| 272 | return 1; |
| 273 | } |
William Lallemand | 49dc048 | 2017-11-24 14:33:54 +0100 | [diff] [blame] | 274 | |
| 275 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 276 | cache_store_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 277 | { |
| 278 | struct cache_st *st = filter->ctx; |
| 279 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 280 | if (!(msg->chn->flags & CF_ISRESP) || !st) |
| 281 | return 1; |
| 282 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 283 | if (st->first_block) |
Christopher Faulet | 67658c9 | 2018-12-06 21:59:39 +0100 | [diff] [blame] | 284 | register_data_filter(s, msg->chn, filter); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 285 | return 1; |
| 286 | } |
| 287 | |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 288 | static inline void disable_cache_entry(struct cache_st *st, |
| 289 | struct filter *filter, struct shared_context *shctx) |
| 290 | { |
| 291 | struct cache_entry *object; |
| 292 | |
| 293 | object = (struct cache_entry *)st->first_block->data; |
| 294 | filter->ctx = NULL; /* disable cache */ |
| 295 | shctx_lock(shctx); |
| 296 | shctx_row_dec_hot(shctx, st->first_block); |
| 297 | object->eb.key = 0; |
| 298 | shctx_unlock(shctx); |
| 299 | pool_free(pool_head_cache_st, st); |
| 300 | } |
| 301 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 302 | static int |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 303 | cache_store_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 304 | unsigned int offset, unsigned int len) |
| 305 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 306 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 307 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 308 | struct cache_st *st = filter->ctx; |
| 309 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 310 | struct htx_blk *blk; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 311 | struct shared_block *fb; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 312 | struct htx_ret htxret; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 313 | unsigned int orig_len, to_forward; |
| 314 | int ret; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 315 | |
| 316 | if (!len) |
| 317 | return len; |
| 318 | |
| 319 | if (!st->first_block) { |
| 320 | unregister_data_filter(s, msg->chn, filter); |
| 321 | return len; |
| 322 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 323 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 324 | chunk_reset(&trash); |
| 325 | orig_len = len; |
| 326 | to_forward = 0; |
Christopher Faulet | 497c759 | 2020-03-02 16:19:50 +0100 | [diff] [blame] | 327 | |
| 328 | htxret = htx_find_offset(htx, offset); |
| 329 | blk = htxret.blk; |
| 330 | offset = htxret.ret; |
| 331 | for (; blk && len; blk = htx_get_next_blk(htx, blk)) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 332 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 333 | uint32_t info, sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 334 | struct ist v; |
| 335 | |
| 336 | switch (type) { |
| 337 | case HTX_BLK_UNUSED: |
| 338 | break; |
| 339 | |
| 340 | case HTX_BLK_DATA: |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 341 | v = htx_get_blk_value(htx, blk); |
| 342 | v.ptr += offset; |
| 343 | v.len -= offset; |
| 344 | if (v.len > len) |
| 345 | v.len = len; |
| 346 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 347 | info = (type << 28) + v.len; |
| 348 | chunk_memcat(&trash, (char *)&info, sizeof(info)); |
| 349 | chunk_memcat(&trash, v.ptr, v.len); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 350 | to_forward += v.len; |
| 351 | len -= v.len; |
| 352 | break; |
| 353 | |
| 354 | default: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 355 | /* Here offset must always be 0 because only |
| 356 | * DATA blocks can be partially transferred. */ |
| 357 | if (offset) |
| 358 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 359 | if (sz > len) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 360 | goto end; |
| 361 | |
| 362 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 363 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 364 | to_forward += sz; |
| 365 | len -= sz; |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | offset = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 370 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 371 | |
| 372 | end: |
| 373 | shctx_lock(shctx); |
| 374 | fb = shctx_row_reserve_hot(shctx, st->first_block, trash.data); |
| 375 | if (!fb) { |
| 376 | shctx_unlock(shctx); |
| 377 | goto no_cache; |
| 378 | } |
| 379 | shctx_unlock(shctx); |
| 380 | |
| 381 | ret = shctx_row_data_append(shctx, st->first_block, st->first_block->last_append, |
| 382 | (unsigned char *)b_head(&trash), b_data(&trash)); |
| 383 | if (ret < 0) |
| 384 | goto no_cache; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 385 | |
| 386 | return to_forward; |
| 387 | |
| 388 | no_cache: |
| 389 | disable_cache_entry(st, filter, shctx); |
| 390 | unregister_data_filter(s, msg->chn, filter); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 391 | return orig_len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static int |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 395 | cache_store_http_end(struct stream *s, struct filter *filter, |
| 396 | struct http_msg *msg) |
| 397 | { |
| 398 | struct cache_st *st = filter->ctx; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 399 | struct cache_flt_conf *cconf = FLT_CONF(filter); |
| 400 | struct cache *cache = cconf->c.cache; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 401 | struct shared_context *shctx = shctx_ptr(cache); |
| 402 | struct cache_entry *object; |
| 403 | |
| 404 | if (!(msg->chn->flags & CF_ISRESP)) |
| 405 | return 1; |
| 406 | |
| 407 | if (st && st->first_block) { |
| 408 | |
| 409 | object = (struct cache_entry *)st->first_block->data; |
| 410 | |
| 411 | /* does not need to test if the insertion worked, if it |
| 412 | * doesn't, the blocks will be reused anyway */ |
| 413 | |
| 414 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 415 | if (eb32_insert(&cache->entries, &object->eb) != &object->eb) { |
| 416 | object->eb.key = 0; |
| 417 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 418 | /* remove from the hotlist */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 419 | shctx_row_dec_hot(shctx, st->first_block); |
| 420 | shctx_unlock(shctx); |
| 421 | |
| 422 | } |
| 423 | if (st) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 424 | pool_free(pool_head_cache_st, st); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 425 | filter->ctx = NULL; |
| 426 | } |
| 427 | |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * This intends to be used when checking HTTP headers for some |
| 433 | * word=value directive. Return a pointer to the first character of value, if |
| 434 | * the word was not found or if there wasn't any value assigned ot it return NULL |
| 435 | */ |
| 436 | char *directive_value(const char *sample, int slen, const char *word, int wlen) |
| 437 | { |
| 438 | int st = 0; |
| 439 | |
| 440 | if (slen < wlen) |
| 441 | return 0; |
| 442 | |
| 443 | while (wlen) { |
| 444 | char c = *sample ^ *word; |
| 445 | if (c && c != ('A' ^ 'a')) |
| 446 | return NULL; |
| 447 | sample++; |
| 448 | word++; |
| 449 | slen--; |
| 450 | wlen--; |
| 451 | } |
| 452 | |
| 453 | while (slen) { |
| 454 | if (st == 0) { |
| 455 | if (*sample != '=') |
| 456 | return NULL; |
| 457 | sample++; |
| 458 | slen--; |
| 459 | st = 1; |
| 460 | continue; |
| 461 | } else { |
| 462 | return (char *)sample; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Return the maxage in seconds of an HTTP response. |
| 471 | * Compute the maxage using either: |
| 472 | * - the assigned max-age of the cache |
| 473 | * - the s-maxage directive |
| 474 | * - the max-age directive |
| 475 | * - (Expires - Data) headers |
| 476 | * - the default-max-age of the cache |
| 477 | * |
| 478 | */ |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 479 | int http_calc_maxage(struct stream *s, struct cache *cache) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 480 | { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 481 | struct htx *htx = htxbuf(&s->res.buf); |
| 482 | struct http_hdr_ctx ctx = { .blk = NULL }; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 483 | int smaxage = -1; |
| 484 | int maxage = -1; |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 485 | int expires = -1; |
| 486 | struct tm tm = {}; |
| 487 | time_t expires_val = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 488 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 489 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 490 | char *value; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 491 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 492 | value = directive_value(ctx.value.ptr, ctx.value.len, "s-maxage", 8); |
| 493 | if (value) { |
| 494 | struct buffer *chk = get_trash_chunk(); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 495 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 496 | chunk_strncat(chk, value, ctx.value.len - 8 + 1); |
| 497 | chunk_strncat(chk, "", 1); |
| 498 | maxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 501 | value = directive_value(ctx.value.ptr, ctx.value.len, "max-age", 7); |
| 502 | if (value) { |
| 503 | struct buffer *chk = get_trash_chunk(); |
Christopher Faulet | 5f2c49f | 2019-07-15 20:49:46 +0200 | [diff] [blame] | 504 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 505 | chunk_strncat(chk, value, ctx.value.len - 7 + 1); |
| 506 | chunk_strncat(chk, "", 1); |
| 507 | smaxage = atoi(chk->area); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 511 | /* Look for Expires header if no s-maxage or max-age Cache-Control data |
| 512 | * was found. */ |
| 513 | if (maxage == -1 && smaxage == -1) { |
| 514 | ctx.blk = NULL; |
| 515 | if (http_find_header(htx, ist("expires"), &ctx, 1)) { |
| 516 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 517 | expires_val = my_timegm(&tm); |
| 518 | /* A request having an expiring date earlier |
| 519 | * than the current date should be considered as |
| 520 | * stale. */ |
| 521 | expires = (expires_val >= now.tv_sec) ? |
| 522 | (expires_val - now.tv_sec) : 0; |
| 523 | } |
| 524 | else { |
| 525 | /* Following RFC 7234#5.3, an invalid date |
| 526 | * format must be treated as a date in the past |
| 527 | * so the cache entry must be seen as already |
| 528 | * expired. */ |
| 529 | expires = 0; |
| 530 | } |
| 531 | } |
| 532 | } |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 533 | |
| 534 | |
| 535 | if (smaxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 536 | return MIN(smaxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 537 | |
| 538 | if (maxage > 0) |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 539 | return MIN(maxage, cache->maxage); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 540 | |
Remi Tricot-Le Breton | a647611 | 2020-10-28 17:52:53 +0100 | [diff] [blame] | 541 | if (expires >= 0) |
| 542 | return MIN(expires, cache->maxage); |
| 543 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 544 | return cache->maxage; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 545 | |
| 546 | } |
| 547 | |
| 548 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 549 | static void cache_free_blocks(struct shared_block *first, struct shared_block *block) |
| 550 | { |
Willy Tarreau | 5bd37fa | 2018-04-04 20:17:03 +0200 | [diff] [blame] | 551 | struct cache_entry *object = (struct cache_entry *)block->data; |
| 552 | |
| 553 | if (first == block && object->eb.key) |
| 554 | eb32_delete(&object->eb); |
| 555 | object->eb.key = 0; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 558 | |
| 559 | /* As per RFC 7234#4.3.2, in case of "If-Modified-Since" conditional request, the |
| 560 | * date value should be compared to a date determined by in a previous response (for |
| 561 | * the same entity). This date could either be the "Last-Modified" value, or the "Date" |
| 562 | * value of the response's reception time (by decreasing order of priority). */ |
| 563 | static time_t get_last_modified_time(struct htx *htx) |
| 564 | { |
| 565 | time_t last_modified = 0; |
| 566 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 567 | struct tm tm = {}; |
| 568 | |
| 569 | if (http_find_header(htx, ist("last-modified"), &ctx, 1)) { |
| 570 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 571 | last_modified = my_timegm(&tm); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (!last_modified) { |
| 576 | ctx.blk = NULL; |
| 577 | if (http_find_header(htx, ist("date"), &ctx, 1)) { |
| 578 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 579 | last_modified = my_timegm(&tm); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /* Fallback on the current time if no "Last-Modified" or "Date" header |
| 585 | * was found. */ |
| 586 | if (!last_modified) |
| 587 | last_modified = now.tv_sec; |
| 588 | |
| 589 | return last_modified; |
| 590 | } |
| 591 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 592 | /* |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 593 | * This function will store the headers of the response in a buffer and then |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 594 | * register a filter to store the data |
| 595 | */ |
| 596 | enum act_return http_action_store_cache(struct act_rule *rule, struct proxy *px, |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 597 | struct session *sess, struct stream *s, int flags) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 598 | { |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 599 | unsigned int age; |
| 600 | long long hdr_age; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 601 | struct http_txn *txn = s->txn; |
| 602 | struct http_msg *msg = &txn->rsp; |
| 603 | struct filter *filter; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 604 | struct shared_block *first = NULL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 605 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
| 606 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 607 | struct cache_st *cache_ctx = NULL; |
| 608 | struct cache_entry *object, *old; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 609 | unsigned int key = read_u32(txn->cache_hash); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 610 | struct htx *htx; |
| 611 | struct http_hdr_ctx ctx; |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 612 | size_t hdrs_len = 0; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 613 | int32_t pos; |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 614 | unsigned int etag_length = 0; |
| 615 | unsigned int etag_offset = 0; |
| 616 | struct ist header_name = IST_NULL; |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 617 | time_t last_modified = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 618 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 619 | /* Don't cache if the response came from a cache */ |
| 620 | if ((obj_type(s->target) == OBJ_TYPE_APPLET) && |
| 621 | s->target == &http_cache_applet.obj_type) { |
| 622 | goto out; |
| 623 | } |
| 624 | |
| 625 | /* cache only HTTP/1.1 */ |
| 626 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 627 | goto out; |
| 628 | |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 629 | /* cache only GET method */ |
| 630 | if (txn->meth != HTTP_METH_GET) |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 631 | goto out; |
| 632 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 633 | /* cache key was not computed */ |
| 634 | if (!key) |
| 635 | goto out; |
| 636 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 637 | /* cache only 200 status code */ |
| 638 | if (txn->status != 200) |
| 639 | goto out; |
| 640 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 641 | /* Find the corresponding filter instance for the current stream */ |
| 642 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 643 | if (FLT_ID(filter) == cache_store_flt_id && FLT_CONF(filter) == cconf) { |
| 644 | /* No filter ctx, don't cache anything */ |
| 645 | if (!filter->ctx) |
| 646 | goto out; |
| 647 | cache_ctx = filter->ctx; |
| 648 | break; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /* from there, cache_ctx is always defined */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 653 | htx = htxbuf(&s->res.buf); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 654 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 655 | /* Do not cache too big objects. */ |
| 656 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && shctx->max_obj_size > 0 && |
| 657 | htx->data + htx->extra > shctx->max_obj_size) |
| 658 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 659 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 660 | /* Does not manage Vary at the moment. We will need a secondary key later for that */ |
| 661 | ctx.blk = NULL; |
| 662 | if (http_find_header(htx, ist("Vary"), &ctx, 0)) |
| 663 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 664 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 665 | http_check_response_for_cacheability(s, &s->res); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 666 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 667 | if (!(txn->flags & TX_CACHEABLE) || !(txn->flags & TX_CACHE_COOK)) |
| 668 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 669 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 670 | age = 0; |
| 671 | ctx.blk = NULL; |
| 672 | if (http_find_header(htx, ist("Age"), &ctx, 0)) { |
| 673 | if (!strl2llrc(ctx.value.ptr, ctx.value.len, &hdr_age) && hdr_age > 0) { |
| 674 | if (unlikely(hdr_age > CACHE_ENTRY_MAX_AGE)) |
| 675 | hdr_age = CACHE_ENTRY_MAX_AGE; |
| 676 | age = hdr_age; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 677 | } |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 678 | http_remove_header(htx, &ctx); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 679 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 680 | |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 681 | /* Build a last-modified time that will be stored in the cache_entry and |
| 682 | * compared to a future If-Modified-Since client header. */ |
| 683 | last_modified = get_last_modified_time(htx); |
| 684 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 685 | chunk_reset(&trash); |
| 686 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 687 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 688 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 689 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 690 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 691 | hdrs_len += sizeof(*blk) + sz; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 692 | chunk_memcat(&trash, (char *)&blk->info, sizeof(blk->info)); |
| 693 | chunk_memcat(&trash, htx_get_blk_ptr(htx, blk), sz); |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 694 | |
| 695 | /* Look for optional ETag header. |
| 696 | * We need to store the offset of the ETag value in order for |
| 697 | * future conditional requests to be able to perform ETag |
| 698 | * comparisons. */ |
| 699 | if (type == HTX_BLK_HDR) { |
| 700 | header_name = htx_get_blk_name(htx, blk); |
| 701 | if (isteq(header_name, ist("etag"))) { |
| 702 | etag_length = sz - istlen(header_name); |
| 703 | etag_offset = sizeof(struct cache_entry) + b_data(&trash) - sz + istlen(header_name); |
| 704 | } |
| 705 | } |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 706 | if (type == HTX_BLK_EOH) |
| 707 | break; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 708 | } |
| 709 | |
Christopher Faulet | b066747 | 2019-09-03 22:22:12 +0200 | [diff] [blame] | 710 | /* Do not cache objects if the headers are too big. */ |
| 711 | if (hdrs_len > htx->size - global.tune.maxrewrite) |
| 712 | goto out; |
| 713 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 714 | shctx_lock(shctx); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 715 | first = shctx_row_reserve_hot(shctx, NULL, sizeof(struct cache_entry) + trash.data); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 716 | if (!first) { |
| 717 | shctx_unlock(shctx); |
| 718 | goto out; |
| 719 | } |
| 720 | shctx_unlock(shctx); |
| 721 | |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 722 | /* the received memory is not initialized, we need at least to mark |
| 723 | * the object as not indexed yet. |
| 724 | */ |
| 725 | object = (struct cache_entry *)first->data; |
| 726 | object->eb.node.leaf_p = NULL; |
| 727 | object->eb.key = 0; |
Frédéric Lécaille | e7a770c | 2018-10-26 14:29:22 +0200 | [diff] [blame] | 728 | object->age = age; |
Remi Tricot Le Breton | 27091b4 | 2020-10-23 10:51:27 +0200 | [diff] [blame] | 729 | object->last_modified = last_modified; |
Willy Tarreau | 1093a45 | 2018-04-06 19:02:25 +0200 | [diff] [blame] | 730 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 731 | /* reserve space for the cache_entry structure */ |
| 732 | first->len = sizeof(struct cache_entry); |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 733 | first->last_append = NULL; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 734 | /* cache the headers in a http action because it allows to chose what |
| 735 | * to cache, for example you might want to cache a response before |
| 736 | * modifying some HTTP headers, or on the contrary after modifying |
| 737 | * those headers. |
| 738 | */ |
| 739 | |
Remi Tricot-Le Breton | dbb65b5 | 2020-10-22 10:40:04 +0200 | [diff] [blame] | 740 | /* Write the ETag information in the cache_entry if needed. */ |
| 741 | object->etag_length = etag_length; |
| 742 | object->etag_offset = etag_offset; |
| 743 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 744 | /* does not need to be locked because it's in the "hot" list, |
| 745 | * copy the headers */ |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 746 | if (shctx_row_data_append(shctx, first, NULL, (unsigned char *)trash.area, trash.data) < 0) |
| 747 | goto out; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 748 | |
| 749 | /* register the buffer in the filter ctx for filling it with data*/ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 750 | if (cache_ctx) { |
| 751 | cache_ctx->first_block = first; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 752 | |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 753 | object->eb.key = key; |
| 754 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 755 | memcpy(object->hash, txn->cache_hash, sizeof(object->hash)); |
| 756 | /* Insert the node later on caching success */ |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 757 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 758 | shctx_lock(shctx); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 759 | |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 760 | old = entry_exist(cconf->c.cache, txn->cache_hash); |
| 761 | if (old) { |
| 762 | eb32_delete(&old->eb); |
| 763 | old->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 764 | } |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 765 | shctx_unlock(shctx); |
| 766 | |
| 767 | /* store latest value and expiration time */ |
| 768 | object->latest_validation = now.tv_sec; |
| 769 | object->expire = now.tv_sec + http_calc_maxage(s, cconf->c.cache); |
| 770 | return ACT_RET_CONT; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | out: |
| 774 | /* if does not cache */ |
| 775 | if (first) { |
| 776 | shctx_lock(shctx); |
William Lallemand | 0872766 | 2017-11-21 20:01:27 +0100 | [diff] [blame] | 777 | first->len = 0; |
| 778 | object->eb.key = 0; |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 779 | shctx_row_dec_hot(shctx, first); |
| 780 | shctx_unlock(shctx); |
| 781 | } |
| 782 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 783 | return ACT_RET_CONT; |
| 784 | } |
| 785 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 786 | #define HTX_CACHE_INIT 0 /* Initial state. */ |
| 787 | #define HTX_CACHE_HEADER 1 /* Cache entry headers forwarding */ |
| 788 | #define HTX_CACHE_DATA 2 /* Cache entry data forwarding */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 789 | #define HTX_CACHE_EOM 3 /* Cache entry completely forwarded. Finish the HTX message */ |
| 790 | #define HTX_CACHE_END 4 /* Cache entry treatment terminated */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 791 | |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 792 | static void http_cache_applet_release(struct appctx *appctx) |
| 793 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 794 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 795 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 796 | struct cache *cache = cconf->c.cache; |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 797 | struct shared_block *first = block_ptr(cache_ptr); |
| 798 | |
| 799 | shctx_lock(shctx_ptr(cache)); |
| 800 | shctx_row_dec_hot(shctx_ptr(cache), first); |
| 801 | shctx_unlock(shctx_ptr(cache)); |
| 802 | } |
| 803 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 804 | |
| 805 | static unsigned int htx_cache_dump_blk(struct appctx *appctx, struct htx *htx, enum htx_blk_type type, |
| 806 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 807 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 808 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 809 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 810 | struct htx_blk *blk; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 811 | char *ptr; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 812 | unsigned int max, total; |
| 813 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 814 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 815 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 816 | if (!max) |
| 817 | return 0; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 818 | blksz = ((type == HTX_BLK_HDR || type == HTX_BLK_TLR) |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 819 | ? (info & 0xff) + ((info >> 8) & 0xfffff) |
| 820 | : info & 0xfffffff); |
| 821 | if (blksz > max) |
| 822 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 823 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 824 | blk = htx_add_blk(htx, type, blksz); |
| 825 | if (!blk) |
| 826 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 827 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 828 | blk->info = info; |
| 829 | total = 4; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 830 | ptr = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 831 | while (blksz) { |
| 832 | max = MIN(blksz, shctx->block_size - offset); |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 833 | memcpy(ptr, (const char *)shblk->data + offset, max); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 834 | offset += max; |
| 835 | blksz -= max; |
| 836 | total += max; |
Christopher Faulet | 15a4ce8 | 2019-09-03 22:11:52 +0200 | [diff] [blame] | 837 | ptr += max; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 838 | if (blksz || offset == shctx->block_size) { |
| 839 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 840 | offset = 0; |
| 841 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 842 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 843 | appctx->ctx.cache.offset = offset; |
| 844 | appctx->ctx.cache.next = shblk; |
| 845 | appctx->ctx.cache.sent += total; |
| 846 | return total; |
| 847 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 848 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 849 | static unsigned int htx_cache_dump_data_blk(struct appctx *appctx, struct htx *htx, |
| 850 | uint32_t info, struct shared_block *shblk, unsigned int offset) |
| 851 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 852 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 853 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 854 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
| 855 | unsigned int max, total, rem_data; |
| 856 | uint32_t blksz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 857 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 858 | max = htx_get_max_blksz(htx, channel_htx_recv_max(si_ic(appctx->owner), htx)); |
| 859 | if (!max) |
| 860 | return 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 861 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 862 | rem_data = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 863 | if (appctx->ctx.cache.rem_data) { |
| 864 | blksz = appctx->ctx.cache.rem_data; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 865 | total = 0; |
Christopher Faulet | bda8397 | 2019-06-11 09:58:09 +0200 | [diff] [blame] | 866 | } |
| 867 | else { |
| 868 | blksz = (info & 0xfffffff); |
| 869 | total = 4; |
| 870 | } |
| 871 | if (blksz > max) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 872 | rem_data = blksz - max; |
| 873 | blksz = max; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 876 | while (blksz) { |
| 877 | size_t sz; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 878 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 879 | max = MIN(blksz, shctx->block_size - offset); |
| 880 | sz = htx_add_data(htx, ist2(shblk->data + offset, max)); |
| 881 | offset += sz; |
| 882 | blksz -= sz; |
| 883 | total += sz; |
| 884 | if (sz < max) |
| 885 | break; |
| 886 | if (blksz || offset == shctx->block_size) { |
| 887 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 888 | offset = 0; |
| 889 | } |
| 890 | } |
| 891 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 892 | appctx->ctx.cache.offset = offset; |
| 893 | appctx->ctx.cache.next = shblk; |
| 894 | appctx->ctx.cache.sent += total; |
| 895 | appctx->ctx.cache.rem_data = rem_data + blksz; |
| 896 | return total; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 897 | } |
| 898 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 899 | static size_t htx_cache_dump_msg(struct appctx *appctx, struct htx *htx, unsigned int len, |
| 900 | enum htx_blk_type mark) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 901 | { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 902 | struct cache_flt_conf *cconf = appctx->rule->arg.act.p[0]; |
| 903 | struct shared_context *shctx = shctx_ptr(cconf->c.cache); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 904 | struct shared_block *shblk; |
| 905 | unsigned int offset, sz; |
| 906 | unsigned int ret, total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 907 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 908 | while (len) { |
| 909 | enum htx_blk_type type; |
| 910 | uint32_t info; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 911 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 912 | shblk = appctx->ctx.cache.next; |
| 913 | offset = appctx->ctx.cache.offset; |
| 914 | if (appctx->ctx.cache.rem_data) { |
| 915 | type = HTX_BLK_DATA; |
| 916 | info = 0; |
| 917 | goto add_data_blk; |
| 918 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 919 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 920 | /* Get info of the next HTX block. May be split on 2 shblk */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 921 | sz = MIN(4, shctx->block_size - offset); |
| 922 | memcpy((char *)&info, (const char *)shblk->data + offset, sz); |
| 923 | offset += sz; |
| 924 | if (sz < 4) { |
| 925 | shblk = LIST_NEXT(&shblk->list, typeof(shblk), list); |
| 926 | memcpy(((char *)&info)+sz, (const char *)shblk->data, 4 - sz); |
| 927 | offset = (4 - sz); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 928 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 929 | |
| 930 | /* Get payload of the next HTX block and insert it. */ |
| 931 | type = (info >> 28); |
| 932 | if (type != HTX_BLK_DATA) |
| 933 | ret = htx_cache_dump_blk(appctx, htx, type, info, shblk, offset); |
| 934 | else { |
| 935 | add_data_blk: |
| 936 | ret = htx_cache_dump_data_blk(appctx, htx, info, shblk, offset); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 937 | } |
| 938 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 939 | if (!ret) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 940 | break; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 941 | total += ret; |
| 942 | len -= ret; |
| 943 | |
| 944 | if (appctx->ctx.cache.rem_data || type == mark) |
| 945 | break; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 946 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 947 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 948 | return total; |
| 949 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 950 | |
| 951 | static int htx_cache_add_age_hdr(struct appctx *appctx, struct htx *htx) |
| 952 | { |
| 953 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 954 | unsigned int age; |
| 955 | char *end; |
| 956 | |
| 957 | chunk_reset(&trash); |
| 958 | age = MAX(0, (int)(now.tv_sec - cache_ptr->latest_validation)) + cache_ptr->age; |
| 959 | if (unlikely(age > CACHE_ENTRY_MAX_AGE)) |
| 960 | age = CACHE_ENTRY_MAX_AGE; |
| 961 | end = ultoa_o(age, b_head(&trash), b_size(&trash)); |
| 962 | b_set_data(&trash, end - b_head(&trash)); |
| 963 | if (!http_add_header(htx, ist("Age"), ist2(b_head(&trash), b_data(&trash)))) |
| 964 | return 0; |
| 965 | return 1; |
| 966 | } |
| 967 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 968 | static void http_cache_io_handler(struct appctx *appctx) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 969 | { |
| 970 | struct cache_entry *cache_ptr = appctx->ctx.cache.entry; |
| 971 | struct shared_block *first = block_ptr(cache_ptr); |
| 972 | struct stream_interface *si = appctx->owner; |
| 973 | struct channel *req = si_oc(si); |
| 974 | struct channel *res = si_ic(si); |
| 975 | struct htx *req_htx, *res_htx; |
| 976 | struct buffer *errmsg; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 977 | unsigned int len; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 978 | size_t ret, total = 0; |
| 979 | |
| 980 | res_htx = htxbuf(&res->buf); |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 981 | total = res_htx->data; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 982 | |
| 983 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 984 | goto out; |
| 985 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 986 | /* Check if the input buffer is available. */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 987 | if (!b_size(&res->buf)) { |
| 988 | si_rx_room_blk(si); |
| 989 | goto out; |
| 990 | } |
| 991 | |
Willy Tarreau | efef323 | 2018-12-16 00:37:45 +0100 | [diff] [blame] | 992 | if (res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTW_NOW)) |
Willy Tarreau | 273e964 | 2018-12-16 00:35:15 +0100 | [diff] [blame] | 993 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 994 | |
| 995 | if (appctx->st0 == HTX_CACHE_INIT) { |
| 996 | appctx->ctx.cache.next = block_ptr(cache_ptr); |
| 997 | appctx->ctx.cache.offset = sizeof(*cache_ptr); |
| 998 | appctx->ctx.cache.sent = 0; |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 999 | appctx->ctx.cache.rem_data = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1000 | appctx->st0 = HTX_CACHE_HEADER; |
| 1001 | } |
| 1002 | |
| 1003 | if (appctx->st0 == HTX_CACHE_HEADER) { |
| 1004 | /* Headers must be dump at once. Otherwise it is an error */ |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1005 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1006 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOH); |
| 1007 | if (!ret || (htx_get_tail_type(res_htx) != HTX_BLK_EOH) || |
| 1008 | !htx_cache_add_age_hdr(appctx, res_htx)) |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1009 | goto error; |
| 1010 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1011 | /* In case of a conditional request, we might want to send a |
| 1012 | * "304 Not Modified" response instead of the stored data. */ |
Tim Duesterhus | e014234 | 2020-10-22 21:15:06 +0200 | [diff] [blame] | 1013 | if (appctx->ctx.cache.send_notmodified) { |
| 1014 | if (!http_replace_res_status(res_htx, ist("304"), ist("Not Modified"))) { |
| 1015 | /* If replacing the status code fails we need to send the full response. */ |
| 1016 | appctx->ctx.cache.send_notmodified = 0; |
| 1017 | } |
| 1018 | } |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1019 | |
| 1020 | /* Skip response body for HEAD requests or in case of "304 Not |
| 1021 | * Modified" response. */ |
| 1022 | if (si_strm(si)->txn->meth == HTTP_METH_HEAD || appctx->ctx.cache.send_notmodified) |
Christopher Faulet | f0dd037 | 2019-02-25 11:08:34 +0100 | [diff] [blame] | 1023 | appctx->st0 = HTX_CACHE_EOM; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1024 | else |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1025 | appctx->st0 = HTX_CACHE_DATA; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | if (appctx->st0 == HTX_CACHE_DATA) { |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1029 | len = first->len - sizeof(*cache_ptr) - appctx->ctx.cache.sent; |
| 1030 | if (len) { |
| 1031 | ret = htx_cache_dump_msg(appctx, res_htx, len, HTX_BLK_EOM); |
| 1032 | if (ret < len) { |
| 1033 | si_rx_room_blk(si); |
| 1034 | goto out; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1035 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1036 | } |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1037 | appctx->st0 = HTX_CACHE_END; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | if (appctx->st0 == HTX_CACHE_EOM) { |
Christopher Faulet | 810df06 | 2020-07-22 16:20:34 +0200 | [diff] [blame] | 1041 | res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1042 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 1043 | si_rx_room_blk(si); |
| 1044 | goto out; |
| 1045 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1046 | appctx->st0 = HTX_CACHE_END; |
| 1047 | } |
| 1048 | |
| 1049 | end: |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1050 | if (!(res->flags & CF_SHUTR) && appctx->st0 == HTX_CACHE_END) { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1051 | res->flags |= CF_READ_NULL; |
| 1052 | si_shutr(si); |
| 1053 | } |
| 1054 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1055 | out: |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1056 | total = res_htx->data - total; |
Christopher Faulet | 6112391 | 2019-01-02 14:10:01 +0100 | [diff] [blame] | 1057 | if (total) |
| 1058 | channel_add_input(res, total); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1059 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | adb3631 | 2019-02-25 11:40:49 +0100 | [diff] [blame] | 1060 | |
| 1061 | /* eat the whole request */ |
| 1062 | if (co_data(req)) { |
| 1063 | req_htx = htx_from_buf(&req->buf); |
| 1064 | co_htx_skip(req, req_htx, co_data(req)); |
| 1065 | htx_to_buf(req_htx, &req->buf); |
| 1066 | } |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1067 | return; |
| 1068 | |
| 1069 | error: |
| 1070 | /* Sent and HTTP error 500 */ |
| 1071 | b_reset(&res->buf); |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 1072 | errmsg = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1073 | res->buf.data = b_data(errmsg); |
| 1074 | memcpy(res->buf.area, b_head(errmsg), b_data(errmsg)); |
| 1075 | res_htx = htx_from_buf(&res->buf); |
| 1076 | |
Christopher Faulet | 8f3c256 | 2019-06-03 22:19:18 +0200 | [diff] [blame] | 1077 | total = 0; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1078 | appctx->st0 = HTX_CACHE_END; |
| 1079 | goto end; |
| 1080 | } |
| 1081 | |
| 1082 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1083 | static int parse_cache_rule(struct proxy *proxy, const char *name, struct act_rule *rule, char **err) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1084 | { |
| 1085 | struct flt_conf *fconf; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1086 | struct cache_flt_conf *cconf = NULL; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1087 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1088 | if (!*name || strcmp(name, "if") == 0 || strcmp(name, "unless") == 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1089 | memprintf(err, "expects a cache name"); |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1090 | goto err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /* check if a cache filter was already registered with this cache |
| 1094 | * name, if that's the case, must use it. */ |
| 1095 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1096 | if (fconf->id == cache_store_flt_id) { |
| 1097 | cconf = fconf->conf; |
| 1098 | if (cconf && !strcmp((char *)cconf->c.name, name)) { |
| 1099 | rule->arg.act.p[0] = cconf; |
| 1100 | return 1; |
| 1101 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1105 | /* Create the filter cache config */ |
| 1106 | cconf = calloc(1, sizeof(*cconf)); |
| 1107 | if (!cconf) { |
| 1108 | memprintf(err, "out of memory\n"); |
| 1109 | goto err; |
| 1110 | } |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1111 | cconf->flags = CACHE_FLT_F_IMPLICIT_DECL; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1112 | cconf->c.name = strdup(name); |
| 1113 | if (!cconf->c.name) { |
| 1114 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1115 | goto err; |
| 1116 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1117 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1118 | /* register a filter to fill the cache buffer */ |
| 1119 | fconf = calloc(1, sizeof(*fconf)); |
| 1120 | if (!fconf) { |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1121 | memprintf(err, "out of memory\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1122 | goto err; |
| 1123 | } |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1124 | fconf->id = cache_store_flt_id; |
| 1125 | fconf->conf = cconf; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1126 | fconf->ops = &cache_ops; |
| 1127 | LIST_ADDQ(&proxy->filter_configs, &fconf->list); |
| 1128 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1129 | rule->arg.act.p[0] = cconf; |
| 1130 | return 1; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1131 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1132 | err: |
| 1133 | free(cconf); |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | enum act_parse_ret parse_cache_store(const char **args, int *orig_arg, struct proxy *proxy, |
| 1138 | struct act_rule *rule, char **err) |
| 1139 | { |
| 1140 | rule->action = ACT_CUSTOM; |
| 1141 | rule->action_ptr = http_action_store_cache; |
| 1142 | |
| 1143 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
| 1144 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1145 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1146 | (*orig_arg)++; |
| 1147 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1150 | /* This produces a sha1 hash of the concatenation of the HTTP method, |
| 1151 | * the first occurrence of the Host header followed by the path component |
| 1152 | * if it begins with a slash ('/'). */ |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1153 | int sha1_hosturi(struct stream *s) |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1154 | { |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1155 | struct http_txn *txn = s->txn; |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1156 | struct htx *htx = htxbuf(&s->req.buf); |
| 1157 | struct htx_sl *sl; |
| 1158 | struct http_hdr_ctx ctx; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1159 | struct ist uri; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1160 | blk_SHA_CTX sha1_ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1161 | struct buffer *trash; |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1162 | |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1163 | trash = get_trash_chunk(); |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1164 | ctx.blk = NULL; |
Baptiste Assmann | db92a83 | 2019-08-05 16:55:32 +0200 | [diff] [blame] | 1165 | |
| 1166 | switch (txn->meth) { |
| 1167 | case HTTP_METH_HEAD: |
| 1168 | case HTTP_METH_GET: |
| 1169 | chunk_memcat(trash, "GET", 3); |
| 1170 | break; |
| 1171 | default: |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1175 | sl = http_get_stline(htx); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1176 | uri = htx_sl_req_uri(sl); // whole uri |
| 1177 | if (!uri.len) |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1178 | return 0; |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1179 | |
| 1180 | /* In HTTP/1, most URIs are seen in origin form ('/path/to/resource'), |
| 1181 | * unless haproxy is deployed in front of an outbound cache. In HTTP/2, |
| 1182 | * URIs are almost always sent in absolute form with their scheme. In |
| 1183 | * this case, the scheme is almost always "https". In order to support |
| 1184 | * sharing of cache objects between H1 and H2, we'll hash the absolute |
| 1185 | * URI whenever known, or prepend "https://" + the Host header for |
| 1186 | * relative URIs. The difference will only appear on absolute HTTP/1 |
| 1187 | * requests sent to an origin server, which practically is never met in |
| 1188 | * the real world so we don't care about the ability to share the same |
| 1189 | * key here.URIs are normalized from the absolute URI to an origin form as |
| 1190 | * well. |
| 1191 | */ |
| 1192 | if (!(sl->flags & HTX_SL_F_HAS_AUTHORITY)) { |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1193 | chunk_istcat(trash, ist("https://")); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1194 | if (!http_find_header(htx, ist("Host"), &ctx, 0)) |
| 1195 | return 0; |
Willy Tarreau | 20020ae | 2019-10-29 13:02:15 +0100 | [diff] [blame] | 1196 | chunk_istcat(trash, ctx.value); |
Willy Tarreau | ccc61d8 | 2019-10-17 09:28:28 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | chunk_memcat(trash, uri.ptr, uri.len); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1200 | |
| 1201 | /* hash everything */ |
| 1202 | blk_SHA1_Init(&sha1_ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1203 | blk_SHA1_Update(&sha1_ctx, trash->area, trash->data); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1204 | blk_SHA1_Final((unsigned char *)txn->cache_hash, &sha1_ctx); |
| 1205 | |
| 1206 | return 1; |
| 1207 | } |
| 1208 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1209 | /* Looks for "If-None-Match" headers in the request and compares their value |
| 1210 | * with the one that might have been stored in the cache_entry. If any of them |
| 1211 | * matches, a "304 Not Modified" response should be sent instead of the cached |
| 1212 | * data. |
| 1213 | * Although unlikely in a GET/HEAD request, the "If-None-Match: *" syntax is |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1214 | * valid and should receive a "304 Not Modified" response (RFC 7234#4.3.2). |
| 1215 | * |
| 1216 | * If no "If-None-Match" header was found, look for an "If-Modified-Since" |
| 1217 | * header and compare its value (date) to the one stored in the cache_entry. |
| 1218 | * If the request's date is later than the cached one, we also send a |
| 1219 | * "304 Not Modified" response (see RFCs 7232#3.3 and 7234#4.3.2). |
| 1220 | * |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1221 | * Returns 1 if "304 Not Modified" should be sent, 0 otherwise. |
| 1222 | */ |
| 1223 | static int should_send_notmodified_response(struct cache *cache, struct htx *htx, |
| 1224 | struct cache_entry *entry) |
| 1225 | { |
| 1226 | int retval = 0; |
| 1227 | |
| 1228 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 1229 | struct ist cache_entry_etag = IST_NULL; |
| 1230 | struct buffer *etag_buffer = NULL; |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1231 | int if_none_match_found = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1232 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1233 | struct tm tm = {}; |
| 1234 | time_t if_modified_since = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1235 | |
| 1236 | /* If we find a "If-None-Match" header in the request, rebuild the |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1237 | * cache_entry's ETag in order to perform comparisons. |
| 1238 | * There could be multiple "if-none-match" header lines. */ |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1239 | while (http_find_header(htx, ist("if-none-match"), &ctx, 0)) { |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1240 | if_none_match_found = 1; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1241 | |
| 1242 | /* A '*' matches everything. */ |
| 1243 | if (isteq(ctx.value, ist("*")) != 0) { |
| 1244 | retval = 1; |
| 1245 | break; |
| 1246 | } |
| 1247 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1248 | /* No need to rebuild an etag if none was stored in the cache. */ |
| 1249 | if (entry->etag_length == 0) |
| 1250 | break; |
| 1251 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1252 | /* Rebuild the stored ETag. */ |
| 1253 | if (etag_buffer == NULL) { |
| 1254 | etag_buffer = get_trash_chunk(); |
| 1255 | |
| 1256 | if (shctx_row_data_get(shctx_ptr(cache), block_ptr(entry), |
| 1257 | (unsigned char*)b_orig(etag_buffer), |
| 1258 | entry->etag_offset, entry->etag_length) == 0) { |
| 1259 | cache_entry_etag = ist2(b_orig(etag_buffer), entry->etag_length); |
| 1260 | } else { |
| 1261 | /* We could not rebuild the ETag in one go, we |
| 1262 | * won't send a "304 Not Modified" response. */ |
| 1263 | break; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | if (http_compare_etags(cache_entry_etag, ctx.value) == 1) { |
| 1268 | retval = 1; |
| 1269 | break; |
| 1270 | } |
| 1271 | } |
| 1272 | |
Remi Tricot-Le Breton | 53161d8 | 2020-10-23 10:51:28 +0200 | [diff] [blame] | 1273 | /* If the request did not contain an "If-None-Match" header, we look for |
| 1274 | * an "If-Modified-Since" header (see RFC 7232#3.3). */ |
| 1275 | if (retval == 0 && if_none_match_found == 0) { |
| 1276 | ctx.blk = NULL; |
| 1277 | if (http_find_header(htx, ist("if-modified-since"), &ctx, 1)) { |
| 1278 | if (parse_http_date(istptr(ctx.value), istlen(ctx.value), &tm)) { |
| 1279 | if_modified_since = my_timegm(&tm); |
| 1280 | |
| 1281 | /* We send a "304 Not Modified" response if the |
| 1282 | * entry's last modified date is earlier than |
| 1283 | * the one found in the "If-Modified-Since" |
| 1284 | * header. */ |
| 1285 | retval = (entry->last_modified <= if_modified_since); |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1290 | return retval; |
| 1291 | } |
| 1292 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1293 | enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *px, |
| 1294 | struct session *sess, struct stream *s, int flags) |
| 1295 | { |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1296 | |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1297 | struct http_txn *txn = s->txn; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1298 | struct cache_entry *res; |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1299 | struct cache_flt_conf *cconf = rule->arg.act.p[0]; |
| 1300 | struct cache *cache = cconf->c.cache; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1301 | |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1302 | /* Ignore cache for HTTP/1.0 requests and for requests other than GET |
| 1303 | * and HEAD */ |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1304 | if (!(txn->req.flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 6905d18 | 2019-10-01 17:59:17 +0200 | [diff] [blame] | 1305 | (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD)) |
Christopher Faulet | b3d4bca | 2019-02-25 10:59:33 +0100 | [diff] [blame] | 1306 | txn->flags |= TX_CACHE_IGNORE; |
| 1307 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1308 | http_check_request_for_cacheability(s, &s->req); |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1309 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1310 | if ((s->txn->flags & (TX_CACHE_IGNORE|TX_CACHEABLE)) == TX_CACHE_IGNORE) |
| 1311 | return ACT_RET_CONT; |
| 1312 | |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1313 | if (!sha1_hosturi(s)) |
Willy Tarreau | 7704b1e | 2017-12-22 16:32:43 +0100 | [diff] [blame] | 1314 | return ACT_RET_CONT; |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1315 | |
Willy Tarreau | 504455c | 2017-12-22 17:47:35 +0100 | [diff] [blame] | 1316 | if (s->txn->flags & TX_CACHE_IGNORE) |
| 1317 | return ACT_RET_CONT; |
| 1318 | |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1319 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1320 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1321 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1322 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_lookups, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1323 | |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1324 | shctx_lock(shctx_ptr(cache)); |
William Lallemand | f528fff | 2017-11-23 19:43:17 +0100 | [diff] [blame] | 1325 | res = entry_exist(cache, s->txn->cache_hash); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1326 | if (res) { |
| 1327 | struct appctx *appctx; |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1328 | shctx_row_inc_hot(shctx_ptr(cache), block_ptr(res)); |
| 1329 | shctx_unlock(shctx_ptr(cache)); |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1330 | s->target = &http_cache_applet.obj_type; |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1331 | if ((appctx = si_register_handler(&s->si[1], objt_applet(s->target)))) { |
Christopher Faulet | 95e7ea3 | 2019-07-15 21:01:29 +0200 | [diff] [blame] | 1332 | appctx->st0 = HTX_CACHE_INIT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1333 | appctx->rule = rule; |
| 1334 | appctx->ctx.cache.entry = res; |
Frédéric Lécaille | 8df65ae | 2018-10-22 18:01:48 +0200 | [diff] [blame] | 1335 | appctx->ctx.cache.next = NULL; |
| 1336 | appctx->ctx.cache.sent = 0; |
Remi Tricot-Le Breton | 6cb1038 | 2020-10-22 10:40:05 +0200 | [diff] [blame] | 1337 | appctx->ctx.cache.send_notmodified = |
| 1338 | should_send_notmodified_response(cache, htxbuf(&s->req.buf), res); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1339 | |
| 1340 | if (px == strm_fe(s)) |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1341 | _HA_ATOMIC_ADD(&px->fe_counters.p.http.cache_hits, 1); |
Willy Tarreau | a1214a5 | 2018-12-14 14:00:25 +0100 | [diff] [blame] | 1342 | else |
Olivier Houchard | aa090d4 | 2019-03-08 18:49:24 +0100 | [diff] [blame] | 1343 | _HA_ATOMIC_ADD(&px->be_counters.p.http.cache_hits, 1); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1344 | return ACT_RET_CONT; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1345 | } else { |
William Lallemand | 55e7674 | 2017-11-21 20:01:28 +0100 | [diff] [blame] | 1346 | shctx_lock(shctx_ptr(cache)); |
| 1347 | shctx_row_dec_hot(shctx_ptr(cache), block_ptr(res)); |
| 1348 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1349 | return ACT_RET_YIELD; |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1350 | } |
| 1351 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1352 | shctx_unlock(shctx_ptr(cache)); |
Olivier Houchard | fccf840 | 2017-11-01 14:04:02 +0100 | [diff] [blame] | 1353 | return ACT_RET_CONT; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | enum act_parse_ret parse_cache_use(const char **args, int *orig_arg, struct proxy *proxy, |
| 1358 | struct act_rule *rule, char **err) |
| 1359 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1360 | rule->action = ACT_CUSTOM; |
| 1361 | rule->action_ptr = http_action_req_cache_use; |
| 1362 | |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1363 | if (!parse_cache_rule(proxy, args[*orig_arg], rule, err)) |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1364 | return ACT_RET_PRS_ERR; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1365 | |
| 1366 | (*orig_arg)++; |
| 1367 | return ACT_RET_PRS_OK; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | int cfg_parse_cache(const char *file, int linenum, char **args, int kwm) |
| 1371 | { |
| 1372 | int err_code = 0; |
| 1373 | |
| 1374 | if (strcmp(args[0], "cache") == 0) { /* new cache section */ |
| 1375 | |
| 1376 | if (!*args[1]) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1377 | ha_alert("parsing [%s:%d] : '%s' expects a <name> argument\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1378 | file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1379 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1380 | goto out; |
| 1381 | } |
| 1382 | |
| 1383 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1384 | err_code |= ERR_ABORT; |
| 1385 | goto out; |
| 1386 | } |
| 1387 | |
| 1388 | if (tmp_cache_config == NULL) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1389 | struct cache *cache_config; |
| 1390 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1391 | tmp_cache_config = calloc(1, sizeof(*tmp_cache_config)); |
| 1392 | if (!tmp_cache_config) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1393 | ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1394 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1395 | goto out; |
| 1396 | } |
| 1397 | |
| 1398 | strlcpy2(tmp_cache_config->id, args[1], 33); |
| 1399 | if (strlen(args[1]) > 32) { |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1400 | ha_warning("parsing [%s:%d]: cache name is limited to 32 characters, truncate to '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1401 | file, linenum, tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1402 | err_code |= ERR_WARN; |
| 1403 | } |
Tim Duesterhus | ff4d86b | 2020-08-18 22:20:27 +0200 | [diff] [blame] | 1404 | |
| 1405 | list_for_each_entry(cache_config, &caches_config, list) { |
| 1406 | if (strcmp(tmp_cache_config->id, cache_config->id) == 0) { |
| 1407 | ha_alert("parsing [%s:%d]: Duplicate cache name '%s'.\n", |
| 1408 | file, linenum, tmp_cache_config->id); |
| 1409 | err_code |= ERR_ALERT | ERR_ABORT; |
| 1410 | goto out; |
| 1411 | } |
| 1412 | } |
| 1413 | |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1414 | tmp_cache_config->maxage = 60; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1415 | tmp_cache_config->maxblocks = 0; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1416 | tmp_cache_config->maxobjsz = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1417 | } |
| 1418 | } else if (strcmp(args[0], "total-max-size") == 0) { |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1419 | unsigned long int maxsize; |
| 1420 | char *err; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1421 | |
| 1422 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1423 | err_code |= ERR_ABORT; |
| 1424 | goto out; |
| 1425 | } |
| 1426 | |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1427 | maxsize = strtoul(args[1], &err, 10); |
| 1428 | if (err == args[1] || *err != '\0') { |
| 1429 | ha_warning("parsing [%s:%d]: total-max-size wrong value '%s'\n", |
| 1430 | file, linenum, args[1]); |
| 1431 | err_code |= ERR_ABORT; |
| 1432 | goto out; |
| 1433 | } |
| 1434 | |
| 1435 | if (maxsize > (UINT_MAX >> 20)) { |
| 1436 | ha_warning("parsing [%s:%d]: \"total-max-size\" (%s) must not be greater than %u\n", |
| 1437 | file, linenum, args[1], UINT_MAX >> 20); |
| 1438 | err_code |= ERR_ABORT; |
| 1439 | goto out; |
| 1440 | } |
| 1441 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1442 | /* size in megabytes */ |
Frédéric Lécaille | b9b8b6b | 2018-10-25 20:17:45 +0200 | [diff] [blame] | 1443 | maxsize *= 1024 * 1024 / CACHE_BLOCKSIZE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1444 | tmp_cache_config->maxblocks = maxsize; |
William Lallemand | 49b4453 | 2017-11-24 18:53:43 +0100 | [diff] [blame] | 1445 | } else if (strcmp(args[0], "max-age") == 0) { |
| 1446 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1447 | err_code |= ERR_ABORT; |
| 1448 | goto out; |
| 1449 | } |
| 1450 | |
| 1451 | if (!*args[1]) { |
| 1452 | ha_warning("parsing [%s:%d]: '%s' expects an age parameter in seconds.\n", |
| 1453 | file, linenum, args[0]); |
| 1454 | err_code |= ERR_WARN; |
| 1455 | } |
| 1456 | |
| 1457 | tmp_cache_config->maxage = atoi(args[1]); |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1458 | } else if (strcmp(args[0], "max-object-size") == 0) { |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1459 | unsigned int maxobjsz; |
| 1460 | char *err; |
| 1461 | |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1462 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 1463 | err_code |= ERR_ABORT; |
| 1464 | goto out; |
| 1465 | } |
| 1466 | |
| 1467 | if (!*args[1]) { |
| 1468 | ha_warning("parsing [%s:%d]: '%s' expects a maximum file size parameter in bytes.\n", |
| 1469 | file, linenum, args[0]); |
| 1470 | err_code |= ERR_WARN; |
| 1471 | } |
| 1472 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1473 | maxobjsz = strtoul(args[1], &err, 10); |
| 1474 | if (err == args[1] || *err != '\0') { |
| 1475 | ha_warning("parsing [%s:%d]: max-object-size wrong value '%s'\n", |
| 1476 | file, linenum, args[1]); |
| 1477 | err_code |= ERR_ABORT; |
| 1478 | goto out; |
| 1479 | } |
| 1480 | tmp_cache_config->maxobjsz = maxobjsz; |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1481 | } |
| 1482 | else if (*args[0] != 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1483 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in 'cache' section\n", file, linenum, args[0]); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1484 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1485 | goto out; |
| 1486 | } |
| 1487 | out: |
| 1488 | return err_code; |
| 1489 | } |
| 1490 | |
| 1491 | /* once the cache section is parsed */ |
| 1492 | |
| 1493 | int cfg_post_parse_section_cache() |
| 1494 | { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1495 | int err_code = 0; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1496 | |
| 1497 | if (tmp_cache_config) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1498 | |
| 1499 | if (tmp_cache_config->maxblocks <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1500 | ha_alert("Size not specified for cache '%s'\n", tmp_cache_config->id); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1501 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1502 | goto out; |
| 1503 | } |
| 1504 | |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1505 | if (!tmp_cache_config->maxobjsz) { |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1506 | /* Default max. file size is a 256th of the cache size. */ |
| 1507 | tmp_cache_config->maxobjsz = |
| 1508 | (tmp_cache_config->maxblocks * CACHE_BLOCKSIZE) >> 8; |
Frédéric Lécaille | 4eba544 | 2018-10-25 20:29:31 +0200 | [diff] [blame] | 1509 | } |
| 1510 | else if (tmp_cache_config->maxobjsz > tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2) { |
| 1511 | ha_alert("\"max-object-size\" is limited to an half of \"total-max-size\" => %u\n", tmp_cache_config->maxblocks * CACHE_BLOCKSIZE / 2); |
| 1512 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1513 | goto out; |
| 1514 | } |
Frédéric Lécaille | a2219f5 | 2018-10-22 16:59:13 +0200 | [diff] [blame] | 1515 | |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1516 | /* add to the list of cache to init and reinit tmp_cache_config |
| 1517 | * for next cache section, if any. |
| 1518 | */ |
| 1519 | LIST_ADDQ(&caches_config, &tmp_cache_config->list); |
| 1520 | tmp_cache_config = NULL; |
| 1521 | return err_code; |
| 1522 | } |
| 1523 | out: |
| 1524 | free(tmp_cache_config); |
| 1525 | tmp_cache_config = NULL; |
| 1526 | return err_code; |
| 1527 | |
| 1528 | } |
| 1529 | |
| 1530 | int post_check_cache() |
| 1531 | { |
| 1532 | struct proxy *px; |
| 1533 | struct cache *back, *cache_config, *cache; |
| 1534 | struct shared_context *shctx; |
| 1535 | int ret_shctx; |
| 1536 | int err_code = 0; |
| 1537 | |
| 1538 | list_for_each_entry_safe(cache_config, back, &caches_config, list) { |
| 1539 | |
| 1540 | ret_shctx = shctx_init(&shctx, cache_config->maxblocks, CACHE_BLOCKSIZE, |
| 1541 | cache_config->maxobjsz, sizeof(struct cache), 1); |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1542 | |
Frédéric Lécaille | bc58449 | 2018-10-25 20:18:59 +0200 | [diff] [blame] | 1543 | if (ret_shctx <= 0) { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1544 | if (ret_shctx == SHCTX_E_INIT_LOCK) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1545 | ha_alert("Unable to initialize the lock for the cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1546 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1547 | ha_alert("Unable to allocate cache.\n"); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1548 | |
| 1549 | err_code |= ERR_FATAL | ERR_ALERT; |
| 1550 | goto out; |
| 1551 | } |
William Lallemand | a400a3a | 2017-11-20 19:13:12 +0100 | [diff] [blame] | 1552 | shctx->free_block = cache_free_blocks; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1553 | /* the cache structure is stored in the shctx and added to the |
| 1554 | * caches list, we can remove the entry from the caches_config |
| 1555 | * list */ |
| 1556 | memcpy(shctx->data, cache_config, sizeof(struct cache)); |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1557 | cache = (struct cache *)shctx->data; |
| 1558 | cache->entries = EB_ROOT_UNIQUE; |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1559 | LIST_ADDQ(&caches, &cache->list); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1560 | LIST_DEL(&cache_config->list); |
| 1561 | free(cache_config); |
| 1562 | |
| 1563 | /* Find all references for this cache in the existing filters |
| 1564 | * (over all proxies) and reference it in matching filters. |
| 1565 | */ |
| 1566 | for (px = proxies_list; px; px = px->next) { |
| 1567 | struct flt_conf *fconf; |
| 1568 | struct cache_flt_conf *cconf; |
| 1569 | |
| 1570 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 1571 | if (fconf->id != cache_store_flt_id) |
| 1572 | continue; |
| 1573 | |
| 1574 | cconf = fconf->conf; |
| 1575 | if (!strcmp(cache->id, cconf->c.name)) { |
| 1576 | free(cconf->c.name); |
Tim Duesterhus | d7c6e6a | 2020-09-14 18:01:33 +0200 | [diff] [blame] | 1577 | cconf->flags |= CACHE_FLT_INIT; |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1578 | cconf->c.cache = cache; |
| 1579 | break; |
| 1580 | } |
| 1581 | } |
| 1582 | } |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1583 | } |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1584 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1585 | out: |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1586 | return err_code; |
| 1587 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1588 | } |
| 1589 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1590 | struct flt_ops cache_ops = { |
| 1591 | .init = cache_store_init, |
Christopher Faulet | 95220e2 | 2018-12-07 17:34:39 +0100 | [diff] [blame] | 1592 | .check = cache_store_check, |
| 1593 | .deinit = cache_store_deinit, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1594 | |
Christopher Faulet | 65554e1 | 2020-03-06 14:52:06 +0100 | [diff] [blame] | 1595 | /* Handle stream init/deinit */ |
| 1596 | .attach = cache_store_strm_init, |
| 1597 | .detach = cache_store_strm_deinit, |
| 1598 | |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1599 | /* Handle channels activity */ |
Christopher Faulet | 839791a | 2019-01-07 16:12:07 +0100 | [diff] [blame] | 1600 | .channel_post_analyze = cache_store_post_analyze, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1601 | |
| 1602 | /* Filter HTTP requests and responses */ |
| 1603 | .http_headers = cache_store_http_headers, |
Christopher Faulet | 54a8d5a | 2018-12-07 12:21:11 +0100 | [diff] [blame] | 1604 | .http_payload = cache_store_http_payload, |
William Lallemand | 4da3f8a | 2017-10-31 14:33:34 +0100 | [diff] [blame] | 1605 | .http_end = cache_store_http_end, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1606 | }; |
| 1607 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1608 | |
| 1609 | |
| 1610 | static int |
| 1611 | parse_cache_flt(char **args, int *cur_arg, struct proxy *px, |
| 1612 | struct flt_conf *fconf, char **err, void *private) |
| 1613 | { |
| 1614 | struct flt_conf *f, *back; |
Willy Tarreau | a73da1e | 2018-12-14 10:19:28 +0100 | [diff] [blame] | 1615 | struct cache_flt_conf *cconf = NULL; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1616 | char *name = NULL; |
| 1617 | int pos = *cur_arg; |
| 1618 | |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 1619 | /* Get the cache filter name. <pos> point on "cache" keyword */ |
| 1620 | if (!*args[pos + 1]) { |
Tim Duesterhus | ea969f6 | 2020-08-18 22:06:51 +0200 | [diff] [blame] | 1621 | memprintf(err, "%s : expects a <name> argument", args[pos]); |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 1622 | goto error; |
| 1623 | } |
| 1624 | name = strdup(args[pos + 1]); |
| 1625 | if (!name) { |
| 1626 | memprintf(err, "%s '%s' : out of memory", args[pos], args[pos + 1]); |
| 1627 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1628 | } |
Christopher Faulet | 2a37cdb | 2020-05-18 11:58:16 +0200 | [diff] [blame] | 1629 | pos += 2; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1630 | |
| 1631 | /* Check if an implicit filter with the same name already exists. If so, |
| 1632 | * we remove the implicit filter to use the explicit one. */ |
| 1633 | list_for_each_entry_safe(f, back, &px->filter_configs, list) { |
| 1634 | if (f->id != cache_store_flt_id) |
| 1635 | continue; |
| 1636 | |
| 1637 | cconf = f->conf; |
| 1638 | if (strcmp(name, cconf->c.name)) { |
| 1639 | cconf = NULL; |
| 1640 | continue; |
| 1641 | } |
| 1642 | |
| 1643 | if (!(cconf->flags & CACHE_FLT_F_IMPLICIT_DECL)) { |
| 1644 | cconf = NULL; |
| 1645 | memprintf(err, "%s: multiple explicit declarations of the cache filter '%s'", |
| 1646 | px->id, name); |
Tim Duesterhus | d34b1ce | 2020-01-18 01:46:18 +0100 | [diff] [blame] | 1647 | goto error; |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | /* Remove the implicit filter. <cconf> is kept for the explicit one */ |
| 1651 | LIST_DEL(&f->list); |
| 1652 | free(f); |
| 1653 | free(name); |
| 1654 | break; |
| 1655 | } |
| 1656 | |
| 1657 | /* No implicit cache filter found, create configuration for the explicit one */ |
| 1658 | if (!cconf) { |
| 1659 | cconf = calloc(1, sizeof(*cconf)); |
| 1660 | if (!cconf) { |
| 1661 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 1662 | goto error; |
| 1663 | } |
| 1664 | cconf->c.name = name; |
| 1665 | } |
| 1666 | |
| 1667 | cconf->flags = 0; |
| 1668 | fconf->id = cache_store_flt_id; |
| 1669 | fconf->conf = cconf; |
| 1670 | fconf->ops = &cache_ops; |
| 1671 | |
| 1672 | *cur_arg = pos; |
| 1673 | return 0; |
| 1674 | |
| 1675 | error: |
| 1676 | free(name); |
| 1677 | free(cconf); |
| 1678 | return -1; |
| 1679 | } |
| 1680 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 1681 | static int cli_parse_show_cache(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1682 | { |
| 1683 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1684 | return 1; |
| 1685 | |
| 1686 | return 0; |
| 1687 | } |
| 1688 | |
| 1689 | static int cli_io_handler_show_cache(struct appctx *appctx) |
| 1690 | { |
| 1691 | struct cache* cache = appctx->ctx.cli.p0; |
| 1692 | struct stream_interface *si = appctx->owner; |
| 1693 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1694 | if (cache == NULL) { |
| 1695 | cache = LIST_ELEM((caches).n, typeof(struct cache *), list); |
| 1696 | } |
| 1697 | |
| 1698 | list_for_each_entry_from(cache, &caches, list) { |
| 1699 | struct eb32_node *node = NULL; |
| 1700 | unsigned int next_key; |
| 1701 | struct cache_entry *entry; |
| 1702 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1703 | next_key = appctx->ctx.cli.i0; |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1704 | if (!next_key) { |
| 1705 | chunk_printf(&trash, "%p: %s (shctx:%p, available blocks:%d)\n", cache, cache->id, shctx_ptr(cache), shctx_ptr(cache)->nbav); |
| 1706 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1707 | si_rx_room_blk(si); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1708 | return 0; |
| 1709 | } |
| 1710 | } |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1711 | |
| 1712 | appctx->ctx.cli.p0 = cache; |
| 1713 | |
| 1714 | while (1) { |
| 1715 | |
| 1716 | shctx_lock(shctx_ptr(cache)); |
| 1717 | node = eb32_lookup_ge(&cache->entries, next_key); |
| 1718 | if (!node) { |
| 1719 | shctx_unlock(shctx_ptr(cache)); |
Willy Tarreau | afe1de5 | 2018-04-04 11:56:43 +0200 | [diff] [blame] | 1720 | appctx->ctx.cli.i0 = 0; |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1721 | break; |
| 1722 | } |
| 1723 | |
| 1724 | entry = container_of(node, struct cache_entry, eb); |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 1725 | chunk_printf(&trash, "%p hash:%u size:%u (%u blocks), refcount:%u, expire:%d\n", entry, read_u32(entry->hash), block_ptr(entry)->len, block_ptr(entry)->block_count, block_ptr(entry)->refcount, entry->expire - (int)now.tv_sec); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1726 | |
| 1727 | next_key = node->key + 1; |
| 1728 | appctx->ctx.cli.i0 = next_key; |
| 1729 | |
| 1730 | shctx_unlock(shctx_ptr(cache)); |
| 1731 | |
| 1732 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1733 | si_rx_room_blk(si); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1734 | return 0; |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | } |
| 1739 | |
| 1740 | return 1; |
| 1741 | |
| 1742 | } |
| 1743 | |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 1744 | |
| 1745 | /* |
| 1746 | * boolean, returns true if response was built out of a cache entry. |
| 1747 | */ |
| 1748 | static int |
| 1749 | smp_fetch_res_cache_hit(const struct arg *args, struct sample *smp, |
| 1750 | const char *kw, void *private) |
| 1751 | { |
| 1752 | smp->data.type = SMP_T_BOOL; |
| 1753 | smp->data.u.sint = (smp->strm ? (smp->strm->target == &http_cache_applet.obj_type) : 0); |
| 1754 | |
| 1755 | return 1; |
| 1756 | } |
| 1757 | |
| 1758 | /* |
| 1759 | * string, returns cache name (if response came from a cache). |
| 1760 | */ |
| 1761 | static int |
| 1762 | smp_fetch_res_cache_name(const struct arg *args, struct sample *smp, |
| 1763 | const char *kw, void *private) |
| 1764 | { |
| 1765 | struct appctx *appctx = NULL; |
| 1766 | |
| 1767 | struct cache_flt_conf *cconf = NULL; |
| 1768 | struct cache *cache = NULL; |
| 1769 | |
| 1770 | if (!smp->strm || smp->strm->target != &http_cache_applet.obj_type) |
| 1771 | return 0; |
| 1772 | |
| 1773 | /* Get appctx from the stream_interface. */ |
| 1774 | appctx = si_appctx(&smp->strm->si[1]); |
| 1775 | if (appctx && appctx->rule) { |
| 1776 | cconf = appctx->rule->arg.act.p[0]; |
| 1777 | if (cconf) { |
| 1778 | cache = cconf->c.cache; |
| 1779 | |
| 1780 | smp->data.type = SMP_T_STR; |
| 1781 | smp->flags = SMP_F_CONST; |
| 1782 | smp->data.u.str.area = cache->id; |
| 1783 | smp->data.u.str.data = strlen(cache->id); |
| 1784 | return 1; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | return 0; |
| 1789 | } |
| 1790 | |
Christopher Faulet | 99a17a2 | 2018-12-11 09:18:27 +0100 | [diff] [blame] | 1791 | /* Declare the filter parser for "cache" keyword */ |
| 1792 | static struct flt_kw_list filter_kws = { "CACHE", { }, { |
| 1793 | { "cache", parse_cache_flt, NULL }, |
| 1794 | { NULL, NULL, NULL }, |
| 1795 | } |
| 1796 | }; |
| 1797 | |
| 1798 | INITCALL1(STG_REGISTER, flt_register_keywords, &filter_kws); |
| 1799 | |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1800 | static struct cli_kw_list cli_kws = {{},{ |
William Lallemand | e899af8 | 2017-11-22 16:41:26 +0100 | [diff] [blame] | 1801 | { { "show", "cache", NULL }, "show cache : show cache status", cli_parse_show_cache, cli_io_handler_show_cache, NULL, NULL }, |
| 1802 | {{},} |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1803 | }}; |
| 1804 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1805 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 1f49a36 | 2017-11-21 20:01:26 +0100 | [diff] [blame] | 1806 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1807 | static struct action_kw_list http_res_actions = { |
| 1808 | .kw = { |
| 1809 | { "cache-store", parse_cache_store }, |
| 1810 | { NULL, NULL } |
| 1811 | } |
| 1812 | }; |
| 1813 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1814 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
| 1815 | |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1816 | static struct action_kw_list http_req_actions = { |
| 1817 | .kw = { |
| 1818 | { "cache-use", parse_cache_use }, |
| 1819 | { NULL, NULL } |
| 1820 | } |
| 1821 | }; |
| 1822 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1823 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 1824 | |
Willy Tarreau | 2231b63 | 2019-03-29 18:26:52 +0100 | [diff] [blame] | 1825 | struct applet http_cache_applet = { |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1826 | .obj_type = OBJ_TYPE_APPLET, |
| 1827 | .name = "<CACHE>", /* used for logging */ |
William Lallemand | 77c1197 | 2017-10-31 20:43:01 +0100 | [diff] [blame] | 1828 | .fct = http_cache_io_handler, |
William Lallemand | ecb73b1 | 2017-11-24 14:33:55 +0100 | [diff] [blame] | 1829 | .release = http_cache_applet_release, |
William Lallemand | 41db460 | 2017-10-30 11:15:51 +0100 | [diff] [blame] | 1830 | }; |
| 1831 | |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 1832 | /* config parsers for this section */ |
| 1833 | REGISTER_CONFIG_SECTION("cache", cfg_parse_cache, cfg_post_parse_section_cache); |
William Lallemand | d1d1e22 | 2019-08-28 15:22:49 +0200 | [diff] [blame] | 1834 | REGISTER_POST_CHECK(post_check_cache); |
Remi Tricot-Le Breton | bf97121 | 2020-10-27 11:55:57 +0100 | [diff] [blame] | 1835 | |
| 1836 | |
| 1837 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1838 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 1839 | { "res.cache_hit", smp_fetch_res_cache_hit, 0, NULL, SMP_T_BOOL, SMP_USE_HRSHP, SMP_VAL_RESPONSE }, |
| 1840 | { "res.cache_name", smp_fetch_res_cache_name, 0, NULL, SMP_T_STR, SMP_USE_HRSHP, SMP_VAL_RESPONSE }, |
| 1841 | { /* END */ }, |
| 1842 | } |
| 1843 | }; |
| 1844 | |
| 1845 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |