Bug 1357997 - Use ProxySelector.openConnection instead of url.openConnection. draft
authorJonathan Hao <jhao@mozilla.com>
Tue, 25 Apr 2017 12:01:46 +0800
changeset 567532 7ecb9abe0c633a7ab1b95d62c3370e4a5ea15dd8
parent 566156 8b854986038cf3f3f240697e27ef48ea65914c13
child 625687 fb560f242be09138cbd7cbe3c7940bd4974ef805
push id55613
push userbmo:jhao@mozilla.com
push dateTue, 25 Apr 2017 07:30:11 +0000
bugs1357997
milestone55.0a1
Bug 1357997 - Use ProxySelector.openConnection instead of url.openConnection. MozReview-Commit-ID: CAF2Ir81Fq
mobile/android/base/java/org/mozilla/gecko/CrashReporter.java
mobile/android/base/java/org/mozilla/gecko/SuggestClient.java
mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java
mobile/android/base/java/org/mozilla/gecko/feeds/FeedFetcher.java
mobile/android/base/java/org/mozilla/gecko/search/SearchEngineManager.java
mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/GeckoMediaDrmBridgeV21.java
--- a/mobile/android/base/java/org/mozilla/gecko/CrashReporter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/CrashReporter.java
@@ -12,23 +12,25 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.InputStreamReader;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
-import java.net.URL;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.channels.Channels;
 import java.nio.channels.FileChannel;
 import java.security.MessageDigest;
 import java.util.zip.GZIPOutputStream;
 
 import org.mozilla.gecko.AppConstants.Versions;
+import org.mozilla.gecko.util.ProxySelector;
 
 import android.annotation.SuppressLint;
 import android.app.AlertDialog;
 import android.app.ProgressDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Build;
