hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r354885 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/Header.java test/org/apache/http/TestHeader.java
Date Wed, 07 Dec 2005 22:44:21 GMT
Author: olegk
Date: Wed Dec  7 14:44:11 2005
New Revision: 354885

URL: http://svn.apache.org/viewcvs?rev=354885&view=rev
Log:
* Header class made immutable again. It is no longer possible to render the content of a Header
invalid by mutating the underlying char array buffer
* Header#parseAll method uses an private inner class to produce headers whose parsing is deferred
until the header value is consumed (if at all)


Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/Header.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/TestHeader.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=354885&r1=354884&r2=354885&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 Wed Dec  7
14:44:11 2005
@@ -51,14 +51,9 @@
     private final String name;
     
     /**
-     * Buffer containing the header in a raw form.
+     * Header value.
      */
-    private final CharArrayBuffer buffer;
-    
-    /**
-     * Position of the value in the header.
-     */
-    private final int posValue;
+    private final String value;
     
     /**
      * Autogenerated header flag.
@@ -72,20 +67,13 @@
      * @param value the header value
      * @param isAutogenerated <tt>true</tt> if the header is autogenerated,
      */
-    public Header(final String name, String value, boolean isAutogenerated) {
+    public Header(final String name, final String value, boolean isAutogenerated) {
         super();
         if (name == null) {
             throw new IllegalArgumentException("Name may not be null");
         }
         this.name = name;
-        this.posValue = name.length() + 2;
-        int len = name.length() + 2 + (value != null ? value.length() : 0);
-        this.buffer = new CharArrayBuffer(len);
-        this.buffer.append(name);
-        this.buffer.append(": ");
-        if (value != null) {
-            this.buffer.append(value);
-        }
+        this.value = value;
         this.isAutogenerated = isAutogenerated;
     }
 
@@ -103,35 +91,6 @@
     }
 
     /**
-     * <p> Constructor with char array buffer </p> 
-     * <p> Please note this constructor does not make a deep copy of the 
-     * char array buffer. If the char array is modified outside the Header
-     * object, it may leave the object in a inconsistent state</p> 
-     *
-     * @param name the header name
-     * @param value the header value
-     * @param isAutogenerated <tt>true</tt> if the header is autogenerated,
-     */
-    public Header(final CharArrayBuffer buffer) throws ProtocolException {
-        super();
-        if (buffer == null) {
-            throw new IllegalArgumentException("Char array buffer may not be null");
-        }
-        int comma = buffer.indexOf(':');
-        if (comma == -1) {
-            throw new ProtocolException("Invalid header: " + buffer.toString());
-        }
-        this.posValue = comma + 1;
-        String s = buffer.substringTrimmed(0, comma);
-        if (s.equals("")) {
-            throw new ProtocolException("Invalid header: " + buffer.toString());
-        }
-        this.name = s;
-        this.buffer = buffer;
-        this.isAutogenerated = false;
-    }
-
-    /**
      * Returns the header name.
      *
      * @return String name The name
@@ -146,7 +105,7 @@
      * @return String value The current value.
      */
     public String getValue() {
-        return this.buffer.substringTrimmed(this.posValue, this.buffer.length());
+        return this.value;
     }
 
     /**
@@ -167,6 +126,12 @@
      * @return a string
      */
     public String toString() {
+        CharArrayBuffer buffer = new CharArrayBuffer(32);
+        buffer.append(this.name);
+        buffer.append(": ");
+        if (this.value != null) {
+            buffer.append(this.value);
+        }
         return buffer.toString();
     }
 
@@ -180,9 +145,39 @@
      * @since 3.0
      */
     public HeaderElement[] getElements() {
-        return HeaderElement.parseAll(this.buffer, this.posValue, this.buffer.length());
+        if (this.value != null) {
+            return HeaderElement.parseAll(this.value);
+        } else {
+            return new HeaderElement[] {}; 
+        }
     }
 
