[master] 7c4cdd485 demo and test use of the stringified session attributes

Nils Goroll nils.goroll at uplex.de
Sat Dec 21 10:41:08 UTC 2019


commit 7c4cdd48563095fb70ff5db642288ae13a06f2ce
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Sat Dec 21 11:30:38 2019 +0100

    demo and test use of the stringified session attributes
    
    While, in most of the code, we now use SA_*_ADDR and format the
    address/port when needed, for the client ip and port, there still exist
    formatted strings as session attributes.
    
    I recently came across this in a different context and wondered if we
    should remove these string session attributes, but as we use them multiple
    times for each request, that would only imply additional overhead for
    repeated string formatting.  So I think we should keep them for now,
    but we might want to consider to add to struct suckaddr optional
    formatted strings to avoid the duplicated formatting which already
    happens.
    
    This commit adds a regression test.
    
    Motivated by #3173

diff --git a/bin/varnishtest/tests/b00070.vtc b/bin/varnishtest/tests/b00070.vtc
new file mode 100644
index 000000000..df7d3d5f7
--- /dev/null
+++ b/bin/varnishtest/tests/b00070.vtc
@@ -0,0 +1,23 @@
+varnishtest "client.ip vs. string session attrs"
+
+varnish v1 -vcl {
+	import debug;
+	import std;
+	backend dummy None;
+	sub vcl_recv {
+		return (synth(200));
+	}
+	sub vcl_synth {
+		set resp.http.ci = client.ip;
+		set resp.http.s-ci = debug.client_ip();
+		set resp.http.pi = std.port(client.ip);
+		set resp.http.s-pi = debug.client_port();
+	}
+} -start
+
+client c1 {
+	txreq
+	rxresp
+	expect resp.http.ci == resp.http.s-ci
+	expect resp.http.pi == resp.http.s-pi
+} -run
diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index 72b24b3ca..c58397fcb 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -274,3 +274,11 @@ $Object director()
 $Method BACKEND .fail()
 
 Return a backend which fails in director context
+
+$Function STRING client_ip()
+
+Get the stringified client ip from the session attr
+
+$Function STRING client_port()
+
+Get the stringified client port from the session attr
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index 1a547e172..5b85a6fc8 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -1011,3 +1011,19 @@ vmod_debug_director_resolve(VRT_CTX, VCL_BACKEND dir)
 	VRT_fail(ctx, "fail");
 	return (NULL);
 }
+
+VCL_STRING v_matchproto_(td_xyzzy_debug_client_ip)
+xyzzy_client_ip(VRT_CTX)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+	return (SES_Get_String_Attr(ctx->sp, SA_CLIENT_IP));
+}
+
+VCL_STRING v_matchproto_(td_xyzzy_debug_client_port)
+xyzzy_client_port(VRT_CTX)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+	return (SES_Get_String_Attr(ctx->sp, SA_CLIENT_PORT));
+}


More information about the varnish-commit mailing list