Skip to content

Commit

Permalink
fix: handle empty requests and query params
Browse files Browse the repository at this point in the history
This commit adds a fix for two things:

1. Do not break if query params are provided. Just ignore them
2. Do not crash on empty requests
  • Loading branch information
open-dynaMIX committed Dec 27, 2019
1 parent de21e17 commit 5c7da03
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion webui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,30 @@ local function handle_request(request, passwd)
end
end

function strip_query_params(str)
path = ""
for c in str:gmatch(".") do
if c == "?" then
break
end
path = path .. c
end
return path
end

local function parse_request(connection)
local request = {}
request['clientip'] = connection:getpeername()
local line = connection:receive()
if line == nil or line == "" then
return
end
while line ~= nil and line ~= "" do
if not request['request'] then
local raw_request = string.gmatch(line, "%S+")
request["request"] = line
request["method"] = raw_request()
request["path"] = string.sub(raw_request(), 2)
request["path"] = strip_query_params(string.sub(raw_request(), 2))
end
if string.starts(line, "User-Agent") then
request["agent"] = string.sub(line, 13)
Expand All @@ -416,6 +430,9 @@ local function listen(server, passwd)
end

local request = parse_request(connection)
if request == nil then
return
end
local code, content_type, content = handle_request(request, passwd)

connection:send(header(code, content_type, #content))
Expand Down

0 comments on commit 5c7da03

Please sign in to comment.