mirror of
https://github.com/lunarmodules/luasocket.git
synced 2025-07-04 18:14:30 +02:00
expose parseRequest() methods for ftp and http requests
This commit is contained in:
parent
321c0c9b1f
commit
a712c4d811
47
src/ftp.lua
47
src/ftp.lua
@ -232,17 +232,6 @@ local function parse(u)
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sput(u, body)
|
|
||||||
local putt = parse(u)
|
|
||||||
putt.source = ltn12.source.string(body)
|
|
||||||
return tput(putt)
|
|
||||||
end
|
|
||||||
|
|
||||||
_M.put = socket.protect(function(putt, body)
|
|
||||||
if base.type(putt) == "string" then return sput(putt, body)
|
|
||||||
else return tput(putt) end
|
|
||||||
end)
|
|
||||||
|
|
||||||
local function tget(gett)
|
local function tget(gett)
|
||||||
gett = override(gett)
|
gett = override(gett)
|
||||||
socket.try(gett.host, "missing hostname")
|
socket.try(gett.host, "missing hostname")
|
||||||
@ -256,12 +245,17 @@ local function tget(gett)
|
|||||||
return f:close()
|
return f:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sget(u)
|
-- parses a simple form into the advanced form
|
||||||
local gett = parse(u)
|
-- if `body` is provided, a PUT, otherwise a GET.
|
||||||
local t = {}
|
-- If GET, then a field `target` is added to store the results
|
||||||
gett.sink = ltn12.sink.table(t)
|
_M.parseRequest = function(u, body)
|
||||||
tget(gett)
|
local t = parse(u)
|
||||||
return table.concat(t)
|
if body then
|
||||||
|
t.source = ltn12.source.string(body)
|
||||||
|
else
|
||||||
|
t.target = {}
|
||||||
|
t.sink = ltn12.sink.table(t.target)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
_M.command = socket.protect(function(cmdt)
|
_M.command = socket.protect(function(cmdt)
|
||||||
@ -277,9 +271,24 @@ _M.command = socket.protect(function(cmdt)
|
|||||||
return f:close()
|
return f:close()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
_M.put = socket.protect(function(putt, body)
|
||||||
|
if base.type(putt) == "string" then
|
||||||
|
putt = _M.parseRequest(putt, body)
|
||||||
|
tput(putt)
|
||||||
|
return table.concat(putt.target)
|
||||||
|
else
|
||||||
|
return tput(putt)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
_M.get = socket.protect(function(gett)
|
_M.get = socket.protect(function(gett)
|
||||||
if base.type(gett) == "string" then return sget(gett)
|
if base.type(gett) == "string" then
|
||||||
else return tget(gett) end
|
gett = _M.parseRequest(gett)
|
||||||
|
tget(gett)
|
||||||
|
return table.concat(gett.target)
|
||||||
|
else
|
||||||
|
return tget(gett)
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return _M
|
return _M
|
21
src/http.lua
21
src/http.lua
@ -76,7 +76,7 @@ socket.sourcet["http-chunked"] = function(sock, headers)
|
|||||||
-- was it the last chunk?
|
-- was it the last chunk?
|
||||||
if size > 0 then
|
if size > 0 then
|
||||||
-- if not, get chunk and skip terminating CRLF
|
-- if not, get chunk and skip terminating CRLF
|
||||||
local chunk, err, part = sock:receive(size)
|
local chunk, err = sock:receive(size)
|
||||||
if chunk then sock:receive() end
|
if chunk then sock:receive() end
|
||||||
return chunk, err
|
return chunk, err
|
||||||
else
|
else
|
||||||
@ -329,11 +329,14 @@ end
|
|||||||
return 1, code, headers, status
|
return 1, code, headers, status
|
||||||
end
|
end
|
||||||
|
|
||||||
local function srequest(u, b)
|
-- parses a shorthand form into the advanced table form.
|
||||||
|
-- adds field `target` to the table. This will hold the return values.
|
||||||
|
_M.parseRequest = function(u, b)
|
||||||
local t = {}
|
local t = {}
|
||||||
local reqt = {
|
local reqt = {
|
||||||
url = u,
|
url = u,
|
||||||
sink = ltn12.sink.table(t)
|
sink = ltn12.sink.table(t),
|
||||||
|
target = t,
|
||||||
}
|
}
|
||||||
if b then
|
if b then
|
||||||
reqt.source = ltn12.source.string(b)
|
reqt.source = ltn12.source.string(b)
|
||||||
@ -343,13 +346,17 @@ local function srequest(u, b)
|
|||||||
}
|
}
|
||||||
reqt.method = "POST"
|
reqt.method = "POST"
|
||||||
end
|
end
|
||||||
local code, headers, status = socket.skip(1, trequest(reqt))
|
return reqt
|
||||||
return table.concat(t), code, headers, status
|
|
||||||
end
|
end
|
||||||
|
|
||||||
_M.request = socket.protect(function(reqt, body)
|
_M.request = socket.protect(function(reqt, body)
|
||||||
if base.type(reqt) == "string" then return srequest(reqt, body)
|
if base.type(reqt) == "string" then
|
||||||
else return trequest(reqt) end
|
reqt = _M.parseRequest(reqt, body)
|
||||||
|
local t, code, headers, status = reqt.target, socket.skip(1, trequest(reqt))
|
||||||
|
return table.concat(t), code, headers, status
|
||||||
|
else
|
||||||
|
return trequest(reqt)
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return _M
|
return _M
|
||||||
|
Loading…
x
Reference in New Issue
Block a user