[ISSUE#3790] Supplement http response Content-Encoding processing. (#3791)
* bug: fix issue #3790, Supplement http response Content-Encoding processing * bug: fix issue #3790, Supplement http response Content-Encoding processing. * bug: fix issue #3790, Supplement http response Content-Encoding processing.
This commit is contained in:
parent
3dc0f243f8
commit
baa699a3f1
@ -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 class JdkHttpClientResponse implements HttpClientResponse {
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,6 @@ public class 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() {
|
||||
|
@ -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 class IoUtils {
|
||||
out = new ByteArrayOutputStream();
|
||||
copy(gis, out);
|
||||
return out.toByteArray();
|
||||
} catch (Exception e) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (out != null) {
|
||||
|
Loading…
Reference in New Issue
Block a user