sanic/sanic/request.py

201 lines
6.4 KiB
Python
Raw Normal View History

2016-10-15 20:59:00 +01:00
from cgi import parse_header
from collections import namedtuple
from http.cookies import SimpleCookie
2016-10-15 20:59:00 +01:00
from httptools import parse_url
from urllib.parse import parse_qs
from ujson import loads as json_loads
2016-12-08 04:33:56 +00:00
from sanic.exceptions import InvalidUsage
2016-10-15 20:59:00 +01:00
from .log import log
DEFAULT_HTTP_CONTENT_TYPE = "application/octet-stream"
# HTTP/1.1: https://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1
# > If the media type remains unknown, the recipient SHOULD treat it
# > as type "application/octet-stream"
2016-10-15 20:59:00 +01:00
class RequestParameters(dict):
"""
Hosts a dict with lists as values where get returns the first
value of the list and getlist returns the whole shebang
"""
def get(self, name, default=None):
"""Return the first value, either the default or actual"""
return super().get(name, [default])[0]
2016-10-15 20:59:00 +01:00
def getlist(self, name, default=None):
"""Return the entire list"""
return super().get(name, default)
2016-10-15 20:59:00 +01:00
2016-11-20 01:48:28 +00:00
class Request(dict):
"""
Properties of an HTTP request such as URL, headers, etc.
"""
2016-10-15 20:59:00 +01:00
__slots__ = (
'url', 'headers', 'version', 'method', '_cookies', 'transport',
2016-10-15 20:59:00 +01:00
'query_string', 'body',
'parsed_json', 'parsed_args', 'parsed_form', 'parsed_files',
'_ip',
2016-10-15 20:59:00 +01:00
)
def __init__(self, url_bytes, headers, version, method, transport):
2016-10-15 20:59:00 +01:00
# TODO: Content-Encoding detection
url_parsed = parse_url(url_bytes)
self.url = url_parsed.path.decode('utf-8')
self.headers = headers
self.version = version
self.method = method
self.transport = transport
2016-10-16 14:01:59 +01:00
self.query_string = None
if url_parsed.query:
self.query_string = url_parsed.query.decode('utf-8')
2016-10-15 20:59:00 +01:00
# Init but do not inhale
self.body = None
self.parsed_json = None
self.parsed_form = None
self.parsed_files = None
self.parsed_args = None
self._cookies = None
2016-10-15 20:59:00 +01:00
@property
def json(self):
if self.parsed_json is None:
2016-10-15 20:59:00 +01:00
try:
self.parsed_json = json_loads(self.body)
2016-10-16 14:01:59 +01:00
except Exception:
2016-12-08 04:33:56 +00:00
raise InvalidUsage("Failed when parsing body as json")
2016-10-15 20:59:00 +01:00
return self.parsed_json
@property
def token(self):
"""
Attempts to return the auth header token.
:return: token related to request
"""
auth_header = self.headers.get('Authorization')
if auth_header is not None:
return auth_header.split()[1]
return auth_header
2016-10-15 20:59:00 +01:00
@property
def form(self):
if self.parsed_form is None:
self.parsed_form = RequestParameters()
self.parsed_files = RequestParameters()
content_type = self.headers.get(
'Content-Type', DEFAULT_HTTP_CONTENT_TYPE)
content_type, parameters = parse_header(content_type)
2016-10-15 20:59:00 +01:00
try:
if content_type == 'application/x-www-form-urlencoded':
2016-10-16 14:01:59 +01:00
self.parsed_form = RequestParameters(
parse_qs(self.body.decode('utf-8')))
2016-10-15 20:59:00 +01:00
elif content_type == 'multipart/form-data':
# TODO: Stream this instead of reading to/from memory
boundary = parameters['boundary'].encode('utf-8')
2016-10-16 14:01:59 +01:00
self.parsed_form, self.parsed_files = (
parse_multipart_form(self.body, boundary))
2016-11-19 07:16:20 +00:00
except Exception:
2016-11-28 19:00:39 +00:00
log.exception("Failed when parsing form")
2016-11-19 07:16:20 +00:00
2016-10-15 20:59:00 +01:00
return self.parsed_form
@property
def files(self):
if self.parsed_files is None:
2016-10-16 10:21:24 +01:00
self.form # compute form to get files
2016-10-15 20:59:00 +01:00
return self.parsed_files
@property
def args(self):
if self.parsed_args is None:
if self.query_string:
2016-10-16 14:01:59 +01:00
self.parsed_args = RequestParameters(
parse_qs(self.query_string))
2016-10-15 20:59:00 +01:00
else:
self.parsed_args = {}
return self.parsed_args
@property
def cookies(self):
if self._cookies is None:
2016-11-27 13:30:46 +00:00
cookie = self.headers.get('Cookie') or self.headers.get('cookie')
if cookie is not None:
cookies = SimpleCookie()
2016-11-27 13:30:46 +00:00
cookies.load(cookie)
self._cookies = {name: cookie.value
for name, cookie in cookies.items()}
else:
self._cookies = {}
return self._cookies
@property
def ip(self):
2017-01-17 01:21:57 +00:00
if not hasattr(self, '_ip'):
2017-01-16 23:45:29 +00:00
self._ip = self.transport.get_extra_info('peername')
2017-01-16 23:51:56 +00:00
return self._ip
2016-10-15 20:59:00 +01:00
File = namedtuple('File', ['type', 'body', 'name'])
def parse_multipart_form(body, boundary):
"""
Parses a request body and returns fields and files
2016-10-15 20:59:00 +01:00
:param body: Bytes request body
:param boundary: Bytes multipart boundary
2016-11-09 13:04:15 +00:00
:return: fields (RequestParameters), files (RequestParameters)
2016-10-15 20:59:00 +01:00
"""
files = RequestParameters()
fields = RequestParameters()
2016-10-15 20:59:00 +01:00
form_parts = body.split(boundary)
for form_part in form_parts[1:-1]:
file_name = None
file_type = None
field_name = None
line_index = 2
line_end_index = 0
while not line_end_index == -1:
line_end_index = form_part.find(b'\r\n', line_index)
form_line = form_part[line_index:line_end_index].decode('utf-8')
line_index = line_end_index + 2
if not form_line:
break
colon_index = form_line.index(':')
form_header_field = form_line[0:colon_index]
2016-10-16 14:01:59 +01:00
form_header_value, form_parameters = parse_header(
form_line[colon_index + 2:])
2016-10-15 20:59:00 +01:00
if form_header_field == 'Content-Disposition':
if 'filename' in form_parameters:
file_name = form_parameters['filename']
field_name = form_parameters.get('name')
elif form_header_field == 'Content-Type':
file_type = form_header_value
post_data = form_part[line_index:-4]
if file_name or file_type:
file = File(type=file_type, name=file_name, body=post_data)
if field_name in files:
files[field_name].append(file)
else:
files[field_name] = [file]
2016-10-15 20:59:00 +01:00
else:
value = post_data.decode('utf-8')
if field_name in fields:
fields[field_name].append(value)
else:
fields[field_name] = [value]
2016-10-15 20:59:00 +01:00
return fields, files