Repository navigation
core: Update HTTP status to gRPC status mapping - #2362
Conversation
| private static Status.Code httpStatusToGrpcCode(int httpStatusCode) { | ||
| if (httpStatusCode > 100 && httpStatusCode < 200) { | ||
| // 1xx. These headers should have been ignored. | ||
| return Status.Code.INTERNAL; |
There was a problem hiding this comment.
Can we update doc and point out explicitly that if 100 happens, it should be INTERNAL ?
There was a problem hiding this comment.
The doc already calls out what should happen with 100: "Technically, 1xx should have the entire header skipped and a subsequent header be read. See RFC 7540 §8.1."
There was a problem hiding this comment.
can you add comments why Status.Code.UNKNOWN is returned for httpStatusCode == 100 or httpStatusCode == 200 or code the cases explicitly?
There was a problem hiding this comment.
I already added a comment for 100. You want something else?
I'm not special-casing 200. 200 is caught by the default case below.
There was a problem hiding this comment.
Oh, I see now. 100 itself is not caught by the condition here.
There was a problem hiding this comment.
Fixed the condition. It doesn't seem any comment is needed.
|
Coverage is low, so I'll add tests for Http2ClientStream* |
|
Marked as backport for the non-reliance on HTTP 200. Just the first commit could be backported, we probably don't want/need to backport the HTTP status code mapping. |
|
I added tests, but just for Http2ClientStreamTransportState. Http2ClientStream is on the chopping block, so I didn't want to waste the time. |
| private static Status.Code httpStatusToGrpcCode(int httpStatusCode) { | ||
| if (httpStatusCode > 100 && httpStatusCode < 200) { | ||
| // 1xx. These headers should have been ignored. | ||
| return Status.Code.INTERNAL; |
There was a problem hiding this comment.
can you add comments why Status.Code.UNKNOWN is returned for httpStatusCode == 100 or httpStatusCode == 200 or code the cases explicitly?
| if (transportError != null) { | ||
| // Note we don't immediately report the transport error, instead we wait for more data on | ||
| // the stream so we can accumulate more detail into the error before reporting it. | ||
| transportError = transportError.augmentDescription(headers.toString()); |
There was a problem hiding this comment.
Could something like .augmentDescription("headers=" + headers) here and line 98 and 181 be more informative?
There was a problem hiding this comment.
Done. Used "headers: " and "trailers: "
|
|
||
| stripTransportDetails(headers); | ||
| inboundHeadersReceived(headers); | ||
| } finally { |
There was a problem hiding this comment.
There seems no exception thrown here. Why use try-finally rather than else if?
There was a problem hiding this comment.
else doesn't run when you return. I could use a do {} while (false) + break to get similar effect, but finally is more natural.
There was a problem hiding this comment.
With else you don't have to return. I think the following will do (might have to be with 3 layers of curly braces vs. 2 layers using try-finally). Similar for the try-finally in Http2ClientStreamTransportState.
if (headersReceived) {
transportError = Status.INTERNAL.withDescription("Received headers twice");
} else {
Integer httpStatus = headers.get(HTTP2_STATUS);
// Ignore the headers if httpStatus is >=100 and <200. See RFC 7540 §8.1
if (httpStatus == null || httpStatus < 100 || httpStatus >= 200) {
headersReceived = true;
transportError = validateInitialMetadata(headers);
if (transportError == null) {
stripTransportDetails(headers);
inboundHeadersReceived(headers);
}
}
}
if (transportError != null) {
// Note we don't immediately report the transport error, instead we wait for more data on
// the stream so we can accumulate more detail into the error before reporting it.
transportError = transportError.augmentDescription(headers.toString());
transportErrorMetadata = headers;
errorCharset = extractCharset(headers);
}
There was a problem hiding this comment.
Ah. I try to avoid nested ifs for verification, because the code becomes arbitrarily indented and nested. And any condition you add may cause indentation of the pre-existing conditions to change (causing review and blame noise).
There was a problem hiding this comment.
What I really want is "goto error", which is actually not a horrible idiom. It can only be emulated in Java, although with the finally it isn't too bad.
There was a problem hiding this comment.
"goto error" makes more sense. To avoid nested ifs is also possible though. (Single layer of curly braces vs two layers using try-final)
Integer httpStatus = headers.get(HTTP2_STATUS);
if (headersReceived) {
transportError = Status.INTERNAL.withDescription("Received headers twice");
} else if (httpStatus == null || httpStatus < 100 || httpStatus >= 200) {
// Ignore the headers if httpStatus is >=100 and <200. See RFC 7540 §8.1
headersReceived = true;
transportError = validateInitialMetadata(headers);
}
if (transportError != null) {
// Note we don't immediately report the transport error, instead we wait for more data on
// the stream so we can accumulate more detail into the error before reporting it.
transportError = transportError.augmentDescription(headers.toString());
transportErrorMetadata = headers;
errorCharset = extractCharset(headers);
} else if (httpStatus == null || httpStatus < 100 || httpStatus >= 200) {
stripTransportDetails(headers);
inboundHeadersReceived(headers);
}
| if (httpStatus != null) { | ||
| status = GrpcUtil.httpStatusToGrpcStatus(httpStatus); | ||
| } else { | ||
| status = Status.INTERNAL.withDescription("missing HTTP status code"); |
There was a problem hiding this comment.
.withDescription("missing HTTP status code") being overridden by the return statement below?
There was a problem hiding this comment.
Yes... I already fixed that though...
Doh. I just fixed it in Http2ClientStreamTransportState.
|
|
||
| stripTransportDetails(headers); | ||
| inboundHeadersReceived(headers); | ||
| } finally { |
There was a problem hiding this comment.
With else you don't have to return. I think the following will do (might have to be with 3 layers of curly braces vs. 2 layers using try-finally). Similar for the try-finally in Http2ClientStreamTransportState.
if (headersReceived) {
transportError = Status.INTERNAL.withDescription("Received headers twice");
} else {
Integer httpStatus = headers.get(HTTP2_STATUS);
// Ignore the headers if httpStatus is >=100 and <200. See RFC 7540 §8.1
if (httpStatus == null || httpStatus < 100 || httpStatus >= 200) {
headersReceived = true;
transportError = validateInitialMetadata(headers);
if (transportError == null) {
stripTransportDetails(headers);
inboundHeadersReceived(headers);
}
}
}
if (transportError != null) {
// Note we don't immediately report the transport error, instead we wait for more data on
// the stream so we can accumulate more detail into the error before reporting it.
transportError = transportError.augmentDescription(headers.toString());
transportErrorMetadata = headers;
errorCharset = extractCharset(headers);
}
| private Status checkContentType(Metadata headers) { | ||
| if (contentTypeChecked) { | ||
| return null; | ||
| private Status validateInitialMetadata(Metadata headers) { |
There was a problem hiding this comment.
This method and statusFromTrailers are exactly the same as the ones in Http2ClientStream. Can they be public static methods of io.grpc.Status?
There was a problem hiding this comment.
Well, one is a copy from the other. I want to delete the code, not focus on sharing it.
We only want to use the HTTP code for errors, when the response is not grpc. grpc status codes may be mapped to HTTP codes in the future, and we don't want to break when that happens. We also don't want to ever accidentally use Status.OK without receiving it from the server, even for HTTP 200.
Proxies can trigger errors, and we'd like to categorize common proxy-generating errors into gRPC status codes for sane application handling. Fixes grpc#2264
a1bd621 to
2e7e25d
Compare
This also includes a commit to stop relying on Status.OK == HTTP 200, what was a necessary pre-refactoring. The commit has a general rework of the processing trying to keep the logic sane instead of just hacking the logic together.
The two commits will remain split when merged to master. It probably doesn't matter much whether you review them together or separately.
The changes to Http2ClientStreamTransportState.java were literally copied from Http2ClientStream.java, without any specialized changes.
CC @lukaszx0