[master] c048d79 Add a dedicated HTTP function to add a header with a timestamp, to avoid some pointless double-buffering.

Poul-Henning Kamp phk at FreeBSD.org
Mon Feb 23 09:50:12 CET 2015


commit c048d79e1a35315b30e4a7699c302265950a93d0
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Feb 23 08:49:43 2015 +0000

    Add a dedicated HTTP function to add a header with a timestamp,
    to avoid some pointless double-buffering.

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index faaaf4f..44c302c 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -830,6 +830,7 @@ int HTTP_Decode(struct http *to, const uint8_t *fm);
 void http_ForceHeader(struct http *to, const char *hdr, const char *val);
 void http_PrintfHeader(struct http *to, const char *fmt, ...)
     __printflike(2, 3);
+void http_TimeHeader(struct http *to, const char *fmt, double now);
 void http_SetHeader(struct http *to, const char *hdr);
 void http_SetH(const struct http *to, unsigned n, const char *fm);
 void http_ForceField(const struct http *to, unsigned n, const char *t);
diff --git a/bin/varnishd/cache/cache_fetch.c b/bin/varnishd/cache/cache_fetch.c
index 8f0ba2e..fbe19a6 100644
--- a/bin/varnishd/cache/cache_fetch.c
+++ b/bin/varnishd/cache/cache_fetch.c
@@ -262,7 +262,6 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
 {
 	int i, do_ims = 0;
 	double now;
-	char time_str[VTIM_FORMAT_SIZE];
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
 	CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
@@ -313,8 +312,7 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
 		 *
 		 * If we didn't get a Date header, we assign one here.
 		 */
-		VTIM_format(now, time_str);
-		http_PrintfHeader(bo->beresp, "Date: %s", time_str);
+		http_TimeHeader(bo->beresp, "Date: ", now);
 	}
 
 	/*
@@ -771,7 +769,6 @@ vbf_stp_error(struct worker *wrk, struct busyobj *bo)
 	ssize_t l, ll, o;
 	double now;
 	uint8_t *ptr;
-	char time_str[VTIM_FORMAT_SIZE];
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
 	CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
@@ -790,8 +787,7 @@ vbf_stp_error(struct worker *wrk, struct busyobj *bo)
 
 	HTTP_Setup(bo->beresp, bo->ws, bo->vsl, SLT_BerespMethod);
 	http_PutResponse(bo->beresp, "HTTP/1.1", 503, "Backend fetch failed");
-	VTIM_format(now, time_str);
-	http_PrintfHeader(bo->beresp, "Date: %s", time_str);
+	http_TimeHeader(bo->beresp, "Date: ", now);
 	http_SetHeader(bo->beresp, "Server: Varnish");
 
 	bo->fetch_objcore->exp.t_origin = bo->t_prev;
diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index 4dcf1f9..bd8d4b1 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -38,6 +38,7 @@
 
 #include "vend.h"
 #include "vct.h"
+#include "vtim.h"
 
 #define HTTPH(a, b, c) char b[] = "*" a ":";
 #include "tbl/http_headers.h"
@@ -1112,6 +1113,27 @@ http_PrintfHeader(struct http *to, const char *fmt, ...)
 	to->nhd++;
 }
 
+void
+http_TimeHeader(struct http *to, const char *fmt, double now)
+{
+	char *p;
+
+	CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
+	p = WS_Alloc(to->ws, strlen(fmt) + VTIM_FORMAT_SIZE);
+	if (p == NULL) {
+		http_fail(to);
+		VSLb(to->vsl, SLT_LostHeader, "%s", fmt);
+		return;
+	}
+	strcpy(p, fmt);
+	VTIM_format(now, strchr(p, '\0'));
+	to->hd[to->nhd].b = p;
+	to->hd[to->nhd].e = strchr(p, '\0');
+	to->hdf[to->nhd] = 0;
+	http_VSLH(to, to->nhd);
+	to->nhd++;
+}
+
 /*--------------------------------------------------------------------*/
 
 void
diff --git a/bin/varnishd/cache/cache_req_fsm.c b/bin/varnishd/cache/cache_req_fsm.c
index 0900fec..d78e24b 100644
--- a/bin/varnishd/cache/cache_req_fsm.c
+++ b/bin/varnishd/cache/cache_req_fsm.c
@@ -171,7 +171,6 @@ cnt_deliver(struct worker *wrk, struct req *req)
 static enum req_fsm_nxt
 cnt_synth(struct worker *wrk, struct req *req)
 {
-	char date[40];
 	struct http *h;
 	double now;
 
@@ -189,8 +188,7 @@ cnt_synth(struct worker *wrk, struct req *req)
 
 	HTTP_Setup(req->resp, req->ws, req->vsl, SLT_RespMethod);
 	h = req->resp;
-	VTIM_format(now, date);
-	http_PrintfHeader(h, "Date: %s", date);
+	http_TimeHeader(h, "Date: ", now);
 	http_SetHeader(h, "Server: Varnish");
 	http_PrintfHeader(req->resp, "X-Varnish: %u", VXID(req->vsl->wid));
 	http_PutResponse(h, "HTTP/1.1", req->err_code, req->err_reason);



More information about the varnish-commit mailing list