From 929c27809dff59d30c1c7fe8fa5e25f4d43f4bb0 Mon Sep 17 00:00:00 2001 From: Sam Steele Date: Fri, 16 Aug 2013 12:07:20 -0700 Subject: [PATCH] HTTP proxy support --- .../android_websockets/WebSocketClient.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/codebutler/android_websockets/WebSocketClient.java b/src/main/java/com/codebutler/android_websockets/WebSocketClient.java index d5e1b73..9e9cbeb 100644 --- a/src/main/java/com/codebutler/android_websockets/WebSocketClient.java +++ b/src/main/java/com/codebutler/android_websockets/WebSocketClient.java @@ -37,6 +37,8 @@ public class WebSocketClient { private Handler mHandler; private List mExtraHeaders; private HybiParser mParser; + private String mProxyHost; + private int mProxyPort; private final Object mSendLock = new Object(); @@ -83,9 +85,34 @@ public void run() { URI origin = new URI(originScheme, "//" + mURI.getHost(), null); SocketFactory factory = mURI.getScheme().equals("wss") ? getSSLSocketFactory() : SocketFactory.getDefault(); - mSocket = factory.createSocket(mURI.getHost(), port); + if(mProxyHost != null && mProxyHost.length() > 0) + mSocket = SocketFactory.getDefault().createSocket(mProxyHost, mProxyPort); + else + mSocket = factory.createSocket(mURI.getHost(), port); PrintWriter out = new PrintWriter(mSocket.getOutputStream()); + if(mProxyHost != null && mProxyHost.length() > 0) { + out.print("CONNECT " + mURI.getHost() + ":" + port + " HTTP/1.1\r\n"); + out.print("\r\n"); + out.flush(); + HybiParser.HappyDataInputStream stream = new HybiParser.HappyDataInputStream(mSocket.getInputStream()); + + // Read HTTP response status line. + StatusLine statusLine = parseStatusLine(readLine(stream)); + if (statusLine == null) { + throw new HttpException("Received no reply from server."); + } else if (statusLine.getStatusCode() != HttpStatus.SC_OK) { + throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); + } + + // Read HTTP response headers. + String line; + while (!TextUtils.isEmpty(line = readLine(stream))); + if(mURI.getScheme().equals("wss")) { + mSocket = getSSLSocketFactory().createSocket(mSocket, mURI.getHost(), port, false); + out = new PrintWriter(mSocket.getOutputStream()); + } + } out.print("GET " + path + " HTTP/1.1\r\n"); out.print("Upgrade: websocket\r\n"); out.print("Connection: Upgrade\r\n"); @@ -249,6 +276,11 @@ public void run() { }); } + public void setProxy(String host, int port) { + mProxyHost = host; + mProxyPort = port; + } + public interface Listener { public void onConnect(); public void onMessage(String message);