[master] 58b155152 http: Configure the Via header with server identity

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Fri Aug 5 09:24:08 UTC 2022


commit 58b1551528ccf4a67b78f2bd71b54000d4a85c38
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Tue Jul 26 11:24:16 2022 +0200

    http: Configure the Via header with server identity
    
    Change made to the Via header:
    
        -Via: 1.1 varnish (Varnish/x.y)
        +Via: 1.1 ${i_opt} (Varnish/x.y)
    
    It should actually better serve its purpose of informing about the hops
    a request (or in this case a response) went through. This is how Via is
    defined in HTTP:
    
        Via = #( received-protocol RWS received-by [ RWS comment ] )
    
    Using the server identity (which defaults to the server host name) is
    more accurate for the received-by field. We still advertise Varnish in
    the optional comment field, and hopefully that's what analytics services
    rely on to recognize a server as a Varnish instance.
    
    A bit of trivia, a proxy MAY add a Via entry to the response and MUST
    add one to a forwarded request: Varnish only adds the optional one.
    
    Refs rfc9110/7.6.3.
    Fixes #3794

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 3ef0512bf..e74104388 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -605,6 +605,7 @@ void http_AppendHeader(struct http *to, hdr_t, const char *val);
 void http_PrintfHeader(struct http *to, const char *fmt, ...)
     v_printflike_(2, 3);
 void http_TimeHeader(struct http *to, const char *fmt, vtim_real now);
+const char * http_ViaHeader(void);
 void http_Proto(struct http *to);
 void http_SetHeader(struct http *to, const char *header);
 void http_SetH(struct http *to, unsigned n, const char *header);
diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index e2f8ba726..b68c18638 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -35,6 +35,9 @@
 
 #include "cache_varnishd.h"
 #include <stdio.h>
+#include <stdlib.h>
+
+#include "common/heritage.h"
 
 #include "vct.h"
 #include "vend.h"
@@ -58,6 +61,8 @@ const char H__Status[]	= "\010:status:";
 const char H__Proto[]	= "\007:proto:";
 const char H__Reason[]	= "\010:reason:";
 
+static char * via_hdr;
+
 /*--------------------------------------------------------------------
  * Perfect hash to rapidly recognize headers from tbl/http_headers.h
  * which have non-zero flags.
@@ -178,9 +183,17 @@ http_init_hdr(char *hdr, int flg)
 void
 HTTP_Init(void)
 {
+	struct vsb *vsb;
 
 #define HTTPH(a, b, c) http_init_hdr(b, c);
 #include "tbl/http_headers.h"
+
+	vsb = VSB_new_auto();
+	VSB_printf(vsb, "1.1 %s (Varnish/" PACKAGE_BRANCH ")",
+	    heritage.identity);
+	AZ(VSB_finish(vsb));
+	REPLACE(via_hdr, VSB_data(vsb));
+	VSB_destroy(&vsb);
 }
 
 /*--------------------------------------------------------------------
@@ -1541,6 +1554,13 @@ http_TimeHeader(struct http *to, const char *fmt, vtim_real now)
 	http_SetH(to, to->nhd++, p);
 }
 
+const char *
+http_ViaHeader(void)
+{
+
+	return (via_hdr);
+}
+
 /*--------------------------------------------------------------------*/
 
 void
diff --git a/bin/varnishd/cache/cache_req_fsm.c b/bin/varnishd/cache/cache_req_fsm.c
index 89b585306..9afba45b5 100644
--- a/bin/varnishd/cache/cache_req_fsm.c
+++ b/bin/varnishd/cache/cache_req_fsm.c
@@ -167,7 +167,7 @@ Resp_Setup_Deliver(struct req *req)
 	http_PrintfHeader(h, "Age: %.0f",
 	    floor(fmax(0., req->t_prev - oc->t_origin)));
 
-	http_AppendHeader(h, H_Via, "1.1 varnish (Varnish/" PACKAGE_BRANCH ")");
+	http_AppendHeader(h, H_Via, http_ViaHeader());
 
 	if (cache_param->http_gzip_support &&
 	    ObjCheckFlag(req->wrk, oc, OF_GZIPED) &&
diff --git a/bin/varnishtest/tests/r03794.vtc b/bin/varnishtest/tests/r03794.vtc
index 7535b0593..f9901a682 100644
--- a/bin/varnishtest/tests/r03794.vtc
+++ b/bin/varnishtest/tests/r03794.vtc
@@ -1,4 +1,4 @@
-varnishtest "Append Via header"
+varnishtest "Append configurable Via header"
 
 server s1 {
 	rxreq
@@ -17,5 +17,5 @@ client c1 -connect ${v2_sock} {
 	txreq
 	rxresp
 	expect resp.http.via == \
-		"1.1 varnish (Varnish/${pkg_branch}), 1.1 varnish (Varnish/${pkg_branch})"
+		"1.1 v1 (Varnish/${pkg_branch}), 1.1 v2 (Varnish/${pkg_branch})"
 } -run


More information about the varnish-commit mailing list