Performance improvements to url parsing

This commit is contained in:
Channel Cat 2016-10-08 16:04:18 -07:00
parent 6041e7cfa6
commit fa980b1445
2 changed files with 14 additions and 17 deletions

View File

@ -1,23 +1,17 @@
from httptools import parse_url
from urllib.parse import parse_qs
from ujson import loads as json_loads
class Request:
__slots__ = ('protocol', 'url', 'headers', 'version', 'method', 'query_string', 'body', 'parsed_json', 'parsed_args')
__slots__ = ('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
def __init__(self, url_bytes, headers, version, method):
url_parsed = parse_url(url_bytes)
self.url = url_parsed.path.decode('utf-8')
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
self.query_string = url_parsed.query.decode('utf-8') if url_parsed.query else None
# Init but do not inhale
self.body = None

View File

@ -23,11 +23,13 @@ from .exceptions import ServerError
from .response import HTTPResponse
from .request import Request
class HttpProtocol(asyncio.Protocol):
__slots__ = ('loop',
'transport', 'request', 'parser',
'url', 'headers', 'sanic')
'url', 'headers', 'sanic',
'_total_body_size')
def __init__(self, *, sanic, loop):
self.loop = loop
@ -44,7 +46,7 @@ class HttpProtocol(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
#TODO: handle keep-alive/connection timeout
#TODO: handle connection timeout
# TCP Nodelay
# I have no evidence to support this makes anything faster
@ -64,6 +66,8 @@ class HttpProtocol(asyncio.Protocol):
# -------------------------------------------- #
def data_received(self, data):
#TODO: handle body too large
if self.parser is None:
assert self.request is None
self.headers = []
@ -84,11 +88,10 @@ class HttpProtocol(asyncio.Protocol):
def on_headers_complete(self):
self.request = Request(
protocol=self,
url=self.url.decode('utf-8'),
url_bytes=self.url,
headers=dict(self.headers),
version=self.parser.get_http_version(),
method=self.parser.get_method().decode('utf-8')
method=self.parser.get_method().decode()
)
#print("res {} - {}".format(n, self.request))