mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 04:28:20 +01:00
Updated ftp and http methods for better async support
This commit is contained in:
parent
76ed24fe8a
commit
29306663d9
15
doc/ftp.html
15
doc/ftp.html
@ -95,7 +95,7 @@ the FTP module:
|
|||||||
<!-- ftp.get ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
<!-- ftp.get ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||||
|
|
||||||
<p class=name id=get>
|
<p class=name id=get>
|
||||||
ftp.<b>get(</b>url<b>)</b><br>
|
ftp.<b>get(</b>url [, create]<b>)</b><br>
|
||||||
ftp.<b>get{</b><br>
|
ftp.<b>get{</b><br>
|
||||||
host = <i>string</i>,<br>
|
host = <i>string</i>,<br>
|
||||||
sink = <i>LTN12 sink</i>,<br>
|
sink = <i>LTN12 sink</i>,<br>
|
||||||
@ -114,7 +114,10 @@ ftp.<b>get{</b><br>
|
|||||||
The <tt>get</tt> function has two forms. The simple form has fixed
|
The <tt>get</tt> function has two forms. The simple form has fixed
|
||||||
functionality: it downloads the contents of a URL and returns it as a
|
functionality: it downloads the contents of a URL and returns it as a
|
||||||
string. The generic form allows a <em>lot</em> more control, as explained
|
string. The generic form allows a <em>lot</em> more control, as explained
|
||||||
below.
|
below. The <tt>create</tt> parameter is only used in the string form,
|
||||||
|
and will be used as alternative for <a href=tcp.html#socket.tcp><tt>socket.tcp</tt></a>
|
||||||
|
if it is provided. In the advanced form <tt>create</tt> has to be specified
|
||||||
|
in the arguments table.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class=parameters>
|
<p class=parameters>
|
||||||
@ -180,7 +183,7 @@ end
|
|||||||
<!-- put ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
<!-- put ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||||
|
|
||||||
<p class=name id=put>
|
<p class=name id=put>
|
||||||
ftp.<b>put(</b>url, content<b>)</b><br>
|
ftp.<b>put(</b>url, content [, create]<b>)</b><br>
|
||||||
ftp.<b>put{</b><br>
|
ftp.<b>put{</b><br>
|
||||||
host = <i>string</i>,<br>
|
host = <i>string</i>,<br>
|
||||||
source = <i>LTN12 sink</i>,<br>
|
source = <i>LTN12 sink</i>,<br>
|
||||||
@ -198,7 +201,11 @@ ftp.<b>put{</b><br>
|
|||||||
<p class=description>
|
<p class=description>
|
||||||
The <tt>put</tt> function has two forms. The simple form has fixed
|
The <tt>put</tt> function has two forms. The simple form has fixed
|
||||||
functionality: it uploads a string of content into a URL. The generic form
|
functionality: it uploads a string of content into a URL. The generic form
|
||||||
allows a <em>lot</em> more control, as explained below.
|
allows a <em>lot</em> more control, as explained below. The <tt>create</tt>
|
||||||
|
parameter is only used in the string form, and will be used as alternative
|
||||||
|
for <a href=tcp.html#socket.tcp><tt>socket.tcp</tt></a> if it is provided.
|
||||||
|
In the advanced form <tt>create</tt> has to be specified in the arguments
|
||||||
|
table.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class=parameters>
|
<p class=parameters>
|
||||||
|
@ -122,7 +122,7 @@ the HTTP module:
|
|||||||
<!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
<!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
||||||
|
|
||||||
<p class=name id="request">
|
<p class=name id="request">
|
||||||
http.<b>request(</b>url [, body]<b>)</b><br>
|
http.<b>request(</b>url [, body [, create]]<b>)</b><br>
|
||||||
http.<b>request{</b><br>
|
http.<b>request{</b><br>
|
||||||
url = <i>string</i>,<br>
|
url = <i>string</i>,<br>
|
||||||
[sink = <i>LTN12 sink</i>,]<br>
|
[sink = <i>LTN12 sink</i>,]<br>
|
||||||
@ -148,7 +148,9 @@ If the first argument of the <tt>request</tt> function is a string, it
|
|||||||
should be an <tt>url</tt>. In that case, if a <tt>body</tt>
|
should be an <tt>url</tt>. In that case, if a <tt>body</tt>
|
||||||
is provided as a string, the function will perform a <tt>POST</tt> method
|
is provided as a string, the function will perform a <tt>POST</tt> method
|
||||||
in the <tt>url</tt>. Otherwise, it performs a <tt>GET</tt> in the
|
in the <tt>url</tt>. Otherwise, it performs a <tt>GET</tt> in the
|
||||||
<tt>url</tt>
|
<tt>url</tt>. The <tt>create</tt> parameter is only used in the string form,
|
||||||
|
and will be used as alternative for <a href=tcp.html#socket.tcp><tt>socket.tcp</tt></a>
|
||||||
|
if it is provided.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p class=parameters>
|
<p class=parameters>
|
||||||
|
14
src/ftp.lua
14
src/ftp.lua
@ -232,14 +232,15 @@ local function parse(u)
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sput(u, body)
|
local function sput(u, body, create)
|
||||||
local putt = parse(u)
|
local putt = parse(u)
|
||||||
putt.source = ltn12.source.string(body)
|
putt.source = ltn12.source.string(body)
|
||||||
|
putt.create = create
|
||||||
return tput(putt)
|
return tput(putt)
|
||||||
end
|
end
|
||||||
|
|
||||||
_M.put = socket.protect(function(putt, body)
|
_M.put = socket.protect(function(putt, body, create)
|
||||||
if base.type(putt) == "string" then return sput(putt, body)
|
if base.type(putt) == "string" then return sput(putt, body, create)
|
||||||
else return tput(putt) end
|
else return tput(putt) end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@ -256,10 +257,11 @@ local function tget(gett)
|
|||||||
return f:close()
|
return f:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sget(u)
|
local function sget(u, create)
|
||||||
local gett = parse(u)
|
local gett = parse(u)
|
||||||
local t = {}
|
local t = {}
|
||||||
gett.sink = ltn12.sink.table(t)
|
gett.sink = ltn12.sink.table(t)
|
||||||
|
gett.create = create
|
||||||
tget(gett)
|
tget(gett)
|
||||||
return table.concat(t)
|
return table.concat(t)
|
||||||
end
|
end
|
||||||
@ -277,8 +279,8 @@ _M.command = socket.protect(function(cmdt)
|
|||||||
return f:close()
|
return f:close()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
_M.get = socket.protect(function(gett)
|
_M.get = socket.protect(function(gett, create)
|
||||||
if base.type(gett) == "string" then return sget(gett)
|
if base.type(gett) == "string" then return sget(gett, create)
|
||||||
else return tget(gett) end
|
else return tget(gett) end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -329,11 +329,12 @@ end
|
|||||||
return 1, code, headers, status
|
return 1, code, headers, status
|
||||||
end
|
end
|
||||||
|
|
||||||
local function srequest(u, b)
|
local function srequest(u, b, create)
|
||||||
local t = {}
|
local t = {}
|
||||||
local reqt = {
|
local reqt = {
|
||||||
url = u,
|
url = u,
|
||||||
sink = ltn12.sink.table(t)
|
sink = ltn12.sink.table(t),
|
||||||
|
create = create,
|
||||||
}
|
}
|
||||||
if b then
|
if b then
|
||||||
reqt.source = ltn12.source.string(b)
|
reqt.source = ltn12.source.string(b)
|
||||||
@ -347,8 +348,8 @@ local function srequest(u, b)
|
|||||||
return table.concat(t), code, headers, status
|
return table.concat(t), code, headers, status
|
||||||
end
|
end
|
||||||
|
|
||||||
_M.request = socket.protect(function(reqt, body)
|
_M.request = socket.protect(function(reqt, body, create)
|
||||||
if base.type(reqt) == "string" then return srequest(reqt, body)
|
if base.type(reqt) == "string" then return srequest(reqt, body, create)
|
||||||
else return trequest(reqt) end
|
else return trequest(reqt) end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user