From 9ccfe3f4fc999e468a068a41f44bd360b5cde150 Mon Sep 17 00:00:00 2001 From: yangsen Date: Thu, 23 Nov 2023 14:56:34 +0800 Subject: [PATCH] Uc query add host (#507) --- .../java/com/qiniu/android/common/Config.java | 9 +++--- .../http/request/HttpRegionRequest.java | 4 +-- .../com/qiniu/android/utils/UrlUtils.java | 31 +++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 library/src/main/java/com/qiniu/android/utils/UrlUtils.java diff --git a/library/src/main/java/com/qiniu/android/common/Config.java b/library/src/main/java/com/qiniu/android/common/Config.java index d5c1e53c..dc02e3bc 100644 --- a/library/src/main/java/com/qiniu/android/common/Config.java +++ b/library/src/main/java/com/qiniu/android/common/Config.java @@ -68,12 +68,13 @@ public final class Config { /** * preQuery host */ - public static String preQueryHost00 = "kodo-config.qiniuapi.com"; - public static String preQueryHost01 = "uc.qbox.me"; - public static String preQueryHost02 = "api.qiniu.com"; + public static String preQueryHost00 = "uc.qiniuapi.com"; + public static String preQueryHost01 = "kodo-config.qiniuapi.com"; + public static String preQueryHost02 = "uc.qbox.me"; + public static String preQueryHost03 = "api.qiniu.com"; public static String[] preQueryHosts() { - return new String[]{preQueryHost00, preQueryHost01, preQueryHost02}; + return new String[]{preQueryHost00, preQueryHost01, preQueryHost02, preQueryHost03}; } /** diff --git a/library/src/main/java/com/qiniu/android/http/request/HttpRegionRequest.java b/library/src/main/java/com/qiniu/android/http/request/HttpRegionRequest.java index 46933a53..9071ebfa 100644 --- a/library/src/main/java/com/qiniu/android/http/request/HttpRegionRequest.java +++ b/library/src/main/java/com/qiniu/android/http/request/HttpRegionRequest.java @@ -11,6 +11,7 @@ import com.qiniu.android.storage.UploadOptions; import com.qiniu.android.utils.LogUtil; import com.qiniu.android.utils.StringUtils; +import com.qiniu.android.utils.UrlUtils; import org.json.JSONObject; @@ -106,8 +107,7 @@ private void performRequest(final IUploadServer server, serverHost = config.urlConverter.convert(serverHost); } - String scheme = config.useHttps ? "https://" : "http://"; - String urlString = scheme + serverHost + (action != null ? action : ""); + String urlString = UrlUtils.setHostScheme(serverHost, config.useHttps) + (action != null ? action : ""); final Request request = new Request(urlString, method, header, data, config.connectTimeout, config.writeTimeout, diff --git a/library/src/main/java/com/qiniu/android/utils/UrlUtils.java b/library/src/main/java/com/qiniu/android/utils/UrlUtils.java new file mode 100644 index 00000000..ecd12539 --- /dev/null +++ b/library/src/main/java/com/qiniu/android/utils/UrlUtils.java @@ -0,0 +1,31 @@ +package com.qiniu.android.utils; + +public class UrlUtils { + + public static String removeHostScheme(String host) { + if (host == null || StringUtils.isNullOrEmpty(host)) { + return null; + } + + host = host.replace("http://", ""); + host = host.replace("https://", ""); + return host; + } + + + /** + * 如果 host 包含 scheme 则优先使用 host 中包含的 scheme + * 如果 host 不包含 scheme 则按照 useHttps 增加 scheme + */ + public static String setHostScheme(String host, boolean useHttps) { + if (StringUtils.isNullOrEmpty(host)) { + return null; + } + + if (host.startsWith("http://") || host.startsWith("https://") ) { + return host; + } + + return (useHttps ? "https://" : "http://") + host; + } +}