From 1636d70f253e3acf364dd90d8db0788e8a445844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A8=E3=83=AA=E3=82=B9?= Date: Fri, 30 Jul 2021 17:08:23 +0900 Subject: [PATCH] feat: support http w/ content src fix (#1298) --- .../org/apache/cordova/ConfigXmlParser.java | 50 +++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/framework/src/org/apache/cordova/ConfigXmlParser.java b/framework/src/org/apache/cordova/ConfigXmlParser.java index 9c14e558..e9365173 100644 --- a/framework/src/org/apache/cordova/ConfigXmlParser.java +++ b/framework/src/org/apache/cordova/ConfigXmlParser.java @@ -24,16 +24,19 @@ import java.util.ArrayList; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; - import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; - import android.content.Context; public class ConfigXmlParser { private static String TAG = "ConfigXmlParser"; - private String launchUrl = null; + private static String SCHEME_HTTP = "http"; + private static String SCHEME_HTTPS = "https"; + private static String DEFAULT_HOSTNAME = "localhost"; + + private String launchUrl; + private String contentSrc; private CordovaPreferences prefs = new CordovaPreferences(); private ArrayList pluginEntries = new ArrayList(20); @@ -47,11 +50,7 @@ public class ConfigXmlParser { public String getLaunchUrl() { if (launchUrl == null) { - launchUrl = "https://" + this.prefs.getString("hostname", "localhost"); - } - - if (this.prefs.getBoolean("AndroidInsecureFileModeEnabled", false)) { - launchUrl = "file:///android_asset/www/index.html"; + setStartUrl(contentSrc); } return launchUrl; @@ -130,7 +129,10 @@ public class ConfigXmlParser { else if (strNode.equals("content")) { String src = xml.getAttributeValue(null, "src"); if (src != null) { - setStartUrl(src); + contentSrc = src; + } else { + // Default + contentSrc = "index.html"; } } } @@ -147,20 +149,40 @@ public class ConfigXmlParser { } } + private String getLaunchUrlPrefix() { + if (prefs.getBoolean("AndroidInsecureFileModeEnabled", false)) { + return "file:///android_asset/www/"; + } else { + String scheme = prefs.getString("scheme", SCHEME_HTTPS).toLowerCase(); + String hostname = prefs.getString("hostname", DEFAULT_HOSTNAME); + + if (!scheme.contentEquals(SCHEME_HTTP) && !scheme.contentEquals(SCHEME_HTTPS)) { + LOG.d(TAG, "The provided scheme \"" + scheme + "\" is not valid. " + + "Defaulting to \"" + SCHEME_HTTPS + "\". " + + "(Valid Options=" + SCHEME_HTTP + "," + SCHEME_HTTPS + ")"); + + scheme = SCHEME_HTTPS; + } + + return scheme + "://" + hostname + '/'; + } + } + private void setStartUrl(String src) { Pattern schemeRegex = Pattern.compile("^[a-z-]+://"); Matcher matcher = schemeRegex.matcher(src); + if (matcher.find()) { launchUrl = src; } else { + String launchUrlPrefix = getLaunchUrlPrefix(); + + // remove leading slash, "/", from content src if existing, if (src.charAt(0) == '/') { src = src.substring(1); } - if (this.prefs.getBoolean("AndroidInsecureFileModeEnabled", false)) { - launchUrl = "file:///android_asset/www/" + src; - } else { - launchUrl = "https://" + this.prefs.getString("hostname", "localhost") + "/" + src; - } + + launchUrl = launchUrlPrefix + src; } } }