From d4ed388021fda732cb08d95844949457fad9631a Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 7 Feb 2011 19:59:37 -0200 Subject: allow http_request to follow redirects hopping from http (LuaSocket) to https (LuaSec) and vice-versa. --- src/luarocks/fs/lua.lua | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index d9e717c1..72e7b022 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -479,8 +479,12 @@ if socket_ok then local ltn12 = require("ltn12") local luasec_ok, https = pcall(require, "ssl.https") +local redirect_protocols = { + http = http, + https = luasec_ok and https, +} -local function http_request(url, http) +local function http_request(url, http, loop_control) local proxy = cfg.proxy local url_arg, proxy_result if proxy then @@ -493,6 +497,23 @@ local function http_request(url, http) local content, err if not res then err = status + elseif status == 301 or status == 302 then + local location = headers.location + if location then + local protocol, rest = dir.split_url(location) + if redirect_protocols[protocol] then + if not loop_control then + loop_control = {} + elseif loop_control[location] then + return nil, "Redirection loop -- broken URL?" + end + loop_control[url] = true + return http_request(location, redirect_protocols[protocol], loop_control) + else + return nil, "URL redirected to unsupported protocol - install luasec to get HTTPS support." + end + end + err = line elseif status ~= 200 then err = line else -- cgit v1.2.3-55-g6feb