Overriding Default Behaviour

The rest server library provides some useful defaults for common usage. Most of these defaults can be overridden or modified.

Threading timeout

The ThreadingHelper.Instance.ExecuteSync method waits 1000ms for the executed workload to finish and returns the result. The wait-time can be modified by setting ThreadingHelper.Instance.ThreadingMillisecondsTimeout to a different value.

Special Handlers

The RestServer.SpecialHandlers provides the following overrides. If you need to override any of them, there are two options:

  1. Extend the RestServer class and set the new special handlers in the Start method
  2. Set the special handlers from any other MonoBehaviour.

The special handlers can be set at any time in the lifecycle of the server. The default implementations for each use case can be found in the class DefaultRequestHandlerImpl. Handlers are lambdas and set like this

server.SpecialHandlers.AuthHandler = DefaultRequestHandlerImpl.AllowAllAuthHandler;

AuthHandler

The AuthHandler is called upon every request and determines, if the rest server should handle the request. The lambda is called upon every request and returns false, if the request should be discarded. Note that it’s expected that the Handler returns the correct response to the client. The request.SendAsyncDefaultNotAuthenticated() can be used to send a request for basic auth to the client.

The default implementation DefaultRequestHandlerImpl.AllowAllAuthHandler allows all requests.

Set this lambda to a custom implementation to modify the behaviour. Note that the implementation has to send a response back to the caller.

EndpointErrorHandler

Exception handler EndpointErrorHandler is called, if the endpoint implementation throws an unhandled error. The default implementation returns a 500 Internal Server Error back to the caller and logs the exception in unity.

Set this lambda to a custom implementation to modify the behaviour. Note that the implementation has to send a response back to the caller.

NoEndpointFoundHandler

NoEndpointFoundHandler is called when no endpoint has been registered for the called location. A 404 Not Found is sent back to the caller by default.

Set this lambda to a custom implementation to modify the behaviour. Note that the implementation has to send a response back to the caller.

LowLevelOnReceivedRequestError

Called when the underlying library has any unspecified error while parsing the incoming request. The default implementation logs the error.

Set this lambda to a custom implementation to modify the behaviour. An argument can be made to send 400 Bad Request back to the caller, as the request can not be parsed.

LowLevelOnError

The lambda specified by SpecialHandlers.LowLevelOnError is called, when the system socket has encountered an error. The default implementation logs the error.

Set this lambda to a custom implementation to modify the behaviour. No information can be sent back to the caller.

AsynchronousExceptionHandler

If the workload supplied to ThreadingHelper.Instance.ExecuteAsync or ThreadingHelper.Instance.ExecuteCoroutine throws an unhandled error, the exception is handed to the AsynchronousExceptionHandler to handle the exception. As this type of workload is asynchronous, the original request can no longer be reached. The default implementation logs this error.

Set this lambda to a custom implementation to modify the behaviour. No information can be sent back to the caller.