Exemple #1
0
        private Status?ValidateHeaders(HttpResponseMessage httpResponse)
        {
            GrpcCallLog.ResponseHeadersReceived(Logger);

            // gRPC status can be returned in the header when there is no message (e.g. unimplemented status)
            // An explicitly specified status header has priority over other failing statuses
            if (GrpcProtocolHelpers.TryGetStatusCore(httpResponse.Headers, out var status))
            {
                // Trailers are in the header because there is no message.
                // Note that some default headers will end up in the trailers (e.g. Date, Server).
                _trailers = GrpcProtocolHelpers.BuildMetadata(httpResponse.Headers);
                return(status);
            }

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                var statusCode = MapHttpStatusToGrpcCode(httpResponse.StatusCode);
                return(new Status(statusCode, "Bad gRPC response. HTTP status code: " + (int)httpResponse.StatusCode));
            }

            if (httpResponse.Content?.Headers.ContentType == null)
            {
                return(new Status(StatusCode.Cancelled, "Bad gRPC response. Response did not have a content-type header."));
            }

            var grpcEncoding = httpResponse.Content.Headers.ContentType;

            if (!GrpcProtocolHelpers.IsGrpcContentType(grpcEncoding))
            {
                return(new Status(StatusCode.Cancelled, "Bad gRPC response. Invalid content-type value: " + grpcEncoding));
            }

            // Call is still in progress
            return(null);
        }
Exemple #2
0
        private Status?ValidateHeaders(HttpResponseMessage httpResponse)
        {
            Log.ResponseHeadersReceived(Logger);

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                return(new Status(StatusCode.Cancelled, "Bad gRPC response. Expected HTTP status code 200. Got status code: " + (int)httpResponse.StatusCode));
            }

            if (httpResponse.Content?.Headers.ContentType == null)
            {
                return(new Status(StatusCode.Cancelled, "Bad gRPC response. Response did not have a content-type header."));
            }

            var grpcEncoding = httpResponse.Content.Headers.ContentType;

            if (!GrpcProtocolHelpers.IsGrpcContentType(grpcEncoding))
            {
                return(new Status(StatusCode.Cancelled, "Bad gRPC response. Invalid content-type value: " + grpcEncoding));
            }
            else
            {
                // gRPC status can be returned in the header when there is no message (e.g. unimplemented status)
                if (GrpcProtocolHelpers.TryGetStatusCore(httpResponse.Headers, out var status))
                {
                    return(status);
                }
            }

            // Call is still in progress
            return(null);
        }
Exemple #3
0
        internal static Status?ValidateHeaders(HttpResponseMessage httpResponse, out Metadata?trailers)
        {
            // gRPC status can be returned in the header when there is no message (e.g. unimplemented status)
            // An explicitly specified status header has priority over other failing statuses
            if (GrpcProtocolHelpers.TryGetStatusCore(httpResponse.Headers, out var status))
            {
                // Trailers are in the header because there is no message.
                // Note that some default headers will end up in the trailers (e.g. Date, Server).
                trailers = GrpcProtocolHelpers.BuildMetadata(httpResponse.Headers);
                return(status);
            }

            trailers = null;

            // ALPN negotiation is sending HTTP/1.1 and HTTP/2.
            // Check that the response wasn't downgraded to HTTP/1.1.
            if (httpResponse.Version < GrpcProtocolConstants.Http2Version)
            {
                return(new Status(StatusCode.Internal, $"Bad gRPC response. Response protocol downgraded to HTTP/{httpResponse.Version.ToString(2)}."));
            }

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                var statusCode = MapHttpStatusToGrpcCode(httpResponse.StatusCode);
                return(new Status(statusCode, "Bad gRPC response. HTTP status code: " + (int)httpResponse.StatusCode));
            }

            // Don't access Headers.ContentType property because it is not threadsafe.
            var contentType = GrpcProtocolHelpers.GetHeaderValue(httpResponse.Content?.Headers, "Content-Type");

            if (contentType == null)
            {
                return(new Status(StatusCode.Cancelled, "Bad gRPC response. Response did not have a content-type header."));
            }

            if (!CommonGrpcProtocolHelpers.IsContentType(GrpcProtocolConstants.GrpcContentType, contentType))
            {
                return(new Status(StatusCode.Cancelled, "Bad gRPC response. Invalid content-type value: " + contentType));
            }

            // Call is still in progress
            return(null);
        }
Exemple #4
0
        private Status?ValidateHeaders(HttpResponseMessage httpResponse)
        {
            GrpcCallLog.ResponseHeadersReceived(Logger);

            // gRPC status can be returned in the header when there is no message (e.g. unimplemented status)
            // An explicitly specified status header has priority over other failing statuses
            if (GrpcProtocolHelpers.TryGetStatusCore(httpResponse.Headers, out var status))
            {
                // Trailers are in the header because there is no message.
                // Note that some default headers will end up in the trailers (e.g. Date, Server).
                _trailers = GrpcProtocolHelpers.BuildMetadata(httpResponse.Headers);
                return(status);
            }

            // ALPN negotiation is sending HTTP/1.1 and HTTP/2.
            // Check that the response wasn't downgraded to HTTP/1.1.
            if (httpResponse.Version < HttpVersion.Version20)
            {
                return(new Status(StatusCode.Internal, $"Bad gRPC response. Response protocol downgraded to HTTP/{httpResponse.Version.ToString(2)}."));
            }

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                var statusCode = MapHttpStatusToGrpcCode(httpResponse.StatusCode);
                return(new Status(statusCode, "Bad gRPC response. HTTP status code: " + (int)httpResponse.StatusCode));
            }

            if (httpResponse.Content?.Headers.ContentType == null)
            {
                return(new Status(StatusCode.Cancelled, "Bad gRPC response. Response did not have a content-type header."));
            }

            var grpcEncoding = httpResponse.Content.Headers.ContentType;

            if (!CommonGrpcProtocolHelpers.IsContentType(GrpcProtocolConstants.GrpcContentType, grpcEncoding?.MediaType))
            {
                return(new Status(StatusCode.Cancelled, "Bad gRPC response. Invalid content-type value: " + grpcEncoding));
            }

            // Call is still in progress
            return(null);
        }