aboutsummaryrefslogtreecommitdiffstats
path: root/net/httpserver.lua
diff options
context:
space:
mode:
Diffstat (limited to 'net/httpserver.lua')
-rw-r--r--net/httpserver.lua43
1 files changed, 19 insertions, 24 deletions
diff --git a/net/httpserver.lua b/net/httpserver.lua
index d3f6a276..59ddbb12 100644
--- a/net/httpserver.lua
+++ b/net/httpserver.lua
@@ -39,40 +39,35 @@ local function send_response(request, response)
if response.body or response.headers then
local body = response.body and 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 body and 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 body and 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
@@ -193,7 +188,7 @@ local function request_reader(request, data, startpos)
request.url = url_parse(request.path);
- log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler.serverport());
+ log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler:serverport());
if request.onlystatus then
if not call_callback(request) then
@@ -211,17 +206,17 @@ end
-- The default handler for requests
default_handler = function (method, body, request)
- log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler.serverport());
- return { status = "404 Not Found",
+ log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler:serverport());
+ return { status = "404 Not Found",
headers = { ["Content-Type"] = "text/html" },
body = "<html><head><title>Page Not Found</title></head><body>Not here :(</body></html>" };
end
function new_request(handler)
- return { handler = handler, conn = handler.socket,
- write = handler.write, state = "request",
- server = http_servers[handler.serverport()],
+ return { handler = handler, conn = handler,
+ write = function (...) return handler:write(...); end, state = "request",
+ server = http_servers[handler:serverport()],
send = send_response,
destroy = destroy_request,
id = tostring{}:match("%x+$")
@@ -239,9 +234,9 @@ function destroy_request(request)
else
log("debug", "Request has no destroy callback");
end
- request.handler.close()
+ request.handler:close()
if request.conn then
- listener.disconnect(request.handler, "closed");
+ listener.ondisconnect(request.conn, "closed");
end
end
end