From d26964ab085071f6e52ccd723c16c56fc3ca0454 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 8 Jul 2024 12:46:23 +0200 Subject: [PATCH 1/5] Added log message for missing python magic --- cps/file_helper.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cps/file_helper.py b/cps/file_helper.py index ff0bebea..095b5ee0 100644 --- a/cps/file_helper.py +++ b/cps/file_helper.py @@ -22,15 +22,16 @@ import shutil import zipfile import mimetypes from io import BytesIO -try: - import magic -except ImportError: - pass from . import logger log = logger.create() +try: + import magic +except ImportError as e: + log.error("Cannot import python-magic, checking uploaded file metadata will not work: %s", e) + def get_temp_dir(): tmp_dir = os.path.join(gettempdir(), 'calibre_web') From 551828f8cf8fb9f052d90456a3eb7ebaa8529cc6 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 8 Jul 2024 14:09:53 +0200 Subject: [PATCH 2/5] Added "br" to not sanitized comments tag --- cps/clean_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cps/clean_html.py b/cps/clean_html.py index fc4fe6d1..c687f154 100644 --- a/cps/clean_html.py +++ b/cps/clean_html.py @@ -35,7 +35,7 @@ def clean_string(unsafe_text, book_id=0): try: if bleach: allowed_tags = list(ALLOWED_TAGS) - allowed_tags.extend(["p", "span", "div", "pre", "h1", "h2", "h3", "h4", "h5", "h6"]) + allowed_tags.extend(["p", "span", "div", "pre", "br", "h1", "h2", "h3", "h4", "h5", "h6"]) safe_text = clean_html(unsafe_text, tags=set(allowed_tags)) else: safe_text = clean_html(unsafe_text) From 1d7dcce98dd2116f95a00c6c956cea39a52df748 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 8 Jul 2024 14:42:48 +0200 Subject: [PATCH 3/5] Log Forwarded for address in accesslog instead of client address for gevent --- cps/gevent_wsgi.py | 24 +++++++++++++++++++++++- cps/server.py | 1 - 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cps/gevent_wsgi.py b/cps/gevent_wsgi.py index cd9614c9..ab988401 100644 --- a/cps/gevent_wsgi.py +++ b/cps/gevent_wsgi.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . - +from datetime import datetime from gevent.pywsgi import WSGIHandler @@ -27,4 +27,26 @@ class MyWSGIHandler(WSGIHandler): env['RAW_URI'] = path return env + def format_request(self): + now = datetime.now().replace(microsecond=0) + length = self.response_length or '-' + if self.time_finish: + delta = '%.6f' % (self.time_finish - self.time_start) + else: + delta = '-' + forwarded = self.environ.get('HTTP_X_FORWARDED_FOR', None) + if forwarded: + client_address = forwarded + else: + client_address = self.client_address[0] if isinstance(self.client_address, tuple) else self.client_address + return '%s - - [%s] "%s" %s %s %s' % ( + client_address or '-', + now, + self.requestline or '', + # Use the native string version of the status, saved so we don't have to + # decode. But fallback to the encoded 'status' in case of subclasses + # (Is that really necessary? At least there's no overhead.) + (self._orig_status or self.status or '000').split()[0], + length, + delta) diff --git a/cps/server.py b/cps/server.py index 1fdc048f..dca407a7 100644 --- a/cps/server.py +++ b/cps/server.py @@ -21,7 +21,6 @@ import os import errno import signal import socket -import asyncio try: from gevent.pywsgi import WSGIServer From de989783e8fc53c2a88a82191e0f4b9f525dfdca Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 8 Jul 2024 15:04:53 +0200 Subject: [PATCH 4/5] Log Forwarded for address in accesslog instead of client address for tornado --- cps/tornado_wsgi.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cps/tornado_wsgi.py b/cps/tornado_wsgi.py index c1571ece..f2aae6dd 100644 --- a/cps/tornado_wsgi.py +++ b/cps/tornado_wsgi.py @@ -22,6 +22,7 @@ import tornado from tornado import escape from tornado import httputil from tornado.ioloop import IOLoop +from tornado.log import access_log from typing import List, Tuple, Optional, Callable, Any, Dict, Text from types import TracebackType @@ -96,5 +97,26 @@ class MyWSGIContainer(WSGIContainer): except TypeError as e: environ = WSGIContainer.environ(request) environ['RAW_URI'] = request.path + self.env = environ return environ + def _log(self, status_code: int, request: httputil.HTTPServerRequest) -> None: + if status_code < 400: + log_method = access_log.info + elif status_code < 500: + log_method = access_log.warning + else: + log_method = access_log.error + request_time = 1000.0 * request.request_time() + assert request.method is not None + assert request.uri is not None + ip = self.env.get("HTTP_FORWARD_FOR", None) or request.remote_ip + summary = ( + request.method # type: ignore[operator] + + " " + + request.uri + + " (" + + ip + + ")" + ) + log_method("%d %s %.2fms", status_code, summary, request_time) From 099dbf95e7b6adbd15e03839dd7f5fa601f7cff8 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Wed, 10 Jul 2024 19:59:21 +0200 Subject: [PATCH 5/5] Fix error 500 on sending test email --- cps/helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cps/helper.py b/cps/helper.py index a1d28275..b7d4452f 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -119,9 +119,9 @@ def convert_book_format(book_id, calibre_path, old_book_format, new_book_format, def send_test_mail(ereader_mail, user_name): for email in ereader_mail.split(','): email = email.strip() - WorkerThread.add(user_name, TaskEmail(_('Calibre-Web Test Email', None, None, + WorkerThread.add(user_name, TaskEmail(_('Calibre-Web Test Email'), None, None, config.get_mail_settings(), email, N_("Test Email"), - _('This Email has been sent via Calibre-Web.')))) + _('This Email has been sent via Calibre-Web.'))) return