test.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. -- figuring out how to parse http header
  2. --require "webServer"
  3. --require "printTable"
  4. --require "b64"
  5. sep = "\r\n"
  6. requestForGet =
  7. "GET /index.html HTTP/1.1" .. sep ..
  8. "Host: 10.0.7.15" .. sep ..
  9. "Accept-Encoding: gzip, deflate" .. sep ..
  10. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" .. sep ..
  11. "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18" .. sep ..
  12. "Accept-Language: en-us" .. sep ..
  13. "Cache-Control: max-age=0" .. sep ..
  14. "Connection: keep-alive" .. sep ..
  15. ""
  16. --print(enc(requestForGet))
  17. --print(dec(enc(requestForGet)))
  18. --parsedRequest = webServer.private.parseRequest(requestForGet)
  19. function parseRequest(request)
  20. local result = {}
  21. local matchEnd = 0
  22. local matchBegin = matchEnd + 1
  23. matchEnd = string.find (requestForGet, " ", matchBegin)
  24. result.method = string.sub(requestForGet, matchBegin, matchEnd-1)
  25. matchBegin = matchEnd + 1
  26. matchEnd = string.find(requestForGet, " ", matchBegin)
  27. result.url = string.sub(requestForGet, matchBegin, matchEnd-1)
  28. matchBegin = matchEnd + 1
  29. matchEnd = string.find(requestForGet, "\r\n", matchBegin)
  30. result.version = string.sub(requestForGet, matchBegin, matchEnd-1)
  31. return result
  32. end
  33. --print(result.method)
  34. --print(result.url)
  35. --print(result.version)
  36. --printTable(parsedRequest, 3)
  37. --printTable(nodemcu-http-server, 3)
  38. --parsedRequest = webServer.parseRequest(requestForGet)
  39. local function validateMethod(method)
  40. -- HTTP Request Methods.
  41. -- HTTP servers are required to implement at least the GET and HEAD methods
  42. -- http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
  43. local httpMethods = {"GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS", "CONNECT", "PATCH"}
  44. for i=1,#httpMethods do
  45. if httpMethods[i] == method then
  46. return method
  47. end
  48. end
  49. return nil
  50. end
  51. --print(validateMethod("GET"))
  52. --print(validateMethod("POST"))
  53. --print(validateMethod("garbage"))
  54. local function uriToFilename(uri)
  55. if uri == "/" then return "http/index.html" end
  56. return "http/" .. string.sub(uri, 2, -1)
  57. end
  58. print(uriToFilename("/index.html"))
  59. print(uriToFilename("/"))
  60. a = nil
  61. if not a then print("hello") end