use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.
the class ConnectHandlerTest method testCONNECTAndPOSTWithContext.
@Test
public void testCONNECTAndPOSTWithContext() throws Exception {
final String contextKey = "contextKey";
final String contextValue = "contextValue";
// Replace the default ProxyHandler with a subclass to test context information passing
disposeProxy();
proxy.setHandler(new ConnectHandler() {
@Override
protected boolean handleAuthentication(HttpServletRequest request, HttpServletResponse response, String address) {
request.setAttribute(contextKey, contextValue);
return super.handleAuthentication(request, response, address);
}
@Override
protected void connectToServer(HttpServletRequest request, String host, int port, Promise<SocketChannel> promise) {
Assert.assertEquals(contextValue, request.getAttribute(contextKey));
super.connectToServer(request, host, port, promise);
}
@Override
protected void prepareContext(HttpServletRequest request, ConcurrentMap<String, Object> context) {
// Transfer data from the HTTP request to the connection context
Assert.assertEquals(contextValue, request.getAttribute(contextKey));
context.put(contextKey, request.getAttribute(contextKey));
}
@Override
protected int read(EndPoint endPoint, ByteBuffer buffer, ConcurrentMap<String, Object> context) throws IOException {
Assert.assertEquals(contextValue, context.get(contextKey));
return super.read(endPoint, buffer, context);
}
@Override
protected void write(EndPoint endPoint, ByteBuffer buffer, Callback callback, ConcurrentMap<String, Object> context) {
Assert.assertEquals(contextValue, context.get(contextKey));
super.write(endPoint, buffer, callback, context);
}
});
proxy.start();
String hostPort = "localhost:" + serverConnector.getLocalPort();
String request = "" + "CONNECT " + hostPort + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n";
try (Socket socket = newSocket()) {
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
// Expect 200 OK from the CONNECT request
HttpTester.Response response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
String body = "0123456789ABCDEF";
request = "" + "POST /echo HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "Content-Length: " + body.length() + "\r\n" + "\r\n" + body;
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
response = readResponse(input);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertEquals("POST /echo\r\n" + body, response.getContent());
}
}
use of org.eclipse.jetty.io.EndPoint 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.EndPoint in project jetty.project by eclipse.
the class AbstractWebSocketConnection method hashCode.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
EndPoint endp = getEndPoint();
if (endp != null) {
result = prime * result + endp.getLocalAddress().hashCode();
result = prime * result + endp.getRemoteAddress().hashCode();
}
return result;
}
use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.
the class AbstractWebSocketConnection method disconnect.
private void disconnect(boolean onlyOutput) {
if (LOG_CLOSE.isDebugEnabled())
LOG_CLOSE.debug("{} disconnect({})", policy.getBehavior(), onlyOutput ? "outputOnly" : "both");
// close FrameFlusher, we cannot write anymore at this point.
flusher.close();
EndPoint endPoint = getEndPoint();
// SSL close alerts to be sent by Jetty
if (LOG_CLOSE.isDebugEnabled())
LOG_CLOSE.debug("Shutting down output {}", endPoint);
endPoint.shutdownOutput();
if (!onlyOutput) {
if (LOG_CLOSE.isDebugEnabled())
LOG_CLOSE.debug("Closing {}", endPoint);
endPoint.close();
}
}
use of org.eclipse.jetty.io.EndPoint in project blade by biezhi.
the class SslClientConnectionFactory method newConnection.
@Override
public Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
String host = (String) context.get(SSL_PEER_HOST_CONTEXT_KEY);
int port = (Integer) context.get(SSL_PEER_PORT_CONTEXT_KEY);
SSLEngine engine = sslContextFactory.newSSLEngine(host, port);
engine.setUseClientMode(true);
context.put(SSL_ENGINE_CONTEXT_KEY, engine);
SslConnection sslConnection = newSslConnection(byteBufferPool, executor, endPoint, engine);
endPoint.setConnection(sslConnection);
customize(sslConnection, context);
EndPoint appEndPoint = sslConnection.getDecryptedEndPoint();
appEndPoint.setConnection(connectionFactory.newConnection(appEndPoint, context));
return sslConnection;
}
Aggregations