From f4e9c4a710915ba1e90975412d8f0e2185f86324 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Wed, 13 Aug 2014 14:39:58 -0700 Subject: [PATCH] Minor MMS cleanup. // FREEBIE Closes #1827 --- .../securesms/mms/MmsCommunication.java | 24 +++++++------ .../securesms/mms/MmsDownloadHelper.java | 18 ++++------ .../securesms/mms/MmsSendHelper.java | 36 ++++++++----------- 3 files changed, 33 insertions(+), 45 deletions(-) diff --git a/src/org/thoughtcrime/securesms/mms/MmsCommunication.java b/src/org/thoughtcrime/securesms/mms/MmsCommunication.java index 2c94f60a2b..e9c33ca0be 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsCommunication.java +++ b/src/org/thoughtcrime/securesms/mms/MmsCommunication.java @@ -89,9 +89,8 @@ public class MmsCommunication { cursor = DatabaseFactory.getMmsDatabase(context).getCarrierMmsInformation(apn); if (cursor == null || !cursor.moveToFirst()) { - MmsConnectionParameters parameters = getLocalMmsConnectionParameters(context); - Log.w(TAG, "Android didn't have a result, using MMSC parameters: " + parameters.get().get(0).getMmsc() + " // " + parameters.get().get(0).getProxy() + " // " + parameters.get().get(0).getPort()); - return parameters; + Log.w(TAG, "Android didn't have a result, querying local parameters."); + return getLocalMmsConnectionParameters(context); } do { @@ -100,15 +99,14 @@ public class MmsCommunication { String port = cursor.getString(cursor.getColumnIndexOrThrow("mmsport")); if (!Util.isEmpty(mmsc)) { - Log.w(TAG, "Using Android-provided MMSC parameters: " + mmsc + " // " + proxy + " // " + port); + Log.w(TAG, "Using Android-provided MMSC parameters."); return new MmsConnectionParameters(mmsc, proxy, port); } } while (cursor.moveToNext()); - MmsConnectionParameters parameters = getLocalMmsConnectionParameters(context); - Log.w(TAG, "Android didn't have a result, using MMSC parameters: " + parameters.get().get(0).getMmsc() + " // " + parameters.get().get(0).getProxy() + " // " + parameters.get().get(0).getPort()); - return parameters; + Log.w(TAG, "Android provided results were empty, querying local parameters."); + return getLocalMmsConnectionParameters(context); } catch (SQLiteException sqe) { Log.w(TAG, sqe); } catch (SecurityException se) { @@ -150,10 +148,12 @@ public class MmsCommunication { } protected static byte[] parseResponse(InputStream is) throws IOException { - InputStream in = new BufferedInputStream(is); + InputStream in = new BufferedInputStream(is); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Util.copy(in, baos); - Log.w(TAG, "received full server response, " + baos.size() + " bytes"); + + Log.w(TAG, "Received full server response, " + baos.size() + " bytes"); + return baos.toByteArray(); } @@ -162,14 +162,16 @@ public class MmsCommunication { { HttpURLConnection urlConnection; URL url = new URL(urlString); + if (proxy != null) { - Log.w(TAG, "constructing http client using a proxy"); + Log.w(TAG, String.format("Constructing http client using a proxy: (%s:%d)", proxy, port)); Proxy proxyRoute = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, port)); urlConnection = (HttpURLConnection) url.openConnection(proxyRoute); } else { - Log.w(TAG, "constructing http client without proxy"); + Log.w(TAG, "Constructing http client without proxy"); urlConnection = (HttpURLConnection) url.openConnection(); } + urlConnection.setConnectTimeout(20*1000); urlConnection.setReadTimeout(20*1000); urlConnection.setUseCaches(false); diff --git a/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java b/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java index cde018d8c8..dbcdc0eb5e 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java +++ b/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java @@ -21,7 +21,6 @@ import android.net.Uri; import android.util.Log; import java.io.IOException; -import java.io.InputStream; import java.net.HttpURLConnection; import ws.com.google.android.mms.pdu.PduParser; @@ -42,23 +41,18 @@ public class MmsDownloadHelper extends MmsCommunication { client.setRequestMethod("GET"); client.setRequestProperty("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); - Log.w(TAG, "connecting to " + url); + Log.w(TAG, "Connecting to " + url); client.connect(); - final InputStream is; - try { - is = client.getInputStream(); - } catch (IOException ioe) { - Log.w(TAG, "failed with response code " + client.getResponseCode() + " / " + client.getResponseMessage()); - throw ioe; - } + int responseCode = client.getResponseCode(); - Log.w(TAG, "response code was " + client.getResponseCode() + "/" + client.getResponseMessage()); - if (client.getResponseCode() != 200) { + Log.w(TAG, "Response code: " + responseCode + "/" + client.getResponseMessage()); + + if (responseCode != 200) { throw new IOException("non-200 response"); } - return parseResponse(is); + return parseResponse(client.getInputStream()); } finally { if (client != null) client.disconnect(); } diff --git a/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java b/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java index 75f0976d38..397553c01a 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java +++ b/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java @@ -51,38 +51,30 @@ public class MmsSendHelper extends MmsCommunication { client.setDoInput(true); client.setDoOutput(true); client.setRequestMethod("POST"); - URI targetUrl = new URI(url); - - if (Util.isEmpty(targetUrl.getHost())) - throw new IOException("Invalid target host: " + targetUrl.getHost() + " , " + targetUrl); - client.setRequestProperty("Content-Type", "application/vnd.wap.mms-message"); client.setRequestProperty("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); client.setRequestProperty("x-wap-profile", "http://www.google.com/oha/rdf/ua-profile-kila.xml"); - Log.w(TAG, "connecting to " + targetUrl); + Log.w(TAG, "Connecting to " + url); client.connect(); - Log.w(TAG, "writing mms payload, " + mms.length + " bytes"); - OutputStream out = new BufferedOutputStream(client.getOutputStream()); + + Log.w(TAG, "Writing mms payload, " + mms.length + " bytes"); + OutputStream out = client.getOutputStream(); out.write(mms); out.flush(); out.close(); - Log.w(TAG, "payload sent"); - final InputStream is; - try { - is = client.getInputStream(); - } catch (IOException ioe) { - Log.w(TAG, "failed with response code " + client.getResponseCode() + " / " + client.getResponseMessage()); - throw ioe; - } - Log.w(TAG, "response code was " + client.getResponseCode() + "/" + client.getResponseMessage()); - if (client.getResponseCode() != 200) { + + Log.w(TAG, "Payload sent"); + + int responseCode = client.getResponseCode(); + + Log.w(TAG, "Response code: " + responseCode + "/" + client.getResponseMessage()); + + if (responseCode != 200) { throw new IOException("non-200 response"); } - return parseResponse(is); - } catch (URISyntaxException use) { - Log.w(TAG, use); - throw new IOException("Couldn't parse URI."); + + return parseResponse(client.getInputStream()); } finally { if (client != null) client.disconnect(); }