[master] 6b3e26c7d vmod_debug: obj.test_priv_top(): use VRT_priv_top_get() and workspace

Nils Goroll nils.goroll at uplex.de
Wed Jan 13 15:35:08 UTC 2021


commit 6b3e26c7d42c204fae134a1b6dc73c9ac885ed3b
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Wed Jan 13 15:58:24 2021 +0100

    vmod_debug: obj.test_priv_top(): use VRT_priv_top_get() and workspace
    
    Adjust our PRIV_TOP test to use VRT_priv_top_get() for read operation
    and copy the saved value onto the top request's workspace if need to
    (workspaces differ and the string to be saved is on the current
    request's workspace).

diff --git a/bin/varnishtest/tests/v00043.vtc b/bin/varnishtest/tests/v00043.vtc
index 949e24bfc..8421d890d 100644
--- a/bin/varnishtest/tests/v00043.vtc
+++ b/bin/varnishtest/tests/v00043.vtc
@@ -8,6 +8,7 @@ server s1 {
 	expect req.http.x0 == "/a0"
 	expect req.http.x1 == "/a0"
 	expect req.http.o1 == <undef>
+	expect req.http.o2 == "/a0"
 	txresp -body {
 		<html>
 		<esi:include src="/foo1"/>
@@ -19,6 +20,7 @@ server s1 {
 	expect req.http.x0 == "/a0"
 	expect req.http.x1 == "/a0"
 	expect req.http.o1 == "/foo11"
+	expect req.http.o2 == "/a0"
 	txresp -body {
 		<html>
 		<esi:include src="/bar"/>
@@ -29,6 +31,7 @@ server s1 {
 	expect req.http.x0 == "/a0"
 	expect req.http.x1 == "/a0"
 	expect req.http.o1 == "/foo11"
+	expect req.http.o2 == "/a0"
 	txresp
 
 	rxreq
@@ -36,6 +39,7 @@ server s1 {
 	expect req.http.x0 == "/a0"
 	expect req.http.x1 == "/a0"
 	expect req.http.o1 == "/foo11"
+	expect req.http.o2 == "/a0"
 	txresp -body {
 		<html>
 		<esi:include src="/bar"/>
@@ -46,6 +50,7 @@ server s1 {
 	expect req.http.x0 == "/b0"
 	expect req.http.x1 == "/b0"
 	expect req.http.o1 == <undef>
+	expect req.http.o2 == "/b0"
 
 	txresp
 } -start
@@ -62,16 +67,16 @@ varnish v1 -cliok "param.set debug +syncvsl" -vcl+backend {
 		set req.http.x0 = debug.test_priv_top(req.url + req.esi_level);
 		if (req.url == "/foo1") {
 			o.test_priv_top(req.url + req.esi_level);
-		} else {
+		}
+		if (req.esi_level == 0) {
 			o2.test_priv_top(req.url + req.esi_level);
 		}
 	}
 
 	sub vcl_miss {
 		set req.http.x1 = debug.test_priv_top("");
-		if (req.esi_level > 0) {
-			set req.http.o1 = o.test_priv_top("");
-		}
+		set req.http.o1 = o.test_priv_top("");
+		set req.http.o2 = o2.test_priv_top("");
 	}
 
 	sub vcl_backend_response {
diff --git a/vmod/vmod_debug_obj.c b/vmod/vmod_debug_obj.c
index 61dc8a454..b7cb49eef 100644
--- a/vmod/vmod_debug_obj.c
+++ b/vmod/vmod_debug_obj.c
@@ -211,29 +211,80 @@ xyzzy_obj_test_priv_task(VRT_CTX, struct xyzzy_debug_obj *o, VCL_STRING s)
 	return (p->priv);
 }
 
+static void
+obj_priv_top_fini(void *ptr)
+{
+	AN(ptr);
+	VSL(SLT_Debug, 0, "obj_priv_top_fini(%p = \"%s\")", ptr, (char *)ptr);
+}
+
 static const struct vmod_priv_methods xyzzy_obj_test_priv_top_methods[1] = {{
 		.magic = VMOD_PRIV_METHODS_MAGIC,
 		.type = "debug_obj_test_priv_top",
-		.fini = free
+		.fini = obj_priv_top_fini
 }};
 
 VCL_STRING v_matchproto_()
 xyzzy_obj_test_priv_top(VRT_CTX, struct xyzzy_debug_obj *o, VCL_STRING s)
 {
 	struct vmod_priv *p;
+	struct req *req;
+	struct ws *ws;
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+	req = ctx->req;
+	if (req == NULL) {
+		VRT_fail(ctx, "%s.priv_top() can only be used "
+		    "in client VCL context", o->vcl_name);
+		return ("");
+	}
+	CHECK_OBJ(req, REQ_MAGIC);
+
+	if (s == NULL || *s == '\0') {
+		p = VRT_priv_top_get(ctx, o);
+		if (p == NULL) {
+			VSLb(ctx->vsl, SLT_Debug, "%s.priv_top() = NULL",
+			    o->vcl_name);
+			return ("");
+		}
+		assert(p->methods == xyzzy_obj_test_priv_top_methods);
+		VSLb(ctx->vsl, SLT_Debug,
+		    "%s.priv_top() = %p .priv = %p (\"%s\")",
+		    o->vcl_name, p, p->priv, p->priv);
+		return (p->priv);
+	}
 
 	p = VRT_priv_top(ctx, o);
+	if (p == NULL)
+		VSLb(ctx->vsl, SLT_Debug, "%s.priv_top() = NULL [err]",
+		    o->vcl_name);
 
-	if (p == NULL) {
-		VRT_fail(ctx, "no priv task - out of ws?");
+	CHECK_OBJ_NOTNULL(req->top, REQTOP_MAGIC);
+	req = req->top->topreq;
+	CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
+	ws = req->ws;
+
+	/* copy to top req's workspace if need to */
+	if (ctx->ws != ws && WS_Inside(ctx->ws, s, NULL))
+		s = WS_Copy(ws, s, -1);
+
+	if (p == NULL || s == NULL) {
+		VRT_fail(ctx, "out of ws?");
 		return ("");
 	}
 
-	if (p->priv == NULL) {
-		p->priv = strdup(s);
+	VSLb(ctx->vsl, SLT_Debug,
+	    "%s.priv_top() = %p .priv = %p (\"%s\") [%s]",
+	    o->vcl_name, p, s, s, p->priv ? "update" : "new");
+
+	if (p->priv == NULL)
 		p->methods = xyzzy_obj_test_priv_top_methods;
-	}
-	assert (p->methods == xyzzy_obj_test_priv_top_methods);
+	else
+		assert(p->methods == xyzzy_obj_test_priv_top_methods);
+
+	p->priv = TRUST_ME(s);
+
 	return (p->priv);
 }
 


More information about the varnish-commit mailing list