This commit is contained in:
Looly 2024-10-10 22:24:51 +08:00
parent ae1f3635ce
commit be3ee886c0
12 changed files with 190 additions and 104 deletions

View File

@ -58,7 +58,8 @@ public class SyncInputStream extends FilterInputStream {
}
/**
* 同步数据到内存
* 同步数据到内存同步后关闭原流
*
* @return this
*/
public SyncInputStream sync() {
@ -82,7 +83,8 @@ public class SyncInputStream extends FilterInputStream {
}
/**
* 将流的内容拷贝到输出流
* 将流的内容拷贝到输出流拷贝结束后关闭输入流
*
* @param out 输出流
* @param streamProgress 进度条
* @return 拷贝长度

View File

@ -82,13 +82,21 @@ public interface Response extends Closeable {
InputStream bodyStream();
/**
* 获取响应体包含服务端返回的内容和Content-Type信息
* 同步<br>
* 如果为异步状态则暂时不读取服务器中响应的内容而是持有Http链接的{@link InputStream}<br>
* 当调用此方法时异步状态转为同步状态此时从Http链接流中读取body内容并暂存在内容(内存)如果已经是同步状态则不进行任何操作
*
* @return this
*/
Response sync();
/**
* 获取响应体包含服务端返回的内容和Content-Type信息<br>
* 如果为HEADCONNECTTRACE等方法无响应体则返回{@code null}
*
* @return {@link ResponseBody}
*/
default ResponseBody body() {
return new ResponseBody(this, bodyStream(), false, true);
}
ResponseBody body();
/**
* 获取响应主体
@ -112,7 +120,7 @@ public interface Response extends Closeable {
*/
default byte[] bodyBytes() {
try (final ResponseBody body = body()) {
return body.getBytes();
return null == body ? null : body.getBytes();
} catch (final IOException e) {
throw new IORuntimeException(e);
}

View File

@ -44,6 +44,16 @@ public class ResponseBody implements HttpBody, Closeable {
*/
private final SyncInputStream bodyStream;
/**
* 构造不读取响应体忽略响应体EOF错误
*
* @param response 响应体
* @param in HTTP主体响应流
*/
public ResponseBody(final Response response, final InputStream in) {
this(response, in, true, true);
}
/**
* 构造
*

View File

@ -21,6 +21,7 @@ import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
@ -75,11 +76,14 @@ public class HttpClient4Engine extends AbstractClientEngine {
initEngine();
final HttpUriRequest request = buildRequest(message);
final CloseableHttpResponse response;
try {
return this.engine.execute(request, response -> new HttpClient4Response(response, message.charset()));
//return this.engine.execute(request, response -> new HttpClient4Response(response, message));
response = this.engine.execute(request);
} catch (final IOException e) {
throw new HttpException(e);
}
return new HttpClient4Response(response, message);
}
@Override

View File

@ -24,13 +24,15 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.lang.wrapper.SimpleWrapper;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.client.Request;
import org.dromara.hutool.http.client.Response;
import org.dromara.hutool.http.client.body.ResponseBody;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
@ -52,18 +54,20 @@ public class HttpClient4Response extends SimpleWrapper<HttpResponse> implements
* 请求时的默认编码
*/
private final Charset requestCharset;
private final ResponseBody body;
/**
* 构造<br>
* 通过传入一个请求时的编码当无法获取响应内容的编码时默认使用响应时的编码
*
* @param rawRes {@link HttpResponse}
* @param requestCharset 请求时的编码
* @param message 请求消息
*/
public HttpClient4Response(final HttpResponse rawRes, final Charset requestCharset) {
public HttpClient4Response(final HttpResponse rawRes, final Request message) {
super(rawRes);
this.entity = rawRes.getEntity();
this.requestCharset = requestCharset;
this.requestCharset = message.charset();
this.body = message.method().isIgnoreBody() ? null : new ResponseBody(this, bodyStream());
}
@ -112,6 +116,21 @@ public class HttpClient4Response extends SimpleWrapper<HttpResponse> implements
}
}
@Override
public HttpClient4Response sync() {
final ResponseBody body = this.body;
if(null != body){
body.sync();
}
IoUtil.closeIfPossible(this.raw);
return this;
}
@Override
public ResponseBody body() {
return this.body;
}
@Override
public String bodyStr() throws HttpException {
try {
@ -124,10 +143,8 @@ public class HttpClient4Response extends SimpleWrapper<HttpResponse> implements
}
@Override
public void close() throws IOException {
if(this.raw instanceof Closeable){
((Closeable) this.raw).close();
}
public void close() {
IoUtil.closeIfPossible(this.raw);
}
@Override

View File

@ -29,6 +29,7 @@ import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.message.BasicHeader;
@ -81,11 +82,14 @@ public class HttpClient5Engine extends AbstractClientEngine {
initEngine();
final ClassicHttpRequest request = buildRequest(message);
final ClassicHttpResponse response;
try {
return this.engine.execute(request, (response -> new HttpClient5Response(response, message.charset())));
//return this.engine.execute(request, (response -> new HttpClient5Response(response, message)));
response = this.engine.executeOpen(null, request, null);
} catch (final IOException e) {
throw new HttpException(e);
}
return new HttpClient5Response(response, message);
}
@Override

View File

@ -16,28 +16,27 @@
package org.dromara.hutool.http.client.engine.httpclient5;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.dromara.hutool.core.io.IORuntimeException;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.lang.wrapper.SimpleWrapper;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.client.Request;
import org.dromara.hutool.http.client.Response;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.dromara.hutool.http.client.body.ResponseBody;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* HttpClient响应包装<br>
@ -52,18 +51,20 @@ public class HttpClient5Response extends SimpleWrapper<ClassicHttpResponse> impl
* 请求时的默认编码
*/
private final Charset requestCharset;
private final ResponseBody body;
/**
* 构造<br>
* 通过传入一个请求时的编码当无法获取响应内容的编码时默认使用响应时的编码
*
* @param rawRes {@link CloseableHttpResponse}
* @param requestCharset 请求时的编码
* @param message 请求消息
*/
public HttpClient5Response(final ClassicHttpResponse rawRes, final Charset requestCharset) {
public HttpClient5Response(final ClassicHttpResponse rawRes, final Request message) {
super(rawRes);
this.entity = rawRes.getEntity();
this.requestCharset = requestCharset;
this.requestCharset = message.charset();
this.body = message.method().isIgnoreBody() ? null : new ResponseBody(this, bodyStream());
}
@ -112,6 +113,21 @@ public class HttpClient5Response extends SimpleWrapper<ClassicHttpResponse> impl
}
}
@Override
public HttpClient5Response sync() {
final ResponseBody body = this.body;
if(null != body){
body.sync();
}
IoUtil.closeQuietly(this.raw);
return this;
}
@Override
public ResponseBody body() {
return this.body;
}
@Override
public String bodyStr() throws HttpException {
try {

View File

@ -26,7 +26,6 @@ import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.client.ClientConfig;
import org.dromara.hutool.http.client.Request;
import org.dromara.hutool.http.client.Response;
import org.dromara.hutool.http.client.body.HttpBody;
import org.dromara.hutool.http.client.engine.AbstractClientEngine;
import org.dromara.hutool.http.meta.HeaderName;
@ -65,18 +64,7 @@ public class JdkClientEngine extends AbstractClientEngine {
}
@Override
public Response send(final Request message) {
return send(message, true);
}
/**
* 发送请求
*
* @param message 请求消息
* @param isAsync 是否异步异步不会立即读取响应内容
* @return {@link Response}
*/
public JdkHttpResponse send(final Request message, final boolean isAsync) {
public JdkHttpResponse send(final Request message) {
final JdkHttpConnection conn = buildConn(message);
try {
doSend(conn, message);
@ -86,7 +74,7 @@ public class JdkClientEngine extends AbstractClientEngine {
throw new IORuntimeException(e);
}
return sendRedirectIfPossible(conn, message, isAsync);
return sendRedirectIfPossible(conn, message);
}
@Override
@ -174,10 +162,9 @@ public class JdkClientEngine extends AbstractClientEngine {
* 调用转发如果需要转发返回转发结果否则返回{@code null}
*
* @param conn {@link JdkHttpConnection}}
* @param isAsync 最终请求是否异步
* @return {@link JdkHttpResponse}无转发返回 {@code null}
*/
private JdkHttpResponse sendRedirectIfPossible(final JdkHttpConnection conn, final Request message, final boolean isAsync) {
private JdkHttpResponse sendRedirectIfPossible(final JdkHttpConnection conn, final Request message) {
// 手动实现重定向
if (message.maxRedirects() > 0) {
final int code;
@ -203,14 +190,14 @@ public class JdkClientEngine extends AbstractClientEngine {
if (conn.redirectCount < message.maxRedirects()) {
conn.redirectCount++;
return send(message, isAsync);
return send(message);
}
}
}
}
// 最终页面
return new JdkHttpResponse(conn, this.cookieManager, true, message.charset(), isAsync, message.method().isIgnoreBody());
return new JdkHttpResponse(conn, this.cookieManager, message);
}
/**

View File

@ -21,6 +21,7 @@ import org.dromara.hutool.core.io.stream.EmptyInputStream;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.client.Request;
import org.dromara.hutool.http.client.Response;
import org.dromara.hutool.http.client.body.ResponseBody;
import org.dromara.hutool.http.meta.HeaderName;
@ -42,6 +43,10 @@ import java.util.Map;
*/
public class JdkHttpResponse implements Response, Closeable {
/**
* 持有连接对象
*/
protected JdkHttpConnection httpConnection;
/**
* 请求时的默认编码
*/
@ -50,26 +55,14 @@ public class JdkHttpResponse implements Response, Closeable {
* Cookie管理器
*/
private final JdkCookieManager cookieManager;
/**
* 响应内容体{@code null} 表示无内容
*/
private ResponseBody body;
/**
* 响应头
*/
private Map<String, List<String>> headers;
/**
* 是否忽略响应读取时可能的EOF异常<br>
* 在Http协议中对于Transfer-Encoding: Chunked在正常情况下末尾会写入一个Length为0的的chunk标识完整结束<br>
* 如果服务端未遵循这个规范或响应没有正常结束会报EOF异常此选项用于是否忽略这个异常
* 响应内容体{@code null} 表示无内容
*/
private final boolean ignoreEOFError;
/**
* 持有连接对象
*/
protected JdkHttpConnection httpConnection;
private ResponseBody body;
/**
* 响应状态码
*/
@ -80,22 +73,15 @@ public class JdkHttpResponse implements Response, Closeable {
*
* @param httpConnection {@link JdkHttpConnection}
* @param cookieManager Cookie管理器
* @param ignoreEOFError 是否忽略响应读取时可能的EOF异常
* @param requestCharset 编码从请求编码中获取默认编码
* @param isAsync 是否异步
* @param isIgnoreBody 是否忽略读取响应体
* @param message 请求消息
*/
protected JdkHttpResponse(final JdkHttpConnection httpConnection,
final JdkCookieManager cookieManager,
final boolean ignoreEOFError,
final Charset requestCharset,
final boolean isAsync,
final boolean isIgnoreBody) {
final Request message) {
this.httpConnection = httpConnection;
this.cookieManager = cookieManager;
this.ignoreEOFError = ignoreEOFError;
this.requestCharset = requestCharset;
init(isAsync, isIgnoreBody);
this.requestCharset = message.charset();
init(message.method().isIgnoreBody());
}
/**
@ -128,18 +114,13 @@ public class JdkHttpResponse implements Response, Closeable {
return ObjUtil.defaultIfNull(Response.super.charset(), requestCharset);
}
/**
* 同步<br>
* 如果为异步状态则暂时不读取服务器中响应的内容而是持有Http链接的{@link InputStream}<br>
* 当调用此方法时异步状态转为同步状态此时从Http链接流中读取body内容并暂存在内容中如果已经是同步状态则不进行任何操作
*
* @return this
*/
@Override
public JdkHttpResponse sync() {
if (null != this.body) {
this.body.sync();
}
close();
// 关闭连接
this.httpConnection.closeQuietly();
return this;
}
@ -250,9 +231,10 @@ public class JdkHttpResponse implements Response, Closeable {
* 3持有Http流并不关闭流
* </pre>
*
* @param isIgnoreBody 是否忽略消息体
* @throws HttpException IO异常
*/
private void init(final boolean isAsync, final boolean isIgnoreBody) throws HttpException {
private void init(final boolean isIgnoreBody) throws HttpException {
// 获取响应状态码
try {
this.status = httpConnection.getCode();
@ -278,7 +260,7 @@ public class JdkHttpResponse implements Response, Closeable {
// 获取响应内容流
if (!isIgnoreBody) {
this.body = new ResponseBody(this, new JdkHttpInputStream(this), isAsync, this.ignoreEOFError);
this.body = new ResponseBody(this, new JdkHttpInputStream(this));
}
}
// ---------------------------------------------------------------- Private method end

View File

@ -71,7 +71,7 @@ public class OkHttpEngine extends AbstractClientEngine {
throw new IORuntimeException(e);
}
return new OkHttpResponse(response, message.charset());
return new OkHttpResponse(response, message);
}
@Override

View File

@ -16,19 +16,20 @@
package org.dromara.hutool.http.client.engine.okhttp;
import kotlin.Pair;
import okhttp3.Headers;
import okhttp3.ResponseBody;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.stream.EmptyInputStream;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.GlobalCompressStreamRegister;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.client.Request;
import org.dromara.hutool.http.client.Response;
import org.dromara.hutool.http.client.body.ResponseBody;
import org.dromara.hutool.http.meta.HeaderName;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.*;
import java.util.List;
import java.util.Map;
/**
* OkHttp3的{@link okhttp3.Response} 响应包装
@ -42,14 +43,16 @@ public class OkHttpResponse implements Response {
* 请求时的默认编码
*/
private final Charset requestCharset;
private final ResponseBody body;
/**
* @param rawRes {@link okhttp3.Response}
* @param requestCharset 请求时的默认编码
* @param message 请求对象
*/
public OkHttpResponse(final okhttp3.Response rawRes, final Charset requestCharset) {
public OkHttpResponse(final okhttp3.Response rawRes, final Request message) {
this.rawRes = rawRes;
this.requestCharset = requestCharset;
this.requestCharset = message.charset();
this.body = message.method().isIgnoreBody() ? null : new ResponseBody(this, bodyStream());
}
@Override
@ -74,7 +77,7 @@ public class OkHttpResponse implements Response {
@Override
public InputStream bodyStream() {
final ResponseBody body = rawRes.body();
final okhttp3.ResponseBody body = rawRes.body();
if(null == body){
return EmptyInputStream.INSTANCE;
}
@ -84,10 +87,22 @@ public class OkHttpResponse implements Response {
}
@Override
public void close() {
if(null != this.rawRes){
rawRes.close();
public OkHttpResponse sync() {
if (null != this.body) {
this.body.sync();
}
IoUtil.closeQuietly(this.rawRes);
return this;
}
@Override
public ResponseBody body() {
return this.body;
}
@Override
public void close() {
IoUtil.closeQuietly(this.rawRes);
}
@Override

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2024 Hutool Team and hutool.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hutool.http.client;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.client.engine.ClientEngine;
import org.dromara.hutool.http.client.engine.ClientEngineFactory;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
public class Issue3765Test {
public static void main(String[] args) {
HttpUtil.createServer(8888)
.setRoot("d:/test/www")
.start();
}
@Test
@Disabled
void downloadTest() {
final String url = "http://localhost:8888/a.mp3";
final ClientEngine engine = ClientEngineFactory.createEngine("httpclient4");
Request.of(url)
.send(engine)
.body().write("d:/test/");
}
}