Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/net/httpserver/Headers.java
38922 views
1
/*
2
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.sun.net.httpserver;
27
28
import java.util.*;
29
30
/**
31
* HTTP request and response headers are represented by this class which implements
32
* the interface {@link java.util.Map}<
33
* {@link java.lang.String},{@link java.util.List}<{@link java.lang.String}>>.
34
* The keys are case-insensitive Strings representing the header names and
35
* the value associated with each key is a {@link List}<{@link String}> with one
36
* element for each occurrence of the header name in the request or response.
37
* <p>
38
* For example, if a response header instance contains one key "HeaderName" with two values "value1 and value2"
39
* then this object is output as two header lines:
40
* <blockquote><pre>
41
* HeaderName: value1
42
* HeaderName: value2
43
* </blockquote></pre>
44
* <p>
45
* All the normal {@link java.util.Map} methods are provided, but the following
46
* additional convenience methods are most likely to be used:
47
* <ul>
48
* <li>{@link #getFirst(String)} returns a single valued header or the first value of
49
* a multi-valued header.</li>
50
* <li>{@link #add(String,String)} adds the given header value to the list for the given key</li>
51
* <li>{@link #set(String,String)} sets the given header field to the single value given
52
* overwriting any existing values in the value list.
53
* </ul><p>
54
* All methods in this class accept <code>null</code> values for keys and values. However, null
55
* keys will never will be present in HTTP request headers, and will not be output/sent in response headers.
56
* Null values can be represented as either a null entry for the key (i.e. the list is null) or
57
* where the key has a list, but one (or more) of the list's values is null. Null values are output
58
* as a header line containing the key but no associated value.
59
* @since 1.6
60
*/
61
@jdk.Exported
62
public class Headers implements Map<String,List<String>> {
63
64
HashMap<String,List<String>> map;
65
66
public Headers () {map = new HashMap<String,List<String>>(32);}
67
68
/* Normalize the key by converting to following form.
69
* First char upper case, rest lower case.
70
* key is presumed to be ASCII
71
*/
72
private String normalize (String key) {
73
if (key == null) {
74
return null;
75
}
76
int len = key.length();
77
if (len == 0) {
78
return key;
79
}
80
char[] b = key.toCharArray();
81
if (b[0] >= 'a' && b[0] <= 'z') {
82
b[0] = (char)(b[0] - ('a' - 'A'));
83
} else if (b[0] == '\r' || b[0] == '\n')
84
throw new IllegalArgumentException("illegal character in key");
85
86
for (int i=1; i<len; i++) {
87
if (b[i] >= 'A' && b[i] <= 'Z') {
88
b[i] = (char) (b[i] + ('a' - 'A'));
89
} else if (b[i] == '\r' || b[i] == '\n')
90
throw new IllegalArgumentException("illegal character in key");
91
}
92
return new String(b);
93
}
94
95
public int size() {return map.size();}
96
97
public boolean isEmpty() {return map.isEmpty();}
98
99
public boolean containsKey(Object key) {
100
if (key == null) {
101
return false;
102
}
103
if (!(key instanceof String)) {
104
return false;
105
}
106
return map.containsKey (normalize((String)key));
107
}
108
109
public boolean containsValue(Object value) {
110
return map.containsValue(value);
111
}
112
113
public List<String> get(Object key) {
114
return map.get(normalize((String)key));
115
}
116
117
/**
118
* returns the first value from the List of String values
119
* for the given key (if at least one exists).
120
* @param key the key to search for
121
* @return the first string value associated with the key
122
*/
123
public String getFirst (String key) {
124
List<String> l = map.get(normalize(key));
125
if (l == null) {
126
return null;
127
}
128
return l.get(0);
129
}
130
131
public List<String> put(String key, List<String> value) {
132
for (String v : value)
133
checkValue(v);
134
return map.put (normalize(key), value);
135
}
136
137
/**
138
* adds the given value to the list of headers
139
* for the given key. If the mapping does not
140
* already exist, then it is created
141
* @param key the header name
142
* @param value the header value to add to the header
143
*/
144
public void add (String key, String value) {
145
checkValue(value);
146
String k = normalize(key);
147
List<String> l = map.get(k);
148
if (l == null) {
149
l = new LinkedList<String>();
150
map.put(k,l);
151
}
152
l.add (value);
153
}
154
155
private static void checkValue(String value) {
156
int len = value.length();
157
for (int i=0; i<len; i++) {
158
char c = value.charAt(i);
159
if (c == '\r') {
160
// is allowed if it is followed by \n and a whitespace char
161
if (i >= len - 2) {
162
throw new IllegalArgumentException("Illegal CR found in header");
163
}
164
char c1 = value.charAt(i+1);
165
char c2 = value.charAt(i+2);
166
if (c1 != '\n') {
167
throw new IllegalArgumentException("Illegal char found after CR in header");
168
}
169
if (c2 != ' ' && c2 != '\t') {
170
throw new IllegalArgumentException("No whitespace found after CRLF in header");
171
}
172
i+=2;
173
} else if (c == '\n') {
174
throw new IllegalArgumentException("Illegal LF found in header");
175
}
176
}
177
}
178
179
/**
180
* sets the given value as the sole header value
181
* for the given key. If the mapping does not
182
* already exist, then it is created
183
* @param key the header name
184
* @param value the header value to set.
185
*/
186
public void set (String key, String value) {
187
LinkedList<String> l = new LinkedList<String>();
188
l.add (value);
189
put (key, l);
190
}
191
192
193
public List<String> remove(Object key) {
194
return map.remove(normalize((String)key));
195
}
196
197
public void putAll(Map<? extends String,? extends List<String>> t) {
198
map.putAll (t);
199
}
200
201
public void clear() {map.clear();}
202
203
public Set<String> keySet() {return map.keySet();}
204
205
public Collection<List<String>> values() {return map.values();}
206
207
public Set<Map.Entry<String, List<String>>> entrySet() {
208
return map.entrySet();
209
}
210
211
public boolean equals(Object o) {return map.equals(o);}
212
213
public int hashCode() {return map.hashCode();}
214
}
215
216