Fix content length in HttpClientConnection

Per the interface specification, the getContentLength method should
return -1 if content length is unknown or greater than
Integer.MAX_VALUE.

For chunked transfer encoding, the content length is not included in the
header, hence will cause a NullPointerException when trying to parse the
content length header.

Change-Id: Iaa36b5c146a8d654e364635fa0bd2d14129baf17
Signed-off-by: Zhen Chen <czhen@google.com>
This commit is contained in:
Zhen Chen 2016-11-21 17:15:15 -08:00
parent 81f9c18433
commit ca2183a403
1 changed files with 11 additions and 2 deletions

View File

@ -323,8 +323,17 @@ public String getHeaderField(String name) {
}
public int getContentLength() {
return Integer.parseInt(resp.getFirstHeader("content-length") //$NON-NLS-1$
.getValue());
Header contentLength = resp.getFirstHeader("content-length"); //$NON-NLS-1$
if (contentLength == null) {
return -1;
}
try {
int l = Integer.parseInt(contentLength.getValue());
return l < 0 ? -1 : l;
} catch (NumberFormatException e) {
return -1;
}
}
public void setInstanceFollowRedirects(boolean followRedirects) {