Merge remote-tracking branch 'refs/remotes/marcoskirsch/master' into development
This commit is contained in:
commit
3b0e3d0aab
@ -1,5 +1,5 @@
|
|||||||
local function sendAttr(connection, attr, val)
|
local function sendAttr(connection, attr, val)
|
||||||
connection:send("<li><b>".. attr .. ":</b> " .. val .. "<br></li>\n")
|
connection:send("<li><b>".. attr .. ":</b> " .. (val or "nil") .. "<br></li>\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
return function (connection, req, args)
|
return function (connection, req, args)
|
||||||
|
|||||||
@ -1,28 +1,8 @@
|
|||||||
-- httpserver-error.lua
|
-- httpserver-error.lua
|
||||||
-- Part of nodemcu-httpserver, handles sending error pages to client.
|
-- Part of nodemcu-httpserver, handles sending error pages to client.
|
||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch, Gregor Hartmann
|
||||||
|
|
||||||
return function (connection, req, args)
|
return function (connection, req, args)
|
||||||
|
local statusString = dofile("httpserver-header.lc")(connection, req.code, "html", false, req.headers)
|
||||||
-- @TODO: would be nice to use httpserver-header.lua
|
connection:send("<html><head><title>" .. req.code .. " - " .. statusString .. "</title></head><body><h1>" .. req.code .. " - " .. statusString .. "</h1></body></html>\r\n")
|
||||||
local function getHeader(connection, code, errorString, extraHeaders, mimeType)
|
|
||||||
local header = "HTTP/1.0 " .. code .. " " .. errorString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n"
|
|
||||||
for i, extraHeader in ipairs(extraHeaders) do
|
|
||||||
header = header .. extraHeader .. "\r\n"
|
|
||||||
end
|
|
||||||
header = header .. "connection: close\r\n\r\n"
|
|
||||||
return header
|
|
||||||
end
|
|
||||||
|
|
||||||
args.logFunction(connection, "Error " .. args.code .. ": " .. args.errorString)
|
|
||||||
|
|
||||||
-- local port, ip = connection:getpeer()
|
|
||||||
-- print("FIX", ip .. ":" .. port, "Error " .. args.code .. ": " .. args.errorString)
|
|
||||||
-- port = nil
|
|
||||||
-- ip = nil
|
|
||||||
|
|
||||||
args.headers = args.headers or {}
|
|
||||||
connection:send(getHeader(connection, args.code, args.errorString, args.headers, "text/html"))
|
|
||||||
connection:send("<html><head><title>" .. args.code .. " - " .. args.errorString .. "</title></head><body><h1>" .. args.code .. " - " .. args.errorString .. "</h1></body></html>\r\n")
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
-- Part of nodemcu-httpserver, knows how to send an HTTP header.
|
-- Part of nodemcu-httpserver, knows how to send an HTTP header.
|
||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
|
|
||||||
return function(connection, code, extension, isGzipped)
|
return function(connection, code, extension, isGzipped, extraHeaders)
|
||||||
|
|
||||||
local function getHTTPStatusString(code)
|
local function getHTTPStatusString(code)
|
||||||
local codez = { [200] = "OK", [400] = "Bad Request", [404] = "Not Found", [500] = "Internal Server Error", }
|
local codez = { [200] = "OK", [400] = "Bad Request", [401] = "Unauthorized", [404] = "Not Found", [405] = "Method Not Allowed", [500] = "Internal Server Error", [501] = "Not Implemented", }
|
||||||
local myResult = codez[code]
|
local myResult = codez[code]
|
||||||
-- enforce returning valid http codes all the way throughout?
|
-- enforce returning valid http codes all the way throughout?
|
||||||
if myResult then return myResult else return "Not Implemented" end
|
if myResult then return myResult else return "Not Implemented" end
|
||||||
@ -19,11 +19,19 @@ return function(connection, code, extension, isGzipped)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local mimeType = getMimeType(extension)
|
local mimeType = getMimeType(extension)
|
||||||
|
local statusString = getHTTPStatusString(code)
|
||||||
connection:send("HTTP/1.0 " .. code .. " " .. getHTTPStatusString(code) .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n")
|
|
||||||
|
connection:send("HTTP/1.0 " .. code .. " " .. statusString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n")
|
||||||
if isGzipped then
|
if isGzipped then
|
||||||
connection:send("Cache-Control: private, max-age=2592000\r\nContent-Encoding: gzip\r\n")
|
connection:send("Cache-Control: private, max-age=2592000\r\nContent-Encoding: gzip\r\n")
|
||||||
end
|
end
|
||||||
|
if (extraHeaders) then
|
||||||
|
for i, extraHeader in ipairs(extraHeaders) do
|
||||||
|
connection:send(extraHeader .. "\r\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
connection:send("Connection: close\r\n\r\n")
|
connection:send("Connection: close\r\n\r\n")
|
||||||
|
return statusString
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -112,6 +112,11 @@ return function (request)
|
|||||||
local line = request:sub(1, e - 1)
|
local line = request:sub(1, e - 1)
|
||||||
local r = {}
|
local r = {}
|
||||||
_, i, r.method, r.request = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[0-9]+$")
|
_, i, r.method, r.request = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[0-9]+$")
|
||||||
|
if not (r.method and r.request) then
|
||||||
|
--print("invalid request: ")
|
||||||
|
--print(request)
|
||||||
|
return nil
|
||||||
|
end
|
||||||
r.methodIsValid = validateMethod(r.method)
|
r.methodIsValid = validateMethod(r.method)
|
||||||
r.uri = parseUri(r.request)
|
r.uri = parseUri(r.request)
|
||||||
r.getRequestData = getRequestData(request)
|
r.getRequestData = getRequestData(request)
|
||||||
|
|||||||
@ -117,7 +117,7 @@ return function (port)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if user and req.methodIsValid and (req.method == "GET" or req.method == "POST" or req.method == "PUT") then
|
if user and req.methodIsValid and (req.method == "GET" or req.method == "POST" or req.method == "PUT") then
|
||||||
handleRequest(connection, req)
|
handleRequest(connection, req, handleError)
|
||||||
else
|
else
|
||||||
local args = {}
|
local args = {}
|
||||||
local fileServeFunction = dofile("httpserver-error.lc")
|
local fileServeFunction = dofile("httpserver-error.lc")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user