From fafb9e61f93e5521bdab3cba702d076acf0d14ae Mon Sep 17 00:00:00 2001 From: PabloCM Date: Thu, 28 Jan 2016 18:22:16 +0100 Subject: [PATCH] [FIX] Crash on some requests without header [IMP] Only load the patch when the module is installed --- log_forwarded_for_ip/__init__.py | 13 ++-------- log_forwarded_for_ip/__openerp__.py | 1 + log_forwarded_for_ip/hooks.py | 12 +++++++++ log_forwarded_for_ip/models/__init__.py | 5 ++++ .../models/log_forwarded_for_ip_installed.py | 26 +++++++++++++++++++ 5 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 log_forwarded_for_ip/hooks.py create mode 100644 log_forwarded_for_ip/models/__init__.py create mode 100644 log_forwarded_for_ip/models/log_forwarded_for_ip_installed.py diff --git a/log_forwarded_for_ip/__init__.py b/log_forwarded_for_ip/__init__.py index 3fd00f7a2..a947032f3 100644 --- a/log_forwarded_for_ip/__init__.py +++ b/log_forwarded_for_ip/__init__.py @@ -2,14 +2,5 @@ # © 2015 Aserti Global Solutions # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from werkzeug.serving import WSGIRequestHandler - - -def address_string(self): - forwarded_for = self.headers.get('X-Forwarded-For', '').split(',') - if forwarded_for and forwarded_for[0]: - return forwarded_for[0] - else: - return self.client_address[0] - -WSGIRequestHandler.address_string = address_string +from . import models +from .hooks import restore_address_string diff --git a/log_forwarded_for_ip/__openerp__.py b/log_forwarded_for_ip/__openerp__.py index 18b02bb4a..f0f811568 100644 --- a/log_forwarded_for_ip/__openerp__.py +++ b/log_forwarded_for_ip/__openerp__.py @@ -14,4 +14,5 @@ "depends": [ "base", ], + "uninstall_hook": "restore_address_string" } diff --git a/log_forwarded_for_ip/hooks.py b/log_forwarded_for_ip/hooks.py new file mode 100644 index 000000000..88cd8c812 --- /dev/null +++ b/log_forwarded_for_ip/hooks.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +# © 2015 Aserti Global Solutions +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from werkzeug.serving import WSGIRequestHandler + + +def restore_address_string(cr, reg): + if hasattr(WSGIRequestHandler, '_address_string_org'): + WSGIRequestHandler.address_string = \ + WSGIRequestHandler._address_string_org + del WSGIRequestHandler._address_string_org diff --git a/log_forwarded_for_ip/models/__init__.py b/log_forwarded_for_ip/models/__init__.py new file mode 100644 index 000000000..5efa39bd8 --- /dev/null +++ b/log_forwarded_for_ip/models/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2015 Aserti Global Solutions +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import log_forwarded_for_ip_installed diff --git a/log_forwarded_for_ip/models/log_forwarded_for_ip_installed.py b/log_forwarded_for_ip/models/log_forwarded_for_ip_installed.py new file mode 100644 index 000000000..c40eb8f49 --- /dev/null +++ b/log_forwarded_for_ip/models/log_forwarded_for_ip_installed.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# © 2015 Aserti Global Solutions +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from werkzeug.serving import WSGIRequestHandler +from mimetools import Message +from openerp import models + + +def address_string(self): + if self.headers and isinstance(self.headers, Message): + forwarded_for = self.headers.get('X-Forwarded-For', '').split(',') + if forwarded_for and forwarded_for[0]: + return forwarded_for[0] + return self.client_address[0] + + +class LogForwardedForIpInstalled(models.AbstractModel): + _name = 'log.forwarded.for.ip.installed' + + def _register_hook(self, cr): + if not hasattr(WSGIRequestHandler, '_address_string_org'): + WSGIRequestHandler._address_string_org = \ + WSGIRequestHandler.address_string + WSGIRequestHandler.address_string = address_string + return super(LogForwardedForIpInstalled, self)._register_hook(cr)