Switch to AndroidHttpClient and explicit targeting.

pull/1/head
Moxie Marlinspike 12 years ago
parent 1e2adadae2
commit df05508a6f

@ -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 {

@ -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());
request.setParams(client.getParams());
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
HttpResponse response = client.execute(request);
StatusLine status = response.getStatusLine();
// 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");
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("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);
}
}

@ -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);
entity.setContentType("application/vnd.wap.mms-message");
request.setEntity(entity);
request.setParams(client.getParams());
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
request.setEntity(entity);
request.setParams(client.getParams());
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
HttpResponse response = client.execute(request);
StatusLine status = response.getStatusLine();
HttpResponse response = client.execute(target, request);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() != 200)
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());
if (status.getStatusCode() != 200)
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());
return parseResponse(response.getEntity());
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...");

Loading…
Cancel
Save