summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--default.lua35
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