Skip to content

Commit e38c0f7

Browse files
committed
Generalize the VDP API: struct vrt_init_ctx
This commit is to prepare use of the VDP API also for the backend side to filter bereq.body through bereq.filters by adding a struct vrt_init_ctx argument to the VDP init function. For background, see varnishcache#4035
1 parent 1659215 commit e38c0f7

File tree

11 files changed

+173
-85
lines changed

11 files changed

+173
-85
lines changed

bin/varnishd/cache/cache_deliver_proc.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,16 @@ VDP_Fini(const struct vdp_ctx *vdc)
7070
}
7171

7272
void
73-
VDP_Init(struct vdp_ctx *vdc, struct worker *wrk, struct vsl_log *vsl,
74-
struct req *req)
73+
VDP_Init(struct vdp_ctx *vdc, struct worker *wrk, struct vsl_log *vsl)
7574
{
7675
AN(vdc);
7776
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
7877
AN(vsl);
79-
CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
8078

8179
INIT_OBJ(vdc, VDP_CTX_MAGIC);
8280
VTAILQ_INIT(&vdc->vdp);
8381
vdc->wrk = wrk;
8482
vdc->vsl = vsl;
85-
vdc->req = req;
8683
}
8784

8885
/* VDP_bytes
@@ -146,8 +143,9 @@ VDP_bytes(struct vdp_ctx *vdc, enum vdp_action act,
146143

147144
int
148145
VDP_Push(VRT_CTX, struct vdp_ctx *vdc, struct ws *ws, const struct vdp *vdp,
149-
void *priv)
146+
void *priv, struct objcore *oc, struct http *hd, intmax_t *cl)
150147
{
148+
struct vdp_init_ctx initctx[1];
151149
struct vdp_entry *vdpe;
152150

153151
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
@@ -157,6 +155,19 @@ VDP_Push(VRT_CTX, struct vdp_ctx *vdc, struct ws *ws, const struct vdp *vdp,
157155
AN(vdp->name);
158156
AN(vdp->bytes);
159157

158+
CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
159+
CHECK_OBJ_NOTNULL(hd, HTTP_MAGIC);
160+
AN(cl);
161+
assert(*cl >= -1);
162+
163+
if (vdc->retval)
164+
return (vdc->retval);
165+
166+
INIT_OBJ(initctx, VDP_INIT_CTX_MAGIC);
167+
initctx->oc = oc;
168+
initctx->hd = hd;
169+
initctx->cl = cl;
170+
160171
if (vdc->retval)
161172
return (vdc->retval);
162173

@@ -175,12 +186,15 @@ VDP_Push(VRT_CTX, struct vdp_ctx *vdc, struct ws *ws, const struct vdp *vdp,
175186
VTAILQ_INSERT_TAIL(&vdc->vdp, vdpe, list);
176187
vdc->nxt = VTAILQ_FIRST(&vdc->vdp);
177188

189+
if (vdc->nxt != vdpe)
190+
initctx->oc = NULL;
191+
178192
AZ(vdc->retval);
179193
if (vdpe->vdp->init == NULL)
180-
return (vdc->retval);
181-
182-
vdc->retval = vdpe->vdp->init(ctx, vdc, &vdpe->priv,
183-
vdpe == vdc->nxt ? vdc->req->objcore : NULL);
194+
vdc->retval = 0;
195+
else
196+
vdc->retval = vdpe->vdp->init(ctx, vdc, &vdpe->priv, initctx);
197+
initctx->oc = NULL;
184198

185199
if (vdc->retval > 0) {
186200
VTAILQ_REMOVE(&vdc->vdp, vdpe, list);
@@ -254,7 +268,6 @@ VDP_DeliverObj(struct vdp_ctx *vdc, struct objcore *oc)
254268
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
255269
CHECK_OBJ_NOTNULL(vdc->wrk, WORKER_MAGIC);
256270
AN(vdc->vsl);
257-
vdc->req = NULL;
258271
final = oc->flags & (OC_F_PRIVATE | OC_F_HFM | OC_F_HFP) ? 1 : 0;
259272
r = ObjIterate(vdc->wrk, oc, vdc, vdp_objiterate, final);
260273
if (r < 0)

bin/varnishd/cache/cache_esi_deliver.c

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -261,35 +261,43 @@ ved_decode_len(struct vsl_log *vsl, const uint8_t **pp)
261261
*/
262262

