Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.httpserver/share/classes/com/sun/net/httpserver/Headers.java
41159 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.Collection;
29
import java.util.HashMap;
30
import java.util.LinkedList;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.Set;
34
35
/**
36
* HTTP request and response headers are represented by this class which
37
* implements the interface
38
* {@link java.util.Map}{@literal <}{@link java.lang.String},
39
* {@link java.util.List} {@literal <}{@link java.lang.String}{@literal >>}.
40
* The keys are case-insensitive Strings representing the header names and
41
* the value associated with each key is
42
* a {@link List}{@literal <}{@link String}{@literal >} with one
43
* element for each occurrence of the header name in the request or response.
44
*
45
* <p> For example, if a response header instance contains
46
* one key "HeaderName" with two values "value1 and value2"
47
* then this object is output as two header lines:
48
*
49
* <blockquote><pre>
50
* HeaderName: value1
51
* HeaderName: value2
52
* </pre></blockquote>
53
*
54
* All the normal {@link java.util.Map} methods are provided, but the
55
* following additional convenience methods are most likely to be used:
56
*
57
* <ul>
58
* <li>{@link #getFirst(String)} returns a single valued header or the first
59
* value of a multi-valued header.
60
* <li>{@link #add(String,String)} adds the given header value to the list
61
* for the given key.
62
* <li>{@link #set(String,String)} sets the given header field to the single
63
* value given overwriting any existing values in the value list.
64
* </ul>
65
*
66
* <p> All methods in this class accept {@code null} values for keys and values.
67
* However, {@code null} keys will never will be present in HTTP request
68
* headers, and will not be output/sent in response headers. Null values can be
69
* represented as either a {@code null} entry for the key (i.e. the list is
70
* {@code null}) or where the key has a list, but one (or more) of the list's
71
* values is {@code null}. Null values are output as a header line containing
72
* the key but no associated value.
73
*
74
* @since 1.6
75
*/
76
public class Headers implements Map<String,List<String>> {
77
78
HashMap<String,List<String>> map;
79
80
/**
81
* Creates an empty instance of {@code Headers}.
82
*/
83
public Headers () {map = new HashMap<String,List<String>>(32);}
84
85
/**
86
* Normalize the key by converting to following form.
87
* First {@code char} upper case, rest lower case.
88
* key is presumed to be {@code ASCII}.
89
*/
90
private String normalize (String key) {
91
if (key == null) {
92
return null;
93
}
94
int len = key.length();
95
if (len == 0) {
96
return key;
97
}
98
char[] b = key.toCharArray();
99
if (b[0] >= 'a' && b[0] <= 'z') {
100
b[0] = (char)(b[0] - ('a' - 'A'));
101
} else if (b[0] == '\r' || b[0] == '\n')
102
throw new IllegalArgumentException("illegal character in key");
103
104
for (int i=1; i<len; i++) {
105
if (b[i] >= 'A' && b[i] <= 'Z') {
106
b[i] = (char) (b[i] + ('a' - 'A'));
107
} else if (b[i] == '\r' || b[i] == '\n')
108
throw new IllegalArgumentException("illegal character in key");
109
}
110
return new String(b);
111
}
112
113
public int size() {return map.size();}
114
115
public boolean isEmpty() {return map.isEmpty();}
116
117
public boolean containsKey(Object key) {
118
if (key == null) {
119
return false;
120
}
121
if (!(key instanceof String)) {
122
return false;
123
}
124
return map.containsKey (normalize((String)key));
125
}
126
127
public boolean containsValue(Object value) {
128
return map.containsValue(value);
129
}
130
131
public List<String> get(Object key) {
132
return map.get(normalize((String)key));
133
}
134
135
/**
136
* Returns the first value from the {@link List} of {@code String}
137
* values for the given key (if at least one exists).
138
*
139
* @param key the key to search for
140
* @return the first {@code String} value associated with the key
141
*/
142
public String getFirst (String key) {
143
List<String> l = map.get(normalize(key));
144
if (l == null) {
145
return null;
146
}
147
return l.get(0);
148
}
149
150
public List<String> put(String key, List<String> value) {
151
for (String v : value)
152
checkValue(v);
153
return map.put (normalize(key), value);
154
}
155
156
/**
157
* Adds the given value to the list of headers for the given key. If
158
* the mapping does not already exist, then it is created.
159
*
160
* @param key the header name
161
* @param value the value to add to the header
162
*/
163
public void add (String key, String value) {
164
checkValue(value);
165
String k = normalize(key);
166
List<String> l = map.get(k);
167
if (l == null) {
168
l = new LinkedList<String>();
169
map.put(k,l);
170
}
171
l.add (value);
172
}
173
174
private static void checkValue(String value) {
175
int len = value.length();
176
for (int i=0; i<len; i++) {
177
char c = value.charAt(i);
178
if (c == '\r') {
179
// is allowed if it is followed by \n and a whitespace char
180
if (i >= len - 2) {
181
throw new IllegalArgumentException("Illegal CR found in header");
182
}
183
char c1 = value.charAt(i+1);
184
char c2 = value.charAt(i+2);
185
if (c1 != '\n') {
186
throw new IllegalArgumentException("Illegal char found after CR in header");
187
}
188
if (c2 != ' ' && c2 != '\t') {
189
throw new IllegalArgumentException("No whitespace found after CRLF in header");
190
}
191
i+=2;
192
} else if (c == '\n') {
193
throw new IllegalArgumentException("Illegal LF found in header");
194
}
195
}
196
}
197
198
/**
199
* Sets the given value as the sole header value for the given
200
* key. If the mapping does not already exist, then it is created.
201
*
202
* @param key the header name
203
* @param value the header value to set
204
*/
205
public void set (String key, String value) {
206
LinkedList<String> l = new LinkedList<String>();
207
l.add (value);
208
put (key, l);
209
}
210
211
212
public List<String> remove(Object key) {
213
return map.remove(normalize((String)key));
214
}
215
216
public void putAll(Map<? extends String,? extends List<String>> t) {
217
map.putAll (t);
218
}
219
220
public void clear() {map.clear();}
221
222
public Set<String> keySet() {return map.keySet();}
223
224
public Collection<List<String>> values() {return map.values();}
225
226
public Set<Map.Entry<String, List<String>>> entrySet() {
227
return map.entrySet();
228
}
229
230
public boolean equals(Object o) {return map.equals(o);}
231
232
public int hashCode() {return map.hashCode();}
233
}
234
235