httpserver.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. -- gzip check
  25. fileExists = file.open(uri.file .. ".gz", "r")
  26. file.close()
  27. if fileExists then
  28. print("gzip variant exists, serving that one")
  29. uri.file = uri.file .. ".gz"
  30. uri.ext = uri.ext .. ".gz"
  31. end
  32. end
  33. if not fileExists then
  34. uri.args = {code = 404, errorString = "Not Found"}
  35. fileServeFunction = dofile("httpserver-error.lc")
  36. elseif uri.isScript then
  37. fileServeFunction = dofile(uri.file)
  38. else
  39. uri.args = {file = uri.file, ext = uri.ext}
  40. fileServeFunction = dofile("httpserver-static.lc")
  41. end
  42. end
  43. connectionThread = coroutine.create(fileServeFunction)
  44. coroutine.resume(connectionThread, connection, uri.args)
  45. end
  46. local function onReceive(connection, payload)
  47. collectgarbage()
  48. local conf = dofile("httpserver-conf.lc")
  49. local auth
  50. local user = "Anonymous"
  51. -- parse payload and decide what to serve.
  52. local req = dofile("httpserver-request.lc")(payload)
  53. print("Requested URI: " .. req.request)
  54. if conf.auth.enabled then
  55. auth = dofile("httpserver-basicauth.lc")
  56. user = auth.authenticate(payload) -- authenticate returns nil on failed auth
  57. end
  58. if user and req.methodIsValid and req.method == "GET" then
  59. onGet(connection, req.uri)
  60. else
  61. local args = {}
  62. local fileServeFunction = dofile("httpserver-error.lc")
  63. if not user then
  64. args = {code = 401, errorString = "Not Authorized", headers = {auth.authErrorHeader()}}
  65. elseif req.methodIsValid then
  66. args = {code = 501, errorString = "Not Implemented"}
  67. else
  68. args = {code = 400, errorString = "Bad Request"}
  69. end
  70. connectionThread = coroutine.create(fileServeFunction)
  71. coroutine.resume(connectionThread, connection, args)
  72. end
  73. end
  74. local function onSent(connection, payload)
  75. collectgarbage()
  76. if connectionThread then
  77. local connectionThreadStatus = coroutine.status(connectionThread)
  78. if connectionThreadStatus == "suspended" then
  79. -- Not finished sending file, resume.
  80. coroutine.resume(connectionThread)
  81. elseif connectionThreadStatus == "dead" then
  82. -- We're done sending file.
  83. connection:close()
  84. connectionThread = nil
  85. end
  86. end
  87. end
  88. connection:on("receive", onReceive)
  89. connection:on("sent", onSent)
  90. end
  91. )
  92. -- false and nil evaluate as false
  93. local ip = wifi.sta.getip()
  94. if not ip then ip = wifi.ap.getip() end
  95. print("nodemcu-httpserver running at http://" .. ip .. ":" .. port)
  96. return s
  97. end