From 266dc608f2b90c8968485a5d40f5f77de2532e28 Mon Sep 17 00:00:00 2001 From: Unrud Date: Tue, 7 Mar 2017 18:18:37 +0100 Subject: [PATCH 1/4] Remove unnecessary if-statements --- radicale/__init__.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index 695439d9..04320423 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -310,20 +310,16 @@ class Application: return [answer] if answer else [] remote_host = "UNKNOWN" - if "REMOTE_HOST" in environ: - if environ["REMOTE_HOST"]: - remote_host = environ["REMOTE_HOST"] - if "HTTP_X_FORWARDED_FOR" in environ: - if environ["HTTP_X_FORWARDED_FOR"]: - remote_host = environ["HTTP_X_FORWARDED_FOR"] + if environ.get("REMOTE_HOST"): + remote_host = environ["REMOTE_HOST"] + if environ.get("HTTP_X_FORWARDED_FOR"): + remote_host = environ["HTTP_X_FORWARDED_FOR"] remote_useragent = "[-no-user-agent-provided-]" - if "HTTP_USER_AGENT" in environ: - if environ["HTTP_USER_AGENT"]: - remote_useragent = environ["HTTP_USER_AGENT"] + if environ.get("HTTP_USER_AGENT"): + remote_useragent = environ["HTTP_USER_AGENT"] depthinfo = "" - if "HTTP_DEPTH" in environ: - if environ["HTTP_DEPTH"]: - depthinfo = " with depth " + environ["HTTP_DEPTH"] + if environ.get("HTTP_DEPTH"): + depthinfo = " with depth " + environ["HTTP_DEPTH"] time_begin = datetime.datetime.now() self.logger.info( "%s request for %s received from %s using \"%s\"", From c104da28ce80bfc42390a0fb69402f30f874ae61 Mon Sep 17 00:00:00 2001 From: Unrud Date: Tue, 7 Mar 2017 18:19:41 +0100 Subject: [PATCH 2/4] Use UNKNOWN if user agent is missing This is much shorter and doesn't clutter the log as much. --- radicale/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index 04320423..d8e9984a 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -314,7 +314,7 @@ class Application: remote_host = environ["REMOTE_HOST"] if environ.get("HTTP_X_FORWARDED_FOR"): remote_host = environ["HTTP_X_FORWARDED_FOR"] - remote_useragent = "[-no-user-agent-provided-]" + remote_useragent = "UNKNOWN" if environ.get("HTTP_USER_AGENT"): remote_useragent = environ["HTTP_USER_AGENT"] depthinfo = "" From 69d39b47ca1201bf26c9717fbf30b01cc6e70c4b Mon Sep 17 00:00:00 2001 From: Unrud Date: Tue, 7 Mar 2017 18:21:27 +0100 Subject: [PATCH 3/4] Also log the forwarding host for forwarded requests --- radicale/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/radicale/__init__.py b/radicale/__init__.py index d8e9984a..cacafccd 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -313,7 +313,8 @@ class Application: if environ.get("REMOTE_HOST"): remote_host = environ["REMOTE_HOST"] if environ.get("HTTP_X_FORWARDED_FOR"): - remote_host = environ["HTTP_X_FORWARDED_FOR"] + remote_host = "%s (forwarded by %s)" % ( + environ["HTTP_X_FORWARDED_FOR"], remote_host) remote_useragent = "UNKNOWN" if environ.get("HTTP_USER_AGENT"): remote_useragent = environ["HTTP_USER_AGENT"] From 5cd5cfe36800452dffe5686bd5b7b4e68924edae Mon Sep 17 00:00:00 2001 From: Unrud Date: Tue, 7 Mar 2017 18:22:17 +0100 Subject: [PATCH 4/4] Use REMOTE_ADDR if REMOTE_HOST is missing WSGIRequestHandler doesn't set REMOTE_HOST if dns lookup is disabled. --- radicale/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/radicale/__init__.py b/radicale/__init__.py index cacafccd..d188fbb7 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -312,6 +312,8 @@ class Application: remote_host = "UNKNOWN" if environ.get("REMOTE_HOST"): remote_host = environ["REMOTE_HOST"] + elif environ.get("REMOTE_ADDR"): + remote_host = environ["REMOTE_ADDR"] if environ.get("HTTP_X_FORWARDED_FOR"): remote_host = "%s (forwarded by %s)" % ( environ["HTTP_X_FORWARDED_FOR"], remote_host)