From 15a34bcc24a14ef0214d34d1b6f60cbb811ba092 Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 15 Jan 2022 22:09:52 +0800 Subject: [PATCH] add method --- CHANGELOG.md | 1 + .../java/cn/hutool/http/HttpGlobalConfig.java | 25 +++++++++++++++++++ .../java/cn/hutool/http/HttpResponse.java | 3 ++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de4449f8f..bb0310d47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * 【core 】 ObjectUtil 添加三个defaultIfXxxx方法,用于节省CPU及内存损耗(pr#2094@Github) * 【db 】 增加单条数据原生upsert语义支持(pr#501@Gitee) * 【core 】 在CollectorUtil提交Collectors.toMap的对null友好实现,避免NPE(pr#502@Gitee) +* 【http 】 增加HttpGlobalConfig.setIgnoreEOFError(issue#2092@Github) * ### 🐞Bug修复 * 【core 】 修复setter重载导致匹配错误(issue#2082@Github) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java index 918085592..99f28b1e1 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java @@ -32,6 +32,7 @@ public class HttpGlobalConfig implements Serializable { private static boolean isAllowPatch = false; private static String boundary = "--------------------Hutool_" + RandomUtil.randomString(16); private static int maxRedirectCount = 0; + private static boolean ignoreEOFError = true; /** * 获取全局默认的超时时长 @@ -99,6 +100,30 @@ public class HttpGlobalConfig implements Serializable { maxRedirectCount = customMaxRedirectCount; } + /** + * 获取是否忽略响应读取时可能的EOF异常。
+ * 在Http协议中,对于Transfer-Encoding: Chunked在正常情况下末尾会写入一个Length为0的的chunk标识完整结束。
+ * 如果服务端未遵循这个规范或响应没有正常结束,会报EOF异常,此选项用于是否忽略这个异常。 + * + * @return 是否忽略响应读取时可能的EOF异常 + * @since 5.7.20 + */ + public static boolean isIgnoreEOFError() { + return ignoreEOFError; + } + + /** + * 设置是否忽略响应读取时可能的EOF异常。
+ * 在Http协议中,对于Transfer-Encoding: Chunked在正常情况下末尾会写入一个Length为0的的chunk标识完整结束。
+ * 如果服务端未遵循这个规范或响应没有正常结束,会报EOF异常,此选项用于是否忽略这个异常。 + * + * @param customIgnoreEOFError 是否忽略响应读取时可能的EOF异常。 + * @since 5.7.20 + */ + synchronized public static void setIgnoreEOFError(boolean customIgnoreEOFError) { + ignoreEOFError = customIgnoreEOFError; + } + /** * 获取Cookie管理器,用于自定义Cookie管理 * diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java index 03ae911da..d43ae1e6c 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java @@ -588,7 +588,8 @@ public class HttpResponse extends HttpBase implements Closeable { copyLength = IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE, contentLength, streamProgress); } catch (IORuntimeException e) { //noinspection StatementWithEmptyBody - if (e.getCause() instanceof EOFException || StrUtil.containsIgnoreCase(e.getMessage(), "Premature EOF")) { + if (HttpGlobalConfig.isIgnoreEOFError() + && (e.getCause() instanceof EOFException || StrUtil.containsIgnoreCase(e.getMessage(), "Premature EOF"))) { // 忽略读取HTTP流中的EOF错误 } else { throw e;