use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.
the class HttpClientTransportOverHTTP method newConnection.
@Override
public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
HttpDestination destination = (HttpDestination) context.get(HTTP_DESTINATION_CONTEXT_KEY);
@SuppressWarnings("unchecked") Promise<Connection> promise = (Promise<Connection>) context.get(HTTP_CONNECTION_PROMISE_CONTEXT_KEY);
HttpConnectionOverHTTP connection = newHttpConnection(endPoint, destination, promise);
if (LOG.isDebugEnabled())
LOG.debug("Created {}", connection);
return customize(connection, context);
}
use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.
the class HttpClientFailureTest method testFailureBeforeRequestCommit.
@Test
public void testFailureBeforeRequestCommit() throws Exception {
startServer(new EmptyServerHandler());
final AtomicReference<HttpConnectionOverHTTP> connectionRef = new AtomicReference<>();
client = new HttpClient(new HttpClientTransportOverHTTP() {
@Override
protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
HttpConnectionOverHTTP connection = super.newHttpConnection(endPoint, destination, promise);
connectionRef.set(connection);
return connection;
}
}, null);
client.start();
try {
client.newRequest("localhost", connector.getLocalPort()).onRequestHeaders(request -> connectionRef.get().getEndPoint().close()).timeout(5, TimeUnit.SECONDS).send();
Assert.fail();
} catch (ExecutionException x) {
// Expected.
}
DuplexConnectionPool connectionPool = (DuplexConnectionPool) connectionRef.get().getHttpDestination().getConnectionPool();
Assert.assertEquals(0, connectionPool.getConnectionCount());
Assert.assertEquals(0, connectionPool.getActiveConnections().size());
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.
the class ProxyProtocolTest method test_PROXY_GET_v1.
@Test
public void test_PROXY_GET_v1() throws Exception {
startServer(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
Assert.assertEquals("1.2.3.4", request.getRemoteAddr());
Assert.assertEquals(1111, request.getRemotePort());
Assert.assertEquals("5.6.7.8", request.getLocalAddr());
Assert.assertEquals(2222, request.getLocalPort());
} catch (Throwable th) {
th.printStackTrace();
response.setStatus(500);
}
baseRequest.setHandled(true);
}
});
String request1 = "PROXY TCP4 1.2.3.4 5.6.7.8 1111 2222\r\n";
SocketChannel channel = SocketChannel.open();
channel.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
channel.write(ByteBuffer.wrap(request1.getBytes(StandardCharsets.UTF_8)));
FuturePromise<Session> promise = new FuturePromise<>();
client.accept(null, channel, new Session.Listener.Adapter(), promise);
Session session = promise.get(5, TimeUnit.SECONDS);
HttpFields fields = new HttpFields();
String uri = "http://localhost:" + connector.getLocalPort() + "/";
MetaData.Request metaData = new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_2, fields);
HeadersFrame frame = new HeadersFrame(metaData, null, true);
CountDownLatch latch = new CountDownLatch(1);
session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
MetaData.Response response = (MetaData.Response) frame.getMetaData();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
if (frame.isEndStream())
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.
the class HTTP2ClientConnectionFactory method newConnection.
@Override
public Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
HTTP2Client client = (HTTP2Client) context.get(CLIENT_CONTEXT_KEY);
ByteBufferPool byteBufferPool = (ByteBufferPool) context.get(BYTE_BUFFER_POOL_CONTEXT_KEY);
Executor executor = (Executor) context.get(EXECUTOR_CONTEXT_KEY);
Scheduler scheduler = (Scheduler) context.get(SCHEDULER_CONTEXT_KEY);
Session.Listener listener = (Session.Listener) context.get(SESSION_LISTENER_CONTEXT_KEY);
@SuppressWarnings("unchecked") Promise<Session> promise = (Promise<Session>) context.get(SESSION_PROMISE_CONTEXT_KEY);
Generator generator = new Generator(byteBufferPool);
FlowControlStrategy flowControl = client.getFlowControlStrategyFactory().newFlowControlStrategy();
HTTP2ClientSession session = new HTTP2ClientSession(scheduler, endPoint, generator, listener, flowControl);
Parser parser = new Parser(byteBufferPool, session, 4096, 8192);
HTTP2ClientConnection connection = new HTTP2ClientConnection(client, byteBufferPool, executor, endPoint, parser, session, client.getInputBufferSize(), promise, listener);
connection.addListener(connectionListener);
return customize(connection, context);
}
use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.
the class HttpClientTest method testConnectHostWithMultipleAddresses.
@Test
public void testConnectHostWithMultipleAddresses() throws Exception {
start(new EmptyServerHandler());
client.setSocketAddressResolver(new SocketAddressResolver.Async(client.getExecutor(), client.getScheduler(), client.getConnectTimeout()) {
@Override
public void resolve(String host, int port, Promise<List<InetSocketAddress>> promise) {
super.resolve(host, port, new Promise<List<InetSocketAddress>>() {
@Override
public void succeeded(List<InetSocketAddress> result) {
// Add as first address an invalid address so that we test
// that the connect operation iterates over the addresses.
result.add(0, new InetSocketAddress("idontexist", port));
promise.succeeded(result);
}
@Override
public void failed(Throwable x) {
promise.failed(x);
}
});
}
});
// If no exceptions the test passes.
client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).header(HttpHeader.CONNECTION, "close").send();
}
Aggregations