From 46b87dc7eb77e0c8c256ff96b5514b48bd01653e Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Mon, 9 Mar 2020 20:25:59 +0100 Subject: [PATCH] Fix for testability of kobo --- cps/kobo.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cps/kobo.py b/cps/kobo.py index 9830f1c2..f634f272 100644 --- a/cps/kobo.py +++ b/cps/kobo.py @@ -214,9 +214,13 @@ def HandleMetadataRequest(book_uuid): def get_download_url_for_book(book, book_format): if not current_app.wsgi_app.is_proxied: + if ':' in request.host and not request.host.endswith(']') : + host = "".join(request.host.split(':')[:-1]) + else: + host = request.host return "{url_scheme}://{url_base}:{url_port}/download/{book_id}/{book_format}".format( url_scheme=request.scheme, - url_base=request.host, + url_base=host, url_port=config.config_port, book_id=book.id, book_format=book_format.lower() @@ -352,15 +356,17 @@ def reading_state(book): @kobo.route("//image.jpg") @requires_kobo_auth def HandleCoverImageRequest(book_uuid): - log.debug("Cover request received for book %s" % book_uuid) book_cover = helper.get_book_cover_with_uuid( book_uuid, use_generic_cover_on_failure=False ) if not book_cover: if config.config_kobo_proxy: + log.debug("Cover for unknown book: %s proxied to kobo" % book_uuid) return redirect(get_store_url_for_current_request(), 307) else: - abort(404) + log.debug("Cover for unknown book: %s requested" % book_uuid) + return redirect_or_proxy_request() + log.debug("Cover request received for book %s" % book_uuid) return book_cover @@ -456,9 +462,13 @@ def HandleInitRequest(): if not current_app.wsgi_app.is_proxied: log.debug('Kobo: Received unproxied request, changed request port to server port') + if ':' in request.host and not request.host.endswith(']'): + host = "".join(request.host.split(':')[:-1]) + else: + host = request.host calibre_web_url = "{url_scheme}://{url_base}:{url_port}".format( url_scheme=request.scheme, - url_base=request.host, + url_base=host, url_port=config.config_port ) else: