private ParsedUri GetUriFromMethod(MethodInfo methodInfo)
        {
            var       uriFormatter = methodInfo.GetCustomAttribute <UriFormatAttribute>();
            ParsedUri parsedUri;

            if (!_uriParser.TryParse(uriFormatter.UriFormat, out parsedUri))
            {
                throw new Exception($"Could not parse uri: {uriFormatter.UriFormat}");
            }

            return(parsedUri);
        }
Beispiel #2
0
        private ParsedUri GetUriFromMethod(MethodInfo methodInfo)
        {
            var uriFormatter = methodInfo.GetCustomAttribute <UriFormatAttribute>();

            string controllerName = RemoveSuffix(methodInfo.DeclaringType.Name, new string[] { "ApiController", "Controller" });

            ParsedUri parsedUri;

            if (!_uriParser.TryParse($"/{controllerName}/{uriFormatter.UriFormat.RemovePreAndPostSlash()}/", out parsedUri))
            {
                throw new Exception($"Could not parse uri: {uriFormatter.UriFormat}");
            }

            return(parsedUri);
        }
Beispiel #3
0
        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));
            }
        }
Beispiel #4
0
        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());
            }
        }