+    
+    static class BufferedHeader extends Header {
+        
+        private final CharArrayBuffer buffer;
+        private final int posValue;
+        
+        private BufferedHeader(final String name, final CharArrayBuffer buffer, int posValue)
{
+            super(name, null, false);
+            this.buffer = buffer;
+            this.posValue = posValue;
+        }
+        
+        public String getValue() {
+            return this.buffer.substringTrimmed(this.posValue, this.buffer.length());
+        }
+        
+        public HeaderElement[] getElements() {
+            return HeaderElement.parseAll(this.buffer, this.posValue, this.buffer.length());
+        }
+        
+        public String toString() {
+            return this.buffer.toString();
+        }
+        
+    }
+    
     public static Header[] parseAll(final HttpDataReceiver datareceiver) 
             throws HttpException, IOException {
         ArrayList headerLines = new ArrayList();
@@ -224,7 +219,16 @@
         }
         Header[] headers = new Header[headerLines.size()];
         for (int i = 0; i < headerLines.size(); i++) {
-            headers[i] = new Header((CharArrayBuffer) headerLines.get(i));
+            CharArrayBuffer buffer = (CharArrayBuffer) headerLines.get(i);
+            int comma = buffer.indexOf(':');
+            if (comma == -1) {
+                throw new ProtocolException("Invalid header: " + buffer.toString());
+            }
+            String s = buffer.substringTrimmed(0, comma);
+            if (s.equals("")) {
+                throw new ProtocolException("Invalid header: " + buffer.toString());
+            }
+            headers[i] = new BufferedHeader(s, buffer, comma + 1);
         }
         return headers;
     }

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=354885&r1=354884&r2=354885&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 Wed Dec
 7 14:44:11 2005
@@ -29,7 +29,6 @@
 
 package org.apache.http;
 
-import org.apache.http.io.CharArrayBuffer;
 import org.apache.http.io.HttpDataReceiver;
 import org.apache.http.mockup.HttpDataReceiverMockup;
 
@@ -64,17 +63,10 @@
         assertTrue(header.isAutogenerated()); 
     }
     
-    public void testBasicConstructorValueWithBlanks() {
-        Header header = new Header("name", "  value  ", true);
-        assertEquals("name", header.getName()); 
-        assertEquals("value", header.getValue()); 
-        assertTrue(header.isAutogenerated()); 
-    }
-    
     public void testBasicConstructorNullValue() {
         Header header = new Header("name", null, true);
         assertEquals("name", header.getName()); 
-        assertEquals("", header.getValue()); 
+        assertEquals(null, header.getValue()); 
         assertTrue(header.isAutogenerated()); 
     }
     
@@ -86,49 +78,7 @@
             //expected
         }
     }
-
-    public void testCharArrayConstructor() throws Exception {
-        CharArrayBuffer buffer1 = new CharArrayBuffer(16);
-        buffer1.append("name:value");
-        Header header1 = new Header(buffer1); 
-        assertEquals("name", header1.getName());
-        assertEquals("value", header1.getValue());
-
-        CharArrayBuffer buffer2 = new CharArrayBuffer(16);
-        buffer2.append("    name   :    value       ");
-        Header header2 = new Header(buffer2); 
-        assertEquals("name", header2.getName());
-        assertEquals("value", header2.getValue());
-    }
-
-    public void testNullCharArrayConstructor() throws Exception {
-        try {
-            new Header(null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException ex) {
-            // expected
-        }
-    }
     
-    public void testMalformedCharArrayConstructor() throws Exception {
-        try {
-            CharArrayBuffer buffer = new CharArrayBuffer(16);
-            buffer.append("  whatever  ");
-            new Header(buffer);
-            fail("ProtocolException should have been thrown");
-        } catch (ProtocolException ex) {
-            // expected
-        }
-        try {
-            CharArrayBuffer buffer = new CharArrayBuffer(16);
-            buffer.append(" : whatever  ");
-            new Header(buffer);
-            fail("ProtocolException should have been thrown");
-        } catch (ProtocolException ex) {
-            // expected
-        }
-    }
-        
     public void testToString() {
         Header header1 = new Header("name1", "value1");
         assertEquals("name1: value1", header1.toString());



Mime
View raw message