From e8ffd09c89267f013ebdc3029e224caefd3f029b Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 18 Jun 2019 22:14:09 +0800 Subject: [PATCH] Only accept valid ACCdnPath --- .../hippo/nimingban/client/ac/ACEngine.java | 4 ++- .../nimingban/client/ac/data/ACCdnPath.java | 17 +++++++-- .../hippo/nimingban/client/data/ACSite.java | 35 ++++++++++++------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/hippo/nimingban/client/ac/ACEngine.java b/app/src/main/java/com/hippo/nimingban/client/ac/ACEngine.java index e7e22c2e..9a0d4385 100644 --- a/app/src/main/java/com/hippo/nimingban/client/ac/ACEngine.java +++ b/app/src/main/java/com/hippo/nimingban/client/ac/ACEngine.java @@ -158,7 +158,9 @@ public static List doGetCdnPath(Call call) throws Exception { try { Response response = call.execute(); body = response.body().string(); - return JSON.parseArray(body, ACCdnPath.class); + List cdn = JSON.parseArray(body, ACCdnPath.class); + Log.d(TAG, "cdn path: " + cdn); + return cdn; } catch (Exception e) { throwException(call, body, e); throw e; diff --git a/app/src/main/java/com/hippo/nimingban/client/ac/data/ACCdnPath.java b/app/src/main/java/com/hippo/nimingban/client/ac/data/ACCdnPath.java index 474aaf51..b9d1b6f1 100644 --- a/app/src/main/java/com/hippo/nimingban/client/ac/data/ACCdnPath.java +++ b/app/src/main/java/com/hippo/nimingban/client/ac/data/ACCdnPath.java @@ -18,8 +18,21 @@ public class ACCdnPath { - public String url = ""; - public float rate = 0.0f; + public static final String DEFAULT_CDN_PATH = "https://nmbimg.fastmirror.org/"; + public static final String DEFAULT_CDN_HOST = "nmbimg.fastmirror.org"; + public static final float DEFAULT_CDN_RATE = 0.5f; + + public String url; + public float rate; + + public ACCdnPath() { + this(DEFAULT_CDN_PATH, DEFAULT_CDN_RATE); + } + + public ACCdnPath(String url, float rate) { + this.url = url; + this.rate = rate; + } @Override public String toString() { diff --git a/app/src/main/java/com/hippo/nimingban/client/data/ACSite.java b/app/src/main/java/com/hippo/nimingban/client/data/ACSite.java index c4be365f..2f191dea 100644 --- a/app/src/main/java/com/hippo/nimingban/client/data/ACSite.java +++ b/app/src/main/java/com/hippo/nimingban/client/data/ACSite.java @@ -18,7 +18,6 @@ import android.content.Context; import android.support.annotation.NonNull; - import com.hippo.nimingban.NMBApplication; import com.hippo.nimingban.client.ac.ACUrl; import com.hippo.nimingban.client.ac.data.ACCdnPath; @@ -26,13 +25,12 @@ import com.hippo.nimingban.network.SimpleCookieStore; import com.hippo.nimingban.util.Settings; import com.hippo.yorozuya.MathUtils; - import java.net.HttpCookie; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.Collections; import java.util.List; - import okhttp3.HttpUrl; public class ACSite extends Site { @@ -41,13 +39,16 @@ public class ACSite extends Site { private static final String DEFAULT_PICTURE_PREFIX = ACUrl.getHost() + "/Public/Upload/"; + private static final List DEFAULT_AC_CDN_PATH_LIST = Collections.singletonList(new ACCdnPath()); + private static final String[] DEFAULT_AC_CDN_HOST_LIST = { ACCdnPath.DEFAULT_CDN_HOST }; + private URL mSiteUrl; - private List mCdnPathList; + private List mCdnPathList = DEFAULT_AC_CDN_PATH_LIST; private float mRateSum; private boolean mCdnHostsDirty; - private String[] mCdnHosts; + private String[] mCdnHosts = DEFAULT_AC_CDN_HOST_LIST; private static ACSite sInstance; @@ -122,21 +123,28 @@ public String getReportForumId() { } public synchronized void setCdnPath(List list) { - mCdnPathList = list; - if (list == null) { + if (list == null || list.isEmpty()) { return; } + // Remove invalid cdn path + List paths = new ArrayList<>(list.size()); + for (ACCdnPath path : list) { + if (path.url != null && HttpUrl.parse(path.url) != null && path.rate > 0) { + paths.add(path); + } + } + if (paths.isEmpty()) { + return; + } + + mCdnPathList = paths; + mRateSum = 0.0f; for (int i = 0, size = list.size(); i < size; i++) { mRateSum += list.get(i).rate; } - if (mRateSum <= 0.0f) { - // Bad ! - mCdnPathList = null; - } - // Set cdn hosts dirty mCdnHostsDirty = true; } @@ -164,6 +172,9 @@ public String[] getCdnHosts() { } else { List hosts = new ArrayList<>(); for (ACCdnPath cdn : mCdnPathList) { + if (cdn.url == null) { + continue; + } HttpUrl url = HttpUrl.parse(cdn.url); if (url != null) { hosts.add(url.host());