Initial fixes for MMS retrieval.

1) Parse the APN information based on what the ConnectionManager
tells us.

2) Accept email addresses as a valid Recipient format.
This commit is contained in:
Moxie Marlinspike
2012-09-30 11:46:45 -07:00
parent 59e7226183
commit cf9dc51f31
9 changed files with 224 additions and 217 deletions

View File

@@ -1,6 +1,6 @@
/**
/**
* Copyright (C) 2011 Whisper Systems
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
@@ -10,15 +10,16 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.mms;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.InetAddress;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.Uri;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
@@ -42,57 +43,58 @@ import org.thoughtcrime.securesms.util.Conversions;
import ws.com.google.android.mms.MmsException;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.Uri;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.InetAddress;
public class MmsCommunication {
protected static MmsConnectionParameters getMmsConnectionParameters(Context context) throws MmsException {
Cursor cursor = DatabaseFactory.getMmsDatabase(context).getCarrierMmsInformation();
protected static MmsConnectionParameters getMmsConnectionParameters(Context context, String apn)
throws MmsException
{
Cursor cursor = DatabaseFactory.getMmsDatabase(context).getCarrierMmsInformation(apn);
try {
if (cursor == null || !cursor.moveToFirst())
throw new MmsException("No carrier MMS information available.");
throw new MmsException("No carrier MMS information available.");
do {
String mmsc = cursor.getString(cursor.getColumnIndexOrThrow("mmsc"));
String proxy = cursor.getString(cursor.getColumnIndexOrThrow("mmsproxy"));
String port = cursor.getString(cursor.getColumnIndexOrThrow("mmsport"));
if (mmsc != null && !mmsc.equals(""))
return new MmsConnectionParameters(mmsc, proxy, port);
} while (cursor.moveToNext());
throw new MmsException("No carrier MMS information available.");
} finally {
if (cursor != null)
cursor.close();
}
}
protected static void checkRouteToHost(Context context, MmsConnectionParameters parameters, String url) throws IOException {
if (parameters == null || !parameters.hasProxy())
checkRouteToHost(context, Uri.parse(url).getHost());
else
checkRouteToHost(context, parameters.getProxy());
}
private static void checkRouteToHost(Context context, String host) throws IOException {
InetAddress inetAddress = InetAddress.getByName(host);
byte[] ipAddressBytes = inetAddress.getAddress();
int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (!manager.requestRouteToHost(MmsDownloader.TYPE_MOBILE_MMS, ipAddress))
throw new IOException("Connection manager could not obtain route to host.");
throw new IOException("Connection manager could not obtain route to host.");
// if (!manager.requestRouteToHost(ConnectivityManager.TYPE_MOBILE, ipAddress))
// throw new IOException("Connection manager could not obtain route to host.");
// throw new IOException("Connection manager could not obtain route to host.");
}
protected static HttpClient constructHttpClient(MmsConnectionParameters mmsConfig) {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setStaleCheckingEnabled(params, false);
@@ -102,11 +104,11 @@ public class MmsCommunication {
HttpClientParams.setRedirecting(params, false);
HttpProtocolParams.setUserAgent(params, "TextSecure/0.1");
HttpProtocolParams.setContentCharset(params, "UTF-8");
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));
@@ -114,47 +116,47 @@ public class MmsCommunication {
ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
return new DefaultHttpClient(manager, params);
}
protected static byte[] parseResponse(HttpEntity entity) throws IOException {
if (entity == null || entity.getContentLength() == 0)
throw new IOException("Null response");
byte[] responseBytes = new byte[(int)entity.getContentLength()];
DataInputStream dataInputStream = new DataInputStream(entity.getContent());
dataInputStream.readFully(responseBytes);
dataInputStream.close();
entity.consumeContent();
return responseBytes;
return responseBytes;
}
protected static class MmsConnectionParameters {
private final String mmsc;
private final String proxy;
private final String port;
public MmsConnectionParameters(String mmsc, String proxy, String port) {
this.mmsc = mmsc;
this.proxy = proxy;
this.port = port;
}
public boolean hasProxy() {
return proxy != null && proxy.trim().length() != 0;
}
public String getMmsc() {
return mmsc;
}
public String getProxy() {
return proxy;
}
public int getPort() {
if (port == null || port.trim().length() == 0)
return 80;
return Integer.parseInt(port);
}
}

View File

@@ -1,6 +1,6 @@
/**
/**
* Copyright (C) 2011 Whisper Systems
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
@@ -10,15 +10,14 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.mms;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import android.content.Context;
import android.util.Log;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
@@ -30,38 +29,41 @@ import org.apache.http.client.methods.HttpGet;
import ws.com.google.android.mms.MmsException;
import android.content.Context;
import android.util.Log;
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) throws ClientProtocolException, IOException {
private static byte[] makeRequest(MmsConnectionParameters connectionParameters, String url)
throws ClientProtocolException, IOException
{
try {
HttpClient client = constructHttpClient(connectionParameters);
HttpClient client = constructHttpClient(connectionParameters);
URI hostUrl = new URI(url);
HttpHost target = new HttpHost(hostUrl.getHost(), hostUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
HttpRequest request = new HttpGet(url);
request.setParams(client.getParams());
request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");
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("MmsDownlader", use);
throw new IOException("Bad URI syntax");
}
}
public static byte[] retrieveMms(Context context, String url) throws IOException {
public static byte[] retrieveMms(Context context, String url, String apn) throws IOException {
try {
MmsConnectionParameters connectionParameters = getMmsConnectionParameters(context);
MmsConnectionParameters connectionParameters = getMmsConnectionParameters(context, apn);
checkRouteToHost(context, connectionParameters, url);
return makeRequest(connectionParameters, url);
} catch (MmsException me) {

View File

@@ -1,6 +1,6 @@
/**
/**
* Copyright (C) 2011 Whisper Systems
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
@@ -10,15 +10,14 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.mms;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import android.content.Context;
import android.util.Log;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
@@ -27,14 +26,15 @@ 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 org.thoughtcrime.securesms.util.Hex;
import ws.com.google.android.mms.MmsException;
import android.content.Context;
import android.util.Log;
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);
try {
@@ -43,30 +43,30 @@ public class MmsSendHelper extends MmsCommunication {
HttpHost target = new HttpHost(hostUrl.getHost(), hostUrl.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");
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("MmsDownlader", use);
throw new IOException("Bad URI syntax");
}
}
public static byte[] sendMms(Context context, byte[] mms) throws IOException {
public static byte[] sendMms(Context context, byte[] mms, String apn) throws IOException {
Log.w("MmsSender", "Sending MMS of length: " + mms.length);
try {
MmsConnectionParameters parameters = getMmsConnectionParameters(context);
MmsConnectionParameters parameters = getMmsConnectionParameters(context, apn);
checkRouteToHost(context, parameters, parameters.getMmsc());
return makePost(parameters, mms);
} catch (MmsException me) {