diff --git a/requirements.txt b/requirements.txt index 439111e8..9b14856d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -uvloop httptools -ujson \ No newline at end of file +ujson +uvloop \ No newline at end of file diff --git a/sanic/request.py b/sanic/request.py index 574d4d07..c6df56b4 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -1,42 +1,44 @@ -class Request: - __slots__ = ('protocol', 'url', 'headers', 'version', 'method', 'query_string', 'body', 'parsed_json', 'parsed_args') - - def __init__(self, protocol, url, headers, version, method): - self.protocol = protocol - self.url = url - self.headers = headers - self.version = version - self.method = method - - # Capture query string - query_string_position = self.url.find("?") - if query_string_position != -1: - self.query_string = self.url[query_string_position+1:] - self.url = self.url[:query_string_position] - else: - self.query_string = None - - # Init but do not inhale - self.body = None - self.parsed_json = None - self.parsed_args = None - - @property - def json(self): - if not self.parsed_json: - if not self.body: - raise ValueError("No body to parse") - self.parsed_json = json_loads(self.body) - - return self.parsed_json - - @property - def args(self): - if self.parsed_args is None: - if self.query_string: - parsed_query_string = parse_qs(self.query_string).items() - self.parsed_args = {k:[_v.decode('utf-8') for _v in v] if len(v)>1 else v[0].decode('utf-8') for k,v in parsed_query_string} - else: - self.parsed_args = {} - +from urllib.parse import parse_qs + +class Request: + __slots__ = ('protocol', 'url', 'headers', 'version', 'method', 'query_string', 'body', 'parsed_json', 'parsed_args') + + def __init__(self, protocol, url, headers, version, method): + self.protocol = protocol + self.url = url + self.headers = headers + self.version = version + self.method = method + + # Capture query string + query_string_position = self.url.find("?") + if query_string_position != -1: + self.query_string = self.url[query_string_position+1:] + self.url = self.url[:query_string_position] + else: + self.query_string = None + + # Init but do not inhale + self.body = None + self.parsed_json = None + self.parsed_args = None + + @property + def json(self): + if not self.parsed_json: + if not self.body: + raise ValueError("No body to parse") + self.parsed_json = json_loads(self.body) + + return self.parsed_json + + @property + def args(self): + if self.parsed_args is None: + if self.query_string: + parsed_query_string = parse_qs(self.query_string).items() + self.parsed_args = {k:[_v.decode('utf-8') for _v in v] if len(v)>1 else v[0].decode('utf-8') for k,v in parsed_query_string} + else: + self.parsed_args = {} + return self.parsed_args \ No newline at end of file diff --git a/sanic/response.py b/sanic/response.py index 26df2ecd..cb2ac57f 100644 --- a/sanic/response.py +++ b/sanic/response.py @@ -1,4 +1,7 @@ import ujson +import httptools +from ujson import loads as json_loads +from urllib.parse import parse_qs STATUS_CODES = { 200: 'OK', @@ -18,7 +21,7 @@ class HTTPResponse: __slots__ = ('body', 'status', 'content_type') def __init__(self, body='', status=200, content_type='text/plain'): - self.content_type = 'text/plain' + self.content_type = content_type self.body = body self.status = status diff --git a/sanic/server.py b/sanic/server.py index 658cfb76..7fe302e9 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -7,7 +7,6 @@ import httptools import logging from inspect import isawaitable from ujson import loads as json_loads -from urllib.parse import parse_qs from traceback import format_exc import httptools @@ -159,7 +158,6 @@ def abort(msg): log.info(msg, file=sys.stderr) sys.exit(1) - def serve(sanic, host, port, debug=False): # Create Event Loop loop = async_loop.new_event_loop() diff --git a/test.py b/test.py index 0b0073bd..5816a2b4 100644 --- a/test.py +++ b/test.py @@ -8,10 +8,6 @@ app = Sanic("test") async def test(request): return json({ "test": True }) -@app.route("/text") -def test(request): - return text('hi') - @app.route("/exception") def test(request): raise ServerError("yep")