core.request

Module Contents

Classes

RequestValidator

RequestValidators can be used to validate a request very early on. If the validate method returns a tuple,

FetchMetaDataValidator

This validator examines the headers “Sec-Fetch-Site”, “sec-fetch-mode” and “sec-fetch-dest” as

BrowseHandler

This class accepts the requests, collect its parameters and routes the request

class core.request.RequestValidator

Bases: abc.ABC

RequestValidators can be used to validate a request very early on. If the validate method returns a tuple, the request is aborted. Can be used to block requests from bots.

To register a new validator, append it to :attr: viur.core.request.BrowseHandler.requestValidators

name = RequestValidator
abstract static validate(request: BrowseHandler) Optional[Tuple[int, str, str]]

The function that checks the current request. If the request is valid, simply return None. If the request should be blocked, it must return a tuple of - The HTTP status code (as int) - The Description of that status code (eg “Forbidden”) - The Response Body (can be a simple string or an HTML-Page)

Parameters

request – The Request instance to check

Returns

None on success, an Error-Tuple otherwise

class core.request.FetchMetaDataValidator

Bases: RequestValidator

This validator examines the headers “Sec-Fetch-Site”, “sec-fetch-mode” and “sec-fetch-dest” as recommended by https://web.dev/fetch-metadata/

name = FetchMetaDataValidator
static validate(request: BrowseHandler) Optional[Tuple[int, str, str]]

The function that checks the current request. If the request is valid, simply return None. If the request should be blocked, it must return a tuple of - The HTTP status code (as int) - The Description of that status code (eg “Forbidden”) - The Response Body (can be a simple string or an HTML-Page)

Parameters

request – The Request instance to check

Returns

None on success, an Error-Tuple otherwise

class core.request.BrowseHandler(request: webob.Request, response: webob.Response)

This class accepts the requests, collect its parameters and routes the request to its destination function. The basic control flow is - Setting up internal variables - Running the Request validators - Emitting the headers (especially the security related ones) - Run the TLS check (ensure it’s a secure connection or check if the URL is whitelisted) - Load or initialize a new session - Set up i18n (choosing the language etc) - Run the request preprocessor (if any) - Normalize & sanity check the parameters - Resolve the exposed function and call it - Save the session / tear down the request - Return the response generated

Warning

Don’t instantiate! Don’t subclass! DON’T TOUCH! ;)

requestValidators
redirect(self, uri, permanent=False, abort=False, code=None, body=None, request=None, response=None)

Issues an HTTP redirect to the given relative URI.

This won’t stop code execution unless abort is True. A common practice is to return when calling this method:

return redirect('/some-path')
Parameters
  • uri – A relative or absolute URI (e.g., '../flowers.html').

  • permanent – If True, uses a 301 redirect instead of a 302 redirect.

  • abort – If True, raises an exception to perform the redirect.

  • code – The redirect status code. Supported codes are 301, 302, 303, 305, and 307. 300 is not supported because it’s not a real redirect and 304 because it’s the answer for a request with defined If-Modified-Since headers.

  • body – Response body, if any.

  • request – Optional request object. If not set, uses get_request().

  • response – Optional response object. If not set, a new response is created.

Returns

A Response instance.

selectLanguage(self, path: str)

Tries to select the best language for the current request. Depending on the value of conf[“viur.languageMethod”], we’ll either try to load it from the session, determine it by the domain or extract it from the URL.

processRequest(self)

Bring up the enviroment for this request, start processing and handle errors

processTypeHint(self, typeHint: ClassVar, inValue: Union[str, List[str]], parsingOnly: bool) Tuple[Union[str, List[str]], Any]

Helper function to enforce/convert the incoming :param: inValue to the type defined in :param: typeHint. Returns a string 2-tuple of the new value we’ll store in self.kwargs as well as the parsed value that’s passed to the caller. The first value is always the unmodified string, the unmodified list of strings or (in case typeHint is List[T] and the provided inValue is a simple string) a List containing only inValue. The second returned value is inValue converted to whatever type is suggested by typeHint.

Parameters
  • typeHint – Type to which inValue should be converted to

  • inValue – The value that should be converted to the given type

  • parsingOnly – If true, the parameter is a keyword argument which we can convert to List

Returns

2-tuple of the original string-value and the converted value

findAndCall(self, path: str, *args, **kwargs)

Does the actual work of sanitizing the parameter, determine which @exposed (or @internalExposed) function to call (and with witch parameters)

saveSession(self)