Skip to content

Commit 2d0055d

Browse files
committed
Checking Content-Length > 0 before reading gzipped data
1 parent 0ecfa23 commit 2d0055d

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.mashape.unirest</groupId>
55
<artifactId>unirest-java</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.4.6-CE-SNAPSHOT</version>
7+
<version>1.4.7-CE-SNAPSHOT</version>
88
<name>unirest-java</name>
99
<description>Simplified, lightweight HTTP client library</description>
1010
<url>http://unirest.io/</url>

src/main/java/com/mashape/unirest/http/HttpResponse.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class HttpResponse<T> {
5050
@SuppressWarnings("unchecked")
5151
public HttpResponse(org.apache.http.HttpResponse response, Class<T> responseClass) {
5252
HttpEntity responseEntity = response.getEntity();
53-
53+
5454
Header[] allHeaders = response.getAllHeaders();
5555
for(Header header : allHeaders) {
5656
String headerName = header.getName().toLowerCase();
@@ -62,23 +62,24 @@ public HttpResponse(org.apache.http.HttpResponse response, Class<T> responseClas
6262
StatusLine statusLine = response.getStatusLine();
6363
this.statusCode = statusLine.getStatusCode();
6464
this.statusText = statusLine.getReasonPhrase();
65-
65+
6666
if (responseEntity != null) {
6767
String charset = "UTF-8";
68-
68+
6969
Header contentType = responseEntity.getContentType();
7070
if (contentType != null) {
7171
String responseCharset = ResponseUtils.getCharsetFromContentType(contentType.getValue());
7272
if (responseCharset != null && !responseCharset.trim().equals("")) {
7373
charset = responseCharset;
7474
}
7575
}
76-
76+
7777
try {
7878
byte[] rawBody;
79+
Long contentLength = responseEntity.getContentLength();
7980
try {
8081
InputStream responseInputStream = responseEntity.getContent();
81-
if (ResponseUtils.isGzipped(responseEntity.getContentEncoding())) {
82+
if (ResponseUtils.isGzipped(responseEntity.getContentEncoding()) && contentLength > 0) {
8283
responseInputStream = new GZIPInputStream(responseEntity.getContent());
8384
}
8485
rawBody = ResponseUtils.getBytes(responseInputStream);
@@ -102,7 +103,7 @@ public HttpResponse(org.apache.http.HttpResponse response, Class<T> responseClas
102103
throw new RuntimeException(e);
103104
}
104105
}
105-
106+
106107
try {
107108
EntityUtils.consume(responseEntity);
108109
} catch (IOException e) {
@@ -113,7 +114,7 @@ public HttpResponse(org.apache.http.HttpResponse response, Class<T> responseClas
113114
public int getStatus() {
114115
return statusCode;
115116
}
116-
117+
117118
public String getStatusText() {
118119
return statusText;
119120
}

0 commit comments

Comments
 (0)