nserver.middleware 🔗
AfterQueryHook
module-attribute
🔗
Type alias for HookMiddleware.after_query
functions.
BeforeFirstQueryHook
module-attribute
🔗
Type alias for HookMiddleware.before_first_query
functions.
BeforeQueryHook
module-attribute
🔗
Type alias for HookMiddleware.before_query
functions.
ExceptionHandler
module-attribute
🔗
Type alias for ExceptionHandlerMiddleware
exception handler functions
QueryMiddlewareCallable
module-attribute
🔗
Type alias for functions that can be used with QueryMiddleware.next_function
RawRecordExceptionHandler
module-attribute
🔗
Type alias for RawRecordExceptionHandlerMiddleware
exception handler functions
RawRecordMiddlewareCallable
module-attribute
🔗
Type alias for functions that can be used with RawRecordMiddleware.next_function
ExceptionHandlerMiddleware 🔗
ExceptionHandlerMiddleware(
exception_handlers: Optional[
Dict[Type[Exception], ExceptionHandler]
] = None
)
Bases: QueryMiddleware
Middleware for handling exceptions originating from a QueryMiddleware
stack.
Allows registering handlers for individual Exception
types. Only one handler can
exist for a given Exception
type.
When an exception is encountered, the middleware will search for the first handler that
matches the class or parent class of the exception in method resolution order. If no handler
is registered will use this classes self.default_exception_handler
.
New in 2.0
.
Attributes:
Name | Type | Description |
---|---|---|
exception_handlers |
registered exception handlers |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exception_handlers |
Optional[Dict[Type[Exception], ExceptionHandler]]
|
exception handlers to assign |
None
|
default_exception_handler
staticmethod
🔗
The default exception handler
get_exception_handler 🔗
Get the exception handler for an Exception
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exception |
Exception
|
the exception we wish to handle |
required |
process_query 🔗
Call the next function catching any handling any errors
register_next_function 🔗
Set the next_function
of this middleware
HookMiddleware 🔗
HookMiddleware(
before_first_query: Optional[
List[BeforeFirstQueryHook]
] = None,
before_query: Optional[List[BeforeQueryHook]] = None,
after_query: Optional[List[AfterQueryHook]] = None,
)
Bases: QueryMiddleware
Middleware for processing hook functions
There are three types of hooks:
before_first_query
hooks will be run once at the time that the first query
is received. They take no arguments and return no results. These are guaranteed
to run at most once - however if any hook fails it will cause no other hooks to
be run. Subsequent queries will continue to be processed regardless of if all
before_first_query
hooks ran or not.
before_query
hooks will be run before each request. They receive a Query
as an argument. If a hooks returns a non None
result, process will skip to
result processing.
after_query
hooks will be run after a result has been returned from a before_query
hook or from the next function in the middleware chain. They take a Response
input
and must return a Response
.
New in 2.0
.
Attributes:
Name | Type | Description |
---|---|---|
before_first_query |
List[BeforeFirstQueryHook]
|
|
before_query |
List[BeforeQueryHook]
|
|
after_query |
List[AfterQueryHook]
|
|
before_first_query_run |
bool
|
have we run the |
before_first_query_failed |
bool
|
did any |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
before_first_query |
Optional[List[BeforeFirstQueryHook]]
|
initial |
None
|
before_query |
Optional[List[BeforeQueryHook]]
|
initial |
None
|
after_query |
Optional[List[AfterQueryHook]]
|
initial |
None
|
QueryMiddleware 🔗
Middleware for interacting with Query
objects
New in 2.0
.
process_query 🔗
Handle an incoming query.
Child classes should override this function (if they do not this middleware will simply pass the query onto the next function).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
Query
|
the incoming query |
required |
call_next |
QueryMiddlewareCallable
|
the next function in the chain |
required |
register_next_function 🔗
Set the next_function
of this middleware
QueryMiddlewareProcessor 🔗
Convert an incoming DNS record and pass it to a QueryMiddleware
stack.
This class serves as the bottom of the RawRcordMiddleware
stack.
New in 2.0
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_middleware |
QueryMiddlewareCallable
|
the top of the middleware stack |
required |
RawRecordExceptionHandlerMiddleware 🔗
RawRecordExceptionHandlerMiddleware(
exception_handlers: Optional[
Dict[Type[Exception], RawRecordExceptionHandler]
] = None
)
Bases: RawRecordMiddleware
Middleware for handling exceptions originating from a RawRecordMiddleware
stack.
Allows registering handlers for individual Exception
types. Only one handler can
exist for a given Exception
type.
When an exception is encountered, the middleware will search for the first handler that
matches the class or parent class of the exception in method resolution order. If no handler
is registered will use this classes self.default_exception_handler
.
Important
Exception handlers are expected to be robust - that is, they must always
return correctly even if they internally encounter an Exception
.
New in 2.0
.
Attributes:
Name | Type | Description |
---|---|---|
exception_handlers |
Dict[Type[Exception], RawRecordExceptionHandler]
|
registered exception handlers |
default_exception_handler
staticmethod
🔗
Default exception handler
get_exception_handler 🔗
Get the exception handler for the given exception
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exception |
Exception
|
the exception we wish to handle |
required |
process_record 🔗
process_record(
record: dnslib.DNSRecord,
call_next: RawRecordMiddlewareCallable,
) -> dnslib.DNSRecord
Call the next function handling any exceptions that arise
register_next_function 🔗
Set the next_function
of this middleware
RawRecordMiddleware 🔗
Middleware to be run against raw dnslib.DNSRecord
s.
New in 2.0
.
process_record 🔗
process_record(
record: dnslib.DNSRecord,
call_next: RawRecordMiddlewareCallable,
) -> dnslib.DNSRecord
Handle an incoming record.
Child classes should override this function (if they do not this middleware will simply pass the record onto the next function).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record |
DNSRecord
|
the incoming record |
required |
call_next |
RawRecordMiddlewareCallable
|
the next function in the chain |
required |
register_next_function 🔗
Set the next_function
of this middleware
RuleProcessor 🔗
Find and run a matching rule function.
This class serves as the bottom of the QueryMiddleware
stack.
New in 2.0
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rules |
List[RuleBase]
|
rules to run against |
required |
coerce_to_response 🔗
Convert some RuleResult
to a Response
New in 2.0
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result |
RuleResult
|
the results to convert |
required |
Raises:
Type | Description |
---|---|
TypeError
|
unsupported result type |