Skip to content

Commit

Permalink
fix #2293: remove leading double slash in URLs returned by wporg servers
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Feb 3, 2015
1 parent 7c3ec06 commit 29beaed
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import android.webkit.MimeTypeMap;
import android.webkit.URLUtil;

import org.wordpress.android.util.AppLog.T;

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;
Expand Down Expand Up @@ -52,11 +55,30 @@ public static String convertUrlToPunycodeIfNeeded(String url) {
return url;
}

/**
* Remove leading double slash, and inherit protocol scheme
*/
public static String removeLeadingDoubleSlash(String url, String scheme) {
if (url != null && url.startsWith("//")) {
url = url.substring(2);
if (scheme != null) {
if (scheme.endsWith("://")){
url = scheme + url;
} else {
AppLog.e(T.UTILS, "Invalid scheme used: " + scheme);
}
}
}
return url;
}

public static String addUrlSchemeIfNeeded(String url, boolean isHTTPS) {
if (url == null) {
return null;
}

url = removeLeadingDoubleSlash(url, (isHTTPS ? "https" : "http") + "://");

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

0 comments on commit 29beaed

Please sign in to comment.