Performance improvements to url parsing
This commit is contained in:
parent
6041e7cfa6
commit
fa980b1445
|
@ -1,23 +1,17 @@
|
||||||
|
from httptools import parse_url
|
||||||
from urllib.parse import parse_qs
|
from urllib.parse import parse_qs
|
||||||
from ujson import loads as json_loads
|
from ujson import loads as json_loads
|
||||||
|
|
||||||
class Request:
|
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):
|
def __init__(self, url_bytes, headers, version, method):
|
||||||
self.protocol = protocol
|
url_parsed = parse_url(url_bytes)
|
||||||
self.url = url
|
self.url = url_parsed.path.decode('utf-8')
|
||||||
self.headers = headers
|
self.headers = headers
|
||||||
self.version = version
|
self.version = version
|
||||||
self.method = method
|
self.method = method
|
||||||
|
self.query_string = url_parsed.query.decode('utf-8') if url_parsed.query else None
|
||||||
# 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
|
# Init but do not inhale
|
||||||
self.body = None
|
self.body = None
|
||||||
|
|
|
@ -23,11 +23,13 @@ from .exceptions import ServerError
|
||||||
from .response import HTTPResponse
|
from .response import HTTPResponse
|
||||||
from .request import Request
|
from .request import Request
|
||||||
|
|
||||||
|
|
||||||
class HttpProtocol(asyncio.Protocol):
|
class HttpProtocol(asyncio.Protocol):
|
||||||
|
|
||||||
__slots__ = ('loop',
|
__slots__ = ('loop',
|
||||||
'transport', 'request', 'parser',
|
'transport', 'request', 'parser',
|
||||||
'url', 'headers', 'sanic')
|
'url', 'headers', 'sanic',
|
||||||
|
'_total_body_size')
|
||||||
|
|
||||||
def __init__(self, *, sanic, loop):
|
def __init__(self, *, sanic, loop):
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
|
@ -44,7 +46,7 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
|
|
||||||
def connection_made(self, transport):
|
def connection_made(self, transport):
|
||||||
self.transport = transport
|
self.transport = transport
|
||||||
#TODO: handle keep-alive/connection timeout
|
#TODO: handle connection timeout
|
||||||
|
|
||||||
# TCP Nodelay
|
# TCP Nodelay
|
||||||
# I have no evidence to support this makes anything faster
|
# I have no evidence to support this makes anything faster
|
||||||
|
@ -64,6 +66,8 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
# -------------------------------------------- #
|
# -------------------------------------------- #
|
||||||
|
|
||||||
def data_received(self, data):
|
def data_received(self, data):
|
||||||
|
#TODO: handle body too large
|
||||||
|
|
||||||
if self.parser is None:
|
if self.parser is None:
|
||||||
assert self.request is None
|
assert self.request is None
|
||||||
self.headers = []
|
self.headers = []
|
||||||
|
@ -84,11 +88,10 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
|
|
||||||
def on_headers_complete(self):
|
def on_headers_complete(self):
|
||||||
self.request = Request(
|
self.request = Request(
|
||||||
protocol=self,
|
url_bytes=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().decode('utf-8')
|
method=self.parser.get_method().decode()
|
||||||
)
|
)
|
||||||
#print("res {} - {}".format(n, self.request))
|
#print("res {} - {}".format(n, self.request))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user