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); }
private void ValidateHeaders() { Log.ResponseHeadersReceived(Logger); Debug.Assert(HttpResponse != null); if (HttpResponse.StatusCode != HttpStatusCode.OK) { _headerValidationError = "Bad gRPC response. Expected HTTP status code 200. Got status code: " + (int)HttpResponse.StatusCode; } else if (HttpResponse.Content?.Headers.ContentType == null) { _headerValidationError = "Bad gRPC response. Response did not have a content-type header."; } else { var grpcEncoding = HttpResponse.Content.Headers.ContentType.ToString(); if (!GrpcProtocolHelpers.IsGrpcContentType(grpcEncoding)) { _headerValidationError = "Bad gRPC response. Invalid content-type value: " + grpcEncoding; } } if (_headerValidationError != null) { // Response is not valid gRPC // Clean up/cancel any pending operations DisposeCore(); throw new InvalidOperationException(_headerValidationError); } // Success! }
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); }
private void ValidateHeaders() { // We don't want to throw in this method, even for non-success situations. // Response is still needed to return headers in GetResponseHeadersAsync. Log.ResponseHeadersReceived(Logger); Debug.Assert(HttpResponse != null); if (HttpResponse.StatusCode != HttpStatusCode.OK) { FinishResponse(throwOnFail: false, new Status(StatusCode.Cancelled, "Bad gRPC response. Expected HTTP status code 200. Got status code: " + (int)HttpResponse.StatusCode)); return; } if (HttpResponse.Content?.Headers.ContentType == null) { FinishResponse(throwOnFail: false, new Status(StatusCode.Cancelled, "Bad gRPC response. Response did not have a content-type header.")); return; } var grpcEncoding = HttpResponse.Content.Headers.ContentType.ToString(); if (!GrpcProtocolHelpers.IsGrpcContentType(grpcEncoding)) { FinishResponse(throwOnFail: false, new Status(StatusCode.Cancelled, "Bad gRPC response. Invalid content-type value: " + grpcEncoding)); return; } else { if (TryGetStatusCore(HttpResponse.Headers, out var status)) { // grpc-status is returned in the header when there is no message body // For example, unimplemented method/service status FinishResponse(throwOnFail: false, status); return; } } // Success! }