hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r589325 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/message/ test/java/org/apache/http/message/
Date Sun, 28 Oct 2007 10:37:59 GMT
Author: olegk
Date: Sun Oct 28 03:37:56 2007
New Revision: 589325

URL: http://svn.apache.org/viewvc?rev=589325&view=rev
Log:
HTTPCORE-126: Improved HTTP message parsing

Improved HeaderElementParser

Contributed by Oleg Kalnichevski
Reviewed by Roland Weber

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/ParserCursor.java
      - copied unchanged from r589310, jakarta/httpcomponents/httpcore/branches/parser_refactoring/module-main/src/main/java/org/apache/http/message/ParserCursor.java
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BufferedHeader.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java?rev=589325&r1=589324&r2=589325&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElement.java
Sun Oct 28 03:37:56 2007
@@ -86,32 +86,13 @@
     private final String value;
     private final NameValuePair[] parameters;
 
-    public BasicHeaderElement(final NameValuePair[] nvps) {
-        super();
-        if (nvps.length > 0) {
-            NameValuePair nvp = nvps[0];
-            this.name = nvp.getName();
-            this.value = nvp.getValue();
-            int len = nvps.length - 1; 
-            if (len > 0) {
-                this.parameters = new NameValuePair[len];
-                System.arraycopy(nvps, 1, this.parameters, 0, len);
-            } else {
-                this.parameters = new NameValuePair[] {}; 
-            }
-        } else {
-            this.name = "";
-            this.value = null;
-            this.parameters = new NameValuePair[] {}; 
-        }
-    }
-    
     /**
      * Constructor with name, value and parameters.
      *
      * @param name header element name
      * @param value header element value. May be <tt>null</tt>
-     * @param parameters header element parameters. May be <tt>null</tt>
+     * @param parameters header element parameters. May be <tt>null</tt>.
+     *   Parameters are copied by reference, not by value
      */
     public BasicHeaderElement(
             final String name, 
@@ -124,7 +105,7 @@
         this.name = name;
         this.value = value;
         if (parameters != null) {
-            this.parameters = (NameValuePair[])parameters.clone();
+            this.parameters = parameters;
         } else {
             this.parameters = new NameValuePair[] {};
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java?rev=589325&r1=589324&r2=589325&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
Sun Oct 28 03:37:56 2007
@@ -71,7 +71,9 @@
     public final static
         BasicHeaderValueParser DEFAULT = new BasicHeaderValueParser();
 
-
+    private char PARAM_DELIMITER                = ';';
+    private char ELEM_DELIMITER                 = ',';
+    
     // public default constructor
 
 
@@ -98,56 +100,28 @@
 
         CharArrayBuffer buffer = new CharArrayBuffer(value.length());
         buffer.append(value);
-        return parser.parseElements(buffer, 0, buffer.length());
+        ParserCursor cursor = new ParserCursor(0, value.length());
+        return parser.parseElements(buffer, cursor);
     }
 
 
     // non-javadoc, see interface HeaderValueParser
     public HeaderElement[] parseElements(final CharArrayBuffer buffer,
-                                         final int indexFrom,
-                                         final int indexTo) {
+                                         final ParserCursor cursor) {
 
         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();
+            throw new IllegalArgumentException("Char array buffer may not be null");
         }
-        if (indexFrom > indexTo) {
-            throw new IndexOutOfBoundsException();
+        if (cursor == null) {
+            throw new IllegalArgumentException("Parser cursor may not be null");
         }
+
         List elements = new ArrayList(); 
-        int cur = indexFrom;
-        int from = indexFrom;
-        boolean qouted = false;
-        boolean escaped = false;
-        while (cur < indexTo) {
-            char ch = buffer.charAt(cur);
-            if (ch == '"' && !escaped) {
-                qouted = !qouted;
-            }
-            HeaderElement element = null;
-            if ((!qouted) && (ch == ',')) {
-                element = parseHeaderElement(buffer, from, cur);
-                from = cur + 1;
-            } else if (cur == indexTo - 1) {
-                element = parseHeaderElement(buffer, from, indexTo);
-            }
-            if (element != null && !(element.getName().length() == 0 &&
-                                     element.getValue() == null)
-                ) {
+        while (!cursor.atEnd()) {
+            HeaderElement element = parseHeaderElement(buffer, cursor);
+            if (!(element.getName().length() == 0 && element.getValue() == null))
{
                 elements.add(element);
             }
-            if (escaped) {
-                escaped = false;
-            } else {
-                escaped = qouted && ch == '\\';
-            }
-            cur++;
         }
         return (HeaderElement[])
             elements.toArray(new HeaderElement[elements.size()]);
@@ -177,45 +151,45 @@
 
         CharArrayBuffer buffer = new CharArrayBuffer(value.length());
         buffer.append(value);
-        return parser.parseHeaderElement(buffer, 0, buffer.length());
+        ParserCursor cursor = new ParserCursor(0, value.length());
+        return parser.parseHeaderElement(buffer, cursor);
     }
 
 
-
-
     // non-javadoc, see interface HeaderValueParser
     public HeaderElement parseHeaderElement(final CharArrayBuffer buffer,
-                                            final int indexFrom,
-                                            final int indexTo) {
+                                            final ParserCursor cursor) {
 
         if (buffer == null) {
-            throw new IllegalArgumentException
-                ("Char array buffer may not be null");
-        }
-        if (indexFrom < 0) {
-            throw new IndexOutOfBoundsException();
+            throw new IllegalArgumentException("Char array buffer may not be null");
         }
-        if (indexTo > buffer.length()) {
-            throw new IndexOutOfBoundsException();
+        if (cursor == null) {
+            throw new IllegalArgumentException("Parser cursor may not be null");
         }
-        if (indexFrom > indexTo) {
-            throw new IndexOutOfBoundsException();
+        
+        NameValuePair nvp = parseNameValuePair(buffer, cursor);
+        NameValuePair[] params = null;
+        if (!cursor.atEnd()) {
+            char ch = buffer.charAt(cursor.getPos() - 1); 
+            if (ch != ELEM_DELIMITER) {
+                params = parseParameters(buffer, cursor);
+            }
         }
-        NameValuePair[] nvps = parseParameters(buffer, indexFrom, indexTo);
-        return createHeaderElement(nvps);
+        return createHeaderElement(nvp.getName(), nvp.getValue(), params);
     }
 
 
     /**
-     * Craetes a header element.
+     * Creates a header element.
      * Called from {@link #parseHeaderElement}.
      *
-     * @param nvps      the name-value pairs
-     *
      * @return  a header element representing the argument
      */
-    protected HeaderElement createHeaderElement(NameValuePair[] nvps) {
-        return new BasicHeaderElement(nvps);
+    protected HeaderElement createHeaderElement(
+            final String name, 
+            final String value, 
+            final NameValuePair[] params) {
+        return new BasicHeaderElement(name, value, params);
     }
 
 
@@ -242,63 +216,53 @@
 
         CharArrayBuffer buffer = new CharArrayBuffer(value.length());
         buffer.append(value);
-        return parser.parseParameters(buffer, 0, buffer.length());
+        ParserCursor cursor = new ParserCursor(0, value.length());
+        return parser.parseParameters(buffer, cursor);
     }
 
 
 
     // non-javadoc, see interface HeaderValueParser
     public NameValuePair[] parseParameters(final CharArrayBuffer buffer,
-                                           final int indexFrom,
-                                           final int indexTo) {
+                                           final ParserCursor cursor) {
 
         if (buffer == null) {
-            throw new IllegalArgumentException
-                ("Char array buffer may not be null");
+            throw new IllegalArgumentException("Char array buffer may not be null");
         }
-        if (indexFrom < 0) {
-            throw new IndexOutOfBoundsException();
+        if (cursor == null) {
+            throw new IllegalArgumentException("Parser cursor may not be null");
         }
-        if (indexTo > buffer.length()) {
-            throw new IndexOutOfBoundsException();
+        
+        int pos = cursor.getPos();
+        int indexTo = cursor.getUpperBound();
+        
+        while (pos < indexTo) {
+            char ch = buffer.charAt(pos);
+            if (HTTP.isWhitespace(ch)) {
+                pos++;
+            } else {
+                break;
+            }
         }
-        if (indexFrom > indexTo) {
-            throw new IndexOutOfBoundsException();
+        cursor.updatePos(pos);
+        if (cursor.atEnd()) {
+            return new NameValuePair[] {};
         }
+        
         List params = new ArrayList(); 
-        int cur = indexFrom;
-        int from = indexFrom;
-        boolean qouted = false;
-        boolean escaped = false;
-        while (cur < indexTo) {
-            char ch = buffer.charAt(cur);
-            if (ch == '"' && !escaped) {
-                qouted = !qouted;
-            }
-            NameValuePair param = null;
-            if (!qouted && ch == ';') {
-                param = parseNameValuePair(buffer, from, cur);
-                from = cur + 1;
-            } else if (cur == indexTo - 1) {
-                param = parseNameValuePair(buffer, from, indexTo);
-            }
-            if (param != null && !(param.getName().length() == 0 &&
-                                   param.getValue() == null)
-                ) {
-                params.add(param);
+        while (!cursor.atEnd()) {
+            NameValuePair param = parseNameValuePair(buffer, cursor);
+            params.add(param);
+            char ch = buffer.charAt(cursor.getPos() - 1);
+            if (ch == ELEM_DELIMITER) {
+                break;
             }
-            if (escaped) {
-                escaped = false;
-            } else {
-                escaped = qouted && ch == '\\';
-            }
-            cur++;
         }
+        
         return (NameValuePair[])
             params.toArray(new NameValuePair[params.size()]);
     }
 
-
     /**
      * Parses a name-value-pair with the given parser.
      *
@@ -322,43 +286,80 @@
 
         CharArrayBuffer buffer = new CharArrayBuffer(value.length());
         buffer.append(value);
-        return parser.parseNameValuePair(buffer, 0, buffer.length());
+        ParserCursor cursor = new ParserCursor(0, value.length());
+        return parser.parseNameValuePair(buffer, cursor);
     }
 
 
-    /**
-     * Parses a name=value specification, where the = and value are optional.
-     *
-     * @param buffer    the buffer holding the name-value pair to parse
-     *
-     * @return  the name-value pair, where the value is <code>null</code>
-     *          if no value is specified
-     */
+    // non-javadoc, see interface HeaderValueParser
     public NameValuePair parseNameValuePair(final CharArrayBuffer buffer,
-                                            final int indexFrom,
-                                            final int indexTo) {
+                                            final ParserCursor cursor) {
 
         if (buffer == null) {
-            throw new IllegalArgumentException
-                ("Char array buffer may not be 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 (cursor == null) {
+            throw new IllegalArgumentException("Parser cursor may not be null");
         }
 
-        int eq = buffer.indexOf('=', indexFrom, indexTo);
-        if (eq < 0) {
-            return createNameValuePair(buffer.substringTrimmed(indexFrom, indexTo), null);
+        boolean terminated = false;
+        
+        int pos = cursor.getPos();
+        int indexFrom = cursor.getPos();
+        int indexTo = cursor.getUpperBound();
+        
+        // Find name
+        String name = null;
+        while (pos < indexTo) {
+            char ch = buffer.charAt(pos);
+            if (ch == '=') {
+                break;
+            }
+            if (ch == PARAM_DELIMITER || ch == ELEM_DELIMITER) {
+                terminated = true;
+                break;
+            }
+            pos++;
         }
-        String name = buffer.substringTrimmed(indexFrom, eq);
-        int i1 = eq + 1;
-        int i2 = indexTo;
+        
+        if (pos == indexTo) {
+            terminated = true;
+            name = buffer.substringTrimmed(indexFrom, indexTo);
+        } else {
+            name = buffer.substringTrimmed(indexFrom, pos);
+            pos++;
+        }
+        
+        if (terminated) {
+            cursor.updatePos(pos);
+            return createNameValuePair(name, null);
+        }
+
+        // Find value
+        String value = null;
+        int i1 = pos;
+        
+        boolean qouted = false;
+        boolean escaped = false;
+        while (pos < indexTo) {
+            char ch = buffer.charAt(pos);
+            if (ch == '"' && !escaped) {
+                qouted = !qouted;
+            }
+            if (!qouted && !escaped && 
+                    (ch == PARAM_DELIMITER || ch == ELEM_DELIMITER)) {
+                terminated = true;
+                break;
+            }
+            if (escaped) {
+                escaped = false;
+            } else {
+                escaped = qouted && ch == '\\';
+            }
+            pos++;
+        }
+        
+        int i2 = pos;
         // Trim leading white spaces
         while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) {
             i1++;
@@ -374,11 +375,14 @@
             i1++;
             i2--;
         }
-        String value = buffer.substring(i1, i2);
+        value = buffer.substring(i1, i2);
+        if (terminated) {
+            pos++;
+        }
+        cursor.updatePos(pos);
         return createNameValuePair(name, value);
     }
 
-
     /**
      * Creates a name-value pair.
      * Called from {@link #parseNameValuePair}.
@@ -391,7 +395,6 @@
     protected NameValuePair createNameValuePair(final String name, final String value) {
         return new BasicNameValuePair(name, value);
     }
-
 
 }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BufferedHeader.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BufferedHeader.java?rev=589325&r1=589324&r2=589325&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BufferedHeader.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BufferedHeader.java
Sun Oct 28 03:37:56 2007
@@ -130,8 +130,9 @@
         HeaderValueParser hvp = this.parser;
         if (hvp == null)
             hvp = BasicHeaderValueParser.DEFAULT;
-        return hvp.parseElements(this.buffer,
-                                 this.valuePos, this.buffer.length());
+        ParserCursor cursor = new ParserCursor(0, this.buffer.length());
+        cursor.updatePos(this.valuePos);
+        return hvp.parseElements(this.buffer, cursor);
     }
 
     public int getValuePos() {

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java?rev=589325&r1=589324&r2=589325&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/HeaderValueParser.java
Sun Oct 28 03:37:56 2007
@@ -81,17 +81,16 @@
      * </p>
      *
      * @param buffer    buffer holding the header value to parse
+     * @param cursor    the parser cursor containing the current position and 
+     *                  the bounds within the buffer for the parsing operation
      *
      * @return  an array holding all elements of the header value
      *
      * @throws ParseException        in case of a parse error
      */
-    HeaderElement[] parseElements(CharArrayBuffer buffer,
-                                  int indexFrom,
-                                  int indexTo)
-        throws ParseException
-        ;
-
+    HeaderElement[] parseElements(
+            CharArrayBuffer buffer,
+            ParserCursor cursor) throws ParseException;
 
     /**
      * Parses a single header element.
@@ -99,17 +98,16 @@
      * of name=value definitions.
      *
      * @param buffer    buffer holding the element to parse
+     * @param cursor    the parser cursor containing the current position and 
+     *                  the bounds within the buffer for the parsing operation
      *
      * @return  the parsed element
      *
      * @throws ParseException        in case of a parse error
      */
-    HeaderElement parseHeaderElement(CharArrayBuffer buffer,
-                                     int indexFrom,
-                                     int indexTo)
-        throws ParseException
-        ;
-
+    HeaderElement parseHeaderElement(
+            CharArrayBuffer buffer,
+            ParserCursor cursor) throws ParseException;
 
     /**
      * Parses a list of name-value pairs.
@@ -186,33 +184,31 @@
      * </pre> 
      *
      * @param buffer    buffer holding the name-value list to parse
+     * @param cursor    the parser cursor containing the current position and 
+     *                  the bounds within the buffer for the parsing operation
      *
      * @return  an array holding all items of the name-value list
      *
      * @throws ParseException        in case of a parse error
      */
-    NameValuePair[] parseParameters(CharArrayBuffer buffer,
-                                    int indexFrom,
-                                    int indexTo)
-        throws ParseException
-        ;
+    NameValuePair[] parseParameters(
+            CharArrayBuffer buffer,
+            ParserCursor cursor) throws ParseException;
 
 
     /**
      * Parses a name=value specification, where the = and value are optional.
      *
      * @param buffer    the buffer holding the name-value pair to parse
+     * @param cursor    the parser cursor containing the current position and 
+     *                  the bounds within the buffer for the parsing operation
      *
      * @return  the name-value pair, where the value is <code>null</code>
      *          if no value is specified
-     *
-     * @throws ParseException        in case of a parse error
      */
-    NameValuePair parseNameValuePair(CharArrayBuffer buffer,
-                                     int indexFrom,
-                                     int indexTo)
-        throws ParseException
-        ;
-
+    NameValuePair parseNameValuePair(
+            CharArrayBuffer buffer,
+            ParserCursor cursor) throws ParseException;
+    
 }
 

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java?rev=589325&r1=589324&r2=589325&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
Sun Oct 28 03:37:56 2007
@@ -134,70 +134,122 @@
         assertEquals("Number of elements", 0, elements.length);
     }
 
-    public void testHEInvalidInput() throws Exception {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        buffer.append("name = value");
-        try {
-            BasicHeaderValueParser.parseElements(null, null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-
-        try {
-            BasicHeaderValueParser.DEFAULT.parseElements(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseElements(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseElements(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseElements(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-
-
-        try {
-            BasicHeaderValueParser.DEFAULT.parseHeaderElement(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseHeaderElement(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseHeaderElement(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseHeaderElement(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-    }
+    public void testNVParseUsingCursor() {
+        
+        HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
+        
+        String s = "test";
+        CharArrayBuffer buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+        
+        NameValuePair param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("test", param.getName());
+        assertEquals(null, param.getValue());
+        assertEquals(s.length(), cursor.getPos());
+        assertTrue(cursor.atEnd());
 
+        s = "test;";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("test", param.getName());
+        assertEquals(null, param.getValue());
+        assertEquals(s.length(), cursor.getPos());
+        assertTrue(cursor.atEnd());
+        
+        s = "test  ,12";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("test", param.getName());
+        assertEquals(null, param.getValue());
+        assertEquals(s.length() - 2, cursor.getPos());
+        assertFalse(cursor.atEnd());
+        
+        s = "test=stuff";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        assertEquals(s.length(), cursor.getPos());
+        assertTrue(cursor.atEnd());
+        
+        s = "   test  =   stuff ";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        assertEquals(s.length(), cursor.getPos());
+        assertTrue(cursor.atEnd());
+
+        s = "   test  =   stuff ;1234";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        assertEquals(s.length() - 4, cursor.getPos());
+        assertFalse(cursor.atEnd());
+        
+        s = "test  = \"stuff\"";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("test", param.getName());
+        assertEquals("stuff", param.getValue());
+        
+        s = "test  = \"  stuff\\\"\"";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("test", param.getName());
+        assertEquals("  stuff\\\"", param.getValue());
+        
+        s = "  test";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("test", param.getName());
+        assertEquals(null, param.getValue());
+
+        s = "  ";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("", param.getName());
+        assertEquals(null, param.getValue());
+
+        s = " = stuff ";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        param = parser.parseNameValuePair(buffer, cursor);
+        assertEquals("", param.getName());
+        assertEquals("stuff", param.getValue());
+    }
 
-    
     public void testNVParse() {
         String s = "test";
         NameValuePair param =
@@ -241,6 +293,53 @@
         assertEquals("stuff", param.getValue());
     }
 
+    public void testNVParseAllWithCursor() {
+        HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
+        
+        String s = 
+            "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
+        CharArrayBuffer buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        ParserCursor cursor = new ParserCursor(0, s.length());
+        
+        NameValuePair[] params = parser.parseParameters(buffer, cursor);
+        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());
+        assertEquals(s.length(), cursor.getPos());
+        assertTrue(cursor.atEnd());
+
+        s = 
+            "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff\",123";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        
+        params = parser.parseParameters(buffer, cursor);
+        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());
+        assertEquals(s.length() - 3, cursor.getPos());
+        assertFalse(cursor.atEnd());
+
+        s = "  ";
+        buffer = new CharArrayBuffer(16);
+        buffer.append(s);
+        cursor = new ParserCursor(0, s.length());
+        params = parser.parseParameters(buffer, cursor);
+        assertEquals(0, params.length);
+    }
+
     public void testNVParseAll() {
         String s = 
           "test; test1 =  stuff   ; test2 =  \"stuff; stuff\"; test3=\"stuff";
@@ -272,73 +371,6 @@
         assertEquals("\\\\", params[1].getValue());
         assertEquals("test3", params[2].getName());
         assertEquals("stuff; stuff", params[2].getValue());
-    }
-
-    public void testNVParseInvalidInput() throws Exception {
-        CharArrayBuffer buffer = new CharArrayBuffer(32);
-        buffer.append("name = value");
-
-        try {
-            BasicHeaderValueParser.parseParameters(null, null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseParameters(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseParameters(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseParameters(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseParameters(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-
-        try {
-            BasicHeaderValueParser.parseNameValuePair(null, null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseNameValuePair(null, 0, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseNameValuePair(buffer, -1, 0);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseNameValuePair(buffer, 0, 1000);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
-        try {
-            BasicHeaderValueParser.DEFAULT.parseNameValuePair(buffer, 2, 1);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IndexOutOfBoundsException ex) {
-            // expected
-        }
     }
 
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java?rev=589325&r1=589324&r2=589325&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/message/TestHeaderElement.java
Sun Oct 28 03:37:56 2007
@@ -38,7 +38,6 @@
 
 import org.apache.http.HeaderElement;
 import org.apache.http.NameValuePair;
-import org.apache.http.util.CharArrayBuffer;
 
 /**
  * Simple tests for {@link HeaderElement}.
@@ -89,16 +88,6 @@
     }
 
 
-    public void testConstructor1() throws Exception {
-        String s = "name = value; param1 = value1";
-        NameValuePair[] nvps = BasicHeaderValueParser.parseParameters(s, null);
-        HeaderElement element = new BasicHeaderElement(nvps);
-        assertEquals("name", element.getName());
-        assertEquals("value", element.getValue());
-        assertEquals(1, element.getParameters().length);
-        assertEquals("value1", element.getParameterByName("param1").getValue());
-    }
-    
     public void testInvalidName() {
         try {
             new BasicHeaderElement(null, null, null); 
@@ -109,10 +98,8 @@
     }
 
     public void testParamByName() throws Exception {
-        CharArrayBuffer buffer = new CharArrayBuffer(64);
-        buffer.append("name = value; param1 = value1; param2 = value2");
-        HeaderElement element = BasicHeaderValueParser.DEFAULT
-            .parseHeaderElement(buffer, 0, buffer.length()); 
+        String s = "name = value; param1 = value1; param2 = value2";
+        HeaderElement element = BasicHeaderValueParser.parseHeaderElement(s, null); 
         assertEquals("value1", element.getParameterByName("param1").getValue());
         assertEquals("value2", element.getParameterByName("param2").getValue());
         assertNull(element.getParameterByName("param3"));
@@ -178,21 +165,11 @@
     }
     
     public void testToString() {
-        CharArrayBuffer buffer = new CharArrayBuffer(64);
         String s = "name=value; param1=value1; param2=value2";
-        buffer.append(s);
-
-
-        HeaderElement element = BasicHeaderValueParser.DEFAULT
-            .parseHeaderElement(buffer, 0, buffer.length());
+        HeaderElement element = BasicHeaderValueParser.parseHeaderElement(s, null);
         assertEquals(s, element.toString());
-
         s = "name; param1=value1; param2=value2";
-        buffer.clear();
-        buffer.append(s);
-
-        element = BasicHeaderValueParser.DEFAULT
-            .parseHeaderElement(buffer, 0, buffer.length());
+        element = BasicHeaderValueParser.parseHeaderElement(s, null);
         assertEquals(s, element.toString());
     }
 }



Mime
View raw message