From c83c8301e6ccca7ee408c7b4b898e81f6c23e222 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 30 Mar 2018 13:28:13 +0200 Subject: [PATCH] improve URL normalizing in "Add Repo" and also support content:// URLs This adds some case normalization to both the scheme and the host. This was previously messing up TreeUri content:// URLs like this: content://com.android.externalstorage.documents/tree/1AFB-2402%3A/document/1AFB-2402%3Atesty.at.or.at%2Ffdroid%2Frepo Turning them into: content://com.android.externalstorage.documents/tree/1AFB-2402:/document/1AFB-2402:testy.at.or.at/fdroid/repo --- .../fdroid/views/ManageReposActivity.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java b/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java index 9417d3b65..a7ebeddbf 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/ManageReposActivity.java @@ -674,17 +674,24 @@ public class ManageReposActivity extends AppCompatActivity * Currently it normalizes the path so that "/./" are removed and "test/../" is collapsed. * This is done using {@link URI#normalize()}. It also removes multiple consecutive forward * slashes in the path and replaces them with one. Finally, it removes trailing slashes. + *

+ * {@code content://} URLs used for repos stored on removable storage get messed up by + * {@link URI}. */ private String normalizeUrl(String urlString) throws URISyntaxException { if (urlString == null) { return null; } - URI uri = new URI(urlString); + Uri uri = Uri.parse(urlString); if (!uri.isAbsolute()) { throw new URISyntaxException(urlString, "Must provide an absolute URI for repositories"); } - - uri = uri.normalize(); + if (!uri.isHierarchical()) { + throw new URISyntaxException(urlString, "Must provide an hierarchical URI for repositories"); + } + if ("content".equals(uri.getScheme())) { + return uri.toString(); + } String path = uri.getPath(); if (path != null) { path = path.replaceAll("//*/", "/"); // Collapse multiple forward slashes into 1. @@ -692,9 +699,13 @@ public class ManageReposActivity extends AppCompatActivity path = path.substring(0, path.length() - 1); } } - - return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), - path, uri.getQuery(), uri.getFragment()).toString(); + return new URI(uri.getScheme().toLowerCase(Locale.ENGLISH), + uri.getUserInfo(), + uri.getHost().toLowerCase(Locale.ENGLISH), + uri.getPort(), + path, + uri.getQuery(), + uri.getFragment()).normalize().toString(); } /**