diff --git a/src/lib/protocol/http_wget.lua b/src/lib/protocol/http_wget.lua new file mode 100644 index 0000000..b2479a4 --- /dev/null +++ b/src/lib/protocol/http_wget.lua @@ -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 diff --git a/src/lib/util/http.lua b/src/lib/util/http.lua index 2ca12ab..9662cc3 100644 --- a/src/lib/util/http.lua +++ b/src/lib/util/http.lua @@ -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 @@ -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