hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1615886 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/message/BasicHeaderValueParser.java test/java/org/apache/http/message/TestBasicHeaderValueParser.java
Date Tue, 05 Aug 2014 08:16:24 GMT
Author: olegk
Date: Tue Aug  5 08:16:24 2014
New Revision: 1615886

URL: http://svn.apache.org/r1615886
Log:
Changed BasicHeaderValueParser to use TokenParser for header element parsing

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java?rev=1615886&r1=1615885&r2=1615886&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/message/BasicHeaderValueParser.java
Tue Aug  5 08:16:24 2014
@@ -28,13 +28,13 @@
 package org.apache.http.message;
 
 import java.util.ArrayList;
+import java.util.BitSet;
 import java.util.List;
 
 import org.apache.http.HeaderElement;
 import org.apache.http.NameValuePair;
 import org.apache.http.ParseException;
 import org.apache.http.annotation.Immutable;
-import org.apache.http.protocol.HTTP;
 import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -64,13 +64,13 @@ public class BasicHeaderValueParser impl
 
     private final static char PARAM_DELIMITER                = ';';
     private final static char ELEM_DELIMITER                 = ',';
-    private final static char[] ALL_DELIMITERS               = new char[] {
-                                                                PARAM_DELIMITER,
-                                                                ELEM_DELIMITER
-                                                                };
+    private static final BitSet TOKEN_DELIMS = TokenParser.INIT_BITSET('=', PARAM_DELIMITER,
ELEM_DELIMITER);
+    private static final BitSet VALUE_DELIMS = TokenParser.INIT_BITSET(PARAM_DELIMITER, ELEM_DELIMITER);
+
+    private final TokenParser tokenParser;
 
     public BasicHeaderValueParser() {
-        super();
+        this.tokenParser = TokenParser.INSTANCE;
     }
 
     /**
@@ -193,22 +193,7 @@ public class BasicHeaderValueParser impl
                                            final ParserCursor cursor) {
         Args.notNull(buffer, "Char array buffer");
         Args.notNull(cursor, "Parser cursor");
-        int pos = cursor.getPos();
-        final int indexTo = cursor.getUpperBound();
-
-        while (pos < indexTo) {
-            final char ch = buffer.charAt(pos);
-            if (HTTP.isWhitespace(ch)) {
-                pos++;
-            } else {
-                break;
-            }
-        }
-        cursor.updatePos(pos);
-        if (cursor.atEnd()) {
-            return new NameValuePair[] {};
-        }
-
+        tokenParser.skipWhiteSpace(buffer, cursor);
         final List<NameValuePair> params = new ArrayList<NameValuePair>();
         while (!cursor.atEnd()) {
             final NameValuePair param = parseNameValuePair(buffer, cursor);
@@ -218,7 +203,6 @@ public class BasicHeaderValueParser impl
                 break;
             }
         }
-
         return params.toArray(new NameValuePair[params.size()]);
     }
 
@@ -247,103 +231,56 @@ public class BasicHeaderValueParser impl
     @Override
     public NameValuePair parseNameValuePair(final CharArrayBuffer buffer,
                                             final ParserCursor cursor) {
-        return parseNameValuePair(buffer, cursor, ALL_DELIMITERS);
-    }
+        Args.notNull(buffer, "Char array buffer");
+        Args.notNull(cursor, "Parser cursor");
 
-    private static boolean isOneOf(final char ch, final char[] chs) {
-        if (chs != null) {
-            for (final char ch2 : chs) {
-                if (ch == ch2) {
-                    return true;
-                }
-            }
+        final String name = tokenParser.parseToken(buffer, cursor, TOKEN_DELIMS);
+        if (cursor.atEnd()) {
+            return new BasicNameValuePair(name, null);
+        }
+        final int delim = buffer.charAt(cursor.getPos());
+        cursor.updatePos(cursor.getPos() + 1);
+        if (delim != '=') {
+            return createNameValuePair(name, null);
+        }
+        final String value = tokenParser.parseValue(buffer, cursor, VALUE_DELIMS);
+        if (!cursor.atEnd()) {
+            cursor.updatePos(cursor.getPos() + 1);
         }
-        return false;
+        return createNameValuePair(name, value);
     }
 
+    /**
+     * @deprecated (4.4) use {@link org.apache.http.message.TokenParser}
+     */
+    @Deprecated
     public NameValuePair parseNameValuePair(final CharArrayBuffer buffer,
                                             final ParserCursor cursor,
                                             final char[] delimiters) {
         Args.notNull(buffer, "Char array buffer");
         Args.notNull(cursor, "Parser cursor");
 
-        boolean terminated = false;
-
-        int pos = cursor.getPos();
-        final int indexFrom = cursor.getPos();
-        final int indexTo = cursor.getUpperBound();
-
-        // Find name
-        final String name;
-        while (pos < indexTo) {
-            final char ch = buffer.charAt(pos);
-            if (ch == '=') {
-                break;
-            }
-            if (isOneOf(ch, delimiters)) {
-                terminated = true;
-                break;
+        final BitSet delimSet = new BitSet();
+        if (delimiters != null) {
+            for (char delimiter: delimiters) {
+                delimSet.set(delimiter);
             }
-            pos++;
         }
-
-        if (pos == indexTo) {
-            terminated = true;
-            name = buffer.substringTrimmed(indexFrom, indexTo);
-        } else {
-            name = buffer.substringTrimmed(indexFrom, pos);
-            pos++;
+        delimSet.set('=');
+        final String name = tokenParser.parseToken(buffer, cursor, delimSet);
+        if (cursor.atEnd()) {
+            return new BasicNameValuePair(name, null);
         }
-
-        if (terminated) {
-            cursor.updatePos(pos);
+        final int delim = buffer.charAt(cursor.getPos());
+        cursor.updatePos(cursor.getPos() + 1);
+        if (delim != '=') {
             return createNameValuePair(name, null);
         }
-
-        // Find value
-        final String value;
-        int i1 = pos;
-
-        boolean qouted = false;
-        boolean escaped = false;
-        while (pos < indexTo) {
-            final char ch = buffer.charAt(pos);
-            if (ch == '"' && !escaped) {
-                qouted = !qouted;
-            }
-            if (!qouted && !escaped && isOneOf(ch, delimiters)) {
-                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++;
-        }
-        // Trim trailing white spaces
-        while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) {
-            i2--;
-        }
-        // Strip away quotes if necessary
-        if (((i2 - i1) >= 2)
-            && (buffer.charAt(i1) == '"')
-            && (buffer.charAt(i2 - 1) == '"')) {
-            i1++;
-            i2--;
-        }
-        value = buffer.substring(i1, i2);
-        if (terminated) {
-            pos++;
+        delimSet.clear('=');
+        final String value = tokenParser.parseValue(buffer, cursor, delimSet);
+        if (!cursor.atEnd()) {
+            cursor.updatePos(cursor.getPos() + 1);
         }
-        cursor.updatePos(pos);
         return createNameValuePair(name, value);
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java?rev=1615886&r1=1615885&r2=1615886&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/message/TestBasicHeaderValueParser.java
Tue Aug  5 08:16:24 2014
@@ -80,9 +80,9 @@ public class TestBasicHeaderValueParser 
         final HeaderElement[] elements = BasicHeaderValueParser.parseElements(s, null);
         Assert.assertEquals(3, elements.length);
         Assert.assertEquals("test1", elements[0].getName());
-        Assert.assertEquals("\\\"stuff\\\"", elements[0].getValue());
+        Assert.assertEquals("\"stuff\"", elements[0].getValue());
         Assert.assertEquals("test2", elements[1].getName());
-        Assert.assertEquals("\\\\", elements[1].getValue());
+        Assert.assertEquals("\\", elements[1].getValue());
         Assert.assertEquals("test3", elements[2].getName());
         Assert.assertEquals("stuff, stuff", elements[2].getValue());
     }
@@ -195,7 +195,7 @@ public class TestBasicHeaderValueParser 
 
         param = parser.parseNameValuePair(buffer, cursor);
         Assert.assertEquals("test", param.getName());
-        Assert.assertEquals("  stuff\\\"", param.getValue());
+        Assert.assertEquals("  stuff\"", param.getValue());
 
         s = "  test";
         buffer = new CharArrayBuffer(16);
@@ -251,7 +251,7 @@ public class TestBasicHeaderValueParser 
         s = "test  = \"  stuff\\\"\"";
         param = BasicHeaderValueParser.parseNameValuePair(s, null);
         Assert.assertEquals("test", param.getName());
-        Assert.assertEquals("  stuff\\\"", param.getValue());
+        Assert.assertEquals("  stuff\"", param.getValue());
 
         s = "  test";
         param = BasicHeaderValueParser.parseNameValuePair(s, null);
@@ -287,7 +287,7 @@ public class TestBasicHeaderValueParser 
         Assert.assertEquals("test2", params[2].getName());
         Assert.assertEquals("stuff; stuff", params[2].getValue());
         Assert.assertEquals("test3", params[3].getName());
-        Assert.assertEquals("\"stuff", params[3].getValue());
+        Assert.assertEquals("stuff", params[3].getValue());
         Assert.assertEquals(s.length(), cursor.getPos());
         Assert.assertTrue(cursor.atEnd());
 
@@ -330,7 +330,7 @@ public class TestBasicHeaderValueParser 
         Assert.assertEquals("test2", params[2].getName());
         Assert.assertEquals("stuff; stuff", params[2].getValue());
         Assert.assertEquals("test3", params[3].getName());
-        Assert.assertEquals("\"stuff", params[3].getValue());
+        Assert.assertEquals("stuff", params[3].getValue());
 
         s = "  ";
         params = BasicHeaderValueParser.parseParameters(s, null);
@@ -345,9 +345,9 @@ public class TestBasicHeaderValueParser 
             BasicHeaderValueParser.parseParameters(s, null);
         Assert.assertEquals(3, params.length);
         Assert.assertEquals("test1", params[0].getName());
-        Assert.assertEquals("\\\"stuff\\\"", params[0].getValue());
+        Assert.assertEquals("\"stuff\"", params[0].getValue());
         Assert.assertEquals("test2", params[1].getName());
-        Assert.assertEquals("\\\\", params[1].getValue());
+        Assert.assertEquals("\\", params[1].getValue());
         Assert.assertEquals("test3", params[2].getName());
         Assert.assertEquals("stuff; stuff", params[2].getValue());
     }



Mime
View raw message