2023-08-15 19:56:36 (UTC-03:00)
Marcel Rodrigues <marcelgmr@gmail.com>
allow payload to be read from file
diff --git a/README.md b/README.md index 07a2ad6..eb29621 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,9 @@ In addition to "data" (i.e. payload), "status", "reason" and "cookies", the response object can also optionally include the "headers" value. This should be a table with additional response headers. +If the response has an "fname" field instead of "data", then the payload +will be read from the file named in that field. + = templates ``` diff --git a/lib/ludweb/http.lua b/lib/ludweb/http.lua index e267921..e9cff9f 100644 --- a/lib/ludweb/http.lua +++ b/lib/ludweb/http.lua @@ -162,6 +162,15 @@ local function new_http() if type(resp) == "string" then resp = {data=resp, status=status, reason=reason, cookies=cookies} end + if resp.fname ~= nil then + local fp = io.open(resp.fname) + if fp == nil then + resp.data = "file not found" + else + resp.data = fp:read("*a") + fp:close() + end + end local keep_alive = req.headers.connection ~= "close" -- randomly close some connections to save resources if keep_alive and math.random(32) == 1 then