httpserver.lua 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. -- httpserver
  2. -- Author: Marcos Kirsch
  3. -- Starts web server in the specified port.
  4. return function (port)
  5. local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
  6. s:listen(
  7. port,
  8. function (connection)
  9. -- This variable holds the thread used for sending data back to the user.
  10. -- We do it in a separate thread because we need to yield when sending lots
  11. -- of data in order to avoid overflowing the mcu's buffer.
  12. local connectionThread
  13. local function onGet(connection, uri)
  14. collectgarbage()
  15. local fileServeFunction = nil
  16. if #(uri.file) > 32 then
  17. -- nodemcu-firmware cannot handle long filenames.
  18. uri.args = {code = 400, errorString = "Bad Request"}
  19. fileServeFunction = dofile("httpserver-error.lc")
  20. else
  21. local fileExists = file.open(uri.file, "r")
  22. file.close()
  23. if not fileExists then
  24. uri.args = {code = 404, errorString = "Not Found"}
  25. fileServeFunction = dofile("httpserver-error.lc")
  26. elseif uri.isScript then
  27. fileServeFunction = dofile(uri.file)
  28. else
  29. uri.args = {file = uri.file, ext = uri.ext}
  30. fileServeFunction = dofile("httpserver-static.lc")
  31. end
  32. end
  33. connectionThread = coroutine.create(fileServeFunction)
  34. coroutine.resume(connectionThread, connection, uri.args)
  35. end
  36. local function onReceive(connection, payload)
  37. collectgarbage()
  38. local conf = dofile("httpserver-conf.lc")
  39. local auth
  40. local user = "Anonymous"
  41. -- parse payload and decide what to serve.
  42. local req = dofile("httpserver-request.lc")(payload)
  43. print("Requested URI: " .. req.request)
  44. if conf.auth.enabled then
  45. auth = dofile("httpserver-basicauth.lc")
  46. user = auth.authenticate(payload) -- authenticate returns nil on failed auth
  47. end
  48. if user and req.methodIsValid and req.method == "GET" then
  49. onGet(connection, req.uri)
  50. else
  51. local args = {}
  52. local fileServeFunction = dofile("httpserver-error.lc")
  53. if not user then
  54. args = {code = 401, errorString = "Not Authorized", headers = {auth.authErrorHeader()}}
  55. elseif req.methodIsValid then
  56. args = {code = 501, errorString = "Not Implemented"}
  57. else
  58. args = {code = 400, errorString = "Bad Request"}
  59. end
  60. connectionThread = coroutine.create(fileServeFunction)
  61. coroutine.resume(connectionThread, connection, args)
  62. end
  63. end
  64. local function onSent(connection, payload)
  65. collectgarbage()
  66. if connectionThread then
  67. local connectionThreadStatus = coroutine.status(connectionThread)
  68. if connectionThreadStatus == "suspended" then
  69. -- Not finished sending file, resume.
  70. coroutine.resume(connectionThread)
  71. elseif connectionThreadStatus == "dead" then
  72. -- We're done sending file.
  73. connection:close()
  74. connectionThread = nil
  75. end
  76. end
  77. end
  78. connection:on("receive", onReceive)
  79. connection:on("sent", onSent)
  80. end
  81. )
  82. -- false and nil evaluate as false
  83. local ip = wifi.sta.getip()
  84. if not ip then ip = wifi.ap.getip() end
  85. print("nodemcu-httpserver running at http://" .. ip .. ":" .. port)
  86. return s
  87. end