| | varnish-cache/bin/varnishd/cache/cache_vrt.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2015 Varnish Software AS |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 6 |
|
* |
| 7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 8 |
|
* |
| 9 |
|
* Redistribution and use in source and binary forms, with or without |
| 10 |
|
* modification, are permitted provided that the following conditions |
| 11 |
|
* are met: |
| 12 |
|
* 1. Redistributions of source code must retain the above copyright |
| 13 |
|
* notice, this list of conditions and the following disclaimer. |
| 14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
|
* notice, this list of conditions and the following disclaimer in the |
| 16 |
|
* documentation and/or other materials provided with the distribution. |
| 17 |
|
* |
| 18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 |
|
* SUCH DAMAGE. |
| 29 |
|
* |
| 30 |
|
* Runtime support for compiled VCL programs |
| 31 |
|
*/ |
| 32 |
|
|
| 33 |
|
#include "config.h" |
| 34 |
|
|
| 35 |
|
#include <stdlib.h> |
| 36 |
|
|
| 37 |
|
#include "cache_varnishd.h" |
| 38 |
|
|
| 39 |
|
#include "cache_objhead.h" |
| 40 |
|
#include "vav.h" |
| 41 |
|
#include "vcl.h" |
| 42 |
|
#include "vct.h" |
| 43 |
|
#include "venc.h" |
| 44 |
|
#include "vend.h" |
| 45 |
|
#include "vrt_obj.h" |
| 46 |
|
#include "vsa.h" |
| 47 |
|
#include "vsha256.h" |
| 48 |
|
#include "vtcp.h" |
| 49 |
|
#include "vtim.h" |
| 50 |
|
#include "vcc_interface.h" |
| 51 |
|
|
| 52 |
|
#include "common/heritage.h" |
| 53 |
|
#include "common/vsmw.h" |
| 54 |
|
#include "proxy/cache_proxy.h" |
| 55 |
|
|
| 56 |
|
// NOT using TOSTRANDS() to create a NULL pointer element despite n == 0 |
| 57 |
|
const struct strands *const vrt_null_strands = &(struct strands){ |
| 58 |
|
.magic = STRANDS_MAGIC, |
| 59 |
|
.n = 0, |
| 60 |
|
.p = (const char *[1]){NULL} |
| 61 |
|
}; |
| 62 |
|
const struct vrt_blob *const vrt_null_blob = &(struct vrt_blob){ |
| 63 |
|
.magic = VRT_BLOB_MAGIC, |
| 64 |
|
.type = VRT_NULL_BLOB_TYPE, |
| 65 |
|
.len = 0, |
| 66 |
|
.blob = "\0" |
| 67 |
|
}; |
| 68 |
|
|
| 69 |
|
/*--------------------------------------------------------------------*/ |
| 70 |
|
|
| 71 |
|
VCL_VOID |
| 72 |
15240 |
VRT_synth(VRT_CTX, VCL_INT code, VCL_STRING reason) |
| 73 |
|
{ |
| 74 |
|
const char *ret; |
| 75 |
|
|
| 76 |
15240 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 77 |
15240 |
assert(ctx->req != NULL || ctx->bo != NULL); |
| 78 |
|
|
| 79 |
15240 |
ret = ctx->req == NULL ? "error" : "synth"; |
| 80 |
15240 |
if (code < 0) { |
| 81 |
0 |
VRT_fail(ctx, "return(%s()) status code (%jd) is negative", |
| 82 |
0 |
ret, (intmax_t)code); |
| 83 |
0 |
return; |
| 84 |
|
} |
| 85 |
15240 |
if (code > 65535) { |
| 86 |
0 |
VRT_fail(ctx, "return(%s()) status code (%jd) > 65535", |
| 87 |
0 |
ret, (intmax_t)code); |
| 88 |
0 |
return; |
| 89 |
|
} |
| 90 |
15240 |
if ((code % 1000) < 100) { |
| 91 |
320 |
VRT_fail(ctx, |
| 92 |
|
"illegal return(%s()) status code (%jd) (..0##)", |
| 93 |
160 |
ret, (intmax_t)code); |
| 94 |
160 |
return; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
15080 |
if (reason && !WS_Allocated(ctx->ws, reason, -1)) { |
| 98 |
2120 |
reason = WS_Copy(ctx->ws, reason, -1); |
| 99 |
2120 |
if (!reason) { |
| 100 |
0 |
VRT_fail(ctx, "Workspace overflow"); |
| 101 |
0 |
return; |
| 102 |
|
} |
| 103 |
2120 |
} |
| 104 |
|
|
| 105 |
15080 |
if (ctx->req == NULL) { |
| 106 |
720 |
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC); |
| 107 |
720 |
ctx->bo->err_code = (uint16_t)code; |
| 108 |
720 |
ctx->bo->err_reason = reason ? reason |
| 109 |
360 |
: http_Status2Reason(ctx->bo->err_code % 1000, NULL); |
| 110 |
720 |
return; |
| 111 |
|
} |
| 112 |
|
|
| 113 |
14360 |
ctx->req->err_code = (uint16_t)code; |
| 114 |
14360 |
ctx->req->err_reason = reason ? reason |
| 115 |
10240 |
: http_Status2Reason(ctx->req->err_code % 1000, NULL); |
| 116 |
15240 |
} |
| 117 |
|
|
| 118 |
|
/*--------------------------------------------------------------------*/ |
| 119 |
|
|
| 120 |
|
void |
| 121 |
4680 |
VPI_acl_log(VRT_CTX, const char *msg) |
| 122 |
|
{ |
| 123 |
|
|
| 124 |
4680 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 125 |
4680 |
AN(msg); |
| 126 |
4680 |
if (ctx->vsl != NULL) |
| 127 |
4680 |
VSLbs(ctx->vsl, SLT_VCL_acl, TOSTRAND(msg)); |
| 128 |
|
else |
| 129 |
0 |
VSL(SLT_VCL_acl, NO_VXID, "%s", msg); |
| 130 |
4680 |
} |
| 131 |
|
|
| 132 |
|
int |
| 133 |
3859680 |
VRT_acl_match(VRT_CTX, VCL_ACL acl, VCL_IP ip) |
| 134 |
|
{ |
| 135 |
|
|
| 136 |
3859680 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 137 |
3859680 |
if (acl == NULL || ip == NULL) { |
| 138 |
240 |
VRT_fail(ctx, "Cannot match a null %s", |
| 139 |
120 |
acl == NULL ? "ACL" : "IP address"); |
| 140 |
120 |
return (0); |
| 141 |
|
} |
| 142 |
3859560 |
CHECK_OBJ(acl, VRT_ACL_MAGIC); |
| 143 |
3859560 |
assert(VSA_Sane(ip)); |
| 144 |
3859560 |
return (acl->match(ctx, ip)); |
| 145 |
3859680 |
} |
| 146 |
|
|
| 147 |
|
static int |
| 148 |
8320 |
acl_tbl_cmp(int fam, const uint8_t *key, const uint8_t *b) |
| 149 |
|
{ |
| 150 |
|
int rv; |
| 151 |
|
|
| 152 |
8320 |
rv = fam - (int)b[3]; |
| 153 |
8320 |
if (rv == 0 && b[1] > 0) |
| 154 |
5760 |
rv = memcmp(key, b + 4, b[1]); |
| 155 |
8320 |
if (rv == 0 && b[2]) |
| 156 |
4720 |
rv = (int)(key[b[1]] & b[2]) - (int)b[4 + b[1]]; |
| 157 |
8320 |
return (rv); |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
static const uint8_t * |
| 161 |
3840 |
bbsearch(int fam, const uint8_t *key, const uint8_t *base0, |
| 162 |
|
size_t nmemb, size_t size) |
| 163 |
|
{ |
| 164 |
3840 |
const uint8_t *base = base0; |
| 165 |
|
size_t lim; |
| 166 |
|
int cmp; |
| 167 |
|
const uint8_t *p; |
| 168 |
|
|
| 169 |
11040 |
for (lim = nmemb; lim != 0; lim >>= 1) { |
| 170 |
8320 |
p = base + (lim >> 1) * size; |
| 171 |
8320 |
cmp = acl_tbl_cmp(fam, key, p); |
| 172 |
8320 |
if (cmp == 0) |
| 173 |
1120 |
return (p); |
| 174 |
7200 |
if (cmp > 0) { |
| 175 |
|
/* key > p: move right */ |
| 176 |
3040 |
base = p + size; |
| 177 |
3040 |
lim--; |
| 178 |
3040 |
} /* else move left */ |
| 179 |
7200 |
} |
| 180 |
2720 |
return (NULL); |
| 181 |
3840 |
} |
| 182 |
|
|
| 183 |
|
int |
| 184 |
3840 |
VPI_acl_table(VRT_CTX, VCL_IP p, unsigned n, unsigned m, const uint8_t *tbl, |
| 185 |
|
const char * const *str, const char *fin) |
| 186 |
|
{ |
| 187 |
|
int fam; |
| 188 |
|
const uint8_t *key; |
| 189 |
|
const uint8_t *ptr; |
| 190 |
|
size_t sz; |
| 191 |
|
|
| 192 |
3840 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 193 |
3840 |
AN(p); |
| 194 |
3840 |
AN(n); |
| 195 |
3840 |
assert(m == 20); |
| 196 |
3840 |
AN(tbl); |
| 197 |
3840 |
AN(fin); |
| 198 |
|
|
| 199 |
3840 |
fam = VRT_VSA_GetPtr(ctx, p, &key); |
| 200 |
3840 |
ptr = bbsearch(fam, key, tbl, n, m); |
| 201 |
|
|
| 202 |
3840 |
if (ptr != NULL) { |
| 203 |
1120 |
sz = ptr - tbl; |
| 204 |
1120 |
AZ(sz % m); |
| 205 |
1120 |
sz /= m; |
| 206 |
1120 |
if (str != NULL) |
| 207 |
560 |
VPI_acl_log(ctx, str[sz]); |
| 208 |
1120 |
return (*ptr); |
| 209 |
|
} |
| 210 |
2720 |
if (str != NULL) |
| 211 |
1360 |
VPI_acl_log(ctx, fin); |
| 212 |
2720 |
return (0); |
| 213 |
3840 |
} |
| 214 |
|
|
| 215 |
|
/*--------------------------------------------------------------------*/ |
| 216 |
|
|
| 217 |
|
VCL_VOID |
| 218 |
880 |
VRT_hit_for_pass(VRT_CTX, VCL_DURATION d) |
| 219 |
|
{ |
| 220 |
|
struct objcore *oc; |
| 221 |
|
|
| 222 |
880 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 223 |
880 |
if (ctx->bo == NULL) { |
| 224 |
0 |
VSLb(ctx->vsl, SLT_Error, |
| 225 |
|
"Note: Ignoring DURATION argument to return(pass);"); |
| 226 |
0 |
return; |
| 227 |
|
} |
| 228 |
880 |
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC); |
| 229 |
880 |
oc = ctx->bo->fetch_objcore; |
| 230 |
880 |
oc->ttl = d; |
| 231 |
880 |
oc->grace = 0.0; |
| 232 |
880 |
oc->keep = 0.0; |
| 233 |
1760 |
VSLb(ctx->vsl, SLT_TTL, "HFP %.0f %.0f %.0f %.0f uncacheable", |
| 234 |
880 |
oc->ttl, oc->grace, oc->keep, oc->t_origin); |
| 235 |
880 |
} |
| 236 |
|
|
| 237 |
|
/*--------------------------------------------------------------------*/ |
| 238 |
|
|
| 239 |
|
VCL_HTTP |
| 240 |
1332723 |
VRT_selecthttp(VRT_CTX, enum gethdr_e where) |
| 241 |
|
{ |
| 242 |
|
VCL_HTTP hp; |
| 243 |
|
|
| 244 |
1332723 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 245 |
1332723 |
switch (where) { |
| 246 |
|
case HDR_REQ: |
| 247 |
799850 |
hp = ctx->http_req; |
| 248 |
799850 |
break; |
| 249 |
|
case HDR_REQ_TOP: |
| 250 |
280 |
hp = ctx->http_req_top; |
| 251 |
280 |
break; |
| 252 |
|
case HDR_BEREQ: |
| 253 |
23677 |
hp = ctx->http_bereq; |
| 254 |
23677 |
break; |
| 255 |
|
case HDR_BERESP: |
| 256 |
371598 |
hp = ctx->http_beresp; |
| 257 |
371598 |
break; |
| 258 |
|
case HDR_RESP: |
| 259 |
137278 |
hp = ctx->http_resp; |
| 260 |
137278 |
break; |
| 261 |
|
case HDR_OBJ_STALE: |
| 262 |
|
case HDR_OBJ: |
| 263 |
40 |
hp = NULL; |
| 264 |
40 |
break; |
| 265 |
|
default: |
| 266 |
0 |
WRONG("VRT_selecthttp 'where' invalid"); |
| 267 |
0 |
} |
| 268 |
1332723 |
return (hp); |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
/*--------------------------------------------------------------------*/ |
| 272 |
|
|
| 273 |
|
VCL_STRING |
| 274 |
1050040 |
VRT_GetHdr(VRT_CTX, VCL_HEADER hs) |
| 275 |
|
{ |
| 276 |
|
VCL_HTTP hp; |
| 277 |
|
const char *p; |
| 278 |
|
|
| 279 |
1050040 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 280 |
1050040 |
if (hs->where == HDR_OBJ) { |
| 281 |
577 |
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC); |
| 282 |
577 |
CHECK_OBJ_NOTNULL(ctx->req->objcore, OBJCORE_MAGIC); |
| 283 |
1154 |
return (HTTP_GetHdrPack(ctx->req->wrk, ctx->req->objcore, |
| 284 |
577 |
hs->what)); |
| 285 |
|
} |
| 286 |
|
|
| 287 |
1049463 |
if (hs->where == HDR_OBJ_STALE) { |
| 288 |
40 |
CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC); |
| 289 |
40 |
CHECK_OBJ_NOTNULL(ctx->bo->stale_oc, OBJCORE_MAGIC); |
| 290 |
80 |
return (HTTP_GetHdrPack(ctx->bo->wrk, ctx->bo->stale_oc, |
| 291 |
40 |
hs->what)); |
| 292 |
|
} |
| 293 |
1049423 |
hp = VRT_selecthttp(ctx, hs->where); |
| 294 |
1049423 |
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); |
| 295 |
1049423 |
if (!http_GetHdr(hp, hs->what, &p)) |
| 296 |
486811 |
return (NULL); |
| 297 |
562612 |
return (p); |
| 298 |
1050040 |
} |
| 299 |
|
|
| 300 |
|
/*-------------------------------------------------------------------- |
| 301 |
|
* Alloc Strands with space for n elements on workspace |
| 302 |
|
* |
| 303 |
|
* Error handling is deliberately left to the caller |
| 304 |
|
*/ |
| 305 |
|
|
| 306 |
|
struct strands * |
| 307 |
0 |
VRT_AllocStrandsWS(struct ws *ws, int n) |
| 308 |
|
{ |
| 309 |
|
struct strands *s; |
| 310 |
|
const char **p; |
| 311 |
|
|
| 312 |
0 |
s = WS_Alloc(ws, sizeof *s); |
| 313 |
0 |
p = WS_Alloc(ws, n * sizeof *p); |
| 314 |
|
|
| 315 |
0 |
if (s == NULL || p == NULL) |
| 316 |
0 |
return (NULL); |
| 317 |
|
|
| 318 |
0 |
s->magic = STRANDS_MAGIC; |
| 319 |
0 |
s->n = n; |
| 320 |
0 |
s->p = p; |
| 321 |
|
|
| 322 |
0 |
return (s); |
| 323 |
0 |
} |
| 324 |
|
|
| 325 |
|
/*-------------------------------------------------------------------- |
| 326 |
|
* Compare two STRANDS |
| 327 |
|
*/ |
| 328 |
|
|
| 329 |
|
int |
| 330 |
7440 |
VRT_CompareStrands(VCL_STRANDS a, VCL_STRANDS b) |
| 331 |
|
{ |
| 332 |
7440 |
const char *pa = NULL, *pb = NULL; |
| 333 |
7440 |
int na = 0, nb = 0; |
| 334 |
|
|
| 335 |
7440 |
CHECK_OBJ_NOTNULL(a, STRANDS_MAGIC); |
| 336 |
7440 |
CHECK_OBJ_NOTNULL(b, STRANDS_MAGIC); |
| 337 |
|
|
| 338 |
50610 |
while (1) { |
| 339 |
50610 |
if (pa != NULL && *pa == '\0') |
| 340 |
4440 |
pa = NULL; |
| 341 |
50610 |
if (pb != NULL && *pb == '\0') |
| 342 |
5960 |
pb = NULL; |
| 343 |
50610 |
if (pa == NULL && na < a->n) |
| 344 |
10120 |
pa = a->p[na++]; |
| 345 |
40490 |
else if (pb == NULL && nb < b->n) |
| 346 |
11880 |
pb = b->p[nb++]; |
| 347 |
28610 |
else if (pa == NULL && pb == NULL) |
| 348 |
2520 |
return (0); |
| 349 |
26090 |
else if (pa == NULL) |
| 350 |
1760 |
return (-1); |
| 351 |
24330 |
else if (pb == NULL) |
| 352 |
480 |
return (1); |
| 353 |
23850 |
else if (*pa == '\0') |
| 354 |
0 |
pa = NULL; |
| 355 |
23850 |
else if (*pb == '\0') |
| 356 |
0 |
pb = NULL; |
| 357 |
23850 |
else if (*pa != *pb) |
| 358 |
2680 |
return (*pa - *pb); |
| 359 |
|
else { |
| 360 |
21170 |
pa++; |
| 361 |
21170 |
pb++; |
| 362 |
|
} |
| 363 |
|
} |
| 364 |
7440 |
} |
| 365 |
|
|
| 366 |
|
/*-------------------------------------------------------------------- |
| 367 |
|
* STRANDS to BOOL |
| 368 |
|
*/ |
| 369 |
|
|
| 370 |
|
VCL_BOOL |
| 371 |
566889 |
VRT_Strands2Bool(VCL_STRANDS s) |
| 372 |
|
{ |
| 373 |
|
int i; |
| 374 |
|
|
| 375 |
566889 |
CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC); |
| 376 |
886943 |
for (i = 0; i < s->n; i++) |
| 377 |
566889 |
if (s->p[i] != NULL) |
| 378 |
246835 |
return (1); |
| 379 |
320054 |
return (0); |
| 380 |
566889 |
} |
| 381 |
|
|
| 382 |
|
/*-------------------------------------------------------------------- |
| 383 |
|
* Hash a STRANDS |
| 384 |
|
*/ |
| 385 |
|
|
| 386 |
|
uint32_t |
| 387 |
69040 |
VRT_HashStrands32(VCL_STRANDS s) |
| 388 |
|
{ |
| 389 |
|
struct VSHA256Context sha_ctx; |
| 390 |
|
unsigned char sha256[VSHA256_LEN]; |
| 391 |
|
const char *p; |
| 392 |
|
int i; |
| 393 |
|
|
| 394 |
69040 |
CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC); |
| 395 |
69040 |
VSHA256_Init(&sha_ctx); |
| 396 |
205520 |
for (i = 0; i < s->n; i++) { |
| 397 |
136480 |
p = s->p[i]; |
| 398 |
136480 |
if (p != NULL && *p != '\0') |
| 399 |
136320 |
VSHA256_Update(&sha_ctx, p, strlen(p)); |
| 400 |
136480 |
} |
| 401 |
69040 |
VSHA256_Final(sha256, &sha_ctx); |
| 402 |
|
|
| 403 |
|
/* NB: for some reason vmod_director's shard director specifically |
| 404 |
|
* relied on little-endian decoding of the last 4 octets. In order |
| 405 |
|
* to maintain a stable hash function to share across consumers we |
| 406 |
|
* need to stick to that. |
| 407 |
|
*/ |
| 408 |
69040 |
return (vle32dec(sha256 + VSHA256_LEN - 4)); |
| 409 |
|
} |
| 410 |
|
|
| 411 |
|
|
| 412 |
|
/*-------------------------------------------------------------------- |
| 413 |
|
* Collapse STRANDS into the space provided, or return NULL |
| 414 |
|
*/ |
| 415 |
|
|
| 416 |
|
char * |
| 417 |
196278 |
VRT_Strands(char *d, size_t dl, VCL_STRANDS s) |
| 418 |
|
{ |
| 419 |
|
char *b; |
| 420 |
|
const char *e; |
| 421 |
|
unsigned x; |
| 422 |
|
|
| 423 |
196278 |
AN(d); |
| 424 |
196278 |
CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC); |
| 425 |
196278 |
b = d; |
| 426 |
196278 |
e = b + dl; |
| 427 |
393996 |
for (int i = 0; i < s->n; i++) |
| 428 |
389036 |
if (s->p[i] != NULL && *s->p[i] != '\0') { |
| 429 |
191318 |
x = strlen(s->p[i]); |
| 430 |
191318 |
if (b + x >= e) |
| 431 |
2240 |
return (NULL); |
| 432 |
189078 |
memcpy(b, s->p[i], x); |
| 433 |
189078 |
b += x; |
| 434 |
189078 |
} |
| 435 |
194038 |
assert(b < e); |
| 436 |
194038 |
*b++ = '\0'; |
| 437 |
194038 |
return (b); |
| 438 |
196278 |
} |
| 439 |
|
|
| 440 |
|
/*-------------------------------------------------------------------- |
| 441 |
|
* Copy and merge STRANDS into a workspace. |
| 442 |
|
*/ |
| 443 |
|
|
| 444 |
|
VCL_STRING |
| 445 |
19560 |
VRT_StrandsWS(struct ws *ws, const char *h, VCL_STRANDS s) |
| 446 |
|
{ |
| 447 |
19560 |
const char *q = NULL; |
| 448 |
|
struct vsb vsb[1]; |
| 449 |
|
int i; |
| 450 |
|
|
| 451 |
19560 |
WS_Assert(ws); |
| 452 |
19560 |
CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC); |
| 453 |
|
|
| 454 |
20640 |
for (i = 0; i < s->n; i++) { |
| 455 |
20280 |
if (s->p[i] != NULL && *s->p[i] != '\0') { |
| 456 |
19200 |
q = s->p[i]; |
| 457 |
19200 |
break; |
| 458 |
|
} |
| 459 |
1080 |
} |
| 460 |
|
|
| 461 |
19560 |
if (q == NULL) { |
| 462 |
360 |
if (h == NULL) |
| 463 |
320 |
return (""); |
| 464 |
40 |
if (WS_Allocated(ws, h, -1)) |
| 465 |
0 |
return (h); |
| 466 |
19240 |
} else if (h == NULL && WS_Allocated(ws, q, -1)) { |
| 467 |
1880 |
for (i++; i < s->n; i++) |
| 468 |
1160 |
if (s->p[i] != NULL && *s->p[i] != '\0') |
| 469 |
760 |
break; |
| 470 |
1480 |
if (i == s->n) |
| 471 |
720 |
return (q); |
| 472 |
760 |
} |
| 473 |
|
|
| 474 |
18520 |
WS_VSB_new(vsb, ws); |
| 475 |
18520 |
if (h != NULL) |
| 476 |
11319 |
VSB_cat(vsb, h); |
| 477 |
42279 |
for (i = 0; i < s->n; i++) { |
| 478 |
23759 |
if (s->p[i] != NULL && *s->p[i] != '\0') |
| 479 |
22359 |
VSB_cat(vsb, s->p[i]); |
| 480 |
23759 |
} |
| 481 |
18520 |
return (WS_VSB_finish(vsb, ws, NULL)); |
| 482 |
19560 |
} |
| 483 |
|
|
| 484 |
|
/*-------------------------------------------------------------------- |
| 485 |
|
* Copy and merge STRANDS on the current workspace |
| 486 |
|
*/ |
| 487 |
|
|
| 488 |
|
VCL_STRING |
| 489 |
3080 |
VRT_STRANDS_string(VRT_CTX, VCL_STRANDS s) |
| 490 |
|
{ |
| 491 |
|
const char *b; |
| 492 |
|
|
| 493 |
3080 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 494 |
3080 |
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC); |
| 495 |
3080 |
b = VRT_StrandsWS(ctx->ws, NULL, s); |
| 496 |
3080 |
if (b == NULL) |
| 497 |
40 |
VRT_fail(ctx, "Workspace overflow"); |
| 498 |
3080 |
return (b); |
| 499 |
|
} |
| 500 |
|
|
| 501 |
|
/*-------------------------------------------------------------------- |
| 502 |
|
* upper/lower-case STRANDS (onto workspace) |
| 503 |
|
*/ |
| 504 |
|
|
| 505 |
|
#include <stdio.h> |
| 506 |
|
|
| 507 |
|
VCL_STRING |
| 508 |
440 |
VRT_UpperLowerStrands(VRT_CTX, VCL_STRANDS s, int up) |
| 509 |
|
{ |
| 510 |
|
unsigned u; |
| 511 |
|
char *b, *e, *r; |
| 512 |
440 |
const char *p, *q = NULL; |
| 513 |
440 |
int i, copy = 0; |
| 514 |
|
|
| 515 |
440 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 516 |
440 |
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC); |
| 517 |
440 |
CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC); |
| 518 |
440 |
u = WS_ReserveAll(ctx->ws); |
| 519 |
440 |
r = b = WS_Reservation(ctx->ws); |
| 520 |
440 |
e = b + u; |
| 521 |
1040 |
for (i = 0; i < s->n; i++) { |
| 522 |
600 |
if (s->p[i] == NULL || s->p[i][0] == '\0') |
| 523 |
0 |
continue; |
| 524 |
600 |
if (q != NULL) |
| 525 |
160 |
copy = 1; |
| 526 |
3360 |
for(p = q = s->p[i]; *p != '\0'; p++) { |
| 527 |
3800 |
if ((up && vct_islower(*p)) || |
| 528 |
2440 |
(!up && vct_isupper(*p))) { |
| 529 |
1360 |
*b++ = *p ^ 0x20; |
| 530 |
1360 |
copy = 1; |
| 531 |
2760 |
} else if (b < e) { |
| 532 |
1400 |
*b++ = *p; |
| 533 |
1400 |
} |
| 534 |
2760 |
if (copy && b == e) |
| 535 |
0 |
break; |
| 536 |
2760 |
} |
| 537 |
600 |
if (copy && b == e) { |
| 538 |
0 |
WS_Release(ctx->ws, 0); |
| 539 |
0 |
VRT_fail(ctx, "Workspace overflow"); |
| 540 |
0 |
return (NULL); |
| 541 |
|
} |
| 542 |
600 |
} |
| 543 |
440 |
assert(b <= e); |
| 544 |
440 |
if (!copy) { |
| 545 |
120 |
WS_Release(ctx->ws, 0); |
| 546 |
120 |
return (q); |
| 547 |
|
} |
| 548 |
320 |
assert(b < e); |
| 549 |
320 |
*b++ = '\0'; |
| 550 |
320 |
assert(b <= e); |
| 551 |
320 |
WS_ReleaseP(ctx->ws, b); |
| 552 |
320 |
return (r); |
| 553 |
440 |
} |
| 554 |
|
|
| 555 |
|
|
| 556 |
|
// rfc9110,l,1585,1589 |
| 557 |
|
// field-content = field-vchar |
| 558 |
|
// [ 1*( SP / HTAB / field-vchar ) field-vchar ] |
| 559 |
|
// field-vchar = VCHAR / obs-text |
| 560 |
|
// obs-text = %x80-FF |
| 561 |
|
// |
| 562 |
|
// This implementation is less strict, see #4221 |
| 563 |
|
static inline VCL_BOOL |
| 564 |
194079 |
validhdr(const char *p) |
| 565 |
|
{ |
| 566 |
194079 |
AN(p); |
| 567 |
7608016 |
for(;*p != '\0'; p++) |
| 568 |
7414017 |
if (! vct_ishdrval(*p)) |
| 569 |
80 |
return (0); |
| 570 |
193999 |
return (1); |
| 571 |
194079 |
} |
| 572 |
|
|
| 573 |
|
/*--------------------------------------------------------------------*/ |
| 574 |
|
VCL_BOOL |
| 575 |
80 |
VRT_ValidHdr(VRT_CTX, VCL_STRANDS s) |
| 576 |
|
{ |
| 577 |
|
int i; |
| 578 |
|
|
| 579 |
80 |
(void) ctx; |
| 580 |
|
|
| 581 |
80 |
CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC); |
| 582 |
120 |
for (i = 0; i < s->n; i++) { |
| 583 |
80 |
if (s->p[i] == NULL || s->p[i][0] == '\0') |
| 584 |
0 |
continue; |
| 585 |
80 |
if (! validhdr(s->p[i])) |
| 586 |
40 |
return (0); |
| 587 |
40 |
} |
| 588 |
|
|
| 589 |
40 |
return (1); |
| 590 |
80 |
} |
| 591 |
|
/*--------------------------------------------------------------------*/ |
| 592 |
|
|
| 593 |
|
VCL_VOID |
| 594 |
80957 |
VRT_UnsetHdr(VRT_CTX, VCL_HEADER hs) |
| 595 |
|
{ |
| 596 |
|
VCL_HTTP hp; |
| 597 |
|
|
| 598 |
80957 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 599 |
80957 |
AN(hs); |
| 600 |
80957 |
AN(hs->what); |
| 601 |
80957 |
hp = VRT_selecthttp(ctx, hs->where); |
| 602 |
80957 |
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); |
| 603 |
80957 |
http_Unset(hp, hs->what); |
| 604 |
80957 |
} |
| 605 |
|
|
| 606 |
|
VCL_VOID |
| 607 |
201436 |
VRT_SetHdr(VRT_CTX, VCL_HEADER hs, const char *pfx, VCL_STRANDS s) |
| 608 |
|
{ |
| 609 |
|
VCL_HTTP hp; |
| 610 |
|
unsigned u, l, pl; |
| 611 |
|
char *p, *b; |
| 612 |
|
|
| 613 |
201436 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 614 |
201436 |
AN(hs); |
| 615 |
201436 |
AN(hs->what); |
| 616 |
201436 |
hp = VRT_selecthttp(ctx, hs->where); |
| 617 |
201436 |
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); |
| 618 |
|
|
| 619 |
201436 |
u = WS_ReserveAll(hp->ws); |
| 620 |
201436 |
pl = (pfx == NULL) ? 0 : strlen(pfx); |
| 621 |
201436 |
l = hs->what->len + 1 + pl; |
| 622 |
201436 |
if (u <= l) { |
| 623 |
5080 |
WS_Release(hp->ws, 0); |
| 624 |
5080 |
WS_MarkOverflow(hp->ws); |
| 625 |
5080 |
VSLbs(ctx->vsl, SLT_LostHeader, TOSTRAND(hs->what->str)); |
| 626 |
5080 |
return; |
| 627 |
|
} |
| 628 |
196356 |
b = WS_Reservation(hp->ws); |
| 629 |
196356 |
if (s != NULL) { |
| 630 |
196036 |
p = VRT_Strands(b + l, u - l, s); |
| 631 |
196036 |
if (p == NULL) { |
| 632 |
2240 |
WS_Release(hp->ws, 0); |
| 633 |
2240 |
WS_MarkOverflow(hp->ws); |
| 634 |
4480 |
VSLbs(ctx->vsl, SLT_LostHeader, |
| 635 |
2240 |
TOSTRAND(hs->what->str)); |
| 636 |
2240 |
return; |
| 637 |
|
} |
| 638 |
193796 |
} else { |
| 639 |
320 |
b[l] = '\0'; |
| 640 |
|
} |
| 641 |
194116 |
p = b; |
| 642 |
194116 |
memcpy(p, hs->what->str, hs->what->len); |
| 643 |
194116 |
p += hs->what->len; |
| 644 |
194116 |
*p++ = ' '; |
| 645 |
194116 |
if (pfx != NULL) |
| 646 |
400 |
memcpy(p, pfx, pl); |
| 647 |
194116 |
p += pl; |
| 648 |
194116 |
if (FEATURE(FEATURE_VALIDATE_HEADERS) && !validhdr(b)) { |
| 649 |
40 |
VRT_fail(ctx, "Bad header %s", b); |
| 650 |
40 |
WS_Release(hp->ws, 0); |
| 651 |
40 |
return; |
| 652 |
|
} |
| 653 |
194076 |
WS_ReleaseP(hp->ws, strchr(p, '\0') + 1); |
| 654 |
194076 |
http_Unset(hp, hs->what); |
| 655 |
194076 |
http_SetHeader(hp, b); |
| 656 |
201436 |
} |
| 657 |
|
|
| 658 |
|
/*--------------------------------------------------------------------*/ |
| 659 |
|
|
| 660 |
|
VCL_VOID |
| 661 |
829114 |
VRT_handling(VRT_CTX, unsigned hand) |
| 662 |
|
{ |
| 663 |
|
|
| 664 |
829114 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 665 |
829114 |
assert(hand != VCL_RET_FAIL); |
| 666 |
829114 |
AN(ctx->vpi); |
| 667 |
829114 |
AZ(ctx->vpi->handling); |
| 668 |
829114 |
assert(hand > 0); |
| 669 |
829114 |
assert(hand < VCL_RET_MAX); |
| 670 |
829114 |
ctx->vpi->handling = hand; |
| 671 |
829114 |
} |
| 672 |
|
|
| 673 |
|
unsigned |
| 674 |
640 |
VRT_handled(VRT_CTX) |
| 675 |
|
{ |
| 676 |
640 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 677 |
640 |
AN(ctx->vpi); |
| 678 |
640 |
return (ctx->vpi->handling); |
| 679 |
|
} |
| 680 |
|
|
| 681 |
|
/* the trace value is cached in the VPI for efficiency */ |
| 682 |
|
VCL_VOID |
| 683 |
40 |
VRT_trace(VRT_CTX, VCL_BOOL a) |
| 684 |
|
{ |
| 685 |
40 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 686 |
40 |
AN(ctx->vpi); |
| 687 |
40 |
ctx->vpi->trace = a; |
| 688 |
40 |
} |
| 689 |
|
|
| 690 |
|
/*--------------------------------------------------------------------*/ |
| 691 |
|
|
| 692 |
|
VCL_VOID |
| 693 |
8120 |
VRT_fail(VRT_CTX, const char *fmt, ...) |
| 694 |
|
{ |
| 695 |
|
va_list ap; |
| 696 |
|
|
| 697 |
8120 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 698 |
8120 |
assert(ctx->vsl != NULL || ctx->msg != NULL); |
| 699 |
8120 |
AN(ctx->vpi); |
| 700 |
8120 |
if (ctx->vpi->handling == VCL_RET_FAIL) |
| 701 |
120 |
return; |
| 702 |
8000 |
AZ(ctx->vpi->handling); |
| 703 |
8000 |
AN(fmt); |
| 704 |
8000 |
AZ(strchr(fmt, '\n')); |
| 705 |
8000 |
va_start(ap, fmt); |
| 706 |
8000 |
if (ctx->vsl != NULL) { |
| 707 |
6360 |
VSLbv(ctx->vsl, SLT_VCL_Error, fmt, ap); |
| 708 |
6360 |
} else { |
| 709 |
1640 |
AN(ctx->msg); |
| 710 |
1640 |
VSB_vprintf(ctx->msg, fmt, ap); |
| 711 |
1640 |
VSB_putc(ctx->msg, '\n'); |
| 712 |
|
} |
| 713 |
8000 |
va_end(ap); |
| 714 |
8000 |
ctx->vpi->handling = VCL_RET_FAIL; |
| 715 |
8120 |
} |
| 716 |
|
|
| 717 |
|
/*-------------------------------------------------------------------- |
| 718 |
|
* Feed data into the hash calculation |
| 719 |
|
*/ |
| 720 |
|
|
| 721 |
|
VCL_VOID |
| 722 |
296903 |
VRT_hashdata(VRT_CTX, VCL_STRANDS s) |
| 723 |
|
{ |
| 724 |
|
int i; |
| 725 |
|
|
| 726 |
296903 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 727 |
296903 |
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC); |
| 728 |
296903 |
AN(ctx->specific); |
| 729 |
296903 |
CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC); |
| 730 |
593935 |
for (i = 0; i < s->n; i++) |
| 731 |
297032 |
HSH_AddString(ctx->req, ctx->specific, s->p[i]); |
| 732 |
|
/* |
| 733 |
|
* Add a 'field-separator' to make it more difficult to |
| 734 |
|
* manipulate the hash. |
| 735 |
|
*/ |
| 736 |
296903 |
HSH_AddString(ctx->req, ctx->specific, NULL); |
| 737 |
296903 |
} |
| 738 |
|
|
| 739 |
|
/*--------------------------------------------------------------------*/ |
| 740 |
|
|
| 741 |
|
VCL_TIME |
| 742 |
6440 |
VRT_r_now(VRT_CTX) |
| 743 |
|
{ |
| 744 |
6440 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 745 |
6440 |
return (ctx->now); |
| 746 |
|
} |
| 747 |
|
|
| 748 |
|
/*--------------------------------------------------------------------*/ |
| 749 |
|
|
| 750 |
|
VCL_STRING v_matchproto_() |
| 751 |
9910 |
VRT_IP_string(VRT_CTX, VCL_IP ip) |
| 752 |
|
{ |
| 753 |
|
char buf[VTCP_ADDRBUFSIZE]; |
| 754 |
|
|
| 755 |
9910 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 756 |
9910 |
if (ip == NULL) { |
| 757 |
0 |
VRT_fail(ctx, "%s: Illegal IP", __func__); |
| 758 |
0 |
return (NULL); |
| 759 |
|
} |
| 760 |
9910 |
VTCP_name(ip, buf, sizeof buf, NULL, 0); |
| 761 |
9910 |
return (WS_Copy(ctx->ws, buf, -1)); |
| 762 |
9910 |
} |
| 763 |
|
|
| 764 |
|
int |
| 765 |
114957 |
VRT_INT_is_valid(VCL_INT arg) |
| 766 |
|
{ |
| 767 |
114957 |
return (arg >= VRT_INTEGER_MIN && arg <= VRT_INTEGER_MAX); |
| 768 |
|
} |
| 769 |
|
|
| 770 |
|
|
| 771 |
|
VCL_STRING v_matchproto_() |
| 772 |
114955 |
VRT_INT_string(VRT_CTX, VCL_INT num) |
| 773 |
|
{ |
| 774 |
|
|
| 775 |
114955 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 776 |
114955 |
if (!VRT_INT_is_valid(num)) |
| 777 |
80 |
VRT_fail(ctx, "INT overflow converting to string (0x%jx)", |
| 778 |
40 |
(intmax_t)num); |
| 779 |
114955 |
return (WS_Printf(ctx->ws, "%jd", (intmax_t)num)); |
| 780 |
|
} |
| 781 |
|
|
| 782 |
|
int |
| 783 |
9440 |
VRT_REAL_is_valid(VCL_REAL arg) |
| 784 |
|
{ |
| 785 |
9440 |
return (!isnan(arg) && arg >= VRT_DECIMAL_MIN && arg <= VRT_DECIMAL_MAX); |
| 786 |
|
} |
| 787 |
|
|
| 788 |
|
VCL_STRING v_matchproto_() |
| 789 |
9440 |
VRT_REAL_string(VRT_CTX, VCL_REAL num) |
| 790 |
|
{ |
| 791 |
|
|
| 792 |
9440 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 793 |
9440 |
if (!VRT_REAL_is_valid(num)) |
| 794 |
40 |
VRT_fail(ctx, "REAL overflow converting to string (%e)", num); |
| 795 |
9440 |
return (WS_Printf(ctx->ws, "%.3f", num)); |
| 796 |
|
} |
| 797 |
|
|
| 798 |
|
VCL_STRING v_matchproto_() |
| 799 |
3160 |
VRT_TIME_string(VRT_CTX, VCL_TIME t) |
| 800 |
|
{ |
| 801 |
|
char *p; |
| 802 |
|
|
| 803 |
3160 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 804 |
3160 |
p = WS_Alloc(ctx->ws, VTIM_FORMAT_SIZE); |
| 805 |
3160 |
if (p == NULL) { |
| 806 |
0 |
VRT_fail(ctx, "Workspace overflow"); |
| 807 |
0 |
return (NULL); |
| 808 |
|
} |
| 809 |
3160 |
VTIM_format(t, p); |
| 810 |
3160 |
if (*p == '\0') { |
| 811 |
40 |
VRT_fail(ctx, "Unformattable VCL_TIME"); |
| 812 |
40 |
return (NULL); |
| 813 |
|
} |
| 814 |
3120 |
return (p); |
| 815 |
3160 |
} |
| 816 |
|
|
| 817 |
|
VCL_STRING v_matchproto_() |
| 818 |
184386 |
VRT_BACKEND_string(VCL_BACKEND d) |
| 819 |
|
{ |
| 820 |
184386 |
if (d == NULL) |
| 821 |
0 |
return (NULL); |
| 822 |
184386 |
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC); |
| 823 |
184386 |
return (d->vcl_name); |
| 824 |
184386 |
} |
| 825 |
|
|
| 826 |
|
VCL_STRING v_matchproto_() |
| 827 |
22320 |
VRT_BOOL_string(VCL_BOOL val) |
| 828 |
|
{ |
| 829 |
|
|
| 830 |
22320 |
return (val ? "true" : "false"); |
| 831 |
|
} |
| 832 |
|
|
| 833 |
|
VCL_STRING v_matchproto_() |
| 834 |
640 |
VRT_BLOB_string(VRT_CTX, VCL_BLOB val) |
| 835 |
|
{ |
| 836 |
|
struct vsb vsb[1]; |
| 837 |
|
const char *s; |
| 838 |
|
|
| 839 |
640 |
if (val == NULL) |
| 840 |
0 |
return (NULL); |
| 841 |
640 |
WS_VSB_new(vsb, ctx->ws); |
| 842 |
640 |
VSB_putc(vsb, ':'); |
| 843 |
640 |
VENC_Encode_Base64(vsb, val->blob, val->len); |
| 844 |
640 |
VSB_putc(vsb, ':'); |
| 845 |
640 |
s = WS_VSB_finish(vsb, ctx->ws, NULL); |
| 846 |
640 |
return (s); |
| 847 |
640 |
} |
| 848 |
|
|
| 849 |
|
/*--------------------------------------------------------------------*/ |
| 850 |
|
|
| 851 |
|
VCL_VOID |
| 852 |
920 |
VRT_Rollback(VRT_CTX, VCL_HTTP hp) |
| 853 |
|
{ |
| 854 |
|
|
| 855 |
920 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 856 |
920 |
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); |
| 857 |
920 |
if (ctx->method & VCL_MET_PIPE) { |
| 858 |
0 |
VRT_fail(ctx, "Cannot rollback in vcl_pipe {}"); |
| 859 |
0 |
return; |
| 860 |
|
} |
| 861 |
920 |
if (hp == ctx->http_req) { |
| 862 |
560 |
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC); |
| 863 |
560 |
Req_Rollback(ctx); |
| 864 |
560 |
if (ctx->method & VCL_MET_DELIVER) |
| 865 |
360 |
XXXAZ(Resp_Setup_Deliver(ctx->req)); |
| 866 |
560 |
if (ctx->method & VCL_MET_SYNTH) |
| 867 |
80 |
Resp_Setup_Synth(ctx->req); |
| 868 |
920 |
} else if (hp == ctx->http_bereq) { |
| 869 |
360 |
Bereq_Rollback(ctx); |
| 870 |
360 |
} else |
| 871 |
0 |
WRONG("VRT_Rollback 'hp' invalid"); |
| 872 |
920 |
} |
| 873 |
|
|
| 874 |
|
/*--------------------------------------------------------------------*/ |
| 875 |
|
|
| 876 |
|
VCL_VOID |
| 877 |
200 |
VRT_synth_strands(VRT_CTX, VCL_STRANDS s) |
| 878 |
|
{ |
| 879 |
|
struct vsb *vsb; |
| 880 |
|
int i; |
| 881 |
|
|
| 882 |
200 |
CAST_OBJ_NOTNULL(vsb, ctx->specific, VSB_MAGIC); |
| 883 |
200 |
CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC); |
| 884 |
400 |
for (i = 0; i < s->n; i++) { |
| 885 |
200 |
if (s->p[i] != NULL) |
| 886 |
160 |
VSB_cat(vsb, s->p[i]); |
| 887 |
|
else |
| 888 |
40 |
VSB_cat(vsb, "(null)"); |
| 889 |
200 |
} |
| 890 |
200 |
} |
| 891 |
|
|
| 892 |
|
VCL_VOID |
| 893 |
0 |
VRT_synth_blob(VRT_CTX, VCL_BLOB b) |
| 894 |
|
{ |
| 895 |
|
struct vsb *vsb; |
| 896 |
0 |
CAST_OBJ_NOTNULL(vsb, ctx->specific, VSB_MAGIC); |
| 897 |
|
|
| 898 |
0 |
CHECK_OBJ_NOTNULL(b, VRT_BLOB_MAGIC); |
| 899 |
0 |
if (b->len > 0 && b->blob != NULL) |
| 900 |
0 |
VSB_bcat(vsb, b->blob, b->len); |
| 901 |
0 |
} |
| 902 |
|
|
| 903 |
|
VCL_VOID |
| 904 |
0 |
VRT_synth_page(VRT_CTX, VCL_STRANDS s) |
| 905 |
|
{ |
| 906 |
0 |
VRT_synth_strands(ctx, s); |
| 907 |
0 |
} |
| 908 |
|
|
| 909 |
|
/*--------------------------------------------------------------------*/ |
| 910 |
|
|
| 911 |
|
static VCL_STRING |
| 912 |
840 |
vrt_ban_error(VRT_CTX, VCL_STRING err) |
| 913 |
|
{ |
| 914 |
840 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 915 |
840 |
AN(ctx->vsl); |
| 916 |
840 |
AN(err); |
| 917 |
|
|
| 918 |
840 |
VSLb(ctx->vsl, SLT_VCL_Error, "ban(): %s", err); |
| 919 |
840 |
return (err); |
| 920 |
|
} |
| 921 |
|
|
| 922 |
|
VCL_STRING |
| 923 |
1560 |
VRT_ban_string(VRT_CTX, VCL_STRING str) |
| 924 |
|
{ |
| 925 |
|
char *a1, *a2, *a3; |
| 926 |
|
char **av; |
| 927 |
|
struct ban_proto *bp; |
| 928 |
1560 |
const char *err = NULL, *berr = NULL; |
| 929 |
|
int i; |
| 930 |
|
|
| 931 |
1560 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 932 |
|
|
| 933 |
1560 |
if (str == NULL) |
| 934 |
80 |
return (vrt_ban_error(ctx, "Null argument")); |
| 935 |
|
|
| 936 |
1480 |
bp = BAN_Build(); |
| 937 |
1480 |
if (bp == NULL) |
| 938 |
0 |
return (vrt_ban_error(ctx, "Out of Memory")); |
| 939 |
|
|
| 940 |
1480 |
av = VAV_Parse(str, NULL, ARGV_NOESC); |
| 941 |
1480 |
AN(av); |
| 942 |
1480 |
if (av[0] != NULL) { |
| 943 |
0 |
err = av[0]; |
| 944 |
0 |
VAV_Free(av); |
| 945 |
0 |
BAN_Abandon(bp); |
| 946 |
0 |
return (vrt_ban_error(ctx, err)); |
| 947 |
|
} |
| 948 |
1680 |
for (i = 0; ;) { |
| 949 |
1680 |
a1 = av[++i]; |
| 950 |
1680 |
if (a1 == NULL) { |
| 951 |
80 |
err = "No ban conditions found."; |
| 952 |
80 |
break; |
| 953 |
|
} |
| 954 |
1600 |
a2 = av[++i]; |
| 955 |
1600 |
if (a2 == NULL) { |
| 956 |
80 |
err = "Expected comparison operator."; |
| 957 |
80 |
break; |
| 958 |
|
} |
| 959 |
1520 |
a3 = av[++i]; |
| 960 |
1520 |
if (a3 == NULL) { |
| 961 |
80 |
err = "Expected second operand."; |
| 962 |
80 |
break; |
| 963 |
|
} |
| 964 |
1440 |
berr = BAN_AddTest(bp, a1, a2, a3); |
| 965 |
1440 |
if (berr != NULL) |
| 966 |
440 |
break; |
| 967 |
|
|
| 968 |
1000 |
if (av[++i] == NULL) { |
| 969 |
720 |
berr = BAN_Commit(bp); |
| 970 |
720 |
if (berr == NULL) |
| 971 |
720 |
bp = NULL; |
| 972 |
720 |
break; |
| 973 |
|
} |
| 974 |
280 |
if (strcmp(av[i], "&&")) { |
| 975 |
160 |
err = WS_Printf(ctx->ws, "Expected && between " |
| 976 |
80 |
"conditions, found \"%s\"", av[i]); |
| 977 |
80 |
if (err == NULL) |
| 978 |
0 |
err = "Expected && between conditions " |
| 979 |
|
"(workspace overflow)"; |
| 980 |
80 |
break; |
| 981 |
|
} |
| 982 |
|
} |
| 983 |
1480 |
if (berr != NULL) { |
| 984 |
440 |
AZ(err); |
| 985 |
440 |
err = WS_Copy(ctx->ws, berr, -1); |
| 986 |
440 |
if (err == NULL) |
| 987 |
0 |
err = "Unknown error (workspace overflow)"; |
| 988 |
440 |
berr = NULL; |
| 989 |
440 |
} |
| 990 |
1480 |
AZ(berr); |
| 991 |
1480 |
if (bp != NULL) |
| 992 |
760 |
BAN_Abandon(bp); |
| 993 |
1480 |
VAV_Free(av); |
| 994 |
1480 |
if (err == NULL) |
| 995 |
720 |
return (NULL); |
| 996 |
760 |
return (vrt_ban_error(ctx, err)); |
| 997 |
1560 |
} |
| 998 |
|
|
| 999 |
|
VCL_BYTES |
| 1000 |
1400 |
VRT_CacheReqBody(VRT_CTX, VCL_BYTES maxsize) |
| 1001 |
|
{ |
| 1002 |
1400 |
const char * const err = "req.body can only be cached in vcl_recv{}"; |
| 1003 |
|
|
| 1004 |
1400 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 1005 |
1400 |
if (ctx->method != VCL_MET_RECV) { |
| 1006 |
0 |
if (ctx->vsl != NULL) { |
| 1007 |
0 |
VSLbs(ctx->vsl, SLT_VCL_Error, TOSTRAND(err)); |
| 1008 |
0 |
} else { |
| 1009 |
0 |
AN(ctx->msg); |
| 1010 |
0 |
VSB_printf(ctx->msg, "%s\n", err); |
| 1011 |
|
}; |
| 1012 |
0 |
return (-1); |
| 1013 |
|
} |
| 1014 |
1400 |
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC); |
| 1015 |
1400 |
return (VRB_Cache(ctx->req, maxsize)); |
| 1016 |
1400 |
} |
| 1017 |
|
|
| 1018 |
|
/*-------------------------------------------------------------------- |
| 1019 |
|
* purges |
| 1020 |
|
*/ |
| 1021 |
|
|
| 1022 |
|
VCL_INT |
| 1023 |
560 |
VRT_purge(VRT_CTX, VCL_DURATION ttl, VCL_DURATION grace, VCL_DURATION keep) |
| 1024 |
|
{ |
| 1025 |
|
|
| 1026 |
560 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 1027 |
|
|
| 1028 |
560 |
if ((ctx->method & (VCL_MET_HIT|VCL_MET_MISS)) == 0) { |
| 1029 |
0 |
VRT_fail(ctx, |
| 1030 |
|
"purge can only happen in vcl_hit{} or vcl_miss{}"); |
| 1031 |
0 |
return (0); |
| 1032 |
|
} |
| 1033 |
|
|
| 1034 |
560 |
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC); |
| 1035 |
560 |
CHECK_OBJ_NOTNULL(ctx->req->wrk, WORKER_MAGIC); |
| 1036 |
1120 |
return (HSH_Purge(ctx->req->wrk, ctx->req->objcore->objhead, |
| 1037 |
560 |
ctx->req->t_req, ttl, grace, keep)); |
| 1038 |
560 |
} |
| 1039 |
|
|
| 1040 |
|
/*-------------------------------------------------------------------- |
| 1041 |
|
*/ |
| 1042 |
|
|
| 1043 |
|
struct vsmw_cluster * v_matchproto_() |
| 1044 |
49400 |
VRT_VSM_Cluster_New(VRT_CTX, size_t sz) |
| 1045 |
|
{ |
| 1046 |
|
struct vsmw_cluster *vc; |
| 1047 |
|
|
| 1048 |
49400 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 1049 |
49400 |
assert(sz > 0); |
| 1050 |
49400 |
AN(vsc_lock); |
| 1051 |
49400 |
AN(vsc_unlock); |
| 1052 |
49400 |
AN(heritage.proc_vsmw); |
| 1053 |
49400 |
vsc_lock(); |
| 1054 |
49400 |
vc = VSMW_NewCluster(heritage.proc_vsmw, sz, "VSC_cluster"); |
| 1055 |
49400 |
vsc_unlock(); |
| 1056 |
49400 |
return (vc); |
| 1057 |
|
} |
| 1058 |
|
|
| 1059 |
|
void v_matchproto_() |
| 1060 |
3107 |
VRT_VSM_Cluster_Destroy(VRT_CTX, struct vsmw_cluster **vsmcp) |
| 1061 |
|
{ |
| 1062 |
|
|
| 1063 |
3107 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 1064 |
3107 |
AN(vsmcp); |
| 1065 |
3107 |
VSMW_DestroyCluster(heritage.proc_vsmw, vsmcp); |
| 1066 |
3107 |
} |
| 1067 |
|
|
| 1068 |
|
/*-------------------------------------------------------------------- |
| 1069 |
|
* Simple stuff |
| 1070 |
|
*/ |
| 1071 |
|
|
| 1072 |
|
int |
| 1073 |
442193 |
VRT_strcmp(const char *s1, const char *s2) |
| 1074 |
|
{ |
| 1075 |
442193 |
if (s1 == NULL || s2 == NULL) |
| 1076 |
48000 |
return (1); |
| 1077 |
394193 |
return (strcmp(s1, s2)); |
| 1078 |
442193 |
} |
| 1079 |
|
|
| 1080 |
|
void |
| 1081 |
0 |
VRT_memmove(void *dst, const void *src, unsigned len) |
| 1082 |
|
{ |
| 1083 |
|
|
| 1084 |
0 |
(void)memmove(dst, src, len); |
| 1085 |
0 |
} |
| 1086 |
|
|
| 1087 |
|
VCL_BOOL |
| 1088 |
940 |
VRT_ipcmp(VRT_CTX, VCL_IP sua1, VCL_IP sua2) |
| 1089 |
|
{ |
| 1090 |
|
|
| 1091 |
940 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 1092 |
|
|
| 1093 |
940 |
if (sua1 == NULL || sua2 == NULL) { |
| 1094 |
0 |
VRT_fail(ctx, "%s: Illegal IP", __func__); |
| 1095 |
0 |
return (1); |
| 1096 |
|
} |
| 1097 |
940 |
return (VSA_Compare_IP(sua1, sua2)); |
| 1098 |
940 |
} |
| 1099 |
|
|
| 1100 |
|
/* |
| 1101 |
|
* the pointer passed as src must have at least VCL_TASK lifetime |
| 1102 |
|
*/ |
| 1103 |
|
VCL_BLOB |
| 1104 |
9880 |
VRT_blob(VRT_CTX, const char *err, const void *src, size_t len, unsigned type) |
| 1105 |
|
{ |
| 1106 |
|
struct vrt_blob *p; |
| 1107 |
|
|
| 1108 |
9880 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 1109 |
9880 |
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC); |
| 1110 |
|
|
| 1111 |
9880 |
if (src == NULL || len == 0) |
| 1112 |
80 |
return (vrt_null_blob); |
| 1113 |
|
|
| 1114 |
9800 |
p = (void *)WS_Alloc(ctx->ws, sizeof *p); |
| 1115 |
9800 |
if (p == NULL) { |
| 1116 |
240 |
VRT_fail(ctx, "Workspace overflow (%s)", err); |
| 1117 |
240 |
return (NULL); |
| 1118 |
|
} |
| 1119 |
|
|
| 1120 |
9560 |
INIT_OBJ(p, VRT_BLOB_MAGIC); |
| 1121 |
9560 |
p->type = type; |
| 1122 |
9560 |
p->len = len; |
| 1123 |
9560 |
p->blob = src; |
| 1124 |
|
|
| 1125 |
9560 |
return (p); |
| 1126 |
9880 |
} |
| 1127 |
|
|
| 1128 |
|
int |
| 1129 |
3863400 |
VRT_VSA_GetPtr(VRT_CTX, const struct suckaddr *sua, const unsigned char ** dst) |
| 1130 |
|
{ |
| 1131 |
|
|
| 1132 |
3863400 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 1133 |
3863400 |
AN(dst); |
| 1134 |
|
|
| 1135 |
3863400 |
if (sua == NULL) { |
| 1136 |
0 |
VRT_fail(ctx, "%s: Illegal IP", __func__); |
| 1137 |
0 |
*dst = NULL; |
| 1138 |
0 |
return (-1); |
| 1139 |
|
} |
| 1140 |
3863400 |
return (VSA_GetPtr(sua, dst)); |
| 1141 |
3863400 |
} |
| 1142 |
|
|
| 1143 |
|
void |
| 1144 |
80 |
VRT_Format_Proxy(struct vsb *vsb, VCL_INT version, VCL_IP sac, VCL_IP sas, |
| 1145 |
|
VCL_STRING auth) |
| 1146 |
|
{ |
| 1147 |
80 |
VPX_Format_Proxy(vsb, (int)version, sac, sas, auth); |
| 1148 |
80 |
} |
| 1149 |
|
|
| 1150 |
|
/* |
| 1151 |
|
* Clone a struct vrt_endpoint in a single malloc() allocation |
| 1152 |
|
*/ |
| 1153 |
|
|
| 1154 |
|
//lint -e{662} Possible of out-of-bounds pointer (___ beyond end of data) |
| 1155 |
|
//lint -e{826} Suspicious pointer-to-pointer conversion (area to o small |
| 1156 |
|
struct vrt_endpoint * |
| 1157 |
107600 |
VRT_Endpoint_Clone(const struct vrt_endpoint * const vep) |
| 1158 |
|
{ |
| 1159 |
|
size_t sz; |
| 1160 |
|
struct vrt_endpoint *nvep; |
| 1161 |
107600 |
struct vrt_blob *blob = NULL; |
| 1162 |
|
struct suckaddr *sa; |
| 1163 |
107600 |
size_t uds_len = 0; |
| 1164 |
|
char *p, *e; |
| 1165 |
|
|
| 1166 |
107600 |
CHECK_OBJ_NOTNULL(vep, VRT_ENDPOINT_MAGIC); |
| 1167 |
107600 |
sz = sizeof *nvep; |
| 1168 |
107600 |
if (vep->ipv4) |
| 1169 |
103640 |
sz += vsa_suckaddr_len; |
| 1170 |
107600 |
if (vep->ipv6) |
| 1171 |
320 |
sz += vsa_suckaddr_len; |
| 1172 |
107600 |
if (vep->uds_path != NULL) { |
| 1173 |
3720 |
uds_len = strlen(vep->uds_path) + 1; |
| 1174 |
3720 |
sz += uds_len; |
| 1175 |
3720 |
} |
| 1176 |
107600 |
if (vep->preamble != NULL && vep->preamble->len) { |
| 1177 |
720 |
sz += sizeof(*blob); |
| 1178 |
720 |
sz += vep->preamble->len; |
| 1179 |
720 |
} |
| 1180 |
107600 |
p = calloc(1, sz); |
| 1181 |
107600 |
AN(p); |
| 1182 |
107600 |
e = p + sz; |
| 1183 |
107600 |
nvep = (void*)p; |
| 1184 |
107600 |
p += sizeof *nvep; |
| 1185 |
107600 |
INIT_OBJ(nvep, VRT_ENDPOINT_MAGIC); |
| 1186 |
107600 |
if (vep->ipv4) { |
| 1187 |
103640 |
sa = (void*)p; |
| 1188 |
103640 |
memcpy(sa, vep->ipv4, vsa_suckaddr_len); |
| 1189 |
103640 |
nvep->ipv4 = sa; |
| 1190 |
103640 |
p += vsa_suckaddr_len; |
| 1191 |
103640 |
} |
| 1192 |
107600 |
if (vep->ipv6) { |
| 1193 |
320 |
sa = (void*)p; |
| 1194 |
320 |
memcpy(sa, vep->ipv6, vsa_suckaddr_len); |
| 1195 |
320 |
nvep->ipv6 = sa; |
| 1196 |
320 |
p += vsa_suckaddr_len; |
| 1197 |
320 |
} |
| 1198 |
107600 |
if (vep->preamble != NULL && vep->preamble->len) { |
| 1199 |
|
/* Before uds because we need p to be aligned still */ |
| 1200 |
720 |
blob = (void*)p; |
| 1201 |
720 |
INIT_OBJ(blob, VRT_BLOB_MAGIC); |
| 1202 |
720 |
p += sizeof(*blob); |
| 1203 |
720 |
nvep->preamble = blob; |
| 1204 |
720 |
memcpy(p, vep->preamble->blob, vep->preamble->len); |
| 1205 |
720 |
blob->type = 0x70ea5b1e; |
| 1206 |
720 |
blob->len = vep->preamble->len; |
| 1207 |
720 |
blob->blob = p; |
| 1208 |
720 |
p += vep->preamble->len; |
| 1209 |
720 |
} |
| 1210 |
107600 |
if (uds_len) { |
| 1211 |
3720 |
memcpy(p, vep->uds_path, uds_len); |
| 1212 |
3720 |
nvep->uds_path = p; |
| 1213 |
3720 |
p += uds_len; |
| 1214 |
3720 |
} |
| 1215 |
107600 |
assert(p == e); |
| 1216 |
107600 |
return (nvep); |
| 1217 |
|
} |