def check(self, path: str, query_string: str, headers) -> tuple:
"""Check the request against policies and return decision and response.
Returned values are:
- allowed: (bool) True if request is accepted
- content: (bytes|None) HTML bytes if rejected, otherwise None
- status: (HTTPDStatus|None) Status if rejected, otherwise None
- mime_type: (str|None) Mime if rejected, otherwise None
:param path: (str) Normalized URL path (without query).
:param query_string: (str) Raw query string (without '?').
:param headers: (Any) Request headers or environ. Must support ".get(...)".
:return: (tuple) (allowed, content, status, mime_type)
"""
if type(path) is not str:
raise TypeError('HTTPDPolicy.check: path must be a string.')
if type(query_string) is not str:
raise TypeError('HTTPDPolicy.check: query_string must be a string.')
user_agent = self._get_user_agent(headers)
if self.__blacklist_enabled is True:
if self.__blacklist.match(path) is True or self.__blacklist.match(user_agent) is True:
content, status = HTTPDHandlerUtils.blacklisted_page_answer()
return (False, content, status, 'text/html')
ttl_seconds = self.__signed_url_cfg.get('ttl', None)
if ttl_seconds is not None:
protect = self.__signed_url_cfg.get('protect', [])
if self.__signed_url.match_protect(path, protect) is True:
if self.__signed_url.verify(path, query_string, ttl_seconds) is False:
content, status = HTTPDHandlerUtils.signed_url_page_answer()
return (False, content, status, 'text/html')
return (True, None, None, None)