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 9659e26 commit 9d7f642
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ public void fetchBlogList(Callback callback) {
// Validate the URL found before calling the client. Prevent a crash that can occur
// during the setup of self-hosted sites.
URI xmlrpcUri;
xmlrpcUri = URI.create(xmlrpcUrl);
getBlogList(xmlrpcUri, callback);

xmlrpcUri = URI.create(xmlrpcUrl);
getBlogList(xmlrpcUri, callback);
}

private String getRsdUrl(String baseUrl) throws SSLHandshakeException {
Expand Down Expand Up @@ -243,7 +242,7 @@ private String getSelfHostedXmlrpcUrl(String url) {
// Attempt to get the XMLRPC URL via RSD
String rsdUrl;
try {
rsdUrl = getRsdUrl(url);
rsdUrl = UrlUtils.addUrlSchemeIfNeeded(getRsdUrl(url), false);
} catch (SSLHandshakeException e) {
if (!UrlUtils.getDomainFromUrl(url).endsWith("wordpress.com")) {
mErroneousSslCertificate = true;
Expand All @@ -254,12 +253,12 @@ private String getSelfHostedXmlrpcUrl(String url) {

try {
if (rsdUrl != null) {
xmlrpcUrl = ApiHelper.getXMLRPCUrl(rsdUrl);
xmlrpcUrl = UrlUtils.addUrlSchemeIfNeeded(ApiHelper.getXMLRPCUrl(rsdUrl), false);
if (xmlrpcUrl == null) {
xmlrpcUrl = rsdUrl.replace("?rsd", "");
xmlrpcUrl = UrlUtils.addUrlSchemeIfNeeded(rsdUrl.replace("?rsd", ""), false);
}
} else {
xmlrpcUrl = getXmlrpcByUserEnteredPath(url);
xmlrpcUrl = UrlUtils.addUrlSchemeIfNeeded(getXmlrpcByUserEnteredPath(url), false);
}
} catch (SSLHandshakeException e) {
if (!UrlUtils.getDomainFromUrl(url).endsWith("wordpress.com")) {
Expand Down
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 9d7f642

Please sign in to comment.