Skip to content

Commit

Permalink
feat: add wget protocoll #63 #90
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Nov 1, 2024
1 parent 192b960 commit 5e5b5f3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/lib/protocol/http_wget.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local http_util = require('src/lib/util/http')

local function http_handler(self)
local params = http_util.url_search_param(self.param_list, self.param_dict)
local command, cleanup = http_util.create_request(self.method, self.url..params)
.add_custom_headers(self.header_list, self.header_dict)
.add_body_content(self.body_content)
.to_wget_cmd()

local handle = io and io.popen and io.popen(command..'; echo $?')

if handle then
local stdout = handle:read("*a")
local ok = handle:close()
local index = stdout:find("(%d+)\n$")
local ok2 = stdout:sub(index):find('0')
if not ok or not ok2 then
self.std.http.ok = false
self.std.http.error = 'unknown error!'
else
self.std.http.ok = 200
self.std.http.body = stdout:sub(1, index - 2)
self.std.http.status = true
end
else
self.std.http.ok = false
self.std.http.error = 'failed to spawn process!'
end

cleanup()
end

local P = {
handler = http_handler
}

return P
7 changes: 7 additions & 0 deletions src/lib/util/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ local function is_ok(status)
return (status and 200 <= status and status < 300) or false
end

local function is_ok_header(header)
local status = tonumber(header:match('HTTP/%d.%d (%d%d%d)'))
local ok = status and is_ok(status) or false
return ok, status
end

local function is_redirect(status)
return (status and 300 <= status and status < 400) or false
end
Expand Down Expand Up @@ -183,6 +189,7 @@ end

return {
is_ok=is_ok,
is_ok_header=is_ok_header,
is_redirect=is_redirect,
url_search_param=url_search_param,
create_request=create_request
Expand Down

0 comments on commit 5e5b5f3

Please sign in to comment.