Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/B4921848.java
41149 views
1
/*
2
* Copyright (c) 2003, 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 4921848
27
* @library /test/lib
28
* @run main/othervm -Dhttp.auth.preference=basic B4921848
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true
30
* -Dhttp.auth.preference=basic B4921848
31
* @summary Allow user control over authentication schemes
32
*/
33
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.PrintWriter;
37
import java.net.Authenticator;
38
import java.net.InetAddress;
39
import java.net.InetSocketAddress;
40
import java.net.PasswordAuthentication;
41
import java.net.ProxySelector;
42
import java.net.URL;
43
import java.net.URLConnection;
44
import java.util.concurrent.Executors;
45
46
import com.sun.net.httpserver.HttpExchange;
47
import com.sun.net.httpserver.HttpHandler;
48
import com.sun.net.httpserver.HttpServer;
49
import jdk.test.lib.net.URIBuilder;
50
51
public class B4921848 implements HttpHandler {
52
53
static int count = 0;
54
55
public void handle (HttpExchange req) {
56
try {
57
if (count == 0 ) {
58
req.getResponseHeaders().set("Connection", "close");
59
req.getResponseHeaders().add("WWW-Authenticate", "Basic realm=\"foo\"");
60
req.getResponseHeaders().add("WWW-Authenticate", "Digest realm=\"bar\" domain=/biz nonce=\"hereisanonce\"");
61
req.sendResponseHeaders(401, -1);
62
} else {
63
String authheader = req.getRequestHeaders().get("Authorization").get(0);
64
if (authheader.startsWith ("Basic")) {
65
req.sendResponseHeaders(200, 0);
66
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
67
pw.print("Hello .");
68
}
69
} else {
70
req.sendResponseHeaders(400, -1);
71
}
72
}
73
count ++;
74
} catch (IOException e) {
75
e.printStackTrace();
76
}
77
}
78
79
static void read (InputStream is) throws IOException {
80
int c;
81
System.out.println ("reading");
82
while ((c=is.read()) != -1) {
83
System.out.write (c);
84
}
85
System.out.println ("");
86
System.out.println ("finished reading");
87
}
88
89
90
static void client (String u) throws Exception {
91
URL url = new URL (u);
92
System.out.println ("client opening connection to: " + u);
93
URLConnection urlc = url.openConnection ();
94
InputStream is = urlc.getInputStream ();
95
read (is);
96
is.close();
97
}
98
99
static HttpServer server;
100
101
public static void main (String[] args) throws Exception {
102
B4921848 b4921848 = new B4921848();
103
MyAuthenticator auth = new MyAuthenticator ();
104
Authenticator.setDefault (auth);
105
ProxySelector.setDefault(ProxySelector.of(null)); // no proxy
106
try {
107
InetAddress loopback = InetAddress.getLoopbackAddress();
108
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
109
server.createContext("/", b4921848);
110
server.setExecutor(Executors.newSingleThreadExecutor());
111
server.start();
112
String serverURL = URIBuilder.newBuilder()
113
.scheme("http")
114
.loopback()
115
.port(server.getAddress().getPort())
116
.path("/")
117
.build()
118
.toString();
119
System.out.println("Server: listening at: " + serverURL);
120
client(serverURL + "d1/d2/d3/foo.html");
121
} catch (Exception e) {
122
if (server != null) {
123
server.stop(1);
124
}
125
throw e;
126
}
127
server.stop(1);
128
}
129
130
public static void except (String s) {
131
server.stop(1);
132
throw new RuntimeException (s);
133
}
134
135
static class MyAuthenticator extends Authenticator {
136
MyAuthenticator () {
137
super ();
138
}
139
140
public PasswordAuthentication getPasswordAuthentication () {
141
return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
142
}
143
144
}
145
146
}
147
148