From b62c5cdde676945535158ef914c095ac5c514f90 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 15 Jul 2011 15:06:26 +1000 Subject: [PATCH] 298502 added ProxyFakeTunnelTest.java to demonstrate the issue --- VERSION.txt | 1 + .../jetty/client/ProxyFakeTunnelTest.java | 205 ++++++++++++++++++ .../jetty/client/ProxyTunnellingTest.java | 23 +- 3 files changed, 220 insertions(+), 9 deletions(-) create mode 100644 jetty-client/src/test/java/org/eclipse/jetty/client/ProxyFakeTunnelTest.java diff --git a/VERSION.txt b/VERSION.txt index c6b16cac638..ce13f04906e 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1,4 +1,5 @@ jetty-7.5.0-SNAPSHOT + + 298502 Added test harness to demonstrate issue + 351516 Refactored sessions to better support nosql session managers + 351576 Do not use deprecated method File.toURL() + 352046 Need try/catch around features set in XmlParser diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ProxyFakeTunnelTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ProxyFakeTunnelTest.java new file mode 100644 index 00000000000..c60e5a6f32b --- /dev/null +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ProxyFakeTunnelTest.java @@ -0,0 +1,205 @@ +package org.eclipse.jetty.client; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; + +import org.eclipse.jetty.toolchain.test.IO; + + +public class ProxyFakeTunnelTest extends ProxyTunnellingTest +{ + ServerSocket _proxySocket; + Thread _proxyThread; + + protected int proxyPort() + { + return _proxySocket.getLocalPort(); + } + + + protected void startProxy() throws Exception + { + _proxySocket = new ServerSocket(0); + + _proxyThread = new Thread() + { + @Override + public void run() + { + while (!_proxySocket.isClosed()) + { + try + { + Socket socket=_proxySocket.accept(); + System.err.println("accepted "+socket); + new FakeProxy(socket).start(); + } + catch (IOException e) + { + } + } + } + }; + _proxyThread.setDaemon(true); + _proxyThread.start(); + + } + + protected void stopProxy() throws Exception + { + _proxySocket.close(); + _proxyThread.interrupt(); + } + + static class FakeProxy extends Thread + { + Socket _socket; + + public FakeProxy(Socket socket) + { + _socket=socket; + } + + public void run() + { + + Socket toserver=null; + final InputStream in; + final OutputStream out; + try + { + in = _socket.getInputStream(); + out = _socket.getOutputStream(); + + String address=""; + int state=0; + + for (int b=in.read();b>=0;b=in.read()) + { + switch(state) + { + case 0: + if (' '==b) + state=1; + break; + + case 1: + if (' '==b) + state=2; + else + address+=(char)b; + break; + + case 2: + if ('\r'==b) + state=3; + break; + + case 3: + if ('\n'==b) + state=4; + else + state=2; + break; + + case 4: + if ('\r'==b) + state=5; + else + state=2; + break; + + case 5: + if ('\n'==b) + { + state=6; + System.err.println("address="+address); + String[] parts=address.split(":"); + String result="200 OK"; + try + { + toserver = new Socket(parts[0],Integer.parseInt(parts[1])); + } + catch(IOException e) + { + result="503 Unavailable"; + } + out.write(( + "HTTP/1.1 "+result+"\r\n"+ + "Server: fake\r\n"+ + "Content-Length: 0\r\n"+ // TODO test fails without this! + "\r\n" + ).getBytes()); + out.flush(); + + System.err.println(toserver); + final InputStream from = toserver.getInputStream(); + + Thread copy = new Thread() + { + public void run() + { + try + { + IO.copy(from,out); + out.close(); + } + catch (IOException e) + { + } + finally + { + try + { + out.close(); + } + catch (IOException e) + { + } + } + } + }; + copy.setDaemon(true); + copy.start(); + + + } + else + state=2; + break; + + case 6: + toserver.getOutputStream().write((byte)b); + + } + } + + + } + catch (IOException e) + { + e.printStackTrace(); + } + finally + { + if (toserver!=null) + { + try + { + toserver.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + } + + } + + +} diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ProxyTunnellingTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ProxyTunnellingTest.java index 68b7e25671d..f56e6a0a686 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ProxyTunnellingTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ProxyTunnellingTest.java @@ -37,7 +37,12 @@ public class ProxyTunnellingTest private Connector proxyConnector; private int serverConnectTimeout = 1000; - private void startSSLServer(Handler handler) throws Exception + protected int proxyPort() + { + return proxyConnector.getLocalPort(); + } + + protected void startSSLServer(Handler handler) throws Exception { SslSelectChannelConnector connector = new SslSelectChannelConnector(); String keyStorePath = MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath(); @@ -48,7 +53,7 @@ public class ProxyTunnellingTest startServer(connector, handler); } - private void startServer(Connector connector, Handler handler) throws Exception + protected void startServer(Connector connector, Handler handler) throws Exception { server = new Server(); serverConnector = connector; @@ -57,7 +62,7 @@ public class ProxyTunnellingTest server.start(); } - private void startProxy() throws Exception + protected void startProxy() throws Exception { proxy = new Server(); proxyConnector = new SelectChannelConnector(); @@ -77,13 +82,13 @@ public class ProxyTunnellingTest stopServer(); } - private void stopServer() throws Exception + protected void stopServer() throws Exception { server.stop(); server.join(); } - private void stopProxy() throws Exception + protected void stopProxy() throws Exception { proxy.stop(); proxy.join(); @@ -96,7 +101,7 @@ public class ProxyTunnellingTest startProxy(); HttpClient httpClient = new HttpClient(); - httpClient.setProxy(new Address("localhost", proxyConnector.getLocalPort())); + httpClient.setProxy(new Address("localhost", proxyPort())); httpClient.start(); try @@ -124,7 +129,7 @@ public class ProxyTunnellingTest startProxy(); HttpClient httpClient = new HttpClient(); - httpClient.setProxy(new Address("localhost", proxyConnector.getLocalPort())); + httpClient.setProxy(new Address("localhost", proxyPort())); httpClient.start(); try @@ -163,7 +168,7 @@ public class ProxyTunnellingTest { startSSLServer(new ServerHandler()); startProxy(); - int proxyPort = proxyConnector.getLocalPort(); + int proxyPort = proxyPort(); stopProxy(); HttpClient httpClient = new HttpClient(); @@ -203,7 +208,7 @@ public class ProxyTunnellingTest startProxy(); HttpClient httpClient = new HttpClient(); - httpClient.setProxy(new Address("localhost", proxyConnector.getLocalPort())); + httpClient.setProxy(new Address("localhost", proxyPort())); httpClient.start(); try