263263
static int v_matchproto_(vdp_init_f)
264-
ved_vdp_esi_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, struct objcore *oc)
264+
ved_vdp_esi_init(VRT_CTX, struct vdp_ctx *vdc, void **priv,
265+
const struct vdp_init_ctx *initctx)
265266
{
266267
struct ecx *ecx;
267-
struct req *req;
268268

269269
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
270+
CHECK_OBJ_ORNULL(ctx->req, REQ_MAGIC);
270271
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
271-
CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
272-
if (oc == NULL || !ObjHasAttr(vdc->wrk, oc, OA_ESIDATA))
273-
return (1);
274-
275-
req = vdc->req;
276-
CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
272+
CHECK_OBJ_NOTNULL(initctx, VDP_INIT_CTX_MAGIC);
273+
CHECK_OBJ_ORNULL(initctx->oc, OBJCORE_MAGIC);
274+
CHECK_OBJ_NOTNULL(initctx->hd, HTTP_MAGIC);
275+
AN(initctx->cl);
277276
AN(priv);
277+
278278
AZ(*priv);
279+
if (initctx->oc == NULL || !ObjHasAttr(vdc->wrk, initctx->oc, OA_ESIDATA))
280+
return (1);
281+
282+
if (ctx->req == NULL) {
283+
VSLb(vdc->vsl, SLT_Error,
284+
"esi can only be used on the client side");
285+
return (1);
286+
}
279287

280288
ALLOC_OBJ(ecx, ECX_MAGIC);
281289
AN(ecx);
282290
assert(sizeof gzip_hdr == 10);
283-
ecx->preq = req;
291+
ecx->preq = ctx->req;
284292
*priv = ecx;
285-
RFC2616_Weaken_Etag(req->resp);
286-
287-
req->res_mode |= RES_ESI;
288-
if (req->resp_len != 0)
289-
req->resp_len = -1;
290-
if (req->esi_level > 0) {
291-
assert(req->transport == &VED_transport);
292-
CAST_OBJ_NOTNULL(ecx->pecx, req->transport_priv, ECX_MAGIC);
293+
RFC2616_Weaken_Etag(initctx->hd);
294+
295+
ctx->req->res_mode |= RES_ESI;
296+
if (*initctx->cl != 0)
297+
*initctx->cl = -1;
298+
if (ctx->req->esi_level > 0) {
299+
assert(ctx->req->transport == &VED_transport);
300+
CAST_OBJ_NOTNULL(ecx->pecx, ctx->req->transport_priv, ECX_MAGIC);
293301
if (!ecx->pecx->isgzip)
294302
ecx->pecx = NULL;
295303
}
@@ -604,18 +612,19 @@ struct ved_foo {
604612
};
605613

606614
static int v_matchproto_(vdp_init_f)
607-
ved_gzgz_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, struct objcore *oc)
615+
ved_gzgz_init(VRT_CTX, struct vdp_ctx *vdc, void **priv,
616+
const struct vdp_init_ctx *initctx)
608617
{
609618
ssize_t l;
610619
const char *p;
611620
struct ved_foo *foo;
612-
struct req *req;
613621

614622
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
615623
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
616-
(void)oc;
617-
req = vdc->req;
618-
CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
624+
AN(priv);
625+
CHECK_OBJ_NOTNULL(initctx, VDP_INIT_CTX_MAGIC);
626+
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
627+
619628
CAST_OBJ_NOTNULL(foo, *priv, VED_FOO_MAGIC);
620629
CHECK_OBJ_NOTNULL(foo->objcore, OBJCORE_MAGIC);
621630

@@ -872,6 +881,8 @@ ved_deliver(struct req *req, struct boc *boc, int wantbody)
872881
struct ecx *ecx;
873882
struct ved_foo foo[1];
874883
struct vrt_ctx ctx[1];
884+
const struct vdp *vdp;
885+
void *priv;
875886

876887
CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
877888
CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
@@ -924,16 +935,21 @@ ved_deliver(struct req *req, struct boc *boc, int wantbody)
924935
INIT_OBJ(foo, VED_FOO_MAGIC);
925936
foo->ecx = ecx;
926937
foo->objcore = req->objcore;
927-
i = VDP_Push(ctx, req->vdc, req->ws, &ved_gzgz, foo);
928-
938+
vdp = &ved_gzgz;
939+
priv = foo;
929940
} else if (ecx->isgzip && !i) {
930941
/* Non-Gzip'ed include in gzip'ed parent */
931-
i = VDP_Push(ctx, req->vdc, req->ws, &ved_pretend_gz, ecx);
942+
vdp = &ved_pretend_gz;
943+
priv = ecx;
932944
} else {
933945
/* Anything else goes straight through */
934-
i = VDP_Push(ctx, req->vdc, req->ws, &ved_ved, ecx);
946+
vdp = &ved_ved;
947+
priv = ecx;
935948
}
936949

950+
i = VDP_Push(ctx, req->vdc, req->ws, vdp, priv,
951+
req->objcore, req->resp, &req->resp_len);
952+
937953
if (i == 0) {
938954
i = VDP_DeliverObj(req->vdc, req->objcore);
939955
} else {

bin/varnishd/cache/cache_filter.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct req;
3333
struct vfp_entry;
3434
struct vfp_ctx;
3535
struct vdp_ctx;
36+
struct vdp_init_ctx;
3637

3738
/* Fetch processors --------------------------------------------------*/
3839

@@ -104,8 +105,9 @@ enum vdp_action {
104105
VDP_END, /* Last buffer or after, implies VDP_FLUSH */
105106
};
106107

108+
107109
typedef int vdp_init_f(VRT_CTX, struct vdp_ctx *, void **priv,
108-
struct objcore *);
110+
const struct vdp_init_ctx *);
109111
/*
110112
* Return value:
111113
* negative: Error - abandon delivery
@@ -147,7 +149,14 @@ struct vdp_ctx {
147149
struct vdp_entry *nxt;
148150
struct worker *wrk;
149151
struct vsl_log *vsl;
150-
struct req *req;
152+
};
153+
154+
struct vdp_init_ctx {
155+
unsigned magic;
156+
#define VDP_INIT_CTX_MAGIC 0x3b876157
157+
struct objcore *oc; // Only first filter, if at all
158+
struct http *hd;
159+
intmax_t *cl;
151160
};
152161

153162
int VDP_bytes(struct vdp_ctx *, enum vdp_action act, const void *, ssize_t);

bin/varnishd/cache/cache_gzip.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,23 @@ VGZ_Gzip(struct vgz *vg, const void **pptr, ssize_t *plen, enum vgz_flag flags)
288288
*/
289289

290290
static int v_matchproto_(vdp_init_f)
291-
vdp_gunzip_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, struct objcore *oc)
291+
vdp_gunzip_init(VRT_CTX, struct vdp_ctx *vdc, void **priv,
292+
const struct vdp_init_ctx *initctx)
292293
{
293294
struct vgz *vg;
294295
struct boc *boc;
295-
struct req *req;
296296
enum boc_state_e bos;
297297
const char *p;
298298
ssize_t dl;
299299
uint64_t u;
300300

301301
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
302302
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
303-
CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
304-
req = vdc->req;
305-
CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
303+
CHECK_OBJ_NOTNULL(initctx, VDP_INIT_CTX_MAGIC);
304+
CHECK_OBJ_ORNULL(initctx->oc, OBJCORE_MAGIC);
305+
CHECK_OBJ_NOTNULL(initctx->hd, HTTP_MAGIC);
306+
AN(initctx->cl);
307+
AN(priv);
306308

307309
vg = VGZ_NewGunzip(vdc->vsl, "U D -");
308310
AN(vg);
@@ -314,27 +316,27 @@ vdp_gunzip_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, struct objcore *oc)
314316
VGZ_Obuf(vg, vg->m_buf, vg->m_sz);
315317
*priv = vg;
316318

