Skip to content

Commit

Permalink
Jetty 12 content length 0 take3 (#9740)
Browse files Browse the repository at this point in the history
* Optimize Content-Length: 0 handling

Create and use a pre-encoded HttpFields.CONTENT_LENGTH_0 constant
Used the constant in more places
renamed the `putLongField` methods of HttpFields.Mutable to just `put`
Fixed wrong docs examples
  • Loading branch information
gregw authored May 7, 2023
1 parent e5dea25 commit 4c16e6a
Show file tree
Hide file tree
Showing 49 changed files with 271 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class HelloHandler0 extends Handler.Abstract
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain");
response.write(true, BufferUtil.toBuffer("Hello World\n"), callback);
return true;
}
Expand All @@ -54,7 +54,7 @@ public static class HelloHandler1 extends Handler.Abstract
public boolean handle(Request request, Response response, Callback callback)
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain");
response.write(true, BufferUtil.toBuffer("Hello World\n"), callback);
return true;
}
Expand All @@ -66,7 +66,7 @@ public static class HelloHandler2 extends Handler.Abstract.NonBlocking
public boolean handle(Request request, Response response, Callback callback)
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain");
response.write(true, BufferUtil.toBuffer("Hello World\n"), callback);
return true;
}
Expand All @@ -78,7 +78,7 @@ public static class HelloHandler3 extends Handler.Abstract.NonBlocking
public boolean handle(Request request, Response response, Callback callback) throws IOException
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain");
Blocker.Shared blocker = new Blocker.Shared();
try (Blocker.Callback cb = blocker.callback())
{
Expand All @@ -101,7 +101,7 @@ public static class HelloHandler4 extends Handler.Abstract
public boolean handle(Request request, Response response, Callback callback) throws IOException
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain");
try (PrintStream out = new PrintStream(Content.Sink.asOutputStream(response)))
{
out.print("Hello ");
Expand All @@ -122,7 +122,7 @@ public static class HelloHandler5 extends Handler.Abstract.NonBlocking
public boolean handle(Request request, Response response, Callback callback) throws IOException
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain");
new HelloWorldPublisher().subscribe(Content.Sink.asSubscriber(response, callback));
return true;
}
Expand Down Expand Up @@ -166,7 +166,7 @@ public boolean handle(Request request, Response response, Callback callback)
return false;

response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain");
response.write(true, BufferUtil.toBuffer("Hello World\n"), callback);
return true;
}
Expand All @@ -182,7 +182,7 @@ public boolean handle(Request request, Response response, Callback callback)

long contentLength = request.getHeaders().getLongField(HttpHeader.CONTENT_LENGTH);
if (contentLength >= 0)
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, contentLength);
response.getHeaders().put(HttpHeader.CONTENT_LENGTH, contentLength);

Content.copy(request, response, callback);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected SendFailure send(HttpChannel channel, HttpExchange exchange)
// the request is associated to the channel and sent.
// Use a counter to support multiplexed requests.
boolean send;
try (AutoLock l = lock.lock())
try (AutoLock ignored = lock.lock())
{
send = idleTimeoutGuard >= 0;
if (send)
Expand All @@ -122,7 +122,7 @@ protected SendFailure send(HttpChannel channel, HttpExchange exchange)
result = new SendFailure(new HttpRequestException("Could not associate request to connection", request), false);
}

