Skip to content

Commit

Permalink
Merge pull request #2295 from wordpress-mobile/issue/2293-crash-url-n…
Browse files Browse the repository at this point in the history
…o-scheme

fix #2293: remove leading double slash in URLs returned by wporg servers
  • Loading branch information
nbradbury committed Feb 5, 2015
2 parents 9659e26 + ed90c67 commit 7226c3d
Show file tree
Hide file tree
Showing 2 changed files with 40 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,40 @@ 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;
}

/**
* Add scheme prefix to an URL. This method must be called on all user entered or server fetched URLs to ensure
* http client will work as expected.
*
* @param url url entered by the user or fetched from a server
* @param isHTTPS true will make the url starts with https;//
* @return transformed url prefixed by its http;// or https;// scheme
*/
public static String addUrlSchemeIfNeeded(String url, boolean isHTTPS) {
if (url == null) {
return null;
}

// Remove leading double slash (eg. //example.com), needed for some wporg instances configured to
// switch between http or https
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 +110,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 7226c3d

Please sign in to comment.