use of org.eclipse.jetty.http2.generator.Generator in project jetty.project by eclipse.
the class HTTP2CServerTest method testHTTP_2_0_DirectWithoutH2C.
@Test
public void testHTTP_2_0_DirectWithoutH2C() throws Exception {
AtomicLong fills = new AtomicLong();
// Remove "h2c", leaving only "http/1.1".
connector.clearConnectionFactories();
HttpConnectionFactory connectionFactory = new HttpConnectionFactory() {
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
HttpConnection connection = new HttpConnection(getHttpConfiguration(), connector, endPoint, getHttpCompliance(), isRecordHttpComplianceViolations()) {
@Override
public void onFillable() {
fills.incrementAndGet();
super.onFillable();
}
};
return configure(connection, connector, endPoint);
}
};
connector.addConnectionFactory(connectionFactory);
connector.setDefaultProtocol(connectionFactory.getProtocol());
// Now send a HTTP/2 direct request, which
// will have the PRI * HTTP/2.0 preface.
byteBufferPool = new MappedByteBufferPool();
generator = new Generator(byteBufferPool);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
// We sent a HTTP/2 preface, but the server has no "h2c" connection
// factory so it does not know how to handle this request.
InputStream input = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
String responseLine = reader.readLine();
Assert.assertThat(responseLine, Matchers.containsString(" 426 "));
while (true) {
if (reader.read() < 0)
break;
}
}
// Make sure we did not spin.
Thread.sleep(1000);
Assert.assertThat(fills.get(), Matchers.lessThan(5L));
}
use of org.eclipse.jetty.http2.generator.Generator in project jetty.project by eclipse.
the class HTTP2ServerTest method testRequestWithContinuationFrames.
private void testRequestWithContinuationFrames(PriorityFrame priorityFrame, Callable<ByteBufferPool.Lease> frames) throws Exception {
final CountDownLatch serverLatch = new CountDownLatch(1);
startServer(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
if (priorityFrame != null) {
PriorityFrame priority = frame.getPriority();
Assert.assertNotNull(priority);
Assert.assertEquals(priorityFrame.getStreamId(), priority.getStreamId());
Assert.assertEquals(priorityFrame.getParentStreamId(), priority.getParentStreamId());
Assert.assertEquals(priorityFrame.getWeight(), priority.getWeight());
Assert.assertEquals(priorityFrame.isExclusive(), priority.isExclusive());
}
serverLatch.countDown();
MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
HeadersFrame responseFrame = new HeadersFrame(stream.getId(), metaData, null, true);
stream.headers(responseFrame, Callback.NOOP);
return null;
}
});
generator = new Generator(byteBufferPool, 4096, 4);
ByteBufferPool.Lease lease = frames.call();
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
output.flush();
Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
final CountDownLatch clientLatch = new CountDownLatch(1);
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onHeaders(HeadersFrame frame) {
if (frame.isEndStream())
clientLatch.countDown();
}
}, 4096, 8192);
boolean closed = parseResponse(client, parser);
Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
Assert.assertFalse(closed);
}
}
use of org.eclipse.jetty.http2.generator.Generator in project jetty.project by eclipse.
the class HttpClientTransportOverHTTP2Test method testClientStopsServerDoesNotCloseClientCloses.
@Test
public void testClientStopsServerDoesNotCloseClientCloses() throws Exception {
try (ServerSocket server = new ServerSocket(0)) {
List<Session> sessions = new ArrayList<>();
HTTP2Client h2Client = new HTTP2Client();
HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(h2Client) {
@Override
protected HttpConnectionOverHTTP2 newHttpConnection(HttpDestination destination, Session session) {
sessions.add(session);
return super.newHttpConnection(destination, session);
}
}, null);
QueuedThreadPool clientExecutor = new QueuedThreadPool();
clientExecutor.setName("client");
client.setExecutor(clientExecutor);
client.start();
CountDownLatch resultLatch = new CountDownLatch(1);
client.newRequest("localhost", server.getLocalPort()).send(result -> {
if (result.getResponse().getStatus() == HttpStatus.OK_200)
resultLatch.countDown();
});
ByteBufferPool byteBufferPool = new MappedByteBufferPool();
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
Generator generator = new Generator(byteBufferPool);
try (Socket socket = server.accept()) {
socket.setSoTimeout(1000);
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
ServerParser parser = new ServerParser(byteBufferPool, new ServerParser.Listener.Adapter() {
@Override
public void onHeaders(HeadersFrame request) {
// Server's preface.
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
// Reply to client's SETTINGS.
generator.control(lease, new SettingsFrame(new HashMap<>(), true));
// Response.
MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
HeadersFrame response = new HeadersFrame(request.getStreamId(), metaData, null, true);
generator.control(lease, response);
try {
// Write the frames.
for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
} catch (Throwable x) {
x.printStackTrace();
}
}
}, 4096, 8192);
byte[] bytes = new byte[1024];
while (true) {
try {
int read = input.read(bytes);
if (read < 0)
Assert.fail();
parser.parse(ByteBuffer.wrap(bytes, 0, read));
} catch (SocketTimeoutException x) {
break;
}
}
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
// The client will send a GO_AWAY, but the server will not close.
client.stop();
// Give some time to process the stop/close operations.
Thread.sleep(1000);
Assert.assertTrue(h2Client.getBeans(Session.class).isEmpty());
for (Session session : sessions) {
Assert.assertTrue(session.isClosed());
Assert.assertTrue(((HTTP2Session) session).isDisconnected());
}
}
}
}
use of org.eclipse.jetty.http2.generator.Generator in project jetty.project by eclipse.
the class AbstractServerTest method prepareServer.
private void prepareServer(ConnectionFactory connectionFactory) {
QueuedThreadPool serverExecutor = new QueuedThreadPool();
serverExecutor.setName("server");
server = new Server(serverExecutor);
connector = new ServerConnector(server, connectionFactory);
server.addConnector(connector);
path = "/test";
byteBufferPool = new MappedByteBufferPool();
generator = new Generator(byteBufferPool);
}
use of org.eclipse.jetty.http2.generator.Generator in project jetty.project by eclipse.
the class PrefaceTest method testClientPrefaceReplySentAfterServerPreface.
@Test
public void testClientPrefaceReplySentAfterServerPreface() throws Exception {
start(new ServerSessionListener.Adapter() {
@Override
public Map<Integer, Integer> onPreface(Session session) {
Map<Integer, Integer> settings = new HashMap<>();
settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 128);
return settings;
}
@Override
public void onPing(Session session, PingFrame frame) {
session.close(ErrorCode.NO_ERROR.code, null, Callback.NOOP);
}
});
ByteBufferPool byteBufferPool = client.getByteBufferPool();
try (SocketChannel socket = SocketChannel.open()) {
socket.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
Generator generator = new Generator(byteBufferPool);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
Map<Integer, Integer> clientSettings = new HashMap<>();
clientSettings.put(SettingsFrame.ENABLE_PUSH, 0);
generator.control(lease, new SettingsFrame(clientSettings, false));
// The PING frame just to make sure the client stops reading.
generator.control(lease, new PingFrame(true));
List<ByteBuffer> buffers = lease.getByteBuffers();
socket.write(buffers.toArray(new ByteBuffer[buffers.size()]));
Queue<SettingsFrame> settings = new ArrayDeque<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onSettings(SettingsFrame frame) {
settings.offer(frame);
}
}, 4096, 8192);
ByteBuffer buffer = byteBufferPool.acquire(1024, true);
while (true) {
BufferUtil.clearToFill(buffer);
int read = socket.read(buffer);
BufferUtil.flipToFlush(buffer, 0);
if (read < 0)
break;
parser.parse(buffer);
}
Assert.assertEquals(2, settings.size());
SettingsFrame frame1 = settings.poll();
Assert.assertFalse(frame1.isReply());
SettingsFrame frame2 = settings.poll();
Assert.assertTrue(frame2.isReply());
}
}
Aggregations