try (AutoLock l = lock.lock())
try (AutoLock ignored = lock.lock())
{
--idleTimeoutGuard;
idleTimeoutNanoTime = NanoTime.now();
Expand Down Expand Up @@ -271,7 +271,7 @@ private void applyProxyAuthentication(Request request, ProxyConfiguration.Proxy

public boolean onIdleTimeout(long idleTimeout, Throwable failure)
{
try (AutoLock l = lock.lock())
try (AutoLock ignored = lock.lock())
{
if (idleTimeoutGuard == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jetty.client.transport.HttpDestination;
import org.eclipse.jetty.client.transport.HttpResponse;
import org.eclipse.jetty.client.transport.internal.HttpConnectionOverHTTP;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpStatus;
Expand Down Expand Up @@ -52,7 +53,7 @@ public boolean handle(Request request, Response response, Callback callback) thr
{
Content.Source.consumeAll(request);

response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, data.length);
response.getHeaders().put(HttpHeader.CONTENT_LENGTH, data.length);
response.write(true, ByteBuffer.wrap(data), callback);

try
Expand Down Expand Up @@ -206,7 +207,7 @@ public void testClientConnectionCloseServerNoConnectionCloseClientCloses(Scenari
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 0);
response.getHeaders().put(HttpFields.CONTENT_LENGTH_0);
Content.Sink.write(response, false, null);

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected void service(org.eclipse.jetty.server.Request request, Response respon
long contentLength = request.getHeaders().getLongField("X-Download");
if (contentLength > 0)
{
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, contentLength);
response.getHeaders().put(HttpHeader.CONTENT_LENGTH, contentLength);
try (Blocker.Callback callback = _blocking.callback())
{
response.write(true, ByteBuffer.allocate((int)contentLength), callback);
Expand All @@ -163,7 +163,7 @@ protected void service(org.eclipse.jetty.server.Request request, Response respon
{
long contentLength = request.getLength();
if (contentLength > 0)
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, contentLength);
response.getHeaders().put(HttpHeader.CONTENT_LENGTH, contentLength);
while (true)
{
Content.Chunk chunk = request.read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.eclipse.jetty.http.HttpCookie;
import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpMethod;
Expand Down Expand Up @@ -553,7 +554,7 @@ public void testExchangeIsCompleteOnlyWhenBothRequestAndResponseAreComplete(Scen
@Override
protected void service(org.eclipse.jetty.server.Request request, org.eclipse.jetty.server.Response response) throws Throwable
{
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 0);
response.getHeaders().put(HttpFields.CONTENT_LENGTH_0);
response.setStatus(HttpStatus.OK_200);
Content.Sink.write(response, true, null);
Content.Source.consumeAll(request);
Expand Down Expand Up @@ -1163,7 +1164,7 @@ public boolean handle(org.eclipse.jetty.server.Request request, org.eclipse.jett
{
// Send the headers at this point, then write the content.
byte[] content = "TEST".getBytes(UTF_8);
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, content.length);
response.getHeaders().put(HttpHeader.CONTENT_LENGTH, content.length);
Content.Sink.write(response, false, null);
response.write(true, ByteBuffer.wrap(content), callback);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpStatus;
Expand Down Expand Up @@ -73,15 +74,15 @@ protected void service(org.eclipse.jetty.server.Request request, Response respon
if (org.eclipse.jetty.server.Request.getPathInContext(request).endsWith("/redirect"))
{
response.setStatus(HttpStatus.TEMPORARY_REDIRECT_307);
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 0);
response.getHeaders().put(HttpFields.CONTENT_LENGTH_0);
response.getHeaders().put(HttpHeader.LOCATION, scenario.getScheme() + "://localhost:" + connector.getLocalPort() + "/");
Content.Sink.write(response, false, null);
request.getConnectionMetaData().getConnection().getEndPoint().shutdownOutput();
}
else
{
response.setStatus(HttpStatus.OK_200);
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 0);
response.getHeaders().put(HttpFields.CONTENT_LENGTH_0);
response.getHeaders().put(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString());
}
}
Expand All @@ -105,7 +106,7 @@ public void testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConne
protected void service(org.eclipse.jetty.server.Request request, Response response)
{
response.setStatus(HttpStatus.OK_200);
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 0);
response.getHeaders().put(HttpFields.CONTENT_LENGTH_0);
response.getHeaders().put(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString());
}
});
Expand All @@ -121,7 +122,7 @@ public void testServerClosesConnectionAfterResponseWithQueuedRequestWithMaxConne
protected void service(org.eclipse.jetty.server.Request request, Response response) throws Throwable
{
response.setStatus(HttpStatus.OK_200);
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 0);
response.getHeaders().put(HttpFields.CONTENT_LENGTH_0);
Content.Sink.write(response, false, null);
request.getConnectionMetaData().getConnection().getEndPoint().shutdownOutput();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public boolean handle(Request request, Response response, Callback callback)
{
assertNotEquals(proxyContext.getContextPath(), request.getContext().getContextPath());
assertEquals(path, Request.getPathInContext(request));
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, data.length);
response.getHeaders().put(HttpHeader.CONTENT_LENGTH, data.length);
response.write(true, ByteBuffer.wrap(data), callback);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public boolean handle(org.eclipse.jetty.server.Request request, org.eclipse.jett
// Setting the Content-Length triggers the HTTP
// content mode for response content parsing,
// otherwise the RAW content mode is used.
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, data.length);
response.getHeaders().put(HttpHeader.CONTENT_LENGTH, data.length);
response.write(true, ByteBuffer.wrap(data), callback);
return true;
}
Expand Down Expand Up @@ -564,7 +564,7 @@ public void testEarlyEOF() throws Exception
public boolean handle(org.eclipse.jetty.server.Request request, org.eclipse.jetty.server.Response response, Callback callback) throws Exception
{
// Promise some content, then flush the headers, then fail to send the content.
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, 16);
response.getHeaders().put(HttpHeader.CONTENT_LENGTH, 16);
Content.Sink.write(response, false, null);
throw new NullPointerException("Explicitly thrown by test");
}
Expand Down
Loading

0 comments on commit 4c16e6a

Please sign in to comment.