예제 #1
0
        public Task HandleCallAsync(HttpContext httpContext)
        {
            if (GrpcProtocolHelpers.IsInvalidContentType(httpContext, out var error))
            {
                // This might be a CORS preflight request and CORS middleware hasn't been configured
                if (GrpcProtocolHelpers.IsCorsPreflightRequest(httpContext))
                {
                    GrpcServerLog.UnhandledCorsPreflightRequest(Logger);

                    GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status405MethodNotAllowed, StatusCode.Internal, "Unhandled CORS preflight request received. CORS may not be configured correctly in the application.");
                    httpContext.Response.Headers[HeaderNames.Allow] = HttpMethods.Post;
                    return(Task.CompletedTask);
                }
                else
                {
                    GrpcServerLog.UnsupportedRequestContentType(Logger, httpContext.Request.ContentType);

                    GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status415UnsupportedMediaType, StatusCode.Internal, error);
                    return(Task.CompletedTask);
                }
            }
            if (httpContext.Request.Protocol != GrpcProtocolConstants.Http2Protocol &&
                httpContext.Request.Protocol != GrpcProtocolConstants.Http20Protocol)
            {
                GrpcServerLog.UnsupportedRequestProtocol(Logger, httpContext.Request.Protocol);

                var protocolError = $"Request protocol '{httpContext.Request.Protocol}' is not supported.";
                GrpcProtocolHelpers.BuildHttpErrorResponse(httpContext.Response, StatusCodes.Status426UpgradeRequired, StatusCode.Internal, protocolError);
                httpContext.Response.Headers[HeaderNames.Upgrade] = GrpcProtocolConstants.Http2Protocol;
                return(Task.CompletedTask);
            }

            var serverCallContext = new HttpContextServerCallContext(httpContext, MethodInvoker.Options, typeof(TRequest), typeof(TResponse), Logger);

            httpContext.Features.Set <IServerCallContextFeature>(serverCallContext);

            GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

            try
            {
                serverCallContext.Initialize();

                var handleCallTask = HandleCallAsyncCore(httpContext, serverCallContext);

                if (handleCallTask.IsCompletedSuccessfully)
                {
                    return(serverCallContext.EndCallAsync());
                }
                else
                {
                    return(AwaitHandleCall(serverCallContext, MethodInvoker.Method, handleCallTask));
                }
            }
            catch (Exception ex)
            {
                return(serverCallContext.ProcessHandlerErrorAsync(ex, MethodInvoker.Method.Name));
            }
예제 #2
0
            public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
            {
                if (httpContext == null)
                {
                    return(false);
                }

                // Constraint needs to be valid when a CORS preflight request is received so that CORS middleware will run
                if (GrpcProtocolHelpers.IsCorsPreflightRequest(httpContext))
                {
                    return(true);
                }

                if (!HttpMethods.IsPost(httpContext.Request.Method))
                {
                    return(false);
                }

                return(CommonGrpcProtocolHelpers.IsContentType(GrpcProtocolConstants.GrpcContentType, httpContext.Request.ContentType) ||
                       CommonGrpcProtocolHelpers.IsContentType(GrpcProtocolConstants.GrpcWebContentType, httpContext.Request.ContentType) ||
                       CommonGrpcProtocolHelpers.IsContentType(GrpcProtocolConstants.GrpcWebTextContentType, httpContext.Request.ContentType));
            }