From df05508a6f8379a9c699a7bcc29974be14822124 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Sun, 17 Feb 2013 22:32:17 -0800 Subject: [PATCH] Switch to AndroidHttpClient and explicit targeting. --- .../securesms/mms/MmsCommunication.java | 30 +++--------- .../securesms/mms/MmsDownloadHelper.java | 48 ++++++++++++++----- .../securesms/mms/MmsSendHelper.java | 47 +++++++++++------- 3 files changed, 73 insertions(+), 52 deletions(-) diff --git a/src/org/thoughtcrime/securesms/mms/MmsCommunication.java b/src/org/thoughtcrime/securesms/mms/MmsCommunication.java index d856af16dd..da150c6352 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsCommunication.java +++ b/src/org/thoughtcrime/securesms/mms/MmsCommunication.java @@ -22,22 +22,13 @@ import android.database.Cursor; import android.database.sqlite.SQLiteException; import android.net.ConnectivityManager; import android.net.Uri; +import android.net.http.AndroidHttpClient; import android.preference.PreferenceManager; import android.util.Log; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; -import org.apache.http.client.HttpClient; -import org.apache.http.client.params.HttpClientParams; -import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.params.ConnRouteParams; -import org.apache.http.conn.scheme.PlainSocketFactory; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; -import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; @@ -126,26 +117,17 @@ public class MmsCommunication { } - protected static HttpClient constructHttpClient(MmsConnectionParameters mmsConfig) { - HttpParams params = new BasicHttpParams(); - HttpConnectionParams.setStaleCheckingEnabled(params, false); - HttpConnectionParams.setConnectionTimeout(params, 20 * 1000); - HttpConnectionParams.setSoTimeout(params, 20 * 1000); - HttpConnectionParams.setSocketBufferSize(params, 8192); - HttpClientParams.setRedirecting(params, false); - HttpProtocolParams.setUserAgent(params, "TextSecure/0.1"); + protected static AndroidHttpClient constructHttpClient(Context context, MmsConnectionParameters mmsConfig) { + AndroidHttpClient client = AndroidHttpClient.newInstance("TextSecure/0.1", context); + HttpParams params = client.getParams(); HttpProtocolParams.setContentCharset(params, "UTF-8"); + HttpConnectionParams.setSoTimeout(params, 20 * 1000); if (mmsConfig.hasProxy()) { ConnRouteParams.setDefaultProxy(params, new HttpHost(mmsConfig.getProxy(), mmsConfig.getPort())); } - SchemeRegistry schemeRegistry = new SchemeRegistry(); - schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); - schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); - - ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); - return new DefaultHttpClient(manager, params); + return client; } protected static byte[] parseResponse(HttpEntity entity) throws IOException { diff --git a/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java b/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java index 3e65da13f0..0e380a911b 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java +++ b/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java @@ -17,34 +17,58 @@ package org.thoughtcrime.securesms.mms; import android.content.Context; +import android.net.http.AndroidHttpClient; import android.util.Log; +import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; public class MmsDownloadHelper extends MmsCommunication { - private static byte[] makeRequest(MmsConnectionParameters connectionParameters, String url) + private static byte[] makeRequest(Context context, MmsConnectionParameters connectionParameters, String url) throws ClientProtocolException, IOException { - HttpClient client = constructHttpClient(connectionParameters); - HttpGet request = new HttpGet(url); + AndroidHttpClient client = null; - request.setParams(client.getParams()); - request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); + try { + client = constructHttpClient(context, connectionParameters); + URI targetUrl = new URI(url.trim()); + HttpHost target = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME); + HttpGet request = new HttpGet(url.trim()); - HttpResponse response = client.execute(request); - StatusLine status = response.getStatusLine(); + request.setParams(client.getParams()); + request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); - if (status.getStatusCode() != 200) - throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase()); +// java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST); +// java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST); +// +// System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); +// System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); +// System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug"); +// System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug"); - return parseResponse(response.getEntity()); + HttpResponse response = client.execute(target, request); + StatusLine status = response.getStatusLine(); + + if (status.getStatusCode() != 200) + throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase()); + + return parseResponse(response.getEntity()); + } catch (URISyntaxException use) { + Log.w("MmsDownloadHelper", use); + throw new IOException("Couldn't parse URI"); + } finally { + if (client != null) + client.close(); + } } public static byte[] retrieveMms(Context context, String url, String apn) throws IOException { @@ -58,6 +82,6 @@ public class MmsDownloadHelper extends MmsCommunication { } checkRouteToHost(context, connectionParameters, url); - return makeRequest(connectionParameters, url); + return makeRequest(context, connectionParameters, url); } } diff --git a/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java b/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java index 4ed5b9d42e..8197629365 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java +++ b/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java @@ -17,38 +17,53 @@ package org.thoughtcrime.securesms.mms; import android.content.Context; +import android.net.http.AndroidHttpClient; import android.util.Log; +import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ByteArrayEntity; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; public class MmsSendHelper extends MmsCommunication { - private static byte[] makePost(MmsConnectionParameters parameters, byte[] mms) throws ClientProtocolException, IOException { - Log.w("MmsSender", "Sending MMS1 of length: " + mms.length); - HttpClient client = constructHttpClient(parameters); - HttpPost request = new HttpPost(parameters.getMmsc()); - ByteArrayEntity entity = new ByteArrayEntity(mms); + private static byte[] makePost(Context context, MmsConnectionParameters parameters, byte[] mms) throws ClientProtocolException, IOException { + AndroidHttpClient client = null; - entity.setContentType("application/vnd.wap.mms-message"); + try { + Log.w("MmsSender", "Sending MMS1 of length: " + mms.length); + client = constructHttpClient(context, parameters); + URI targetUrl = new URI(parameters.getMmsc()); + HttpHost target = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME); + HttpPost request = new HttpPost(parameters.getMmsc()); + ByteArrayEntity entity = new ByteArrayEntity(mms); - request.setEntity(entity); - request.setParams(client.getParams()); - request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); + entity.setContentType("application/vnd.wap.mms-message"); - HttpResponse response = client.execute(request); - StatusLine status = response.getStatusLine(); + request.setEntity(entity); + request.setParams(client.getParams()); + request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); - if (status.getStatusCode() != 200) - throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase()); + HttpResponse response = client.execute(target, request); + StatusLine status = response.getStatusLine(); - return parseResponse(response.getEntity()); + if (status.getStatusCode() != 200) + throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase()); + + return parseResponse(response.getEntity()); + } catch (URISyntaxException use) { + Log.w("MmsSendHelper", use); + throw new IOException("Couldn't parse URI."); + } finally { + if (client != null) + client.close(); + } } public static byte[] sendMms(Context context, byte[] mms, String apn) throws IOException { @@ -56,7 +71,7 @@ public class MmsSendHelper extends MmsCommunication { try { MmsConnectionParameters parameters = getMmsConnectionParameters(context, apn); checkRouteToHost(context, parameters, parameters.getMmsc()); - return makePost(parameters, mms); + return makePost(context, parameters, mms); } catch (ApnUnavailableException aue) { Log.w("MmsSender", aue); throw new IOException("Failed to get MMSC information...");