Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
Only accept valid ACCdnPath
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Jun 18, 2019
1 parent 0e80572 commit e8ffd09
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ public static List<ACCdnPath> doGetCdnPath(Call call) throws Exception {
try {
Response response = call.execute();
body = response.body().string();
return JSON.parseArray(body, ACCdnPath.class);
List<ACCdnPath> cdn = JSON.parseArray(body, ACCdnPath.class);
Log.d(TAG, "cdn path: " + cdn);
return cdn;
} catch (Exception e) {
throwException(call, body, e);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
35 changes: 23 additions & 12 deletions app/src/main/java/com/hippo/nimingban/client/data/ACSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@

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;
import com.hippo.nimingban.network.HttpCookieWithId;
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 {
Expand All @@ -41,13 +39,16 @@ public class ACSite extends Site {

private static final String DEFAULT_PICTURE_PREFIX = ACUrl.getHost() + "/Public/Upload/";

private static final List<ACCdnPath> 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<ACCdnPath> mCdnPathList;
private List<ACCdnPath> 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;

Expand Down Expand Up @@ -122,21 +123,28 @@ public String getReportForumId() {
}

public synchronized void setCdnPath(List<ACCdnPath> list) {
mCdnPathList = list;
if (list == null) {
if (list == null || list.isEmpty()) {
return;
}

// Remove invalid cdn path
List<ACCdnPath> 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;
}
Expand Down Expand Up @@ -164,6 +172,9 @@ public String[] getCdnHosts() {
} else {
List<String> 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());
Expand Down

0 comments on commit e8ffd09

Please sign in to comment.