diff options
Diffstat (limited to 'Build/source/texk/web2c/luatexdir/luasocket/src/http.lua')
-rw-r--r-- | Build/source/texk/web2c/luatexdir/luasocket/src/http.lua | 109 |
1 files changed, 71 insertions, 38 deletions
diff --git a/Build/source/texk/web2c/luatexdir/luasocket/src/http.lua b/Build/source/texk/web2c/luatexdir/luasocket/src/http.lua index 4c7e7d80e30..ac7b2c0ffa7 100644 --- a/Build/source/texk/web2c/luatexdir/luasocket/src/http.lua +++ b/Build/source/texk/web2c/luatexdir/luasocket/src/http.lua @@ -7,25 +7,30 @@ ----------------------------------------------------------------------------- -- Declare module and import dependencies ------------------------------------------------------------------------------- -local socket = require("socket") -local url = require("socket.url") -local ltn12 = require("ltn12") -local mime = require("mime") +local socket = socket or require("socket") +local url = socket.url or require("socket.url") +local ltn12 = ltn12 or require("ltn12") +local mime = mime or require("mime") local string = require("string") -local headers = require("socket.headers") +local headers = socket.headers or require("socket.headers") local base = _G local table = require("table") +socket.http = {} +local _M = socket.http module("socket.http") ----------------------------------------------------------------------------- -- Program constants ----------------------------------------------------------------------------- -- connection timeout in seconds -TIMEOUT = 60 --- default port for document retrieval -PORT = 80 +_M.TIMEOUT = 60 -- user agent field sent in request -USERAGENT = socket._VERSION +_M.USERAGENT = socket._VERSION + +-- supported schemes +local SCHEMES = { ["http"] = true } +-- default port for document retrieval +local PORT = 80 ----------------------------------------------------------------------------- -- Reads MIME headers from a connection, unfolding where needed @@ -105,14 +110,14 @@ end ----------------------------------------------------------------------------- local metat = { __index = {} } -function open(host, port, create) +function _M.open(host, port, create) -- create socket with user connect function, or with default local c = socket.try((create or socket.tcp)()) local h = base.setmetatable({ c = c }, metat) -- create finalized try h.try = socket.newtry(function() h:close() end) -- set timeout before connecting - h.try(c:settimeout(TIMEOUT)) + h.try(c:settimeout(_M.TIMEOUT)) h.try(c:connect(host, port or PORT)) -- here everything worked return h @@ -185,7 +190,7 @@ end local function adjusturi(reqt) local u = reqt -- if there is a proxy, we need the full url. otherwise, just a part. - if not reqt.proxy and not PROXY then + if not reqt.proxy and not _M.PROXY then u = { path = socket.try(reqt.path, "invalid path 'nil'"), params = reqt.params, @@ -197,7 +202,7 @@ local function adjusturi(reqt) end local function adjustproxy(reqt) - local proxy = reqt.proxy or PROXY + local proxy = reqt.proxy or _M.PROXY if proxy then proxy = url.parse(proxy) return proxy.host, proxy.port or 3128 @@ -208,16 +213,27 @@ end local function adjustheaders(reqt) -- default headers + local host = string.gsub(reqt.authority, "^.-@", "") local lower = { - ["user-agent"] = USERAGENT, - ["host"] = reqt.host, + ["user-agent"] = _M.USERAGENT, + ["host"] = host, ["connection"] = "close, TE", ["te"] = "trailers" } -- if we have authentication information, pass it along if reqt.user and reqt.password then - lower["authorization"] = - "Basic " .. (mime.b64(reqt.user .. ":" .. reqt.password)) + lower["authorization"] = + "Basic " .. (mime.b64(reqt.user .. ":" .. + url.unescape(reqt.password))) + end + -- if we have proxy authentication information, pass it along + local proxy = reqt.proxy or _M.PROXY + if proxy then + proxy = url.parse(proxy) + if proxy.user and proxy.password then + lower["proxy-authorization"] = + "Basic " .. (mime.b64(proxy.user .. ":" .. proxy.password)) + end end -- override with user headers for i,v in base.pairs(reqt.headers or lower) do @@ -239,22 +255,27 @@ local function adjustrequest(reqt) local nreqt = reqt.url and url.parse(reqt.url, default) or {} -- explicit components override url for i,v in base.pairs(reqt) do nreqt[i] = v end - if nreqt.port == "" then nreqt.port = 80 end - socket.try(nreqt.host and nreqt.host ~= "", - "invalid host '" .. base.tostring(nreqt.host) .. "'") + if nreqt.port == "" then nreqt.port = PORT end + if not (nreqt.host and nreqt.host ~= "") then + socket.try(nil, "invalid host '" .. base.tostring(nreqt.host) .. "'") + end -- compute uri if user hasn't overriden nreqt.uri = reqt.uri or adjusturi(nreqt) - -- ajust host and port if there is a proxy - nreqt.host, nreqt.port = adjustproxy(nreqt) -- adjust headers in request nreqt.headers = adjustheaders(nreqt) + -- ajust host and port if there is a proxy + nreqt.host, nreqt.port = adjustproxy(nreqt) return nreqt end local function shouldredirect(reqt, code, headers) - return headers.location and - string.gsub(headers.location, "%s", "") ~= "" and - (reqt.redirect ~= false) and + local location = headers.location + if not location then return false end + location = string.gsub(location, "%s", "") + if location == "" then return false end + local scheme = string.match(location, "^([%w][%w%+%-%.]*)%:") + if scheme and not SCHEMES[scheme] then return false end + return (reqt.redirect ~= false) and (code == 301 or code == 302 or code == 303 or code == 307) and (not reqt.method or reqt.method == "GET" or reqt.method == "HEAD") and (not reqt.nredirects or reqt.nredirects < 5) @@ -270,7 +291,7 @@ end -- forward declarations local trequest, tredirect -function tredirect(reqt, location) +--[[local]] function tredirect(reqt, location) local result, code, headers, status = trequest { -- the RFC says the redirect URL has to be absolute, but some -- servers do not respect that @@ -278,27 +299,27 @@ function tredirect(reqt, location) source = reqt.source, sink = reqt.sink, headers = reqt.headers, - proxy = reqt.proxy, + proxy = reqt.proxy, nredirects = (reqt.nredirects or 0) + 1, create = reqt.create - } + } -- pass location header back as a hint we redirected headers = headers or {} headers.location = headers.location or location return result, code, headers, status end -function trequest(reqt) +--[[local]] function trequest(reqt) -- we loop until we get what we want, or -- until we are sure there is no way to get it local nreqt = adjustrequest(reqt) - local h = open(nreqt.host, nreqt.port, nreqt.create) + local h = _M.open(nreqt.host, nreqt.port, nreqt.create) -- send request line and headers h:sendrequestline(nreqt.method, nreqt.uri) h:sendheaders(nreqt.headers) -- if there is a body, send it if nreqt.source then - h:sendbody(nreqt.headers, nreqt.source, nreqt.step) + h:sendbody(nreqt.headers, nreqt.source, nreqt.step) end local code, status = h:receivestatusline() -- if it is an HTTP/0.9 server, simply get the body and we are done @@ -308,13 +329,13 @@ function trequest(reqt) end local headers -- ignore any 100-continue messages - while code == 100 do + while code == 100 do headers = h:receiveheaders() code, status = h:receivestatusline() end headers = h:receiveheaders() -- at this point we should have a honest reply from the server - -- we can't redirect if we already used the source, so we report the error + -- we can't redirect if we already used the source, so we report the error if shouldredirect(nreqt, code, headers) and not nreqt.source then h:close() return tredirect(reqt, headers.location) @@ -327,11 +348,13 @@ function trequest(reqt) return 1, code, headers, status end -local function srequest(u, b) +-- turns an url and a body into a generic request +local function genericform(u, b) local t = {} local reqt = { url = u, - sink = ltn12.sink.table(t) + sink = ltn12.sink.table(t), + target = t } if b then reqt.source = ltn12.source.string(b) @@ -341,11 +364,21 @@ local function srequest(u, b) } reqt.method = "POST" end - local code, headers, status = socket.skip(1, trequest(reqt)) - return table.concat(t), code, headers, status + return reqt end -request = socket.protect(function(reqt, body) +_M.genericform = genericform + +local function srequest(u, b) + local reqt = genericform(u, b) + local _, code, headers, status = trequest(reqt) + return table.concat(reqt.target), code, headers, status +end + +_M.request = socket.protect(function(reqt, body) if base.type(reqt) == "string" then return srequest(reqt, body) else return trequest(reqt) end end) + +socket.http = _M +return _M |