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.159.2.16 diff -u -r1.159.2.16 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 21 Oct 2003 20:15:03 -0000 1.159.2.16 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 3 Nov 2003 17:46:12 -0000 @@ -1080,6 +1080,7 @@ // Discard status line this.statusLine = null; + this.connectionCloseForced = false; //write the request and read the response, will retry processRequest(state, conn); @@ -2068,9 +2069,13 @@ } else { int expectedLength = getResponseContentLength(); if (expectedLength == -1) { - if (canResponseHaveBody(statusLine.getStatusCode())) { - LOG.warn("Response content length is not known"); - setConnectionCloseForced(true); + if (canResponseHaveBody(statusLine.getStatusCode())) { + Header connectionHeader = responseHeaders.getFirstHeader("Connection"); + if ((connectionHeader == null) + || (!connectionHeader.getValue().equalsIgnoreCase("close"))) { + LOG.warn("Response content length is not known"); + setConnectionCloseForced(true); + } result = is; } } else { @@ -2746,6 +2751,7 @@ responseConnection.close(); } } + this.connectionCloseForced = false; doneWithConnection = true; if (!inExecute) { ensureConnectionRelease(); Index: test/org/apache/commons/httpclient/TestHttpConnection.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpConnection.java,v retrieving revision 1.8 diff -u -r1.8 TestHttpConnection.java --- test/org/apache/commons/httpclient/TestHttpConnection.java 5 Mar 2003 04:02:57 -0000 1.8 +++ test/org/apache/commons/httpclient/TestHttpConnection.java 3 Nov 2003 17:46:13 -0000 @@ -1,7 +1,7 @@ /* - * $Header: $ - * $Revision: $ - * $Date: $ + * $Header$ + * $Revision$ + * $Date$ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -81,7 +81,7 @@ * * @author Sean C. Sullivan * - * @version $Id: $ + * @version $Id$ * */ public class TestHttpConnection extends TestLocalHostBase { 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.8 diff -u -r1.8 TestResponseHeaders.java --- test/org/apache/commons/httpclient/TestResponseHeaders.java 20 Jun 2003 13:33:09 -0000 1.8 +++ test/org/apache/commons/httpclient/TestResponseHeaders.java 3 Nov 2003 17:46:13 -0000 @@ -96,18 +96,6 @@ - /** - * Simple extension of HttpMethodBase. - */ - private class SimpleHttpMethod extends HttpMethodBase { - public SimpleHttpMethod() { - super(""); - } - public String getName() { - return "simple"; - } - } - // ----------------------------------------------------------- Test Methods public void testHeaders() throws Exception { String body = "XXX\r\nYYY\r\nZZZ"; @@ -310,4 +298,32 @@ assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue()); assertTrue(method.getResponseHeader("Content-Type").toString().indexOf("boundary") != -1); } + + + public void testForceCloseConnection() throws Exception { + String body = "stuff"; + String headers = + "HTTP/1.1 200 OK\r\n" + + "Content-Type: garbage\r\n"; + HttpState state = new HttpState(); + SimpleHttpMethod method = new SimpleHttpMethod(); + SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); + method.execute(state, conn); + assertTrue("Connection should be closed", method.shouldCloseConnection(conn)); + assertTrue("Connection should be force-closed", method.isConnectionCloseForced()); + } + + public void testForceCloseConnection2() throws Exception { + String body = "stuff"; + String headers = + "HTTP/1.1 200 OK\r\n" + + "Content-Type: garbage\r\n" + + "Connection: close\r\n"; + HttpState state = new HttpState(); + SimpleHttpMethod method = new SimpleHttpMethod(); + SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); + method.execute(state, conn); + assertTrue("Connection should be closed", method.shouldCloseConnection(conn)); + assertFalse("Connection should NOT be closed", method.isConnectionCloseForced()); + } }