Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #6328 - avoid binding WebSocket MethodHandles #8087

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,7 @@ public void testGzip() throws Exception

rawResponse = connector.getResponse("GET /context/data0.txt HTTP/1.0\r\nHost:localhost:8080\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
System.err.println(response);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response, containsHeaderValue(HttpHeader.CONTENT_LENGTH, "12"));
assertThat(response, containsHeaderValue(HttpHeader.CONTENT_TYPE, "text/plain"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@

package org.eclipse.jetty.websocket.core.internal.messages;

import java.lang.invoke.MethodHandle;
import java.util.Objects;

import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

public abstract class AbstractMessageSink implements MessageSink
{
protected final CoreSession session;
protected final MethodHandle methodHandle;
protected final MethodHolder methodHolder;

public AbstractMessageSink(CoreSession session, MethodHandle methodHandle)
public AbstractMessageSink(CoreSession session, MethodHolder methodHolder)
{
this.session = Objects.requireNonNull(session, "CoreSession");
this.methodHandle = Objects.requireNonNull(methodHandle, "MethodHandle");
this.methodHolder = Objects.requireNonNull(methodHolder, "MethodHolder");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,24 @@

package org.eclipse.jetty.websocket.core.internal.messages;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;

import org.eclipse.jetty.io.ByteBufferCallbackAccumulator;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.exception.InvalidSignatureException;
import org.eclipse.jetty.websocket.core.exception.MessageTooLargeException;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

public class ByteArrayMessageSink extends AbstractMessageSink
{
private static final byte[] EMPTY_BUFFER = new byte[0];
private ByteBufferCallbackAccumulator out;

public ByteArrayMessageSink(CoreSession session, MethodHandle methodHandle)
public ByteArrayMessageSink(CoreSession session, MethodHolder methodHolder)
{
super(session, methodHandle);

// This uses the offset length byte array signature not supported by javax websocket.
// The javax layer instead uses decoders for whole byte array messages instead of this message sink.
MethodType onMessageType = MethodType.methodType(Void.TYPE, byte[].class, int.class, int.class);
if (methodHandle.type().changeReturnType(void.class) != onMessageType.changeReturnType(void.class))
{
throw InvalidSignatureException.build(onMessageType, methodHandle.type());
}
super(session, methodHolder);
}

@Override
Expand All @@ -62,10 +52,10 @@ public void accept(Frame frame, Callback callback)
if (frame.hasPayload())
{
byte[] buf = BufferUtil.toArray(frame.getPayload());
methodHandle.invoke(buf, 0, buf.length);
methodHolder.invoke(buf, 0, buf.length);
}
else
methodHandle.invoke(EMPTY_BUFFER, 0, 0);
methodHolder.invoke(EMPTY_BUFFER, 0, 0);

callback.succeeded();
session.demand(1);
Expand All @@ -86,7 +76,7 @@ public void accept(Frame frame, Callback callback)
if (frame.isFin())
{
byte[] buf = out.takeByteArray();
methodHandle.invoke(buf, 0, buf.length);
methodHolder.invoke(buf, 0, buf.length);
}

session.demand(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,24 @@

package org.eclipse.jetty.websocket.core.internal.messages;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;
import java.util.Objects;

import org.eclipse.jetty.io.ByteBufferCallbackAccumulator;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.exception.InvalidSignatureException;
import org.eclipse.jetty.websocket.core.exception.MessageTooLargeException;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

public class ByteBufferMessageSink extends AbstractMessageSink
{
private ByteBufferCallbackAccumulator out;

public ByteBufferMessageSink(CoreSession session, MethodHandle methodHandle)
public ByteBufferMessageSink(CoreSession session, MethodHolder methodHolder)
{
super(session, methodHandle);

// Validate onMessageMethod
Objects.requireNonNull(methodHandle, "MethodHandle");
MethodType onMessageType = MethodType.methodType(Void.TYPE, ByteBuffer.class);
if (methodHandle.type() != onMessageType)
{
throw InvalidSignatureException.build(onMessageType, methodHandle.type());
}
super(session, methodHolder);
}

@Override
Expand All @@ -61,9 +50,9 @@ public void accept(Frame frame, Callback callback)
if (frame.isFin() && (out == null))
{
if (frame.hasPayload())
methodHandle.invoke(frame.getPayload());
methodHolder.invoke(frame.getPayload());
else
methodHandle.invoke(BufferUtil.EMPTY_BUFFER);
methodHolder.invoke(BufferUtil.EMPTY_BUFFER);

callback.succeeded();
session.demand(1);
Expand All @@ -89,7 +78,7 @@ public void accept(Frame frame, Callback callback)

try
{
methodHandle.invoke(buffer);
methodHolder.invoke(buffer);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
package org.eclipse.jetty.websocket.core.internal.messages;

import java.io.Closeable;
import java.lang.invoke.MethodHandle;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

/**
* Centralized logic for Dispatched Message Handling.
Expand Down Expand Up @@ -98,9 +98,9 @@ public abstract class DispatchedMessageSink extends AbstractMessageSink
private MessageSink typeSink;
private final Executor executor;

public DispatchedMessageSink(CoreSession session, MethodHandle methodHandle)
public DispatchedMessageSink(CoreSession session, MethodHolder methodHolder)
{
super(session, methodHandle);
super(session, methodHolder);
executor = session.getWebSocketComponents().getExecutor();
}

Expand All @@ -119,7 +119,7 @@ public void accept(Frame frame, final Callback callback)
{
try
{
methodHandle.invoke(typeSink);
methodHolder.invoke(typeSink);
if (typeSink instanceof Closeable)
IO.close((Closeable)typeSink);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

package org.eclipse.jetty.websocket.core.internal.messages;

import java.lang.invoke.MethodHandle;

import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

public class InputStreamMessageSink extends DispatchedMessageSink
{
public InputStreamMessageSink(CoreSession session, MethodHandle methodHandle)
public InputStreamMessageSink(CoreSession session, MethodHolder methodHolder)
{
super(session, methodHandle);
super(session, methodHolder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@

package org.eclipse.jetty.websocket.core.internal.messages;

import java.lang.invoke.MethodHandle;

import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

public class PartialByteArrayMessageSink extends AbstractMessageSink
{
private static final byte[] EMPTY_BUFFER = new byte[0];

public PartialByteArrayMessageSink(CoreSession session, MethodHandle methodHandle)
public PartialByteArrayMessageSink(CoreSession session, MethodHolder methodHolder)
{
super(session, methodHandle);
super(session, methodHolder);
}

@Override
Expand All @@ -37,7 +36,7 @@ public void accept(Frame frame, Callback callback)
if (frame.hasPayload() || frame.isFin())
{
byte[] buffer = frame.hasPayload() ? BufferUtil.toArray(frame.getPayload()) : EMPTY_BUFFER;
methodHandle.invoke(buffer, frame.isFin());
methodHolder.invoke(buffer, frame.isFin());
}

callback.succeeded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@

package org.eclipse.jetty.websocket.core.internal.messages;

import java.lang.invoke.MethodHandle;

import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

public class PartialByteBufferMessageSink extends AbstractMessageSink
{
public PartialByteBufferMessageSink(CoreSession session, MethodHandle methodHandle)
public PartialByteBufferMessageSink(CoreSession session, MethodHolder methodHolder)
{
super(session, methodHandle);
super(session, methodHolder);
}

@Override
Expand All @@ -32,7 +31,7 @@ public void accept(Frame frame, Callback callback)
try
{
if (frame.hasPayload() || frame.isFin())
methodHandle.invoke(frame.getPayload(), frame.isFin());
methodHolder.invoke(frame.getPayload(), frame.isFin());

callback.succeeded();
session.demand(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@

package org.eclipse.jetty.websocket.core.internal.messages;

import java.lang.invoke.MethodHandle;
import java.util.Objects;

import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

public class PartialStringMessageSink extends AbstractMessageSink
{
private Utf8StringBuilder out;

public PartialStringMessageSink(CoreSession session, MethodHandle methodHandle)
public PartialStringMessageSink(CoreSession session, MethodHolder methodHolder)
{
super(session, methodHandle);
Objects.requireNonNull(methodHandle, "MethodHandle");
super(session, methodHolder);
}

@Override
Expand All @@ -42,12 +39,12 @@ public void accept(Frame frame, Callback callback)
out.append(frame.getPayload());
if (frame.isFin())
{
methodHandle.invoke(out.toString(), true);
methodHolder.invoke(out.toString(), true);
out = null;
}
else
{
methodHandle.invoke(out.takePartialString(), false);
methodHolder.invoke(out.takePartialString(), false);
}

callback.succeeded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

package org.eclipse.jetty.websocket.core.internal.messages;

import java.lang.invoke.MethodHandle;

import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

public class ReaderMessageSink extends DispatchedMessageSink
{
public ReaderMessageSink(CoreSession session, MethodHandle methodHandle)
public ReaderMessageSink(CoreSession session, MethodHolder methodHolder)
{
super(session, methodHandle);
super(session, methodHolder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@

package org.eclipse.jetty.websocket.core.internal.messages;

import java.lang.invoke.MethodHandle;

import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.eclipse.jetty.websocket.core.CoreSession;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.exception.MessageTooLargeException;
import org.eclipse.jetty.websocket.core.internal.util.MethodHolder;

public class StringMessageSink extends AbstractMessageSink
{
private Utf8StringBuilder out;
private int size;

public StringMessageSink(CoreSession session, MethodHandle methodHandle)
public StringMessageSink(CoreSession session, MethodHolder methodHolder)
{
super(session, methodHandle);
super(session, methodHolder);
this.size = 0;
}

Expand All @@ -50,7 +49,7 @@ public void accept(Frame frame, Callback callback)

out.append(frame.getPayload());
if (frame.isFin())
methodHandle.invoke(out.toString());
methodHolder.invoke(out.toString());

callback.succeeded();
session.demand(1);
Expand Down
Loading