use of org.eclipse.jetty.http2.api.Session in project jetty.project by eclipse.
the class TrailersTest method testTrailersSentByClient.
@Test
public void testTrailersSentByClient() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
MetaData.Request request = (MetaData.Request) frame.getMetaData();
Assert.assertFalse(frame.isEndStream());
Assert.assertTrue(request.getFields().containsKey("X-Request"));
return new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
MetaData trailer = frame.getMetaData();
Assert.assertTrue(frame.isEndStream());
Assert.assertTrue(trailer.getFields().containsKey("X-Trailer"));
latch.countDown();
}
};
}
});
Session session = newClient(new Session.Listener.Adapter());
HttpFields requestFields = new HttpFields();
requestFields.put("X-Request", "true");
MetaData.Request request = newRequest("GET", requestFields);
HeadersFrame requestFrame = new HeadersFrame(request, null, false);
FuturePromise<Stream> streamPromise = new FuturePromise<>();
session.newStream(requestFrame, streamPromise, new Stream.Listener.Adapter());
Stream stream = streamPromise.get(5, TimeUnit.SECONDS);
// Send the trailers.
HttpFields trailerFields = new HttpFields();
trailerFields.put("X-Trailer", "true");
MetaData trailers = new MetaData(HttpVersion.HTTP_2, trailerFields);
HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailers, null, true);
stream.headers(trailerFrame, Callback.NOOP);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.http2.api.Session in project jetty.project by eclipse.
the class SimpleFlowControlStrategy method onDataConsumed.
@Override
public void onDataConsumed(ISession session, IStream stream, int length) {
if (length <= 0)
return;
// This is the simple algorithm for flow control.
// This method is called when a whole flow controlled frame has been consumed.
// We send a WindowUpdate every time, even if the frame was very small.
WindowUpdateFrame sessionFrame = new WindowUpdateFrame(0, length);
session.updateRecvWindow(length);
if (LOG.isDebugEnabled())
LOG.debug("Data consumed, increased session recv window by {} for {}", length, session);
Frame[] streamFrame = Frame.EMPTY_ARRAY;
if (stream != null) {
if (stream.isClosed()) {
if (LOG.isDebugEnabled())
LOG.debug("Data consumed, ignoring update stream recv window by {} for closed {}", length, stream);
} else {
streamFrame = new Frame[1];
streamFrame[0] = new WindowUpdateFrame(stream.getId(), length);
stream.updateRecvWindow(length);
if (LOG.isDebugEnabled())
LOG.debug("Data consumed, increased stream recv window by {} for {}", length, stream);
}
}
session.frames(stream, Callback.NOOP, sessionFrame, streamFrame);
}
use of org.eclipse.jetty.http2.api.Session in project be5 by DevelopmentOnTheEdge.
the class UserHelper method initGuest.
public void initGuest(Request req) {
Locale locale = Locale.US;
String remoteAddr = "";
Session session;
if (req != null) {
locale = req.getLocale();
remoteAddr = req.getRemoteAddr();
session = req.getSession();
} else {
session = new TestSession();
}
if (ModuleLoader2.getDevRoles().size() > 0) {
saveUser("dev", ModuleLoader2.getDevRoles(), ModuleLoader2.getDevRoles(), locale, remoteAddr, session);
} else {
List<String> roles = Collections.singletonList(RoleType.ROLE_GUEST);
saveUser(RoleType.ROLE_GUEST, roles, roles, locale, remoteAddr, session);
}
}
use of org.eclipse.jetty.http2.api.Session in project jetty.project by eclipse.
the class HttpClientTransportOverHTTP2Test method testLastStreamId.
@Test
public void testLastStreamId() throws Exception {
prepareServer(new RawHTTP2ServerConnectionFactory(new HttpConfiguration(), new ServerSessionListener.Adapter() {
@Override
public Map<Integer, Integer> onPreface(Session session) {
Map<Integer, Integer> settings = new HashMap<>();
settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 1);
return settings;
}
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
MetaData.Request request = (MetaData.Request) frame.getMetaData();
if (HttpMethod.HEAD.is(request.getMethod())) {
stream.getSession().close(ErrorCode.REFUSED_STREAM_ERROR.code, null, Callback.NOOP);
} else {
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
}
return null;
}
}));
server.start();
CountDownLatch latch = new CountDownLatch(2);
AtomicInteger lastStream = new AtomicInteger();
AtomicReference<Stream> streamRef = new AtomicReference<>();
CountDownLatch streamLatch = new CountDownLatch(1);
client = new HttpClient(new HttpClientTransportOverHTTP2(new HTTP2Client()) {
@Override
protected HttpConnectionOverHTTP2 newHttpConnection(HttpDestination destination, Session session) {
return new HttpConnectionOverHTTP2(destination, session) {
@Override
protected HttpChannelOverHTTP2 newHttpChannel(boolean push) {
return new HttpChannelOverHTTP2(getHttpDestination(), this, getSession(), push) {
@Override
public void setStream(Stream stream) {
super.setStream(stream);
streamRef.set(stream);
streamLatch.countDown();
}
};
}
};
}
@Override
protected void onClose(HttpConnectionOverHTTP2 connection, GoAwayFrame frame) {
super.onClose(connection, frame);
lastStream.set(frame.getLastStreamId());
latch.countDown();
}
}, null);
QueuedThreadPool clientExecutor = new QueuedThreadPool();
clientExecutor.setName("client");
client.setExecutor(clientExecutor);
client.start();
// Prime the connection to allow client and server prefaces to be exchanged.
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).path("/zero").timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
org.eclipse.jetty.client.api.Request request = client.newRequest("localhost", connector.getLocalPort()).method(HttpMethod.HEAD).path("/one");
request.send(result -> {
if (result.isFailed())
latch.countDown();
});
Assert.assertTrue(streamLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Stream stream = streamRef.get();
Assert.assertNotNull(stream);
Assert.assertEquals(lastStream.get(), stream.getId());
}
use of org.eclipse.jetty.http2.api.Session in project jetty.project by eclipse.
the class AbstractHTTP2ServerConnectionFactory method newConnection.
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
ServerSessionListener listener = newSessionListener(connector, endPoint);
Generator generator = new Generator(connector.getByteBufferPool(), getMaxDynamicTableSize(), getMaxHeaderBlockFragment());
FlowControlStrategy flowControl = getFlowControlStrategyFactory().newFlowControlStrategy();
HTTP2ServerSession session = new HTTP2ServerSession(connector.getScheduler(), endPoint, generator, listener, flowControl);
session.setMaxLocalStreams(getMaxConcurrentStreams());
session.setMaxRemoteStreams(getMaxConcurrentStreams());
// For a single stream in a connection, there will be a race between
// the stream idle timeout and the connection idle timeout. However,
// the typical case is that the connection will be busier and the
// stream idle timeout will expire earlier than the connection's.
long streamIdleTimeout = getStreamIdleTimeout();
if (streamIdleTimeout <= 0)
streamIdleTimeout = endPoint.getIdleTimeout();
session.setStreamIdleTimeout(streamIdleTimeout);
session.setInitialSessionRecvWindow(getInitialSessionRecvWindow());
ServerParser parser = newServerParser(connector, session);
HTTP2Connection connection = new HTTP2ServerConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, httpConfiguration, parser, session, getInputBufferSize(), listener);
connection.addListener(connectionListener);
return configure(connection, connector, endPoint);
}
Aggregations