Exemple #1
0
        public void AddPath_Different_NoMatch()
        {
            RpcPath fullPath  = RpcPath.Parse("/Base/Test");
            RpcPath otherPath = fullPath.Add(RpcPath.Parse("/Test"));

            Assert.NotEqual(fullPath, otherPath);
        }
Exemple #2
0
        public bool MatchesRpcRoute(RpcRouteCollection routes, string requestUrl, out RpcRoute route)
        {
            if (routes == null)
            {
                throw new ArgumentNullException(nameof(routes));
            }
            if (requestUrl == null)
            {
                throw new ArgumentNullException(nameof(requestUrl));
            }
            this.Logger?.LogVerbose($"Attempting to match Rpc route for the request url '{requestUrl}'");
            RpcPath requestPath = RpcPath.Parse(requestUrl);
            RpcPath routePrefix = RpcPath.Parse(routes.RoutePrefix);

            foreach (RpcRoute rpcRoute in routes)
            {
                RpcPath routePath = RpcPath.Parse(rpcRoute.Name);
                routePath = routePrefix.Add(routePath);
                if (requestPath == routePath)
                {
                    this.Logger?.LogVerbose($"Matched the request url '{requestUrl}' to the route '{rpcRoute.Name}'");
                    route = rpcRoute;
                    return(true);
                }
            }
            this.Logger?.LogVerbose($"Failed to match the request url '{requestUrl}' to a route");
            route = null;
            return(false);
        }
Exemple #3
0
        public void AddPath_Same_Match()
        {
            RpcPath fullPath  = RpcPath.Parse("/Base/Test");
            RpcPath otherPath = RpcPath.Parse("/Base").Add(RpcPath.Parse("/Test"));

            Assert.Equal(fullPath, otherPath);
        }
        /// <summary>
        /// Indicates if the incoming request matches any predefined routes
        /// </summary>
        /// <param name="requestUrl">The current request url</param>
        /// <param name="route">The matching route corresponding to the request url if found, otherwise it is null</param>
        /// <param name="routeProvider">Provider that allows the retrieval of all configured routes</param>
        /// <returns>True if the request url matches any Rpc routes, otherwise False</returns>
        public bool MatchesRpcRoute(IRpcRouteProvider routeProvider, string requestUrl, out RpcRoute route)
        {
            if (requestUrl == null)
            {
                throw new ArgumentNullException(nameof(requestUrl));
            }
            this.logger?.LogDebug($"Attempting to match Rpc route for the request url '{requestUrl}'");
            RpcPath requestPath = RpcPath.Parse(requestUrl);

            this.logger?.LogTrace($"Request path: {requestPath}");

            foreach (RpcRoute rpcRoute in routeProvider.GetRoutes())
            {
                RpcPath routePath = RpcPath.Parse(rpcRoute.Name);
                this.logger?.LogTrace($"Trying to match against route - Name: {rpcRoute.Name}, Path: {routePath}");
                if (requestPath == routePath)
                {
                    this.logger?.LogDebug($"Matched the request url '{requestUrl}' to the route '{rpcRoute.Name}'");
                    route = rpcRoute;
                    return(true);
                }
            }
            this.logger?.LogDebug($"Failed to match the request url '{requestUrl}' to a route");
            route = null;
            return(false);
        }
Exemple #5
0
        public void ParsePath_DifferentPaths_Valid(string requestUrl, string availableRouteName, bool shouldMatch)
        {
            RpcPath requestPath = RpcPath.Parse(requestUrl);
            RpcPath routePath   = RpcPath.Parse(availableRouteName);

            if (shouldMatch)
            {
                Assert.Equal(routePath, requestPath);
            }
            else
            {
                Assert.NotEqual(routePath, requestPath);
            }
        }
Exemple #6
0
        public void TryRemoveBasePath_1Part_NullOutput()
        {
            RpcPath basePath = RpcPath.Parse("/Base");
            RpcPath fullPath = RpcPath.Parse("/Base/");
            bool    removed  = fullPath.TryRemoveBasePath(basePath, out RpcPath? path);

            Assert.True(removed);
            Assert.Null(path);

            //Also check the Remove is the same
            RpcPath?path2 = fullPath.RemoveBasePath(basePath);

            Assert.Equal(path, path2);
        }
