[master] 2267706 Add INT std.port(IP) which extracts the TCP port number.

Poul-Henning Kamp phk at varnish-cache.org
Fri Nov 22 16:38:00 CET 2013


commit 2267706254f63894d3735707a90b85c7e5b69fe5
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri Nov 22 15:35:58 2013 +0000

    Add INT std.port(IP) which extracts the TCP port number.
    
    By default IP renders as STRING with just the IP number, so if you
    want to get the port number there too, you have to do something
    like:
    
    	set resp.http.where = client.ip + ":" + std.port(client.ip);
    
    Not too elegant, but it will do.
    
    Remove the "hard-coded" "foo.port" VCL variables.

diff --git a/bin/varnishd/cache/cache_vrt_var.c b/bin/varnishd/cache/cache_vrt_var.c
index 5a80ff0..5e67e76 100644
--- a/bin/varnishd/cache/cache_vrt_var.c
+++ b/bin/varnishd/cache/cache_vrt_var.c
@@ -40,7 +40,6 @@
 #include "cache_backend.h"
 #include "vrt.h"
 #include "vrt_obj.h"
-#include "vsa.h"
 
 static char vrt_hostname[255] = "";
 
@@ -265,16 +264,6 @@ VRT_r_beresp_backend_ip(const struct vrt_ctx *ctx)
 	return(ctx->bo->vbc->addr);
 }
 
-long
-VRT_r_beresp_backend_port(const struct vrt_ctx *ctx)
-{
-
-	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
-	CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
-	CHECK_OBJ_NOTNULL(ctx->bo->vbc, VBC_MAGIC);
-	return (VSA_Port(ctx->bo->vbc->addr));
-}
-
 const char *
 VRT_r_beresp_storage(const struct vrt_ctx *ctx)
 {
@@ -546,20 +535,6 @@ VRT_r_server_hostname(const struct vrt_ctx *ctx)
 	return (vrt_hostname);
 }
 
-/*--------------------------------------------------------------------
- * XXX: This is pessimistically silly
- */
-
-long
-VRT_r_server_port(const struct vrt_ctx *ctx)
-{
-
-	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
-	CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
-	AN(ctx->req->sp->local_addr);
-	return (VSA_Port(ctx->req->sp->local_addr));
-}
-
 /*--------------------------------------------------------------------*/
 
 #define VOBJ_L(type, field)						\
diff --git a/bin/varnishtest/tests/v00018.vtc b/bin/varnishtest/tests/v00018.vtc
index 748e92f..c264cca 100644
--- a/bin/varnishtest/tests/v00018.vtc
+++ b/bin/varnishtest/tests/v00018.vtc
@@ -12,9 +12,9 @@ varnish v1 -vcl {
 	sub vcl_hit { return(error(100,"the butter please")); }
 }
 
-varnish v1 -errvcl {Variable 'server.port' is read only.} {
+varnish v1 -errvcl {Variable 'now' is read only.} {
 	backend b { .host = "127.0.0.1"; }
-	sub vcl_miss { set server.port = 1000; }
+	sub vcl_miss { set now = 1000; }
 }
 
 varnish v1 -vcl {
@@ -34,7 +34,7 @@ varnish v1 -errvcl {Expected '=' got '+='} {
 
 varnish v1 -errvcl {Expected '=' got '+='} {
 	backend b { .host = "127.0.0.1"; }
-	sub vcl_recv { set req.url += server.port; }
+	sub vcl_recv { set req.url += now; }
 }
 
 varnish v1 -errvcl {Expected ';' got 'if'} {
diff --git a/bin/varnishtest/tests/v00025.vtc b/bin/varnishtest/tests/v00025.vtc
index 23c2530..b090502 100644
--- a/bin/varnishtest/tests/v00025.vtc
+++ b/bin/varnishtest/tests/v00025.vtc
@@ -7,8 +7,10 @@ server s1 {
 
 varnish v1 -vcl+backend {
 
+	import ${vmod_std};
+
 	sub vcl_deliver {
-		set resp.http.server_port = server.port;
+		set resp.http.server_port = std.port(server.ip);
 	}
 
 	sub vcl_backend_response {
diff --git a/lib/libvcc/generate.py b/lib/libvcc/generate.py
index a41c246..d7b0de9 100755
--- a/lib/libvcc/generate.py
+++ b/lib/libvcc/generate.py
@@ -142,14 +142,6 @@ The client's IP address.
 		specified by the -n parameter.
 		"""
 	),
-	('server.port',
-		'INT',
-		( 'client',),
-		( ), """
-		The port number of the socket on which the client
-		connection was received.
-		"""
-	),
 	('req.method',
 		'STRING',
 		( 'client',),
@@ -424,13 +416,6 @@ The client's IP address.
 		IP of the backend this response was fetched from.
 		"""
 	),
-	('beresp.backend.port',
-		'INT',
-		( 'backend_response',),
-		( ), """
-		Port of the backend this response was fetched from.
-		"""
-	),
 	('beresp.storage',
 		'STRING',
 		( 'backend_response',),
diff --git a/lib/libvmod_std/vmod.vcc b/lib/libvmod_std/vmod.vcc
index f07b8c2..7db1ab0 100644
--- a/lib/libvmod_std/vmod.vcc
+++ b/lib/libvmod_std/vmod.vcc
@@ -38,3 +38,4 @@ Function INT integer(STRING, INT)
 Function VOID collect(HEADER)
 Function IP ip(STRING, IP)
 Function BOOL healthy(BACKEND)
+Function INT port(IP)
diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index 9d20b95..d0ec3d3 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -40,6 +40,7 @@
 
 #include "vrt.h"
 #include "vtcp.h"
+#include "vsa.h"
 
 #include "cache/cache.h"
 #include "cache/cache_backend.h"
@@ -194,3 +195,12 @@ vmod_healthy(const struct vrt_ctx *ctx, VCL_BACKEND be)
 	CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC);
 	return (VDI_Healthy(be));
 }
+
+VCL_INT __match_proto__(td_std_port)
+vmod_port(const struct vrt_ctx *ctx, VCL_IP ip)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	if (ip == NULL)
+		return (0);
+	return (VSA_Port(ip));
+}



More information about the varnish-commit mailing list