Fix "HttpUrlConnection.addRequestProperty replaces existing properties instead of append them".

The bug was actually that the copy constructor wasn't reconstructing the 'props'
list correctly. Upstream has applied the same fix, so I'm also bringing my
earlier change into sync with theirs, whitespace-wise (and removing no longer
necessary 'android-changed' tags).

Bug: http://code.google.com/p/android/issues/detail?id=6722
diff --git a/libcore/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java b/libcore/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java
index 0f8dfa9..f919b39 100644
--- a/libcore/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java
+++ b/libcore/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java
@@ -19,11 +19,12 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.SortedMap;
+import java.util.TreeMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.TreeMap;
 
 /**
  * The general structure for request / response header. It is essentially
@@ -32,10 +33,7 @@
 public class Header implements Cloneable {
     private ArrayList<String> props;
 
-    // BEGIN android-changed: header fields should be case-insensitive but case-preserving.
-    // http://code.google.com/p/android/issues/detail?id=6684
-    private TreeMap<String, LinkedList<String>> keyTable;
-    // END android-changed
+    private SortedMap<String, LinkedList<String>> keyTable;
 
     private String statusLine;
 
@@ -47,7 +45,8 @@
     public Header() {
         super();
         this.props = new ArrayList<String>(20);
-        this.keyTable = new TreeMap<String, LinkedList<String>>(String.CASE_INSENSITIVE_ORDER); // android-changed
+        this.keyTable = new TreeMap<String, LinkedList<String>>(
+                String.CASE_INSENSITIVE_ORDER);
     }
 
     /**
@@ -61,11 +60,11 @@
         this(); // initialize fields
         for (Entry<String, List<String>> next : map.entrySet()) {
             String key = next.getKey();
-            props.add(key);
             List<String> value = next.getValue();
             LinkedList<String> linkedList = new LinkedList<String>();
             for (String element : value) {
                 linkedList.add(element);
+                props.add(key);
                 props.add(element);
             }
             keyTable.put(key, linkedList);
@@ -78,9 +77,12 @@
         try {
             Header clone = (Header) super.clone();
             clone.props = (ArrayList<String>) props.clone();
-            clone.keyTable = new TreeMap<String, LinkedList<String>>(String.CASE_INSENSITIVE_ORDER); // android-changed
-            for (Map.Entry<String, LinkedList<String>> next : this.keyTable.entrySet()) {
-                LinkedList<String> v = (LinkedList<String>) next.getValue().clone();
+            clone.keyTable = new TreeMap<String, LinkedList<String>>(
+                    String.CASE_INSENSITIVE_ORDER);
+            for (Map.Entry<String, LinkedList<String>> next : this.keyTable
+                    .entrySet()) {
+                LinkedList<String> v = (LinkedList<String>) next.getValue()
+                        .clone();
                 clone.keyTable.put(next.getKey(), v);
             }
             return clone;
@@ -102,7 +104,7 @@
         LinkedList<String> list = keyTable.get(key);
         if (list == null) {
             list = new LinkedList<String>();
-            keyTable.put(key, list); // android-changed
+            keyTable.put(key, list);
         }
         list.add(value);
         props.add(key);
@@ -193,7 +195,7 @@
      *         such key exists.
      */
     public String get(String key) {
-        LinkedList<String> result = keyTable.get(key); // android-changed
+        LinkedList<String> result = keyTable.get(key);
         if (result == null) {
             return null;
         }
diff --git a/libcore/luni/src/test/java/org/apache/harmony/luni/internal/net/www/protocol/http/HeaderTest.java b/libcore/luni/src/test/java/org/apache/harmony/luni/internal/net/www/protocol/http/HeaderTest.java
index 7ec5fd8..3dc411e 100644
--- a/libcore/luni/src/test/java/org/apache/harmony/luni/internal/net/www/protocol/http/HeaderTest.java
+++ b/libcore/luni/src/test/java/org/apache/harmony/luni/internal/net/www/protocol/http/HeaderTest.java
@@ -38,4 +38,18 @@
         assertEquals(Arrays.asList("text/plain"), h.getFieldMap().get("Content-Type"));
         assertEquals(Arrays.asList("text/plain"), h.getFieldMap().get("Content-type")); // RI fails this.
     }
+
+    // The copy constructor used to be broken for headers with multiple values.
+    // http://code.google.com/p/android/issues/detail?id=6722
+    public void test_copyConstructor() {
+        Header h1 = new Header();
+        h1.add("key", "value1");
+        h1.add("key", "value2");
+        Header h2 = new Header(h1.getFieldMap());
+        assertEquals(2, h2.length());
+        assertEquals("key", h2.getKey(0));
+        assertEquals("value1", h2.get(0));
+        assertEquals("key", h2.getKey(1));
+        assertEquals("value2", h2.get(1));
+    }
 }