Decode data by default

This commit is contained in:
Channel Cat 2016-10-02 23:55:46 -07:00
parent 2ad7014666
commit 7cdeff63f6
3 changed files with 47 additions and 47 deletions

42
sanic/request.py Normal file
View File

@ -0,0 +1,42 @@
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

View File

@ -11,9 +11,8 @@ class Router():
self.routes[uri] = handler self.routes[uri] = handler
def get(self, request): def get(self, request):
uri_string = request.url.decode('utf-8') handler = self.routes.get(request.url)
handler = self.routes.get(uri_string)
if handler: if handler:
return handler return handler
else: else:
raise NotFound("Requested URL {} not found".format(uri_string)) raise NotFound("Requested URL {} not found".format(request.url))

View File

@ -22,51 +22,10 @@ from .log import log
from .config import LOGO from .config import LOGO
from .exceptions import ServerError from .exceptions import ServerError
from .response import HTTPResponse from .response import HTTPResponse
from .request import Request
PRINT = 0 PRINT = 0
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(b"?")
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:
self.parsed_args = {k:v if len(v)>1 else v[0] for k,v in parse_qs(self.query_string).items()}
else:
self.parsed_args = {}
return self.parsed_args
class HttpProtocol(asyncio.Protocol): class HttpProtocol(asyncio.Protocol):
__slots__ = ('loop', __slots__ = ('loop',
@ -124,12 +83,12 @@ class HttpProtocol(asyncio.Protocol):
self.url = url self.url = url
def on_header(self, name, value): def on_header(self, name, value):
self.headers.append((name, value)) self.headers.append((name, value.decode('utf-8')))
def on_headers_complete(self): def on_headers_complete(self):
self.request = Request( self.request = Request(
protocol=self, protocol=self,
url=self.url, url=self.url.decode('utf-8'),
headers=dict(self.headers), headers=dict(self.headers),
version=self.parser.get_http_version(), version=self.parser.get_http_version(),
method=self.parser.get_method() method=self.parser.get_method()