httpserver.lua 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. -- httpserver
  2. -- Author: Marcos Kirsch
  3. module("httpserver", package.seeall)
  4. -- Functions below aren't part of the public API
  5. -- Clients don't need to worry about them.
  6. -- given an HTTP method, returns it or if invalid returns nil
  7. local function validateMethod(method)
  8. -- HTTP Request Methods.
  9. -- HTTP servers are required to implement at least the GET and HEAD methods
  10. -- http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
  11. local httpMethods = {"GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS", "CONNECT", "PATCH"}
  12. for i=1,#httpMethods do
  13. if httpMethods[i] == method then
  14. return method
  15. end
  16. end
  17. return nil
  18. end
  19. function parseRequest(request)
  20. local e = request:find("\r\n", 1, true)
  21. if not e then return nil end
  22. local line = request:sub(1, e - 1)
  23. local r = {}
  24. _, i, r.method, r.uri = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[1-9]+$")
  25. return r
  26. end
  27. local function uriToFilename(uri)
  28. if uri == "/" then return "http/index.html" end
  29. return "http/" .. string.sub(uri, 2, -1)
  30. end
  31. local function onError(connection, errorCode, errorString)
  32. print(errorCode .. ": " .. errorString)
  33. connection:send("HTTP/1.0 " .. errorCode .. " " .. errorString .. "\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n")
  34. connection:send("<html><head><title>" .. errorCode .. " - " .. errorString .. "</title></head><body><h1>" .. errorCode .. " - " .. errorString .. "</h1></body></html>\r\n")
  35. connection:close()
  36. end
  37. local function onGet(connection, uri)
  38. print("onGet: requested uri is: " .. uri)
  39. local fileExists = file.open(uriToFilename(uri), "r")
  40. if not fileExists then
  41. onError(connection, 404, "Not Found")
  42. else
  43. -- Use HTTP/1.0 to ensure client closes connection.
  44. connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\Cache-Control: private, no-store\r\n\r\n")
  45. -- Send file in little 128-byte chunks
  46. while true do
  47. local chunk = file.read(128)
  48. if chunk == nil then break end
  49. connection:send(chunk)
  50. end
  51. connection:close()
  52. file.close()
  53. end
  54. end
  55. local function onReceive(connection, payload)
  56. print ("onReceive: We have a customer!")
  57. --print(payload) -- for debugging
  58. -- parse payload and decide what to serve.
  59. parsedRequest = parseRequest(payload)
  60. parsedRequest.method = validateMethod(parsedRequest.method)
  61. if parsedRequest.method == nil then onError(connection, 400, "Bad Request")
  62. elseif parsedRequest.method == "GET" then onGet(connection, parsedRequest.uri)
  63. else onNotImplemented(connection, 501, "Not Implemented") end
  64. end
  65. local function onSent(connection)
  66. print ("onSent: Thank you, come again.")
  67. end
  68. local function handleRequest(connection)
  69. connection:on("receive", onReceive)
  70. connection:on("sent", onSent)
  71. end
  72. -- Starts web server in the specified port.
  73. function httpserver.start(port, clientTimeoutInSeconds)
  74. server = net.createServer(net.TCP, clientTimeoutInSeconds)
  75. server:listen(port, handleRequest)
  76. print("nodemcu-httpserver running at " .. port .. ":" .. wifi.sta.getip())
  77. return server
  78. end
  79. -- Stops the server.
  80. function httpserver.stop(server)
  81. server:close()
  82. end