From 1bd1ed4e5a1e5f0ee11fb10785421fbc3696e4a2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 5 Dec 2022 18:45:56 +0530 Subject: [PATCH] Content server: Redirect the index page to always have trailing slash when using URL prefixes. Fixes #1998767 [Calibre content server prefix not used for mobile link](https://bugs.launchpad.net/calibre/+bug/1998767) --- src/calibre/srv/code.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/calibre/srv/code.py b/src/calibre/srv/code.py index 1dc8057e92..0a93b525db 100644 --- a/src/calibre/srv/code.py +++ b/src/calibre/srv/code.py @@ -16,17 +16,17 @@ from calibre.customize.ui import available_input_formats from calibre.db.view import sanitize_sort_field_name from calibre.srv.ajax import search_result from calibre.srv.errors import ( - BookNotFound, HTTPBadRequest, HTTPForbidden, HTTPNotFound + BookNotFound, HTTPBadRequest, HTTPForbidden, HTTPNotFound, HTTPRedirect, ) from calibre.srv.metadata import ( - book_as_json, categories_as_json, categories_settings, icon_map + book_as_json, categories_as_json, categories_settings, icon_map, ) from calibre.srv.routes import endpoint, json from calibre.srv.utils import get_library_data, get_use_roman from calibre.utils.config import prefs, tweaks from calibre.utils.icu import numeric_sort_key, sort_key from calibre.utils.localization import ( - get_lang, lang_map_for_ui, localize_website_link, lang_code_for_user_manual + get_lang, lang_code_for_user_manual, lang_map_for_ui, localize_website_link, ) from calibre.utils.search_query_parser import ParseException from calibre.utils.serialize import json_dumps @@ -37,6 +37,14 @@ POSTABLE = frozenset({'GET', 'POST', 'HEAD'}) @endpoint('', auth_required=True) # auth_required=True needed for Chrome: https://bugs.launchpad.net/calibre/+bug/1982060 def index(ctx, rd): + if rd.opts.url_prefix and rd.request_original_uri: + # We need a trailing slash for relative URLs to resolve correctly, for + # example the link to the mobile page in index.html + from urllib.parse import urlparse, urlunparse + p = urlparse(rd.request_original_uri) + if not p.path.endswith(b'/'): + p = p._replace(path=p.path + b'/') + raise HTTPRedirect(urlunparse(p).decode('utf-8')) ans_file = lopen(P('content-server/index-generated.html'), 'rb') if not in_develop_mode: return ans_file