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