From 0175ab04cc75c28393095cd0cb717abe1fcc1271 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 24 Apr 2021 10:50:24 +0200 Subject: net.http.server: Split out method for sending only the header Makes it easier to reuse, e.g. for SSE or websockets or other custom responses. --- net/http/server.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'net/http') diff --git a/net/http/server.lua b/net/http/server.lua index ab71dbc9..97e15e42 100644 --- a/net/http/server.lua +++ b/net/http/server.lua @@ -256,6 +256,7 @@ function handle_request(conn, request, finish_cb) persistent = persistent; conn = conn; send = _M.send_response; + write_headers = _M.write_headers; send_file = _M.send_file; done = _M.finish_response; finish_cb = finish_cb; @@ -329,10 +330,14 @@ local function prepare_header(response) return output; end _M.prepare_header = prepare_header; -function _M.send_head_response(response) +function _M.write_headers(response) if response.finished then return; end local output = prepare_header(response); response.conn:write(t_concat(output)); +end +function _M.send_head_response(response) + if response.finished then return; end + _M.write_headers(response); response:done(); end function _M.send_response(response, body) @@ -381,7 +386,7 @@ function _M.send_file(response, f) return response:done(); end end - response.conn:write(t_concat(prepare_header(response))); + _M.write_headers(response); return true; end function _M.finish_response(response) -- cgit v1.2.3