Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/EmptyAuthenticate.java
41149 views
1
/*
2
* Copyright (c) 2021, 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 8263899
27
* @summary HttpClient throws NPE in AuthenticationFilter when parsing www-authenticate head
28
*
29
* @run main/othervm EmptyAuthenticate
30
*/
31
import com.sun.net.httpserver.HttpServer;
32
import java.io.IOException;
33
import java.io.OutputStream;
34
import java.net.InetSocketAddress;
35
import java.net.URI;
36
import java.net.URISyntaxException;
37
import java.net.http.HttpClient;
38
import java.net.http.HttpRequest;
39
import java.net.http.HttpResponse;
40
41
public class EmptyAuthenticate {
42
43
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
44
int port = 0;
45
46
//start server:
47
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
48
port = server.getAddress().getPort();
49
server.createContext("/", exchange -> {
50
String response = "test body";
51
//this empty header will make the HttpClient throw NPE
52
exchange.getResponseHeaders().add("www-authenticate", "");
53
exchange.sendResponseHeaders(401, response.length());
54
OutputStream os = exchange.getResponseBody();
55
os.write(response.getBytes());
56
os.close();
57
});
58
server.start();
59
60
HttpResponse<String> response = null;
61
//run client:
62
try {
63
HttpClient httpClient = HttpClient.newHttpClient();
64
HttpRequest request = HttpRequest.newBuilder(new URI("http://localhost:" + port + "/")).GET().build();
65
//this line will throw NPE (wrapped by IOException) when parsing empty www-authenticate response header in AuthenticationFilter:
66
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
67
boolean ok = !response.headers().firstValue("WWW-Authenticate").isEmpty();
68
if (!ok) {
69
throw new RuntimeException("WWW-Authenicate missing");
70
}
71
} catch (IOException e) {
72
e.printStackTrace();
73
throw new RuntimeException("Test failed");
74
} finally {
75
server.stop(0);
76
}
77
}
78
}
79
80