rwaldhoff 01/08/13 10:38:56
Modified: httpclient/src/java/org/apache/commons/httpclient/methods
Tag: rlwrefactoring GetMethod.java HeadMethod.java
PostMethod.java
httpclient/src/java/org/apache/commons/httpclient Tag:
rlwrefactoring HttpConnection.java
HttpMethodBase.java ResponseInputStream.java
Log:
better handling of some HTTP/1.1 features (chunked responses, 100 continue header,
etc.)
Revision Changes Path
No revision
No revision
1.2.2.4 +5 -5
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/GetMethod.java
Index: GetMethod.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/GetMethod.java,v
retrieving revision 1.2.2.3
retrieving revision 1.2.2.4
diff -u -r1.2.2.3 -r1.2.2.4
--- GetMethod.java 2001/08/11 00:48:40 1.2.2.3
+++ GetMethod.java 2001/08/13 17:38:56 1.2.2.4
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/GetMethod.java,v
1.2.2.3 2001/08/11 00:48:40 rwaldhoff Exp $
- * $Revision: 1.2.2.3 $
- * $Date: 2001/08/11 00:48:40 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/GetMethod.java,v
1.2.2.4 2001/08/13 17:38:56 rwaldhoff Exp $
+ * $Revision: 1.2.2.4 $
+ * $Date: 2001/08/13 17:38:56 $
*
* ====================================================================
*
@@ -71,6 +71,7 @@
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpConnection;
+import org.apache.commons.httpclient.ResponseInputStream;
/**
* GET Method.
@@ -387,7 +388,7 @@
}
}
}
- InputStream is = conn.getResponseInputStream();
+ InputStream is = conn.getResponseInputStream(this);
byte[] buffer = new byte[4096];
int nb = 0;
while (true) {
@@ -416,7 +417,6 @@
}
out.close();
-
}
/**
1.3.2.2 +9 -3
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/HeadMethod.java
Index: HeadMethod.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/HeadMethod.java,v
retrieving revision 1.3.2.1
retrieving revision 1.3.2.2
diff -u -r1.3.2.1 -r1.3.2.2
--- HeadMethod.java 2001/08/10 22:27:12 1.3.2.1
+++ HeadMethod.java 2001/08/13 17:38:56 1.3.2.2
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/HeadMethod.java,v
1.3.2.1 2001/08/10 22:27:12 rwaldhoff Exp $
- * $Revision: 1.3.2.1 $
- * $Date: 2001/08/10 22:27:12 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/HeadMethod.java,v
1.3.2.2 2001/08/13 17:38:56 rwaldhoff Exp $
+ * $Revision: 1.3.2.2 $
+ * $Date: 2001/08/13 17:38:56 $
*
* ====================================================================
*
@@ -69,6 +69,8 @@
import org.apache.commons.httpclient.State;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpConnection;
/**
* HEAD Method.
@@ -124,6 +126,10 @@
return false;
}
+ protected void readResponseBody(State state, HttpConnection conn) throws
IOException, HttpException {
+ log.debug("HeadMethod.readResponseBody(State,HttpConnection)");
+ return; // despite the possible presence of a content-length header, HEAD
returns no response body
+ }
/**
* Return true if the method's response is expected to have a body.
1.3.2.3 +4 -4
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java
Index: PostMethod.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
retrieving revision 1.3.2.2
retrieving revision 1.3.2.3
diff -u -r1.3.2.2 -r1.3.2.3
--- PostMethod.java 2001/08/13 16:01:05 1.3.2.2
+++ PostMethod.java 2001/08/13 17:38:56 1.3.2.3
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
1.3.2.2 2001/08/13 16:01:05 rwaldhoff Exp $
- * $Revision: 1.3.2.2 $
- * $Date: 2001/08/13 16:01:05 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
1.3.2.3 2001/08/13 17:38:56 rwaldhoff Exp $
+ * $Revision: 1.3.2.3 $
+ * $Date: 2001/08/13 17:38:56 $
* ====================================================================
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
@@ -143,7 +143,7 @@
if(null == requestBody) {
requestBody = generateRequestBody(parameters);
}
- return requestBody.length();
+ return requestBody.getBytes().length;
}
protected String generateRequestBody(HashMap params) {
No revision
No revision
1.1.2.4 +6 -10
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Attic/HttpConnection.java
Index: HttpConnection.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Attic/HttpConnection.java,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -r1.1.2.3 -r1.1.2.4
--- HttpConnection.java 2001/08/10 23:58:39 1.1.2.3
+++ HttpConnection.java 2001/08/13 17:38:56 1.1.2.4
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Attic/HttpConnection.java,v
1.1.2.3 2001/08/10 23:58:39 rwaldhoff Exp $
- * $Revision: 1.1.2.3 $
- * $Date: 2001/08/10 23:58:39 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Attic/HttpConnection.java,v
1.1.2.4 2001/08/13 17:38:56 rwaldhoff Exp $
+ * $Revision: 1.1.2.4 $
+ * $Date: 2001/08/13 17:38:56 $
* ====================================================================
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
@@ -25,7 +25,7 @@
/**
* @author Rod Waldhoff
- * @version $Id: HttpConnection.java,v 1.1.2.3 2001/08/10 23:58:39 rwaldhoff Exp $
+ * @version $Id: HttpConnection.java,v 1.1.2.4 2001/08/13 17:38:56 rwaldhoff Exp $
*/
public class HttpConnection {
@@ -167,12 +167,8 @@
return out;
}
- public InputStream getResponseInputStream() {
- return _input;
- }
-
- public ResponseInputStream getResponseInputStream(HttpMethod method, Hashtable
responseHeaders) {
- return new ResponseInputStream(_input,method,responseHeaders);
+ public ResponseInputStream getResponseInputStream(HttpMethod method) {
+ return new ResponseInputStream(_input,method);
}
public void write(byte[] data) throws IOException {
1.10.2.5 +53 -11
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.10.2.4
retrieving revision 1.10.2.5
diff -u -r1.10.2.4 -r1.10.2.5
--- HttpMethodBase.java 2001/08/13 16:01:05 1.10.2.4
+++ HttpMethodBase.java 2001/08/13 17:38:56 1.10.2.5
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
1.10.2.4 2001/08/13 16:01:05 rwaldhoff Exp $
- * $Revision: 1.10.2.4 $
- * $Date: 2001/08/13 16:01:05 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
1.10.2.5 2001/08/13 17:38:56 rwaldhoff Exp $
+ * $Revision: 1.10.2.5 $
+ * $Date: 2001/08/13 17:38:56 $
* ====================================================================
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
@@ -14,6 +14,7 @@
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.HashMap;
@@ -48,7 +49,7 @@
protected int statusCode = -1;
protected String statusText = "Not executed";
protected boolean used = false;
- protected boolean http11 = false;
+ protected boolean http11 = true;
protected boolean bodySent = false;
public HttpMethodBase() {
@@ -61,7 +62,7 @@
public abstract String getName();
protected int getRequestContentLength() {
- return -1;
+ return 0;
}
protected boolean needExpectation() {
@@ -491,7 +492,50 @@
protected void readResponseBody(State state, HttpConnection conn) throws
IOException, HttpException {
log.debug("HttpMethodBase.readResponseBody(State,HttpConnection)");
- return;
+
+ OutputStream out = new ByteArrayOutputStream();
+ int expectedLength = 0;
+ int foundLength = 0;
+ {
+ Header lengthHeader = getResponseHeader("Content-Length");
+ Header transferEncodingHeader = getResponseHeader("Transfer-Encoding");
+ if(null != lengthHeader) {
+ try {
+ expectedLength = Integer.parseInt(lengthHeader.getValue());
+ } catch(NumberFormatException e) {
+ // ignored
+ }
+ } else if(null != transferEncodingHeader) {
+ if("chunked".equalsIgnoreCase(transferEncodingHeader.getValue())) {
+ expectedLength = -1;
+ }
+ }
+ }
+ InputStream is = conn.getResponseInputStream(this);
+ byte[] buffer = new byte[4096];
+ int nb = 0;
+ while(expectedLength == -1 || foundLength < expectedLength) {
+ nb = is.read(buffer);
+ if (nb == -1)
+ break;
+ if (out == null)
+ throw new IOException("Unable to buffer data");
+ if(wireLog.isInfoEnabled()) {
+ wireLog.info("<< \"" + new String(buffer,0,nb) + "\"");
+ }
+ out.write(buffer, 0, nb);
+ foundLength += nb;
+ if(expectedLength > -1) {
+ if(foundLength == expectedLength) {
+ break;
+ } else if(foundLength > expectedLength) {
+ log.warn("HttpMethodBase.readResponseBody(): expected length ("
+ expectedLength + ") exceeded. Found " + foundLength + " bytes.");
+ break;
+ }
+ }
+ }
+ out.close();
+ //is.close();
}
protected void addRequestHeaders(HashMap headers) {
@@ -610,11 +654,9 @@
if(!requestHeaders.containsKey("content-length")) {
if(-1 < len) {
setRequestHeader("Content-Length",String.valueOf(len));
- } else {
- if(http11) {
- // XXX should this be an "add" rather than a "set"?
- setRequestHeader("Transfer-Encoding","chunked");
- }
+ } else if(http11 && len < 0) {
+ // XXX should this be an "add" rather than a "set"?
+ setRequestHeader("Transfer-Encoding","chunked");
}
}
1.6.2.2 +10 -13
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ResponseInputStream.java
Index: ResponseInputStream.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ResponseInputStream.java,v
retrieving revision 1.6.2.1
retrieving revision 1.6.2.2
diff -u -r1.6.2.1 -r1.6.2.2
--- ResponseInputStream.java 2001/08/10 22:27:12 1.6.2.1
+++ ResponseInputStream.java 2001/08/13 17:38:56 1.6.2.2
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ResponseInputStream.java,v
1.6.2.1 2001/08/10 22:27:12 rwaldhoff Exp $
- * $Revision: 1.6.2.1 $
- * $Date: 2001/08/10 22:27:12 $
+ * $Header:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ResponseInputStream.java,v
1.6.2.2 2001/08/13 17:38:56 rwaldhoff Exp $
+ * $Revision: 1.6.2.2 $
+ * $Date: 2001/08/13 17:38:56 $
*
* ====================================================================
*
@@ -74,7 +74,7 @@
* Socket input stream wrapper.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a>
- * @version $Revision: 1.6.2.1 $ $Date: 2001/08/10 22:27:12 $
+ * @version $Revision: 1.6.2.2 $ $Date: 2001/08/13 17:38:56 $
*/
public class ResponseInputStream
@@ -96,24 +96,21 @@
*
* @param request The associated request
*/
- public ResponseInputStream(InputStream stream, HttpMethod method,
- Hashtable responseHeaders) {
+ public ResponseInputStream(InputStream stream, HttpMethod method) {
super();
closed = false;
count = 0;
// Retrieving transfer encoding header
- Header transferEncoding =
- (Header) responseHeaders.get("transfer-encoding");
- if ((transferEncoding != null)
+ Header transferEncoding = method.getResponseHeader("transfer-encoding");
+ if((transferEncoding != null)
&& (transferEncoding.getValue().indexOf("chunked") != -1))
chunk = true;
// Retrieving content length header
- Header contentLength =
- (Header) responseHeaders.get("content-length");
- if (contentLength != null) {
+ Header contentLength = method.getResponseHeader("content-length");
+ if(contentLength != null) {
try {
this.contentLength =
Integer.parseInt(contentLength.getValue());
@@ -131,7 +128,7 @@
// ----------------------------------------------------- Instance Variables
- protected boolean expectsBody = false;
+ protected boolean expectsBody = true;
/**
* Stream interceptor.
*/