summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-08-21 22:56:19 +0200
committerAki <please@ignore.pl>2021-08-21 22:56:19 +0200
commitfe94acc29d93b766eac79d2f9ded6874919eadfa (patch)
tree63162b5fc13f364d3b9b65d54b0c6196bc300eed
parent6d09f85836097846d4450a27263adca30a8d80c9 (diff)
downloadplop-fe94acc29d93b766eac79d2f9ded6874919eadfa.zip
plop-fe94acc29d93b766eac79d2f9ded6874919eadfa.tar.gz
plop-fe94acc29d93b766eac79d2f9ded6874919eadfa.tar.bz2
Extended default script to handle multiple requests
-rw-r--r--default.lua18
1 files changed, 14 insertions, 4 deletions
diff --git a/default.lua b/default.lua
index 56e5082..bc409ef 100644
--- a/default.lua
+++ b/default.lua
@@ -13,9 +13,8 @@ local RESPONSE = [[<!doctype html>
]]
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
+function handle_request (stream)
local status = 200
local method, path, version, h = stream:read(" ", " ", "\r\n", "\r\n\r\n")
local headers = {}
@@ -24,6 +23,7 @@ return function (stream)
headers[header:lower()] = value
end
end
+ local connection = headers.connection or "close"
if method == nil or path == nil or version ~= "HTTP/1.1" or h == nil then
status = 400
end
@@ -34,8 +34,18 @@ return function (stream)
local response = RESPONSE:format(method, path, version, headers_table)
stream:write(
"HTTP/1.1 ", status, "\r\n",
- "Connection: close\r\n",
+ "Connection: ", connection, "\r\n",
"Content-Length: ", #response, "\r\n",
"Content-Type: text/html\r\n\r\n", response)
stream:flush()
+ return connection
+end
+
+--- Default client request handler.
+-- TODO: Add documentation once request and response API are more stable.
+return
+function (stream)
+ repeat
+ local connection = handle_request(stream)
+ until connection == "close"
end