The rest server library provides some useful defaults for common usage. Most of these defaults can be overridden or modified.
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.
The RestServer.SpecialHandlers
provides the following overrides. If you need to override any of them, there are two options:
Start
methodMonoBehaviour
.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;
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.
Note that the AuthHandler is only called for http requests and not for websocket requests.
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.
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
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.
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.
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.
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.
Called when a websocket upgrade request would be allowed.
Called when a websocket upgrade request was sent to a non-websocket endpoint.
Called after the websocket upgrade request was accepted and before the websocket session is created. Note that the HTTP authentication handler will not be called for websockets.