From 2a47170c8fe6a0f11e41e2feb4a1ed55eb305335 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Mon, 21 Dec 2015 12:29:46 +0100 Subject: [PATCH] Add utility methods to check if a URL is wpcom. Add utility methods that check if it's safe to add the Authentication token to the request. --- .../wordpress/android/util/UrlUtilsTest.java | 30 +++++++++++++++++++ .../org/wordpress/android/util/UrlUtils.java | 12 ++++++++ 2 files changed, 42 insertions(+) diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java index 0b4ff2b62932..abf7a8fae526 100644 --- a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java @@ -2,6 +2,8 @@ import android.test.InstrumentationTestCase; +import java.net.MalformedURLException; +import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -75,4 +77,32 @@ public void testAppendUrlParameters2() { assertTrue("failed test on url: " + url, false); } } + + public void testHttps1() { + assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php"))); + } + + public void testHttps2() { + assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com#.b.com/test"))); + } + + public void testHttps3() { + assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php"))); + } + + public void testHttps4() { + assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com"))); + } + + public void testHttps5() { + assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com/test#test"))); + } + + private URL buildURL(String address) { + URL url = null; + try { + url = new URL(address); + } catch (MalformedURLException e) {} + return url; + } } diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java index 3a0d7fd59a28..6843a87fb676 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java @@ -10,6 +10,7 @@ import java.io.UnsupportedEncodingException; import java.net.IDN; import java.net.URI; +import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.Charset; @@ -172,6 +173,17 @@ public static boolean isHttps(final String urlString) { return (urlString != null && urlString.startsWith("https:")); } + public static boolean isHttps(URL url) { + return url != null && "https".equals(url.getProtocol()); + } + + public static boolean isHttps(URI uri) { + if (uri == null) return false; + + String protocol = uri.getScheme(); + return protocol != null && protocol.equals("https"); + } + /** * returns https: version of passed http: url */