Skip to content

Commit 0ddad69

Browse files
authored
core: Clarify missing content-type on HTTP error responses (#12720)
## Summary In this case, grpc-java preserves the HTTP status-based context, but may later append `invalid content-type: null`. That seems misleading when the response is missing `content-type`, because the header appears to be absent rather than invalid, and the `null` value exposes a Java implementation detail. This PR keeps the existing HTTP-to-gRPC status mapping unchanged and only adjusts the diagnostic for the missing `content-type` case. ## Changes - keep the existing HTTP-to-gRPC status mapping unchanged - keep the existing behavior for invalid non-null content-types unchanged - replace `invalid content-type: null` with `missing content-type in response headers` Ref #12418
1 parent d92ca44 commit 0ddad69

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

core/src/main/java/io/grpc/internal/Http2ClientStreamTransportState.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,11 @@ private Status validateInitialMetadata(Metadata headers) {
223223
}
224224
String contentType = headers.get(GrpcUtil.CONTENT_TYPE_KEY);
225225
if (!GrpcUtil.isGrpcContentType(contentType)) {
226-
return GrpcUtil.httpStatusToGrpcStatus(httpStatus)
227-
.augmentDescription("invalid content-type: " + contentType);
226+
Status status = GrpcUtil.httpStatusToGrpcStatus(httpStatus);
227+
if (contentType == null) {
228+
return status.augmentDescription("missing content-type in response headers");
229+
}
230+
return status.augmentDescription("invalid content-type: " + contentType);
228231
}
229232
return null;
230233
}

core/src/test/java/io/grpc/internal/Http2ClientStreamTransportStateTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,24 @@ public void transportTrailersReceived_missingStatusUsesHttpStatus() {
301301
assertTrue(statusCaptor.getValue().getDescription().contains("401"));
302302
}
303303

304+
@Test
305+
public void transportTrailersReceived_missingContentTypeUsesHttpStatus() {
306+
BaseTransportState state = new BaseTransportState(transportTracer);
307+
state.setListener(mockListener);
308+
Metadata trailers = new Metadata();
309+
trailers.put(testStatusMashaller, "431");
310+
311+
state.transportTrailersReceived(trailers);
312+
313+
verify(mockListener, never()).headersRead(any(Metadata.class));
314+
verify(mockListener).closed(statusCaptor.capture(), same(PROCESSED), same(trailers));
315+
assertEquals(Code.INTERNAL, statusCaptor.getValue().getCode());
316+
assertTrue(statusCaptor.getValue().getDescription().contains("HTTP status code 431"));
317+
assertTrue(
318+
statusCaptor.getValue().getDescription().contains(
319+
"missing content-type in response headers"));
320+
}
321+
304322
@Test
305323
public void transportTrailersReceived_missingHttpStatus() {
306324
BaseTransportState state = new BaseTransportState(transportTracer);

0 commit comments

Comments
 (0)