Switch to AndroidHttpClient and explicit targeting.

This commit is contained in:
Moxie Marlinspike 2013-02-17 22:32:17 -08:00
parent 1e2adadae2
commit df05508a6f
3 changed files with 73 additions and 52 deletions

View File

@ -22,22 +22,13 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteException;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Uri; import android.net.Uri;
import android.net.http.AndroidHttpClient;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; 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.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.HttpConnectionParams;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams; import org.apache.http.params.HttpProtocolParams;
@ -126,26 +117,17 @@ public class MmsCommunication {
} }
protected static HttpClient constructHttpClient(MmsConnectionParameters mmsConfig) { protected static AndroidHttpClient constructHttpClient(Context context, MmsConnectionParameters mmsConfig) {
HttpParams params = new BasicHttpParams(); AndroidHttpClient client = AndroidHttpClient.newInstance("TextSecure/0.1", context);
HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpParams params = client.getParams();
HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
HttpConnectionParams.setSoTimeout(params, 20 * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);
HttpClientParams.setRedirecting(params, false);
HttpProtocolParams.setUserAgent(params, "TextSecure/0.1");
HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpConnectionParams.setSoTimeout(params, 20 * 1000);
if (mmsConfig.hasProxy()) { if (mmsConfig.hasProxy()) {
ConnRouteParams.setDefaultProxy(params, new HttpHost(mmsConfig.getProxy(), mmsConfig.getPort())); ConnRouteParams.setDefaultProxy(params, new HttpHost(mmsConfig.getProxy(), mmsConfig.getPort()));
} }
SchemeRegistry schemeRegistry = new SchemeRegistry(); return client;
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);
} }
protected static byte[] parseResponse(HttpEntity entity) throws IOException { protected static byte[] parseResponse(HttpEntity entity) throws IOException {

View File

@ -17,34 +17,58 @@
package org.thoughtcrime.securesms.mms; package org.thoughtcrime.securesms.mms;
import android.content.Context; import android.content.Context;
import android.net.http.AndroidHttpClient;
import android.util.Log; import android.util.Log;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.StatusLine; import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class MmsDownloadHelper extends MmsCommunication { 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 throws ClientProtocolException, IOException
{ {
HttpClient client = constructHttpClient(connectionParameters); AndroidHttpClient client = null;
HttpGet request = new HttpGet(url);
request.setParams(client.getParams()); try {
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); 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); request.setParams(client.getParams());
StatusLine status = response.getStatusLine(); request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
if (status.getStatusCode() != 200) // java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase()); // 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 { 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); checkRouteToHost(context, connectionParameters, url);
return makeRequest(connectionParameters, url); return makeRequest(context, connectionParameters, url);
} }
} }

View File

@ -17,38 +17,53 @@
package org.thoughtcrime.securesms.mms; package org.thoughtcrime.securesms.mms;
import android.content.Context; import android.content.Context;
import android.net.http.AndroidHttpClient;
import android.util.Log; import android.util.Log;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.StatusLine; import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ByteArrayEntity;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class MmsSendHelper extends MmsCommunication { public class MmsSendHelper extends MmsCommunication {
private static byte[] makePost(MmsConnectionParameters parameters, byte[] mms) throws ClientProtocolException, IOException { private static byte[] makePost(Context context, MmsConnectionParameters parameters, byte[] mms) throws ClientProtocolException, IOException {
Log.w("MmsSender", "Sending MMS1 of length: " + mms.length); AndroidHttpClient client = null;
HttpClient client = constructHttpClient(parameters);
HttpPost request = new HttpPost(parameters.getMmsc());
ByteArrayEntity entity = new ByteArrayEntity(mms);
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); entity.setContentType("application/vnd.wap.mms-message");
request.setParams(client.getParams());
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
HttpResponse response = client.execute(request); request.setEntity(entity);
StatusLine status = response.getStatusLine(); request.setParams(client.getParams());
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
if (status.getStatusCode() != 200) HttpResponse response = client.execute(target, request);
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase()); 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 { public static byte[] sendMms(Context context, byte[] mms, String apn) throws IOException {
@ -56,7 +71,7 @@ public class MmsSendHelper extends MmsCommunication {
try { try {
MmsConnectionParameters parameters = getMmsConnectionParameters(context, apn); MmsConnectionParameters parameters = getMmsConnectionParameters(context, apn);
checkRouteToHost(context, parameters, parameters.getMmsc()); checkRouteToHost(context, parameters, parameters.getMmsc());
return makePost(parameters, mms); return makePost(context, parameters, mms);
} catch (ApnUnavailableException aue) { } catch (ApnUnavailableException aue) {
Log.w("MmsSender", aue); Log.w("MmsSender", aue);
throw new IOException("Failed to get MMSC information..."); throw new IOException("Failed to get MMSC information...");