44import android .webkit .MimeTypeMap ;
55import android .webkit .URLUtil ;
66
7+ import org .wordpress .android .util .AppLog .T ;
8+
79import java .io .UnsupportedEncodingException ;
810import java .net .IDN ;
911import java .net .URI ;
12+ import java .net .URL ;
1013import java .net .URLDecoder ;
1114import java .net .URLEncoder ;
1215import 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