[master] cd44549 Expose the hash via {bereq,req}.hash

Federico G. Schwindt fgsch at lodoss.net
Sat Aug 19 11:40:06 CEST 2017


commit cd44549432ec809c508b5966c80347e6ff47b582
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Sat Aug 19 10:23:41 2017 +0100

    Expose the hash via {bereq,req}.hash
    
    This is available after vcl_hash{}. Tests will follow.

diff --git a/bin/varnishd/cache/cache_vrt_var.c b/bin/varnishd/cache/cache_vrt_var.c
index f99b2a6..6cc635d 100644
--- a/bin/varnishd/cache/cache_vrt_var.c
+++ b/bin/varnishd/cache/cache_vrt_var.c
@@ -30,12 +30,15 @@
  */
 #include "config.h"
 
+#include <stdio.h>
+
 #include "cache.h"
 #include "common/heritage.h"
 
 #include "cache_director.h"
 #include "vrt.h"
 #include "vrt_obj.h"
+#include "vsha256.h"
 
 static char vrt_hostname[255] = "";
 
@@ -761,6 +764,40 @@ VRT_BODY_L(resp)
 
 /*--------------------------------------------------------------------*/
 
+const char *
+VRT_r_req_hash(VRT_CTX)
+{
+	char *p;
+	int i;
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
+	p = WS_Alloc(ctx->ws, SHA256_LEN * 2 + 1);
+	if (p == NULL)
+		return (NULL);
+	for (i = 0; i < SHA256_LEN; i++)
+		sprintf(&p[i * 2], "%02x", ctx->req->digest[i]);
+	return (p);
+}
+
+const char *
+VRT_r_bereq_hash(VRT_CTX)
+{
+	char *p;
+	int i;
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
+	p = WS_Alloc(ctx->ws, SHA256_LEN * 2 + 1);
+	if (p == NULL)
+		return (NULL);
+	for (i = 0; i < SHA256_LEN; i++)
+		sprintf(&p[i * 2], "%02x", ctx->bo->digest[i]);
+	return (p);
+}
+
+/*--------------------------------------------------------------------*/
+
 #define HTTP_VAR(x)						\
 struct http *							\
 VRT_r_##x(VRT_CTX)						\
diff --git a/bin/varnishtest/tests/v00016.vtc b/bin/varnishtest/tests/v00016.vtc
index e8a57a6..dd81a8c 100644
--- a/bin/varnishtest/tests/v00016.vtc
+++ b/bin/varnishtest/tests/v00016.vtc
@@ -41,9 +41,9 @@ varnish v1 -errvcl {Operator > not possible on BACKEND} {
 	sub vcl_recv { if (a > b) { } }
 }
 
-varnish v1 -errvcl {Symbol not found: 'req.hash' (expected type BOOL):} {
+varnish v1 -errvcl {Symbol not found: 'req.foo' (expected type BOOL):} {
 	backend b { .host = "127.0.0.1"; }
-	sub vcl_hash { if (req.hash != "foo") { } }
+	sub vcl_hash { if (req.foo != "bar") { } }
 }
 
 varnish v1 -errvcl {Symbol not found: 'foo.bar'} {
diff --git a/bin/varnishtest/tests/v00018.vtc b/bin/varnishtest/tests/v00018.vtc
index a0bbf02..3e84fb0 100644
--- a/bin/varnishtest/tests/v00018.vtc
+++ b/bin/varnishtest/tests/v00018.vtc
@@ -38,9 +38,9 @@ varnish v1 -errvcl {Expected ';' got 'if'} {
 	sub vcl_recv { set req.url = "foo" if "bar"; }
 }
 
-varnish v1 -errvcl {Symbol not found: 'req.hash' (expected type STRING_LIST):} {
+varnish v1 -errvcl {Symbol not found: 'req.foo' (expected type STRING_LIST):} {
 	backend b { .host = "127.0.0.1"; }
-	sub vcl_hash { hash_data(req.hash); }
+	sub vcl_hash { hash_data(req.foo); }
 }
 
 varnish v1 -vcl {
diff --git a/lib/libvcc/generate.py b/lib/libvcc/generate.py
index 9413d3f..263a2b5 100755
--- a/lib/libvcc/generate.py
+++ b/lib/libvcc/generate.py
@@ -233,6 +233,13 @@ sp_variables = [
 		The request type (e.g. "GET", "HEAD").
 		"""
 	),
+        ('req.hash',
+		'STRING',
+		('hit', 'miss', 'pass', 'purge', 'deliver', ),
+		(), """
+		The hash key of this request.
+		"""
+	),
 	('req.url',
 		'STRING',
 		('client',),
@@ -418,6 +425,13 @@ sp_variables = [
 		The request body.
 		"""
 	),
+        ('bereq.hash',
+		'STRING',
+		('pipe', 'backend', ),
+		(), """
+		The hash key of this request.
+		"""
+	),
 	('bereq.method',
 		'STRING',
 		('pipe', 'backend', ),



More information about the varnish-commit mailing list