.
This commit is contained in:
parent
7cdeff63f6
commit
ee98c8b42f
|
@ -1,3 +1,3 @@
|
||||||
uvloop
|
|
||||||
httptools
|
httptools
|
||||||
ujson
|
ujson
|
||||||
|
uvloop
|
|
@ -1,42 +1,44 @@
|
||||||
class Request:
|
from urllib.parse import parse_qs
|
||||||
__slots__ = ('protocol', 'url', 'headers', 'version', 'method', 'query_string', 'body', 'parsed_json', 'parsed_args')
|
|
||||||
|
class Request:
|
||||||
def __init__(self, protocol, url, headers, version, method):
|
__slots__ = ('protocol', 'url', 'headers', 'version', 'method', 'query_string', 'body', 'parsed_json', 'parsed_args')
|
||||||
self.protocol = protocol
|
|
||||||
self.url = url
|
def __init__(self, protocol, url, headers, version, method):
|
||||||
self.headers = headers
|
self.protocol = protocol
|
||||||
self.version = version
|
self.url = url
|
||||||
self.method = method
|
self.headers = headers
|
||||||
|
self.version = version
|
||||||
# Capture query string
|
self.method = method
|
||||||
query_string_position = self.url.find("?")
|
|
||||||
if query_string_position != -1:
|
# Capture query string
|
||||||
self.query_string = self.url[query_string_position+1:]
|
query_string_position = self.url.find("?")
|
||||||
self.url = self.url[:query_string_position]
|
if query_string_position != -1:
|
||||||
else:
|
self.query_string = self.url[query_string_position+1:]
|
||||||
self.query_string = None
|
self.url = self.url[:query_string_position]
|
||||||
|
else:
|
||||||
# Init but do not inhale
|
self.query_string = None
|
||||||
self.body = None
|
|
||||||
self.parsed_json = None
|
# Init but do not inhale
|
||||||
self.parsed_args = None
|
self.body = None
|
||||||
|
self.parsed_json = None
|
||||||
@property
|
self.parsed_args = None
|
||||||
def json(self):
|
|
||||||
if not self.parsed_json:
|
@property
|
||||||
if not self.body:
|
def json(self):
|
||||||
raise ValueError("No body to parse")
|
if not self.parsed_json:
|
||||||
self.parsed_json = json_loads(self.body)
|
if not self.body:
|
||||||
|
raise ValueError("No body to parse")
|
||||||
return self.parsed_json
|
self.parsed_json = json_loads(self.body)
|
||||||
|
|
||||||
@property
|
return self.parsed_json
|
||||||
def args(self):
|
|
||||||
if self.parsed_args is None:
|
@property
|
||||||
if self.query_string:
|
def args(self):
|
||||||
parsed_query_string = parse_qs(self.query_string).items()
|
if self.parsed_args is None:
|
||||||
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}
|
if self.query_string:
|
||||||
else:
|
parsed_query_string = parse_qs(self.query_string).items()
|
||||||
self.parsed_args = {}
|
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
|
return self.parsed_args
|
|
@ -1,4 +1,7 @@
|
||||||
import ujson
|
import ujson
|
||||||
|
import httptools
|
||||||
|
from ujson import loads as json_loads
|
||||||
|
from urllib.parse import parse_qs
|
||||||
|
|
||||||
STATUS_CODES = {
|
STATUS_CODES = {
|
||||||
200: 'OK',
|
200: 'OK',
|
||||||
|
@ -18,7 +21,7 @@ class HTTPResponse:
|
||||||
__slots__ = ('body', 'status', 'content_type')
|
__slots__ = ('body', 'status', 'content_type')
|
||||||
|
|
||||||
def __init__(self, body='', status=200, content_type='text/plain'):
|
def __init__(self, body='', status=200, content_type='text/plain'):
|
||||||
self.content_type = 'text/plain'
|
self.content_type = content_type
|
||||||
self.body = body
|
self.body = body
|
||||||
self.status = status
|
self.status = status
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import httptools
|
||||||
import logging
|
import logging
|
||||||
from inspect import isawaitable
|
from inspect import isawaitable
|
||||||
from ujson import loads as json_loads
|
from ujson import loads as json_loads
|
||||||
from urllib.parse import parse_qs
|
|
||||||
from traceback import format_exc
|
from traceback import format_exc
|
||||||
|
|
||||||
import httptools
|
import httptools
|
||||||
|
@ -159,7 +158,6 @@ def abort(msg):
|
||||||
log.info(msg, file=sys.stderr)
|
log.info(msg, file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def serve(sanic, host, port, debug=False):
|
def serve(sanic, host, port, debug=False):
|
||||||
# Create Event Loop
|
# Create Event Loop
|
||||||
loop = async_loop.new_event_loop()
|
loop = async_loop.new_event_loop()
|
||||||
|
|
4
test.py
4
test.py
|
@ -8,10 +8,6 @@ app = Sanic("test")
|
||||||
async def test(request):
|
async def test(request):
|
||||||
return json({ "test": True })
|
return json({ "test": True })
|
||||||
|
|
||||||
@app.route("/text")
|
|
||||||
def test(request):
|
|
||||||
return text('hi')
|
|
||||||
|
|
||||||
@app.route("/exception")
|
@app.route("/exception")
|
||||||
def test(request):
|
def test(request):
|
||||||
raise ServerError("yep")
|
raise ServerError("yep")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user