Skip to content

Commit 3d0a125

Browse files
committed
不再对HTTP类请求做Content-Length校验
1 parent b6cd2fb commit 3d0a125

3 files changed

Lines changed: 17 additions & 33 deletions

File tree

src/main/java/com/github/balloonupdate/mcpatch/client/config/AppConfig.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ public class AppConfig {
105105
*/
106106
public boolean ignoreSSLCertificate;
107107

108-
109-
/**
110-
* 是否忽略对http协议中content-length的校验
111-
*/
112-
public boolean ignoreHttpContentLength;
113-
114108
/**
115109
* 测试模式,开启后每次都会重头更新,会增加流量消耗,仅用来测试更新时网速
116110
*/
@@ -133,7 +127,6 @@ public AppConfig(Map<String, Object> map) {
133127
int httpTimeout = getInt(map, "http-timeout", null, 7000);
134128
int reties = getInt(map, "retries", "http-retries", 3);
135129
boolean ignoreSSLCertificate = getBoolean(map, "ignore-ssl-cert", "http-ignore-certificate", false);
136-
boolean ignoreHttpContentLength = getBoolean(map, "ignore-http-content-length", "", false);
137130
boolean testMode = getBoolean(map, "test-mode", null, false);
138131

139132
// if (urls.contains("webda"))
@@ -154,7 +147,6 @@ public AppConfig(Map<String, Object> map) {
154147
this.httpTimeout = httpTimeout;
155148
this.reties = reties;
156149
this.ignoreSSLCertificate = ignoreSSLCertificate;
157-
this.ignoreHttpContentLength = ignoreHttpContentLength;
158150
this.testMode = testMode;
159151
}
160152

src/main/java/com/github/balloonupdate/mcpatch/client/network/impl/HttpProtocol.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import com.github.balloonupdate.mcpatch.client.config.AppConfig;
44
import com.github.balloonupdate.mcpatch.client.data.Range;
55
import com.github.balloonupdate.mcpatch.client.exceptions.McpatchBusinessException;
6+
import com.github.balloonupdate.mcpatch.client.logging.Log;
67
import com.github.balloonupdate.mcpatch.client.network.UpdatingServer;
78
import com.github.balloonupdate.mcpatch.client.utils.BytesUtils;
89
import com.github.balloonupdate.mcpatch.client.utils.ReduceReportingFrequency;
910
import com.github.balloonupdate.mcpatch.client.utils.RuntimeAssert;
11+
import kotlin.Pair;
1012
import okhttp3.OkHttpClient;
1113
import okhttp3.Request;
1214
import okhttp3.Response;
@@ -160,11 +162,22 @@ Response request(String path, Range range, String desc) throws McpatchBusinessEx
160162
// 拼接 URL
161163
String url = baseUrl + path;
162164

165+
Log.debug("Http Request URL: " + url);
166+
163167
// 构建请求
164168
Request req = buildRequest(url, range, null);
165169

166170
try {
171+
167172
Response rsp = client.newCall(req).execute();
173+
174+
// for (Pair<? extends String, ? extends String> header : req.headers()) {
175+
// Log.info("Req Header: " + header.component1() + ": " + header.component2());
176+
// }
177+
// for (Pair<? extends String, ? extends String> header : rsp.headers()) {
178+
// Log.info("Rsp Header: " + header.component1() + ": " + header.component2());
179+
// }
180+
168181
int code = rsp.code();
169182

170183
// 检查状态码
@@ -177,22 +190,6 @@ Response request(String path, Range range, String desc) throws McpatchBusinessEx
177190
throw new McpatchBusinessException(content);
178191
}
179192

180-
if (!config.ignoreHttpContentLength)
181-
{
182-
// 检查content-length
183-
long len = rsp.body().contentLength();
184-
185-
if (len == -1) {
186-
throw new McpatchBusinessException(String.format("服务器(%d)没有返回 content-length 头:%s (%s)", number, path, desc));
187-
}
188-
189-
if (range.len() > 0 && len != range.len()) {
190-
String text = String.format("服务器(%d)返回的 content-length 头 %d 不等于 %d: %s", number, len, range.len(), path);
191-
192-
throw new McpatchBusinessException(text);
193-
}
194-
}
195-
196193
return rsp;
197194
} catch (ConnectException e) {
198195
throw new McpatchBusinessException("连接被拒绝,请检查网络。" + url, e);
@@ -246,14 +243,12 @@ public IgnoreSSLCert() {
246243
context = SSLContext.getInstance("TLS");
247244

248245
trustManager = new X509TrustManager() {
249-
public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) { }
250-
251-
public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) { }
252-
253-
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
246+
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
247+
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
248+
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
254249
};
255250

256-
context.init(null, new TrustManager[]{ trustManager }, null);
251+
context.init(null, new TrustManager[]{ trustManager }, new java.security.SecureRandom());
257252
} catch (NoSuchAlgorithmException | KeyManagementException e) {
258253
throw new RuntimeException(e);
259254
}

src/main/resources/mcpatch.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ retries: 3
8282
# 设置 http 系协议是否忽略SSL证书验证
8383
ignore-ssl-cert: false
8484

85-
# 是否忽略对http协议中content-length的校验
86-
ignore-http-content-length: false
87-
8885
# 测试模式,专门用来调试更新时的网速
8986
# 开启后每次都会重头更新,且不会优化任何文件下载
9087
# 仅用于测试用途,平时正常使用勿开

0 commit comments

Comments
 (0)