Skip to content

Commit

Permalink
Reset trailers on recycled response
Browse files Browse the repository at this point in the history
Whilst investigating #4711 for jetty-10, it was noticed that trailers are not nulled on recycled Response instances, nor on reset.

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Mar 25, 2020
1 parent e913ed2 commit f4da976
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Expand Up @@ -132,6 +132,7 @@ protected void recycle()
_out.recycle();
_fields.clear();
_encodingFrom = EncodingFrom.NOT_SET;
_trailers = null;
}

public HttpOutput getHttpOutput()
Expand Down Expand Up @@ -1080,6 +1081,7 @@ public void reset()
_mimeType = null;
_characterEncoding = null;
_encodingFrom = EncodingFrom.NOT_SET;
_trailers = null;

// Clear all response headers
_fields.clear();
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -165,6 +166,7 @@ public void testResponseTrailersWithContent(Transport transport) throws Exceptio

private void testResponseTrailers(byte[] content) throws Exception
{
final AtomicBoolean once = new AtomicBoolean(false);
String trailerName = "Trailer";
String trailerValue = "value";
scenario.start(new AbstractHandler()
Expand All @@ -173,12 +175,15 @@ private void testResponseTrailers(byte[] content) throws Exception
public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
jettyRequest.setHandled(true);
Response jettyResponse = (Response)response;

HttpFields trailers = new HttpFields();
trailers.put(trailerName, trailerValue);
if (once.compareAndSet(false, true))
{
HttpFields trailers = new HttpFields();
trailers.put(trailerName, trailerValue);
jettyResponse.setTrailers(() -> trailers);
}

Response jettyResponse = (Response)response;
jettyResponse.setTrailers(() -> trailers);
if (content != null)
response.getOutputStream().write(content);
}
Expand All @@ -205,6 +210,26 @@ public void handle(String target, Request jettyRequest, HttpServletRequest reque
.send();
assertEquals(HttpStatus.OK_200, response.getStatus());
assertNull(failure.get());

// subsequent requests should not have trailers
response = scenario.client.newRequest(scenario.newURI())
.onResponseSuccess(r ->
{
try
{
HttpResponse httpResponse = (HttpResponse)r;
assertNull(httpResponse.getTrailers());
failure.set(null);
}
catch (Throwable x)
{
failure.set(x);
}
})
.timeout(5, TimeUnit.SECONDS)
.send();
assertEquals(HttpStatus.OK_200, response.getStatus());
assertNull(failure.get());
}

@ParameterizedTest
Expand Down

0 comments on commit f4da976

Please sign in to comment.