diff --git a/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/HTTP2Session.java b/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/HTTP2Session.java index 80b0e22b88f..6a080a1652d 100644 --- a/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/HTTP2Session.java +++ b/jetty-http2/http2-common/src/main/java/org/eclipse/jetty/http2/HTTP2Session.java @@ -611,20 +611,9 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio * allocated stream id, or null if not interested in the modified headers frame * @param listener the listener that gets notified of stream events */ - public void newUpgradeStream(HeadersFrame frame, Stream.Listener listener, Promise promise) + public Stream newUpgradeStream(HeadersFrame frame, Stream.Listener listener, Consumer failFn) { - streamsState.newUpgradeStream(frame, listener, promise); -/* - // TODO: cannot do this, we need to call StreamsState. - HeadersFrame frame = frame; - int streamId = frame.getStreamId(); - if (streamId <= 0) - { - streamId = localStreamIds.getAndAdd(2); - frame = frame.withStreamId(streamId); - } - return createLocalStream(streamId, (MetaData.Request)frame.getMetaData()); - */ + return streamsState.newUpgradeStream(frame, listener, failFn); } protected IStream newStream(int streamId, MetaData.Request request, boolean local) @@ -790,7 +779,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio } } - protected IStream createLocalStream(int streamId, MetaData.Request request, Promise promise) + protected IStream createLocalStream(int streamId, MetaData.Request request, Consumer failFn) { while (true) { @@ -798,7 +787,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio int maxCount = getMaxLocalStreams(); if (maxCount >= 0 && localCount >= maxCount) { - promise.failed(new IllegalStateException("Max local stream count " + maxCount + " exceeded")); + failFn.accept(new IllegalStateException("Max local stream count " + maxCount + " exceeded")); return null; } if (localStreamCount.compareAndSet(localCount, localCount + 1)) @@ -817,7 +806,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio else { localStreamCount.decrementAndGet(); - promise.failed(new IllegalStateException("Duplicate stream " + streamId)); + failFn.accept(new IllegalStateException("Duplicate stream " + streamId)); return null; } } @@ -2110,7 +2099,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio } } - private void newUpgradeStream(HeadersFrame frame, Stream.Listener listener, Promise promise) + private Stream newUpgradeStream(HeadersFrame frame, Stream.Listener listener, Consumer failFn) { int streamId; try (AutoLock l = lock.lock()) @@ -2118,20 +2107,17 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio streamId = localStreamIds.getAndAdd(2); HTTP2Session.this.onStreamCreated(streamId); } - IStream stream = HTTP2Session.this.createLocalStream(streamId, (MetaData.Request)frame.getMetaData(), new Promise<>() + IStream stream = HTTP2Session.this.createLocalStream(streamId, (MetaData.Request)frame.getMetaData(), x -> { - @Override - public void failed(Throwable x) - { - HTTP2Session.this.onStreamDestroyed(streamId); - promise.failed(x); - } + HTTP2Session.this.onStreamDestroyed(streamId); + failFn.accept(x); }); if (stream != null) { stream.setListener(listener); stream.updateClose(frame.isEndStream(), CloseState.Event.AFTER_SEND); } + return stream; } private boolean newRemoteStream(int streamId) @@ -2180,7 +2166,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio MetaData.Request request = extractMetaDataRequest(frames.get(0)); if (request == null) return false; - IStream stream = HTTP2Session.this.createLocalStream(streamId, request, promise); + IStream stream = HTTP2Session.this.createLocalStream(streamId, request, promise::failed); if (stream == null) return false; diff --git a/jetty-http2/http2-http-client-transport/src/main/java/org/eclipse/jetty/http2/client/http/HttpConnectionOverHTTP2.java b/jetty-http2/http2-http-client-transport/src/main/java/org/eclipse/jetty/http2/client/http/HttpConnectionOverHTTP2.java index c469f592321..a1d00b1b97e 100644 --- a/jetty-http2/http2-http-client-transport/src/main/java/org/eclipse/jetty/http2/client/http/HttpConnectionOverHTTP2.java +++ b/jetty-http2/http2-http-client-transport/src/main/java/org/eclipse/jetty/http2/client/http/HttpConnectionOverHTTP2.java @@ -46,7 +46,6 @@ import org.eclipse.jetty.http2.api.Session; import org.eclipse.jetty.http2.api.Stream; import org.eclipse.jetty.http2.frames.HeadersFrame; import org.eclipse.jetty.util.Callback; -import org.eclipse.jetty.util.Promise; import org.eclipse.jetty.util.thread.Sweeper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -115,27 +114,21 @@ public class HttpConnectionOverHTTP2 extends HttpConnection implements Sweeper.S MetaData.Request metaData = new MetaData.Request(request.getMethod(), HttpURI.from(request.getURI()), HttpVersion.HTTP_2, request.getHeaders()); // We do not support upgrade requests with content, so endStream=true. HeadersFrame frame = new HeadersFrame(metaData, null, true); - ((HTTP2Session)session).newUpgradeStream(frame, http2Channel.getStreamListener(), new Promise<>() + Stream stream = ((HTTP2Session)session).newUpgradeStream(frame, http2Channel.getStreamListener(), failure -> { - @Override - public void succeeded(Stream stream) - { - http2Channel.setStream(stream); - newExchange.requestComplete(null); - newExchange.terminateRequest(); - if (LOG.isDebugEnabled()) - LOG.debug("Upgrade succeeded for {}", HttpConnectionOverHTTP2.this); - } - - @Override - public void failed(Throwable failure) - { - newExchange.requestComplete(failure); - newExchange.terminateRequest(); - if (LOG.isDebugEnabled()) - LOG.debug("Upgrade failed for {}", HttpConnectionOverHTTP2.this); - } + newExchange.requestComplete(failure); + newExchange.terminateRequest(); + if (LOG.isDebugEnabled()) + LOG.debug("Upgrade failed for {}", HttpConnectionOverHTTP2.this); }); + if (stream != null) + { + http2Channel.setStream(stream); + newExchange.requestComplete(null); + newExchange.terminateRequest(); + if (LOG.isDebugEnabled()) + LOG.debug("Upgrade succeeded for {}", HttpConnectionOverHTTP2.this); + } } @Override diff --git a/tests/test-http-client-transport/src/test/java/org/eclipse/jetty/http/client/HttpClientTransportDynamicTest.java b/tests/test-http-client-transport/src/test/java/org/eclipse/jetty/http/client/HttpClientTransportDynamicTest.java index 01b8d14eda6..aa2fc09b813 100644 --- a/tests/test-http-client-transport/src/test/java/org/eclipse/jetty/http/client/HttpClientTransportDynamicTest.java +++ b/tests/test-http-client-transport/src/test/java/org/eclipse/jetty/http/client/HttpClientTransportDynamicTest.java @@ -718,7 +718,7 @@ public class HttpClientTransportDynamicTest @Override protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) { - jettyRequest.getHttpChannel().getEndPoint().getConnection().close(); + jettyRequest.getHttpChannel().getEndPoint().close(); } }); ClientConnector clientConnector = new ClientConnector();