Browse Source

Merge remote-tracking branch 'refs/remotes/marcoskirsch/master' into development

Gregor 6 years ago
parent
commit
3b0e3d0aab
5 changed files with 22 additions and 29 deletions
  1. 1 1
      http/node_info.lua
  2. 3 23
      httpserver-error.lua
  3. 12 4
      httpserver-header.lua
  4. 5 0
      httpserver-request.lua
  5. 1 1
      httpserver.lua

+ 1 - 1
http/node_info.lua

@@ -1,5 +1,5 @@
 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
 
 return function (connection, req, args)

+ 3 - 23
httpserver-error.lua

@@ -1,28 +1,8 @@
 -- httpserver-error.lua
 -- Part of nodemcu-httpserver, handles sending error pages to client.
--- Author: Marcos Kirsch
+-- Author: Marcos Kirsch, Gregor Hartmann
 
 return function (connection, req, args)
-
-   -- @TODO: would be nice to use httpserver-header.lua
-   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")
-
+   local statusString = dofile("httpserver-header.lc")(connection, req.code, "html", false, req.headers)
+   connection:send("<html><head><title>" .. req.code .. " - " .. statusString .. "</title></head><body><h1>" .. req.code .. " - " .. statusString .. "</h1></body></html>\r\n")
 end

+ 12 - 4
httpserver-header.lua

@@ -2,10 +2,10 @@
 -- Part of nodemcu-httpserver, knows how to send an HTTP header.
 -- Author: Marcos Kirsch
 
-return function(connection, code, extension, isGzipped)
+return function(connection, code, extension, isGzipped, extraHeaders)
 
    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]
       -- enforce returning valid http codes all the way throughout?
       if myResult then return myResult else return "Not Implemented" end
@@ -19,11 +19,19 @@ return function(connection, code, extension, isGzipped)
    end
 
    local mimeType = getMimeType(extension)
-
-   connection:send("HTTP/1.0 " .. code .. " " .. getHTTPStatusString(code) .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n")
+   local statusString = getHTTPStatusString(code)
+   
+   connection:send("HTTP/1.0 " .. code .. " " .. statusString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\n")
    if isGzipped then
       connection:send("Cache-Control: private, max-age=2592000\r\nContent-Encoding: gzip\r\n")
    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")
+   return statusString
 end
 

+ 5 - 0
httpserver-request.lua

@@ -112,6 +112,11 @@ return function (request)
    local line = request:sub(1, e - 1)
    local r = {}
    _, 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.uri = parseUri(r.request)
    r.getRequestData = getRequestData(request)

+ 1 - 1
httpserver.lua

@@ -117,7 +117,7 @@ return function (port)
             end
 
             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
                local args = {}
                local fileServeFunction = dofile("httpserver-error.lc")