internal async Task <IRestResponse> HandleRequestAsync(RestServerRequest req)
{
if (!req.HttpServerRequest.IsComplete ||
req.HttpServerRequest.Method == HttpMethod.Unsupported)
{
return(_responseFactory.CreateBadRequest());
}
ParsedUri parsedUri;
var incomingUriAsString = req.HttpServerRequest.Uri.ToRelativeString();
if (!_uriParser.TryParse(incomingUriAsString, out parsedUri))
{
throw new Exception($"Could not parse uri: {incomingUriAsString}");
}
var restMethods = _restMethodCollection.Where(r => r.Match(parsedUri)).ToList();
if (!restMethods.Any())
{
return(_responseFactory.CreateBadRequest());
}
var restMethod = restMethods.FirstOrDefault(r => r.Verb == req.HttpServerRequest.Method);
if (restMethod == null)
{
return(new MethodNotAllowedResponse(restMethods.Select(r => r.Verb)));
}
var restMethodExecutor = _methodExecuteFactory.Create(restMethod);
try
{
var task = restMethodExecutor.ExecuteMethodAsync(restMethod, req, parsedUri);
return(await task);
}
catch (Exception ex)
{
return(_responseFactory.CreateInternalServerErrorResponse(ex));
}
}