Skip to content

Commit

Permalink
[chore] NewsDownloader: remove custom redirect handling
Browse files Browse the repository at this point in the history
It's been implemented in LuaSocket http.request() in the meantime.
  • Loading branch information
Frenzie committed Jan 31, 2025
1 parent 8b31f68 commit 48373a4
Showing 1 changed file with 3 additions and 27 deletions.
30 changes: 3 additions & 27 deletions plugins/newsdownloader.koplugin/epubdownloadbackend.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ local EpubDownloadBackend = {
-- accessible here so that caller can know it's a user dismiss.
dismissed_error_code = "Interrupted by user",
}
local max_redirects = 5; --prevent infinite redirects

local FeedCache = CacheSQLite:new{
slots = 500,
Expand Down Expand Up @@ -158,19 +157,13 @@ local function build_cookies(cookies)
end

-- Get URL content
local function getUrlContent(url, cookies, timeout, maxtime, redirectCount, add_to_cache)
logger.dbg("getUrlContent(", url, ",", cookies, ", ", timeout, ",", maxtime, ",", redirectCount, ",", add_to_cache, ")")
if not redirectCount then
redirectCount = 0
elseif redirectCount == max_redirects then
error("EpubDownloadBackend: reached max redirects: ", redirectCount)
end
local function getUrlContent(url, cookies, timeout, maxtime, add_to_cache)
logger.dbg("getUrlContent(", url, ",", cookies, ", ", timeout, ",", maxtime, ",", add_to_cache, ")")

if not timeout then timeout = 10 end
logger.dbg("timeout:", timeout)

local sink = {}
local parsed = socket_url.parse(url)
socketutil:set_timeout(timeout, maxtime or 30)
local request = {
url = url,
Expand Down Expand Up @@ -207,23 +200,6 @@ local function getUrlContent(url, cookies, timeout, maxtime, redirectCount, add_
logger.warn("No HTTP headers:", status or code or "network unreachable")
return false, "Network or remote server unavailable"
end
if not code or code < 200 or code > 299 then -- all 200..299 HTTP codes are OK
if code and code > 299 and code < 400 and headers and headers.location then -- handle 301, 302...
local redirected_url = headers.location
local parsed_redirect_location = socket_url.parse(redirected_url)
if not parsed_redirect_location.host then
parsed_redirect_location.host = parsed.host
parsed_redirect_location.scheme = parsed.scheme
redirected_url = socket_url.build(parsed_redirect_location)
end
logger.dbg("getUrlContent: Redirecting to url: ", redirected_url)
return getUrlContent(redirected_url, timeout, maxtime, redirectCount + 1, add_to_cache)
else
error("EpubDownloadBackend: Don't know how to handle HTTP response status:", status or code)
end
logger.warn("HTTP status not okay:", status or code)
return false, status or code
end
if headers and headers["content-length"] then
-- Check we really got the announced content size
local content_length = tonumber(headers["content-length"])
Expand Down Expand Up @@ -281,7 +257,7 @@ end

function EpubDownloadBackend:getResponseAsString(url, cookies, add_to_cache)
logger.dbg("EpubDownloadBackend:getResponseAsString(", url, ")")
local success, content = getUrlContent(url, cookies, nil, nil, nil, add_to_cache)
local success, content = getUrlContent(url, cookies, nil, nil, add_to_cache)
if (success) then
return content
else
Expand Down

0 comments on commit 48373a4

Please sign in to comment.