mirror of
https://github.com/lunarmodules/luasocket.git
synced 2025-07-12 22:03:31 +02:00
HTTP now has only one function.
This commit is contained in:
@ -8,8 +8,6 @@ local http = require("http")
|
||||
local url = require("url")
|
||||
http.TIMEOUT = 10
|
||||
|
||||
cache = {}
|
||||
|
||||
function readfile(path)
|
||||
path = url.unescape(path)
|
||||
local file, error = io.open(path, "r")
|
||||
@ -22,22 +20,14 @@ end
|
||||
|
||||
function getstatus(u)
|
||||
local parsed = url.parse(u, {scheme = "file"})
|
||||
if cache[u] then return cache[u] end
|
||||
local res
|
||||
if parsed.scheme == "http" then
|
||||
local request = {url = u, method = "HEAD"}
|
||||
local response = http.request(request)
|
||||
if response.code == 200 then res = nil
|
||||
else res = response.status or response.error end
|
||||
local r, c, h, s = http.request{url = u, method = "HEAD"}
|
||||
if c ~= 200 then return s or c end
|
||||
elseif parsed.scheme == "file" then
|
||||
local file, error = io.open(url.unescape(parsed.path), "r")
|
||||
if file then
|
||||
file:close()
|
||||
res = nil
|
||||
else res = error end
|
||||
else res = string.format("unhandled scheme '%s'", parsed.scheme) end
|
||||
cache[u] = res
|
||||
return res
|
||||
if file then file:close()
|
||||
else return error end
|
||||
else return string.format("unhandled scheme '%s'", parsed.scheme) end
|
||||
end
|
||||
|
||||
function retrieve(u)
|
||||
@ -45,10 +35,13 @@ function retrieve(u)
|
||||
local body, headers, code, error
|
||||
local base = u
|
||||
if parsed.scheme == "http" then
|
||||
body, headers, code, error = http.get(u)
|
||||
body, code, headers = http.request(u)
|
||||
if code == 200 then
|
||||
base = base or headers.location
|
||||
end
|
||||
if not body then
|
||||
error = code
|
||||
end
|
||||
elseif parsed.scheme == "file" then
|
||||
body, error = readfile(parsed.path)
|
||||
else error = string.format("unhandled scheme '%s'", parsed.scheme) end
|
||||
|
10
etc/get.lua
10
etc/get.lua
@ -87,9 +87,9 @@ end
|
||||
|
||||
-- determines the size of a http file
|
||||
function gethttpsize(u)
|
||||
local respt = http.request {method = "HEAD", url = u}
|
||||
if respt.code == 200 then
|
||||
return tonumber(respt.headers["content-length"])
|
||||
local r, c, h = http.request {method = "HEAD", url = u}
|
||||
if c == 200 then
|
||||
return tonumber(h["content-length"])
|
||||
end
|
||||
end
|
||||
|
||||
@ -98,8 +98,8 @@ function getbyhttp(u, file)
|
||||
local save = ltn12.sink.file(file or io.stdout)
|
||||
-- only print feedback if output is not stdout
|
||||
if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
|
||||
local respt = http.request {url = u, sink = save }
|
||||
if respt.code ~= 200 then print(respt.status or respt.error) end
|
||||
local r, c, h, s = http.request {url = u, sink = save }
|
||||
if c ~= 200 then io.stderr:write(s or c, "\n") end
|
||||
end
|
||||
|
||||
-- downloads a file using the ftp protocol
|
||||
|
Reference in New Issue
Block a user