Example #1
0
        /// <summary>
        /// Takes a route/http contexts and attempts to parse, invoke, respond to an Rpc request
        /// </summary>
        /// <param name="context">Route context</param>
        /// <returns>Task for async routing</returns>
        public async Task RouteAsync(RouteContext context)
        {
            ILogger <RpcRouter> logger = context.HttpContext.RequestServices.GetService <ILogger <RpcRouter> >();

            try
            {
                RpcPath requestPath;
                if (!context.HttpContext.Request.Path.HasValue)
                {
                    requestPath = RpcPath.Default;
                }
                else
                {
                    if (!RpcPath.TryParse(context.HttpContext.Request.Path.Value, out requestPath))
                    {
                        logger?.LogInformation($"Could not parse the path '{context.HttpContext.Request.Path.Value}' for the " +
                                               $"request into an rpc path. Skipping rpc router middleware.");
                        return;
                    }
                }
                if (!requestPath.TryRemoveBasePath(this.routeProvider.BaseRequestPath, out requestPath))
                {
                    logger?.LogTrace("Request did not match the base request path. Skipping rpc router.");
                    return;
                }
                HashSet <RpcPath> availableRoutes = this.routeProvider.GetRoutes();
                if (!availableRoutes.Any())
                {
                    logger?.LogDebug($"Request matched base request path but no routes.");
                    return;
                }
                logger?.LogInformation($"Rpc request route '{requestPath}' matched.");

                Stream contentStream = context.HttpContext.Request.Body;

                string jsonString;
                if (contentStream == null)
                {
                    jsonString = null;
                }
                else
                {
                    StreamReader streamReader = new StreamReader(contentStream);
                    jsonString = streamReader.ReadToEnd().Trim();
                }

                var    requestHandler = context.HttpContext.RequestServices.GetRequiredService <IRpcRequestHandler>();
                var    routeContext   = DefaultRouteContext.FromHttpContext(context.HttpContext, this.routeProvider);
                string responseJson   = await requestHandler.HandleRequestAsync(requestPath, jsonString, routeContext);

                if (responseJson == null)
                {
                    //No response required
                    return;
                }

                context.HttpContext.Response.ContentType = "application/json";

                bool   responseSet    = false;
                string acceptEncoding = context.HttpContext.Request.Headers["Accept-Encoding"];
                if (!string.IsNullOrWhiteSpace(acceptEncoding))
                {
                    IRpcCompressor compressor = context.HttpContext.RequestServices.GetService <IRpcCompressor>();
                    if (compressor != null)
                    {
                        string[] encodings = acceptEncoding.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string encoding in encodings)
                        {
                            bool haveType = Enum.TryParse(encoding, true, out CompressionType compressionType);
                            if (!haveType)
                            {
                                continue;
                            }
                            context.HttpContext.Response.Headers.Add("Content-Encoding", new[] { encoding });
                            compressor.CompressText(context.HttpContext.Response.Body, responseJson, Encoding.UTF8, compressionType);
                            responseSet = true;
                            break;
                        }
                    }
                }
                if (!responseSet)
                {
                    await context.HttpContext.Response.WriteAsync(responseJson);
                }

                context.MarkAsHandled();

                logger?.LogInformation("Rpc request complete");
            }
            catch (Exception ex)
            {
                string errorMessage = "Unknown exception occurred when trying to process Rpc request. Marking route unhandled";
                logger?.LogException(ex, errorMessage);
                context.MarkAsHandled();
            }
        }