Changeset - 6b1ed75d7ac9
[Not reviewed]
0 1 0
Ben Sturmfels (bsturmfels) - 2 years ago 2022-03-14 23:59:47
ben@sturm.com.au
Handle UnicodeEncodeErrors from junk URLs.
1 file changed with 6 insertions and 1 deletions:
0 comments (0 inline, 0 general)
www/conservancy/static/views.py
Show inline comments
...
 
@@ -25,25 +25,30 @@ def handler403(request):
 

	
 
def handler404(request):
 
    return handler(request, 404)
 

	
 
def handler500(request):
 
    return handler(request, 500)
 

	
 
def index(request, *args, **kwargs):
 
    path = request.path.lstrip(u'/')
 
    if path.endswith(u'/'):
 
        path += u'index.html'
 
    fullpath = os.path.join(STATIC_ROOT, path)
 
    if not os.path.exists(fullpath):
 
    try:
 
        # Junk URLs in production (Python 3.5) are causing UnicodeEncodeErrors
 
        # here. Can't reproduce in development in Python 3.9 - only Python 2.7.
 
        if not os.path.exists(fullpath):
 
            return handler404(request)
 
    except UnicodeEncodeError:
 
        return handler404(request)
 
    content_type, _ = mimetypes.guess_type(path)
 
    if content_type != 'text/html':
 
        return HttpResponse(open(fullpath, 'rb'), content_type)
 
    else:
 
        context = kwargs.copy()
 
        try:
 
            context['fundgoal'] = fundgoal_lookup(kwargs['fundraiser_sought'])
 
        except KeyError:
 
            pass
 
        return TemplateResponse(request, path, context)
 

	
0 comments (0 inline, 0 general)