changeset 3084:5b4e7db5943c

mod_http_avatar: Add caching support.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 01 Jun 2018 22:45:41 +0200
parents 46d71b38bc16
children 1ea6861b533f
files mod_http_avatar/mod_http_avatar.lua
diffstat 1 files changed, 9 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mod_http_avatar/mod_http_avatar.lua	Fri Jun 01 22:11:42 2018 +0200
+++ b/mod_http_avatar/mod_http_avatar.lua	Fri Jun 01 22:45:41 2018 +0200
@@ -5,6 +5,7 @@
 -- COPYING file in the source package for more information.
 
 local base64 = require"util.encodings".base64;
+local sha1 = require"util.hashes".sha1;
 local st = require"util.stanza";
 module:depends"http";
 
@@ -16,7 +17,7 @@
 </svg>]];
 
 local function get_avatar(event, path)
-	local response = event.response;
+	local request, response = event.request, event.response;
 	local photo_type, binval;
 	local vcard, err = vcard_storage:get(path);
 	if vcard then
@@ -32,8 +33,14 @@
 		response.headers.content_type = "image/svg+xml";
 		return default_avatar;
 	end
+	local avatar = base64.decode(binval);
+	local hash = sha1(avatar, true);
+	if request.headers.if_none_match == hash then
+		return 304;
+	end
 	response.headers.content_type = photo_type;
-	return base64.decode(binval);
+	response.headers.etag = hash;
+	return avatar;
 end
 
 module:provides("http", {