httpserver.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. -- httpserver
  2. -- Author: Marcos Kirsch
  3. module("httpserver", package.seeall)
  4. require("TablePrinter")
  5. -- Functions below aren't part of the public API
  6. -- Clients don't need to worry about them.
  7. -- given an HTTP request, returns the method (i.e. GET)
  8. local function getRequestMethod(request)
  9. -- HTTP Request Methods.
  10. -- HTTP servers are required to implement at least the GET and HEAD methods
  11. -- http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
  12. httpMethods = {"GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS", "CONNECT", "PATCH"}
  13. method = nil
  14. for i=1,#httpMethods do
  15. found = string.find(request, httpMethods[i])
  16. if found == 1 then
  17. break
  18. end
  19. end
  20. return (httpMethods[found])
  21. end
  22. ---- given an HTTP request, returns a table with all the information.
  23. --local function parseRequest(request)
  24. -- parsedRequest = {}
  25. --
  26. -- -- First get the method
  27. -- parsedRequest["method"] = getRequestMethod(request)
  28. -- if parsedRequest["method"] == nil then
  29. -- return nil
  30. -- end
  31. -- -- Now get each value out of the header, skip the first line
  32. -- lineNumber = 0
  33. -- for line in request:gmatch("[^\r\n]+") do
  34. -- if lineNumber ~=0 then
  35. -- -- tag / value are of the style "Host: 10.0.7.15". Break them up.
  36. -- found, valueIndex = string.find(line, ": ")
  37. -- if found == nil then
  38. -- break
  39. -- end
  40. -- tag = string.sub(line, 1, found - 1)
  41. -- value = string.sub(line, found + 2, #line)
  42. -- parsedRequest[tag] = value
  43. -- end
  44. -- lineNumber = lineNumber + 1
  45. -- end
  46. -- return parsedRequest
  47. --end
  48. function parseRequest(request)
  49. local result = {}
  50. local matchEnd = 0
  51. local matchBegin = matchEnd + 1
  52. matchEnd = string.find (request, " ", matchBegin)
  53. result.method = string.sub(request, matchBegin, matchEnd-1)
  54. matchBegin = matchEnd + 1
  55. matchEnd = string.find(request, " ", matchBegin)
  56. result.url = string.sub(request, matchBegin, matchEnd-1)
  57. matchBegin = matchEnd + 1
  58. matchEnd = string.find(request, "\r\n", matchBegin)
  59. result.version = string.sub(request, matchBegin, matchEnd-1)
  60. return result
  61. end
  62. local function onReceive(connection, payload)
  63. print(payload) -- for debugging
  64. -- parse payload and decide what to serve.
  65. parsedRequest = parseRequest(payload)
  66. --TablePrinter.print(parsedRequest, 3)
  67. --generates HTML web site
  68. httpHeader200 = "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n"
  69. html = "<h1>Hola mundo</h1>"
  70. connection:send(httpHeader200 .. html)
  71. end
  72. local function handleRequest(connection)
  73. connection:on("receive", onReceive)
  74. connection:on("sent", function(connection) connection:close() end)
  75. end
  76. -- Starts web server in the specified port.
  77. function httpserver.start(port, clientTimeoutInSeconds)
  78. server = net.createServer(net.TCP, clientTimeoutInSeconds)
  79. server:listen(port, handleRequest)
  80. return server
  81. end
  82. -- Stops the server.
  83. function httpserver.stop(server)
  84. server:close()
  85. end
  86. return mymodule