diff --git a/frontend/apps/cloudstorage/syncservice.lua b/frontend/apps/cloudstorage/syncservice.lua index 754280096..82a5466ae 100644 --- a/frontend/apps/cloudstorage/syncservice.lua +++ b/frontend/apps/cloudstorage/syncservice.lua @@ -151,12 +151,17 @@ function SyncService.sync(server, file_path, sync_cb, is_silent) end local code_response = 412 -- If-Match header failed local etag + -- Bound the If-Match retry loop so a persistently failing 412 (e.g. a server + -- that only ever returns a weak ETag which can't satisfy If-Match) can't hang + -- the app indefinitely; give up after a few tries and report failure. + local max_tries, tries = 5, 0 local api = server.type == "dropbox" and require("apps/cloudstorage/dropboxapi") or require("apps/cloudstorage/webdavapi") local token = server.password if server.type == "dropbox" and not (server.address == nil or server.address == "") then token = api:getAccessToken(server.password, server.address) end - while code_response == 412 do + while code_response == 412 and tries < max_tries do + tries = tries + 1 os.remove(income_file_path) if server.type == "dropbox" then local url_base = server.url:sub(-1) == "/" and server.url or server.url.."/" diff --git a/frontend/apps/cloudstorage/webdavapi.lua b/frontend/apps/cloudstorage/webdavapi.lua index ec5022163..c200e92b4 100644 --- a/frontend/apps/cloudstorage/webdavapi.lua +++ b/frontend/apps/cloudstorage/webdavapi.lua @@ -175,6 +175,12 @@ end function WebDavApi:uploadFile(file_url, user, pass, local_path, etag) socketutil:set_timeout(socketutil.FILE_BLOCK_TIMEOUT, socketutil.FILE_TOTAL_TIMEOUT) + -- If-Match uses strong comparison (RFC 7232 §3.1), so a weak validator + -- (W/"…", returned e.g. for gzip-compressed responses) can never match and + -- would 412 forever. Strip the weak prefix; proxies keep the same value. + if type(etag) == "string" then + etag = etag:gsub("^%s*[Ww]/", "") + end local code, _, status = socket.skip(1, http.request{ url = file_url, method = "PUT", diff --git a/plugins/cloudstorage.koplugin/providers/webdav.lua b/plugins/cloudstorage.koplugin/providers/webdav.lua index e54b3159e..d8790cb3b 100644 --- a/plugins/cloudstorage.koplugin/providers/webdav.lua +++ b/plugins/cloudstorage.koplugin/providers/webdav.lua @@ -163,6 +163,12 @@ end function WebDavApi.uploadFile(file_url, user, pass, local_path, etag) socketutil:set_timeout(socketutil.FILE_BLOCK_TIMEOUT, socketutil.FILE_TOTAL_TIMEOUT) + -- If-Match uses strong comparison (RFC 7232 §3.1), so a weak validator + -- (W/"…", returned e.g. for gzip-compressed responses) can never match and + -- would 412 forever. Strip the weak prefix; proxies keep the same value. + if type(etag) == "string" then + etag = etag:gsub("^%s*[Ww]/", "") + end local code, _, status = socket.skip(1, http.request{ url = file_url, method = "PUT",