[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit WebResponse.java,1.120,1.121
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-07-06 13:21:19
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory sc8-pr-cvs1:/tmp/cvs-serv2045/src/com/meterware/httpunit
Modified Files:
WebResponse.java
Log Message:
Correct handling of gzipped response
Index: WebResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v
retrieving revision 1.120
retrieving revision 1.121
diff -u -r1.120 -r1.121
--- WebResponse.java 23 Jun 2003 23:54:03 -0000 1.120
+++ WebResponse.java 6 Jul 2003 13:21:16 -0000 1.121
@@ -174,7 +174,7 @@
_contentLength = (length == null) ? -1 : Integer.parseInt( length );
}
return _contentLength;
- }
+ }
/**
@@ -288,6 +288,22 @@
/**
+ * Returns a list of HTML element names contained in this HTML section.
+ */
+ public String[] getElementNames() throws SAXException {
+ return getReceivedPage().getElementNames();
+ }
+
+
+ /**
+ * Returns the HTMLElements found in this segment with the specified name.
+ */
+ public HTMLElement[] getElementsWithName( String name ) throws SAXException {
+ return getReceivedPage().getElementsWithName( name );
+ }
+
+
+ /**
* Returns the forms found in the page in the order in which they appear.
* @exception SAXException thrown if there is an error parsing the response.
**/
@@ -719,7 +735,8 @@
}
if (encodedUsingGZIP()) {
- _inputStream = new GZIPInputStream( inputStream );
+ byte[] compressedData = readFromStream( inputStream, getContentLength() );
+ _inputStream = new GZIPInputStream( new ByteArrayInputStream( compressedData ) );
} else {
_inputStream = inputStream;
}
@@ -838,29 +855,37 @@
InputStream inputStream = getInputStream();
try {
- int bytesRemaining = getContentLength() < 0 ? Integer.MAX_VALUE : getContentLength();
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[8 * 1024];
- int count = 0;
- do {
- outputStream.write( buffer, 0, count );
- bytesRemaining -= count;
- if (bytesRemaining <= 0) break;
- count = inputStream.read( buffer, 0, buffer.length );
- } while (count != -1);
+ final int contentLength = this.encodedUsingGZIP() ? -1 : getContentLength();
+ int bytesRemaining = contentLength < 0 ? Integer.MAX_VALUE : contentLength;
+ byte[] bytes = readFromStream( inputStream, bytesRemaining );
- byte[] bytes = outputStream.toByteArray();
readTags( bytes );
_responseText = new String( bytes, getCharacterSet() );
_inputStream = new ByteArrayInputStream( bytes );
- if (HttpUnitOptions.isCheckContentLength() && getContentLength() >= 0 && bytes.length != getContentLength()) {
- throw new IOException("Truncated message. Expected length: " + getContentLength() +
+ if (HttpUnitOptions.isCheckContentLength() && contentLength >= 0 && bytes.length != contentLength) {
+ throw new IOException("Truncated message. Expected length: " + contentLength +
", Actual length: " + bytes.length);
}
} finally {
inputStream.close();
}
+ }
+
+
+ private byte[] readFromStream( InputStream inputStream, int maxBytes ) throws IOException {
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ byte[] buffer = new byte[8 * 1024];
+ int count = 0;
+ do {
+ outputStream.write( buffer, 0, count );
+ maxBytes -= count;
+ if (maxBytes <= 0) break;
+ count = inputStream.read( buffer, 0, Math.min( maxBytes, buffer.length ) );
+ } while (count != -1);
+
+ byte[] bytes = outputStream.toByteArray();
+ return bytes;
}
|