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
|
2001-08-07 21:50:04 +02:00
|
|
|
-- RCS ID: $Id$
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-05-28 08:16:43 +02:00
|
|
|
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-06-15 08:24:00 +02:00
|
|
|
-- Load required modules
|
2004-06-04 17:15:45 +02:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
local socket = require("socket")
|
|
|
|
local ltn12 = require("ltn12")
|
|
|
|
local mime = require("mime")
|
|
|
|
local url = require("url")
|
2004-05-28 08:16:43 +02:00
|
|
|
|
2000-12-29 23:15:09 +01:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Program constants
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- connection timeout in seconds
|
2004-06-16 03:02:07 +02: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-06-15 08:24:00 +02:00
|
|
|
USERAGENT = socket.VERSION
|
2004-03-16 07:42:53 +01:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2004-06-15 08:24:00 +02:00
|
|
|
-- Low level HTTP API
|
2004-03-16 07:42:53 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2004-06-15 08:24:00 +02:00
|
|
|
local metat = { __index = {} }
|
|
|
|
|
|
|
|
function open(host, port)
|
2004-06-18 10:02:09 +02:00
|
|
|
local c = socket.try(socket.tcp())
|
2004-06-18 23:41:44 +02:00
|
|
|
local h = setmetatable({ c = c }, metat)
|
2004-06-18 10:02:09 +02:00
|
|
|
-- make sure the connection gets closed on exception
|
2004-06-18 23:41:44 +02:00
|
|
|
h.try = socket.newtry(function() h:close() end)
|
|
|
|
h.try(c:settimeout(TIMEOUT))
|
|
|
|
h.try(c:connect(host, port or PORT))
|
|
|
|
return h
|
2004-06-15 08:24:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:sendrequestline(method, uri)
|
|
|
|
local reqline = string.format("%s %s HTTP/1.1\r\n", method or "GET", uri)
|
2004-06-18 10:02:09 +02:00
|
|
|
return self.try(self.c:send(reqline))
|
2004-03-16 07:42:53 +01:00
|
|
|
end
|
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
function metat.__index:sendheaders(headers)
|
|
|
|
for i, v in pairs(headers) do
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.c:send(i .. ": " .. v .. "\r\n"))
|
2004-06-15 08:24:00 +02:00
|
|
|
end
|
|
|
|
-- mark end of request headers
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(self.c:send("\r\n"))
|
2004-06-15 08:24:00 +02:00
|
|
|
return 1
|
2004-03-16 07:42:53 +01:00
|
|
|
end
|
2001-07-29 05:51:36 +02:00
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
function metat.__index:sendbody(headers, source, step)
|
|
|
|
source = source or ltn12.source.empty()
|
|
|
|
step = step or ltn12.pump.step
|
|
|
|
-- if we don't know the size in advance, send chunked and hope for the best
|
2004-06-18 23:41:44 +02:00
|
|
|
local mode = "http-chunked"
|
|
|
|
if headers["content-length"] then mode = "keep-open" end
|
2004-06-18 10:02:09 +02:00
|
|
|
return self.try(ltn12.pump.all(source, socket.sink(mode, self.c), step))
|
2004-06-15 08:24:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:receivestatusline()
|
2004-06-18 10:02:09 +02:00
|
|
|
local status = self.try(self.c:receive())
|
2004-06-15 08:24:00 +02:00
|
|
|
local code = socket.skip(2, string.find(status, "HTTP/%d*%.%d* (%d%d%d)"))
|
2004-06-18 10:02:09 +02:00
|
|
|
return self.try(tonumber(code), status)
|
2004-06-15 08:24:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function metat.__index:receiveheaders()
|
2004-05-28 08:16:43 +02:00
|
|
|
local line, name, value
|
2004-03-22 08:45:07 +01:00
|
|
|
local headers = {}
|
2001-01-25 23:01:37 +01:00
|
|
|
-- get first line
|
2004-06-18 10:02:09 +02:00
|
|
|
line = self.try(self.c: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-05-28 08:16:43 +02:00
|
|
|
name, value = socket.skip(2, string.find(line, "^(.-):%s*(.*)"))
|
2004-06-18 10:02:09 +02:00
|
|
|
self.try(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-06-18 10:02:09 +02:00
|
|
|
line = self.try(self.c: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-06-18 10:02:09 +02:00
|
|
|
line = self.try(self.c: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
|
2004-06-15 08:24:00 +02:00
|
|
|
return headers
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
function metat.__index:receivebody(headers, sink, step)
|
|
|
|
sink = sink or ltn12.sink.null()
|
|
|
|
step = step or ltn12.pump.step
|
|
|
|
local length = tonumber(headers["content-length"])
|
|
|
|
local TE = headers["transfer-encoding"]
|
2004-06-18 23:41:44 +02:00
|
|
|
local mode = "default" -- connection close
|
2004-06-15 08:24:00 +02:00
|
|
|
if TE and TE ~= "identity" then mode = "http-chunked"
|
2004-06-18 23:41:44 +02:00
|
|
|
elseif tonumber(headers["content-length"]) then mode = "by-length" end
|
2004-06-18 10:02:09 +02:00
|
|
|
return self.try(ltn12.pump.all(socket.source(mode, self.c, length),
|
2004-06-15 08:24:00 +02:00
|
|
|
sink, step))
|
2001-06-06 22:55:45 +02:00
|
|
|
end
|
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
function metat.__index:close()
|
2004-06-18 10:02:09 +02:00
|
|
|
return self.c:close()
|
2001-09-12 20:16:09 +02:00
|
|
|
end
|
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- High level HTTP API
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local function uri(reqt)
|
|
|
|
local u = reqt
|
|
|
|
if not reqt.proxy and not PROXY then
|
|
|
|
u = {
|
2004-06-17 00:51:04 +02:00
|
|
|
path = socket.try(reqt.path, "invalid path 'nil'"),
|
2004-06-15 08:24:00 +02:00
|
|
|
params = reqt.params,
|
|
|
|
query = reqt.query,
|
|
|
|
fragment = reqt.fragment
|
2004-03-21 08:50:15 +01:00
|
|
|
}
|
2004-03-22 08:45:07 +01:00
|
|
|
end
|
2004-06-04 17:15:45 +02:00
|
|
|
return url.build(u)
|
2004-03-21 08:50:15 +01:00
|
|
|
end
|
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
local function adjustheaders(headers, host)
|
2001-01-25 23:01:37 +01:00
|
|
|
local lower = {}
|
2001-07-29 05:51:36 +02:00
|
|
|
-- override with user values
|
2004-06-15 08:24:00 +02:00
|
|
|
for i,v in (headers or lower) do
|
2002-12-03 00:34:41 +01:00
|
|
|
lower[string.lower(i)] = v
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
2004-06-04 17:15:45 +02:00
|
|
|
lower["user-agent"] = lower["user-agent"] or USERAGENT
|
|
|
|
-- these cannot be overriden
|
2004-06-15 08:24:00 +02:00
|
|
|
lower["host"] = host
|
|
|
|
return lower
|
2000-12-29 23:15:09 +01:00
|
|
|
end
|
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
local default = {
|
|
|
|
host = "",
|
|
|
|
port = PORT,
|
|
|
|
path ="/",
|
|
|
|
scheme = "http"
|
|
|
|
}
|
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
local function adjustrequest(reqt)
|
2004-06-16 23:56:23 +02:00
|
|
|
-- parse url if provided
|
2004-06-17 00:51:04 +02:00
|
|
|
local nreqt = reqt.url and url.parse(reqt.url, default) or {}
|
|
|
|
-- explicit components override url
|
|
|
|
for i,v in reqt do nreqt[i] = reqt[i] end
|
|
|
|
socket.try(nreqt.host, "invalid host '" .. tostring(nreqt.host) .. "'")
|
2004-06-15 08:24:00 +02:00
|
|
|
-- compute uri if user hasn't overriden
|
2004-06-17 00:51:04 +02:00
|
|
|
nreqt.uri = nreqt.uri or uri(nreqt)
|
2004-06-15 08:24:00 +02:00
|
|
|
-- adjust headers in request
|
2004-06-17 00:51:04 +02:00
|
|
|
nreqt.headers = adjustheaders(nreqt.headers, nreqt.host)
|
|
|
|
return nreqt
|
2004-03-21 08:50:15 +01:00
|
|
|
end
|
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
local function shouldredirect(reqt, code)
|
2004-06-15 08:24:00 +02:00
|
|
|
return (reqt.redirect ~= false) and
|
2004-06-16 23:56:23 +02:00
|
|
|
(code == 301 or code == 302) and
|
2004-06-15 08:24:00 +02:00
|
|
|
(not reqt.method or reqt.method == "GET" or reqt.method == "HEAD")
|
|
|
|
and (not reqt.nredirects or reqt.nredirects < 5)
|
|
|
|
end
|
2004-03-22 05:15:03 +01:00
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
local function shouldauthorize(reqt, code)
|
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-06-16 23:56:23 +02:00
|
|
|
return code == 401 and reqt.user and reqt.password
|
2001-05-21 20:12:20 +02:00
|
|
|
end
|
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
local function shouldreceivebody(reqt, code)
|
2004-06-15 08:24:00 +02:00
|
|
|
if reqt.method == "HEAD" then return nil end
|
|
|
|
if code == 204 or code == 304 then return nil end
|
|
|
|
if code >= 100 and code < 200 then return nil end
|
|
|
|
return 1
|
2004-03-21 08:50:15 +01:00
|
|
|
end
|
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
-- forward declarations
|
|
|
|
local trequest, tauthorize, tredirect
|
2001-06-06 22:55:45 +02:00
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
function tauthorize(reqt)
|
2004-06-15 08:24:00 +02:00
|
|
|
local auth = "Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password))
|
|
|
|
reqt.headers["authorization"] = auth
|
2004-06-16 23:56:23 +02:00
|
|
|
return trequest(reqt)
|
2001-01-25 23:01:37 +01:00
|
|
|
end
|
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
function tredirect(reqt, headers)
|
|
|
|
return trequest {
|
2004-06-18 23:41:44 +02:00
|
|
|
-- the RFC says the redirect URL has to be absolute, but some
|
|
|
|
-- servers do not respect that
|
2004-06-16 23:56:23 +02:00
|
|
|
url = url.absolute(reqt, 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,
|
2004-06-15 08:24:00 +02:00
|
|
|
proxy = reqt.proxy,
|
|
|
|
nredirects = (reqt.nredirects or 0) + 1
|
2001-07-29 05:51:36 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
function trequest(reqt)
|
|
|
|
reqt = adjustrequest(reqt)
|
2004-06-18 10:02:09 +02:00
|
|
|
local h = open(reqt.host, reqt.port)
|
|
|
|
h:sendrequestline(reqt.method, reqt.uri)
|
|
|
|
h:sendheaders(reqt.headers)
|
|
|
|
h:sendbody(reqt.headers, reqt.source, reqt.step)
|
2004-06-16 23:56:23 +02:00
|
|
|
local code, headers, status
|
2004-06-18 10:02:09 +02:00
|
|
|
code, status = h:receivestatusline()
|
|
|
|
headers = h:receiveheaders()
|
2004-06-16 23:56:23 +02:00
|
|
|
if shouldredirect(reqt, code) then
|
2004-06-18 10:02:09 +02:00
|
|
|
h:close()
|
2004-06-16 23:56:23 +02:00
|
|
|
return tredirect(reqt, headers)
|
|
|
|
elseif shouldauthorize(reqt, code) then
|
2004-06-18 10:02:09 +02:00
|
|
|
h:close()
|
2004-06-16 23:56:23 +02:00
|
|
|
return tauthorize(reqt)
|
|
|
|
elseif shouldreceivebody(reqt, code) then
|
2004-06-18 10:02:09 +02:00
|
|
|
h:receivebody(headers, reqt.sink, reqt.step)
|
2004-06-16 23:56:23 +02:00
|
|
|
end
|
2004-06-18 10:02:09 +02:00
|
|
|
h:close()
|
2004-06-16 23:56:23 +02:00
|
|
|
return 1, code, headers, status
|
|
|
|
end
|
2004-06-15 08:24:00 +02:00
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
local function srequest(u, body)
|
2004-03-21 08:50:15 +01:00
|
|
|
local t = {}
|
2004-06-16 23:56:23 +02:00
|
|
|
local reqt = {
|
2004-06-15 08:24:00 +02:00
|
|
|
url = u,
|
|
|
|
sink = ltn12.sink.table(t)
|
2004-03-21 08:50:15 +01:00
|
|
|
}
|
2004-06-16 23:56:23 +02:00
|
|
|
if body then
|
|
|
|
reqt.source = ltn12.source.string(body)
|
|
|
|
reqt.headers = { ["content-length"] = string.len(body) }
|
|
|
|
reqt.method = "POST"
|
|
|
|
end
|
|
|
|
local code, headers, status = socket.skip(1, trequest(reqt))
|
|
|
|
return table.concat(t), code, headers, status
|
|
|
|
end
|
2001-06-06 22:55:45 +02:00
|
|
|
|
2004-06-16 23:56:23 +02:00
|
|
|
request = socket.protect(function(reqt, body)
|
|
|
|
if type(reqt) == "string" then return srequest(reqt, body)
|
|
|
|
else return trequest(reqt) end
|
2004-06-15 08:24:00 +02:00
|
|
|
end)
|