httpserver.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. local function parseRequest(request)
  20. local result = {}
  21. local matchEnd = 0
  22. local matchBegin = matchEnd + 1
  23. matchEnd = string.find (request, " ", matchBegin)
  24. result.method = string.sub(request, matchBegin, matchEnd-1)
  25. matchBegin = matchEnd + 1
  26. matchEnd = string.find(request, " ", matchBegin)
  27. result.uri = string.sub(request, matchBegin, matchEnd-1)
  28. matchBegin = matchEnd + 1
  29. matchEnd = string.find(request, "\r\n", matchBegin)
  30. result.version = string.sub(request, matchBegin, matchEnd-1)
  31. return result
  32. end
  33. local function uriToFilename(uri)
  34. if uri == "/" then return "http/index.html" end
  35. return "http/" .. string.sub(uri, 2, -1)
  36. end
  37. local function onError(connection, errorCode, errorString)
  38. print(errorCode .. ": " .. errorString)
  39. connection:send("HTTP/1.0 " .. errorCode .. " " .. errorString .. "\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n")
  40. connection:send("<html><head><title>" .. errorCode .. " - " .. errorString .. "</title></head><body><h1>" .. errorCode .. " - " .. errorString .. "</h1></body></html>\r\n")
  41. connection:close()
  42. end
  43. local function onGet(connection, uri)
  44. print("onGet: requested uri is: " .. uri)
  45. local fileExists = file.open(uriToFilename(uri), "r")
  46. if not fileExists then
  47. onError(connection, 404, "Not Found")
  48. else
  49. -- Use HTTP/1.0 to ensure client closes connection.
  50. connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\Cache-Control: private, no-store\r\n\r\n")
  51. -- Send file in little 128-byte chunks
  52. while true do
  53. local chunk = file.read(128)
  54. if chunk == nil then break end
  55. connection:send(chunk)
  56. end
  57. connection:close()
  58. file.close()
  59. end
  60. end
  61. local function onReceive(connection, payload)
  62. print ("onReceive: We have a customer!")
  63. --print(payload) -- for debugging
  64. -- parse payload and decide what to serve.
  65. parsedRequest = parseRequest(payload)
  66. method = validateMethod(parsedRequest.method)
  67. if method == nil then
  68. onError(connection, 400, "Bad Request")
  69. return
  70. end
  71. if method == "GET" then
  72. onGet(connection, parsedRequest.uri)
  73. return
  74. end
  75. onNotImplemented(connection, 501, "Not Implemented")
  76. end
  77. local function onSent(connection)
  78. print ("onSent: Thank you, come again.")
  79. end
  80. local function handleRequest(connection)
  81. connection:on("receive", onReceive)
  82. connection:on("sent", onSent)
  83. end
  84. -- Starts web server in the specified port.
  85. function httpserver.start(port, clientTimeoutInSeconds)
  86. server = net.createServer(net.TCP, clientTimeoutInSeconds)
  87. server:listen(port, handleRequest)
  88. print("nodemcu-httpserver running at " .. port .. ":" .. wifi.sta.getip())
  89. return server
  90. end
  91. -- Stops the server.
  92. function httpserver.stop(server)
  93. server:close()
  94. end