mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 20:38:22 +01:00
Streaming by callbacks implemented.
This commit is contained in:
parent
77090c53fe
commit
c53ad62b00
179
src/ftp.lua
179
src/ftp.lua
@ -15,6 +15,8 @@ local PORT = 21
|
|||||||
-- this is the default anonymous password. used when no password is
|
-- this is the default anonymous password. used when no password is
|
||||||
-- provided in url. should be changed for your e-mail.
|
-- provided in url. should be changed for your e-mail.
|
||||||
local EMAIL = "anonymous@anonymous.org"
|
local EMAIL = "anonymous@anonymous.org"
|
||||||
|
-- block size used in transfers
|
||||||
|
local BLOCKSIZE = 4096
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Parses a url and returns its scheme, user, password, host, port
|
-- Parses a url and returns its scheme, user, password, host, port
|
||||||
@ -68,7 +70,7 @@ local get_pasv = function(pasv)
|
|||||||
local ip, port
|
local ip, port
|
||||||
_,_, a, b, c, d, p1, p2 =
|
_,_, a, b, c, d, p1, p2 =
|
||||||
strfind(pasv, "(%d*),(%d*),(%d*),(%d*),(%d*),(%d*)")
|
strfind(pasv, "(%d*),(%d*),(%d*),(%d*),(%d*),(%d*)")
|
||||||
if not a or not b or not c or not d or not p1 or not p2 then
|
if not (a and b and c and d and p1 and p2) then
|
||||||
return nil, nil
|
return nil, nil
|
||||||
end
|
end
|
||||||
ip = format("%d.%d.%d.%d", a, b, c, d)
|
ip = format("%d.%d.%d.%d", a, b, c, d)
|
||||||
@ -82,6 +84,8 @@ end
|
|||||||
-- control: control connection socket
|
-- control: control connection socket
|
||||||
-- cmd: command
|
-- cmd: command
|
||||||
-- arg: command argument if any
|
-- arg: command argument if any
|
||||||
|
-- Returns
|
||||||
|
-- error message in case of error, nil otherwise
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local send_command = function(control, cmd, arg)
|
local send_command = function(control, cmd, arg)
|
||||||
local line, err
|
local line, err
|
||||||
@ -283,6 +287,24 @@ local logout = function(control)
|
|||||||
return code, answer
|
return code, answer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Receives data and send it to a callback
|
||||||
|
-- Input
|
||||||
|
-- data: data connection
|
||||||
|
-- callback: callback to return file contents
|
||||||
|
-- Returns
|
||||||
|
-- nil if successfull, or an error message in case of error
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
local receive_indirect = function(data, callback)
|
||||||
|
local chunk, err, res
|
||||||
|
while not err do
|
||||||
|
chunk, err = data:receive(%BLOCKSIZE)
|
||||||
|
if err == "closed" then err = "done" end
|
||||||
|
res = callback(chunk, err)
|
||||||
|
if not res then break end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Retrieves file or directory listing
|
-- Retrieves file or directory listing
|
||||||
-- Input
|
-- Input
|
||||||
@ -290,29 +312,60 @@ end
|
|||||||
-- server: server socket bound to local address
|
-- server: server socket bound to local address
|
||||||
-- file: file name under current directory
|
-- file: file name under current directory
|
||||||
-- isdir: is file a directory name?
|
-- isdir: is file a directory name?
|
||||||
|
-- callback: callback to receive file contents
|
||||||
-- Returns
|
-- Returns
|
||||||
-- file: string with file contents, nil if error
|
-- err: error message in case of error, nil otherwise
|
||||||
-- answer: server answer or error message
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local retrieve_file = function(control, server, file, isdir)
|
local retrieve = function(control, server, file, isdir, callback)
|
||||||
|
local code, answer
|
||||||
local data
|
local data
|
||||||
-- ask server for file or directory listing accordingly
|
-- ask server for file or directory listing accordingly
|
||||||
if isdir then code, answer = %try_command(control, "nlst", file, {150, 125})
|
if isdir then code, answer = %try_command(control, "nlst", file, {150, 125})
|
||||||
else code, answer = %try_command(control, "retr", file, {150, 125}) end
|
else code, answer = %try_command(control, "retr", file, {150, 125}) end
|
||||||
data, answer = server:accept()
|
data, answer = server:accept()
|
||||||
server:close()
|
server:close()
|
||||||
if not data then return nil, answer end
|
if not data then
|
||||||
-- download whole file
|
|
||||||
file, err = data:receive("*a")
|
|
||||||
data:close()
|
|
||||||
if err then
|
|
||||||
control:close()
|
control:close()
|
||||||
return nil, err
|
return answer
|
||||||
end
|
end
|
||||||
|
answer = %receive_indirect(data, callback)
|
||||||
|
if answer then
|
||||||
|
control:close()
|
||||||
|
return answer
|
||||||
|
end
|
||||||
|
data:close()
|
||||||
-- make sure file transfered ok
|
-- make sure file transfered ok
|
||||||
code, answer = %check_answer(control, {226, 250})
|
code, answer = %check_answer(control, {226, 250})
|
||||||
if not code then return nil, answer
|
if not code then return answer end
|
||||||
else return file, answer end
|
end
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Sends data comming from a callback
|
||||||
|
-- Input
|
||||||
|
-- data: data connection
|
||||||
|
-- send_cb: callback to produce file contents
|
||||||
|
-- chunk, size: first callback results
|
||||||
|
-- Returns
|
||||||
|
-- nil if successfull, or an error message in case of error
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
local try_sendindirect = function(data, send_cb, chunk, size)
|
||||||
|
local sent, err
|
||||||
|
sent = 0
|
||||||
|
while 1 do
|
||||||
|
if type(chunk) ~= "string" or type(size) ~= "number" then
|
||||||
|
data:close()
|
||||||
|
if not chunk and type(size) == "string" then return size
|
||||||
|
else return "invalid callback return" end
|
||||||
|
end
|
||||||
|
err = data:send(chunk)
|
||||||
|
if err then
|
||||||
|
data:close()
|
||||||
|
return err
|
||||||
|
end
|
||||||
|
sent = sent + strlen(chunk)
|
||||||
|
if sent >= size then break end
|
||||||
|
chunk, size = send_cb()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -321,29 +374,34 @@ end
|
|||||||
-- control: control connection with server
|
-- control: control connection with server
|
||||||
-- server: server socket bound to local address
|
-- server: server socket bound to local address
|
||||||
-- file: file name under current directory
|
-- file: file name under current directory
|
||||||
-- bytes: file contents in string
|
-- send_cb: callback to produce the file contents
|
||||||
-- Returns
|
-- Returns
|
||||||
-- code: return code, nil if error
|
-- code: return code, nil if error
|
||||||
-- answer: server answer or error message
|
-- answer: server answer or error message
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local store_file = function (control, server, file, bytes)
|
local store = function(control, server, file, send_cb)
|
||||||
local data
|
local data
|
||||||
local code, answer = %try_command(control, "stor", file, {150, 125})
|
local code, answer = %try_command(control, "stor", file, {150, 125})
|
||||||
if not code then
|
if not code then
|
||||||
data:close()
|
|
||||||
return nil, answer
|
|
||||||
end
|
|
||||||
data, answer = server:accept()
|
|
||||||
server:close()
|
|
||||||
if not data then return nil, answer end
|
|
||||||
-- send whole file and close connection to mark file end
|
|
||||||
answer = data:send(bytes)
|
|
||||||
data:close()
|
|
||||||
if answer then
|
|
||||||
control:close()
|
control:close()
|
||||||
return nil, answer
|
return nil, answer
|
||||||
end
|
end
|
||||||
-- check if file was received right
|
-- start data connection
|
||||||
|
data, answer = server:accept()
|
||||||
|
server:close()
|
||||||
|
if not data then
|
||||||
|
control:close()
|
||||||
|
return nil, answer
|
||||||
|
end
|
||||||
|
-- send whole file
|
||||||
|
err = %try_sendindirect(data, send_cb, send_cb())
|
||||||
|
if err then
|
||||||
|
control:close()
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
-- close connection to inform that file transmission is complete
|
||||||
|
data:close()
|
||||||
|
-- check if file was received correctly
|
||||||
return %check_answer(control, {226, 250})
|
return %check_answer(control, {226, 250})
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -365,55 +423,53 @@ end
|
|||||||
-- Retrieve a file from a ftp server
|
-- Retrieve a file from a ftp server
|
||||||
-- Input
|
-- Input
|
||||||
-- url: file location
|
-- url: file location
|
||||||
|
-- receive_cb: callback to receive file contents
|
||||||
-- type: "binary" or "ascii"
|
-- type: "binary" or "ascii"
|
||||||
-- Returns
|
-- Returns
|
||||||
-- file: downloaded file or nil in case of error
|
|
||||||
-- err: error message if any
|
-- err: error message if any
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function ftp_get(url, type)
|
function ftp_getindirect(url, receive_cb, type)
|
||||||
local control, server, data, err
|
local control, server, data, err
|
||||||
local answer, code, server, pfile, file
|
local answer, code, server, pfile, file
|
||||||
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL})
|
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL})
|
||||||
-- start control connection
|
-- start control connection
|
||||||
control, err = connect(parsed.host, parsed.port)
|
control, err = connect(parsed.host, parsed.port)
|
||||||
if not control then return nil, err end
|
if not control then return err end
|
||||||
control:timeout(%TIMEOUT)
|
control:timeout(%TIMEOUT)
|
||||||
-- get and check greeting
|
-- get and check greeting
|
||||||
code, answer = %check_greeting(control)
|
code, answer = %check_greeting(control)
|
||||||
if not code then return nil, answer end
|
if not code then return answer end
|
||||||
-- try to log in
|
-- try to log in
|
||||||
code, answer = %login(control, parsed.user, parsed.pass)
|
code, answer = %login(control, parsed.user, parsed.pass)
|
||||||
if not code then return nil, answer end
|
if not code then return answer end
|
||||||
-- go to directory
|
-- go to directory
|
||||||
pfile = %split_path(parsed.path)
|
pfile = %split_path(parsed.path)
|
||||||
if not pfile then return nil, "invalid path" end
|
if not pfile then return "invalid path" end
|
||||||
code, answer = %cwd(control, pfile.path)
|
code, answer = %cwd(control, pfile.path)
|
||||||
if not code then return nil, answer end
|
if not code then return answer end
|
||||||
-- change to binary type?
|
-- change to binary type?
|
||||||
code, answer = %change_type(control, type)
|
code, answer = %change_type(control, type)
|
||||||
if not code then return nil, answer end
|
if not code then return answer end
|
||||||
-- setup passive connection
|
-- setup passive connection
|
||||||
server, answer = %port(control)
|
server, answer = %port(control)
|
||||||
if not server then return nil, answer end
|
if not server then return answer end
|
||||||
-- ask server to send file or directory listing
|
-- ask server to send file or directory listing
|
||||||
file, answer = %retrieve_file(control, server, pfile.name, pfile.isdir)
|
err = %retrieve(control, server, pfile.name, pfile.isdir, receive_cb)
|
||||||
if not file then return nil, answer end
|
if err then return err end
|
||||||
-- disconnect
|
-- disconnect
|
||||||
%logout(control)
|
%logout(control)
|
||||||
-- return whatever file we received plus a possible error message
|
|
||||||
return file, answer
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Uploads a file to a FTP server
|
-- Uploads a file to a FTP server
|
||||||
-- Input
|
-- Input
|
||||||
-- url: file location
|
-- url: file location
|
||||||
-- bytes: file contents
|
-- send_cb: callback to produce the file contents
|
||||||
-- type: "binary" or "ascii"
|
-- type: "binary" or "ascii"
|
||||||
-- Returns
|
-- Returns
|
||||||
-- err: error message if any
|
-- err: error message if any
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function ftp_put(url, bytes, type)
|
function ftp_putindirect(url, send_cb, type)
|
||||||
local control, data
|
local control, data
|
||||||
local answer, code, server, file, pfile
|
local answer, code, server, file, pfile
|
||||||
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL})
|
parsed = %split_url(url, {user = "anonymous", port = 21, pass = %EMAIL})
|
||||||
@ -439,10 +495,51 @@ function ftp_put(url, bytes, type)
|
|||||||
server, answer = %port(control)
|
server, answer = %port(control)
|
||||||
if not server then return answer end
|
if not server then return answer end
|
||||||
-- ask server to send file
|
-- ask server to send file
|
||||||
code, answer = %store_file(control, server, pfile.name, bytes)
|
code, answer = %store(control, server, pfile.name, send_cb)
|
||||||
if not code then return answer end
|
if not code then return answer end
|
||||||
-- disconnect
|
-- disconnect
|
||||||
%logout(control)
|
%logout(control)
|
||||||
-- no errors
|
-- no errors
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Uploads a file to a FTP server
|
||||||
|
-- Input
|
||||||
|
-- url: file location
|
||||||
|
-- bytes: file contents
|
||||||
|
-- type: "binary" or "ascii"
|
||||||
|
-- Returns
|
||||||
|
-- err: error message if any
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
function ftp_put(url, bytes, type)
|
||||||
|
local send_cb = function()
|
||||||
|
return %bytes, strlen(%bytes)
|
||||||
|
end
|
||||||
|
return ftp_putindirect(url, send_cb, type)
|
||||||
|
end
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- We need fast concatenation routines for direct requests
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
dofile("buffer.lua")
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Retrieve a file from a ftp server
|
||||||
|
-- Input
|
||||||
|
-- url: file location
|
||||||
|
-- type: "binary" or "ascii"
|
||||||
|
-- Returns
|
||||||
|
-- data: file contents as a string
|
||||||
|
-- err: error message in case of error, nil otherwise
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
function ftp_get(url, type)
|
||||||
|
local bytes = { buf = buf_create() }
|
||||||
|
local receive_cb = function(chunk, err)
|
||||||
|
if not chunk then %bytes.buf = nil end
|
||||||
|
buf_addstring(%bytes.buf, chunk)
|
||||||
|
return 1
|
||||||
|
end
|
||||||
|
err = ftp_getindirect(url, receive_cb, type)
|
||||||
|
return buf_getresult(bytes.buf), err
|
||||||
|
end
|
||||||
|
321
src/http.lua
321
src/http.lua
@ -14,7 +14,9 @@ local TIMEOUT = 60
|
|||||||
-- default port for document retrieval
|
-- default port for document retrieval
|
||||||
local PORT = 80
|
local PORT = 80
|
||||||
-- user agent field sent in request
|
-- user agent field sent in request
|
||||||
local USERAGENT = "LuaSocket 1.3 HTTP 1.1"
|
local USERAGENT = "LuaSocket 1.3b HTTP 1.1"
|
||||||
|
-- block size used in transfers
|
||||||
|
local BLOCKSIZE = 4096
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Tries to get a pattern from the server and closes socket on error
|
-- Tries to get a pattern from the server and closes socket on error
|
||||||
@ -79,14 +81,14 @@ end
|
|||||||
-- Receive and parse responce header fields
|
-- Receive and parse responce header fields
|
||||||
-- Input
|
-- Input
|
||||||
-- sock: socket connected to the server
|
-- sock: socket connected to the server
|
||||||
-- headers: a table that might already contain headers
|
-- hdrs: a table that might already contain headers
|
||||||
-- Returns
|
-- Returns
|
||||||
-- headers: a table with all headers fields in the form
|
-- hdrs: a table with all headers fields in the form
|
||||||
-- {name_1 = "value_1", name_2 = "value_2" ... name_n = "value_n"}
|
-- {name_1 = "value_1", name_2 = "value_2" ... name_n = "value_n"}
|
||||||
-- all name_i are lowercase
|
-- all name_i are lowercase
|
||||||
-- nil and error message in case of error
|
-- nil and error message in case of error
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local get_hdrs = function(sock, headers)
|
local get_hdrs = function(sock, hdrs)
|
||||||
local line, err
|
local line, err
|
||||||
local name, value
|
local name, value
|
||||||
-- get first line
|
-- get first line
|
||||||
@ -111,106 +113,114 @@ local get_hdrs = function(sock, headers)
|
|||||||
if err then return nil, err end
|
if err then return nil, err end
|
||||||
end
|
end
|
||||||
-- save pair in table
|
-- save pair in table
|
||||||
if headers[name] then headers[name] = headers[name] .. ", " .. value
|
if hdrs[name] then hdrs[name] = hdrs[name] .. ", " .. value
|
||||||
else headers[name] = value end
|
else hdrs[name] = value end
|
||||||
end
|
end
|
||||||
return headers
|
return hdrs
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Receives a chunked message body
|
-- Receives a chunked message body
|
||||||
-- Input
|
-- Input
|
||||||
-- sock: socket connected to the server
|
-- sock: socket connected to the server
|
||||||
-- callback: function to receive chunks
|
-- receive_cb: function to receive chunks
|
||||||
-- Returns
|
-- Returns
|
||||||
-- nil if successfull or an error message in case of error
|
-- nil if successfull or an error message in case of error
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local try_getchunked = function(sock, callback)
|
local try_getchunked = function(sock, receive_cb)
|
||||||
local chunk, size, line, err
|
local chunk, size, line, err, go, uerr, _
|
||||||
repeat
|
repeat
|
||||||
-- get chunk size, skip extention
|
-- get chunk size, skip extention
|
||||||
line, err = %try_get(sock)
|
line, err = %try_get(sock)
|
||||||
if err then
|
if err then
|
||||||
callback(nil, err)
|
local _, uerr = receive_cb(nil, err)
|
||||||
return err
|
return uerr or err
|
||||||
end
|
end
|
||||||
size = tonumber(gsub(line, ";.*", ""), 16)
|
size = tonumber(gsub(line, ";.*", ""), 16)
|
||||||
if not size then
|
if not size then
|
||||||
|
err = "invalid chunk size"
|
||||||
sock:close()
|
sock:close()
|
||||||
callback(nil, "invalid chunk size")
|
_, uerr = receive_cb(nil, err)
|
||||||
return "invalid chunk size"
|
return uerr or err
|
||||||
end
|
end
|
||||||
-- get chunk
|
-- get chunk
|
||||||
chunk, err = %try_get(sock, size)
|
chunk, err = %try_get(sock, size)
|
||||||
if err then
|
if err then
|
||||||
callback(nil, err)
|
_, uerr = receive_cb(nil, err)
|
||||||
return err
|
return uerr or err
|
||||||
end
|
end
|
||||||
-- pass chunk to callback
|
-- pass chunk to callback
|
||||||
if not callback(chunk) then
|
go, uerr = receive_cb(chunk)
|
||||||
|
if not go then
|
||||||
sock:close()
|
sock:close()
|
||||||
return "aborted by callback"
|
return uerr or "aborted by callback"
|
||||||
end
|
end
|
||||||
-- skip blank line
|
-- skip blank line
|
||||||
_, err = %try_get(sock)
|
_, err = %try_get(sock)
|
||||||
if err then
|
if err then
|
||||||
callback(nil, err)
|
_, uerr = receive_cb(nil, err)
|
||||||
return err
|
return uerr or err
|
||||||
end
|
end
|
||||||
until size <= 0
|
until size <= 0
|
||||||
-- let callback know we are done
|
-- let callback know we are done
|
||||||
callback("", "done")
|
_, uerr = receive_cb("")
|
||||||
|
return uerr
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Receives a message body by content-length
|
-- Receives a message body by content-length
|
||||||
-- Input
|
-- Input
|
||||||
-- sock: socket connected to the server
|
-- sock: socket connected to the server
|
||||||
-- callback: function to receive chunks
|
-- receive_cb: function to receive chunks
|
||||||
-- Returns
|
-- Returns
|
||||||
-- nil if successfull or an error message in case of error
|
-- nil if successfull or an error message in case of error
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local try_getbylength = function(sock, length, callback)
|
local try_getbylength = function(sock, length, receive_cb)
|
||||||
|
local uerr, go
|
||||||
while length > 0 do
|
while length > 0 do
|
||||||
local size = min(4096, length)
|
local size = min(%BLOCKSIZE, length)
|
||||||
local chunk, err = sock:receive(size)
|
local chunk, err = sock:receive(size)
|
||||||
if err then
|
if err then
|
||||||
callback(nil, err)
|
go, uerr = receive_cb(nil, err)
|
||||||
return err
|
return uerr or err
|
||||||
end
|
end
|
||||||
if not callback(chunk) then
|
go, uerr = receive_cb(chunk)
|
||||||
|
if not go then
|
||||||
sock:close()
|
sock:close()
|
||||||
return "aborted by callback"
|
return uerr or "aborted by callback"
|
||||||
end
|
end
|
||||||
length = length - size
|
length = length - size
|
||||||
end
|
end
|
||||||
callback("", "done")
|
go, uerr = receive_cb("")
|
||||||
|
return uerr
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Receives a message body by content-length
|
-- Receives a message body by content-length
|
||||||
-- Input
|
-- Input
|
||||||
-- sock: socket connected to the server
|
-- sock: socket connected to the server
|
||||||
-- callback: function to receive chunks
|
-- receive_cb: function to receive chunks
|
||||||
-- Returns
|
-- Returns
|
||||||
-- nil if successfull or an error message in case of error
|
-- nil if successfull or an error message in case of error
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local try_getuntilclosed = function(sock, callback)
|
local try_getuntilclosed = function(sock, receive_cb)
|
||||||
local err
|
local err, go, uerr
|
||||||
while 1 do
|
while 1 do
|
||||||
local chunk, err = sock:receive(4096)
|
local chunk, err = sock:receive(%BLOCKSIZE)
|
||||||
if err == "closed" or not err then
|
if err == "closed" or not err then
|
||||||
if not callback(chunk) then
|
go, uerr = receive_cb(chunk)
|
||||||
|
if not go then
|
||||||
sock:close()
|
sock:close()
|
||||||
return "aborted by callback"
|
return uerr or "aborted by callback"
|
||||||
end
|
end
|
||||||
if err then break end
|
if err then break end
|
||||||
else
|
else
|
||||||
callback(nil, err)
|
go, uerr = callback(nil, err)
|
||||||
return err
|
return uerr or err
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
callback("", "done")
|
go, uerr = receive_cb("")
|
||||||
|
return uerr
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
@ -218,22 +228,22 @@ end
|
|||||||
-- Input
|
-- Input
|
||||||
-- sock: socket connected to the server
|
-- sock: socket connected to the server
|
||||||
-- resp_hdrs: response header fields
|
-- resp_hdrs: response header fields
|
||||||
-- callback: function to receive chunks
|
-- receive_cb: function to receive chunks
|
||||||
-- Returns
|
-- Returns
|
||||||
-- nil if successfull or an error message in case of error
|
-- nil if successfull or an error message in case of error
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local try_getbody = function(sock, resp_hdrs, callback)
|
local try_getbody = function(sock, resp_hdrs, receive_cb)
|
||||||
local err
|
local err
|
||||||
if resp_hdrs["transfer-encoding"] == "chunked" then
|
if resp_hdrs["transfer-encoding"] == "chunked" then
|
||||||
-- get by chunked transfer-coding of message body
|
-- get by chunked transfer-coding of message body
|
||||||
return %try_getchunked(sock, callback)
|
return %try_getchunked(sock, receive_cb)
|
||||||
elseif tonumber(resp_hdrs["content-length"]) then
|
elseif tonumber(resp_hdrs["content-length"]) then
|
||||||
-- get by content-length
|
-- get by content-length
|
||||||
local length = tonumber(resp_hdrs["content-length"])
|
local length = tonumber(resp_hdrs["content-length"])
|
||||||
return %try_getbylength(sock, length, callback)
|
return %try_getbylength(sock, length, receive_cb)
|
||||||
else
|
else
|
||||||
-- get it all until connection closes
|
-- get it all until connection closes
|
||||||
return %try_getuntilclosed(sock, callback)
|
return %try_getuntilclosed(sock, receive_cb)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -275,6 +285,35 @@ local split_url = function(url, default)
|
|||||||
return parsed
|
return parsed
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Sends data comming from a callback
|
||||||
|
-- Input
|
||||||
|
-- data: data connection
|
||||||
|
-- send_cb: callback to produce file contents
|
||||||
|
-- chunk, size: first callback results
|
||||||
|
-- Returns
|
||||||
|
-- nil if successfull, or an error message in case of error
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
local try_sendindirect = function(data, send_cb, chunk, size)
|
||||||
|
local sent, err
|
||||||
|
sent = 0
|
||||||
|
while 1 do
|
||||||
|
if type(chunk) ~= "string" or type(size) ~= "number" then
|
||||||
|
data:close()
|
||||||
|
if not chunk and type(size) == "string" then return size
|
||||||
|
else return "invalid callback return" end
|
||||||
|
end
|
||||||
|
err = data:send(chunk)
|
||||||
|
if err then
|
||||||
|
data:close()
|
||||||
|
return err
|
||||||
|
end
|
||||||
|
sent = sent + strlen(chunk)
|
||||||
|
if sent >= size then break end
|
||||||
|
chunk, size = send_cb()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Sends a http request message through socket
|
-- Sends a http request message through socket
|
||||||
-- Input
|
-- Input
|
||||||
@ -282,44 +321,37 @@ end
|
|||||||
-- method: request method to be used
|
-- method: request method to be used
|
||||||
-- path: url path
|
-- path: url path
|
||||||
-- req_hdrs: request headers to be sent
|
-- req_hdrs: request headers to be sent
|
||||||
-- callback: callback to send request message body
|
-- req_body_cb: callback to send request message body
|
||||||
-- Returns
|
-- Returns
|
||||||
-- err: nil in case of success, error message otherwise
|
-- err: nil in case of success, error message otherwise
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local send_request = function(sock, method, path, req_hdrs, callback)
|
local send_request = function(sock, method, path, req_hdrs, req_body_cb)
|
||||||
local chunk, size, done
|
local chunk, size, done, err
|
||||||
-- send request line
|
-- send request line
|
||||||
local err = %try_send(sock, method .. " " .. path .. " HTTP/1.1\r\n")
|
err = %try_send(sock, method .. " " .. path .. " HTTP/1.1\r\n")
|
||||||
if err then return err end
|
if err then return err end
|
||||||
|
-- if there is a request message body, add content-length header
|
||||||
|
if req_body_cb then
|
||||||
|
chunk, size = req_body_cb()
|
||||||
|
if type(chunk) == "string" and type(size) == "number" then
|
||||||
|
req_hdrs["content-length"] = tostring(size)
|
||||||
|
else
|
||||||
|
sock:close()
|
||||||
|
if not chunk and type(size) == "string" then return size
|
||||||
|
else return "invalid callback return" end
|
||||||
|
end
|
||||||
|
end
|
||||||
-- send request headers
|
-- send request headers
|
||||||
for i, v in req_hdrs do
|
for i, v in req_hdrs do
|
||||||
err = %try_send(sock, i .. ": " .. v .. "\r\n")
|
err = %try_send(sock, i .. ": " .. v .. "\r\n")
|
||||||
if err then return err end
|
if err then return err end
|
||||||
end
|
|
||||||
-- if there is a request message body, add content-length header
|
|
||||||
if callback then
|
|
||||||
chunk, size = callback()
|
|
||||||
if chunk and size then
|
|
||||||
err = %try_send(sock, "content-length: "..tostring(size).."\r\n")
|
|
||||||
if err then return err end
|
|
||||||
else
|
|
||||||
sock:close()
|
|
||||||
return size or "invalid callback return"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
-- mark end of request headers
|
-- mark end of request headers
|
||||||
err = %try_send(sock, "\r\n")
|
err = %try_send(sock, "\r\n")
|
||||||
if err then return err end
|
if err then return err end
|
||||||
-- send message request body, getting it chunk by chunk from callback
|
-- send request message body, if any
|
||||||
if callback then
|
if req_body_cb then
|
||||||
done = 0
|
return %try_sendindirect(sock, req_body_cb, chunk, size)
|
||||||
while chunk and chunk ~= "" and done < size do
|
|
||||||
err = %try_send(sock, chunk)
|
|
||||||
if err then return err end
|
|
||||||
done = done + strlen(chunk)
|
|
||||||
chunk, err = callback()
|
|
||||||
end
|
|
||||||
if not chunk then return err end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -344,18 +376,17 @@ end
|
|||||||
dofile("base64.lua")
|
dofile("base64.lua")
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Converts field names to lowercase and add message body size specification
|
-- Converts field names to lowercase and adds a few needed headers
|
||||||
-- Input
|
-- Input
|
||||||
-- headers: request header fields
|
-- hdrs: request header fields
|
||||||
-- parsed: parsed url components
|
-- parsed: parsed url components
|
||||||
-- body: request message body, if any
|
|
||||||
-- Returns
|
-- Returns
|
||||||
-- lower: a table with the same headers, but with lowercase field names
|
-- lower: a table with the same headers, but with lowercase field names
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
local fill_hdrs = function(headers, parsed, body)
|
local fill_hdrs = function(hdrs, parsed)
|
||||||
local lower = {}
|
local lower = {}
|
||||||
headers = headers or {}
|
hdrs = hdrs or {}
|
||||||
for i,v in headers do
|
for i,v in hdrs do
|
||||||
lower[strlower(i)] = v
|
lower[strlower(i)] = v
|
||||||
end
|
end
|
||||||
lower["connection"] = "close"
|
lower["connection"] = "close"
|
||||||
@ -374,15 +405,17 @@ end
|
|||||||
-- Input
|
-- Input
|
||||||
-- method: "GET", "PUT", "POST" etc
|
-- method: "GET", "PUT", "POST" etc
|
||||||
-- url: target uniform resource locator
|
-- url: target uniform resource locator
|
||||||
-- req_hdrs: request headers to send
|
-- resp_body_cb: response message body receive callback
|
||||||
-- req_body: function to return request message body
|
-- req_hdrs: request headers to send, or nil if none
|
||||||
-- resp_body: function to receive response message body
|
-- req_body_cb: request message body send callback, or nil if none
|
||||||
|
-- stay: should we refrain from following a server redirect message?
|
||||||
-- Returns
|
-- Returns
|
||||||
-- resp_hdrs: response header fields received, if sucessfull
|
-- resp_hdrs: response header fields received, or nil if failed
|
||||||
-- resp_line: server response status line, if successfull
|
-- resp_line: server response status line, or nil if failed
|
||||||
-- err: error message if any
|
-- err: error message, or nil if successfull
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function http_requestindirect(method, url, req_hdrs, req_body, resp_body)
|
function http_requestindirect(method, url, resp_body_cb, req_hdrs,
|
||||||
|
req_body_cb, stay)
|
||||||
local sock, err
|
local sock, err
|
||||||
local resp_hdrs
|
local resp_hdrs
|
||||||
local resp_line, resp_code
|
local resp_line, resp_code
|
||||||
@ -398,7 +431,7 @@ function http_requestindirect(method, url, req_hdrs, req_body, resp_body)
|
|||||||
-- set connection timeout
|
-- set connection timeout
|
||||||
sock:timeout(%TIMEOUT)
|
sock:timeout(%TIMEOUT)
|
||||||
-- send request
|
-- send request
|
||||||
err = %send_request(sock, method, parsed.path, req_hdrs, req_body)
|
err = %send_request(sock, method, parsed.path, req_hdrs, req_body_cb)
|
||||||
if err then return nil, nil, err end
|
if err then return nil, nil, err end
|
||||||
-- get server message
|
-- get server message
|
||||||
resp_code, resp_line, err = %get_status(sock)
|
resp_code, resp_line, err = %get_status(sock)
|
||||||
@ -407,83 +440,123 @@ function http_requestindirect(method, url, req_hdrs, req_body, resp_body)
|
|||||||
resp_hdrs, err = %get_hdrs(sock, {})
|
resp_hdrs, err = %get_hdrs(sock, {})
|
||||||
if err then return nil, line, err end
|
if err then return nil, line, err end
|
||||||
-- did we get a redirect? should we automatically retry?
|
-- did we get a redirect? should we automatically retry?
|
||||||
if (resp_code == 301 or resp_code == 302) and
|
if not stay and (resp_code == 301 or resp_code == 302) and
|
||||||
(method == "GET" or method == "HEAD") then
|
(method == "GET" or method == "HEAD") then
|
||||||
sock:close()
|
sock:close()
|
||||||
return http_requestindirect(method, resp_hdrs["location"], req_hdrs,
|
return http_requestindirect(method, resp_hdrs["location"],
|
||||||
req_body, resp_body)
|
resp_body_cb, req_hdrs, req_body_cb, stay)
|
||||||
end
|
end
|
||||||
-- get body if status and method combination allow one
|
-- get response message body if status and method combination allow one
|
||||||
if has_respbody(method, resp_code) then
|
if has_respbody(method, resp_code) then
|
||||||
err = %try_getbody(sock, resp_hdrs, resp_body)
|
err = %try_getbody(sock, resp_hdrs, resp_body_cb)
|
||||||
if err then return resp_hdrs, resp_line, err end
|
if err then return resp_hdrs, resp_line, err end
|
||||||
end
|
end
|
||||||
sock:close()
|
sock:close()
|
||||||
return resp_hdrs, resp_line
|
return resp_hdrs, resp_line
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- We need fast concatenation routines for direct requests
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
dofile("buffer.lua")
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Sends a HTTP request and retrieves the server reply
|
-- Sends a HTTP request and retrieves the server reply
|
||||||
-- Input
|
-- Input
|
||||||
-- method: "GET", "PUT", "POST" etc
|
-- method: "GET", "PUT", "POST" etc
|
||||||
-- url: target uniform resource locator
|
-- url: target uniform resource locator
|
||||||
-- headers: request headers to send
|
-- req_hdrs: request headers to send, or nil if none
|
||||||
-- body: request message body
|
-- req_body: request message body as a string, or nil if none
|
||||||
|
-- stay: should we refrain from following a server redirect message?
|
||||||
-- Returns
|
-- Returns
|
||||||
-- resp_body: response message body, if successfull
|
-- resp_body: response message body, or nil if failed
|
||||||
-- resp_hdrs: response header fields received, if sucessfull
|
-- resp_hdrs: response header fields received, or nil if failed
|
||||||
-- resp_line: server response status line, if successfull
|
-- resp_line: server response status line, or nil if failed
|
||||||
-- err: error message if any
|
-- err: error message, or nil if successfull
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function http_request(method, url, req_hdrs, body)
|
function http_request(method, url, req_hdrs, req_body, stay)
|
||||||
local resp_hdrs, resp_line, err
|
local resp_hdrs, resp_line, err
|
||||||
local req_callback = function()
|
local req_body_cb = function()
|
||||||
return %body, strlen(%body)
|
return %req_body, strlen(%req_body)
|
||||||
end
|
end
|
||||||
local resp_aux = { resp_body = "" }
|
local resp_body = { buf = buf_create() }
|
||||||
local resp_callback = function(chunk, err)
|
local resp_body_cb = function(chunk, err)
|
||||||
if not chunk then
|
if not chunk then %resp_body.buf = nil end
|
||||||
%resp_aux.resp_body = nil
|
buf_addstring(%resp_body.buf, chunk)
|
||||||
%resp_aux.err = err
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
%resp_aux.resp_body = %resp_aux.resp_body .. chunk
|
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
if not body then resp_callback = nil end
|
if not req_body then req_body_cb = nil end
|
||||||
resp_hdrs, resp_line, err = http_requestindirect(method, url, req_hdrs,
|
resp_hdrs, resp_line, err = http_requestindirect(method, url, resp_body_cb,
|
||||||
req_callback, resp_callback)
|
req_hdrs, req_body_cb, stay)
|
||||||
if err then return nil, resp_hdrs, resp_line, err
|
return buf_getresult(resp_body.buf), resp_hdrs, resp_line, err
|
||||||
else return resp_aux.resp_body, resp_hdrs, resp_line, resp_aux.err end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Retrieves a URL by the method "GET"
|
-- Retrieves a URL by the method "GET"
|
||||||
-- Input
|
-- Input
|
||||||
-- url: target uniform resource locator
|
-- url: target uniform resource locator
|
||||||
-- headers: request headers to send
|
-- req_hdrs: request headers to send, or nil if none
|
||||||
|
-- stay: should we refrain from following a server redirect message?
|
||||||
-- Returns
|
-- Returns
|
||||||
-- body: response message body, if successfull
|
-- resp_body: response message body, or nil if failed
|
||||||
-- headers: response header fields, if sucessfull
|
-- resp_hdrs: response header fields received, or nil if failed
|
||||||
-- line: response status line, if successfull
|
-- resp_line: server response status line, or nil if failed
|
||||||
-- err: error message, if any
|
-- err: error message, or nil if successfull
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function http_get(url, headers)
|
function http_get(url, req_hdrs, stay)
|
||||||
return http_request("GET", url, headers)
|
return http_request("GET", url, req_hdrs, stay)
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
-- Retrieves a URL by the method "GET"
|
-- Retrieves a URL by the method "GET"
|
||||||
-- Input
|
-- Input
|
||||||
-- url: target uniform resource locator
|
-- url: target uniform resource locator
|
||||||
-- body: request message body
|
-- resp_body_cb: response message body receive callback
|
||||||
-- headers: request headers to send
|
-- req_hdrs: request headers to send, or nil if none
|
||||||
|
-- stay: should we refrain from following a server redirect message?
|
||||||
-- Returns
|
-- Returns
|
||||||
-- body: response message body, if successfull
|
-- resp_body: response message body, or nil if failed
|
||||||
-- headers: response header fields, if sucessfull
|
-- resp_hdrs: response header fields received, or nil if failed
|
||||||
-- line: response status line, if successfull
|
-- resp_line: server response status line, or nil if failed
|
||||||
-- err: error message, if any
|
-- err: error message, or nil if successfull
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function http_post(url, body, headers)
|
function http_getindirect(url, resp_body_cb, req_hdrs, stay)
|
||||||
return http_request("POST", url, headers, body)
|
return http_requestindirect("GET", url, resp_body_cb, req_hdrs, nil, stay)
|
||||||
|
end
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Retrieves a URL by the method "POST"
|
||||||
|
-- Input
|
||||||
|
-- method: "GET", "PUT", "POST" etc
|
||||||
|
-- url: target uniform resource locator
|
||||||
|
-- req_hdrs: request headers to send, or nil if none
|
||||||
|
-- req_body: request message body, or nil if none
|
||||||
|
-- stay: should we refrain from following a server redirect message?
|
||||||
|
-- Returns
|
||||||
|
-- resp_body: response message body, or nil if failed
|
||||||
|
-- resp_hdrs: response header fields received, or nil if failed
|
||||||
|
-- resp_line: server response status line, or nil if failed
|
||||||
|
-- err: error message, or nil if successfull
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
function http_post(url, req_body, req_hdrs, stay)
|
||||||
|
return http_request("POST", url, req_hdrs, req_body, stay)
|
||||||
|
end
|
||||||
|
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
-- Retrieves a URL by the method "POST"
|
||||||
|
-- Input
|
||||||
|
-- url: target uniform resource locator
|
||||||
|
-- resp_body_cb: response message body receive callback
|
||||||
|
-- req_body_cb: request message body send callback
|
||||||
|
-- req_hdrs: request headers to send, or nil if none
|
||||||
|
-- stay: should we refrain from following a server redirect message?
|
||||||
|
-- Returns
|
||||||
|
-- resp_body: response message body, or nil if failed
|
||||||
|
-- resp_hdrs: response header fields received, or nil if failed
|
||||||
|
-- resp_line: server response status line, or nil if failed
|
||||||
|
-- err: error message, or nil if successfull
|
||||||
|
-----------------------------------------------------------------------------
|
||||||
|
function http_getindirect(url, resp_body_cb, req_body_cb, req_hdrs, stay)
|
||||||
|
return http_requestindirect("GET", url, resp_body_cb, req_hdrs,
|
||||||
|
req_body_cb, stay)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user