use of org.eclipse.jetty.io.AbstractConnection in project jetty.project by eclipse.
the class HttpClientTest method testCONNECTWithHTTP10.
@Test
public void testCONNECTWithHTTP10() throws Exception {
try (ServerSocket server = new ServerSocket(0)) {
startClient();
String host = "localhost";
int port = server.getLocalPort();
Request request = client.newRequest(host, port).method(HttpMethod.CONNECT).version(HttpVersion.HTTP_1_0);
FuturePromise<Connection> promise = new FuturePromise<>();
client.getDestination("http", host, port).newConnection(promise);
Connection connection = promise.get(5, TimeUnit.SECONDS);
FutureResponseListener listener = new FutureResponseListener(request);
connection.send(request, listener);
try (Socket socket = server.accept()) {
InputStream input = socket.getInputStream();
consume(input, false);
// HTTP/1.0 response, the client must not close the connection.
String httpResponse = "" + "HTTP/1.0 200 OK\r\n" + "\r\n";
OutputStream output = socket.getOutputStream();
output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
output.flush();
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatus());
// Because the tunnel was successful, this connection will be
// upgraded to an SslConnection, so it will not be fill interested.
// This test doesn't upgrade, so it needs to restore the fill interest.
((AbstractConnection) connection).fillInterested();
// Test that I can send another request on the same connection.
request = client.newRequest(host, port);
listener = new FutureResponseListener(request);
connection.send(request, listener);
consume(input, false);
httpResponse = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
output.flush();
listener.get(5, TimeUnit.SECONDS);
}
}
}
use of org.eclipse.jetty.io.AbstractConnection in project jetty.project by eclipse.
the class ForwardProxyServerTest method testRequestTarget.
@Test
public void testRequestTarget() throws Exception {
startServer(new AbstractConnectionFactory("http/1.1") {
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
return new AbstractConnection(endPoint, connector.getExecutor()) {
@Override
public void onOpen() {
super.onOpen();
fillInterested();
}
@Override
public void onFillable() {
try {
// When using TLS, multiple reads are required.
ByteBuffer buffer = BufferUtil.allocate(1024);
int filled = 0;
while (filled == 0) filled = getEndPoint().fill(buffer);
Utf8StringBuilder builder = new Utf8StringBuilder();
builder.append(buffer);
String request = builder.toString();
// ProxyServlet will receive an absolute URI from
// the client, and convert it to a relative URI.
// The ConnectHandler won't modify what the client
// sent, which must be a relative URI.
Assert.assertThat(request.length(), Matchers.greaterThan(0));
if (serverSslContextFactory == null)
Assert.assertFalse(request.contains("http://"));
else
Assert.assertFalse(request.contains("https://"));
String response = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
getEndPoint().write(Callback.NOOP, ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8)));
} catch (Throwable x) {
x.printStackTrace();
close();
}
}
};
}
});
startProxy();
HttpClient httpClient = new HttpClient(newSslContextFactory());
httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
httpClient.start();
try {
ContentResponse response = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(serverSslContextFactory == null ? "http" : "https").method(HttpMethod.GET).path("/test").send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
} finally {
httpClient.stop();
}
}
use of org.eclipse.jetty.io.AbstractConnection in project jetty.project by eclipse.
the class SslConnectionTest method testSslConnectionClosedBeforeFill.
@Test
public void testSslConnectionClosedBeforeFill() throws Exception {
File keyStore = MavenTestingUtils.getTestResourceFile("keystore.jks");
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keyStore.getAbsolutePath());
sslContextFactory.setKeyStorePassword("storepwd");
sslContextFactory.start();
ByteBufferPool byteBufferPool = new MappedByteBufferPool();
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.start();
ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
SSLEngine sslEngine = sslContextFactory.newSSLEngine();
sslEngine.setUseClientMode(false);
SslConnection sslConnection = new SslConnection(byteBufferPool, threadPool, endPoint, sslEngine);
EndPoint sslEndPoint = sslConnection.getDecryptedEndPoint();
sslEndPoint.setConnection(new AbstractConnection(sslEndPoint, threadPool) {
@Override
public void onFillable() {
}
});
// There are no bytes in the endPoint, so we fill zero.
// However, this will trigger state changes in SSLEngine
// that will later cause it to throw ISE("Internal error").
sslEndPoint.fill(BufferUtil.EMPTY_BUFFER);
// Close the connection before filling.
sslEndPoint.shutdownOutput();
// Put some bytes in the endPoint to trigger
// the required state changes in SSLEngine.
byte[] bytes = new byte[] { 0x16, 0x03, 0x03, 0x00, 0x00 };
endPoint.addInput(ByteBuffer.wrap(bytes));
// reads from the EndPoint.
try {
sslEndPoint.fill(BufferUtil.EMPTY_BUFFER);
Assert.fail();
} catch (SSLHandshakeException x) {
// Expected.
}
}
Aggregations