diff options
author | Aki <please@ignore.pl> | 2021-03-03 23:55:28 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2021-03-03 23:55:28 +0100 |
commit | 8f89bcaf51f5287eea4d10b1363141b28f836527 (patch) | |
tree | 05e4a3810db0fb9551736d98d45251a6de083dd0 | |
parent | 9b2a36ee8da4e25d919a435f871cc1e0fee8eace (diff) | |
download | plop-8f89bcaf51f5287eea4d10b1363141b28f836527.zip plop-8f89bcaf51f5287eea4d10b1363141b28f836527.tar.gz plop-8f89bcaf51f5287eea4d10b1363141b28f836527.tar.bz2 |
Extended default handler a bit
-rw-r--r-- | default.lua | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/default.lua b/default.lua index ab15a95..0685f0f 100644 --- a/default.lua +++ b/default.lua @@ -1,14 +1,33 @@ +local RESPONSE = [[<!doctype html> +<html lang="en"> +<title>plop</title> +<h1>Hello, from plop/lua!</h1> +<p>You requested <code>%s</code> using <b>%s</b> with %s and: +<table> +<tr><th>Header<th>Value +%s</table> +]] +local HEADER = "%s<tr><td>%s<td>%s\n" + --- Default client request handler. -- TODO: Add documentation once request and response API are more stable. return function (stream) - local method, path, version, h = stream:read " ", stream:read " ", stream:read "\r\n", stream:read "\r\n\r\n" + local status = 200 + local method, path, version, h = stream:read(" ", " ", "\r\n", "\r\n\r\n") local headers = {} - for header, value in h:gmatch "([^%s:]+)%s*:%s+([^\r\n]*)" do - headers[header:lower()] = value + if h then + for header, value in h:gmatch "([^%s:]+)%s*:%s+([^\r\n]*)" do + headers[header:lower()] = value + end end - - local response_data = [["Hello from plop/lua!"]] - local headers = {Connection="close", ["Content-Length"]=#response_data, ["Content-Type"]="application/json"} - - return {status=200, headers=headers, data=response_data} + if method == nil or path == nil or version ~= "HTTP/1.1" or h == nil then + status = 400 + end + local headers_table = "" + for header, value in pairs(headers) do + headers_table = HEADER:format(headers_table_body, header, value) + end + local response = RESPONSE:format(path, method, version, headers_table_body) + local headers = {Connection="close", ["Content-Length"]=#response, ["Content-Type"]="text/html"} + return {status=status, headers=headers, data=response_data} end |