Skip to content

Commit

Permalink
Ensure any custom CL/TE header is removed when 304 response code (
Browse files Browse the repository at this point in the history
  • Loading branch information
violetagg authored Jun 6, 2023
1 parent ba557e2 commit 32459b2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,7 @@ public NettyOutbound send(Publisher<? extends ByteBuf> source) {
return new PostHeadersNettyOutbound(((Mono<ByteBuf>) source)
.flatMap(b -> {
if (markSentHeaderAndBody(b)) {
HttpMessage msg;
if (HttpUtil.getContentLength(outboundHttpMessage(), -1) == 0 ||
isContentAlwaysEmpty()) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Dropped HTTP content, since response has " +
"1. [Content-Length: 0] or 2. there must be no content: {}"), b);
}
b.release();
msg = newFullBodyMessage(Unpooled.EMPTY_BUFFER);
}
else {
msg = newFullBodyMessage(b);
}
HttpMessage msg = prepareHttpMessage(b);

try {
afterMarkSentHeaders();
Expand Down Expand Up @@ -161,18 +149,7 @@ public NettyOutbound sendObject(Object message) {
ByteBuf b = (ByteBuf) message;
return new PostHeadersNettyOutbound(FutureMono.deferFuture(() -> {
if (markSentHeaderAndBody(b)) {
HttpMessage msg;
if (HttpUtil.getContentLength(outboundHttpMessage(), -1) == 0) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Dropped HTTP content, " +
"since response has Content-Length: 0 {}"), b);
}
b.release();
msg = newFullBodyMessage(Unpooled.EMPTY_BUFFER);
}
else {
msg = newFullBodyMessage(b);
}
HttpMessage msg = prepareHttpMessage(b);

try {
afterMarkSentHeaders();
Expand Down Expand Up @@ -437,6 +414,24 @@ else if (!SCHEME_PATTERN.matcher(tempUri).matches()) {
*/
protected abstract HttpMessage outboundHttpMessage();

HttpMessage prepareHttpMessage(ByteBuf buffer) {
HttpMessage msg;
if (HttpUtil.getContentLength(outboundHttpMessage(), -1) == 0 ||
isContentAlwaysEmpty()) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Dropped HTTP content, since response has " +
"1. [Content-Length: 0] or 2. there must be no content: {}"), buffer);
}
buffer.release();
msg = newFullBodyMessage(Unpooled.EMPTY_BUFFER);
}
else {
msg = newFullBodyMessage(buffer);
}

return msg;
}

@SuppressWarnings("rawtypes")
final static AtomicIntegerFieldUpdater<HttpOperations> HTTP_STATE =
AtomicIntegerFieldUpdater.newUpdater(HttpOperations.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,12 @@ protected void beforeMarkSentHeaders() {
@Override
protected boolean isContentAlwaysEmpty() {
int code = status().code();
return HttpResponseStatus.NOT_MODIFIED.code() == code ||
HttpResponseStatus.NO_CONTENT.code() == code ||
if (HttpResponseStatus.NOT_MODIFIED.code() == code) {
responseHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING)
.remove(HttpHeaderNames.CONTENT_LENGTH);
return true;
}
return HttpResponseStatus.NO_CONTENT.code() == code ||
HttpResponseStatus.RESET_CONTENT.code() == code;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package reactor.netty.http;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
Expand All @@ -33,6 +34,7 @@
import reactor.netty.http.server.HttpServer;
import reactor.test.StepVerifier;

import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Arrays;
import java.util.stream.Stream;
Expand Down Expand Up @@ -102,9 +104,17 @@ void noContentStatusCodes(HttpProtocol[] serverProtocols, HttpProtocol[] clientP
.get("/304-3", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED)
.sendString(Mono.just("/304-3")))
.get("/304-4", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED)
.sendString(Flux.just("/", "304-4")))
.header(HttpHeaderNames.CONTENT_LENGTH, "6")
.sendString(Mono.just("/304-4")))
.get("/304-5", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED)
.send()))
.sendString(Flux.just("/", "304-5")))
.get("/304-6", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED)
.send())
.get("/304-7", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED)
.sendObject(Unpooled.wrappedBuffer("/304-7".getBytes(Charset.defaultCharset()))))
.get("/304-8", (req, res) -> res.status(HttpResponseStatus.NOT_MODIFIED)
.header(HttpHeaderNames.CONTENT_LENGTH, "6")
.sendObject(Unpooled.wrappedBuffer("/304-8".getBytes(Charset.defaultCharset())))))
.bindNow();

HttpClient client = createClient(disposableServer::address).protocol(clientProtocols);
Expand All @@ -130,6 +140,9 @@ void noContentStatusCodes(HttpProtocol[] serverProtocols, HttpProtocol[] clientP
checkResponse("/304-3", client);
checkResponse("/304-4", client);
checkResponse("/304-5", client);
checkResponse("/304-6", client);
checkResponse("/304-7", client);
checkResponse("/304-8", client);
}

static void checkResponse(String url, HttpClient client) {
Expand Down

0 comments on commit 32459b2

Please sign in to comment.