httpserver.lua 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. -- print(payload) -- for debugging
  39. -- parse payload and decide what to serve.
  40. local req = dofile("httpserver-request.lc")(payload)
  41. print("Requested URI: " .. req.request)
  42. if req.methodIsValid and req.method == "GET" then
  43. onGet(connection, req.uri)
  44. else
  45. local args = {}
  46. local fileServeFunction = dofile("httpserver-error.lc")
  47. if req.methodIsValid then
  48. args = {code = 501, errorString = "Not Implemented"}
  49. else
  50. args = {code = 400, errorString = "Bad Request"}
  51. end
  52. connectionThread = coroutine.create(fileServeFunction)
  53. coroutine.resume(connectionThread, connection, args)
  54. end
  55. end
  56. local function onSent(connection, payload)
  57. collectgarbage()
  58. if connectionThread then
  59. local connectionThreadStatus = coroutine.status(connectionThread)
  60. if connectionThreadStatus == "suspended" then
  61. -- Not finished sending file, resume.
  62. coroutine.resume(connectionThread)
  63. elseif connectionThreadStatus == "dead" then
  64. -- We're done sending file.
  65. connection:close()
  66. connectionThread = nil
  67. end
  68. end
  69. end
  70. connection:on("receive", onReceive)
  71. connection:on("sent", onSent)
  72. end
  73. )
  74. -- false and nil evaluate as false
  75. local ip = wifi.sta.getip()
  76. if not ip then ip = wifi.ap.getip() end
  77. print("nodemcu-httpserver running at http://" .. ip .. ":" .. port)
  78. return s
  79. end