mirror of https://github.com/grpc/grpc-java.git
Inspect content type from trailers when there are no headers.
------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=81291102
This commit is contained in:
parent
fb09bba456
commit
63fc64761a
|
|
@ -47,6 +47,7 @@ public abstract class Http2ClientStream extends AbstractClientStream<Integer> {
|
||||||
|
|
||||||
private Status transportError;
|
private Status transportError;
|
||||||
private Charset errorCharset = Charsets.UTF_8;
|
private Charset errorCharset = Charsets.UTF_8;
|
||||||
|
private boolean contentTypeChecked;
|
||||||
|
|
||||||
protected Http2ClientStream(ClientStreamListener listener,
|
protected Http2ClientStream(ClientStreamListener listener,
|
||||||
@Nullable Decompressor decompressor,
|
@Nullable Decompressor decompressor,
|
||||||
|
|
@ -61,23 +62,20 @@ public abstract class Http2ClientStream extends AbstractClientStream<Integer> {
|
||||||
transportError = transportError.augmentDescription(headers.toString());
|
transportError = transportError.augmentDescription(headers.toString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String contentType = headers.get(HttpUtil.CONTENT_TYPE);
|
|
||||||
Status httpStatus = statusFromHttpStatus(headers);
|
Status httpStatus = statusFromHttpStatus(headers);
|
||||||
if (httpStatus == null) {
|
if (httpStatus == null) {
|
||||||
transportError = Status.INTERNAL.withDescription(
|
transportError = Status.INTERNAL.withDescription(
|
||||||
"received non-terminal headers with no :status");
|
"received non-terminal headers with no :status");
|
||||||
} else if (!httpStatus.isOk()) {
|
} else if (!httpStatus.isOk()) {
|
||||||
transportError = httpStatus;
|
transportError = httpStatus;
|
||||||
} else if (TEMP_CHECK_CONTENT_TYPE &&
|
} else {
|
||||||
!HttpUtil.CONTENT_TYPE_GRPC.equalsIgnoreCase(contentType)) {
|
transportError = checkContentType(headers);
|
||||||
// Malformed content-type so report an error
|
|
||||||
transportError = Status.INTERNAL.withDescription("invalid content-type " + contentType);
|
|
||||||
}
|
}
|
||||||
if (transportError != null) {
|
if (transportError != null) {
|
||||||
// Note we don't immediately report the transport error, instead we wait for more data on the
|
// 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.
|
// stream so we can accumulate more detail into the error before reporting it.
|
||||||
transportError = transportError.withDescription("\n" + headers.toString());
|
transportError = transportError.withDescription("\n" + headers.toString());
|
||||||
errorCharset = charsetFromContentType(contentType);
|
errorCharset = extractCharset(headers);
|
||||||
} else {
|
} else {
|
||||||
stripTransportDetails(headers);
|
stripTransportDetails(headers);
|
||||||
inboundHeadersReceived(headers);
|
inboundHeadersReceived(headers);
|
||||||
|
|
@ -137,6 +135,10 @@ public abstract class Http2ClientStream extends AbstractClientStream<Integer> {
|
||||||
if (transportError != null) {
|
if (transportError != null) {
|
||||||
// Already received a transport error so just augment it.
|
// Already received a transport error so just augment it.
|
||||||
transportError = transportError.augmentDescription(trailers.toString());
|
transportError = transportError.augmentDescription(trailers.toString());
|
||||||
|
} else {
|
||||||
|
transportError = checkContentType(trailers);
|
||||||
|
}
|
||||||
|
if (transportError != null) {
|
||||||
inboundTransportError(transportError);
|
inboundTransportError(transportError);
|
||||||
} else {
|
} else {
|
||||||
Status status = statusFromTrailers(trailers);
|
Status status = statusFromTrailers(trailers);
|
||||||
|
|
@ -178,10 +180,29 @@ public abstract class Http2ClientStream extends AbstractClientStream<Integer> {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inspect the content type field from received headers or trailers and return an error Status if
|
||||||
|
* content type is invalid or not present. Returns null if no error was found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private Status checkContentType(Metadata headers) {
|
||||||
|
if (contentTypeChecked) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
contentTypeChecked = true;
|
||||||
|
String contentType = headers.get(HttpUtil.CONTENT_TYPE);
|
||||||
|
if (TEMP_CHECK_CONTENT_TYPE && !HttpUtil.CONTENT_TYPE_GRPC.equalsIgnoreCase(contentType)) {
|
||||||
|
// Malformed content-type so report an error
|
||||||
|
return Status.INTERNAL.withDescription("invalid content-type " + contentType);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inspect the raw metadata and figure out what charset is being used.
|
* Inspect the raw metadata and figure out what charset is being used.
|
||||||
*/
|
*/
|
||||||
private static Charset charsetFromContentType(String contentType) {
|
private static Charset extractCharset(Metadata headers) {
|
||||||
|
String contentType = headers.get(HttpUtil.CONTENT_TYPE);
|
||||||
if (contentType != null) {
|
if (contentType != null) {
|
||||||
String[] split = contentType.split("charset=");
|
String[] split = contentType.split("charset=");
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue