src/jdk.httpserver/share/classes/com/sun/net/httpserver/Headers.java
author jwilhelm
Fri, 14 Jun 2019 03:50:25 +0200
changeset 55409 c53db49c7a2f
parent 47216 71c04702a3d5
permissions -rw-r--r--
Added tag jdk-13+25 for changeset 22b3b7983ada
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * HTTP request and response headers are represented by this class which implements
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    32
 * the interface
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    33
 * {@link java.util.Map}{@literal <}{@link java.lang.String}, {@link java.util.List}
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    34
 * {@literal <}{@link java.lang.String}{@literal >>}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The keys are case-insensitive Strings representing the header names and
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    36
 * the value associated with each key is
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    37
 * a {@link List}{@literal <}{@link String}{@literal >} with one
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20742
diff changeset
    38
 * element for each occurrence of the header name in the request or response.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    40
 * For example, if a response header instance contains
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    41
 * one key "HeaderName" with two values "value1 and value2"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * then this object is output as two header lines:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * HeaderName: value1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * HeaderName: value2
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 25859
diff changeset
    46
 * </pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * All the normal {@link java.util.Map} methods are provided, but the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * additional convenience methods are most likely to be used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <li>{@link #getFirst(String)} returns a single valued header or the first value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * a multi-valued header.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <li>{@link #add(String,String)} adds the given header value to the list for the given key</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <li>{@link #set(String,String)} sets the given header field to the single value given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * overwriting any existing values in the value list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * </ul><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * All methods in this class accept <code>null</code> values for keys and values. However, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * keys will never will be present in HTTP request headers, and will not be output/sent in response headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Null values can be represented as either a null entry for the key (i.e. the list is null) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * where the key has a list, but one (or more) of the list's values is null. Null values are output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * as a header line containing the key but no associated value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class Headers implements Map<String,List<String>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        HashMap<String,List<String>> map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        public Headers () {map = new HashMap<String,List<String>>(32);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        /* Normalize the key by converting to following form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         * First char upper case, rest lower case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * key is presumed to be ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        private String normalize (String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            int len = key.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
6906
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    82
            char[] b = key.toCharArray();
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    83
            if (b[0] >= 'a' && b[0] <= 'z') {
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    84
                b[0] = (char)(b[0] - ('a' - 'A'));
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    85
            }
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    86
            for (int i=1; i<len; i++) {
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    87
                if (b[i] >= 'A' && b[i] <= 'Z') {
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    88
                    b[i] = (char) (b[i] + ('a' - 'A'));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
6906
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    90
            }
d57020b6118f 6992545: FindBugs scan - Malicious code vulnerability Warnings in com.sun.net.httpserver.HttpsParameters.*
chegar
parents: 5506
diff changeset
    91
            return new String(b);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public int size() {return map.size();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public boolean isEmpty() {return map.isEmpty();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (!(key instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return map.containsKey (normalize((String)key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return map.containsValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        public List<String> get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return map.get(normalize((String)key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * returns the first value from the List of String values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * for the given key (if at least one exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * @param key the key to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * @return the first string value associated with the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public String getFirst (String key) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   123
            List<String> l = map.get(normalize(key));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return l.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        public List<String> put(String key, List<String> value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return map.put (normalize(key), value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * adds the given value to the list of headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         * for the given key. If the mapping does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         * already exist, then it is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
         * @param key the header name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         * @param value the header value to add to the header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public void add (String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            String k = normalize(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            List<String> l = map.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                l = new LinkedList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                map.put(k,l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            l.add (value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * sets the given value as the sole header value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         * for the given key. If the mapping does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         * already exist, then it is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * @param key the header name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         * @param value the header value to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        public void set (String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            LinkedList<String> l = new LinkedList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            l.add (value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            put (key, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        public List<String> remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return map.remove(normalize((String)key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        public void putAll(Map<? extends String,? extends List<String>> t)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            map.putAll (t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public void clear() {map.clear();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        public Set<String> keySet() {return map.keySet();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        public Collection<List<String>> values() {return map.values();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public Set<Map.Entry<String, List<String>>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return map.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        public boolean equals(Object o) {return map.equals(o);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        public int hashCode() {return map.hashCode();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }