diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java index f4ad0794db3..90fef344589 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java @@ -16,9 +16,11 @@ package com.alibaba.nacos.common.http.client.response; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.utils.IoUtils; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; @@ -36,6 +38,8 @@ public class JdkHttpClientResponse implements HttpClientResponse { private Header responseHeader; + private static final String CONTENT_ENCODING = "gzip"; + public JdkHttpClientResponse(HttpURLConnection conn) { this.conn = conn; } @@ -51,8 +55,18 @@ public Header getHeaders() { @Override public InputStream getBody() throws IOException { + Header headers = getHeaders(); InputStream errorStream = this.conn.getErrorStream(); this.responseStream = (errorStream != null ? errorStream : this.conn.getInputStream()); + String contentEncoding = headers.getValue(HttpHeaderConsts.CONTENT_ENCODING); + // Used to process http content_encoding, when content_encoding is GZIP, use GZIPInputStream + if (CONTENT_ENCODING.equals(contentEncoding)) { + byte[] bytes = IoUtils.tryDecompress(this.responseStream); + if (bytes == null) { + throw new IOException("decompress http response error"); + } + return new ByteArrayInputStream(bytes); + } return this.responseStream; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java index bebba34eac5..c581d7b26cd 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java @@ -45,7 +45,6 @@ private Header() { addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_JSON); addParam(HttpHeaderConsts.ACCEPT_CHARSET, "UTF-8"); addParam(HttpHeaderConsts.ACCEPT_ENCODING, "gzip"); - addParam(HttpHeaderConsts.CONTENT_ENCODING, "gzip"); } public static Header newInstance() { diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java index 0f949ba425f..c38de6dad3b 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java @@ -50,9 +50,9 @@ public class IoUtils { * * @param raw compress stream * @return byte array after decompress - * @throws Exception exception + * @throws IOException exception */ - public static byte[] tryDecompress(InputStream raw) throws Exception { + public static byte[] tryDecompress(InputStream raw) throws IOException { GZIPInputStream gis = null; ByteArrayOutputStream out = null; try { @@ -60,7 +60,7 @@ public static byte[] tryDecompress(InputStream raw) throws Exception { out = new ByteArrayOutputStream(); copy(gis, out); return out.toByteArray(); - } catch (Exception e) { + } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) {