From 9dcc4167e9e9186dbd51aaa68ee9c96500ec85d2 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 20 Aug 2017 12:33:18 +0200 Subject: [PATCH] Make OPDS report an HTTP error if the user-level search expression is not valid. --- src/calibre/srv/errors.py | 6 ++++++ src/calibre/srv/handler.py | 6 ++++-- src/calibre/srv/opds.py | 14 ++++++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/calibre/srv/errors.py b/src/calibre/srv/errors.py index 676a69083c..db251b34cf 100644 --- a/src/calibre/srv/errors.py +++ b/src/calibre/srv/errors.py @@ -58,6 +58,12 @@ class HTTPForbidden(HTTPSimpleResponse): HTTPSimpleResponse.__init__(self, httplib.FORBIDDEN, http_message, close_connection, log=log) +class HTTPInternalServerError(HTTPSimpleResponse): + + def __init__(self, http_message='', close_connection=True, log=None): + HTTPSimpleResponse.__init__(self, httplib.INTERNAL_SERVER_ERROR, http_message, close_connection, log=log) + + class BookNotFound(HTTPNotFound): def __init__(self, book_id, db): diff --git a/src/calibre/srv/handler.py b/src/calibre/srv/handler.py index 12a33e4735..421d313342 100644 --- a/src/calibre/srv/handler.py +++ b/src/calibre/srv/handler.py @@ -118,8 +118,10 @@ class Context(object): raise return frozenset() - def get_categories(self, request_data, db, sort='name', first_letter_sort=True, vl=''): - restrict_to_ids = self.get_effective_book_ids(db, request_data, vl) + def get_categories(self, request_data, db, sort='name', first_letter_sort=True, + vl='', report_parse_errors=False): + restrict_to_ids = self.get_effective_book_ids(db, request_data, vl, + report_parse_errors=report_parse_errors) key = restrict_to_ids, sort, first_letter_sort with self.lock: cache = self.library_broker.category_caches[db.server_library_id] diff --git a/src/calibre/srv/opds.py b/src/calibre/srv/opds.py index 35e3868666..ae14025fea 100644 --- a/src/calibre/srv/opds.py +++ b/src/calibre/srv/opds.py @@ -20,8 +20,9 @@ from calibre.library.comments import comments_to_html from calibre import guess_type, prepare_string_for_xml as xml from calibre.utils.icu import sort_key from calibre.utils.date import as_utc, timestampfromdt, is_date_undefined +from calibre.utils.search_query_parser import ParseException -from calibre.srv.errors import HTTPNotFound +from calibre.srv.errors import HTTPNotFound, HTTPInternalServerError from calibre.srv.routes import endpoint from calibre.srv.utils import get_library_data, http_date, Offsets @@ -381,8 +382,9 @@ class RequestContext(object): def last_modified(self): return self.db.last_modified() - def get_categories(self): - return self.ctx.get_categories(self.rd, self.db) + def get_categories(self, report_parse_errors=False): + return self.ctx.get_categories(self.rd, self.db, + report_parse_errors=report_parse_errors) def search(self, query): return self.ctx.search(self.rd, self.db, query) @@ -470,7 +472,11 @@ def get_navcatalog(request_context, which, page_url, up_url, offset=0): def opds(ctx, rd): rc = RequestContext(ctx, rd) db = rc.db - categories = rc.get_categories() + try: + categories = rc.get_categories(report_parse_errors=True) + except ParseException as p: + raise HTTPInternalServerError(p.msg) + category_meta = db.field_metadata cats = [ (_('Newest'), _('Date'), 'Onewest'),