| | varnish-cache/bin/varnishd/cache/cache_vary.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006-2015 Varnish Software AS |
| 2 |
|
* All rights reserved. |
| 3 |
|
* |
| 4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 5 |
|
* |
| 6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 7 |
|
* |
| 8 |
|
* Redistribution and use in source and binary forms, with or without |
| 9 |
|
* modification, are permitted provided that the following conditions |
| 10 |
|
* are met: |
| 11 |
|
* 1. Redistributions of source code must retain the above copyright |
| 12 |
|
* notice, this list of conditions and the following disclaimer. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer in the |
| 15 |
|
* documentation and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
|
* SUCH DAMAGE. |
| 28 |
|
* |
| 29 |
|
* Do Vary processing. |
| 30 |
|
* |
| 31 |
|
* When we insert an object into the cache which has a Vary: header, |
| 32 |
|
* we encode a vary matching string containing the headers mentioned |
| 33 |
|
* and their value. |
| 34 |
|
* |
| 35 |
|
* When we match an object in the cache, we check the present request |
| 36 |
|
* against the vary matching string. |
| 37 |
|
* |
| 38 |
|
* The only kind of header-munging we do is leading & trailing space |
| 39 |
|
* removal. All the potential "q=foo" gymnastics is not worth the |
| 40 |
|
* effort. |
| 41 |
|
* |
| 42 |
|
* The vary matching string has the following format: |
| 43 |
|
* |
| 44 |
|
* Sequence of: { |
| 45 |
|
* <msb> \ Length of header contents. |
| 46 |
|
* <lsb> / |
| 47 |
|
* <length of header + 1> \ |
| 48 |
|
* <header> \ Same format as argument to http_GetHdr() |
| 49 |
|
* ':' / |
| 50 |
|
* '\0' / |
| 51 |
|
* <header> > Only present if length != 0xffff |
| 52 |
|
* } |
| 53 |
|
* 0xff, \ Length field |
| 54 |
|
* 0xff, / |
| 55 |
|
* '\0' > Terminator |
| 56 |
|
*/ |
| 57 |
|
|
| 58 |
|
#include "config.h" |
| 59 |
|
|
| 60 |
|
#include <stdlib.h> |
| 61 |
|
|
| 62 |
|
#include "cache_varnishd.h" |
| 63 |
|
|
| 64 |
|
#include "vct.h" |
| 65 |
|
#include "vend.h" |
| 66 |
|
|
| 67 |
|
static unsigned VRY_Validate(const uint8_t *vary); |
| 68 |
|
|
| 69 |
|
/********************************************************************** |
| 70 |
|
* Create a Vary matching string from a Vary header |
| 71 |
|
* |
| 72 |
|
* Return value: |
| 73 |
|
* <0: Parse error |
| 74 |
|
* 0: No Vary header on object |
| 75 |
|
* >0: Length of Vary matching string in *psb |
| 76 |
|
*/ |
| 77 |
|
|
| 78 |
|
int |
| 79 |
58839 |
VRY_Create(struct busyobj *bo, struct vsb **psb) |
| 80 |
|
{ |
| 81 |
|
const char *v, *p, *q, *h, *e; |
| 82 |
|
struct vsb *sb, *sbh; |
| 83 |
|
hdr_t hdr; |
| 84 |
|
unsigned l; |
| 85 |
58839 |
int error = 0; |
| 86 |
|
|
| 87 |
58839 |
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC); |
| 88 |
58839 |
CHECK_OBJ_NOTNULL(bo->bereq, HTTP_MAGIC); |
| 89 |
58839 |
CHECK_OBJ_NOTNULL(bo->beresp, HTTP_MAGIC); |
| 90 |
58839 |
AN(psb); |
| 91 |
58839 |
AZ(*psb); |
| 92 |
|
|
| 93 |
|
/* No Vary: header, no worries */ |
| 94 |
58839 |
if (!http_GetHdr(bo->beresp, H_Vary, &v)) |
| 95 |
50039 |
return (0); |
| 96 |
|
|
| 97 |
|
/* For vary matching string */ |
| 98 |
8800 |
sb = VSB_new_auto(); |
| 99 |
8800 |
AN(sb); |
| 100 |
|
|
| 101 |
|
/* For header matching strings */ |
| 102 |
8800 |
sbh = VSB_new_auto(); |
| 103 |
8800 |
AN(sbh); |
| 104 |
|
|
| 105 |
9440 |
for (p = v; *p; p++) { |
| 106 |
|
|
| 107 |
|
/* Find next header-name */ |
| 108 |
9440 |
if (vct_issp(*p)) |
| 109 |
320 |
continue; |
| 110 |
121000 |
for (q = p; *q && !vct_issp(*q) && *q != ','; q++) |
| 111 |
111880 |
continue; |
| 112 |
|
|
| 113 |
9120 |
if (q - p > INT8_MAX) { |
| 114 |
80 |
VSLb(bo->vsl, SLT_Error, |
| 115 |
|
"Vary header name length exceeded"); |
| 116 |
80 |
error = 1; |
| 117 |
80 |
break; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
/* Build a header-matching string out of it */ |
| 121 |
9040 |
VSB_clear(sbh); |
| 122 |
9040 |
AZ(VSB_printf(sbh, "%c%.*s:%c", |
| 123 |
|
(char)(1 + (q - p)), (int)(q - p), p, 0)); |
| 124 |
9040 |
AZ(VSB_finish(sbh)); |
| 125 |
9040 |
CAST_HDR(hdr, VSB_data(sbh)); |
| 126 |
|
|
| 127 |
9040 |
if (http_GetHdr(bo->bereq, hdr, &h)) { |
| 128 |
8360 |
AZ(vct_issp(*h)); |
| 129 |
|
/* Trim trailing space */ |
| 130 |
8360 |
e = strchr(h, '\0'); |
| 131 |
8360 |
while (e > h && vct_issp(e[-1])) |
| 132 |
0 |
e--; |
| 133 |
|
/* Encode two byte length and contents */ |
| 134 |
8360 |
l = e - h; |
| 135 |
8360 |
if (l > 0xffff - 1) { |
| 136 |
0 |
VSLb(bo->vsl, SLT_Error, |
| 137 |
|
"Vary header maximum length exceeded"); |
| 138 |
0 |
error = 1; |
| 139 |
0 |
break; |
| 140 |
|
} |
| 141 |
8360 |
} else { |
| 142 |
680 |
e = h; |
| 143 |
680 |
l = 0xffff; |
| 144 |
|
} |
| 145 |
9040 |
AZ(VSB_printf(sb, "%c%c", (int)(l >> 8), (int)(l & 0xff))); |
| 146 |
|
/* Append to vary matching string */ |
| 147 |
9040 |
AZ(VSB_bcat(sb, VSB_data(sbh), VSB_len(sbh))); |
| 148 |
9040 |
if (e != h) |
| 149 |
8320 |
AZ(VSB_bcat(sb, h, e - h)); |
| 150 |
|
|
| 151 |
9160 |
while (vct_issp(*q)) |
| 152 |
120 |
q++; |
| 153 |
9040 |
if (*q == '\0') |
| 154 |
8600 |
break; |
| 155 |
440 |
if (*q != ',') { |
| 156 |
120 |
VSLb(bo->vsl, SLT_Error, "Malformed Vary header"); |
| 157 |
120 |
error = 1; |
| 158 |
120 |
break; |
| 159 |
|
} |
| 160 |
320 |
p = q; |
| 161 |
320 |
} |
| 162 |
|
|
| 163 |
8800 |
if (error) { |
| 164 |
200 |
VSB_destroy(&sbh); |
| 165 |
200 |
VSB_destroy(&sb); |
| 166 |
200 |
return (-1); |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
/* Terminate vary matching string */ |
| 170 |
8600 |
VSB_printf(sb, "%c%c%c", 0xff, 0xff, 0); |
| 171 |
|
|
| 172 |
8600 |
VSB_destroy(&sbh); |
| 173 |
8600 |
AZ(VSB_finish(sb)); |
| 174 |
8600 |
*psb = sb; |
| 175 |
8600 |
return (VSB_len(sb)); |
| 176 |
58839 |
} |
| 177 |
|
|
| 178 |
|
/* |
| 179 |
|
* Find length of a vary entry |
| 180 |
|
*/ |
| 181 |
|
static unsigned |
| 182 |
165707 |
VRY_Len(const uint8_t *p) |
| 183 |
|
{ |
| 184 |
165707 |
unsigned l = vbe16dec(p); |
| 185 |
|
|
| 186 |
165707 |
return (2 + p[2] + 2 + (l == 0xffff ? 0 : l)); |
| 187 |
|
} |
| 188 |
|
|
| 189 |
|
/* |
| 190 |
|
* Compare two vary entries |
| 191 |
|
*/ |
| 192 |
|
static int |
| 193 |
125356 |
vry_cmp(const uint8_t *v1, const uint8_t *v2) |
| 194 |
|
{ |
| 195 |
125356 |
unsigned retval = 0; |
| 196 |
|
|
| 197 |
125356 |
if (!memcmp(v1, v2, VRY_Len(v1))) { |
| 198 |
|
/* Same same */ |
| 199 |
6142 |
retval = 0; |
| 200 |
125356 |
} else if (memcmp(v1 + 2, v2 + 2, v1[2] + 2)) { |
| 201 |
|
/* Different header */ |
| 202 |
12120 |
retval = 1; |
| 203 |
119214 |
} else if (cache_param->http_gzip_support && |
| 204 |
107054 |
http_hdr_eq(H_Accept_Encoding, (const char*) v1 + 2)) { |
| 205 |
|
/* |
| 206 |
|
* If we do gzip processing, we do not vary on Accept-Encoding, |
| 207 |
|
* because we want everybody to get the gzipped object, and |
| 208 |
|
* varnish will gunzip as necessary. We implement the skip at |
| 209 |
|
* check time, rather than create time, so that object in |
| 210 |
|
* persistent storage can be used with either setting of |
| 211 |
|
* http_gzip_support. |
| 212 |
|
*/ |
| 213 |
1974 |
retval = 0; |
| 214 |
1974 |
} else { |
| 215 |
|
/* Same header, different content */ |
| 216 |
105120 |
retval = 2; |
| 217 |
|
} |
| 218 |
125356 |
return (retval); |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
/********************************************************************** |
| 222 |
|
* Prepare predictive vary string |
| 223 |
|
*/ |
| 224 |
|
|
| 225 |
|
void |
| 226 |
106860 |
VRY_Prep(struct req *req) |
| 227 |
|
{ |
| 228 |
106860 |
if (req->waitinglist_gen == 0) { |
| 229 |
104662 |
AZ(req->vary_b); |
| 230 |
104662 |
AZ(req->vary_e); |
| 231 |
104662 |
(void)WS_ReserveAll(req->ws); |
| 232 |
104662 |
} else { |
| 233 |
2198 |
AN(WS_Reservation(req->ws)); |
| 234 |
|
} |
| 235 |
106860 |
req->vary_b = WS_Reservation(req->ws); |
| 236 |
106860 |
req->vary_e = req->vary_b + WS_ReservationSize(req->ws); |
| 237 |
106860 |
if (req->vary_b + 2 < req->vary_e) |
| 238 |
106813 |
req->vary_b[2] = '\0'; |
| 239 |
106860 |
} |
| 240 |
|
|
| 241 |
|
void |
| 242 |
1025 |
VRY_Clear(struct req *req) |
| 243 |
|
{ |
| 244 |
|
|
| 245 |
1025 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
| 246 |
1025 |
if (req->vary_b != NULL) |
| 247 |
80 |
free(req->vary_b); |
| 248 |
1025 |
req->vary_b = NULL; |
| 249 |
1025 |
AZ(req->vary_e); |
| 250 |
1025 |
} |
| 251 |
|
|
| 252 |
|
/********************************************************************** |
| 253 |
|
* Finish predictive vary processing |
| 254 |
|
*/ |
| 255 |
|
|
| 256 |
|
void |
| 257 |
104668 |
VRY_Finish(struct req *req, enum vry_finish_flag flg) |
| 258 |
|
{ |
| 259 |
104668 |
uint8_t *p = NULL; |
| 260 |
|
size_t l; |
| 261 |
|
|
| 262 |
104668 |
if (req->vary_b + 2 >= req->vary_e) { |
| 263 |
40 |
req->vary_b = NULL; |
| 264 |
40 |
req->vary_e = NULL; |
| 265 |
40 |
WS_Release(req->ws, 0); |
| 266 |
40 |
WS_MarkOverflow(req->ws); |
| 267 |
40 |
return; |
| 268 |
|
} |
| 269 |
|
|
| 270 |
104628 |
l = VRY_Validate(req->vary_b); |
| 271 |
104628 |
if (flg == KEEP && l > 3) { |
| 272 |
4280 |
p = malloc(l); |
| 273 |
4280 |
if (p != NULL) |
| 274 |
4280 |
memcpy(p, req->vary_b, l); |
| 275 |
4280 |
} |
| 276 |
104628 |
WS_Release(req->ws, 0); |
| 277 |
104628 |
req->vary_e = NULL; |
| 278 |
104628 |
req->vary_b = p; |
| 279 |
104668 |
} |
| 280 |
|
|
| 281 |
|
/********************************************************************** |
| 282 |
|
* Match vary strings, and build a new cached string if possible. |
| 283 |
|
* |
| 284 |
|
* Return zero if there is certainly no match. |
| 285 |
|
* Return non-zero if there could be a match or if we couldn't tell. |
| 286 |
|
*/ |
| 287 |
|
|
| 288 |
|
int |
| 289 |
112956 |
VRY_Match(const struct req *req, const uint8_t *vary) |
| 290 |
|
{ |
| 291 |
112956 |
uint8_t *vsp = req->vary_b; |
| 292 |
|
const char *h, *e; |
| 293 |
|
unsigned lh, ln; |
| 294 |
112956 |
int i, oflo = 0; |
| 295 |
|
hdr_t hdr; |
| 296 |
|
|
| 297 |
112956 |
AN(vsp); |
| 298 |
112956 |
AN(vary); |
| 299 |
121072 |
while (vary[2]) { |
| 300 |
113276 |
if (vsp + 2 >= req->vary_e) { |
| 301 |
|
/* |
| 302 |
|
* Too little workspace to find out |
| 303 |
|
*/ |
| 304 |
0 |
oflo = 1; |
| 305 |
0 |
break; |
| 306 |
|
} |
| 307 |
113276 |
i = vry_cmp(vary, vsp); |
| 308 |
113276 |
if (i == 1) { |
| 309 |
|
/* |
| 310 |
|
* Different header, build a new entry, |
| 311 |
|
* then compare again with that new entry. |
| 312 |
|
*/ |
| 313 |
|
|
| 314 |
12120 |
CAST_HDR(hdr, vary + 2); |
| 315 |
12120 |
ln = 2 + vary[2] + 2; |
| 316 |
12120 |
i = http_GetHdr(req->http, hdr, &h); |
| 317 |
12120 |
if (i) { |
| 318 |
|
/* Trim trailing space */ |
| 319 |
6746 |
e = strchr(h, '\0'); |
| 320 |
6746 |
while (e > h && vct_issp(e[-1])) |
| 321 |
0 |
e--; |
| 322 |
6746 |
lh = e - h; |
| 323 |
6746 |
assert(lh < 0xffff); |
| 324 |
6746 |
ln += lh; |
| 325 |
6746 |
} else { |
| 326 |
5374 |
e = h = NULL; |
| 327 |
5374 |
lh = 0xffff; |
| 328 |
|
} |
| 329 |
|
|
| 330 |
12120 |
if (vsp + ln + 3 >= req->vary_e) { |
| 331 |
|
/* |
| 332 |
|
* Not enough space to build new entry |
| 333 |
|
* and put terminator behind it. |
| 334 |
|
*/ |
| 335 |
40 |
oflo = 1; |
| 336 |
40 |
break; |
| 337 |
|
} |
| 338 |
|
|
| 339 |
12080 |
vbe16enc(vsp, (uint16_t)lh); |
| 340 |
12080 |
memcpy(vsp + 2, vary + 2, vary[2] + 2); |
| 341 |
12080 |
if (h != NULL) |
| 342 |
6706 |
memcpy(vsp + 2 + vsp[2] + 2, h, lh); |
| 343 |
12080 |
vsp[ln++] = 0xff; |
| 344 |
12080 |
vsp[ln++] = 0xff; |
| 345 |
12080 |
vsp[ln++] = 0; |
| 346 |
12080 |
assert(VRY_Validate(vsp) == ln); |
| 347 |
|
|
| 348 |
12080 |
i = vry_cmp(vary, vsp); |
| 349 |
12080 |
assert(i == 0 || i == 2); |
| 350 |
12080 |
} |
| 351 |
113236 |
if (i == 0) { |
| 352 |
|
/* Same header, same contents */ |
| 353 |
8116 |
vsp += VRY_Len(vsp); |
| 354 |
8116 |
vary += VRY_Len(vary); |
| 355 |
113236 |
} else if (i == 2) { |
| 356 |
|
/* Same header, different contents, cannot match */ |
| 357 |
105120 |
return (0); |
| 358 |
|
} |
| 359 |
|
} |
| 360 |
7836 |
if (oflo) { |
| 361 |
40 |
vsp = req->vary_b; |
| 362 |
40 |
if (vsp + 2 < req->vary_e) { |
| 363 |
40 |
vsp[0] = 0xff; |
| 364 |
40 |
vsp[1] = 0xff; |
| 365 |
40 |
vsp[2] = 0; |
| 366 |
40 |
} |
| 367 |
40 |
return (0); |
| 368 |
|
} else { |
| 369 |
7796 |
return (1); |
| 370 |
|
} |
| 371 |
112956 |
} |
| 372 |
|
|
| 373 |
|
/* |
| 374 |
|
* Check the validity of a Vary string and return its total length |
| 375 |
|
*/ |
| 376 |
|
|
| 377 |
|
static unsigned |
| 378 |
116708 |
VRY_Validate(const uint8_t *vary) |
| 379 |
|
{ |
| 380 |
116708 |
unsigned l, retval = 0; |
| 381 |
|
|
| 382 |
140827 |
while (vary[2] != 0) { |
| 383 |
24119 |
assert(strlen((const char*)vary + 3) == vary[2]); |
| 384 |
24119 |
l = VRY_Len(vary); |
| 385 |
24119 |
retval += l; |
| 386 |
24119 |
vary += l; |
| 387 |
|
} |
| 388 |
116708 |
return (retval + 3); |
| 389 |
|
} |