use of org.eclipse.jetty.io.MappedByteBufferPool in project jetty.project by eclipse.
the class HttpClient method doStart.
@Override
protected void doStart() throws Exception {
if (sslContextFactory != null)
addBean(sslContextFactory);
String name = HttpClient.class.getSimpleName() + "@" + hashCode();
if (executor == null) {
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setName(name);
executor = threadPool;
}
addBean(executor);
if (byteBufferPool == null)
byteBufferPool = new MappedByteBufferPool();
addBean(byteBufferPool);
if (scheduler == null)
scheduler = new ScheduledExecutorScheduler(name + "-scheduler", false);
addBean(scheduler);
transport.setHttpClient(this);
addBean(transport);
if (resolver == null)
resolver = new SocketAddressResolver.Async(executor, scheduler, getAddressResolutionTimeout());
addBean(resolver);
handlers.put(new ContinueProtocolHandler());
handlers.put(new RedirectProtocolHandler(this));
handlers.put(new WWWAuthenticationProtocolHandler(this));
handlers.put(new ProxyAuthenticationProtocolHandler(this));
decoderFactories.add(new GZIPContentDecoder.Factory(byteBufferPool));
cookieManager = newCookieManager();
cookieStore = cookieManager.getCookieStore();
super.doStart();
}
use of org.eclipse.jetty.io.MappedByteBufferPool 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.io.MappedByteBufferPool 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.io.MappedByteBufferPool 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.io.MappedByteBufferPool in project jetty.project by eclipse.
the class ConnectHandler method doStart.
@Override
protected void doStart() throws Exception {
if (executor == null)
executor = getServer().getThreadPool();
if (scheduler == null)
addBean(scheduler = new ScheduledExecutorScheduler());
if (bufferPool == null)
addBean(bufferPool = new MappedByteBufferPool());
addBean(selector = newSelectorManager());
selector.setConnectTimeout(getConnectTimeout());
super.doStart();
}
Aggregations