[fix] ODPS: socket.http redirect, where the target is a different host (#4420)

Split https/http code-path (as socket.http can adjust the Authorization/Host headers automatically)
Warn the user when the protocol differs from http/https
pull/4427/head
endofline 5 years ago committed by Frans de Jonge
parent 8e575a5f3b
commit 295cc55e03

@ -473,15 +473,33 @@ function OPDSBrowser:downloadFile(item, format, remote_url)
UIManager:scheduleIn(1, function()
logger.dbg("downloading file", local_path, "from", remote_url)
local parsed = url.parse(remote_url)
local hostname = parsed.host
http.TIMEOUT, https.TIMEOUT = 20, 20
local httpRequest = parsed.scheme == 'http' and http.request or https.request
local auth = string.format("%s:%s", item.username, item.password)
local __, c = httpRequest {
url = remote_url,
headers = { Authorization = "Basic " .. mime.b64(auth), ["Host"] = hostname, },
sink = ltn12.sink.file(io.open(local_path, "w")),
}
local dummy, c = nil
if parsed.scheme == "http" then
dummy, c = http.request {
url = remote_url,
sink = ltn12.sink.file(io.open(local_path, "w")),
user = item.username,
password = item.password
}
elseif parsed.scheme == "https" then
local auth = string.format("%s:%s", item.username, item.password)
local hostname = parsed.host
dummy, c = https.request {
url = remote_url,
headers = { Authorization = "Basic " .. mime.b64(auth), ["Host"] = hostname },
sink = ltn12.sink.file(io.open(local_path, "w")),
}
else
UIManager:show(InfoMessage:new {
text = T(_("Invalid protocol:\n%1"), parsed.scheme),
timeout = 3,
})
end
if c == 200 then
logger.dbg("file downloaded to", local_path)
if self.file_downloaded_callback then

Loading…
Cancel
Save