6370908: Add support for HTTP_CONNECT proxy in Socket class

Christopher Hegarty - Sun Microsystems Ireland Christopher.Hegarty at Sun.COM
Mon Feb 22 07:02:11 PST 2010


Hi Damjan,

Actually, I did some work on this back in 2006 (!), but never finished 
it. I brought the changes into a mercurial repository and created a webrev:

   http://cr.openjdk.java.net/~chegar/6370908/webrev.00/webrev/

Basically, this change provides the basic functionality, without any 
frills, authentication, etc. I think for tunneling sockets through a 
HTTP proxy it should be sufficient. Do you require authentication in 
your environment?

To have authentication supported we would need to restructure the HTTP 
protocol handler in sun.net.www.protocol.http.HttpURLConnection, so that 
we can take advantage of the authentication schemes it already supports. 
Not a big deal, just needs to be done.

-Chris.

On 21/02/2010 13:09, Damjan Jovanovic wrote:
> Hi
>
>> From http://bugs.sun.com/view_bug.do?bug_id=6370908
>
> This RFE is basically about getting a TCP socket to tunnel through an
> HTTP proxy using the HTTP CONNECT request.
>
> I've found a hack to get this feature to work, using sun.net.*
> packages and lots of reflection. Would it be acceptable to use this
> solution (with some way to change socket identity) in a patch that
> adds a java.net.HttpSocketImpl class similar to the
> java.net.SocksSocketImpl class that's already used to tunnel through
> SOCKS proxies? If not, in what other way should such a patch be done?
>
> Thank you
> Damjan Jovanovic
>
> import java.net.*;
> import java.io.*;
> import java.lang.reflect.*;
>
> public class TunnelProxy {
> 	private static Socket connectThroughHTTPProxy(String proxyHost, int
> proxyPort, String destinationHost, int destinationPort) throws
> Exception
> 	{
> 		URL destinationURL = new URL("http://" + destinationHost + ":" +
> destinationPort);
> 		sun.net.www.protocol.http.HttpURLConnection conn =
> 			new sun.net.www.protocol.http.HttpURLConnection(
> 				destinationURL, new java.net.Proxy(java.net.Proxy.Type.HTTP, new
> InetSocketAddress(proxyHost, proxyPort)));
> 		conn.setDoInput(true);
> 		conn.setDoOutput(true);
> 		conn.connect();
> 		conn.doTunneling();
> 		Field httpField = conn.getClass().getDeclaredField("http");
> 		httpField.setAccessible(true);
> 		sun.net.www.http.HttpClient httpClient =
> (sun.net.www.http.HttpClient) httpField.get(conn);
> 		Field serverSocketField =
> sun.net.NetworkClient.class.getDeclaredField("serverSocket");
> 		serverSocketField.setAccessible(true);
> 		Socket socket = (Socket) serverSocketField.get(httpClient);
> 		return socket;
> 	}
>
> 	public static void main(String[] args) throws Exception {
> 		System.setProperty("java.net.useSystemProxies", "true");
> 		InputStream in = connectThroughHTTPProxy(args[0],
> Integer.parseInt(args[1]), args[2],
> Integer.parseInt(args[3])).getInputStream();
> 		byte[] bytes = new byte[1024];
> 		int bytesRead;
> 		while ((bytesRead = in.read(bytes)) != -1) {
> 			System.out.print(new String(bytes));
> 		}
> 	}
> }



More information about the net-dev mailing list