net.http.server: Ensure HEAD requests are sent with empty body
authorKim Alvefur <zash@zash.se>
Sat, 12 Oct 2019 18:27:02 +0200
changeset 10327 73938168681c
parent 10326 b54c94f25947
child 10328 3f4c25425589
net.http.server: Ensure HEAD requests are sent with empty body
net/http/server.lua
--- a/net/http/server.lua	Fri Oct 11 00:58:33 2019 +0200
+++ b/net/http/server.lua	Sat Oct 12 18:27:02 2019 +0200
@@ -194,8 +194,11 @@
 		response_conn_header = httpversion == "1.1" and "close" or nil
 	end
 
+	local is_head_request = request.method == "HEAD";
+
 	local response = {
 		request = request;
+		is_head_request = is_head_request;
 		status_code = 200;
 		headers = { date = date_header, connection = response_conn_header };
 		persistent = persistent;
@@ -291,16 +294,29 @@
 	return output;
 end
 _M.prepare_header = prepare_header;
+function _M.send_head_response(response)
+	if response.finished then return; end
+	local output = prepare_header(response);
+	response.conn:write(t_concat(output));
+	response:done();
+end
 function _M.send_response(response, body)
 	if response.finished then return; end
 	body = body or response.body or "";
 	response.headers.content_length = #body;
+	if response.is_head_request then
+		return _M.send_head_response(response)
+	end
 	local output = prepare_header(response);
 	t_insert(output, body);
 	response.conn:write(t_concat(output));
 	response:done();
 end
 function _M.send_file(response, f)
+	if response.is_head_request then
+		if f.close then f:close(); end
+		return _M.send_head_response(response);
+	end
 	if response.finished then return; end
 	local chunked = not response.headers.content_length;
 	if chunked then response.headers.transfer_encoding = "chunked"; end