Jetty 12 simplify EchoHandler (#9783)

This commit is contained in:
Greg Wilkins 2023-05-18 09:50:09 +02:00 committed by GitHub
parent 068a60a868
commit 18c4846b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 20 deletions

View File

@ -29,6 +29,7 @@ import java.util.Spliterators;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -41,7 +42,7 @@ import java.util.stream.StreamSupport;
* To implement this interface, only the {@link #iterator()} method
* need be implemented, however default implementations may be inefficient
*/
public interface HttpFields extends Iterable<HttpField>
public interface HttpFields extends Iterable<HttpField>, Supplier<HttpFields>
{
HttpField EXPIRES_01JAN1970 = new PreEncodedHttpField(HttpHeader.EXPIRES, DateGenerator.__01Jan1970);
HttpField CONNECTION_CLOSE = new PreEncodedHttpField(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString());
@ -90,6 +91,16 @@ public interface HttpFields extends Iterable<HttpField>
return HttpFields.build(this).asImmutable();
}
/**
* <p>Supplies this instance, typically used to supply HTTP trailers.</p>
* @return this instance
*/
@Override
default HttpFields get()
{
return this;
}
/**
* Efficiently take the fields as an Immutable that cannot be changed by any further mutations
* to this instance.

View File

@ -212,7 +212,7 @@ public abstract class ConnectorTimeoutTest extends HttpServerTestFixture
startServer(new EchoHandler()
{
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
public boolean handle(Request request, Response response, Callback callback)
{
try
{

View File

@ -1257,7 +1257,7 @@ public class HttpChannelTest
EchoHandler echoHandler = new EchoHandler()
{
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
public boolean handle(Request request, Response response, Callback callback)
{
request.addHttpStreamWrapper(s -> new HttpStream.Wrapper(s)
{

View File

@ -1872,7 +1872,7 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture
}
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
public boolean handle(Request request, Response response, Callback callback)
{
AtomicBoolean hasContent = new AtomicBoolean();
Request.Wrapper wrapper = new Request.Wrapper(request)

View File

@ -13,14 +13,13 @@
package org.eclipse.jetty.server.handler;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.StringUtil;
/**
* Dump request handler.
@ -32,26 +31,32 @@ public class EchoHandler extends Handler.Abstract.NonBlocking
public EchoHandler()
{
}
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
public boolean handle(Request request, Response response, Callback callback)
{
response.setStatus(200);
String contentType = request.getHeaders().get(HttpHeader.CONTENT_TYPE);
if (StringUtil.isNotBlank(contentType))
response.getHeaders().put(HttpHeader.CONTENT_TYPE, contentType);
if (request.getHeaders().contains(HttpHeader.TRAILER))
long contentLength = -1;
for (HttpField field : request.getHeaders())
{
HttpFields.Mutable responseTrailers = HttpFields.build();
response.setTrailersSupplier(() -> responseTrailers);
if (field.getHeader() != null)
{
switch (field.getHeader())
{
case CONTENT_LENGTH ->
{
response.getHeaders().add(field);
contentLength = field.getLongValue();
}
case CONTENT_TYPE -> response.getHeaders().add(field);
case TRAILER -> response.setTrailersSupplier(HttpFields.build());
case TRANSFER_ENCODING -> contentLength = Long.MAX_VALUE;
}
}
}
long contentLength = request.getHeaders().getLongField(HttpHeader.CONTENT_LENGTH);
if (contentLength >= 0)
response.getHeaders().put(HttpHeader.CONTENT_LENGTH, contentLength);
if (contentLength > 0 || contentLength == -1 && request.getHeaders().contains(HttpHeader.TRANSFER_ENCODING))
if (contentLength > 0)
Content.copy(request, response, Response.newTrailersChunkProcessor(response), callback);
else
callback.succeeded();

View File

@ -248,7 +248,7 @@ public class SslContextFactoryReloadTest
private static class TestHandler extends EchoHandler
{
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
public boolean handle(Request request, Response response, Callback callback)
{
if (HttpMethod.POST.is(request.getMethod()))
return super.handle(request, response, callback);