Browse Source

use httpserver-header.lua in httpserver-error.lua implementation

Add all HTTP response codes used in the project
allow extra headers to be passed
Gregor 6 years ago
parent
commit
9b33c564db
2 changed files with 15 additions and 27 deletions
  1. 3 23
      httpserver-error.lua
  2. 12 4
      httpserver-header.lua

+ 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