Skip to content

Commit

Permalink
Fix empty responses when content length is unknown.
Browse files Browse the repository at this point in the history
For whatever reason, it seems that HttpUrlConnection#getContentLength may
return -1 even if the response includes a Content-Length header. This
still worked fine in Volley, which only used this header to try to
pre-size buffers for reading the response. However, this regressed when
we introduced the change to HttpResponse to support byte[] content in
addition to InputStream content, as we used the content-length to determine
whether content was expected.

Resolve this regression by just checking that content was provided (in
either form) before returning it, or else returning null, without
requiring a valid Content-Length header to be set.
  • Loading branch information
jpd236 committed Oct 6, 2020
1 parent 055fc53 commit ae701b3
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/android/volley/toolbox/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,12 @@ public final byte[] getContentBytes() {
*/
@Nullable
public final InputStream getContent() {
if (mContentLength == -1) {
return null;
}
if (mContent != null) {
return mContent;
} else {
} else if (mContentBytes != null) {
return new ByteArrayInputStream(mContentBytes);
} else {
return null;
}
}
}

0 comments on commit ae701b3

Please sign in to comment.