hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r349138 - 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/ test/org/apache/http/io/ test/org/apache/http/util/
Date Sat, 26 Nov 2005 19:41:57 GMT
Author: olegk
Date: Sat Nov 26 11:41:38 2005
New Revision: 349138

URL: http://svn.apache.org/viewcvs?rev=349138&view=rev
Log:
* Header, HeaderElement and NameValurPair parsing code completely reworked once more. The
new parse / parseAll static method should produce virtually no intermediate garbage
* ParameterParser code moved to NameValurPair class
* ParameterParser deleted

Removed:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/ParameterParser.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/util/TestParameterParser.java
Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HeaderElement.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/HttpVersion.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/NameValuePair.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/java/org/apache/http/io/CharArrayBuffer.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.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/io/TestCharArrayBuffer.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=349138&r1=349137&r2=349138&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 Sat Nov 26
11:41:38 2005
@@ -176,7 +176,7 @@
      * @since 3.0
      */
     public HeaderElement[] getElements() {
-        return HeaderElement.parseElements(this.buffer, this.posValue, this.buffer.length());
+        return HeaderElement.parseAll(this.buffer, this.posValue, this.buffer.length());
     }
     
 }

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=349138&r1=349137&r2=349138&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 Sat
Nov 26 11:41:38 2005
@@ -34,7 +34,6 @@
 
 import org.apache.http.io.CharArrayBuffer;
 import org.apache.http.util.LangUtils;
-import org.apache.http.util.ParameterParser;
 
 /**
  * <p>One element of an HTTP header's value.</p>
@@ -78,7 +77,7 @@
  * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
  * @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
- * @author <a href="mailto:oleg@ural.com">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.com">Oleg Kalnichevski</a>
  * 
  * @since 1.0
  * @version $Revision$ $Date$
@@ -163,7 +162,7 @@
      * 
      * @since 3.0
      */