@@ -403,18 +405,18 @@ public class CrashReporter extends AppCo
         String spec = extras.get(SERVER_URL_KEY);
         if (spec == null) {
             doFinish();
             return;
         }
 
         Log.i(LOGTAG, "server url: " + spec);
         try {
-            URL url = new URL(spec);
-            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+            URI uri = new URI(spec);
+            HttpURLConnection conn = (HttpURLConnection)ProxySelector.openConnectionWithProxy(uri);
             conn.setRequestMethod("POST");
             String boundary = generateBoundary();
             conn.setDoOutput(true);
             conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
             conn.setRequestProperty("Content-Encoding", "gzip");
 
             OutputStream os = new GZIPOutputStream(conn.getOutputStream());
             for (String key : extras.keySet()) {
@@ -493,16 +495,18 @@ public class CrashReporter extends AppCo
                 fos.write("Crash ID: ".getBytes());
                 fos.write(crashid.getBytes());
                 fos.close();
             } else {
                 Log.i(LOGTAG, "Received failure HTTP response code from server: " + conn.getResponseCode());
             }
         } catch (IOException e) {
             Log.e(LOGTAG, "exception during send: ", e);
+        } catch (URISyntaxException e) {
+            Log.e(LOGTAG, "exception during new URI: ", e);
         }
 
         doFinish();
     }
 
     private void doRestart() {
         try {
             String action = "android.intent.action.MAIN";
--- a/mobile/android/base/java/org/mozilla/gecko/SuggestClient.java
+++ b/mobile/android/base/java/org/mozilla/gecko/SuggestClient.java
@@ -11,16 +11,17 @@ import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLEncoder;
 import java.util.ArrayList;
 
 import org.json.JSONArray;
 import org.mozilla.gecko.annotation.RobocopTarget;
 import org.mozilla.gecko.util.HardwareUtils;
+import org.mozilla.gecko.util.ProxySelector;
 
 import android.content.Context;
 import android.text.TextUtils;
 import android.util.Log;
 import org.mozilla.gecko.util.NetworkUtils;
 import org.mozilla.gecko.util.StringUtils;
 
 /**
@@ -84,17 +85,17 @@ public class SuggestClient {
             String encoded = URLEncoder.encode(query, "UTF-8");
             String suggestUri = mSuggestTemplate.replace("__searchTerms__", encoded);
 
             URL url = new URL(suggestUri);
             String json = null;
             HttpURLConnection urlConnection = null;
             InputStream in = null;
             try {
-                urlConnection = (HttpURLConnection) url.openConnection();
+                urlConnection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(url.toURI());
                 urlConnection.setConnectTimeout(mTimeout);
                 urlConnection.setRequestProperty("User-Agent", USER_AGENT);
                 in = new BufferedInputStream(urlConnection.getInputStream());
                 json = convertStreamToString(in);
             } finally {
                 if (urlConnection != null)
                     urlConnection.disconnect();
                 if (in != null) {
--- a/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java
+++ b/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java
@@ -42,16 +42,17 @@ import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoSharedPrefs;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.annotation.JNITarget;
 import org.mozilla.gecko.util.FileUtils;
 import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.util.HardwareUtils;
+import org.mozilla.gecko.util.ProxySelector;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.app.Activity;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.SystemClock;
 import android.support.annotation.WorkerThread;
 import android.telephony.TelephonyManager;
@@ -537,17 +538,17 @@ public class Distribution {
         if (uri == null) {
             return false;
         }
 
         long start = SystemClock.uptimeMillis();
         Log.v(LOGTAG, "Downloading referred distribution: " + uri);
 
         try {
-            final HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
+            final HttpURLConnection connection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(uri);
 
             // If the Search Activity starts, and we handle the referrer intent, this'll return
             // null. Recover gracefully in this case.
             final GeckoAppShell.GeckoInterface geckoInterface = GeckoAppShell.getGeckoInterface();
             final String ua;
             if (geckoInterface == null) {
                 // Fall back to GeckoApp's default implementation.
                 ua = HardwareUtils.isTablet() ? AppConstants.USER_AGENT_FENNEC_TABLET :
--- a/mobile/android/base/java/org/mozilla/gecko/feeds/FeedFetcher.java
+++ b/mobile/android/base/java/org/mozilla/gecko/feeds/FeedFetcher.java
@@ -6,22 +6,24 @@
 package org.mozilla.gecko.feeds;
 
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 
 import org.mozilla.gecko.feeds.parser.Feed;
 import org.mozilla.gecko.feeds.parser.SimpleFeedParser;
 import org.mozilla.gecko.util.IOUtils;
+import org.mozilla.gecko.util.ProxySelector;
 
 import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
-import java.net.URL;
+import java.net.URI;
+import java.net.URISyntaxException;
 
 import ch.boye.httpclientandroidlib.util.TextUtils;
 
 /**
  * Helper class for fetching and parsing a feed.
  */
 public class FeedFetcher {
     private static final int CONNECT_TIMEOUT = 15000;
@@ -58,17 +60,17 @@ public class FeedFetcher {
      * @return A FeedResponse or null if no feed could be fetched (error or no new version available)
      */
     @Nullable
     public static FeedResponse fetchAndParseFeedIfModified(@NonNull String url, @Nullable String eTag, @Nullable String lastModified) {
         HttpURLConnection connection = null;
         InputStream stream = null;
 
         try {
-            connection = (HttpURLConnection) new URL(url).openConnection();
+            connection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(new URI(url));
             connection.setInstanceFollowRedirects(true);
             connection.setConnectTimeout(CONNECT_TIMEOUT);
             connection.setReadTimeout(READ_TIMEOUT);
 
             if (!TextUtils.isEmpty(eTag)) {
                 connection.setRequestProperty("If-None-Match", eTag);
             }
 
@@ -95,16 +97,18 @@ public class FeedFetcher {
             final SimpleFeedParser parser = new SimpleFeedParser();
             final Feed feed = parser.parse(stream);
 
             return new FeedResponse(feed, responseEtag, updatedLastModified);
         } catch (IOException e) {
             return null;
         } catch (SimpleFeedParser.ParserException e) {
             return null;
+        } catch (URISyntaxException e) {
+            return null;
         } finally {
             if (connection != null) {
                 connection.disconnect();
             }
             IOUtils.safeStreamClose(stream);
         }
     }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/search/SearchEngineManager.java
+++ b/mobile/android/base/java/org/mozilla/gecko/search/SearchEngineManager.java
@@ -18,33 +18,34 @@ import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.GeckoSharedPrefs;
 import org.mozilla.gecko.Locales;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.distribution.Distribution;
 import org.mozilla.gecko.util.FileUtils;
 import org.mozilla.gecko.util.GeckoJarReader;
 import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.util.IOUtils;
+import org.mozilla.gecko.util.ProxySelector;
 import org.mozilla.gecko.util.RawResource;
 import org.mozilla.gecko.util.StringUtils;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.xmlpull.v1.XmlPullParserException;
 
 import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.lang.ref.WeakReference;
 import java.net.HttpURLConnection;
-import java.net.URL;
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Locale;
 import org.json.JSONObject;
 import org.json.JSONArray;
 
 /**
  * This class is not thread-safe, except where otherwise noted.
@@ -383,18 +384,18 @@ public class SearchEngineManager impleme
         if (region != null) {
             return region;
         }
 
         // Since we didn't have a cached code, we need to fetch a code from the service.
         try {
             String responseText = null;
 
-            URL url = new URL(GEOIP_LOCATION_URL);
-            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
+            URI uri = new URI(GEOIP_LOCATION_URL);
+            HttpURLConnection urlConnection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(uri);
             try {
                 // POST an empty JSON object.
                 final String message = "{}";
 
                 urlConnection.setDoOutput(true);
                 urlConnection.setConnectTimeout(10000);
                 urlConnection.setReadTimeout(10000);
                 urlConnection.setRequestMethod("POST");
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/GeckoMediaDrmBridgeV21.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/GeckoMediaDrmBridgeV21.java
@@ -4,17 +4,18 @@
 
 package org.mozilla.gecko.media;
 
 import java.lang.*;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
-import java.net.URL;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URLEncoder;
 import java.nio.ByteBuffer;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.UUID;
 import java.util.ArrayDeque;
 
 import android.annotation.SuppressLint;
@@ -25,16 +26,17 @@ import android.media.DeniedByServerExcep
 import android.media.MediaCrypto;
 import android.media.MediaCryptoException;
 import android.media.MediaDrm;
 import android.media.MediaDrmException;
 import android.media.NotProvisionedException;
 import android.util.Log;
 
 import org.mozilla.gecko.util.StringUtils;
+import org.mozilla.gecko.util.ProxySelector;
 
 public class GeckoMediaDrmBridgeV21 implements GeckoMediaDrm {
     protected final String LOGTAG;
     private static final String INVALID_SESSION_ID = "Invalid";
     private static final String WIDEVINE_KEY_SYSTEM = "com.widevine.alpha";
     private static final boolean DEBUG = false;
     private static final UUID WIDEVINE_SCHEME_UUID =
         new UUID(0xedef8ba979d64aceL, 0xa3c827dcd51d21edL);
@@ -448,20 +450,20 @@ public class GeckoMediaDrmBridgeV21 impl
             this.mPromiseId = promiseId;
             this.mURL = url;
             this.mDrmRequest = drmRequest;
         }
 
         @Override
         protected Void doInBackground(Void... params) {
             try {
-                URL finalURL = new URL(mURL + "&signedRequest=" + URLEncoder.encode(new String(mDrmRequest), "UTF-8"));
-                HttpURLConnection urlConnection = (HttpURLConnection) finalURL.openConnection();
+                URI finalURI = new URI(mURL + "&signedRequest=" + URLEncoder.encode(new String(mDrmRequest), "UTF-8"));
+                HttpURLConnection urlConnection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(finalURI);
                 urlConnection.setRequestMethod("POST");
-                if (DEBUG) Log.d(LOGTAG, "Provisioning, posting url =" + finalURL.toString());
+                if (DEBUG) Log.d(LOGTAG, "Provisioning, posting url =" + finalURI.toString());
 
                 // Add data
                 urlConnection.setRequestProperty("Accept", "*/*");
                 urlConnection.setRequestProperty("User-Agent", getCDMUserAgent());
                 urlConnection.setRequestProperty("Content-Type", "application/json");
 
                 // Execute HTTP Post Request
                 urlConnection.connect();
@@ -480,16 +482,18 @@ public class GeckoMediaDrmBridgeV21 impl
                     mResponseBody = String.valueOf(response).getBytes(StringUtils.UTF_8);
                     if (DEBUG) Log.d(LOGTAG, "Provisioning, response received.");
                     if (mResponseBody != null) Log.d(LOGTAG, "response length=" + mResponseBody.length);
                 } else {
                     Log.d(LOGTAG, "Provisioning, server returned HTTP error code :" + responseCode);
                 }
             } catch (IOException e) {
                 Log.e(LOGTAG, "Got exception during posting provisioning request ...", e);
+            } catch (URISyntaxException e) {
+                Log.e(LOGTAG, "Got exception during creating uri ...", e);
             }
             return null;
         }
 
         @Override
         protected void onPostExecute(Void v) {
             onProvisionResponse(mPromiseId, mResponseBody);
         }