2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2001-08-07 21:50:04 +02:00
|
|
|
-- HTTP/1.1 client support for the Lua language.
|
2003-06-26 20:47:49 +02:00
|
|
|
-- LuaSocket toolkit.
|
2000-12-29 23:15:09 +01:00
|
|
|
-- Author: Diego Nehab
|
2004-03-22 05:15:03 +01:00
|
|
|
-- Conforming to RFC 2616
|
2001-08-07 21:50:04 +02:00
|
|
|
-- RCS ID: $Id$
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
-- make sure LuaSocket is loaded
|
|
|
|
if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end
|
|
|
|
-- get LuaSocket namespace
|
|
|
|
local socket = _G[LUASOCKET_LIBNAME]
|
|
|
|
if not socket then error('module requires LuaSocket') end
|
2004-03-16 07:42:53 +01:00
|
|
|
-- create namespace inside LuaSocket namespace
|
|
|
|
socket.http = socket.http or {}
|
|
|
|
-- make all module globals fall into namespace
|
|
|
|
setmetatable(socket.http, { __index = _G })
|
|
|
|
setfenv(1, socket.http)
|
2001-07-29 05:51:36 +02:00
|
|
|
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Program constants
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- connection timeout in seconds
|
2004-01-16 08:06:31 +01:00
|
|
|
TIMEOUT = 60
|
2000-12-29 23:15:09 +01:00
|
|
|
-- default port for document retrieval
|
2004-01-16 08:06:31 +01:00
|
|
|
PORT = 80
|
2000-12-29 23:15:09 +01:00
|
|
|
-- user agent field sent in request
|
2004-01-19 19:22:51 +01:00
|
|
|
USERAGENT = socket.version
|
2001-06-06 22:55:45 +02:00
|
|
|
-- block size used in transfers
|
2004-03-16 07:42:53 +01:00
|
|
|
BLOCKSIZE = 2048
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Function return value selectors
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local function second(a, b)
|
|
|
|
return b
|
|
|
|
end
|
|
|
|
|
|
|
|
local function third(a, b, c)
|
|
|
|
return c
|
|
|
|
end
|
2001-07-29 05:51:36 +02:00
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function receive_headers(reqt, respt, tmp)
|
|
|
|
local sock = tmp.sock
|
2004-03-22 05:15:03 +01:00
|
|
|
local line, name, value, _
|
2004-03-22 08:45:07 +01:00
|
|
|
local headers = {}
|
2004-03-22 05:15:03 +01:00
|
|
|
-- store results
|
|
|
|
respt.headers = headers
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get first line
|
2004-03-21 08:50:15 +01:00
|
|
|
line = socket.try(sock:receive())
|
2000-12-29 23:15:09 +01:00
|
|
|
-- headers go until a blank line is found
|
|
|
|
while line ~= "" do
|
|
|
|
-- get field-name and value
|
2004-03-22 05:15:03 +01:00
|
|
|
_, _, name, value = string.find(line, "^(.-):%s*(.*)")
|
2004-03-21 08:50:15 +01:00
|
|
|
assert(name and value, "malformed reponse headers")
|
2002-12-03 00:34:41 +01:00
|
|
|
name = string.lower(name)
|
2000-12-29 23:15:09 +01:00
|
|
|
-- get next line (value might be folded)
|
2004-03-21 08:50:15 +01:00
|
|
|
line = socket.try(sock:receive())
|
2000-12-29 23:15:09 +01:00
|
|
|
-- unfold any folded values
|
2004-03-21 08:50:15 +01:00
|
|
|
while string.find(line, "^%s") do
|
2000-12-29 23:15:09 +01:00
|
|
|
value = value .. line
|
2004-03-21 08:50:15 +01:00
|
|
|
line = socket.try(sock:receive())
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
-- save pair in table
|
2001-07-29 05:51:36 +02:00
|
|
|
if headers[name] then headers[name] = headers[name] .. ", " .. value
|
|
|
|
else headers[name] = value end
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
|
|
|
|
2004-01-16 08:06:31 +01:00
|
|
|
local function abort(cb, err)
|
2004-03-16 07:42:53 +01:00
|
|
|
local go, cb_err = cb(nil, err)
|
2004-03-21 08:50:15 +01:00
|
|
|
error(cb_err or err)
|
2004-01-16 08:06:31 +01:00
|
|
|
end
|
|
|
|
|
2004-03-21 08:50:15 +01:00
|
|
|
local function hand(cb, chunk)
|
|
|
|
local go, cb_err = cb(chunk)
|
|
|
|
assert(go, cb_err or "aborted by callback")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function receive_body_bychunks(sock, sink)
|
2001-07-29 05:51:36 +02:00
|
|
|
while 1 do
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get chunk size, skip extention
|
2004-03-21 08:50:15 +01:00
|
|
|
local line, err = sock:receive()
|
|
|
|
if err then abort(sink, err) end
|
|
|
|
local size = tonumber(string.gsub(line, ";.*", ""), 16)
|
|
|
|
if not size then abort(sink, "invalid chunk size") end
|
2001-09-18 22:22:59 +02:00
|
|
|
-- was it the last chunk?
|
|
|
|
if size <= 0 then break end
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get chunk
|
2004-03-21 08:50:15 +01:00
|
|
|
local chunk, err = sock:receive(size)
|
|
|
|
if err then abort(sink, err) end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- pass chunk to callback
|
2004-03-21 08:50:15 +01:00
|
|
|
hand(sink, chunk)
|
2001-07-29 05:51:36 +02:00
|
|
|
-- skip CRLF on end of chunk
|
2004-03-21 08:50:15 +01:00
|
|
|
err = second(sock:receive())
|
|
|
|
if err then abort(sink, err) end
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
-- let callback know we are done
|
2004-03-21 08:50:15 +01:00
|
|
|
hand(sink, nil)
|
|
|
|
-- servers shouldn't send trailer headers, but who trusts them?
|
2004-03-22 05:15:03 +01:00
|
|
|
local line = socket.try(sock:receive())
|
|
|
|
while line ~= "" do
|
|
|
|
line = socket.try(sock:receive())
|
|
|
|
end
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2004-03-16 07:42:53 +01:00
|
|
|
local function receive_body_bylength(sock, length, sink)
|
2001-06-06 22:55:45 +02:00
|
|
|
while length > 0 do
|
2004-01-16 08:06:31 +01:00
|
|
|
local size = math.min(BLOCKSIZE, length)
|
2001-06-06 22:55:45 +02:00
|
|
|
local chunk, err = sock:receive(size)
|
2004-03-21 08:50:15 +01:00
|
|
|
if err then abort(sink, err) end
|
2004-01-16 08:06:31 +01:00
|
|
|
length = length - string.len(chunk)
|
|
|
|
-- see if there was an error
|
2004-03-21 08:50:15 +01:00
|
|
|
hand(sink, chunk)
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
2004-03-21 08:50:15 +01:00
|
|
|
-- let callback know we are done
|
|
|
|
hand(sink, nil)
|
2001-05-21 20:12:20 +02:00
|
|
|
end
|
|
|
|
|
2004-03-16 07:42:53 +01:00
|
|
|
local function receive_body_untilclosed(sock, sink)
|
2004-03-21 08:50:15 +01:00
|
|
|
while true do
|
|
|
|
local chunk, err, partial = sock:receive(BLOCKSIZE)
|
2004-01-16 08:06:31 +01:00
|
|
|
-- see if we are done
|
2004-03-21 08:50:15 +01:00
|
|
|
if err == "closed" then
|
|
|
|
hand(sink, partial)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
hand(sink, chunk)
|
2004-01-16 08:06:31 +01:00
|
|
|
-- see if there was an error
|
2004-03-21 08:50:15 +01:00
|
|
|
if err then abort(sink, err) end
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
2004-03-21 08:50:15 +01:00
|
|
|
-- let callback know we are done
|
|
|
|
hand(sink, nil)
|
2001-05-21 20:12:20 +02:00
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function receive_body(reqt, respt, tmp)
|
2004-03-21 08:50:15 +01:00
|
|
|
local sink = reqt.sink or ltn12.sink.null()
|
|
|
|
local headers = respt.headers
|
2004-03-22 08:45:07 +01:00
|
|
|
local sock = tmp.sock
|
2001-09-18 22:22:59 +02:00
|
|
|
local te = headers["transfer-encoding"]
|
2001-07-29 05:51:36 +02:00
|
|
|
if te and te ~= "identity" then
|
2001-05-21 20:12:20 +02:00
|
|
|
-- get by chunked transfer-coding of message body
|
2004-03-21 08:50:15 +01:00
|
|
|
receive_body_bychunks(sock, sink)
|
2001-07-29 05:51:36 +02:00
|
|
|
elseif tonumber(headers["content-length"]) then
|
2001-05-21 20:12:20 +02:00
|
|
|
-- get by content-length
|
2001-07-29 05:51:36 +02:00
|
|
|
local length = tonumber(headers["content-length"])
|
2004-03-21 08:50:15 +01:00
|
|
|
receive_body_bylength(sock, length, sink)
|
2000-12-29 23:15:09 +01:00
|
|
|
else
|
2001-05-21 20:12:20 +02:00
|
|
|
-- get it all until connection closes
|
2004-03-21 08:50:15 +01:00
|
|
|
receive_body_untilclosed(sock, sink)
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-03-16 07:42:53 +01:00
|
|
|
local function send_body_bychunks(data, source)
|
2004-03-21 08:50:15 +01:00
|
|
|
while true do
|
|
|
|
local chunk, err = source()
|
|
|
|
assert(chunk or not err, err)
|
|
|
|
if not chunk then break end
|
|
|
|
socket.try(data:send(string.format("%X\r\n", string.len(chunk))))
|
|
|
|
socket.try(data:send(chunk, "\r\n"))
|
2004-01-16 08:06:31 +01:00
|
|
|
end
|
2004-03-21 08:50:15 +01:00
|
|
|
socket.try(data:send("0\r\n\r\n"))
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2004-03-16 07:42:53 +01:00
|
|
|
local function send_body(data, source)
|
2004-03-21 08:50:15 +01:00
|
|
|
while true do
|
|
|
|
local chunk, err = source()
|
|
|
|
assert(chunk or not err, err)
|
|
|
|
if not chunk then break end
|
|
|
|
socket.try(data:send(chunk))
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-01-16 08:06:31 +01:00
|
|
|
local function send_headers(sock, headers)
|
2001-09-12 20:16:09 +02:00
|
|
|
-- send request headers
|
2004-03-21 08:50:15 +01:00
|
|
|
for i, v in pairs(headers) do
|
|
|
|
socket.try(sock:send(i .. ": " .. v .. "\r\n"))
|
2004-03-22 08:45:07 +01:00
|
|
|
--io.write(i .. ": " .. v .. "\r\n")
|
2001-09-12 20:16:09 +02:00
|
|
|
end
|
|
|
|
-- mark end of request headers
|
2004-03-21 08:50:15 +01:00
|
|
|
socket.try(sock:send("\r\n"))
|
2004-03-22 08:45:07 +01:00
|
|
|
--io.write("\r\n")
|
2001-09-12 20:16:09 +02:00
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function should_receive_body(reqt, respt, tmp)
|
2004-03-21 08:50:15 +01:00
|
|
|
if reqt.method == "HEAD" then return nil end
|
|
|
|
if respt.code == 204 or respt.code == 304 then return nil end
|
|
|
|
if respt.code >= 100 and respt.code < 200 then return nil end
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function receive_status(reqt, respt, tmp)
|
|
|
|
local status = socket.try(tmp.sock:receive())
|
2004-03-21 08:50:15 +01:00
|
|
|
local code = third(string.find(status, "HTTP/%d*%.%d* (%d%d%d)"))
|
|
|
|
-- store results
|
|
|
|
respt.code, respt.status = tonumber(code), status
|
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function request_uri(reqt, respt, tmp)
|
|
|
|
local url = tmp.parsed
|
2004-03-21 08:50:15 +01:00
|
|
|
if not reqt.proxy then
|
2004-03-22 08:45:07 +01:00
|
|
|
local parsed = tmp.parsed
|
2004-03-21 08:50:15 +01:00
|
|
|
url = {
|
|
|
|
path = parsed.path,
|
|
|
|
params = parsed.params,
|
|
|
|
query = parsed.query,
|
|
|
|
fragment = parsed.fragment
|
|
|
|
}
|
2004-03-22 08:45:07 +01:00
|
|
|
end
|
2004-03-21 08:50:15 +01:00
|
|
|
return socket.url.build(url)
|
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function send_request(reqt, respt, tmp)
|
|
|
|
local uri = request_uri(reqt, respt, tmp)
|
|
|
|
local sock = tmp.sock
|
|
|
|
local headers = tmp.headers
|
2001-06-06 22:55:45 +02:00
|
|
|
-- send request line
|
2004-03-21 08:50:15 +01:00
|
|
|
socket.try(sock:send((reqt.method or "GET")
|
|
|
|
.. " " .. uri .. " HTTP/1.1\r\n"))
|
2004-03-22 08:45:07 +01:00
|
|
|
--io.write((reqt.method or "GET")
|
|
|
|
--.. " " .. uri .. " HTTP/1.1\r\n")
|
2004-03-21 08:50:15 +01:00
|
|
|
-- send request headers headeres
|
|
|
|
if reqt.source and not headers["content-length"] then
|
2004-01-16 08:06:31 +01:00
|
|
|
headers["transfer-encoding"] = "chunked"
|
2003-08-31 02:58:07 +02:00
|
|
|
end
|
2004-03-21 08:50:15 +01:00
|
|
|
send_headers(sock, headers)
|
2001-06-06 22:55:45 +02:00
|
|
|
-- send request message body, if any
|
2004-03-21 08:50:15 +01:00
|
|
|
if reqt.source then
|
|
|
|
if headers["content-length"] then send_body(sock, reqt.source)
|
|
|
|
else send_body_bychunks(sock, reqt.source) end
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function open(reqt, respt, tmp)
|
2004-03-21 08:50:15 +01:00
|
|
|
local proxy = reqt.proxy or PROXY
|
|
|
|
local host, port
|
|
|
|
if proxy then
|
|
|
|
local pproxy = socket.url.parse(proxy)
|
|
|
|
assert(pproxy.port and pproxy.host, "invalid proxy")
|
|
|
|
host, port = pproxy.host, pproxy.port
|
|
|
|
else
|
2004-03-22 08:45:07 +01:00
|
|
|
host, port = tmp.parsed.host, tmp.parsed.port
|
2004-03-21 08:50:15 +01:00
|
|
|
end
|
|
|
|
-- store results
|
2004-03-22 08:45:07 +01:00
|
|
|
tmp.sock = socket.try(socket.tcp())
|
|
|
|
socket.try(tmp.sock:settimeout(reqt.timeout or TIMEOUT))
|
|
|
|
socket.try(tmp.sock:connect(host, port))
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function adjust_headers(reqt, respt, tmp)
|
2001-01-25 23:01:37 +01:00
|
|
|
local lower = {}
|
2004-03-21 08:50:15 +01:00
|
|
|
local headers = reqt.headers or {}
|
2001-07-29 05:51:36 +02:00
|
|
|
-- set default headers
|
2004-01-16 08:06:31 +01:00
|
|
|
lower["user-agent"] = USERAGENT
|
2001-07-29 05:51:36 +02:00
|
|
|
-- override with user values
|
|
|
|
for i,v in headers do
|
2002-12-03 00:34:41 +01:00
|
|
|
lower[string.lower(i)] = v
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
2004-03-22 08:45:07 +01:00
|
|
|
lower["host"] = tmp.parsed.host
|
2001-07-29 05:51:36 +02:00
|
|
|
-- this cannot be overriden
|
2001-01-25 23:01:37 +01:00
|
|
|
lower["connection"] = "close"
|
2004-03-21 08:50:15 +01:00
|
|
|
-- store results
|
2004-03-22 08:45:07 +01:00
|
|
|
tmp.headers = lower
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function parse_url(reqt, respt, tmp)
|
2004-03-21 08:50:15 +01:00
|
|
|
-- parse url with default fields
|
|
|
|
local parsed = socket.url.parse(reqt.url, {
|
|
|
|
host = "",
|
|
|
|
port = PORT,
|
|
|
|
path ="/",
|
|
|
|
scheme = "http"
|
|
|
|
})
|
|
|
|
-- scheme has to be http
|
|
|
|
if parsed.scheme ~= "http" then
|
|
|
|
error(string.format("unknown scheme '%s'", parsed.scheme))
|
|
|
|
end
|
|
|
|
-- explicit authentication info overrides that given by the URL
|
|
|
|
parsed.user = reqt.user or parsed.user
|
|
|
|
parsed.password = reqt.password or parsed.password
|
|
|
|
-- store results
|
2004-03-22 08:45:07 +01:00
|
|
|
tmp.parsed = parsed
|
2004-03-21 08:50:15 +01:00
|
|
|
end
|
|
|
|
|
2004-03-22 05:15:03 +01:00
|
|
|
-- forward declaration
|
|
|
|
local request_p
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function should_authorize(reqt, respt, tmp)
|
2001-07-29 05:51:36 +02:00
|
|
|
-- if there has been an authorization attempt, it must have failed
|
2004-03-21 08:50:15 +01:00
|
|
|
if reqt.headers and reqt.headers["authorization"] then return nil end
|
2004-03-22 05:15:03 +01:00
|
|
|
-- if last attempt didn't fail due to lack of authentication,
|
|
|
|
-- or we don't have authorization information, we can't retry
|
2004-03-22 08:45:07 +01:00
|
|
|
return respt.code == 401 and tmp.parsed.user and tmp.parsed.password
|
2001-05-21 20:12:20 +02:00
|
|
|
end
|
|
|
|
|
2004-03-21 08:50:15 +01:00
|
|
|
local function clone(headers)
|
|
|
|
if not headers then return nil end
|
|
|
|
local copy = {}
|
|
|
|
for i,v in pairs(headers) do
|
|
|
|
copy[i] = v
|
|
|
|
end
|
|
|
|
return copy
|
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function authorize(reqt, respt, tmp)
|
2004-03-21 08:50:15 +01:00
|
|
|
local headers = clone(reqt.headers) or {}
|
|
|
|
headers["authorization"] = "Basic " ..
|
2004-03-22 08:45:07 +01:00
|
|
|
(mime.b64(tmp.parsed.user .. ":" .. tmp.parsed.password))
|
2004-01-16 08:06:31 +01:00
|
|
|
local autht = {
|
|
|
|
method = reqt.method,
|
|
|
|
url = reqt.url,
|
2004-03-16 07:42:53 +01:00
|
|
|
source = reqt.source,
|
|
|
|
sink = reqt.sink,
|
2004-03-21 08:50:15 +01:00
|
|
|
headers = headers,
|
2004-01-19 01:24:41 +01:00
|
|
|
timeout = reqt.timeout,
|
2004-01-19 19:22:51 +01:00
|
|
|
proxy = reqt.proxy,
|
2001-07-29 05:51:36 +02:00
|
|
|
}
|
2004-03-22 08:45:07 +01:00
|
|
|
request_p(autht, respt, tmp)
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function should_redirect(reqt, respt, tmp)
|
2004-03-16 07:42:53 +01:00
|
|
|
return (reqt.redirect ~= false) and
|
|
|
|
(respt.code == 301 or respt.code == 302) and
|
2004-03-21 08:50:15 +01:00
|
|
|
(not reqt.method or reqt.method == "GET" or reqt.method == "HEAD")
|
2004-03-22 08:45:07 +01:00
|
|
|
and (not tmp.nredirects or tmp.nredirects < 5)
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
|
|
|
|
2004-03-22 08:45:07 +01:00
|
|
|
local function redirect(reqt, respt, tmp)
|
|
|
|
tmp.nredirects = (tmp.nredirects or 0) + 1
|
2004-01-16 08:06:31 +01:00
|
|
|
local redirt = {
|
|
|
|
method = reqt.method,
|
2001-08-07 21:50:04 +02:00
|
|
|
-- the RFC says the redirect URL has to be absolute, but some
|
2004-03-21 08:50:15 +01:00
|
|
|
-- servers do not respect that
|
2004-01-16 08:06:31 +01:00
|
|
|
url = socket.url.absolute(reqt.url, respt.headers["location"]),
|
2004-03-16 07:42:53 +01:00
|
|
|
source = reqt.source,
|
|
|
|
sink = reqt.sink,
|
2004-01-19 01:24:41 +01:00
|
|
|
headers = reqt.headers,
|
|
|
|
timeout = reqt.timeout,
|
2004-01-19 19:22:51 +01:00
|
|
|
proxy = reqt.proxy
|
2001-07-29 05:51:36 +02:00
|
|
|
}
|
2004-03-22 08:45:07 +01:00
|
|
|
request_p(redirt, respt, tmp)
|
2004-03-21 08:50:15 +01:00
|
|
|
-- we pass the location header as a clue we redirected
|
2004-01-16 08:06:31 +01:00
|
|
|
if respt.headers then respt.headers.location = redirt.url end
|
2001-09-12 20:16:09 +02:00
|
|
|
end
|
|
|
|
|
2004-03-22 05:15:03 +01:00
|
|
|
-- execute a request of through an exception
|
2004-03-22 08:45:07 +01:00
|
|
|
function request_p(reqt, respt, tmp)
|
|
|
|
parse_url(reqt, respt, tmp)
|
|
|
|
adjust_headers(reqt, respt, tmp)
|
|
|
|
open(reqt, respt, tmp)
|
|
|
|
send_request(reqt, respt, tmp)
|
|
|
|
receive_status(reqt, respt, tmp)
|
|
|
|
receive_headers(reqt, respt, tmp)
|
|
|
|
if should_redirect(reqt, respt, tmp) then
|
|
|
|
tmp.sock:close()
|
|
|
|
redirect(reqt, respt, tmp)
|
|
|
|
elseif should_authorize(reqt, respt, tmp) then
|
|
|
|
tmp.sock:close()
|
|
|
|
authorize(reqt, respt, tmp)
|
|
|
|
elseif should_receive_body(reqt, respt, tmp) then
|
|
|
|
receive_body(reqt, respt, tmp)
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
|
2004-01-16 08:06:31 +01:00
|
|
|
function request(reqt)
|
2004-03-22 08:45:07 +01:00
|
|
|
local respt, tmp = {}, {}
|
|
|
|
local s, e = pcall(request_p, reqt, respt, tmp)
|
2004-03-21 08:50:15 +01:00
|
|
|
if not s then respt.error = e end
|
2004-03-22 08:45:07 +01:00
|
|
|
if tmp.sock then tmp.sock:close() end
|
2004-01-16 08:06:31 +01:00
|
|
|
return respt
|
2001-07-29 05:51:36 +02:00
|
|
|
end
|
|
|
|
|
2004-03-21 08:50:15 +01:00
|
|
|
function get(url)
|
|
|
|
local t = {}
|
|
|
|
respt = request {
|
|
|
|
url = url,
|
|
|
|
sink = ltn12.sink.table(t)
|
|
|
|
}
|
2004-03-22 08:45:07 +01:00
|
|
|
return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers,
|
2004-03-21 08:50:15 +01:00
|
|
|
respt.code, respt.error
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
|
2004-03-21 08:50:15 +01:00
|
|
|
function post(url, body)
|
|
|
|
local t = {}
|
|
|
|
respt = request {
|
|
|
|
url = url,
|
|
|
|
method = "POST",
|
|
|
|
source = ltn12.source.string(body),
|
|
|
|
sink = ltn12.sink.table(t),
|
|
|
|
headers = { ["content-length"] = string.len(body) }
|
|
|
|
}
|
2004-03-22 08:45:07 +01:00
|
|
|
return (table.getn(t) > 0 or nil) and table.concat(t),
|
2004-03-21 08:50:15 +01:00
|
|
|
respt.headers, respt.code, respt.error
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|