Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: discard outbound content-length header #8522

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
core: discard outbound content-length header
  • Loading branch information
dapengzhang0 committed Sep 13, 2021
commit cc3ff99ca7e416d84614604dad5eb7db6b8c4bd1
2 changes: 2 additions & 0 deletions core/src/main/java/io/grpc/internal/ClientCallImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static io.grpc.Status.DEADLINE_EXCEEDED;
import static io.grpc.internal.GrpcUtil.CONTENT_ACCEPT_ENCODING_KEY;
import static io.grpc.internal.GrpcUtil.CONTENT_ENCODING_KEY;
import static io.grpc.internal.GrpcUtil.CONTENT_LENGTH_KEY;
import static io.grpc.internal.GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY;
import static io.grpc.internal.GrpcUtil.MESSAGE_ENCODING_KEY;
import static java.lang.Math.max;
Expand Down Expand Up @@ -163,6 +164,7 @@ static void prepareHeaders(
DecompressorRegistry decompressorRegistry,
Compressor compressor,
boolean fullStreamDecompression) {
headers.discardAll(CONTENT_LENGTH_KEY);
headers.discardAll(MESSAGE_ENCODING_KEY);
if (compressor != Codec.Identity.NONE) {
headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding());
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/io/grpc/internal/GrpcUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public final class GrpcUtil {
public static final Metadata.Key<byte[]> CONTENT_ACCEPT_ENCODING_KEY =
InternalMetadata.keyOf(GrpcUtil.CONTENT_ACCEPT_ENCODING, new AcceptEncodingMarshaller());

static final Metadata.Key<String> CONTENT_LENGTH_KEY =
Metadata.Key.of("content-length", Metadata.ASCII_STRING_MARSHALLER);

private static final class AcceptEncodingMarshaller implements TrustedAsciiMarshaller<byte[]> {
@Override
public byte[] toAsciiString(byte[] value) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/io/grpc/internal/ServerCallImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static io.grpc.internal.GrpcUtil.ACCEPT_ENCODING_SPLITTER;
import static io.grpc.internal.GrpcUtil.CONTENT_LENGTH_KEY;
import static io.grpc.internal.GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY;
import static io.grpc.internal.GrpcUtil.MESSAGE_ENCODING_KEY;

Expand Down Expand Up @@ -107,6 +108,7 @@ private void sendHeadersInternal(Metadata headers) {
checkState(!sendHeadersCalled, "sendHeaders has already been called");
checkState(!closeCalled, "call is closed");

headers.discardAll(CONTENT_LENGTH_KEY);
headers.discardAll(MESSAGE_ENCODING_KEY);
if (compressor == null) {
compressor = Codec.Identity.NONE;
Expand Down
9 changes: 9 additions & 0 deletions core/src/test/java/io/grpc/internal/ClientCallImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,15 @@ public void prepareHeaders_ignoreIdentityEncoding() {
assertNull(m.get(GrpcUtil.MESSAGE_ENCODING_KEY));
}

@Test
public void prepareHeaders_ignoreContentLength() {
Metadata m = new Metadata();
m.put(GrpcUtil.CONTENT_LENGTH_KEY, "123");
ClientCallImpl.prepareHeaders(m, decompressorRegistry, Codec.Identity.NONE, false);

assertNull(m.get(GrpcUtil.CONTENT_LENGTH_KEY));
}

@Test
public void prepareHeaders_acceptedMessageEncodingsAdded() {
Metadata m = new Metadata();
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/java/io/grpc/internal/ServerCallImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc.internal;

import static com.google.common.base.Charsets.UTF_8;
import static io.grpc.internal.GrpcUtil.CONTENT_LENGTH_KEY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -151,6 +152,16 @@ public void sendHeader_firstCall() {
verify(stream).writeHeaders(headers);
}

@Test
public void sendHeader_contentLengthDiscarded() {
Metadata headers = new Metadata();
headers.put(CONTENT_LENGTH_KEY, "123");
call.sendHeaders(headers);

verify(stream).writeHeaders(headers);
assertNull(headers.get(CONTENT_LENGTH_KEY));
}

@Test
public void sendHeader_failsOnSecondCall() {
call.sendHeaders(new Metadata());
Expand Down