Redact MMS message URL logging

Sprint embeds phone number into message URL.

b/19444241

Change-Id: I19505816d6b0639e2bd66bcfe12b53a6531db766
diff --git a/src/com/android/mms/service/MmsHttpClient.java b/src/com/android/mms/service/MmsHttpClient.java
index 281d9b6..1099b64 100644
--- a/src/com/android/mms/service/MmsHttpClient.java
+++ b/src/com/android/mms/service/MmsHttpClient.java
@@ -105,7 +105,7 @@
     public byte[] execute(String urlString, byte[] pdu, String method, boolean isProxySet,
             String proxyHost, int proxyPort, MmsConfig.Overridden mmsConfig)
             throws MmsHttpException {
-        Log.d(MmsService.TAG, "HTTP: " + method + " " + urlString
+        Log.d(MmsService.TAG, "HTTP: " + method + " " + redactUrlForNonVerbose(urlString)
                 + (isProxySet ? (", proxy=" + proxyHost + ":" + proxyPort) : "")
                 + ", PDU size=" + (pdu != null ? pdu.length : 0));
         checkMethod(method);
@@ -193,11 +193,13 @@
                     + (responseBody != null ? responseBody.length : 0));
             return responseBody;
         } catch (MalformedURLException e) {
-            Log.e(MmsService.TAG, "HTTP: invalid URL " + urlString, e);
-            throw new MmsHttpException(0/*statusCode*/, "Invalid URL " + urlString, e);
+            final String redactedUrl = redactUrlForNonVerbose(urlString);
+            Log.e(MmsService.TAG, "HTTP: invalid URL " + redactedUrl, e);
+            throw new MmsHttpException(0/*statusCode*/, "Invalid URL " + redactedUrl, e);
         } catch (ProtocolException e) {
-            Log.e(MmsService.TAG, "HTTP: invalid URL protocol " + urlString, e);
-            throw new MmsHttpException(0/*statusCode*/, "Invalid URL protocol " + urlString, e);
+            final String redactedUrl = redactUrlForNonVerbose(urlString);
+            Log.e(MmsService.TAG, "HTTP: invalid URL protocol " + redactedUrl, e);
+            throw new MmsHttpException(0/*statusCode*/, "Invalid URL protocol " + redactedUrl, e);
         } catch (IOException e) {
             Log.e(MmsService.TAG, "HTTP: IO failure", e);
             throw new MmsHttpException(0/*statusCode*/, e);
@@ -380,4 +382,35 @@
             }
         }
     }
+
+    /**
+     * Redact the URL for non-VERBOSE logging. Replace url with only the host part and the length
+     * of the input URL string.
+     *
+     * @param urlString
+     * @return
+     */
+    public static String redactUrlForNonVerbose(String urlString) {
+        if (Log.isLoggable(MmsService.TAG, Log.VERBOSE)) {
+            // Don't redact for VERBOSE level logging
+            return urlString;
+        }
+        if (TextUtils.isEmpty(urlString)) {
+            return urlString;
+        }
+        String protocol = "http";
+        String host = "";
+        try {
+            final URL url = new URL(urlString);
+            protocol = url.getProtocol();
+            host = url.getHost();
+        } catch (MalformedURLException e) {
+            // Ignore
+        }
+        // Print "http://host[length]"
+        final StringBuilder sb = new StringBuilder();
+        sb.append(protocol).append("://").append(host)
+                .append("[").append(urlString.length()).append("]");
+        return sb.toString();
+    }
 }
diff --git a/src/com/android/mms/service/MmsService.java b/src/com/android/mms/service/MmsService.java
index 09a86a9..8c31e71 100644
--- a/src/com/android/mms/service/MmsService.java
+++ b/src/com/android/mms/service/MmsService.java
@@ -64,7 +64,6 @@
 import java.util.List;
 import java.util.Queue;
 import java.util.concurrent.Callable;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
@@ -204,7 +203,7 @@
         public void downloadMessage(int subId, String callingPkg, String locationUrl,
                 Uri contentUri, Bundle configOverrides,
                 PendingIntent downloadedIntent) throws RemoteException {
-            Log.d(TAG, "downloadMessage: " + locationUrl);
+            Log.d(TAG, "downloadMessage: " + MmsHttpClient.redactUrlForNonVerbose(locationUrl));
             enforceSystemUid();
             // Make sure the subId is correct
             subId = checkSubId(subId);
diff --git a/src/com/android/mms/service/PhoneUtils.java b/src/com/android/mms/service/PhoneUtils.java
index fc1ff4b..8de881a 100644
--- a/src/com/android/mms/service/PhoneUtils.java
+++ b/src/com/android/mms/service/PhoneUtils.java
@@ -60,12 +60,12 @@
             if (phoneNumberUtil.isValidNumber(phoneNumber)) {
                 return phoneNumber;
             } else {
-                Log.e(MmsService.TAG, "getParsedNumber: not a valid phone number " + phoneText
+                Log.e(MmsService.TAG, "getParsedNumber: not a valid phone number"
                         + " for country " + country);
                 return null;
             }
         } catch (final NumberParseException e) {
-            Log.e(MmsService.TAG, "getParsedNumber: Not able to parse phone number " + phoneText);
+            Log.e(MmsService.TAG, "getParsedNumber: Not able to parse phone number");
             return null;
         }
     }