# HG changeset patch # User Waqas Hussain # Date 1261890562 -18000 # Node ID 0325f241a26c7b054d6f1e9b3e6e038595f081c0 # Parent 44f694ce6aec82bec8f48b67d601551dd6e3afdc net.httpserver: Optimized response serialization. diff -r 44f694ce6aec -r 0325f241a26c net/httpserver.lua --- a/net/httpserver.lua Wed Dec 23 23:13:39 2009 +0100 +++ b/net/httpserver.lua Sun Dec 27 10:09:22 2009 +0500 @@ -39,40 +39,35 @@ 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