Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fromstring = etree.HTML
if self.mimetype == 'text/html':
return fromstring(self.data)
return etree.XML(self.data)
lxml = cached_property(lxml)
def json(self):
"""Get the result of simplejson.loads if possible."""
if 'json' not in self.mimetype:
raise AttributeError('Not a JSON response')
try:
from simplejson import loads
except ImportError:
from json import loads
return loads(self.data)
json = cached_property(json)
class TestResponse(Response, ContentAccessors):
"""Pass this to `werkzeug.test.Client` for easier unittesting."""
@cached_property
def access_route(self):
"""If a forwarded header exists this is a list of all ip addresses
from the client ip to the last proxy server.
"""
if 'HTTP_X_FORWARDED_FOR' in self.environ:
addr = self.environ['HTTP_X_FORWARDED_FOR'].split(',')
return self.list_storage_class([x.strip() for x in addr])
elif 'REMOTE_ADDR' in self.environ:
return self.list_storage_class([self.environ['REMOTE_ADDR']])
return self.list_storage_class()
@cached_property
def role_in_cluster(self):
return 'master' if self.master else 'slave'
@cached_property
def _property_attributes(self):
if not self.properties:
return ()
return [field.attribute or key for key, field in self.properties.items()]
@cached_property
def request(self):
schema = self.schema()
if isinstance(schema, tuple):
return schema[1]
return schema
@cached_property
def base_path(self):
"""The base path of the URL."""
url = self.values['PROJECT'].get('url')
if url:
return url_parse(url).path.rstrip('/') + '/'
return '/'
@cached_property
def tree_details(self):
tree_name = self.request_arguments.get('tree_name')
if tree_name:
return self._get_tree_details(tree_name).document
else:
return dict()
@cached_property
def data(self):
"""
Contains the incoming request data as string in case it came with
a mimetype Werkzeug does not handle.
"""
if self.disable_data_descriptor:
raise AttributeError('data descriptor is disabled')
# XXX: this should eventually be deprecated.
# We trigger form data parsing first which means that the descriptor
# will not cache the data that would otherwise be .form or .files
# data. This restores the behavior that was there in Werkzeug
# before 0.9. New code should use :meth:`get_data` explicitly as
# this will make behavior explicit.
return self.get_data(parse_form_data=True)
@cached_property
def if_unmodified_since(self):
"""The parsed `If-Unmodified-Since` header as datetime object."""
return parse_date(self.environ.get('HTTP_IF_UNMODIFIED_SINCE'))
@cached_property
def meta(self):
"""
Store a dict of metadata parsed as YAML
from the header of the file.
"""
meta = yaml.safe_load(self._meta)
# YAML documents can be any type but we want a dict
# eg. yaml.safe_load('') -> None
# yaml.safe_load('- 1\n- a') -> [1, 'a']
if not meta:
return {}
if not isinstance(meta, dict):
raise ValueError("Expected a dict in metadata for '{0}', got {1}".
format(self.path, type(meta).__name__))
return meta