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:
@@ -1,11 +1,9 @@
|
||||
local DocumentRegistry = require("document/documentregistry")
|
||||
local FFIUtil = require("ffi/util")
|
||||
local http = require('socket.http')
|
||||
local https = require('ssl.https')
|
||||
local ltn12 = require('ltn12')
|
||||
local mime = require('mime')
|
||||
local socket = require('socket')
|
||||
local url = require('socket.url')
|
||||
local http = require("socket.http")
|
||||
local ltn12 = require("ltn12")
|
||||
local socket = require("socket")
|
||||
local socketutil = require("socketutil")
|
||||
local util = require("util")
|
||||
local _ = require("gettext")
|
||||
|
||||
@@ -77,23 +75,24 @@ function WebDavApi:listFolder(address, user, pass, folder_path)
|
||||
webdav_url = webdav_url .. "/"
|
||||
end
|
||||
|
||||
local request, sink = {}, {}
|
||||
local parsed = url.parse(webdav_url)
|
||||
local sink = {}
|
||||
local data = [[<?xml version="1.0"?><a:propfind xmlns:a="DAV:"><a:prop><a:resourcetype/></a:prop></a:propfind>]]
|
||||
local auth = string.format("%s:%s", user, pass)
|
||||
local headers = { ["Authorization"] = "Basic " .. mime.b64( auth ),
|
||||
["Content-Type"] = "application/xml",
|
||||
["Depth"] = "1",
|
||||
["Content-Length"] = #data}
|
||||
request["url"] = webdav_url
|
||||
request["method"] = "PROPFIND"
|
||||
request["headers"] = headers
|
||||
request["source"] = ltn12.source.string(data)
|
||||
request["sink"] = ltn12.sink.table(sink)
|
||||
http.TIMEOUT = 5
|
||||
https.TIMEOUT = 5
|
||||
local httpRequest = parsed.scheme == "http" and http.request or https.request
|
||||
local headers_request = socket.skip(1, httpRequest(request))
|
||||
socketutil:set_timeout()
|
||||
local request = {
|
||||
url = webdav_url,
|
||||
method = "PROPFIND",
|
||||
headers = {
|
||||
["Content-Type"] = "application/xml",
|
||||
["Depth"] = "1",
|
||||
["Content-Length"] = #data,
|
||||
},
|
||||
username = user,
|
||||
password = pass,
|
||||
source = ltn12.source.string(data),
|
||||
sink = ltn12.sink.table(sink),
|
||||
}
|
||||
local headers_request = socket.skip(1, http.request(request))
|
||||
socketutil:reset_timeout()
|
||||
if headers_request == nil then
|
||||
return nil
|
||||
end
|
||||
@@ -152,18 +151,15 @@ function WebDavApi:listFolder(address, user, pass, folder_path)
|
||||
end
|
||||
|
||||
function WebDavApi:downloadFile(file_url, user, pass, local_path)
|
||||
local parsed = url.parse(file_url)
|
||||
local auth = string.format("%s:%s", user, pass)
|
||||
local headers = { ["Authorization"] = "Basic " .. mime.b64( auth ) }
|
||||
http.TIMEOUT = 5
|
||||
https.TIMEOUT = 5
|
||||
local httpRequest = parsed.scheme == "http" and http.request or https.request
|
||||
local _, code_return, _ = httpRequest{
|
||||
url = file_url,
|
||||
method = "GET",
|
||||
headers = headers,
|
||||
sink = ltn12.sink.file(io.open(local_path, "w"))
|
||||
}
|
||||
socketutil:set_timeout(socketutil.FILE_BLOCK_TIMEOUT, socketutil.FILE_TOTAL_TIMEOUT)
|
||||
local code_return = socket.skip(1, http.request{
|
||||
url = file_url,
|
||||
method = "GET",
|
||||
sink = ltn12.sink.file(io.open(local_path, "w")),
|
||||
username = user,
|
||||
password = pass,
|
||||
})
|
||||
socketutil:reset_timeout()
|
||||
return code_return
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user