From c415e7c52ac236e605e0477e63228a2b8b82fab3 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Sat, 12 Mar 2022 17:12:17 +0000 Subject: [PATCH] Move EDDNWSGIHandler into its own file --- src/eddn/Gateway.py | 51 +---------------------------- src/eddn/core/EDDNWSGIHandler.py | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 50 deletions(-) create mode 100644 src/eddn/core/EDDNWSGIHandler.py diff --git a/src/eddn/Gateway.py b/src/eddn/Gateway.py index 95387c5..525f5c2 100644 --- a/src/eddn/Gateway.py +++ b/src/eddn/Gateway.py @@ -14,7 +14,6 @@ from typing import Dict from urllib.parse import parse_qs import gevent -import gevent.pywsgi import simplejson import zmq.green as zmq from gevent import monkey @@ -51,6 +50,7 @@ validator = Validator() # This import must be done post-monkey-patching! from eddn.core.StatsCollector import StatsCollector # noqa: E402 +from eddn.core.EDDNWSGIHandler import EDDNWSGIHandler stats_collector = StatsCollector() stats_collector.start() @@ -425,55 +425,6 @@ def apply_cors() -> None: ) -class EDDNWSGIHandler(gevent.pywsgi.WSGIHandler): - """iHandles overriding request logging.""" - - def format_request(self): - """ - Format information about the request for logging. - - The default causes, e.g.: - - - - [2022-03-12 16:44:39] "POST /upload/ HTTP/1.1" 400 399 0.000566 - - and makes no attempt to handle reverse proxying where we know that - X-Forwarded-For has been set by our reverse proxy. So this will use - that header if present to output the correct IP. - - Also, as we're pointing output at our logger, there is no need to - include a timestamp in the output. - - This is why we're overriding *this* and not `handle_one_response()`, - where we'd change self.client_address there instead. This does, - unfortunately, mean re-creating most of the super-class'es version - of the function. - - Resulting output: - - 2022-03-12 16:44:39.132 - INFO - pywsgi:1226: - - "POST /upload/ HTTP/1.1" 400 399 0.000566 - """ - # Start with the same as the super-class would use... - client_address = self.client_address[0] if isinstance(self.client_address, tuple) else self.client_address - - # ... but now over-ride it if the header is set. - if self.environ.get("HTTP_X_FORWARDED_FOR"): - client_address = self.environ["HTTP_X_FORWARDED_FOR"] - - length = self.response_length or '-' - - if self.time_finish: - d = self.time_finish - self.time_start - delta = f"{d:6f}" - - else: - delta = '-' - - # This differs from the super-class version in not having a datestamp - return f"{client_address or '-'} - - \"{self.requestline or '-'}\"" \ - f" {(self._orig_status or self.status or '000').split()[0]}" \ - f" {length} {delta}" - - def main() -> None: """Handle setting up and running the bottle app.""" cl_args = parse_cl_args() diff --git a/src/eddn/core/EDDNWSGIHandler.py b/src/eddn/core/EDDNWSGIHandler.py new file mode 100644 index 0000000..4f45357 --- /dev/null +++ b/src/eddn/core/EDDNWSGIHandler.py @@ -0,0 +1,56 @@ +""" +Sub-class of main gevent.pywsgi.WSGIHandler. + +Necessary in order to override some behaviour. +""" +import gevent +import gevent.pywsgi + + +class EDDNWSGIHandler(gevent.pywsgi.WSGIHandler): + """Handles overriding request logging.""" + + def format_request(self) -> str: + """ + Format information about the request for logging. + + The default causes, e.g.: + + - - [2022-03-12 16:44:39] "POST /upload/ HTTP/1.1" 400 399 0.000566 + + and makes no attempt to handle reverse proxying where we know that + X-Forwarded-For has been set by our reverse proxy. So this will use + that header if present to output the correct IP. + + Also, as we're pointing output at our logger, there is no need to + include a timestamp in the output. + + This is why we're overriding *this* and not `handle_one_response()`, + where we'd change self.client_address there instead. This does, + unfortunately, mean re-creating most of the super-class'es version + of the function. + + Resulting output: + + 2022-03-12 16:44:39.132 - INFO - pywsgi:1226: - - "POST /upload/ HTTP/1.1" 400 399 0.000566 + """ + # Start with the same as the super-class would use... + client_address = self.client_address[0] if isinstance(self.client_address, tuple) else self.client_address + + # ... but now over-ride it if the header is set. + if self.environ.get("HTTP_X_FORWARDED_FOR"): + client_address = self.environ["HTTP_X_FORWARDED_FOR"] + + length = self.response_length or '-' + + if self.time_finish: + d = self.time_finish - self.time_start + delta = f"{d:6f}" + + else: + delta = '-' + + # This differs from the super-class version in not having a datestamp + return f"{client_address or '-'} - - \"{self.requestline or '-'}\"" \ + f" {(self._orig_status or self.status or '000').split()[0]}" \ + f" {length} {delta}"