Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.152 diff -u -r1.152 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 13 Jun 2003 21:32:17 -0000 1.152 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 20 Jun 2003 12:45:01 -0000 @@ -880,6 +880,14 @@ */ protected boolean shouldCloseConnection(HttpConnection conn) { + // if we are not chunked and there is no content length the connection + // cannot be reused + if (responseHeaders.getFirstHeader("Transfer-Encoding") == null + && getResponseContentLength() < 0) { + LOG.debug("Should close connection as content-length is missing."); + return true; + } + Header connectionHeader = null; // In case being connected via a proxy server if (!conn.isTransparent()) { Index: test/org/apache/commons/httpclient/TestResponseHeaders.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v retrieving revision 1.7 diff -u -r1.7 TestResponseHeaders.java --- test/org/apache/commons/httpclient/TestResponseHeaders.java 10 Jun 2003 22:42:52 -0000 1.7 +++ test/org/apache/commons/httpclient/TestResponseHeaders.java 20 Jun 2003 12:45:06 -0000 @@ -153,6 +153,7 @@ "HTTP/1.1 200 OK\r\n" + "proxy-connection: close\r\n" + "proxy-connection: close\r\n" + + "Content-Length: 0\r\n" + "\r\n"; conn.addResponse(headers, ""); @@ -169,6 +170,7 @@ "HTTP/1.0 200 OK\r\n" + "proxy-connection: keep-alive\r\n" + "proxy-connection: keep-alive\r\n" + + "Content-Length: 0\r\n" + "\r\n"; conn.addResponse(headers, ""); @@ -202,6 +204,7 @@ "HTTP/1.0 200 OK\r\n" +"Connection: keep-alive\r\n" +"Connection: keep-alive\r\n" + + "Content-Length: 0\r\n" +"\r\n"; conn.addResponse(headers, ""); @@ -210,6 +213,65 @@ method.getResponseBodyAsString(); assertTrue(conn.isOpen()); + } + + public void testNoContentLength() throws Exception { + // test with connection header + SimpleHttpConnection conn = new SimpleHttpConnection(); + String headers = + "HTTP/1.1 200 OK\r\n" + + "Connection: keep-alive\r\n" + + "\r\n"; + + conn.addResponse(headers, "12345"); + GetMethod method = new GetMethod("/"); + method.execute(new HttpState(), conn); + method.getResponseBodyAsString(); + + assertFalse(conn.isOpen()); + + // test without connection header + conn = new SimpleHttpConnection(); + headers = "HTTP/1.1 200 OK\r\n\r\n"; + + // test with connection header + conn.addResponse(headers, "12345"); + method = new GetMethod("/"); + method.execute(new HttpState(), conn); + method.getResponseBodyAsString(); + + assertFalse(conn.isOpen()); + } + + public void testProxyNoContentLength() throws Exception { + // test with proxy-connection header + SimpleHttpConnection conn = new SimpleHttpConnection(); + String headers = + "HTTP/1.1 200 OK\r\n" + + "proxy-connection: keep-alive\r\n" + + "\r\n"; + + conn.addResponse(headers, "12345"); + conn.setProxyHost("proxy"); + conn.setProxyPort(1); + GetMethod method = new GetMethod("/"); + method.execute(new HttpState(), conn); + method.getResponseBodyAsString(); + + assertFalse(conn.isOpen()); + + // test without proxy-connection header + conn = new SimpleHttpConnection(); + headers = "HTTP/1.1 200 OK\r\n\r\n"; + + conn.addResponse(headers, "12345"); + conn.setProxyHost("proxy"); + conn.setProxyPort(1); + method = new GetMethod("/"); + method.execute(new HttpState(), conn); + method.getResponseBodyAsString(); + + assertFalse(conn.isOpen()); } public void testNullHeaders() throws Exception {