From b867cfc1b900280b0120d2cbb428e70c1e01ac3e Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 22 May 2023 07:25:12 -0500 Subject: [PATCH] Issue #8885 - Restore HttpChannel.Listener --- .../jetty/server/HttpChannelListeners.java | 4 ++-- .../jetty/server/internal/HttpChannelState.java | 17 +++++++++-------- .../jetty/server/HttpChannelEventTest.java | 2 -- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelListeners.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelListeners.java index 099b4d0a568e..608820d985df 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelListeners.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannelListeners.java @@ -191,7 +191,7 @@ public void onResponseCommitted(Request request, int status, HttpFields response { try { - onResponseCommittedHandle.invoke(request, response); + onResponseCommittedHandle.invoke(request, status, response); } catch (Throwable ignore) { @@ -205,7 +205,7 @@ public void onResponseWrite(Request request, boolean last, ByteBuffer content, T { try { - onResponseWriteHandle.invoke(request, last, content); + onResponseWriteHandle.invoke(request, last, content, failure); } catch (Throwable ignore) { diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java index 8a7d58b85c62..7457dc01f169 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpChannelState.java @@ -266,7 +266,7 @@ public Runnable onRequest(MetaData.Request request) if (_request != null) throw new IllegalStateException("duplicate request"); _request = new ChannelRequest(this, request); - _response = new ChannelResponse(_request); + _response = new ChannelResponse(this, _request); HttpFields.Mutable responseHeaders = _response.getHeaders(); if (getHttpConfiguration().getSendServerVersion()) @@ -354,7 +354,7 @@ public Runnable onFailure(Throwable x) // If the channel doesn't have a request, then the error must have occurred during the parsing of // the request line / headers, so make a temp request for logging and producing an error response. _request = new ChannelRequest(this, ERROR_REQUEST); - _response = new ChannelResponse(_request); + _response = new ChannelResponse(this, _request); } // Set the error to arrange for any subsequent reads, demands or writes to fail. @@ -545,6 +545,7 @@ public void run() Server server = _connectionMetaData.getConnector().getServer(); boolean handled = false; + Throwable failure = null; try { if (!HttpMethod.PRI.is(request.getMethod()) && @@ -586,11 +587,12 @@ public void run() catch (Throwable t) { request._callback.failed(t); + failure = t; } + _combinedListener.onAfterHandling(request, handled, failure); HttpStream stream; - Throwable failure; boolean completeStream; boolean callbackCompleted; boolean lastStreamSendComplete; @@ -716,8 +718,7 @@ public static class ChannelRequest implements Attributes, Request _id = httpChannelState.getHttpStream().getId(); // Copy ID now, as stream will ultimately be nulled _connectionMetaData = httpChannelState.getConnectionMetaData(); _metaData = Objects.requireNonNull(metaData); - _listener = httpChannel._combinedListener; - _response = new ChannelResponse(this, _listener); + _listener = httpChannelState._combinedListener; _lock = httpChannelState._lock; } @@ -1034,10 +1035,10 @@ public static class ChannelResponse implements Response, Callback private Callback _writeCallback; protected boolean _errorMode; - private ChannelResponse(ChannelRequest request, HttpChannel.Listener listener) + private ChannelResponse(HttpChannelState httpChannelState, ChannelRequest request) { _request = request; - _listener = listener; + _listener = httpChannelState._combinedListener; } private void lockedPrepareErrorResponse() @@ -1208,7 +1209,7 @@ public void failed(Throwable x) } Throwable t = failure; - httpChannel._serializedInvoker.run(() -> + httpChannelState._serializedInvoker.run(() -> { _listener.onResponseWrite(_request, last, content.slice(), t); callback.succeeded(); diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelEventTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelEventTest.java index f689c0f662e9..bbbbdae42186 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelEventTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/HttpChannelEventTest.java @@ -82,7 +82,6 @@ public void dispose() Foo, Bar This is the Request, This is the Response """) - @Disabled("re-enable once PR #9684 is merged") public void testRequestReadResponseWrite(String requestBody, String expectedResponseBody) throws Exception { start(new Handler.Abstract() @@ -250,7 +249,6 @@ public void onRequestRead(Request request, Content.Chunk chunk) } @Test - @Disabled("re-enable once PR #9684 is merged") public void testResponseContentSlice() throws Exception { byte[] data = new byte[]{'y'};