Exemple #7
0
        private Dictionary <RpcPath, List <IRpcMethodProvider> > GetAllRoutes()
        {
            if (this._routeCache == null)
            {
                List <TypeInfo> controllerTypes = new[] { Assembly.GetEntryAssembly(), Assembly.GetExecutingAssembly() }
                .SelectMany(a => a.DefinedTypes)
                .Where(t => !t.IsAbstract && t.IsSubclassOf(typeof(RpcControllerBase)))
                .ToList();

                var controllerRoutes = new Dictionary <RpcPath, List <IRpcMethodProvider> >();
                foreach (TypeInfo controllerType in controllerTypes)
                {
                    var    attribute = controllerType.GetCustomAttribute <RpcRouteAttribute>(true);
                    string routePathString;
                    if (attribute == null || attribute.RouteName == null)
                    {
                        if (controllerType.Name.EndsWith("RpcController"))
                        {
                            routePathString = controllerType.Name.Substring(0,
                                                                            controllerType.Name.IndexOf("RpcController"));
                        }
                        else
                        {
                            routePathString = controllerType.Name;
                        }
                        routePathString = routePathString.Dasherize();
                    }
                    else
                    {
                        routePathString = attribute.RouteName;
                    }
                    RpcPath routePath = RpcPath.Parse(routePathString);
                    if (!controllerRoutes.TryGetValue(routePath, out List <IRpcMethodProvider> methodProviders))
                    {
                        methodProviders             = new List <IRpcMethodProvider>();
                        controllerRoutes[routePath] = methodProviders;
                    }
                    methodProviders.Add(new ControllerPublicMethodProvider(controllerType.AsType()));
                }
                this._routeCache = controllerRoutes;
            }
            return(this._routeCache);
        }
Exemple #8
0
        private Dictionary <RpcPath, List <IRpcMethodProvider> > GetAllRoutes()
        {
            if (this.routeCache == null)
            {
                //TODO will entry assembly be good enough
                List <TypeInfo> controllerTypes = Assembly.GetEntryAssembly().DefinedTypes
                                                  .Where(t => !t.IsAbstract && t.IsSubclassOf(this.Options.BaseControllerType))
                                                  .ToList();

                var controllerRoutes = new Dictionary <RpcPath, List <IRpcMethodProvider> >();
                foreach (TypeInfo controllerType in controllerTypes)
                {
                    var    attribute = controllerType.GetCustomAttribute <RpcRouteAttribute>(true);
                    string routePathString;
                    if (attribute == null || string.IsNullOrWhiteSpace(attribute.RouteName))
                    {
                        if (controllerType.Name.EndsWith("Controller"))
                        {
                            routePathString = controllerType.Name.Substring(0, controllerType.Name.IndexOf("Controller"));
                        }
                        else
                        {
                            routePathString = controllerType.Name;
                        }
                    }
                    else
                    {
                        routePathString = attribute.RouteName;
                    }
                    RpcPath routePath = RpcPath.Parse(routePathString);
                    if (!controllerRoutes.TryGetValue(routePath, out List <IRpcMethodProvider> methodProviders))
                    {
                        methodProviders             = new List <IRpcMethodProvider>();
                        controllerRoutes[routePath] = methodProviders;
                    }
                    methodProviders.Add(new ControllerPublicMethodProvider(controllerType.AsType()));
                }
                this.routeCache = controllerRoutes;
            }
            return(this.routeCache);
        }
        public static async Task HandleJsonRpcWebSocketRequest(this WebSocket webSocket, HttpContext context, IRpcRequestHandler rpcRequestHandler, IRpcRouteProvider rpcRouteProvider)
        {
            byte[]        buffer       = new byte[1024 * 4];
            IRouteContext routeContext = DefaultRouteContext.FromHttpContext(context, rpcRouteProvider);

            while (webSocket.State == WebSocketState.Open)
            {
                WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);

                if (result.MessageType != WebSocketMessageType.Text)
                {
                    continue;
                }
                string requestBody = Encoding.ASCII.GetString(buffer);
                string response    = await rpcRequestHandler.HandleRequestAsync(RpcPath.Parse(context.Request.Path), requestBody, routeContext);

                await webSocket.SendAsync(
                    new ArraySegment <byte>(Encoding.ASCII.GetBytes(response), 0, response.Length),
                    WebSocketMessageType.Text,
                    true,
                    CancellationToken.None
                    );
            }
        }
Exemple #10
0
        public void ToString_LowerCaseAndTrimSlash(string path, string expected)
        {
            RpcPath fullPath = RpcPath.Parse(path);

            Assert.Equal(expected, fullPath?.ToString());
        }