jsdever 2002/11/08 15:14:31 Modified: httpclient/src/java/org/apache/commons/httpclient HttpMethodBase.java httpclient/src/test/org/apache/commons/httpclient TestResponseHeaders.java Log: Fix handling of multiple Content-Length headers. Reported by Joerg Heinicke Contributed by: Mike Becke Revision Changes Path 1.78 +27 -6 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java Index: HttpMethodBase.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.77 retrieving revision 1.78 diff -u -r1.77 -r1.78 --- HttpMethodBase.java 8 Nov 2002 22:25:07 -0000 1.77 +++ HttpMethodBase.java 8 Nov 2002 23:14:30 -0000 1.78 @@ -1597,12 +1597,33 @@ result = new ChunkedInputStream(is, this); } } else if (null != lengthHeader) { + + // we're using this just in case the content length is duplicated + // i.e. '57, 57' + HeaderElement[] lengthElements = lengthHeader.getValues(); + String lengthValue = null; + + if ( lengthElements.length > 1 ) { + // looks like the content length header was duplicated. if so + // they won't be key=value pairs so we just want to get + // the name not the value (which should be null) + // take the first value and ignore any others + lengthValue = lengthElements[0].getName(); + } else { + lengthValue = lengthHeader.getValue(); + } + try { - int expectedLength = Integer.parseInt(lengthHeader.getValue()); + int expectedLength = Integer.parseInt(lengthValue); result = new ContentLengthInputStream(is, expectedLength); } catch(NumberFormatException e) { - // ignored + throw new HttpException( + "Unable to parse server response content length: '" + + lengthValue + "'" + ); + } + } else if(canResponseHaveBody(statusLine.getStatusCode()) && !getName().equals(ConnectMethod.NAME)){ result = is; 1.4 +22 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java Index: TestResponseHeaders.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TestResponseHeaders.java 6 Aug 2002 19:47:29 -0000 1.3 +++ TestResponseHeaders.java 8 Nov 2002 23:14:30 -0000 1.4 @@ -132,6 +132,24 @@ assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue()); } + /** + * Tests that having a duplicate content length causes no problems. + */ + public void testDuplicateContentLength() throws Exception { + + String body = "XXX\r\nYYY\r\nZZZ"; + String headers = + "HTTP/1.1 200 OK\r\n" + + "Content-Length: " + body.length() + "\r\n" + + "Content-Length: " + body.length() + "\r\n"; + HttpState state = new HttpState(); + HttpMethod method = new SimpleHttpMethod(); + SimpleHttpConnection conn = new SimpleHttpConnection(headers, body); + method.execute(state, conn); + assertNotNull( "Response body is null.", method.getResponseBodyAsStream() ); + + } + public void testNullHeaders() throws Exception { String body = "XXX\r\nYYY\r\nZZZ"; String headers =
-- To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>