Bladeren bron

commit some changes but I want to stop tracking, so that's next

Marcos Kirsch 9 jaren geleden
bovenliggende
commit
796d17b205
1 gewijzigde bestanden met toevoegingen van 131 en 3 verwijderingen
  1. 131 3
      test.lua

+ 131 - 3
test.lua

@@ -1,11 +1,12 @@
 -- figuring out how to parse http header
 --require "webServer"
---require "printTable"
+printTable = dofile( "TablePrinter.lua")
 --require "b64"
 
+--[[
 sep = "\r\n"
 requestForGet =
-   "GET /index.html HTTP/1.1" .. sep ..
+   "GET /folder/index.html?query=5&b=6 HTTP/1.1" .. sep ..
    "Host: 10.0.7.15" .. sep ..
    "Accept-Encoding: gzip, deflate" .. sep ..
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" .. sep ..
@@ -18,7 +19,22 @@ requestForGet =
 --print(dec(enc(requestForGet)))
 
 --parsedRequest = webServer.private.parseRequest(requestForGet)
+--]]
 
+local function validateMethod(method)
+   -- HTTP Request Methods.
+   -- HTTP servers are required to implement at least the GET and HEAD methods
+   -- http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods
+   local httpMethods = {GET=true, HEAD=true, POST=true, PUT=true, DELETE=true, TRACE=true, OPTIONS=true, CONNECT=true, PATCH=true}
+   if httpMethods[method] then return method else return nil end
+end
+
+print(validateMethod("GET"))
+print(validateMethod("POST"))
+print(validateMethod("FOO"))
+
+
+--[[
 function parseRequest(request)
    local result = {}
    local matchEnd = 0
@@ -37,6 +53,109 @@ function parseRequest(request)
 
    return result
 end
+--]]
+
+--[[
+function parseRequest(request)
+   local e = request:find("\r\n", 1, true)
+   if not e then return nil end
+   local line = request:sub(1, e - 1)
+   local r = {}
+   _, i, r.method, r.url, r.x, r.y = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[1-9]+$")
+   return r
+end
+
+--]]
+
+local function parseArgs(args)
+   r = {}; i=1
+   if args == nil or args == "" then return r end
+   for arg in string.gmatch(args, "([^&]+)") do
+      local name, value = string.match(arg, "(.*)=(.*)")
+      if name ~= nil then r[name] = value end
+      i = i + 1
+   end
+   return r
+end
+
+local function parseUri(uri)
+   local r = {}
+   if uri == nil then return r end
+   if uri == "/" then uri = "/index.html" end
+   questionMarkPos, b, c, d, e, f = uri:find("?")
+   if questionMarkPos == nil then
+      r.file = uri:sub(1, questionMarkPos)
+      r.args = {}
+   else
+      r.file = uri:sub(1, questionMarkPos - 1)
+      r.args = parseArgs(uri:sub(questionMarkPos+1, #uri))
+   end
+   _, r.ext = r.file:match("(.+)%.(.+)")
+   r.isScript = r.ext == "lua" or r.ext == "lc"
+   return r
+end
+
+
+
+--uri = "/folder/index.html?query=5&b=6"
+--uri = "/folder/index.html"
+--uri = "/folder/index.lua?f=2&r=2"
+uri = "/folder/index.lua?f="
+r = parseUri(uri)
+
+print("uri", uri)
+print("r.file", r.file)
+print("r.args", r.args)
+print("r.ext", r.ext)
+print("r.isScript", r.isScript)
+for k,v in pairs(r.args) do print("name: "..k.."    val:"..v) end
+
+
+--[[
+local function parseArgs(args)
+   local r = {}
+   while args ~= nil do
+      local arg = nil
+      a, b = args:find("&")
+      if a == nil then
+         arg = args
+         args = nil
+      else
+         arg = args:sub(1, b-1)
+         args = args:sub(#arg + 2)
+      end
+      local splitArg = {}
+      splitArg.attr, splitArg.val = arg:match("(.+)=(.+)")
+      table.insert(r, splitArg)
+   end
+   return r
+end
+
+r = parseArgs("arg1=44&arg2=55&arg3=66")
+printTable(r)
+--]]
+
+--[[
+name = "http/button.css"
+local isHttpFile, url = string.match(name, "(http)/(%g+)")
+print ("Moshe was here")
+print (isHttpFile, url)
+]]--
+
+--[[
+   local l = file.list()
+   for name, size in pairs(l) do
+      local isHttpFile = string.match(name, "(http)") ~= nil
+      local url = string.match(name, ".*/(.*)")
+      print("name", name)
+      print("isHttpFile", isHttpFile)
+      print("url", url)
+--      if isHttpFile and url then
+--         connection:send('   <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n")
+--      end
+   end
+--]]
+
 
 
 --print(result.method)
@@ -45,7 +164,6 @@ end
 
 --printTable(parsedRequest, 3)
 --printTable(nodemcu-http-server, 3)
---parsedRequest = webServer.parseRequest(requestForGet)
 
 local function validateMethod(method)
    -- HTTP Request Methods.
@@ -60,6 +178,15 @@ local function validateMethod(method)
    return nil
 end
 
+--[[
+r = parseRequest(requestForGet)
+print(r.method)
+print(r.url)
+print(r.x)
+print(r.y)
+--]]
+
+--[[
 --print(validateMethod("GET"))
 --print(validateMethod("POST"))
 --print(validateMethod("garbage"))
@@ -74,3 +201,4 @@ print(uriToFilename("/"))
 
 a = nil
 if not a then print("hello") end
+]]--