Return-Path: Mailing-List: contact commons-httpclient-dev-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list commons-httpclient-dev@jakarta.apache.org Received: (qmail 6109 invoked from network); 13 Apr 2003 10:13:59 -0000 Received: from mail3.bluewin.ch (195.186.1.75) by daedalus.apache.org with SMTP; 13 Apr 2003 10:13:59 -0000 Received: from [192.168.1.48] (195.186.152.33) by mail3.bluewin.ch (Bluewin AG 6.7.016) id 3E675D0C0047DAF2 for commons-httpclient-dev@jakarta.apache.org; Sun, 13 Apr 2003 10:14:12 +0000 Subject: [PATCH] EntityEnclosingMethod 'modality' fix From: Oleg Kalnichevski To: Commons HttpClient Project Content-Type: multipart/mixed; boundary="=-gY4ZkJUeKvMK8WqOgLNz" Organization: Message-Id: <1050228906.1651.28.camel@kczrh-okt22.corp.bearingpoint.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-5) Date: 13 Apr 2003 12:15:07 +0200 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N --=-gY4ZkJUeKvMK8WqOgLNz Content-Type: text/plain Content-Transfer-Encoding: 7bit Folks, Here's the draft version of the patch intended to fix the problem reported by Andre John Mas (Order of methods in PostMethod) Please let me know what you think Cheers Oleg --=-gY4ZkJUeKvMK8WqOgLNz Content-Disposition: attachment; filename=post.patch Content-Type: text/x-patch; name=post.patch; charset=KOI8-R Content-Transfer-Encoding: 7bit Index: java/org/apache/commons/httpclient/HttpConnection.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v retrieving revision 1.55 diff -u -r1.55 HttpConnection.java --- java/org/apache/commons/httpclient/HttpConnection.java 10 Apr 2003 23:36:36 -0000 1.55 +++ java/org/apache/commons/httpclient/HttpConnection.java 13 Apr 2003 09:48:19 -0000 @@ -63,7 +63,6 @@ package org.apache.commons.httpclient; -import java.io.FilterOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; Index: java/org/apache/commons/httpclient/HttpConstants.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java,v retrieving revision 1.9 diff -u -r1.9 HttpConstants.java --- java/org/apache/commons/httpclient/HttpConstants.java 10 Apr 2003 17:18:49 -0000 1.9 +++ java/org/apache/commons/httpclient/HttpConstants.java 13 Apr 2003 09:48:22 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java,v 1.9 2003/04/10 17:18:49 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java,v 1.9 2003/04/10 17:18:49 olegk Exp $ * $Revision: 1.9 $ * $Date: 2003/04/10 17:18:49 $ * @@ -70,7 +70,7 @@ /** - * DOCUMENT ME! + * HTTP content conversion routines. * * @author Oleg Kalnichevski * @author Mike Bowler Index: java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v retrieving revision 1.14 diff -u -r1.14 EntityEnclosingMethod.java --- java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java 1 Apr 2003 19:04:19 -0000 1.14 +++ java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java 13 Apr 2003 09:48:26 -0000 @@ -67,10 +67,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.Reader; -import java.io.UnsupportedEncodingException; import org.apache.commons.httpclient.ChunkedOutputStream; import org.apache.commons.httpclient.ContentLengthInputStream; @@ -114,7 +111,10 @@ private byte[] buffer = null; /** The unbuffered request body, if any. */ - private InputStream requestBodyStream = null; + private InputStream requestStream = null; + + /** The buffered request body as string, if any. */ + private String requestString = null; /** Counts how often the request was sent to the server. */ private int repeatCount = 0; @@ -187,7 +187,7 @@ * @since 2.0beta1 */ protected boolean hasRequestContent() { - return (this.requestBodyStream != null) || (this.buffer != null); + return (this.requestStream != null) || (this.requestStream != null); } /** @@ -268,22 +268,31 @@ */ public void setRequestBody(InputStream body) { LOG.trace("enter EntityEnclosingMethod.setRequestBody(InputStream)"); - this.requestBodyStream = body; + this.requestStream = body; + this.requestString = null; this.buffer = null; } /** * Gets the request body as a stream. + * Calling this method will cause the content to be buffered. * * @return The request body {@link java.io.InputStream} if it has been set. */ public InputStream getRequestBody() { LOG.trace("enter EntityEnclosingMethod.getRequestBody()"); - if (this.buffer != null) { - return new ByteArrayInputStream(this.buffer); + if (this.requestStream != null) { + bufferContent(); + } + byte [] content = null; + if (this.buffer != null) { + content = this.buffer; + } else if (this.requestString != null) { + content = HttpConstants.getContentBytes(this.requestString, getRequestCharSet()); } else { - return this.requestBodyStream; + content = new byte [] {}; } + return new ByteArrayInputStream(content); } /** @@ -293,17 +302,14 @@ */ public void setRequestBody(String body) { LOG.trace("enter EntityEnclosingMethod.setRequestBody(String)"); - - if (body == null) { - this.requestBodyStream = null; - this.buffer = null; - return; - } - this.buffer = HttpConstants.getContentBytes(body, getRequestCharSet()); + this.requestString = body; + this.requestStream = null; + this.buffer = null; } /** * Gets the request body as a String. + * Calling this method will cause the content to be buffered. * * @return the request body as a string * @@ -312,24 +318,16 @@ public String getRequestBodyAsString() throws IOException { LOG.trace("enter EntityEnclosingMethod.getRequestBodyAsString()"); - Reader instream = null; - try { - instream = new InputStreamReader(getRequestBody(), getRequestCharSet()); - } catch (UnsupportedEncodingException e) { - if (LOG.isWarnEnabled()) { - LOG.warn("Unsupported encoding: " + e.getMessage()); - } - instream = new InputStreamReader(getRequestBody()); + if (this.requestStream != null) { + bufferContent(); } - - StringBuffer buffer = new StringBuffer(); - char[] tmp = new char[4096]; - int l = 0; - while ((l = instream.read(tmp)) >= 0) { - buffer.append(tmp, 0, l); + if (this.buffer != null) { + return HttpConstants.getContentString(this.buffer, getRequestCharSet()); + } else if (this.requestString != null) { + return this.requestString; + } else { + return null; } - - return buffer.toString(); } @@ -356,7 +354,21 @@ throw new HttpException( "Chunked transfer encoding not allowed for HTTP/1.0"); } - InputStream instream = getRequestBody(); + + if (this.requestString != null) { + // make sure we have the request content buffered at this point + bufferContent(); + } + + InputStream instream = null; + if (this.buffer != null) { + // Request body has been buffered + instream = new ByteArrayInputStream(this.buffer); + } else { + // Request body has not been buffered + instream = this.requestStream; + } + if (instream == null) { LOG.debug("Request body is empty"); return true; @@ -406,7 +418,8 @@ public void recycle() { LOG.trace("enter EntityEnclosingMethod.recycle()"); this.requestContentLength = CONTENT_LENGTH_AUTO; - this.requestBodyStream = null; + this.requestStream = null; + this.requestString = null; this.buffer = null; this.repeatCount = 0; super.recycle(); @@ -420,24 +433,27 @@ LOG.trace("enter EntityEnclosingMethod.bufferContent()"); if (this.buffer != null) { + // Already been buffered return; } - if (this.requestBodyStream == null) { - return; - } - try { - ByteArrayOutputStream tmp = new ByteArrayOutputStream(); - byte[] data = new byte[4096]; - int l = 0; - while ((l = this.requestBodyStream.read(data)) >= 0) { - tmp.write(data, 0, l); + if (this.requestStream != null) { + try { + ByteArrayOutputStream tmp = new ByteArrayOutputStream(); + byte[] data = new byte[4096]; + int l = 0; + while ((l = this.requestStream.read(data)) >= 0) { + tmp.write(data, 0, l); + } + this.buffer = tmp.toByteArray(); + this.requestStream = null; + } catch (IOException e) { + LOG.error(e.getMessage(), e); + this.buffer = null; + this.requestStream = null; } - this.buffer = tmp.toByteArray(); - this.requestBodyStream = null; - } catch (IOException e) { - LOG.error(e.getMessage(), e); - this.buffer = null; - this.requestBodyStream = null; + } else if (this.requestString != null) { + this.buffer = HttpConstants.getContentBytes( + this.requestString, getResponseCharSet()); } } } --=-gY4ZkJUeKvMK8WqOgLNz--