diff --git a/sanic/request.py b/sanic/request.py new file mode 100644 index 00000000..574d4d07 --- /dev/null +++ b/sanic/request.py @@ -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 \ No newline at end of file diff --git a/sanic/router.py b/sanic/router.py index 51abd880..1266ef70 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -11,9 +11,8 @@ class Router(): self.routes[uri] = handler def get(self, request): - uri_string = request.url.decode('utf-8') - handler = self.routes.get(uri_string) + handler = self.routes.get(request.url) if handler: return handler else: - raise NotFound("Requested URL {} not found".format(uri_string)) \ No newline at end of file + raise NotFound("Requested URL {} not found".format(request.url)) \ No newline at end of file diff --git a/sanic/server.py b/sanic/server.py index 0ca92a13..658cfb76 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -22,51 +22,10 @@ from .log import log from .config import LOGO from .exceptions import ServerError from .response import HTTPResponse +from .request import Request 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): __slots__ = ('loop', @@ -124,12 +83,12 @@ class HttpProtocol(asyncio.Protocol): self.url = url def on_header(self, name, value): - self.headers.append((name, value)) + self.headers.append((name, value.decode('utf-8'))) def on_headers_complete(self): self.request = Request( protocol=self, - url=self.url, + url=self.url.decode('utf-8'), headers=dict(self.headers), version=self.parser.get_http_version(), method=self.parser.get_method()