Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,23 @@ private void applyHeaderUpdates(final Iterable<HeaderValueOption> headerOptions,
private void updateHeader(final HeaderValueOption option, Metadata mutableHeaders) {
HeaderValue header = option.header();
HeaderAppendAction action = option.appendAction();
boolean keepEmptyValue = option.keepEmptyValue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably delete the config field as well if unused.


if (header.key().endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
if (header.rawValue().isPresent()) {
byte[] value = header.rawValue().get().toByteArray();
if (value.length > 0 || keepEmptyValue) {
updateHeader(action, Metadata.Key.of(header.key(), Metadata.BINARY_BYTE_MARSHALLER),
value, mutableHeaders);
Metadata.Key<byte[]> key = Metadata.Key.of(header.key(), Metadata.BINARY_BYTE_MARSHALLER);
updateHeader(action, key, header.rawValue().get().toByteArray(), mutableHeaders);
if (containsEmpty(key, mutableHeaders)) {
mutableHeaders.discardAll(key);
}
} else {
logger.fine("Missing binary rawValue for header: " + header.key());
}
} else {
if (header.value().isPresent()) {
String value = header.value().get();
if (!value.isEmpty() || keepEmptyValue) {
updateHeader(action, Metadata.Key.of(header.key(), Metadata.ASCII_STRING_MARSHALLER),
value, mutableHeaders);
Metadata.Key<String> key = Metadata.Key.of(header.key(), Metadata.ASCII_STRING_MARSHALLER);
updateHeader(action, key, header.value().get(), mutableHeaders);
if (containsEmpty(key, mutableHeaders)) {
mutableHeaders.discardAll(key);
}
} else {
logger.fine("Missing value for header: " + header.key());
Expand Down Expand Up @@ -119,5 +118,24 @@ private <T> void updateHeader(final HeaderAppendAction action, final Metadata.Ke
logger.fine("Unknown HeaderAppendAction: " + action);
}
}

private <T> boolean containsEmpty(Metadata.Key<T> key, Metadata headers) {
Iterable<T> values = headers.getAll(key);
if (values == null) {
return false;
}
for (T val : values) {
if (val instanceof String) {
if (((String) val).isEmpty()) {
return true;
}
} else if (val instanceof byte[]) {
if (((byte[]) val).length == 0) {
return true;
}
}
}
return false;
}
Comment on lines +122 to +139

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since empty mutations are now handled directly in updateHeader without needing to scan the metadata, the containsEmpty helper method is no longer needed and can be safely removed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to investigate the need for this function. I've added a comment here previously grpc/proposal#481 (comment)
about compliance with envoy behavior.

}

Original file line number Diff line number Diff line change
Expand Up @@ -247,30 +247,27 @@ public void applyMutations_keepEmptyValue() {
headerMutator.applyMutations(mutations, headers);

assertThat(headers.containsKey(NEW_ADD_KEY)).isFalse();
assertThat(headers.getAll(APPEND_KEY)).containsExactly("existing-value");
assertThat(headers.get(OVERWRITE_KEY)).isEqualTo("existing-value");
assertThat(headers.containsKey(APPEND_KEY)).isFalse();
assertThat(headers.containsKey(OVERWRITE_KEY)).isFalse();
assertThat(headers.containsKey(ADD_KEY)).isFalse();
assertThat(headers.get(OVERWRITE_IF_EXISTS_KEY)).isEqualTo("existing-value");
assertThat(headers.containsKey(OVERWRITE_IF_EXISTS_KEY)).isFalse();

Metadata.Key<String> keepEmptyKey =
Metadata.Key.of("keep-empty-key", Metadata.ASCII_STRING_MARSHALLER);
Metadata.Key<String> keepEmptyOverwriteKey =
Metadata.Key.of("keep-empty-overwrite-key", Metadata.ASCII_STRING_MARSHALLER);

assertThat(headers.containsKey(keepEmptyKey)).isTrue();
assertThat(headers.get(keepEmptyKey)).isEqualTo("");
assertThat(headers.containsKey(keepEmptyOverwriteKey)).isTrue();
assertThat(headers.get(keepEmptyOverwriteKey)).isEqualTo("");
assertThat(headers.containsKey(keepEmptyKey)).isFalse();
assertThat(headers.containsKey(keepEmptyOverwriteKey)).isFalse();

Metadata.Key<byte[]> keepEmptyBinKey =
Metadata.Key.of("keep-empty-bin-key-bin", Metadata.BINARY_BYTE_MARSHALLER);
Metadata.Key<byte[]> ignoreEmptyBinKey =
Metadata.Key.of("ignore-empty-bin-key-bin", Metadata.BINARY_BYTE_MARSHALLER);

assertThat(headers.containsKey(keepEmptyBinKey)).isTrue();
assertThat(headers.get(keepEmptyBinKey)).isEqualTo(new byte[0]);
assertThat(headers.containsKey(keepEmptyBinKey)).isFalse();
assertThat(headers.containsKey(ignoreEmptyBinKey)).isFalse();
assertThat(headers.get(overwriteEmptyBinKey)).isEqualTo(originalBinValue);
assertThat(headers.containsKey(overwriteEmptyBinKey)).isFalse();
}

@Test
Expand Down
Loading