httpserver.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 on404NotFound(connection)
  38. print("onNotFound: The requested file was not found.")
  39. connection:send("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: " .. string.len(html) .. "\r\nConnection: close\r\n\r\n")
  40. connection:send("<html><head><title>404 - Not Found</title></head><body><h1>404 - Not Found</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. on404NotFound(connection)
  48. else
  49. connection:send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\Cache-Control: private, no-store\r\n\r\n")
  50. connection:send(file.read())
  51. connection:close()
  52. file.close()
  53. end
  54. end
  55. local function onReceive(connection, payload)
  56. print ("onReceive: We have a customer!")
  57. print(payload) -- for debugging
  58. -- parse payload and decide what to serve.
  59. parsedRequest = parseRequest(payload)
  60. method = validateMethod(parsedRequest.method)
  61. if method == nil then
  62. onBadRequest(connection)
  63. return
  64. end
  65. if method == "GET" then
  66. onGet(connection, parsedRequest.uri)
  67. return
  68. end
  69. onNotImplemented(connection)
  70. end
  71. local function onSent(connection)
  72. print ("onSent: Thank you, come again.")
  73. end
  74. local function handleRequest(connection)
  75. connection:on("receive", onReceive)
  76. connection:on("sent", onSent)
  77. end
  78. -- Starts web server in the specified port.
  79. function httpserver.start(port, clientTimeoutInSeconds)
  80. server = net.createServer(net.TCP, clientTimeoutInSeconds)
  81. server:listen(port, handleRequest)
  82. return server
  83. end
  84. -- Stops the server.
  85. function httpserver.stop(server)
  86. server:close()
  87. end