From httpclient-commits-returnbrittneyanderson@example.org Thu Dec 01 20:39:03 2005 Return-Path: <httpclient-commits-returnbrittneyanderson@example.org> Delivered-To: brittneyanderson@example.org Received: (qmail 60373 invoked from network); 1 Dec 2005 20:39:03 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (83.161.120.155) by minotaur.apache.org with SMTP; 1 Dec 2005 20:39:03 -0000 Received: (qmail 14523 invoked by uid 500); 1 Dec 2005 20:39:03 -0000 Mailing-List: contact brittneyanderson@example.org; run by ezmlm Precedence: bulk List-Help: <mailto:brittneyanderson@example.org> List-Unsubscribe: <mailto:brittneyanderson@example.org> List-Post: <mailto:brittneyanderson@example.org> List-Id: <httpclient-commits.jakarta.apache.org> Reply-To: brittneyanderson@example.org Delivered-To: mailing list brittneyanderson@example.org Received: (qmail 14512 invoked by uid 99); 1 Dec 2005 20:39:03 -0000 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [83.161.120.155] (HELO minotaur.apache.org) (83.161.120.155) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 01 Dec 2005 12:39:02 -0800 Received: (qmail 60309 invoked by uid 65534); 1 Dec 2005 20:38:42 -0000 Message-ID: <brittneyanderson@example.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r351465 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/impl/io/ java/org/apache/http/io/ java/org/apache/http/util/ test/org/apache/http/io/ Date: Thu, 01 Dec 2005 20:38:40 -0000 To: brittneyanderson@example.org From: brittneyanderson@example.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: olegk Date: Thu Dec 1 12:38:25 2005 New Revision: 351465 URL: http://svn.apache.org/viewcvs?rev=351465&view=rev Log: ByteArrayBuffer and CharArrayBuffer classes no longer publicly expose underlying internal buffers Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java?rev=351465&r1=351464&r2=351465&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/io/AbstractHttpDataReceiver.java Thu Dec 1 12:38:25 2005 @@ -207,10 +207,13 @@ } } } - copyToCharBuffer( - this.linebuffer.internBuffer(), 0, this.linebuffer.length(), - charbuffer); - return this.linebuffer.length(); + l = this.linebuffer.length(); + if (this.ascii) { + charbuffer.append(this.linebuffer, 0, l); + } else { + charbuffer.append(this.linebuffer, 0, l, this.charset); + } + return l; } private int lineFromReadBuffer(final CharArrayBuffer charbuffer, int pos) { @@ -222,33 +225,14 @@ pos--; } len = pos - off; - copyToCharBuffer(this.buffer, off, len, charbuffer); - return len; - } - - private void copyToCharBuffer(final byte[] b, int off, int len, - final CharArrayBuffer charbuffer) { if (this.ascii) { - // this is an uuuuugly performance hack - charbuffer.ensureCapacity(len); - int oldlen = charbuffer.length(); - int newlen = oldlen + len; - charbuffer.setLength(newlen); - char[] tmp = charbuffer.internBuffer(); - for (int i = oldlen; i < newlen; i++) { - int ch = b[off + i]; - if (ch < 0) { - ch = 256 + ch; - } - tmp[i] = (char) ch; - } + charbuffer.append(this.buffer, off, len); } else { - String s = EncodingUtils.getString(b, off, len, this.charset); - charbuffer.ensureCapacity(s.length()); - charbuffer.append(s); + charbuffer.append(this.buffer, off, len, this.charset); } + return len; } - + public String readLine() throws IOException { CharArrayBuffer charbuffer = new CharArrayBuffer(64); int l = readLine(charbuffer); Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java?rev=351465&r1=351464&r2=351465&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ByteArrayBuffer.java Thu Dec 1 12:38:25 2005 @@ -36,10 +36,10 @@ * * @since 4.0 */ -public class ByteArrayBuffer { +public final class ByteArrayBuffer { - private byte[] buffer; - private int len; + protected byte[] buffer; + protected int len; public ByteArrayBuffer(int capacity) { super(); @@ -76,10 +76,6 @@ public void clear() { this.len = 0; - } - - public byte[] internBuffer() { - return this.buffer; } public byte[] toByteArray() { Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java?rev=351465&r1=351464&r2=351465&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/CharArrayBuffer.java Thu Dec 1 12:38:25 2005 @@ -29,6 +29,8 @@ package org.apache.http.io; +import org.apache.http.util.EncodingUtils; + /** * @author <a href="mailto:brittneyanderson@example.org">Oleg Kalnichevski</a> * @@ -36,10 +38,10 @@ * * @since 4.0 */ -public class CharArrayBuffer { +public final class CharArrayBuffer { - private char[] buffer; - private int len; + protected char[] buffer; + protected int len; public CharArrayBuffer(int capacity) { super(); @@ -87,6 +89,20 @@ this.len = newlen; } + public void append(final CharArrayBuffer b, int off, int len) { + if (b == null) { + return; + } + append(b.buffer, off, len); + } + + public void append(final CharArrayBuffer b) { + if (b == null) { + return; + } + append(b.buffer,0, b.len); + } + public void append(char ch) { int newlen = this.len + 1; if (newlen > this.buffer.length) { @@ -96,16 +112,59 @@ this.len = newlen; } + public void append(final byte[] b, int off, int len) { + if (b == null) { + return; + } + if ((off < 0) || (off > b.length) || (len < 0) || + ((off + len) < 0) || ((off + len) > b.length)) { + throw new IndexOutOfBoundsException(); + } + if (len == 0) { + return; + } + int oldlen = this.len; + int newlen = oldlen + len; + if (newlen > this.buffer.length) { + expand(newlen); + } + for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) { + int ch = b[i1]; + if (ch < 0) { + ch = 256 + ch; + } + this.buffer[i2] = (char) ch; + } + this.len = newlen; + } + + public void append(final byte[] b, int off, int len, final String charset) { + if (b == null) { + return; + } + append(EncodingUtils.getString(b, off, len, charset)); + } + + public void append(final ByteArrayBuffer b, int off, int len) { + if (b == null) { + return; + } + append(b.buffer, off, len); + } + + public void append(final ByteArrayBuffer b, int off, int len, final String charset) { + if (b == null) { + return; + } + append(b.buffer, off, len, charset); + } + public void append(final Object obj) { append(String.valueOf(obj)); } public void clear() { this.len = 0; - } - - public char[] internBuffer() { - return this.buffer; } public char[] toCharArray() { Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java?rev=351465&r1=351464&r2=351465&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java Thu Dec 1 12:38:25 2005 @@ -85,7 +85,7 @@ i++; } previous.append(' '); - previous.append(current.internBuffer(), i, current.length() - i); + previous.append(current, i, current.length() - i); } else { headerLines.add(current); previous = current; Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java?rev=351465&r1=351464&r2=351465&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestByteArrayBuffer.java Thu Dec 1 12:38:25 2005 @@ -59,7 +59,6 @@ ByteArrayBuffer buffer = new ByteArrayBuffer(16); assertEquals(16, buffer.capacity()); assertEquals(0, buffer.length()); - assertEquals(16, buffer.internBuffer().length); try { new ByteArrayBuffer(-1); fail("IllegalArgumentException should have been thrown"); Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java?rev=351465&r1=351464&r2=351465&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/io/TestCharArrayBuffer.java Thu Dec 1 12:38:25 2005 @@ -59,7 +59,6 @@ CharArrayBuffer buffer = new CharArrayBuffer(16); assertEquals(16, buffer.capacity()); assertEquals(0, buffer.length()); - assertEquals(16, buffer.internBuffer().length); try { new CharArrayBuffer(-1); fail("IllegalArgumentException should have been thrown"); @@ -127,10 +126,26 @@ public void testAppendNullString() throws Exception { CharArrayBuffer buffer = new CharArrayBuffer(8); - buffer.append(null); + buffer.append((String)null); assertEquals("null", buffer.toString()); } + public void testAppendCharArrayBuffer() throws Exception { + CharArrayBuffer buffer1 = new CharArrayBuffer(8); + buffer1.append(" and more stuff"); + CharArrayBuffer buffer2 = new CharArrayBuffer(8); + buffer2.append("stuff"); + buffer2.append(buffer1); + assertEquals("stuff and more stuff", buffer2.toString()); + } + + public void testAppendNullCharArrayBuffer() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(8); + buffer.append((CharArrayBuffer)null); + buffer.append((CharArrayBuffer)null, 0, 0); + assertEquals("", buffer.toString()); + } + public void testAppendSingleChar() throws Exception { CharArrayBuffer buffer = new CharArrayBuffer(4); buffer.append('1'); @@ -142,9 +157,9 @@ assertEquals("123456", buffer.toString()); } - public void testInvalidAppend() throws Exception { + public void testInvalidCharArrayAppend() throws Exception { CharArrayBuffer buffer = new CharArrayBuffer(4); - buffer.append(null, 0, 0); + buffer.append((char[])null, 0, 0); char[] tmp = new char[] { '1', '2', '3', '4'}; try { @@ -269,5 +284,112 @@ } catch (IndexOutOfBoundsException ex) { // expected } + } + + static final int SWISS_GERMAN_HELLO [] = { + 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 + }; + + static final int RUSSIAN_HELLO [] = { + 0x412, 0x441, 0x435, 0x43C, 0x5F, 0x43F, 0x440, 0x438, + 0x432, 0x435, 0x442 + }; + + private static String constructString(int [] unicodeChars) { + StringBuffer buffer = new StringBuffer(); + if (unicodeChars != null) { + for (int i = 0; i < unicodeChars.length; i++) { + buffer.append((char)unicodeChars[i]); + } + } + return buffer.toString(); + } + + public void testAppendAsciiByteArray() throws Exception { + String s1 = "stuff"; + String s2 = " and more stuff"; + byte[] b1 = s1.getBytes("US-ASCII"); + byte[] b2 = s2.getBytes("US-ASCII"); + + CharArrayBuffer buffer = new CharArrayBuffer(8); + buffer.append(b1, 0, b1.length); + buffer.append(b2, 0, b2.length); + + assertEquals("stuff and more stuff", buffer.toString()); + } + + public void testAppendISOByteArray() throws Exception { + String s1 = constructString(SWISS_GERMAN_HELLO); + byte[] b1 = s1.getBytes("ISO-8859-1"); + + CharArrayBuffer buffer = new CharArrayBuffer(8); + buffer.append(b1, 0, b1.length); + + assertEquals(s1, buffer.toString()); + } + + public void testAppendByteArrayWithCharset() throws Exception { + String s1 = constructString(SWISS_GERMAN_HELLO); + String s2 = constructString(RUSSIAN_HELLO); + byte[] b1 = s1.getBytes("UTF-8"); + byte[] b2 = s2.getBytes("UTF-8"); + + CharArrayBuffer buffer = new CharArrayBuffer(8); + buffer.append(b1, 0, b1.length, "UTF-8"); + buffer.append(b2, 0, b2.length, "UTF-8"); + + assertEquals(s1 + s2, buffer.toString()); + } + + public void testAppendNullByteArray() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(8); + buffer.append((byte[])null, 0, 0); + buffer.append((byte[])null, 0, 0, "US-ASCII"); + assertEquals("", buffer.toString()); } + + public void testAppendNullByteArrayBuffer() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(8); + buffer.append((ByteArrayBuffer)null, 0, 0); + buffer.append((ByteArrayBuffer)null, 0, 0, "US-ASCII"); + assertEquals("", buffer.toString()); + } + + public void testInvalidAppendAsciiByteArray() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(4); + buffer.append((byte[])null, 0, 0); + + byte[] tmp = new byte[] { '1', '2', '3', '4'}; + try { + buffer.append(tmp, -1, 0); + fail("IndexOutOfBoundsException should have been thrown"); + } catch (IndexOutOfBoundsException ex) { + // expected + } + try { + buffer.append(tmp, 0, -1); + fail("IndexOutOfBoundsException should have been thrown"); + } catch (IndexOutOfBoundsException ex) { + // expected + } + try { + buffer.append(tmp, 0, 8); + fail("IndexOutOfBoundsException should have been thrown"); + } catch (IndexOutOfBoundsException ex) { + // expected + } + try { + buffer.append(tmp, 10, Integer.MAX_VALUE); + fail("IndexOutOfBoundsException should have been thrown"); + } catch (IndexOutOfBoundsException ex) { + // expected + } + try { + buffer.append(tmp, 2, 4); + fail("IndexOutOfBoundsException should have been thrown"); + } catch (IndexOutOfBoundsException ex) { + // expected + } + } + } From httpclient-commits-returnbrittneyanderson@example.org Thu Dec 01 21:05:05 2005 Return-Path: <httpclient-commits-returnbrittneyanderson@example.org> Delivered-To: brittneyanderson@example.org Received: (qmail 76487 invoked from network); 1 Dec 2005 21:05:05 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (83.161.120.155) by minotaur.apache.org with SMTP; 1 Dec 2005 21:05:05 -0000 Received: (qmail 73102 invoked by uid 500); 1 Dec 2005 21:04:59 -0000 Mailing-List: contact brittneyanderson@example.org; run by ezmlm Precedence: bulk List-Help: <mailto:brittneyanderson@example.org> List-Unsubscribe: <mailto:brittneyanderson@example.org> List-Post: <mailto:brittneyanderson@example.org> List-Id: <httpclient-commits.jakarta.apache.org> Reply-To: brittneyanderson@example.org Delivered-To: mailing list brittneyanderson@example.org Received: (qmail 73070 invoked by uid 99); 1 Dec 2005 21:04:59 -0000 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [83.161.120.155] (HELO minotaur.apache.org) (83.161.120.155) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 01 Dec 2005 13:04:58 -0800 Received: (qmail 76335 invoked by uid 65534); 1 Dec 2005 21:04:38 -0000 Message-ID: <brittneyanderson@example.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r351468 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/ java/org/apache/http/impl/ java/org/apache/http/io/ java/org/apache/http/util/ test/org/apache/http/ test/org/apache/http/util/ Date: Thu, 01 Dec 2005 21:04:36 -0000 To: brittneyanderson@example.org From: brittneyanderson@example.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: olegk Date: Thu Dec 1 13:04:21 2005 New Revision: 351468 URL: http://svn.apache.org/viewcvs?rev=351468&view=rev Log: HeaderParser code moved to Header class Removed: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/HeaderParser.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestHeadersParser.java Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java?rev=351468&r1=351467&r2=351468&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java Thu Dec 1 13:04:21 2005 @@ -29,7 +29,11 @@ package org.apache.http; +import java.io.IOException; +import java.util.ArrayList; + import org.apache.http.io.CharArrayBuffer; +import org.apache.http.io.HttpDataReceiver; /** * <p>An HTTP header.</p> @@ -177,6 +181,52 @@ */ public HeaderElement[] getElements() { return HeaderElement.parseAll(this.buffer, this.posValue, this.buffer.length()); + } + + public static Header[] parseAll(final HttpDataReceiver datareceiver) + throws HttpException, IOException { + ArrayList headerLines = new ArrayList(); + + CharArrayBuffer current = null; + CharArrayBuffer previous = null; + for (;;) { + if (current == null) { + current = new CharArrayBuffer(64); + } else { + current.clear(); + } + int l = datareceiver.readLine(current); + if (l == -1 || current.length() < 1) { + break; + } + // Parse the header name and value + // Check for folded headers first + // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2 + // discussion on folded headers + if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) { + // we have continuation folded header + // so append value + int i = 0; + while (i < current.length()) { + char ch = current.charAt(i); + if (ch != ' ' && ch != '\t') { + break; + } + i++; + } + previous.append(' '); + previous.append(current, i, current.length() - i); + } else { + headerLines.add(current); + previous = current; + current = null; + } + } + Header[] headers = new Header[headerLines.size()]; + for (int i = 0; i < headerLines.size(); i++) { + headers[i] = new Header((CharArrayBuffer) headerLines.get(i)); + } + return headers; } } Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java?rev=351468&r1=351467&r2=351468&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpClientConnection.java Thu Dec 1 13:04:21 2005 @@ -57,7 +57,6 @@ import org.apache.http.io.SocketFactory; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; -import org.apache.http.util.HeaderParser; /** * <p> @@ -332,7 +331,7 @@ protected void readResponseHeaders( final HttpMutableResponse response) throws HttpException, IOException { - Header[] headers = HeaderParser.parse(this.datareceiver); + Header[] headers = Header.parseAll(this.datareceiver); for (int i = 0; i < headers.length; i++) { response.addHeader(headers[i]); if (isWirelogEnabled()) { Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java?rev=351468&r1=351467&r2=351468&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/impl/DefaultHttpServerConnection.java Thu Dec 1 13:04:21 2005 @@ -49,7 +49,6 @@ import org.apache.http.impl.entity.EntityWriter; import org.apache.http.io.CharArrayBuffer; import org.apache.http.params.HttpParams; -import org.apache.http.util.HeaderParser; /** * <p> @@ -123,7 +122,7 @@ protected void receiveRequestHeaders(final HttpMutableRequest request) throws HttpException, IOException { - Header[] headers = HeaderParser.parse(this.datareceiver); + Header[] headers = Header.parseAll(this.datareceiver); for (int i = 0; i < headers.length; i++) { request.addHeader(headers[i]); if (isWirelogEnabled()) { Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java?rev=351468&r1=351467&r2=351468&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java Thu Dec 1 13:04:21 2005 @@ -35,7 +35,6 @@ import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.util.ExceptionUtils; -import org.apache.http.util.HeaderParser; /** * <p>This class implements chunked transfer coding as described in the @@ -289,7 +288,7 @@ */ private void parseTrailerHeaders() throws IOException { try { - this.footers = HeaderParser.parse(in); + this.footers = Header.parseAll(in); } catch (HttpException e) { IOException ioe = new MalformedChunkCodingException("Invalid footer: " + e.getMessage()); Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java?rev=351468&r1=351467&r2=351468&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.java Thu Dec 1 13:04:21 2005 @@ -30,6 +30,8 @@ package org.apache.http; import org.apache.http.io.CharArrayBuffer; +import org.apache.http.io.HttpDataReceiver; +import org.apache.http.mockup.HttpDataReceiverMockup; import junit.framework.Test; import junit.framework.TestCase; @@ -146,5 +148,48 @@ assertEquals(1, elements[1].getParameters().length); } + public void testBasicHeaderParsing() throws Exception { + String s = + "header1: stuff\r\n" + + "header2 : stuff \r\n" + + "header3: stuff\r\n" + + " and more stuff\r\n" + + "\t and even more stuff\r\n" + + " \r\n" + + "\r\n"; + HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII"); + Header[] headers = Header.parseAll(receiver); + assertNotNull(headers); + assertEquals(3, headers.length); + assertEquals("header1", headers[0].getName()); + assertEquals("stuff", headers[0].getValue()); + assertEquals("header2", headers[1].getName()); + assertEquals("stuff", headers[1].getValue()); + assertEquals("header3", headers[2].getName()); + assertEquals("stuff and more stuff and even more stuff", headers[2].getValue()); + } + + public void testParsingMalformedFirstHeader() throws Exception { + String s = + " header1: stuff\r\n" + + "header2 : stuff \r\n"; + HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII"); + Header[] headers = Header.parseAll(receiver); + assertNotNull(headers); + assertEquals(2, headers.length); + assertEquals("header1", headers[0].getName()); + assertEquals("stuff", headers[0].getValue()); + assertEquals("header2", headers[1].getName()); + assertEquals("stuff", headers[1].getValue()); + } + + public void testEmptyDataStream() throws Exception { + String s = ""; + HttpDataReceiver receiver = new HttpDataReceiverMockup(s, "US-ASCII"); + Header[] headers = Header.parseAll(receiver); + assertNotNull(headers); + assertEquals(0, headers.length); + } + } Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java?rev=351468&r1=351467&r2=351468&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java Thu Dec 1 13:04:21 2005 @@ -42,7 +42,6 @@ suite.addTest(TestExceptionUtils.suite()); suite.addTest(TestEncodingUtils.suite()); suite.addTest(TestParameterFormatter.suite()); - suite.addTest(TestHeadersParser.suite()); suite.addTest(TestDateUtils.suite()); suite.addTest(TestEntityUtils.suite()); return suite; From httpclient-commits-returnbrittneyanderson@example.org Sat Dec 03 00:25:59 2005 Return-Path: <httpclient-commits-returnbrittneyanderson@example.org> Delivered-To: brittneyanderson@example.org Received: (qmail 50056 invoked from network); 3 Dec 2005 00:25:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (83.161.120.155) by minotaur.apache.org with SMTP; 3 Dec 2005 00:25:45 -0000 Received: (qmail 91697 invoked by uid 500); 3 Dec 2005 00:24:40 -0000 Mailing-List: contact brittneyanderson@example.org; run by ezmlm Precedence: bulk List-Help: <mailto:brittneyanderson@example.org> List-Unsubscribe: <mailto:brittneyanderson@example.org> List-Post: <mailto:brittneyanderson@example.org> List-Id: <httpclient-commits.jakarta.apache.org> Reply-To: brittneyanderson@example.org Delivered-To: mailing list brittneyanderson@example.org Received: (qmail 91445 invoked by uid 99); 3 Dec 2005 00:24:36 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (83.161.120.155) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 02 Dec 2005 16:24:36 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [83.161.120.155] (HELO minotaur.apache.org) (83.161.120.155) by apache.org (qpsmtpd/0.29) with SMTP; Fri, 02 Dec 2005 16:25:59 -0800 Received: (qmail 42058 invoked by uid 65534); 3 Dec 2005 00:23:57 -0000 Message-ID: <brittneyanderson@example.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r351858 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/ java/org/apache/http/util/ test/org/apache/http/ test/org/apache/http/util/ Date: Sat, 03 Dec 2005 00:23:50 -0000 To: brittneyanderson@example.org From: brittneyanderson@example.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: olegk Date: Fri Dec 2 16:23:34 2005 New Revision: 351858 URL: http://svn.apache.org/viewcvs?rev=351858&view=rev Log: * Fixed HTTP spec compliance bug in NameValueValue and HeaderElement parsing code * Folded ParameterFormatter code into NameValuePair class * Added format methods to NameValueValue and HeaderElement Removed: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/ParameterFormatter.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestParameterFormatter.java Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/NameValuePair.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestNameValuePair.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java?rev=351858&r1=351857&r2=351858&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java Fri Dec 2 16:23:34 2005 @@ -180,14 +180,14 @@ int cur = indexFrom; int from = indexFrom; boolean qouted = false; - char previous = 0; + boolean escaped = false; while (cur < indexTo) { char ch = buffer.charAt(cur); - if (ch == '"' && previous != '\\') { + if (ch == '"' && !escaped) { qouted = !qouted; } HeaderElement element = null; - if ((!qouted) && (ch == ',' && previous != '\\')) { + if ((!qouted) && (ch == ',')) { element = parse(buffer, from, cur); from = cur + 1; } else if (cur == indexTo - 1) { @@ -196,7 +196,11 @@ if (element != null && !element.getName().equals("")) { elements.add(element); } - previous = ch; + if (escaped) { + escaped = false; + } else { + escaped = qouted && ch == '\\'; + } cur++; } return (HeaderElement[]) @@ -262,6 +266,56 @@ return parse(buffer, 0, buffer.length()); } + public static void format( + final CharArrayBuffer buffer, + final HeaderElement element) { + if (buffer == null) { + throw new IllegalArgumentException("String buffer may not be null"); + } + if (element == null) { + throw new IllegalArgumentException("Header element may not be null"); + } + buffer.append(element.getName()); + if (element.getValue() != null) { + buffer.append("="); + buffer.append(element.getValue()); + } + NameValuePair[] params = element.getParameters(); + for (int i = 0; i < params.length; i++) { + buffer.append("; "); + NameValuePair.format(buffer, params[i], false); + } + } + + public static String format(final HeaderElement element) { + CharArrayBuffer buffer = new CharArrayBuffer(32); + format(buffer, element); + return buffer.toString(); + } + + public static void formatAll( + final CharArrayBuffer buffer, + final HeaderElement[] elements) { + if (buffer == null) { + throw new IllegalArgumentException("String buffer may not be null"); + } + if (elements == null) { + throw new IllegalArgumentException("Array of header element may not be null"); + } + for (int i = 0; i < elements.length; i++) { + if (i > 0) { + buffer.append(", "); + } + format(buffer, elements[i]); + } + } + + public static String formatAll(final HeaderElement[] elements) { + CharArrayBuffer buffer = new CharArrayBuffer(64); + formatAll(buffer, elements); + return buffer.toString(); + } + /** * Returns parameter with the given name, if found. Otherwise null * is returned @@ -311,7 +365,7 @@ CharArrayBuffer buffer = new CharArrayBuffer(64); buffer.append(this.name); if (this.value != null) { - buffer.append(" = "); + buffer.append("="); buffer.append(this.value); } for (int i = 0; i < this.parameters.length; i++) { Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/NameValuePair.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/NameValuePair.java?rev=351858&r1=351857&r2=351858&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/NameValuePair.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/NameValuePair.java Fri Dec 2 16:23:34 2005 @@ -36,15 +36,72 @@ import org.apache.http.util.LangUtils; /** - * <p>A simple class encapsulating a name/value pair.</p> + * <p>A simple class encapsulating an attribute/value pair</p> + * <p> + * This class comforms to the generic grammar and formatting rules outlined in the + * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2">Section 2.2</a> + * and + * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6">Section 3.6</a> + * of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.txt">RFC 2616</a> + * </p> + * <h>2.2 Basic Rules</h> + * <p> + * The following rules are used throughout this specification to describe basic parsing constructs. + * The US-ASCII coded character set is defined by ANSI X3.4-1986. + * </p> + * <pre> + * OCTET = <any 8-bit sequence of data> + * CHAR = <any US-ASCII character (octets 0 - 127)> + * UPALPHA = <any US-ASCII uppercase letter "A".."Z"> + * LOALPHA = <any US-ASCII lowercase letter "a".."z"> + * ALPHA = UPALPHA | LOALPHA + * DIGIT = <any US-ASCII digit "0".."9"> + * CTL = <any US-ASCII control character + * (octets 0 - 31) and DEL (127)> + * CR = <US-ASCII CR, carriage return (13)> + * LF = <US-ASCII LF, linefeed (10)> + * SP = <US-ASCII SP, space (32)> + * HT = <US-ASCII HT, horizontal-tab (9)> + * <"> = <US-ASCII double-quote mark (34)> + * </pre> + * <p> + * Many HTTP/1.1 header field values consist of words separated by LWS or special + * characters. These special characters MUST be in a quoted string to be used within + * a parameter value (as defined in section 3.6). + * <p> + * <pre> + * token = 1*<any CHAR except CTLs or separators> + * separators = "(" | ")" | "<" | ">" | "@" + * | "," | ";" | ":" | "\" | <"> + * | "/" | "[" | "]" | "?" | "=" + * | "{" | "}" | SP | HT + * </pre> + * <p> + * A string of text is parsed as a single word if it is quoted using double-quote marks. + * </p> + * <pre> + * quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) + * qdtext = <any TEXT except <">> + * </pre> + * <p> + * The backslash character ("\") MAY be used as a single-character quoting mechanism only + * within quoted-string and comment constructs. + * </p> + * <pre> + * quoted-pair = "\" CHAR + * </pre> + * <h>3.6 Transfer Codings</h> + * <p> + * Parameters are in the form of attribute/value pairs. + * </p> + * <pre> + * parameter = attribute "=" value + * attribute = token + * value = token | quoted-string + * </pre> * - * @author <a href="mailto:brittneyanderson@example.org">B.C. Holmes</a> - * @author Sean C. Sullivan - * @author <a href="mailto:brittneyanderson@example.org">Mike Bowler</a> * @author <a href="mailto:brittneyanderson@example.org">Oleg Kalnichevski</a> * - * @version $Revision$ $Date$ - * */ public class NameValuePair { @@ -102,14 +159,14 @@ int cur = indexFrom; int from = indexFrom; boolean qouted = false; - char previous = 0; + boolean escaped = false; while (cur < indexTo) { char ch = buffer.charAt(cur); - if (ch == '"' && previous != '\\') { + if (ch == '"' && !escaped) { qouted = !qouted; } NameValuePair param = null; - if (!qouted && ch == ';' && previous != '\\') { + if (!qouted && ch == ';') { param = parse(buffer, from, cur); from = cur + 1; } else if (cur == indexTo - 1) { @@ -118,7 +175,11 @@ if (param != null && !(param.getName().equals("") && param.getValue() == null)) { params.add(param); } - previous = ch; + if (escaped) { + escaped = false; + } else { + escaped = qouted && ch == '\\'; + } cur++; } return (NameValuePair[]) params.toArray(new NameValuePair[params.size()]); @@ -183,15 +244,168 @@ } /** + * Special characters that can be used as separators in HTTP parameters. + * These special characters MUST be in a quoted string to be used within + * a parameter value + */ + private static final char[] SEPARATORS = { + '(', ')', '<', '>', '@', + ',', ';', ':', '\\', '"', + '/', '[', ']', '?', '=', + '{', '}', ' ', '\t' + }; + + /** + * Unsafe special characters that must be escaped using the backslash + * character + */ + private static final char[] UNSAFE_CHARS = { + '"', '\\' + }; + + private static boolean isOneOf(char[] chars, char ch) { + for (int i = 0; i < chars.length; i++) { + if (ch == chars[i]) { + return true; + } + } + return false; + } + + private static boolean isUnsafeChar(char ch) { + return isOneOf(UNSAFE_CHARS, ch); + } + + private static boolean isSeparator(char ch) { + return isOneOf(SEPARATORS, ch); + } + + private static void format( + final CharArrayBuffer buffer, + final String value, + boolean alwaysUseQuotes) { + boolean unsafe = false; + if (alwaysUseQuotes) { + unsafe = true; + } else { + for (int i = 0; i < value.length(); i++) { + if (isSeparator(value.charAt(i))) { + unsafe = true; + break; + } + } + } + if (unsafe) buffer.append('"'); + for (int i = 0; i < value.length(); i++) { + char ch = value.charAt(i); + if (isUnsafeChar(ch)) { + buffer.append('\\'); + } + buffer.append(ch); + } + if (unsafe) buffer.append('"'); + } + + /** + * Produces textual representaion of the attribute/value pair using + * formatting rules defined in RFC 2616 + * + * @param buffer output buffer + * @param param the parameter to be formatted + * @param alwaysUseQuotes <tt>true</tt> if the parameter values must + * always be enclosed in quotation marks, <tt>false</tt> otherwise + */ + public static void format( + final CharArrayBuffer buffer, + final NameValuePair param, + boolean alwaysUseQuotes) { + if (buffer == null) { + throw new IllegalArgumentException("String buffer may not be null"); + } + if (param == null) { + throw new IllegalArgumentException("Parameter may not be null"); + } + buffer.append(param.getName()); + String value = param.getValue(); + if (value != null) { + buffer.append("="); + format(buffer, value, alwaysUseQuotes); + } + } + + /** + * Produces textual representaion of the attribute/value pairs using + * formatting rules defined in RFC 2616 + * + * @param buffer output buffer + * @param params the parameters to be formatted + * @param alwaysUseQuotes <tt>true</tt> if the parameter values must + * always be enclosed in quotation marks, <tt>false</tt> otherwise + */ + public static void formatAll( + final CharArrayBuffer buffer, + final NameValuePair[] params, + boolean alwaysUseQuotes) { + if (buffer == null) { + throw new IllegalArgumentException("String buffer may not be null"); + } + if (params == null) { + throw new IllegalArgumentException("Array of parameter may not be null"); + } + for (int i = 0; i < params.length; i++) { + if (i > 0) { + buffer.append("; "); + } + format(buffer, params[i], alwaysUseQuotes); + } + } + + /** + * Produces textual representaion of the attribute/value pair using + * formatting rules defined in RFC 2616 + * + * @param param the parameter to be formatted + * @param alwaysUseQuotes <tt>true</tt> if the parameter values must + * always be enclosed in quotation marks, <tt>false</tt> otherwise + * + * @return RFC 2616 conformant textual representaion of the + * attribute/value pair + */ + public static String format(final NameValuePair param, boolean alwaysUseQuotes) { + CharArrayBuffer buffer = new CharArrayBuffer(16); + format(buffer, param, alwaysUseQuotes); + return buffer.toString(); + } + + /** + * Produces textual representaion of the attribute/value pair using + * formatting rules defined in RFC 2616 + * + * @params param the parameters to be formatted + * @param alwaysUseQuotes <tt>true</tt> if the parameter values must + * always be enclosed in quotation marks, <tt>false</tt> otherwise + * + * @return RFC 2616 conformant textual representaion of the + * attribute/value pair + */ + public static String formatAll(final NameValuePair[] params, boolean alwaysUseQuotes) { + CharArrayBuffer buffer = new CharArrayBuffer(16); + formatAll(buffer, params, alwaysUseQuotes); + return buffer.toString(); + } + + /** * Get a string representation of this pair. * * @return A string representation. */ public String toString() { - CharArrayBuffer buffer = new CharArrayBuffer(32); + CharArrayBuffer buffer = new CharArrayBuffer(16); buffer.append(this.name); - buffer.append(" = "); - buffer.append(this.value); + if (this.value != null) { + buffer.append("="); + format(buffer, this.value, false); + } return buffer.toString(); } Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java?rev=351858&r1=351857&r2=351858&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeaderElement.java Fri Dec 2 16:23:34 2005 @@ -135,12 +135,15 @@ public void testParseEscaped() { String s = - "test1 = stuff\\, stuff, test2 = \"\\\"stuff\\\"\""; + "test1 = \"\\\"stuff\\\"\", test2= \"\\\\\", test3 = \"stuff, stuff\""; HeaderElement[] elements = HeaderElement.parseAll(s); + assertEquals(3, elements.length); assertEquals("test1", elements[0].getName()); - assertEquals("stuff\\, stuff", elements[0].getValue()); + assertEquals("\\\"stuff\\\"", elements[0].getValue()); assertEquals("test2", elements[1].getName()); - assertEquals("\\\"stuff\\\"", elements[1].getValue()); + assertEquals("\\\\", elements[1].getValue()); + assertEquals("test3", elements[2].getName()); + assertEquals("stuff, stuff", elements[2].getValue()); } public void testFringeCase1() throws Exception { @@ -294,12 +297,69 @@ } public void testToString() { - String s = "name = value; param1 = value1; param2 = value2"; + String s = "name=value; param1=value1; param2=value2"; HeaderElement element = HeaderElement.parse(s); assertEquals(s, element.toString()); - s = "name; param1 = value1; param2 = value2"; + s = "name; param1=value1; param2=value2"; element = HeaderElement.parse(s); assertEquals(s, element.toString()); + } + + public void testElementFormatting() throws Exception { + NameValuePair param1 = new NameValuePair("param", "regular_stuff"); + NameValuePair param2 = new NameValuePair("param", "this\\that"); + NameValuePair param3 = new NameValuePair("param", "this,that"); + NameValuePair param4 = new NameValuePair("param", null); + NameValuePair[] params = new NameValuePair[] {param1, param2, param3, param4}; + HeaderElement element = new HeaderElement("name", "value", params); + + assertEquals("name=value; param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"; param", + HeaderElement.format(element)); + } + + public void testElementArrayFormatting() throws Exception { + NameValuePair param1 = new NameValuePair("param", "regular_stuff"); + NameValuePair param2 = new NameValuePair("param", "this\\that"); + NameValuePair param3 = new NameValuePair("param", "this,that"); + NameValuePair param4 = new NameValuePair("param", null); + HeaderElement element1 = new HeaderElement("name1", "value1", new NameValuePair[] {param1}); + HeaderElement element2 = new HeaderElement("name2", "value2", new NameValuePair[] {param2}); + HeaderElement element3 = new HeaderElement("name3", "value3", new NameValuePair[] {param3}); + HeaderElement element4 = new HeaderElement("name4", "value4", new NameValuePair[] {param4}); + HeaderElement element5 = new HeaderElement("name5", null); + HeaderElement[] elements = new HeaderElement[] {element1, element2, element3, element4, element5}; + + assertEquals("name1=value1; param=regular_stuff, name2=value2; " + + "param=\"this\\\\that\", name3=value3; param=\"this,that\", " + + "name4=value4; param, name5", + HeaderElement.formatAll(elements)); + } + + public void testFormatInvalidInput() throws Exception { + try { + HeaderElement.format(null, new HeaderElement("name1", "value1")); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + HeaderElement.format(new CharArrayBuffer(10), (HeaderElement) null); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + HeaderElement.formatAll(null, new HeaderElement[] {new HeaderElement("name1", "value1")}); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + HeaderElement.formatAll(new CharArrayBuffer(10), (HeaderElement[]) null); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } } } Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestNameValuePair.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestNameValuePair.java?rev=351858&r1=351857&r2=351858&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestNameValuePair.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestNameValuePair.java Fri Dec 2 16:23:34 2005 @@ -92,7 +92,9 @@ public void testToString() { NameValuePair param1 = new NameValuePair("name1", "value1"); - assertEquals("name1 = value1", param1.toString()); + assertEquals("name1=value1", param1.toString()); + NameValuePair param2 = new NameValuePair("name1", null); + assertEquals("name1", param2.toString()); } public void testParse() { @@ -157,15 +159,18 @@ public void testParseEscaped() { String s = - "test1 = stuff\\; stuff; test2 = \"\\\"stuff\\\"\""; + "test1 = \"\\\"stuff\\\"\"; test2= \"\\\\\"; test3 = \"stuff; stuff\""; NameValuePair[] params = NameValuePair.parseAll(s); + assertEquals(3, params.length); assertEquals("test1", params[0].getName()); - assertEquals("stuff\\; stuff", params[0].getValue()); + assertEquals("\\\"stuff\\\"", params[0].getValue()); assertEquals("test2", params[1].getName()); - assertEquals("\\\"stuff\\\"", params[1].getValue()); + assertEquals("\\\\", params[1].getValue()); + assertEquals("test3", params[2].getName()); + assertEquals("stuff; stuff", params[2].getValue()); } - public void testInvalidInput() throws Exception { + public void testParseInvalidInput() throws Exception { CharArrayBuffer buffer = new CharArrayBuffer(32); buffer.append("name = value"); try { @@ -226,6 +231,70 @@ NameValuePair.parse(buffer, 2, 1); fail("IllegalArgumentException should have been thrown"); } catch (IndexOutOfBoundsException ex) { + // expected + } + } + + public void testBasicFormatting() throws Exception { + NameValuePair param1 = new NameValuePair("param", "regular_stuff"); + NameValuePair param2 = new NameValuePair("param", "this\\that"); + NameValuePair param3 = new NameValuePair("param", "this,that"); + NameValuePair param4 = new NameValuePair("param", "quote marks (\") must be escaped"); + NameValuePair param5 = new NameValuePair("param", "back slash (\\) must be escaped too"); + NameValuePair param6 = new NameValuePair("param", "values with\tblanks must always be quoted"); + NameValuePair param7 = new NameValuePair("param", null); + + assertEquals("param=regular_stuff", NameValuePair.format(param1, false)); + assertEquals("param=\"this\\\\that\"", NameValuePair.format(param2, false)); + assertEquals("param=\"this,that\"", NameValuePair.format(param3, false)); + assertEquals("param=\"quote marks (\\\") must be escaped\"", NameValuePair.format(param4, false)); + assertEquals("param=\"back slash (\\\\) must be escaped too\"", NameValuePair.format(param5, false)); + assertEquals("param=\"values with\tblanks must always be quoted\"", NameValuePair.format(param6, false)); + assertEquals("param", NameValuePair.format(param7, false)); + + assertEquals("param=\"regular_stuff\"", NameValuePair.format(param1, true)); + assertEquals("param=\"this\\\\that\"", NameValuePair.format(param2, true)); + assertEquals("param=\"this,that\"", NameValuePair.format(param3, true)); + assertEquals("param=\"quote marks (\\\") must be escaped\"", NameValuePair.format(param4, true)); + assertEquals("param=\"back slash (\\\\) must be escaped too\"", NameValuePair.format(param5, true)); + assertEquals("param=\"values with\tblanks must always be quoted\"", NameValuePair.format(param6, true)); + assertEquals("param", NameValuePair.format(param7, false)); + } + + public void testArrayFormatting() throws Exception { + NameValuePair param1 = new NameValuePair("param", "regular_stuff"); + NameValuePair param2 = new NameValuePair("param", "this\\that"); + NameValuePair param3 = new NameValuePair("param", "this,that"); + NameValuePair[] params = new NameValuePair[] {param1, param2, param3}; + assertEquals("param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"", + NameValuePair.formatAll(params, false)); + assertEquals("param=\"regular_stuff\"; param=\"this\\\\that\"; param=\"this,that\"", + NameValuePair.formatAll(params, true)); + } + + public void testFormatInvalidInput() throws Exception { + try { + NameValuePair.format(null, new NameValuePair("param", "value"), true); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + NameValuePair.format(new CharArrayBuffer(10), (NameValuePair) null, true); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + NameValuePair.formatAll(null, new NameValuePair[] {new NameValuePair("param", "value")}, true); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + NameValuePair.formatAll(new CharArrayBuffer(10), (NameValuePair[]) null, true); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { // expected } } Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java?rev=351858&r1=351857&r2=351858&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java Fri Dec 2 16:23:34 2005 @@ -41,7 +41,6 @@ suite.addTest(TestLangUtils.suite()); suite.addTest(TestExceptionUtils.suite()); suite.addTest(TestEncodingUtils.suite()); - suite.addTest(TestParameterFormatter.suite()); suite.addTest(TestDateUtils.suite()); suite.addTest(TestEntityUtils.suite()); return suite; From httpclient-commits-returnbrittneyanderson@example.org Sun Dec 04 14:00:02 2005 Return-Path: <httpclient-commits-returnbrittneyanderson@example.org> Delivered-To: brittneyanderson@example.org Received: (qmail 47429 invoked from network); 4 Dec 2005 14:00:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (83.161.120.155) by minotaur.apache.org with SMTP; 4 Dec 2005 14:00:02 -0000 Received: (qmail 49002 invoked by uid 500); 4 Dec 2005 14:00:01 -0000 Mailing-List: contact brittneyanderson@example.org; run by ezmlm Precedence: bulk List-Help: <mailto:brittneyanderson@example.org> List-Unsubscribe: <mailto:brittneyanderson@example.org> List-Post: <mailto:brittneyanderson@example.org> List-Id: <httpclient-commits.jakarta.apache.org> Reply-To: brittneyanderson@example.org Delivered-To: mailing list brittneyanderson@example.org Received: (qmail 48975 invoked by uid 99); 4 Dec 2005 14:00:01 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (83.161.120.155) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 04 Dec 2005 06:00:01 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [83.161.120.155] (HELO minotaur.apache.org) (83.161.120.155) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 04 Dec 2005 06:00:00 -0800 Received: (qmail 47285 invoked by uid 65534); 4 Dec 2005 13:59:40 -0000 Message-ID: <brittneyanderson@example.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r353848 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/ java/org/apache/http/io/ java/org/apache/http/util/ test/org/apache/http/util/ Date: Sun, 04 Dec 2005 13:59:38 -0000 To: brittneyanderson@example.org From: brittneyanderson@example.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: olegk Date: Sun Dec 4 05:59:25 2005 New Revision: 353848 URL: http://svn.apache.org/viewcvs?rev=353848&view=rev Log: Provided a utility class to parse unsigned integers from a char array buffer without creating intermediate objects (garbage) Added: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/NumUtils.java (with props) jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestNumUtils.java (with props) Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java?rev=353848&r1=353847&r2=353848&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java Sun Dec 4 05:59:25 2005 @@ -30,6 +30,7 @@ package org.apache.http; import org.apache.http.io.CharArrayBuffer; +import org.apache.http.util.NumUtils; /** * <p>HTTP version, as specified in RFC 2616.</p> @@ -255,13 +256,13 @@ buffer.substring(indexFrom, indexTo)); } try { - major = Integer.parseInt(buffer.substringTrimmed(i, period)); + major = NumUtils.parseUnsignedInt(buffer, i, period); } catch (NumberFormatException e) { throw new ProtocolException("Invalid HTTP major version number: " + buffer.substring(indexFrom, indexTo)); } try { - minor = Integer.parseInt(buffer.substringTrimmed(period + 1, indexTo)); + minor = NumUtils.parseUnsignedInt(buffer, period + 1, indexTo); } catch (NumberFormatException e) { throw new ProtocolException("Invalid HTTP minor version number: " + buffer.substring(indexFrom, indexTo)); Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java?rev=353848&r1=353847&r2=353848&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/io/ChunkedInputStream.java Sun Dec 4 05:59:25 2005 @@ -35,6 +35,7 @@ import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.util.ExceptionUtils; +import org.apache.http.util.NumUtils; /** * <p>This class implements chunked transfer coding as described in the @@ -274,9 +275,8 @@ if (separator < 0) { separator = this.buffer.length(); } - String s = this.buffer.substringTrimmed(0, separator); try { - return Integer.parseInt(s, 16); + return NumUtils.parseUnsignedHexInt(this.buffer, 0, separator); } catch (NumberFormatException e) { throw new MalformedChunkCodingException("Bad chunk header"); } Added: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/NumUtils.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/NumUtils.java?rev=353848&view=auto ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/NumUtils.java (added) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/NumUtils.java Sun Dec 4 05:59:25 2005 @@ -0,0 +1,83 @@ +package org.apache.http.util; + +import org.apache.http.io.CharArrayBuffer; + +public class NumUtils { + + private NumUtils() { + } + + public static int parseUnsignedInt( + final CharArrayBuffer buffer, final int indexFrom, final int indexTo, + int radix) + throws NumberFormatException { + if (buffer == null) { + throw new IllegalArgumentException("Char array buffer may not be null"); + } + if (indexFrom < 0) { + throw new IndexOutOfBoundsException(); + } + if (indexTo > buffer.length()) { + throw new IndexOutOfBoundsException(); + } + if (indexFrom > indexTo) { + throw new IndexOutOfBoundsException(); + } + if (radix < Character.MIN_RADIX) { + throw new IllegalArgumentException ("Radix may not be less than Character.MIN_RADIX"); + } + if (radix > Character.MAX_RADIX) { + throw new IllegalArgumentException ("Radix may not be greater than Character.MAX_RADIX"); + } + int i1 = indexFrom; + int i2 = indexTo; + while (i1 < indexTo && Character.isWhitespace(buffer.charAt(i1))) { + i1++; + } + while (i2 > i1 && Character.isWhitespace(buffer.charAt(i2 - 1))) { + i2--; + } + if (i1 == i2) { + throw new NumberFormatException("Empty input"); + } + int digit = Character.digit(buffer.charAt(i1++), radix); + if (digit < 0) { + throw new NumberFormatException("Invalid unsigned integer: " + + buffer.substring(indexFrom, indexTo)); + } + int num = digit; + while (i1 < i2) { + digit = Character.digit(buffer.charAt(i1++), radix); + if (digit < 0) { + throw new NumberFormatException("Invalid unsigned integer: " + + buffer.substring(indexFrom, indexTo)); + } + num *= radix; + num += digit; + if (num < 0) { + throw new NumberFormatException("Unsigned integer too large: " + + buffer.substring(indexFrom, indexTo)); + } + } + return num; + } + + public static int parseUnsignedInt( + final CharArrayBuffer buffer, final int indexFrom, final int indexTo) + throws NumberFormatException { + return parseUnsignedInt(buffer, indexFrom, indexTo, 10); + } + + public static int parseUnsignedHexInt( + final CharArrayBuffer buffer, final int indexFrom, final int indexTo) + throws NumberFormatException { + return parseUnsignedInt(buffer, indexFrom, indexTo, 16); + } + + public static int parseUnsignedBinInt( + final CharArrayBuffer buffer, final int indexFrom, final int indexTo) + throws NumberFormatException { + return parseUnsignedInt(buffer, indexFrom, indexTo, 2); + } + +} Propchange: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/NumUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/NumUtils.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/NumUtils.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java?rev=353848&r1=353847&r2=353848&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestAllUtil.java Sun Dec 4 05:59:25 2005 @@ -43,6 +43,7 @@ suite.addTest(TestEncodingUtils.suite()); suite.addTest(TestDateUtils.suite()); suite.addTest(TestEntityUtils.suite()); + suite.addTest(TestNumUtils.suite()); return suite; } Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestNumUtils.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestNumUtils.java?rev=353848&view=auto ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestNumUtils.java (added) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestNumUtils.java Sun Dec 4 05:59:25 2005 @@ -0,0 +1,218 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * ==================================================================== + * + * Copyright 1999-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.http.util; + +import org.apache.http.io.CharArrayBuffer; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit tests for {@link ParameterFormatter}. + * + * @author <a href="mailto:brittneyanderson@example.org">Oleg Kalnichevski</a> + */ +public class TestNumUtils extends TestCase { + + // ------------------------------------------------------------ Constructor + public TestNumUtils(String testName) { + super(testName); + } + + // ------------------------------------------------------------------- Main + public static void main(String args[]) { + String[] testCaseName = { TestNumUtils.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + // ------------------------------------------------------- TestCase Methods + + public static Test suite() { + return new TestSuite(TestNumUtils.class); + } + + public void testBasicParseUnsignedInt() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(16); + buffer.append(" 100 "); + int num = NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + assertEquals(100, num); + + buffer.clear(); + buffer.append("132"); + num = NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + assertEquals(132, num); + + buffer.clear(); + buffer.append("010101 "); + num = NumUtils.parseUnsignedBinInt(buffer, 0, buffer.length()); + assertEquals(21, num); + + buffer.clear(); + buffer.append("FFFF"); + num = NumUtils.parseUnsignedHexInt(buffer, 0, buffer.length()); + assertEquals(65535, num); + + buffer.clear(); + buffer.append("aAaA"); + num = NumUtils.parseUnsignedHexInt(buffer, 0, buffer.length()); + assertEquals(43690, num); + + buffer.clear(); + buffer.append("0"); + num = NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + assertEquals(0, num); + } + + public void testInvalidInputParseUnsignedInt() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(16); + buffer.append(" "); + try { + NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + fail("NumberFormatException should have been thrown"); + } catch (NumberFormatException ex) { + // expected + } + buffer.clear(); + buffer.append(""); + try { + NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + fail("NumberFormatException should have been thrown"); + } catch (NumberFormatException ex) { + // expected + } + buffer.clear(); + buffer.append("1000"); + try { + NumUtils.parseUnsignedInt(null, 0, 0); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + NumUtils.parseUnsignedInt(buffer, -1, 0); + fail("IllegalArgumentException should have been thrown"); + } catch (IndexOutOfBoundsException ex) { + // expected + } + try { + NumUtils.parseUnsignedInt(buffer, 0, 1000); + fail("IllegalArgumentException should have been thrown"); + } catch (IndexOutOfBoundsException ex) { + // expected + } + try { + NumUtils.parseUnsignedInt(buffer, 2, 1); + fail("IllegalArgumentException should have been thrown"); + } catch (IndexOutOfBoundsException ex) { + // expected + } + } + + public void testInvalidRadixParseUnsignedInt() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(16); + buffer.append("10000"); + try { + NumUtils.parseUnsignedInt(buffer, 0, buffer.length(), 1); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + NumUtils.parseUnsignedInt(buffer, 0, buffer.length(), + Character.MAX_RADIX + 1); + fail("IllegalArgumentException should have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + + public void testInvalidNumberParseUnsignedInt() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(16); + buffer.append("aa"); + try { + NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + fail("NumberFormatException should have been thrown"); + } catch (NumberFormatException ex) { + // expected + } + buffer.clear(); + buffer.append("3"); + try { + NumUtils.parseUnsignedBinInt(buffer, 0, buffer.length()); + fail("NumberFormatException should have been thrown"); + } catch (NumberFormatException ex) { + // expected + } + buffer.clear(); + buffer.append("-3"); + try { + NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + fail("NumberFormatException should have been thrown"); + } catch (NumberFormatException ex) { + // expected + } + buffer.clear(); + buffer.append("3 3"); + try { + NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + fail("NumberFormatException should have been thrown"); + } catch (NumberFormatException ex) { + // expected + } + } + + public void testParseUnsignedIntLargeNumbers() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(16); + buffer.append(Integer.toString(Integer.MAX_VALUE)); + int num = NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + assertEquals(Integer.MAX_VALUE, num); + + buffer.clear(); + buffer.append(Integer.toBinaryString(Integer.MAX_VALUE)); + num = NumUtils.parseUnsignedBinInt(buffer, 0, buffer.length()); + assertEquals(Integer.MAX_VALUE, num); + + buffer.clear(); + buffer.append(Integer.toHexString(Integer.MAX_VALUE)); + num = NumUtils.parseUnsignedHexInt(buffer, 0, buffer.length()); + assertEquals(Integer.MAX_VALUE, num); + } + + public void testParseUnsignedIntLargeNumberOverflow() throws Exception { + CharArrayBuffer buffer = new CharArrayBuffer(16); + buffer.append(Long.toString((long)Integer.MAX_VALUE + 1)); + try { + NumUtils.parseUnsignedInt(buffer, 0, buffer.length()); + fail("NumberFormatException should have been thrown"); + } catch (NumberFormatException ex) { + // expected + } + } +} Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestNumUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestNumUtils.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestNumUtils.java ------------------------------------------------------------------------------ svn:mime-type = text/plain From httpclient-commits-returnbrittneyanderson@example.org Wed Dec 07 19:11:22 2005 Return-Path: <httpclient-commits-returnbrittneyanderson@example.org> Delivered-To: brittneyanderson@example.org Received: (qmail 11560 invoked from network); 7 Dec 2005 19:11:22 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (83.161.120.155) by minotaur.apache.org with SMTP; 7 Dec 2005 19:11:22 -0000 Received: (qmail 55030 invoked by uid 500); 7 Dec 2005 19:11:21 -0000 Mailing-List: contact brittneyanderson@example.org; run by ezmlm Precedence: bulk List-Help: <mailto:brittneyanderson@example.org> List-Unsubscribe: <mailto:brittneyanderson@example.org> List-Post: <mailto:brittneyanderson@example.org> List-Id: <httpclient-commits.jakarta.apache.org> Reply-To: brittneyanderson@example.org Delivered-To: mailing list brittneyanderson@example.org Received: (qmail 55019 invoked by uid 99); 7 Dec 2005 19:11:21 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (83.161.120.155) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 07 Dec 2005 11:11:21 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [83.161.120.155] (HELO minotaur.apache.org) (83.161.120.155) by apache.org (qpsmtpd/0.29) with SMTP; Wed, 07 Dec 2005 11:11:20 -0800 Received: (qmail 11056 invoked by uid 65534); 7 Dec 2005 19:11:00 -0000 Message-ID: <brittneyanderson@example.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r354831 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/ test/org/apache/http/ Date: Wed, 07 Dec 2005 19:10:58 -0000 To: brittneyanderson@example.org From: brittneyanderson@example.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: olegk Date: Wed Dec 7 11:10:47 2005 New Revision: 354831 URL: http://svn.apache.org/viewcvs?rev=354831&view=rev Log: Added static #format methods to HTTP version, request line and status line classes Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestStatusLine.java Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java?rev=354831&r1=354830&r2=354831&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java Wed Dec 7 11:10:47 2005 @@ -284,5 +284,24 @@ buffer.append(s); return parse(buffer, 0, buffer.length()); } - + + public static void format(final CharArrayBuffer buffer, final HttpVersion ver) { + if (buffer == null) { + throw new IllegalArgumentException("String buffer may not be null"); + } + if (ver == null) { + throw new IllegalArgumentException("Version may not be null"); + } + buffer.append("HTTP/"); + buffer.append(Integer.toString(ver.getMajor())); + buffer.append('.'); + buffer.append(Integer.toString(ver.getMinor())); + } + + public static String format(final HttpVersion ver) { + CharArrayBuffer buffer = new CharArrayBuffer(16); + format(buffer, ver); + return buffer.toString(); + } + } Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java?rev=354831&r1=354830&r2=354831&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/RequestLine.java Wed Dec 7 11:10:47 2005 @@ -138,4 +138,24 @@ return parse(buffer, 0, buffer.length()); } + public static void format(final CharArrayBuffer buffer, final RequestLine requestline) { + if (buffer == null) { + throw new IllegalArgumentException("String buffer may not be null"); + } + if (requestline == null) { + throw new IllegalArgumentException("Request line may not be null"); + } + buffer.append(requestline.getMethod()); + buffer.append(' '); + buffer.append(requestline.getUri()); + buffer.append(' '); + HttpVersion.format(buffer, requestline.getHttpVersion()); + } + + public static String format(final RequestLine requestline) { + CharArrayBuffer buffer = new CharArrayBuffer(64); + format(buffer, requestline); + return buffer.toString(); + } + } Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java?rev=354831&r1=354830&r2=354831&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/StatusLine.java Wed Dec 7 11:10:47 2005 @@ -207,4 +207,26 @@ return buffer.toString(); } + public static void format(final CharArrayBuffer buffer, final StatusLine statusline) { + if (buffer == null) { + throw new IllegalArgumentException("String buffer may not be null"); + } + if (statusline == null) { + throw new IllegalArgumentException("Status line may not be null"); + } + HttpVersion.format(buffer, statusline.getHttpVersion()); + buffer.append(' '); + buffer.append(Integer.toString(statusline.getStatusCode())); + if (statusline.getReasonPhrase() != null) { + buffer.append(' '); + buffer.append(statusline.getReasonPhrase()); + } + } + + public static String format(final StatusLine statusline) { + CharArrayBuffer buffer = new CharArrayBuffer(32); + format(buffer, statusline); + return buffer.toString(); + } + } Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java?rev=354831&r1=354830&r2=354831&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHttpVersion.java Wed Dec 7 11:10:47 2005 @@ -154,7 +154,7 @@ } } - public void testInvalidInput() throws Exception { + public void testHttpVersionParsingInvalidInput() throws Exception { CharArrayBuffer buffer = new CharArrayBuffer(32); buffer.append("HTTP/1.1"); try { @@ -222,5 +222,26 @@ assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_0_9) > 0); assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_0) == 0); } + + public void testHttpVersionFormatting() throws Exception { + String s = HttpVersion.format(HttpVersion.HTTP_1_1); + assertEquals("HTTP/1.1", s); + } + + public void testHttpVersionFormattingInvalidInput() throws Exception { + try { + HttpVersion.format(null, HttpVersion.HTTP_1_1); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + HttpVersion.format(new CharArrayBuffer(10), (HttpVersion) null); + fail("IllegalArgumentException should habe been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + } Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java?rev=354831&r1=354830&r2=354831&view=diff ============================================================================== --- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java (original) +++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestRequestLine.java Wed Dec 7 11:10:47 2005 @@ -80,7 +80,7 @@ } catch (IllegalArgumentException e) { /* expected */ } } - public void testSuccess() throws Exception { + public void testParseSuccess() throws Exception { //typical request line RequestLine requestline = RequestLine.parse("GET /stuff HTTP/1.1"); assertEquals("GET /stuff HTTP/1.1", requestline.toString()); @@ -102,7 +102,7 @@ assertEquals(HttpVersion.HTTP_1_1, requestline.getHttpVersion()); } - public void testFailure() throws Exception { + public void testParseFailure() throws Exception { try { RequestLine.parse(" "); fail(); @@ -124,7 +124,7 @@ } catch (HttpException e) { /* expected */ } } - public void testInvalidInput() throws Exception { + public void testParseInvalidInput() throws Exception { CharArrayBuffer buffer = new CharArrayBuffer(32); buffer.append("GET /stuff HTTP/1.1"); try { @@ -159,4 +159,25 @@ } } + public void testFormatting() throws Exception { + RequestLine requestline = new RequestLine("GET", "/stuff", HttpVersion.HTTP_1_1); + String s = RequestLine.format(requestline); + assertEquals("GET /stuff HTTP/1.1", s); + } + + public void testFormattingInvalidInput() throws Exception { + try { + RequestLine.format(null, new RequestLine("GET", "/stuff", HttpVersion.HTTP_1_1)); + fail("IllegalArgumentException should habe been thrown"); + } catch
{'source': 'Apache Software Foundation Public Mail Archives'}