From 9a8bf5754ff6012de96aafe5a8d9bcbaa1890fce Mon Sep 17 00:00:00 2001 From: Mark Stuart Date: Tue, 7 Apr 2020 00:35:21 +1200 Subject: [PATCH 1/2] Updated middleware Providers to take Sender config --- python2/raygun4py/middleware/flask.py | 5 +++-- python2/raygun4py/middleware/wsgi.py | 4 ++-- python3/raygun4py/middleware/flask.py | 5 +++-- python3/raygun4py/middleware/wsgi.py | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/python2/raygun4py/middleware/flask.py b/python2/raygun4py/middleware/flask.py index 36f19fb..676278e 100644 --- a/python2/raygun4py/middleware/flask.py +++ b/python2/raygun4py/middleware/flask.py @@ -11,9 +11,10 @@ class Provider(object): - def __init__(self, flaskApp, apiKey): + def __init__(self, flaskApp, apiKey, config=None): self.flaskApp = flaskApp self.apiKey = apiKey + self.config = config self.sender = None got_request_exception.connect(self.send_exception, sender=flaskApp) @@ -24,7 +25,7 @@ def attach(self): if not hasattr(self.flaskApp, 'extensions'): self.flaskApp.extensions = {} - self.sender = raygunprovider.RaygunSender(self.apiKey) + self.sender = raygunprovider.RaygunSender(self.apiKey, config=self.config) return self.sender def send_exception(self, *args, **kwargs): diff --git a/python2/raygun4py/middleware/wsgi.py b/python2/raygun4py/middleware/wsgi.py index 6fd201d..61ddee7 100644 --- a/python2/raygun4py/middleware/wsgi.py +++ b/python2/raygun4py/middleware/wsgi.py @@ -8,9 +8,9 @@ class Provider(object): - def __init__(self, app, apiKey): + def __init__(self, app, apiKey, config=None): self.app = app - self.sender = raygunprovider.RaygunSender(apiKey) + self.sender = raygunprovider.RaygunSender(apiKey, config=config) def __call__(self, environ, start_response): if not self.sender: diff --git a/python3/raygun4py/middleware/flask.py b/python3/raygun4py/middleware/flask.py index 11fdae4..f859829 100644 --- a/python3/raygun4py/middleware/flask.py +++ b/python3/raygun4py/middleware/flask.py @@ -7,9 +7,10 @@ class Provider(object): - def __init__(self, flaskApp, apiKey): + def __init__(self, flaskApp, apiKey, config=None): self.flaskApp = flaskApp self.apiKey = apiKey + self.config = config self.sender = None got_request_exception.connect(self.send_exception, sender=flaskApp) @@ -20,7 +21,7 @@ def attach(self): if not hasattr(self.flaskApp, 'extensions'): self.flaskApp.extensions = {} - self.sender = raygunprovider.RaygunSender(self.apiKey) + self.sender = raygunprovider.RaygunSender(self.apiKey, config=self.config) return self.sender def send_exception(self, *args, **kwargs): diff --git a/python3/raygun4py/middleware/wsgi.py b/python3/raygun4py/middleware/wsgi.py index 6fd201d..61ddee7 100644 --- a/python3/raygun4py/middleware/wsgi.py +++ b/python3/raygun4py/middleware/wsgi.py @@ -8,9 +8,9 @@ class Provider(object): - def __init__(self, app, apiKey): + def __init__(self, app, apiKey, config=None): self.app = app - self.sender = raygunprovider.RaygunSender(apiKey) + self.sender = raygunprovider.RaygunSender(apiKey, config=config) def __call__(self, environ, start_response): if not self.sender: From 2d0e4fd53767589803f0598f28d1bb83ebc2f20a Mon Sep 17 00:00:00 2001 From: Mark Stuart Date: Tue, 7 Apr 2020 16:53:56 +1200 Subject: [PATCH 2/2] Fix int conversion errors when CONTENT_LENGTH is '' Earlier versions of Werkzerg created a default of '' for the CONTENT_LENGTH environ var if the client did not provide the header. See https://github.com/pallets/werkzeug/pull/1056 --- python2/raygun4py/http_utilities.py | 16 +++++++++------- python3/raygun4py/http_utilities.py | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/python2/raygun4py/http_utilities.py b/python2/raygun4py/http_utilities.py index bd29a4a..9e5f8ad 100644 --- a/python2/raygun4py/http_utilities.py +++ b/python2/raygun4py/http_utilities.py @@ -22,9 +22,9 @@ def build_wsgi_compliant_request(request): # fallback to wsgi.input if http_form is None and 'wsgi.input' in request: # we can assume WSGI keys inside this block - content_length = int(request.get('CONTENT_LENGTH', 0)) - if content_length: - http_form = request['wsgi.input'].read(content_length) + content_length = request.get('CONTENT_LENGTH', 0) + if content_length and content_length != '': + http_form = request['wsgi.input'].read(int(content_length)) rg_request = { 'httpMethod': (request.get('httpMethod') or request.get('REQUEST_METHOD')), @@ -43,10 +43,12 @@ def build_wsgi_compliant_request(request): _headers = request.get('headers') if _headers is None: # manually try to build up headers given known WSGI keys - _headers = { - 'Content-Type': request.get('CONTENT_TYPE'), - 'Content-Length': request.get('CONTENT_LENGTH'), - } + _headers = {} + # don't add content headers if they are empty strings, Werkzeug has strange defaults + if request.get('CONTENT_TYPE') != '': + _headers['Content-Type'] = request.get('CONTENT_TYPE') + if request.get('CONTENT_LENGTH') != '': + _headers['Content-Length'] = request.get('CONTENT_LENGTH') for key, value in request.items(): if key.startswith('HTTP_'): diff --git a/python3/raygun4py/http_utilities.py b/python3/raygun4py/http_utilities.py index bd29a4a..9e5f8ad 100644 --- a/python3/raygun4py/http_utilities.py +++ b/python3/raygun4py/http_utilities.py @@ -22,9 +22,9 @@ def build_wsgi_compliant_request(request): # fallback to wsgi.input if http_form is None and 'wsgi.input' in request: # we can assume WSGI keys inside this block - content_length = int(request.get('CONTENT_LENGTH', 0)) - if content_length: - http_form = request['wsgi.input'].read(content_length) + content_length = request.get('CONTENT_LENGTH', 0) + if content_length and content_length != '': + http_form = request['wsgi.input'].read(int(content_length)) rg_request = { 'httpMethod': (request.get('httpMethod') or request.get('REQUEST_METHOD')), @@ -43,10 +43,12 @@ def build_wsgi_compliant_request(request): _headers = request.get('headers') if _headers is None: # manually try to build up headers given known WSGI keys - _headers = { - 'Content-Type': request.get('CONTENT_TYPE'), - 'Content-Length': request.get('CONTENT_LENGTH'), - } + _headers = {} + # don't add content headers if they are empty strings, Werkzeug has strange defaults + if request.get('CONTENT_TYPE') != '': + _headers['Content-Type'] = request.get('CONTENT_TYPE') + if request.get('CONTENT_LENGTH') != '': + _headers['Content-Length'] = request.get('CONTENT_LENGTH') for key, value in request.items(): if key.startswith('HTTP_'):