Skip to content

Commit

Permalink
[ISSUE#3790] Supplement http response Content-Encoding processing. (a…
Browse files Browse the repository at this point in the history
…libaba#3791)

* bug: fix issue alibaba#3790, Supplement http response Content-Encoding processing

* bug: fix issue alibaba#3790, Supplement http response Content-Encoding processing.

* bug: fix issue alibaba#3790, Supplement http response Content-Encoding processing.
  • Loading branch information
Maijh97 authored Sep 9, 2020
1 parent 3dc0f24 commit baa699a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ 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 {
gis = new GZIPInputStream(raw);
out = new ByteArrayOutputStream();
copy(gis, out);
return out.toByteArray();
} catch (Exception e) {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
Expand Down

0 comments on commit baa699a

Please sign in to comment.