Browse Source

[FIX] Crash on some requests without header

[IMP] Only load the patch when the module is installed
pull/344/head
PabloCM 9 years ago
parent
commit
fafb9e61f9
  1. 13
      log_forwarded_for_ip/__init__.py
  2. 1
      log_forwarded_for_ip/__openerp__.py
  3. 12
      log_forwarded_for_ip/hooks.py
  4. 5
      log_forwarded_for_ip/models/__init__.py
  5. 26
      log_forwarded_for_ip/models/log_forwarded_for_ip_installed.py

13
log_forwarded_for_ip/__init__.py

@ -2,14 +2,5 @@
# © 2015 Aserti Global Solutions # © 2015 Aserti Global Solutions
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # 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

1
log_forwarded_for_ip/__openerp__.py

@ -14,4 +14,5 @@
"depends": [ "depends": [
"base", "base",
], ],
"uninstall_hook": "restore_address_string"
} }

12
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

5
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

26
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)
Loading…
Cancel
Save