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

Set the trailing headers directly when constructing DefaultLastHttpContent #2495

Merged
merged 1 commit into from Sep 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -32,6 +32,7 @@
import java.util.function.Function;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
Expand Down Expand Up @@ -654,7 +655,7 @@ protected void onOutboundComplete() {
f = channel().writeAndFlush(newFullBodyMessage(EMPTY_BUFFER));
}
else if (markSentBody()) {
LastHttpContent lastHttpContent = LastHttpContent.EMPTY_LAST_CONTENT;
HttpHeaders trailerHeaders = null;
// https://datatracker.ietf.org/doc/html/rfc7230#section-4.1.2
// A trailer allows the sender to include additional fields at the end
// of a chunked message in order to supply metadata that might be
Expand All @@ -670,22 +671,21 @@ else if (markSentBody()) {
// which fields will be present in the trailers.
String declaredHeaderNames = responseHeaders.get(HttpHeaderNames.TRAILER);
if (declaredHeaderNames != null) {
HttpHeaders trailerHeaders = new TrailerHeaders(declaredHeaderNames);
trailerHeaders = new TrailerHeaders(declaredHeaderNames);
try {
trailerHeadersConsumer.accept(trailerHeaders);
}
catch (IllegalArgumentException e) {
// A sender MUST NOT generate a trailer when header names are
// HttpServerOperations.TrailerHeaders.DISALLOWED_TRAILER_HEADER_NAMES
log.error(format(channel(), "Cannot apply trailer headers [{0}]"), declaredHeaderNames, e);
}
if (!trailerHeaders.isEmpty()) {
lastHttpContent = new DefaultLastHttpContent();
lastHttpContent.trailingHeaders().set(trailerHeaders);
log.error(format(channel(), "Cannot apply trailer headers [{}]"), declaredHeaderNames, e);
}
}
}
f = channel().writeAndFlush(lastHttpContent);

f = channel().writeAndFlush(trailerHeaders != null && !trailerHeaders.isEmpty() ?
new DefaultLastHttpContent(Unpooled.buffer(0), trailerHeaders) :
LastHttpContent.EMPTY_LAST_CONTENT);
}
else {
discard();
Expand Down