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)); } }
protected override object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri) { var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType); object contentObj = null; try { if (request.HttpServerRequest.Content != null) { contentObj = _contentSerializer.FromContent( request.ContentEncoding.GetString(request.HttpServerRequest.Content), request.ContentMediaType, info.ContentParameterType); } } catch (JsonReaderException) { return(_responseFactory.CreateBadRequest()); } catch (InvalidOperationException) { return(_responseFactory.CreateBadRequest()); } object[] parameters = null; try { parameters = info.GetParametersFromUri(requestUri).Concat(new[] { contentObj }).ToArray(); } catch (FormatException) { return(_responseFactory.CreateBadRequest()); } return(info.MethodInfo.Invoke( instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()), parameters)); }
protected override object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri) { var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType); object[] parameters; try { parameters = info.GetParametersFromUri(requestUri).ToArray(); } catch (FormatException) { return(_responseFactory.CreateBadRequest()); } return(info.MethodInfo.Invoke( instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()), parameters)); }
internal async Task <IRestResponse> HandleRequestAsync(RestServerRequest req, IAuthorizationProvider authorizationProvider) { 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))); } // check if authentication is required AuthorizeAttribute authAttribute = null; // first check on controller level if (restMethod.MethodInfo.DeclaringType.GetTypeInfo().IsDefined(typeof(AuthorizeAttribute))) { authAttribute = restMethod.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes <AuthorizeAttribute>().Single(); } // otherwise check on method level else if (restMethod.MethodInfo.IsDefined(typeof(AuthorizeAttribute))) { authAttribute = restMethod.MethodInfo.GetCustomAttributes <AuthorizeAttribute>().Single(); } if (authAttribute != null) // need to check authentication { if (authorizationProvider == null) { _log.Error("HandleRequestAsync|AuthenticationProvider not configured"); return(_responseFactory.CreateInternalServerError()); } var authResult = authorizationProvider.Authorize(req.HttpServerRequest); if (authResult == HttpResponseStatus.Unauthorized) { return(_responseFactory.CreateWwwAuthenticate(authorizationProvider.Realm)); } } var restMethodExecutor = _methodExecuteFactory.Create(restMethod); try { return(await restMethodExecutor.ExecuteMethodAsync(restMethod, req, parsedUri)); } catch { return(_responseFactory.CreateBadRequest()); } }