From ca2183a403b986d5ed2a2562b614a3dd4f36ffad Mon Sep 17 00:00:00 2001 From: Zhen Chen Date: Mon, 21 Nov 2016 17:15:15 -0800 Subject: 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 --- .../jgit/transport/http/apache/HttpClientConnection.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'org.eclipse.jgit.http.apache') diff --git a/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java b/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java index 5a53490eed..281154fb1a 100644 --- a/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java +++ b/org.eclipse.jgit.http.apache/src/org/eclipse/jgit/transport/http/apache/HttpClientConnection.java @@ -323,8 +323,17 @@ public class HttpClientConnection implements HttpConnection { } 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) { -- cgit v1.2.3