httpserver.lua 4.8 KB

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