Skip to content

Commit 02c361f

Browse files
committed
fix #2293: remove leading double slash in URLs returned by wporg servers
1 parent 8ff9413 commit 02c361f

File tree

1 file changed

+24
-1
lines changed
  • WordPressUtils/src/main/java/org/wordpress/android/util

1 file changed

+24
-1
lines changed

WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import android.webkit.MimeTypeMap;
55
import android.webkit.URLUtil;
66

7+
import org.wordpress.android.util.AppLog.T;
8+
79
import java.io.UnsupportedEncodingException;
810
import java.net.IDN;
911
import java.net.URI;
12+
import java.net.URL;
1013
import java.net.URLDecoder;
1114
import java.net.URLEncoder;
1215
import java.nio.charset.Charset;
@@ -52,11 +55,30 @@ public static String convertUrlToPunycodeIfNeeded(String url) {
5255
return url;
5356
}
5457

58+
/**
59+
* Remove leading double slash, and inherit protocol scheme
60+
*/
61+
public static String removeLeadingDoubleSlash(String url, String scheme) {
62+
if (url != null && url.startsWith("//")) {
63+
url = url.substring(2);
64+
if (scheme != null) {
65+
if (scheme.endsWith("://")){
66+
url = scheme + url;
67+
} else {
68+
AppLog.e(T.UTILS, "Invalid scheme used: " + scheme);
69+
}
70+
}
71+
}
72+
return url;
73+
}
74+
5575
public static String addUrlSchemeIfNeeded(String url, boolean isHTTPS) {
5676
if (url == null) {
5777
return null;
5878
}
5979

80+
url = removeLeadingDoubleSlash(url, (isHTTPS ? "https" : "http") + "://");
81+
6082
if (!URLUtil.isValidUrl(url)) {
6183
if (!(url.toLowerCase().startsWith("http://")) && !(url.toLowerCase().startsWith("https://"))) {
6284
url = (isHTTPS ? "https" : "http") + "://" + url;
@@ -78,7 +100,8 @@ public static String normalizeUrl(final String urlString) {
78100
// this routine is called from some performance-critical code and creating a URI from a string
79101
// is slow, so skip it when possible - if we know it's not a relative path (and 99.9% of the
80102
// time it won't be for our purposes) then we can normalize it without java.net.URI.normalize()
81-
if (urlString.startsWith("http") && !urlString.contains("build/intermediates/exploded-aar/org.wordpress/graphview/3.1.1")) {
103+
if (urlString.startsWith("http") &&
104+
!urlString.contains("build/intermediates/exploded-aar/org.wordpress/graphview/3.1.1")) {
82105
// return without a trailing slash
83106
if (urlString.endsWith("/")) {
84107
return urlString.substring(0, urlString.length() - 1);

0 commit comments

Comments
 (0)