hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1626680 - /httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java
Date Mon, 22 Sep 2014 07:33:12 GMT
Author: olegk
Date: Mon Sep 22 07:33:12 2014
New Revision: 1626680

URL: http://svn.apache.org/r1626680
Log:
NetscapeDraftHeaderParser to use TokenParser from core internally

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java?rev=1626680&r1=1626679&r2=1626680&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftHeaderParser.java
Mon Sep 22 07:33:12 2014
@@ -28,6 +28,7 @@
 package org.apache.http.impl.cookie;
 
 import java.util.ArrayList;
+import java.util.BitSet;
 import java.util.List;
 
 import org.apache.http.HeaderElement;
@@ -37,7 +38,7 @@ import org.apache.http.annotation.Immuta
 import org.apache.http.message.BasicHeaderElement;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.message.ParserCursor;
-import org.apache.http.protocol.HTTP;
+import org.apache.http.message.TokenParser;
 import org.apache.http.util.Args;
 import org.apache.http.util.CharArrayBuffer;
 
@@ -50,8 +51,18 @@ public class NetscapeDraftHeaderParser {
 
     public final static NetscapeDraftHeaderParser DEFAULT = new NetscapeDraftHeaderParser();
 
+    private final static char PARAM_DELIMITER                = ';';
+
+    // IMPORTANT!
+    // These private static variables must be treated as immutable and never exposed outside
this class
+    private static final BitSet TOKEN_DELIMS = TokenParser.INIT_BITSET('=', PARAM_DELIMITER);
+    private static final BitSet VALUE_DELIMS = TokenParser.INIT_BITSET(PARAM_DELIMITER);
+
+    private final TokenParser tokenParser;
+
     public NetscapeDraftHeaderParser() {
         super();
+        this.tokenParser = TokenParser.INSTANCE;
     }
 
     public HeaderElement parseHeader(
@@ -72,66 +83,19 @@ public class NetscapeDraftHeaderParser {
 
     private NameValuePair parseNameValuePair(
             final CharArrayBuffer buffer, final ParserCursor cursor) {
-        boolean terminated = false;
-
-        int pos = cursor.getPos();
-        final int indexFrom = cursor.getPos();
-        final int indexTo = cursor.getUpperBound();
-
-        // Find name
-        String name = null;
-        while (pos < indexTo) {
-            final char ch = buffer.charAt(pos);
-            if (ch == '=') {
-                break;
-            }
-            if (ch == ';') {
-                terminated = true;
-                break;
-            }
-            pos++;
-        }
-
-        if (pos == indexTo) {
-            terminated = true;
-            name = buffer.substringTrimmed(indexFrom, indexTo);
-        } else {
-            name = buffer.substringTrimmed(indexFrom, pos);
-            pos++;
-        }
-
-        if (terminated) {
-            cursor.updatePos(pos);
+        final String name = tokenParser.parseToken(buffer, cursor, TOKEN_DELIMS);
+        if (cursor.atEnd()) {
             return new BasicNameValuePair(name, null);
         }
-
-        // Find value
-        String value = null;
-        int i1 = pos;
-
-        while (pos < indexTo) {
-            final char ch = buffer.charAt(pos);
-            if (ch == ';') {
-                terminated = true;
-                break;
-            }
-            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--;
+        final int delim = buffer.charAt(cursor.getPos());
+        cursor.updatePos(cursor.getPos() + 1);
+        if (delim != '=') {
+            return new BasicNameValuePair(name, null);
         }
-        value = buffer.substring(i1, i2);
-        if (terminated) {
-            pos++;
+        final String value = tokenParser.parseToken(buffer, cursor, VALUE_DELIMS);
+        if (!cursor.atEnd()) {
+            cursor.updatePos(cursor.getPos() + 1);
         }
-        cursor.updatePos(pos);
         return new BasicNameValuePair(name, value);
     }
 



Mime
View raw message