redirectory.libs_int.hyperscan.hs_manager module

class redirectory.libs_int.hyperscan.hs_manager.HsManager[source]

Bases: object

database = None
static get_error_code(error: hyperscan.error) → int[source]

Hyperscan errors are differentiated by their message instead of an Exception object. This method extracts the error code of a Hyperscan error from the message of that error.

Parameters:error – a Hyperscan error object
Returns:integer representing the Hyperscan error
static pick_result(db_session, redirect_rule_ids: list) → Tuple[Optional[redirectory.models.redirect_rule.RedirectRule], Optional[bool]][source]

Checks which of the redirect rules has the largest weight. Gets every redirect rule from the DB and compares their weights. If all the redirect rules have the same weight then the request is considered ambiguous

Parameters:
  • db_session – the database session to be used with all DB actions
  • redirect_rule_ids – a list of all the redirect rule ids
Returns:

the picked redirect rule and if the choice is ambiguous or not

search(domain: str, path: str, is_test: bool = False) → Union[list, dict, None][source]

Searches the two Hyperscan databases for the best match. First it searches the domains to find the right one. Then it combines the id of the domain with the path into a rule. The rule is searched again with the Rule Hyperscan database.

Parameters:
  • domain – the domain to search for
  • path – the path to the domain to search for
  • is_test – if set to true the function returns the two search context objects for the domain and rule
Returns:

if no match is found int: the id of the redirect rule dict: a dictionary with both the domain and rule search context objects for testing

Return type:

None

search_domain(domain: str, domain_search_ctx: redirectory.libs_int.hyperscan.search_context.SearchContext = None) → Optional[redirectory.libs_int.hyperscan.search_context.SearchContext][source]

Searches a domain in the hyperscan domain database. Creates a SearchContext object and runs a scan for the domain. Also handles a cancellation of the search which is a hyperscan error with error code -3. If the search doesn’t find any matches a None is returned. If there are matches then a SearchContext object will be returned.

Parameters:
  • domain – the domain to search for
  • domain_search_ctx – SearchContext to be passed to Hyperscan
Returns:

None or a SearchContext object

search_rule(rule: str, rule_search_ctx: redirectory.libs_int.hyperscan.search_context.SearchContext = None) → Optional[redirectory.libs_int.hyperscan.search_context.SearchContext][source]

Searches a rule in the hyperscan rule database. Really similar to the search_domain() method. If the search doesn’t find any matches a None is returned. If there are matches then a SearchContext object will be returned.

Parameters:
  • rule – the rule to search for. {domain_id}/{path}
  • rule_search_ctx – SearchContext to be passed to Hyperscan
Returns:

None or SearchContext object