Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated middleware Providers to take Sender config, fixed wsgi CONTENT_LENGTH error #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions python2/raygun4py/http_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 != '':
Copy link
Author

@markstuart markstuart Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some wsgi servers set the CONTENT_LENGTH to '' if the client did not provide the header. This was happening in my case and the result was that the attempt to cast '' to an int was raising an exception, and the try:except was handily swallowing the error.

The result was that rg_request never got instantiated and was failing later on when trying to set rg_request['headers'] = _headers.

I think this change should be compatible with the intent of what was there previously. It does however mean that the http_form would not get created if request.get('CONTENT_LENGTH') == ''

http_form = request['wsgi.input'].read(int(content_length))

rg_request = {
'httpMethod': (request.get('httpMethod') or request.get('REQUEST_METHOD')),
Expand All @@ -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_'):
Expand Down
5 changes: 3 additions & 2 deletions python2/raygun4py/middleware/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions python2/raygun4py/middleware/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 9 additions & 7 deletions python3/raygun4py/http_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand All @@ -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_'):
Expand Down
5 changes: 3 additions & 2 deletions python3/raygun4py/middleware/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions python3/raygun4py/middleware/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down