-    public static final HeaderElement[] parseElements(
+    public static final HeaderElement[] parseAll(
             final CharArrayBuffer buffer, final int indexFrom, final int indexTo) {
         if (buffer == null) {
             throw new IllegalArgumentException("Char array buffer may not be null");
@@ -181,13 +180,14 @@
         int cur = indexFrom;
         int from = indexFrom;
         boolean qouted = false;
+        char previous = 0;
         while (cur < indexTo) {
             char ch = buffer.charAt(cur);
-            if (ch == '"') {
+            if (ch == '"' && previous != '\\') {
                 qouted = !qouted;
             }
             HeaderElement element = null;
-            if ((!qouted) && (ch == ',')) {
+            if ((!qouted) && (ch == ',' && previous != '\\')) {
                 element = parse(buffer, from, cur);
                 from = cur + 1;
             } else if (cur == indexTo - 1) {
@@ -196,6 +196,7 @@
             if (element != null && !element.getName().equals("")) {
                 elements.add(element);
             }
+            previous = ch;
             cur++;
         }
         return (HeaderElement[])
@@ -212,13 +213,13 @@
      * 
      * @since 3.0
      */
-    public static final HeaderElement[] parseElements(final String s) {
+    public static final HeaderElement[] parseAll(final String s) {
         if (s == null) {
             throw new IllegalArgumentException("String may not be null");
         }
         CharArrayBuffer buffer = new CharArrayBuffer(s.length()); 
         buffer.append(s);
-        return parseElements(buffer, 0, buffer.length());
+        return parseAll(buffer, 0, buffer.length());
     }
 
     public static HeaderElement parse(
@@ -235,15 +236,16 @@
         if (indexFrom > indexTo) {
             throw new IndexOutOfBoundsException();
         }
-        ParameterParser parser = new ParameterParser();
-        List paramlist = parser.parse(buffer.internBuffer(), indexFrom, indexTo, ';');
-        if (paramlist.size() > 0) {
-            NameValuePair element = (NameValuePair) paramlist.remove(0);
-            String name = element.getName();
-            String value = element.getValue();
+        NameValuePair[] nvps = NameValuePair.parseAll(buffer, indexFrom, indexTo);
+        if (nvps.length > 0) {
+            NameValuePair nvp = nvps[0];
+            String name = nvp.getName();
+            String value = nvp.getValue();
             NameValuePair[] params = null;
-            if (paramlist.size() > 0) {
-                params = (NameValuePair[]) paramlist.toArray(new NameValuePair[paramlist.size()]);
   
+            int len = nvps.length - 1; 
+            if (len > 0) {
+                params = new NameValuePair[len];
+                System.arraycopy(nvps, 1, params, 0, len);
             }
             return new HeaderElement(name, value, params);
         } else {

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=349138&r1=349137&r2=349138&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 Sat Nov
26 11:41:38 2005
@@ -249,7 +249,7 @@
                         buffer.substring(indexFrom, indexTo));
             }
             i += 5;
-            int period = buffer.indexOf('.', i);
+            int period = buffer.indexOf('.', i, indexTo);
             if (period == -1) {
                 throw new ProtocolException("Invalid HTTP version number: " + 
                         buffer.substring(indexFrom, indexTo));

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=349138&r1=349137&r2=349138&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 Sat
Nov 26 11:41:38 2005
@@ -29,7 +29,8 @@
 
 package org.apache.http;
 
-import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.http.io.CharArrayBuffer;
 import org.apache.http.util.LangUtils;
@@ -40,14 +41,13 @@
  * @author <a href="mailto:bcholmes@interlog.com">B.C. Holmes</a>
  * @author Sean C. Sullivan
  * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
+ * @author <a href="mailto:oleg at ural.com">Oleg Kalnichevski</a>
  * 
  * @version $Revision$ $Date$
  * 
  */
-public class NameValuePair implements Serializable {
+public class NameValuePair {
 
-	static final long serialVersionUID = -759179838086890081L;
-	 
     private final String name;
     private final String value;
 
@@ -82,6 +82,104 @@
      */
     public String getValue() {
         return this.value;
+    }
+
+    public static final NameValuePair[] parseAll(
+            final CharArrayBuffer buffer, final int indexFrom, final int indexTo) {
+        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();
+        }
+        List params = new ArrayList(); 
+        int cur = indexFrom;
+        int from = indexFrom;
+        boolean qouted = false;
+        char previous = 0;
+        while (cur < indexTo) {
+            char ch = buffer.charAt(cur);
+            if (ch == '"' && previous != '\\') {
+                qouted = !qouted;
+            }
+            NameValuePair param = null;
+            if (!qouted && ch == ';' && previous != '\\') {
+                param = parse(buffer, from, cur);
+                from = cur + 1;
+            } else if (cur == indexTo - 1) {
+                param = parse(buffer, from, indexTo);
+            }
+            if (param != null && !(param.getName().equals("") && param.getValue()
== null)) {
+                params.add(param);
+            }
+            previous = ch;
+            cur++;
+        }
+        return (NameValuePair[]) params.toArray(new NameValuePair[params.size()]);
+    }
+    
+    public static final NameValuePair[] parseAll(final String s) {
+        if (s == null) {
+            throw new IllegalArgumentException("String may not be null");
+        }
+        CharArrayBuffer buffer = new CharArrayBuffer(s.length());
+        buffer.append(s);
+        return parseAll(buffer, 0, buffer.length());
+    }
+
+    public static NameValuePair parse(
+            final CharArrayBuffer buffer, final int indexFrom, final int indexTo) {
+        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();
+        }
+        int eq = buffer.indexOf('=', indexFrom, indexTo);
+        if (eq < 0) {
+            return new NameValuePair(buffer.substringTrimmed(indexFrom, indexTo), null);
+        }
+        String name = buffer.substringTrimmed(indexFrom, eq);
+        int i1 = eq + 1;
+        int i2 = indexTo;
+        // Trim leading white spaces
+        while (i1 < i2 && (Character.isWhitespace(buffer.charAt(i1)))) {
+            i1++;
+        }
+        // Trim trailing white spaces
+        while ((i2 > i1) && (Character.isWhitespace(buffer.charAt(i2 - 1)))) {
+            i2--;
+        }
+        // Strip away quotes if necessary
+        if (((i2 - i1) >= 2) 
+            && (buffer.charAt(i1) == '"') 
+            && (buffer.charAt(i2 - 1) == '"')) {
+            i1++;
+            i2--;
+        }
+        String value = buffer.substring(i1, i2);
+        return new NameValuePair(name, value);
+    }
+
+    public static final NameValuePair parse(final String s) {
+        if (s == null) {
+            throw new IllegalArgumentException("String may not be null");
+        }
+        CharArrayBuffer buffer = new CharArrayBuffer(s.length());
+        buffer.append(s);
+        return parse(buffer, 0, buffer.length());
     }
 
     /**

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=349138&r1=349137&r2=349138&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 Sat Nov
26 11:41:38 2005
@@ -104,7 +104,7 @@
             while (Character.isWhitespace(buffer.charAt(i))) {
                 i++;
             }
-            int blank = buffer.indexOf(' ', i);
+            int blank = buffer.indexOf(' ', i, indexTo);
             if (blank < 0) {
                 throw new ProtocolException("Invalid request line: " + 
                         buffer.substring(indexFrom, indexTo));
@@ -114,7 +114,7 @@
             while (Character.isWhitespace(buffer.charAt(i))) {
                 i++;
             }
-            blank = buffer.indexOf(' ', i);
+            blank = buffer.indexOf(' ', i, indexTo);
             if (blank < 0) {
                 throw new ProtocolException("Invalid request line: " + 
                         buffer.substring(indexFrom, indexTo));

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=349138&r1=349137&r2=349138&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 Sat Nov
26 11:41:38 2005
@@ -120,7 +120,7 @@
             while (Character.isWhitespace(buffer.charAt(i))) {
                 i++;
             }            
-            int blank = buffer.indexOf(' ', i);
+            int blank = buffer.indexOf(' ', i, indexTo);
             if (blank <= 0) {
                 throw new ProtocolException(
                         "Unable to parse HTTP-Version from the status line: "
@@ -135,7 +135,7 @@
             }            
 
             //handle the Status-Code
-            blank = buffer.indexOf(' ', i);
+            blank = buffer.indexOf(' ', i, indexTo);
             if (blank < 0) {
                 blank = indexTo;
             }

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=349138&r1=349137&r2=349138&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
Sat Nov 26 11:41:38 2005
@@ -146,14 +146,17 @@
         return this.len == 0; 
     }
     
-    public int indexOf(int ch, int fromIndex) {
-        if (fromIndex < 0) {
-            fromIndex = 0;
+    public int indexOf(int ch, int beginIndex, int endIndex) {
+        if (beginIndex < 0) {
+            beginIndex = 0;
         }
-        if (fromIndex > this.len) {
+        if (endIndex > this.len) {
+            endIndex = this.len;
+        }
+        if (beginIndex > endIndex) {
             return -1;
         }
-        for (int i = fromIndex; i < this.len; i++) {
+        for (int i = beginIndex; i < endIndex; i++) {
             if (this.buffer[i] == ch) {
                 return i;
             }
@@ -162,7 +165,7 @@
     }
     
     public int indexOf(int ch) {
-        return indexOf(ch, 0);
+        return indexOf(ch, 0, this.len);
     }
 
     public String substring(int beginIndex, int endIndex) {

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.java?rev=349138&r1=349137&r2=349138&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/util/EntityUtils.java
Sat Nov 26 11:41:38 2005
@@ -84,7 +84,7 @@
         }
         String charset = null;
         if (entity.getContentType() != null) { 
-            HeaderElement values[] = HeaderElement.parseElements(entity.getContentType());
+            HeaderElement values[] = HeaderElement.parseAll(entity.getContentType());
             if (values.length > 0) {
                 NameValuePair param = values[0].getParameterByName("charset");
                 if (param != null) {

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=349138&r1=349137&r2=349138&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
Sat Nov 26 11:41:38 2005
@@ -101,10 +101,9 @@
     }
     
     public void testParseHeaderElements() throws Exception {
-        // this is derived from the old main method in HeaderElement
         String headerValue = "name1 = value1; name2; name3=\"value3\" , name4=value4; " +
             "name5=value5, name6= ; name7 = value7; name8 = \" value8\"";
-        HeaderElement[] elements = HeaderElement.parseElements(headerValue);
+        HeaderElement[] elements = HeaderElement.parseAll(headerValue);
         // there are 3 elements
         assertEquals(3,elements.length);
         // 1st element
@@ -134,21 +133,31 @@
         assertEquals(" value8",elements[2].getParameters()[1].getValue());
     }
 
+    public void testParseEscaped() {
+        String s = 
+          "test1 = stuff\\, stuff, test2 =  \"\\\"stuff\\\"\"";
+        HeaderElement[] elements = HeaderElement.parseAll(s);
+        assertEquals("test1", elements[0].getName());
+        assertEquals("stuff\\, stuff", elements[0].getValue());
+        assertEquals("test2", elements[1].getName());
+        assertEquals("\\\"stuff\\\"", elements[1].getValue());
+    }
+
     public void testFringeCase1() throws Exception {
         String headerValue = "name1 = value1,";
-        HeaderElement[] elements = HeaderElement.parseElements(headerValue);
+        HeaderElement[] elements = HeaderElement.parseAll(headerValue);
         assertEquals("Number of elements", 1, elements.length);
     }
 
     public void testFringeCase2() throws Exception {
         String headerValue = "name1 = value1, ";
-        HeaderElement[] elements = HeaderElement.parseElements(headerValue);
+        HeaderElement[] elements = HeaderElement.parseAll(headerValue);
         assertEquals("Number of elements", 1, elements.length);
     }
 
     public void testFringeCase3() throws Exception {
         String headerValue = ",, ,, ,";
-        HeaderElement[] elements = HeaderElement.parseElements(headerValue);
+        HeaderElement[] elements = HeaderElement.parseAll(headerValue);
         assertEquals("Number of elements", 0, elements.length);
     }
 
@@ -156,31 +165,31 @@
         CharArrayBuffer buffer = new CharArrayBuffer(32);
         buffer.append("name = value");
         try {
-            HeaderElement.parseElements(null, 0, 0);
+            HeaderElement.parseAll(null, 0, 0);
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException ex) {
             // expected
         }
         try {
-            HeaderElement.parseElements(null);
+            HeaderElement.parseAll(null);
             fail("IllegalArgumentException should have been thrown");
         } catch (IllegalArgumentException ex) {
             // expected
         }
         try {
-            HeaderElement.parseElements(buffer, -1, 0);
+            HeaderElement.parseAll(buffer, -1, 0);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException ex) {
             // expected
         }
         try {
-            HeaderElement.parseElements(buffer, 0, 1000);
+            HeaderElement.parseAll(buffer, 0, 1000);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException ex) {
             // expected
         }
         try {
-            HeaderElement.parseElements(buffer, 2, 1);
+            HeaderElement.parseAll(buffer, 2, 1);
             fail("IllegalArgumentException should have been thrown");
         } catch (IndexOutOfBoundsException 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=349138&r1=349137&r2=349138&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
Sat Nov 26 11:41:38 2005
@@ -29,6 +29,8 @@
 
 package org.apache.http;
 
+import org.apache.http.io.CharArrayBuffer;
+
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -92,4 +94,140 @@
         NameValuePair param1 = new NameValuePair("name1", "value1");
         assertEquals("name1 = value1", param1.toString());
     }
+    
+    public void testParse() {
+        String s = "test";
+        NameValuePair param = NameValuePair.parse(s);
+        assertEquals("test", param.getName());
+        assertEquals(null, param.getValue());
+
+        s = "test=stuff";
+        param = NameValuePair.parse(s);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        
+        s = "   test  =   stuff ";
+        param = NameValuePair.parse(s);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        
+        s = "test  = \"stuff\"";
+        param = NameValuePair.parse(s);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        
+        s = "test  = \"  stuff\\\"\"";
+        param = NameValuePair.parse(s);
+        assertEquals("test", param.getName());
+        assertEquals("  stuff\\\"", param.getValue());
+        
+        s = "  test";
+        param = NameValuePair.parse(s);
+        assertEquals("test", param.getName());
+        assertEquals(null, param.getValue());
+
+        s = "  ";
+        param = NameValuePair.parse(s);
+        assertEquals("", param.getName());
+        assertEquals(null, param.getValue());
+
+        s = " = stuff ";
+        param = NameValuePair.parse(s);
+        assertEquals("", param.getName());
+        assertEquals("stuff", param.getValue());
+    }
+
+    public void testParseAll() {
+        String s = 
+          "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
+        NameValuePair[] params = NameValuePair.parseAll(s);
+        assertEquals("test", params[0].getName());
+        assertEquals(null, params[0].getValue());
+        assertEquals("test1", params[1].getName());
+        assertEquals("stuff", params[1].getValue());
+        assertEquals("test2", params[2].getName());
+        assertEquals("stuff; stuff", params[2].getValue());
+        assertEquals("test3", params[3].getName());
+        assertEquals("\"stuff", params[3].getValue());
+
+        s = "  ";
+        params = NameValuePair.parseAll(s);
+        assertEquals(0, params.length);
+    }
+
+    public void testParseEscaped() {
+        String s = 
+          "test1 = stuff\\; stuff; test2 =  \"\\\"stuff\\\"\"";
+        NameValuePair[] params = NameValuePair.parseAll(s);
+        assertEquals("test1", params[0].getName());
+        assertEquals("stuff\\; stuff", params[0].getValue());
+        assertEquals("test2", params[1].getName());
+        assertEquals("\\\"stuff\\\"", params[1].getValue());
+    }
+
+    public void testInvalidInput() throws Exception {
+        CharArrayBuffer buffer = new CharArrayBuffer(32);
+        buffer.append("name = value");
+        try {
+            NameValuePair.parseAll(null, 0, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            NameValuePair.parseAll(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            NameValuePair.parseAll(buffer, -1, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            NameValuePair.parseAll(buffer, 0, 1000);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            NameValuePair.parseAll(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            NameValuePair.parse(null, 0, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            NameValuePair.parse(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+        try {
+            NameValuePair.parse(buffer, -1, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            NameValuePair.parse(buffer, 0, 1000);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+        try {
+            NameValuePair.parse(buffer, 2, 1);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IndexOutOfBoundsException ex) {
+            // expected
+        }
+    }
+    
 }

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=349138&r1=349137&r2=349138&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
Sat Nov 26 11:41:38 2005
@@ -214,8 +214,9 @@
         buffer.append("name: value");
         assertEquals(4, buffer.indexOf(':'));
         assertEquals(-1, buffer.indexOf(','));
-        assertEquals(4, buffer.indexOf(':', -1));
-        assertEquals(-1, buffer.indexOf(',', 1000));
+        assertEquals(4, buffer.indexOf(':', -1, 11));
+        assertEquals(4, buffer.indexOf(':', 0, 1000));
+        assertEquals(-1, buffer.indexOf(':', 2, 1));
     }
     
     public void testSubstring() {

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=349138&r1=349137&r2=349138&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
Sat Nov 26 11:41:38 2005
@@ -41,7 +41,6 @@
         suite.addTest(TestLangUtils.suite());
         suite.addTest(TestExceptionUtils.suite());
         suite.addTest(TestEncodingUtils.suite());
-        suite.addTest(TestParameterParser.suite());
         suite.addTest(TestParameterFormatter.suite());
         suite.addTest(TestHeadersParser.suite());
         suite.addTest(TestDateUtils.suite());



Mime
View raw message