From 5efe51b66129d5bc902f0a70f0290b94fbd17c84 Mon Sep 17 00:00:00 2001 From: Pahaz Blinov Date: Tue, 8 Nov 2016 02:27:50 +0500 Subject: [PATCH] fix(request.py): problem in case of request without content-type header (#142) * fix(request.py): exception if access request.form on GET request * fix(request): just make a unification (parsed_form and parsed_files) + RFC fixes parsed_form and parsed_files must be a RequestParameters type in all cases! --- sanic/request.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index c2ab7260..109b1483 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -8,6 +8,12 @@ from ujson import loads as json_loads from .log import log +DEFAULT_HTTP_CONTENT_TYPE = "application/octet-stream" +# HTTP/1.1: https://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1 +# > If the media type remains unknown, the recipient SHOULD treat it +# > as type "application/octet-stream" + + class RequestParameters(dict): """ Hosts a dict with lists as values where get returns the first @@ -68,14 +74,13 @@ class Request: @property def form(self): if self.parsed_form is None: - self.parsed_form = {} - self.parsed_files = {} - content_type, parameters = parse_header( - self.headers.get('Content-Type', '')) + self.parsed_form = RequestParameters() + self.parsed_files = RequestParameters() + content_type = self.headers.get( + 'Content-Type', DEFAULT_HTTP_CONTENT_TYPE) + content_type, parameters = parse_header(content_type) try: - is_url_encoded = ( - content_type == 'application/x-www-form-urlencoded') - if content_type is None or is_url_encoded: + if content_type == 'application/x-www-form-urlencoded': self.parsed_form = RequestParameters( parse_qs(self.body.decode('utf-8'))) elif content_type == 'multipart/form-data': @@ -86,7 +91,6 @@ class Request: except Exception as e: log.exception(e) pass - return self.parsed_form @property