// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote.http;

import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteStreams;
import com.google.common.net.MediaType;

import org.openqa.selenium.WebDriverException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

class HttpMessage {

  private final Multimap<String, String> headers = ArrayListMultimap.create();

  private final Map<String, Object> attributes = new HashMap<>();

  private InputStream content = new ByteArrayInputStream(new byte[0]);
  private volatile byte[] readContent = null;

  /**
   * Retrieves a user-defined attribute of this message. Attributes are stored as simple key-value
   * pairs and are not included in a message's serialized form.
   *
   * @param key attribute name
   * @return attribute object
   */
  public Object getAttribute(String key) {
    return attributes.get(key);
  }

  public void setAttribute(String key, Object value) {
    attributes.put(key, value);
  }

  public void removeAttribute(String key) {
    attributes.remove(key);
  }

  public Iterable<String> getHeaderNames() {
    return headers.keySet();
  }

  public Iterable<String> getHeaders(String name) {
    return headers.get(name);
  }

  public String getHeader(String name) {
    return headers.entries().stream()
        .filter(e -> Objects.nonNull(e.getKey()))
        .filter(e -> e.getKey().equalsIgnoreCase(name.toLowerCase()))
        .map(Map.Entry::getValue)
        .findFirst()
        .orElse(null);
  }

  public void setHeader(String name, String value) {
    removeHeader(name);
    headers.put(name, value);
  }

  public void addHeader(String name, String value) {
    headers.put(name, value);
  }

  public void removeHeader(String name) {
    headers.removeAll(name);
  }

  public Charset getContentEncoding() {
    Charset charset = UTF_8;
    try {
      String contentType = getHeader(CONTENT_TYPE);
      if (contentType != null) {
        MediaType mediaType = MediaType.parse(contentType);
        charset = mediaType.charset().or(UTF_8);
      }
    } catch (IllegalArgumentException ignored) {
      // Do nothing.
    }
    return charset;
  }

  public void setContent(byte[] data) {
    this.content = new ByteArrayInputStream(data);
  }

  public void setContent(InputStream toStreamFrom) {
    this.content = toStreamFrom;
  }

  public byte[] getContent() {
    if (readContent == null) {
      synchronized (this) {
        if (readContent == null) {
          try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            ByteStreams.copy(consumeContentStream(), bos);
            readContent = bos.toByteArray();
          } catch (IOException e) {
            throw new WebDriverException(e);
          }
        }
      }
    }
    return readContent;
  }

  public String getContentString() {
    return new String(getContent(), getContentEncoding());
  }

  /**
   * Get the underlying content stream, bypassing the caching mechanisms that allow it to be read
   * again.
   */
  public InputStream consumeContentStream() {
    return content;
  }
}

@@ -1368,24 +1368,51 @@
 il.HashMap;%0A
+import java.util.Iterator;%0A
 import java.
@@ -1446,16 +1446,52 @@
 Objects;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aclass 
@@ -2395,84 +2395,8 @@
 ) %7B%0A
-    return headers.get(name);%0A  %7D%0A%0A  public String getHeader(String name) %7B%0A
     
@@ -2429,16 +2429,16 @@
 tream()%0A
+
         
@@ -2596,41 +2596,337 @@
    .
-findFirst()%0A        .orElse(
+collect(Collectors.toList());%0A  %7D%0A%0A  public String getHeader(String name) %7B%0A    Iterable%3CString%3E initialHeaders = getHeaders(name);%0A    if (initialHeaders == null) %7B%0A      return null;%0A    %7D%0A%0A    Iterator%3CString%3E headers = initialHeaders.iterator();%0A    if (headers.hasNext()) %7B%0A      return headers.next();%0A    %7D%0A    return 
 null
-)
 ;%0A  
@@ -3005,35 +3005,33 @@
 (name);%0A    
-h
+addH
 eader
-s.put
 (name, value

Tidy up getting headers in HttpMessage