From b8a3e87f3d5077ab99b50850a13f1822e2f6d4b0 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Wed, 10 Sep 2014 00:20:07 -0400 Subject: [PATCH] custom redirect logic // FREEBIE --- .../securesms/mms/MmsCommunication.java | 4 +- .../securesms/mms/MmsDownloadHelper.java | 59 ++++++++----- .../securesms/mms/MmsSendHelper.java | 86 +++++++++++-------- 3 files changed, 92 insertions(+), 57 deletions(-) diff --git a/src/org/thoughtcrime/securesms/mms/MmsCommunication.java b/src/org/thoughtcrime/securesms/mms/MmsCommunication.java index 1ca2ae9253..bbe385f359 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsCommunication.java +++ b/src/org/thoughtcrime/securesms/mms/MmsCommunication.java @@ -44,6 +44,8 @@ import java.util.List; public class MmsCommunication { private static final String TAG = "MmsCommunication"; + public static final int MAX_REDIRECTS = 10; + protected static MmsConnectionParameters getLocallyConfiguredMmsConnectionParameters(Context context) throws ApnUnavailableException { @@ -180,7 +182,7 @@ public class MmsCommunication { urlConnection = (HttpURLConnection) url.openConnection(); } - urlConnection.setInstanceFollowRedirects(true); + urlConnection.setInstanceFollowRedirects(false); 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 800fb7aefd..b48f7e1cc6 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java +++ b/src/org/thoughtcrime/securesms/mms/MmsDownloadHelper.java @@ -18,12 +18,15 @@ package org.thoughtcrime.securesms.mms; import android.content.Context; import android.net.Uri; +import android.text.TextUtils; import android.util.Log; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import ws.com.google.android.mms.pdu.PduParser; import ws.com.google.android.mms.pdu.RetrieveConf; @@ -36,29 +39,45 @@ public class MmsDownloadHelper extends MmsCommunication { { HttpURLConnection client = null; - try { - client = constructHttpClient(url, proxy, proxyPort); - - client.setDoInput(true); - client.setRequestMethod("GET"); - client.setRequestProperty("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); - - Log.w(TAG, "Connecting to " + url); - client.connect(); - - final InputStream is = client.getInputStream(); - final int responseCode = client.getResponseCode(); - - Log.w(TAG, "Response code: " + responseCode + "/" + client.getResponseMessage()); - - if (responseCode != 200) { - throw new IOException("non-200 response"); + int redirects = MAX_REDIRECTS; + final Set previousUrls = new HashSet(); + String currentUrl = url; + while (redirects-- > 0) { + if (previousUrls.contains(currentUrl)) { + throw new IOException("redirect loop detected"); } + try { + client = constructHttpClient(currentUrl, proxy, proxyPort); - return parseResponse(is); - } finally { - if (client != null) client.disconnect(); + client.setDoInput(true); + client.setRequestMethod("GET"); + client.setRequestProperty("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); + + Log.w(TAG, "connecting to " + currentUrl); + client.connect(); + + int responseCode = client.getResponseCode(); + Log.w(TAG, "* response code: " + responseCode + "/" + client.getResponseMessage()); + + if (responseCode == 301 || responseCode == 302) { + final String redirectUrl = client.getHeaderField("Location"); + Log.w(TAG, "* Location: " + redirectUrl); + if (TextUtils.isEmpty(redirectUrl)) { + throw new IOException("Got redirect response code, but Location header was empty or missing"); + } + previousUrls.add(currentUrl); + currentUrl = redirectUrl; + } else if (responseCode == 200) { + final InputStream is = client.getInputStream(); + return parseResponse(is); + } else { + throw new IOException("unhandled response code"); + } + } finally { + if (client != null) client.disconnect(); + } } + throw new IOException("max redirects hit"); } public static boolean isMmsConnectionParametersAvailable(Context context, String apn) { diff --git a/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java b/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java index eeb78ad8c1..d1391483fb 100644 --- a/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java +++ b/src/org/thoughtcrime/securesms/mms/MmsSendHelper.java @@ -20,17 +20,15 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; +import android.text.TextUtils; import android.util.Log; -import org.whispersystems.textsecure.util.Util; - -import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URISyntaxException; +import java.util.HashSet; +import java.util.Set; import ws.com.google.android.mms.pdu.PduParser; import ws.com.google.android.mms.pdu.SendConf; @@ -45,40 +43,56 @@ public class MmsSendHelper extends MmsCommunication { HttpURLConnection client = null; - try { - client = constructHttpClient(url, proxy, proxyPort); - client.setFixedLengthStreamingMode(mms.length); - client.setDoInput(true); - client.setDoOutput(true); - client.setRequestMethod("POST"); - 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 " + url); - client.connect(); - - 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 = client.getInputStream(); - final int responseCode = client.getResponseCode(); - - Log.w(TAG, "Response code: " + responseCode + "/" + client.getResponseMessage()); - - if (responseCode != 200) { - throw new IOException("non-200 response"); + int redirects = MAX_REDIRECTS; + final Set previousUrls = new HashSet(); + String currentUrl = url; + while (redirects-- > 0) { + if (previousUrls.contains(currentUrl)) { + throw new IOException("redirect loop detected"); } + try { + client = constructHttpClient(currentUrl, proxy, proxyPort); + client.setFixedLengthStreamingMode(mms.length); + client.setDoInput(true); + client.setDoOutput(true); + client.setRequestMethod("POST"); + 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"); - return parseResponse(is); - } finally { - if (client != null) client.disconnect(); + Log.w(TAG, "connecting to " + currentUrl); + client.connect(); + + 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"); + + int responseCode = client.getResponseCode(); + Log.w(TAG, "* response code: " + responseCode + "/" + client.getResponseMessage()); + + if (responseCode == 301 || responseCode == 302) { + final String redirectUrl = client.getHeaderField("Location"); + Log.w(TAG, "* Location: " + redirectUrl); + if (TextUtils.isEmpty(redirectUrl)) { + throw new IOException("Got redirect response code, but Location header was empty or missing"); + } + previousUrls.add(currentUrl); + currentUrl = redirectUrl; + } else if (responseCode == 200) { + final InputStream is = client.getInputStream(); + return parseResponse(is); + } else { + throw new IOException("unhandled response code"); + } + } finally { + if (client != null) client.disconnect(); + } } + throw new IOException("max redirects hit"); } public static void sendNotificationReceived(Context context, byte[] mms, String apn,