317-
http_Unset(req->resp, H_Content_Encoding);
319+
http_Unset(initctx->hd, H_Content_Encoding);
318320

319-
req->resp_len = -1;
321+
*initctx->cl = -1;
320322

321-
if (oc == NULL)
323+
if (initctx->oc == NULL)
322324
return (0);
323325

324-
boc = HSH_RefBoc(oc);
326+
boc = HSH_RefBoc(initctx->oc);
325327
if (boc != NULL) {
326328
CHECK_OBJ(boc, BOC_MAGIC);
327329
bos = boc->state;
328-
HSH_DerefBoc(vdc->wrk, oc);
330+
HSH_DerefBoc(vdc->wrk, initctx->oc);
329331
if (bos < BOS_FINISHED)
330332
return (0); /* OA_GZIPBITS is not stable yet */
331333
}
332334

333-
p = ObjGetAttr(vdc->wrk, oc, OA_GZIPBITS, &dl);
335+
p = ObjGetAttr(vdc->wrk, initctx->oc, OA_GZIPBITS, &dl);
334336
if (p != NULL && dl == 32) {
335337
u = vbe64dec(p + 24);
336338
if (u != 0)
337-
req->resp_len = u;
339+
*initctx->cl = u;
338340
}
339341
return (0);
340342
}

bin/varnishd/cache/cache_range.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,33 +196,43 @@ vrg_ifrange(struct req *req)
196196
}
197197

