From c700f8867c7f78c5c4b27775aee0b638b24d5dda Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 27 Dec 2009 10:09:22 +0500 Subject: net.httpserver: Optimized response serialization. --- net/httpserver.lua | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/net/httpserver.lua b/net/httpserver.lua index 6273bbb9..dec13a0b 100644 --- a/net/httpserver.lua +++ b/net/httpserver.lua @@ -39,40 +39,35 @@ local function send_response(request, response) if response.body then local body = tostring(response.body); log("debug", "Sending response to %s", request.id); - resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"}; + resp = { "HTTP/1.0 "..(response.status or "200 OK").."\r\n" }; local h = response.headers; if h then for k, v in pairs(h) do - t_insert(resp, k); - t_insert(resp, ": "); - t_insert(resp, v); - t_insert(resp, "\r\n"); + t_insert(resp, k..": "..v.."\r\n"); end end if not (h and h["Content-Length"]) then - t_insert(resp, "Content-Length: "); - t_insert(resp, #body); - t_insert(resp, "\r\n"); + t_insert(resp, "Content-Length: "..#body.."\r\n"); end t_insert(resp, "\r\n"); if request.method ~= "HEAD" then t_insert(resp, body); end + request.write(t_concat(resp)); else -- Response we have is just a string (the body) log("debug", "Sending 200 response to %s", request.id or ""); - resp = { "HTTP/1.0 200 OK\r\n" }; - t_insert(resp, "Connection: close\r\n"); - t_insert(resp, "Content-Type: text/html\r\n"); - t_insert(resp, "Content-Length: "); - t_insert(resp, #response); - t_insert(resp, "\r\n\r\n"); + local resp = "HTTP/1.0 200 OK\r\n" + .. "Connection: close\r\n" + .. "Content-Type: text/html\r\n" + .. "Content-Length: "..#response.."\r\n" + .. "\r\n" + .. response; - t_insert(resp, response); + request.write(resp); end - request.write(t_concat(resp)); if not request.stayopen then request:destroy(); end -- cgit v1.2.3