2022-02-27 17:29:56 (UTC-03:00)
Marcel Rodrigues <marcelgmr@gmail.com>
parse form data
diff --git a/src/http.lua b/src/http.lua index b13a0f2..2c82324 100644 --- a/src/http.lua +++ b/src/http.lua @@ -1,7 +1,6 @@ local tcp = require "tcp" -local function parse_uri(uri) - local path, query_str, fragment = uri:match("([^?#]*)%??([^#]*)#?(.*)") +local function parse_query(query_str) local query = {} if #query_str > 0 then for pair in (query_str.."&"):gmatch("([^&]*)&") do @@ -9,13 +8,19 @@ local function parse_uri(uri) query[key] = val end end + return query +end + +local function parse_uri(uri) + local path, query_str, fragment = uri:match("([^?#]*)%??([^#]*)#?(.*)") + local query = parse_query(query_str) return path, query, fragment end local function parse_request(data) local req = {payload="", headers={}} local stage = "status" - for line in data:gmatch("(.-)\r?\n") do + for line in (data.."\n"):gmatch("(.-)\r?\n") do if stage == "status" then local target, protocol req.method, target, protocol = line:match("(%S*) (%S*) (%S*)") @@ -33,6 +38,11 @@ local function parse_request(data) req.payload = req.payload..line.."\n" end end + if req.headers["content-type"] == "application/x-www-form-urlencoded" then + local query_str = req.payload + query_str = query_str:sub(1, #query_str-1) + req["form"] = parse_query(query_str) + end return req end