Changeset

2399:0325f241a26c

net.httpserver: Optimized response serialization.
author Waqas Hussain <waqas20@gmail.com>
date Sun, 27 Dec 2009 10:09:22 +0500
parents 2398:44f694ce6aec
children 2400:b8d2168dc9c3 2411:c2b6c55201af
files net/httpserver.lua
diffstat 1 files changed, 11 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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 "<none>");
 		
-		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