Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/B8185898.java
38841 views
1
/*
2
* Copyright (c) 2019, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 8185898
27
* @library /lib/testlibrary
28
* @run main/othervm B8185898
29
* @summary setRequestProperty(key, null) results in HTTP header without colon in request
30
*/
31
32
import java.io.*;
33
import java.net.*;
34
import java.util.Arrays;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.HashMap;
38
import java.util.concurrent.ExecutorService;
39
import java.util.concurrent.Executors;
40
import java.util.stream.Collectors;
41
import java.util.Collections;
42
43
import jdk.testlibrary.net.URIBuilder;
44
import sun.net.www.MessageHeader;
45
import com.sun.net.httpserver.HttpContext;
46
import com.sun.net.httpserver.HttpExchange;
47
import com.sun.net.httpserver.HttpHandler;
48
import com.sun.net.httpserver.HttpServer;
49
50
import static java.nio.charset.StandardCharsets.ISO_8859_1;
51
import static java.nio.charset.StandardCharsets.UTF_8;
52
53
/*
54
* Test checks that MessageHeader with key != null and value == null is set correctly
55
* and printed according to HTTP standard in the format <key>: <value>
56
* */
57
public class B8185898 {
58
59
static HttpServer server;
60
static final String RESPONSE_BODY = "Test response body";
61
static final String H1 = "X-header1";
62
static final String H2 = "X-header2";
63
static final String VALUE = "This test value should appear";
64
static final List<String> oneList = Arrays.asList(VALUE);
65
static final List<String> zeroList = Arrays.asList("");
66
static int port;
67
static URL url;
68
static volatile Map<String, List<String>> headers;
69
70
static class Handler implements HttpHandler {
71
72
public void handle(HttpExchange t) throws IOException {
73
InputStream is = t.getRequestBody();
74
InetSocketAddress rem = t.getRemoteAddress();
75
headers = t.getRequestHeaders(); // Get request headers on the server side
76
while(is.read() != -1){}
77
is.close();
78
79
OutputStream os = t.getResponseBody();
80
t.sendResponseHeaders(200, RESPONSE_BODY.length());
81
os.write(RESPONSE_BODY.getBytes(UTF_8));
82
t.close();
83
}
84
}
85
86
public static void main(String[] args) throws Exception {
87
ExecutorService exec = Executors.newCachedThreadPool();
88
InetAddress loopback = InetAddress.getLoopbackAddress();
89
90
try {
91
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
92
server = HttpServer.create(addr, 100);
93
HttpHandler handler = new Handler();
94
HttpContext context = server.createContext("/", handler);
95
server.setExecutor(exec);
96
server.start();
97
98
port = server.getAddress().getPort();
99
System.out.println("Server on port: " + port);
100
url = URIBuilder.newBuilder()
101
.scheme("http")
102
.loopback()
103
.port(port)
104
.path("/foo")
105
.toURLUnchecked();
106
System.out.println("URL: " + url);
107
testMessageHeader();
108
testMessageHeaderMethods();
109
testURLConnectionMethods();
110
} finally {
111
server.stop(0);
112
System.out.println("After server shutdown");
113
exec.shutdown();
114
}
115
}
116
117
// Test message header with malformed message header and fake request line
118
static void testMessageHeader() {
119
final String badHeader = "This is not a request line for HTTP/1.1";
120
final String fakeRequestLine = "This /is/a/fake/status/line HTTP/2.0";
121
final String expectedHeaders = fakeRequestLine + "\r\n"
122
+ H1 + ": " + VALUE + "\r\n"
123
+ H2 + ": " + VALUE + "\r\n"
124
+ badHeader + ":\r\n\r\n";
125
126
MessageHeader header = new MessageHeader();
127
header.add(H1, VALUE);
128
header.add(H2, VALUE);
129
header.add(badHeader, null);
130
header.prepend(fakeRequestLine, null);
131
ByteArrayOutputStream out = new ByteArrayOutputStream();
132
header.print(new PrintStream(out));
133
134
if (!out.toString().equals(expectedHeaders)) {
135
throw new AssertionError("FAILED: expected: "
136
+ expectedHeaders + "\nReceived: " + out.toString());
137
} else {
138
System.out.println("PASSED: ::print returned correct "
139
+ "status line and headers:\n" + out.toString());
140
}
141
}
142
143
// Test MessageHeader::print, ::toString, implicitly testing that
144
// MessageHeader::mergeHeader formats headers correctly for responses
145
static void testMessageHeaderMethods() throws IOException {
146
// {{inputString1, expectedToString1, expectedPrint1}, {...}}
147
String[][] strings = {
148
{"HTTP/1.1 200 OK\r\n"
149
+ "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
150
+ "Connection: keep-alive\r\n"
151
+ "Host: 127.0.0.1:12345\r\n"
152
+ "User-agent: Java/12\r\n\r\nfoooo",
153
"pairs: {null: HTTP/1.1 200 OK}"
154
+ "{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}"
155
+ "{Connection: keep-alive}"
156
+ "{Host: 127.0.0.1:12345}"
157
+ "{User-agent: Java/12}",
158
"Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
159
+ "Connection: keep-alive\r\n"
160
+ "Host: 127.0.0.1:12345\r\n"
161
+ "User-agent: Java/12\r\n\r\n"},
162
{"HTTP/1.1 200 OK\r\n"
163
+ "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
164
+ "Connection: keep-alive\r\n"
165
+ "Host: 127.0.0.1:12345\r\n"
166
+ "User-agent: Java/12\r\n"
167
+ "X-Header:\r\n\r\n",
168
"pairs: {null: HTTP/1.1 200 OK}"
169
+ "{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}"
170
+ "{Connection: keep-alive}"
171
+ "{Host: 127.0.0.1:12345}"
172
+ "{User-agent: Java/12}"
173
+ "{X-Header: }",
174
"Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
175
+ "Connection: keep-alive\r\n"
176
+ "Host: 127.0.0.1:12345\r\n"
177
+ "User-agent: Java/12\r\n"
178
+ "X-Header: \r\n\r\n"},
179
};
180
181
System.out.println("Test custom message headers");
182
for (String[] s : strings) {
183
// Test MessageHeader::toString
184
MessageHeader header = new MessageHeader(
185
new ByteArrayInputStream(s[0].getBytes(ISO_8859_1)));
186
if (!header.toString().endsWith(s[1])) {
187
throw new AssertionError("FAILED: expected: "
188
+ s[1] + "\nReceived: " + header);
189
} else {
190
System.out.println("PASSED: ::toString returned correct "
191
+ "status line and headers:\n" + header);
192
}
193
194
// Test MessageHeader::print
195
ByteArrayOutputStream out = new ByteArrayOutputStream();
196
header.print(new PrintStream(out));
197
if (!out.toString().equals(s[2])) {
198
throw new AssertionError("FAILED: expected: "
199
+ s[2] + "\nReceived: " + out.toString());
200
} else {
201
System.out.println("PASSED: ::print returned correct "
202
+ "status line and headers:\n" + out.toString());
203
}
204
}
205
}
206
207
// Test methods URLConnection::getRequestProperties,
208
// ::getHeaderField, ::getHeaderFieldKey
209
static void testURLConnectionMethods() throws IOException {
210
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
211
urlConn.setRequestProperty(H1, "");
212
urlConn.setRequestProperty(H1, VALUE);
213
urlConn.setRequestProperty(H2, null); // Expected to contain ':' between key and value
214
Map<String, List<String>> props = urlConn.getRequestProperties();
215
Map<String, List<String>> expectedMap = new HashMap<String, List<String>>();
216
expectedMap.put(H1, oneList);
217
expectedMap.put(H2, Arrays.asList((String)null));
218
219
// Test request properties
220
System.out.println("Client request properties");
221
StringBuilder sb = new StringBuilder();
222
props.forEach((k, v) -> sb.append(k + ": "
223
+ v.stream().collect(Collectors.joining()) + "\n"));
224
System.out.println(sb);
225
226
if (!props.equals(expectedMap)) {
227
throw new AssertionError("Unexpected properties returned: "
228
+ props);
229
} else {
230
System.out.println("Properties returned as expected");
231
}
232
233
// Test header fields
234
String headerField = urlConn.getHeaderField(0);
235
if (!headerField.contains("200 OK")) {
236
throw new AssertionError("Expected headerField[0]: status line. "
237
+ "Received: " + headerField);
238
} else {
239
System.out.println("PASSED: headerField[0] contains status line: "
240
+ headerField);
241
}
242
243
String headerFieldKey = urlConn.getHeaderFieldKey(0);
244
if (headerFieldKey != null) {
245
throw new AssertionError("Expected headerFieldKey[0]: null. "
246
+ "Received: " + headerFieldKey);
247
} else {
248
System.out.println("PASSED: headerFieldKey[0] is null");
249
}
250
251
// Check that test request headers are included with correct format
252
try (
253
BufferedReader in = new BufferedReader(
254
new InputStreamReader(urlConn.getInputStream()))
255
) {
256
if (!headers.keySet().contains(H1)) {
257
throw new AssertionError("Expected key not found: "
258
+ H1 + ": " + VALUE);
259
} else if (!headers.get(H1).equals(oneList)) {
260
throw new AssertionError("Unexpected key-value pair: "
261
+ H1 + ": " + headers.get(H1));
262
} else {
263
System.out.println("PASSED: " + H1 + " included in request headers");
264
}
265
266
if (!headers.keySet().contains(H2)) {
267
throw new AssertionError("Expected key not found: "
268
+ H2 + ": ");
269
// Check that empty list is returned
270
} else if (!headers.get(H2).equals(zeroList)) {
271
throw new AssertionError("Unexpected key-value pair: "
272
+ H2 + ": " + headers.get(H2));
273
} else {
274
System.out.println("PASSED: " + H2 + " included in request headers");
275
}
276
277
String inputLine;
278
while ((inputLine = in.readLine()) != null) {
279
System.out.println(inputLine);
280
}
281
}
282
}
283
}
284
285