mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-22 16:07:30 +00:00
Switch to AndroidHttpClient and explicit targeting.
This commit is contained in:
parent
1e2adadae2
commit
df05508a6f
@ -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 {
|
||||||
|
@ -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);
|
|
||||||
|
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.setParams(client.getParams());
|
||||||
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
||||||
|
|
||||||
HttpResponse response = client.execute(request);
|
// 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");
|
||||||
|
|
||||||
|
HttpResponse response = client.execute(target, request);
|
||||||
StatusLine status = response.getStatusLine();
|
StatusLine status = response.getStatusLine();
|
||||||
|
|
||||||
if (status.getStatusCode() != 200)
|
if (status.getStatusCode() != 200)
|
||||||
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());
|
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());
|
||||||
|
|
||||||
return parseResponse(response.getEntity());
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,22 +17,30 @@
|
|||||||
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 {
|
||||||
|
AndroidHttpClient client = null;
|
||||||
|
|
||||||
|
try {
|
||||||
Log.w("MmsSender", "Sending MMS1 of length: " + mms.length);
|
Log.w("MmsSender", "Sending MMS1 of length: " + mms.length);
|
||||||
HttpClient client = constructHttpClient(parameters);
|
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());
|
HttpPost request = new HttpPost(parameters.getMmsc());
|
||||||
ByteArrayEntity entity = new ByteArrayEntity(mms);
|
ByteArrayEntity entity = new ByteArrayEntity(mms);
|
||||||
|
|
||||||
@ -42,13 +50,20 @@ public class MmsSendHelper extends MmsCommunication {
|
|||||||
request.setParams(client.getParams());
|
request.setParams(client.getParams());
|
||||||
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
|
||||||
|
|
||||||
HttpResponse response = client.execute(request);
|
HttpResponse response = client.execute(target, request);
|
||||||
StatusLine status = response.getStatusLine();
|
StatusLine status = response.getStatusLine();
|
||||||
|
|
||||||
if (status.getStatusCode() != 200)
|
if (status.getStatusCode() != 200)
|
||||||
throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());
|
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 {
|
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...");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user