198198
static int v_matchproto_(vdp_init_f)
199-
vrg_range_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, struct objcore *oc)
199+
vrg_range_init(VRT_CTX, struct vdp_ctx *vdc, void **priv,
200+
const struct vdp_init_ctx *initctx)
200201
{
201202
const char *err;
202-
struct req *req;
203203

204204
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
205+
CHECK_OBJ_ORNULL(ctx->req, REQ_MAGIC);
205206
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
206-
(void)oc;
207-
req = vdc->req;
208-
CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
209-
if (!vrg_ifrange(req)) // rfc7233,l,455,456
207+
AN(priv);
208+
CHECK_OBJ_NOTNULL(initctx, VDP_INIT_CTX_MAGIC);
209+
210+
if (ctx->req == NULL) {
211+
VSLb(vdc->vsl, SLT_Error,
212+
"range can only be used on the client side");
213+
return (1);
214+
}
215+
216+
// not using initctx->{hd,cl}, because range needs req anyway for
217+
// Req_Fail()
218+
219+
if (!vrg_ifrange(ctx->req)) // rfc7233,l,455,456
210220
return (1);
211-
err = vrg_dorange(req, priv);
221+
err = vrg_dorange(ctx->req, priv);
212222
if (err == NULL)
213223
return (*priv == NULL ? 1 : 0);
214224

215225
VSLb(vdc->vsl, SLT_Debug, "RANGE_FAIL %s", err);
216-
if (req->resp_len >= 0)
217-
http_PrintfHeader(req->resp,
226+
if (ctx->req->resp_len >= 0)
227+
http_PrintfHeader(ctx->req->resp,
218228
"Content-Range: bytes */%jd",
219-
(intmax_t)req->resp_len);
220-
http_PutResponse(req->resp, "HTTP/1.1", 416, NULL);
229+
(intmax_t)ctx->req->resp_len);
230+
http_PutResponse(ctx->req->resp, "HTTP/1.1", 416, NULL);
221231
/*
222232
* XXX: We ought to produce a body explaining things.
223233
* XXX: That really calls for us to hit vcl_synth{}
224234
*/
225-
req->resp_len = 0;
235+
ctx->req->resp_len = 0;
226236
return (1);
227237
}
228238

bin/varnishd/cache/cache_req_fsm.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,12 @@ cnt_transmit(struct worker *wrk, struct req *req)
460460
sendbody = 1;
461461
}
462462

463-
VDP_Init(req->vdc, req->wrk, req->vsl, req);
463+
VDP_Init(req->vdc, req->wrk, req->vsl);
464464
if (req->vdp_filter_list == NULL)
465465
req->vdp_filter_list = resp_Get_Filter_List(req);
466466
if (req->vdp_filter_list == NULL ||
467-
VCL_StackVDP(req, req->vcl, req->vdp_filter_list)) {
467+
VCL_StackVDP(req->vdc, req->vcl, req->vdp_filter_list,
468+
req, NULL, &req->resp_len)) {
468469
VSLb(req->vsl, SLT_Error, "Failure to push processors");
469470
req->doclose = SC_OVERLOAD;
470471
req->acct.resp_bodybytes +=

bin/varnishd/cache/cache_varnishd.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,11 @@ void VDI_Init(void);
188188

189189
/* cache_deliver_proc.c */
190190
void VDP_Fini(const struct vdp_ctx *vdc);
191-
void VDP_Init(struct vdp_ctx *vdc, struct worker *wrk, struct vsl_log *vsl,
192-
struct req *req);
191+
void VDP_Init(struct vdp_ctx *vdc, struct worker *wrk, struct vsl_log *vsl);
193192
uint64_t VDP_Close(struct vdp_ctx *, struct objcore *, struct boc *);
194193
void VDP_Panic(struct vsb *vsb, const struct vdp_ctx *vdc);
195-
int VDP_Push(VRT_CTX, struct vdp_ctx *, struct ws *, const struct vdp *,
196-
void *priv);
194+
int VDP_Push(VRT_CTX, struct vdp_ctx *vdc, struct ws *ws, const struct vdp *vdp,
195+
void *priv, struct objcore *oc, struct http *hd, intmax_t *cl);
197196
int VDP_DeliverObj(struct vdp_ctx *vdc, struct objcore *oc);
198197
extern const struct vdp VDP_gunzip;
199198
extern const struct vdp VDP_esi;
@@ -507,7 +506,8 @@ void pan_privs(struct vsb *, const struct vrt_privs *);
507506

508507
/* cache_vrt_filter.c */
509508
int VCL_StackVFP(struct vfp_ctx *, const struct vcl *, const char *);
510-
int VCL_StackVDP(struct req *, const struct vcl *, const char *);
509+
int VCL_StackVDP(struct vdp_ctx *vdc, const struct vcl *vcl, const char *fl,
510+
struct req *req, struct busyobj *bo, intmax_t *cl);
511511
const char *resp_Get_Filter_List(struct req *req);
512512
void VCL_VRT_Init(void);
513513

0 commit comments

Comments
 (0)