Merge branch 'jpiasetz-speedup_response'
This commit is contained in:
commit
4153028096
|
@ -1,21 +1,18 @@
|
||||||
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: b'OK',
|
||||||
400: 'Bad Request',
|
400: b'Bad Request',
|
||||||
401: 'Unauthorized',
|
401: b'Unauthorized',
|
||||||
402: 'Payment Required',
|
402: b'Payment Required',
|
||||||
403: 'Forbidden',
|
403: b'Forbidden',
|
||||||
404: 'Not Found',
|
404: b'Not Found',
|
||||||
405: 'Method Not Allowed',
|
405: b'Method Not Allowed',
|
||||||
500: 'Internal Server Error',
|
500: b'Internal Server Error',
|
||||||
501: 'Not Implemented',
|
501: b'Not Implemented',
|
||||||
502: 'Bad Gateway',
|
502: b'Bad Gateway',
|
||||||
503: 'Service Unavailable',
|
503: b'Service Unavailable',
|
||||||
504: 'Gateway Timeout',
|
504: b'Gateway Timeout',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,24 +33,24 @@ class HTTPResponse:
|
||||||
def output(self, version="1.1", keep_alive=False, keep_alive_timeout=None):
|
def output(self, version="1.1", keep_alive=False, keep_alive_timeout=None):
|
||||||
# This is all returned in a kind-of funky way
|
# This is all returned in a kind-of funky way
|
||||||
# We tried to make this as fast as possible in pure python
|
# We tried to make this as fast as possible in pure python
|
||||||
additional_headers = []
|
timeout_header = b''
|
||||||
if keep_alive and not keep_alive_timeout is None:
|
if keep_alive and keep_alive_timeout:
|
||||||
additional_headers = [b'Keep-Alive: timeout=', str(keep_alive_timeout).encode(), b's\r\n']
|
timeout_header = b'Keep-Alive: timeout=%d\r\n' % keep_alive_timeout
|
||||||
|
|
||||||
|
headers = b''
|
||||||
if self.headers:
|
if self.headers:
|
||||||
for name, value in self.headers.items():
|
headers = b''.join(b'%b: %b\r\n' % (name.encode(), value.encode('utf-8')) for name, value in self.headers.items())
|
||||||
additional_headers.append('{}: {}\r\n'.format(name, value).encode('utf-8'))
|
return b'HTTP/%b %d %b\r\nContent-Type: %b\r\nContent-Length: %d\r\nConnection: %b\r\n%b%b\r\n%b' % (
|
||||||
|
version.encode(),
|
||||||
return b''.join([
|
self.status,
|
||||||
'HTTP/{} {} {}\r\n'.format(version, self.status,
|
STATUS_CODES.get(self.status, b'FAIL'),
|
||||||
STATUS_CODES.get(self.status, 'FAIL')).encode(),
|
self.content_type.encode(),
|
||||||
b'Content-Type: ', self.content_type.encode(), b'\r\n',
|
len(self.body),
|
||||||
b'Content-Length: ', str(len(self.body)).encode(), b'\r\n',
|
b'keep-alive' if keep_alive else b'close',
|
||||||
b'Connection: ', ('keep-alive' if keep_alive else 'close').encode(), b'\r\n',
|
timeout_header,
|
||||||
] + additional_headers + [
|
headers,
|
||||||
b'\r\n',
|
self.body
|
||||||
self.body,
|
)
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
def json(body, status=200, headers=None):
|
def json(body, status=200, headers=None):
|
||||||
return HTTPResponse(ujson.dumps(body), headers=headers, status=status,
|
return HTTPResponse(ujson.dumps(body), headers=headers, status=status,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user