1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2025, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *    or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.jetty;
15  
16  import org.eclipse.jetty.http.HttpField;
17  import org.eclipse.jetty.http.HttpFields;
18  
19  import java.util.Map;
20  import java.util.TreeMap;
21  
22  /**
23   * <p>A utility class that builds a map from HttpFields (headers).</p>
24   *
25   * <p>In case a homonymous header exists, it is merged into the existing value by concatenation. </p>
26   *
27   * @author Ceki G&uuml;lc&uuml;
28   * @author Robert Elliot
29   * @since 2.0.7
30   */
31  class HeaderUtil {
32      static Map<String, String> buildHeaderMap(HttpFields headers) {
33          Map<String, String> requestHeaderMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
34          for (HttpField httpField : headers) {
35              String existing = requestHeaderMap.get(httpField.getName());
36              String value = combine(existing, httpField.getValue());
37              requestHeaderMap.put(httpField.getName(), value);
38          }
39          return requestHeaderMap;
40      }
41  
42      private static String combine(String existing, String field) {
43          if (existing == null) {
44              return field;
45          } else {
46              return existing + "," + field;
47          }
48      }
49  }