mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Unify LuaSocket usage (#7405)
* Add a new socketutil module with a few helper functions that allow us to: * Always use a sane User-Agent (previously, only Wikipedia did so) * Set timeouts in an almost sane manner. Doing it explicitly prevents an interaction with KOSync that does crazy stuff I don't even want to try to understand. * Unified said timeouts based on the request's intended usage (except for Wikipedia, which already had meaningful timeout values). * Stopped using LuaSec directly, LuaSocket defers to LuaSec sanely on its own. Everything now transparently supports HTTPS without code duplication.
This commit is contained in:
@@ -965,25 +965,21 @@ end
|
||||
function ReaderDictionary:downloadDictionary(dict, download_location, continue)
|
||||
continue = continue or false
|
||||
local socket = require("socket")
|
||||
local socketutil = require("socketutil")
|
||||
local http = socket.http
|
||||
local https = require("ssl.https")
|
||||
local ltn12 = require("ltn12")
|
||||
local url = socket.url
|
||||
|
||||
local parsed = url.parse(dict.url)
|
||||
local httpRequest = parsed.scheme == "http" and http.request or https.request
|
||||
|
||||
if not continue then
|
||||
local file_size
|
||||
--local r, c, h = httpRequest {
|
||||
local dummy, headers, dummy = socket.skip(1, httpRequest{
|
||||
method = "HEAD",
|
||||
url = dict.url,
|
||||
-- Skip body & code args
|
||||
socketutil:set_timeout()
|
||||
local headers = socket.skip(2, http.request{
|
||||
method = "HEAD",
|
||||
url = dict.url,
|
||||
--redirect = true,
|
||||
})
|
||||
--logger.dbg(status)
|
||||
socketutil:reset_timeout()
|
||||
--logger.dbg(headers)
|
||||
--logger.dbg(code)
|
||||
file_size = headers and headers["content-length"]
|
||||
|
||||
UIManager:show(ConfirmBox:new{
|
||||
@@ -1004,10 +1000,12 @@ function ReaderDictionary:downloadDictionary(dict, download_location, continue)
|
||||
end)
|
||||
end
|
||||
|
||||
local dummy, c, dummy = httpRequest{
|
||||
url = dict.url,
|
||||
sink = ltn12.sink.file(io.open(download_location, "w")),
|
||||
}
|
||||
socketutil:set_timeout(socketutil.FILE_BLOCK_TIMEOUT, socketutil.FILE_TOTAL_TIMEOUT)
|
||||
local c = socket.skip(1, http.request{
|
||||
url = dict.url,
|
||||
sink = ltn12.sink.file(io.open(download_location, "w")),
|
||||
})
|
||||
socketutil:reset_timeout()
|
||||
if c == 200 then
|
||||
logger.dbg("file downloaded to", download_location)
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user