1
0
mirror of https://github.com/janeczku/calibre-web synced 2025-04-05 10:17:00 +00:00

Better error handling during proxying kobo requests (fix for #3334)

This commit is contained in:
Ozzie Isaacs 2025-03-23 07:56:14 +01:00
parent efc7ce31d2
commit 46f111cd6e

View File

@ -106,24 +106,29 @@ def make_request_to_kobo_store(sync_token=None):
return store_response
def redirect_or_proxy_request():
def redirect_or_proxy_request(auth=False):
if config.config_kobo_proxy:
if request.method == "GET":
return redirect(get_store_url_for_current_request(), 307)
else:
# The Kobo device turns other request types into GET requests on redirects,
# so we instead proxy to the Kobo store ourselves.
store_response = make_request_to_kobo_store()
try:
if request.method == "GET":
alfa = redirect(get_store_url_for_current_request(), 307)
return alfa
else:
# The Kobo device turns other request types into GET requests on redirects,
# so we instead proxy to the Kobo store ourselves.
store_response = make_request_to_kobo_store()
response_headers = store_response.headers
for header_key in CONNECTION_SPECIFIC_HEADERS:
response_headers.pop(header_key, default=None)
response_headers = store_response.headers
for header_key in CONNECTION_SPECIFIC_HEADERS:
response_headers.pop(header_key, default=None)
return make_response(
store_response.content, store_response.status_code, response_headers.items()
)
else:
return make_response(jsonify({}))
return make_response(
store_response.content, store_response.status_code, response_headers.items()
)
except Exception as e:
log.error("Failed to receive or parse response from Kobo's endpoint: {}".format(e))
if auth:
return make_calibre_web_auth_response()
return make_response(jsonify({}))
def convert_to_kobo_timestamp_string(timestamp):
@ -1042,7 +1047,7 @@ def HandleAuthRequest():
log.debug('Kobo Auth request')
if config.config_kobo_proxy:
try:
return redirect_or_proxy_request()
return redirect_or_proxy_request(auth=True)
except Exception:
log.error("Failed to receive or parse response from Kobo's auth endpoint. Falling back to un-proxied mode.")
return make_calibre_web_auth_response()