Skip to content

Commit

Permalink
Issue #8885 - Restore HttpChannel.Listener
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed May 23, 2023
1 parent 1a10be7 commit c77c2fb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jetty.server;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandleInfo;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -115,21 +116,38 @@ public void set(Collection<HttpChannel.Listener> listeners)
if (listeners == null)
return;

for (HttpChannel.Listener listener : listeners)
try
{
onHandlingBeforeHandle = MethodHandles.foldArguments(onHandlingBeforeHandle, LISTENER_HANDLER_ON_HANDLING_BEFORE.bindTo(listener));
onHandlingAfterHandle = MethodHandles.foldArguments(onHandlingAfterHandle, LISTENER_HANDLER_ON_HANDLING_AFTER.bindTo(listener));

onRequestBeginHandle = MethodHandles.foldArguments(onRequestBeginHandle, LISTENER_HANDLER_ON_REQUEST_BEGIN.bindTo(listener));
onRequestReadHandle = MethodHandles.foldArguments(onRequestReadHandle, LISTENER_HANDLER_ON_REQUEST_READ.bindTo(listener));

onResponseCommittedHandle = MethodHandles.foldArguments(onResponseCommittedHandle, LISTENER_HANDLER_ON_RESPONSE_COMMITTED.bindTo(listener));
onResponseWriteHandle = MethodHandles.foldArguments(onResponseWriteHandle, LISTENER_HANDLER_ON_RESPONSE_WRITE.bindTo(listener));

onCompleteHandle = MethodHandles.foldArguments(onCompleteHandle, LISTENER_HANDLER_ON_COMPLETE.bindTo(listener));
for (HttpChannel.Listener listener : listeners)
{
if (notDefault(LISTENER_HANDLER_ON_HANDLING_BEFORE, listener))
onHandlingBeforeHandle = MethodHandles.foldArguments(onHandlingBeforeHandle, LISTENER_HANDLER_ON_HANDLING_BEFORE.bindTo(listener));
if (notDefault(LISTENER_HANDLER_ON_HANDLING_AFTER, listener))
onHandlingAfterHandle = MethodHandles.foldArguments(onHandlingAfterHandle, LISTENER_HANDLER_ON_HANDLING_AFTER.bindTo(listener));
if (notDefault(LISTENER_HANDLER_ON_REQUEST_BEGIN, listener))
onRequestBeginHandle = MethodHandles.foldArguments(onRequestBeginHandle, LISTENER_HANDLER_ON_REQUEST_BEGIN.bindTo(listener));
if (notDefault(LISTENER_HANDLER_ON_REQUEST_READ, listener))
onRequestReadHandle = MethodHandles.foldArguments(onRequestReadHandle, LISTENER_HANDLER_ON_REQUEST_READ.bindTo(listener));
if (notDefault(LISTENER_HANDLER_ON_RESPONSE_COMMITTED, listener))
onResponseCommittedHandle = MethodHandles.foldArguments(onResponseCommittedHandle, LISTENER_HANDLER_ON_RESPONSE_COMMITTED.bindTo(listener));
if (notDefault(LISTENER_HANDLER_ON_RESPONSE_WRITE, listener))
onResponseWriteHandle = MethodHandles.foldArguments(onResponseWriteHandle, LISTENER_HANDLER_ON_RESPONSE_WRITE.bindTo(listener));
if (notDefault(LISTENER_HANDLER_ON_COMPLETE, listener))
onCompleteHandle = MethodHandles.foldArguments(onCompleteHandle, LISTENER_HANDLER_ON_COMPLETE.bindTo(listener));
}
}
catch (NoSuchMethodException e)
{
throw new RuntimeException("Invalid HttpChannel.Listener: " + listeners.getClass().getName(), e);
}
}

private boolean notDefault(MethodHandle methodHandle, HttpChannel.Listener listener) throws NoSuchMethodException
{
MethodHandleInfo methodHandleInfo = MethodHandles.lookup().in(listener.getClass()).revealDirect(methodHandle);
return !listener.getClass().getMethod(methodHandleInfo.getName(), methodHandleInfo.getMethodType().parameterArray()).isDefault();
}

@Override
public void onRequestBegin(Request request)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public void run()
Server server = _connectionMetaData.getConnector().getServer();

boolean handled = false;
Throwable failure = null;
Throwable thrownFailure = null;
try
{
if (!HttpMethod.PRI.is(request.getMethod()) &&
Expand Down Expand Up @@ -587,12 +587,13 @@ public void run()
catch (Throwable t)
{
request._callback.failed(t);
failure = t;
thrownFailure = t;
}

_combinedListener.onAfterHandling(request, handled, failure);
_combinedListener.onAfterHandling(request, handled, thrownFailure);

HttpStream stream;
Throwable failure;
boolean completeStream;
boolean callbackCompleted;
boolean lastStreamSendComplete;
Expand Down Expand Up @@ -1124,16 +1125,19 @@ public void setTrailersSupplier(Supplier<HttpFields> trailers)
@Override
public void write(boolean last, ByteBuffer content, Callback callback)
{
boolean isCommitted = isCommitted();
long length = BufferUtil.length(content);

long totalWritten;
HttpChannelState httpChannelState;
HttpStream stream = null;
Throwable failure = null;
MetaData.Response responseMetaData = null;
boolean isCommitted;

try (AutoLock ignored = _request._lock.lock())
{
httpChannelState = _request.lockedGetHttpChannelState();
isCommitted = isCommitted();
long committedContentLength = httpChannelState._committedContentLength;
totalWritten = _contentBytesWritten + length;
long contentLength = committedContentLength >= 0 ? committedContentLength : getHeaders().getLongField(HttpHeader.CONTENT_LENGTH);
Expand Down

0 comments on commit c77c2fb

Please sign in to comment.