Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/B4962064.java
41149 views
1
/*
2
* Copyright (c) 2004, 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 4962064
27
* @run main/othervm B4962064
28
* @run main/othervm -Djava.net.preferIPv6Addresses=true B4962064
29
* @summary Extend Authenticator to provide access to request URI and server/proxy
30
*/
31
32
import java.io.IOException;
33
import java.io.InputStream;
34
import java.io.PrintWriter;
35
import java.net.Authenticator;
36
import java.net.InetAddress;
37
import java.net.InetSocketAddress;
38
import java.net.PasswordAuthentication;
39
import java.net.URL;
40
import java.net.URLConnection;
41
import java.util.concurrent.Executors;
42
43
import com.sun.net.httpserver.HttpExchange;
44
import com.sun.net.httpserver.HttpHandler;
45
import com.sun.net.httpserver.HttpServer;
46
47
public class B4962064 implements HttpHandler {
48
49
static int count = 0;
50
51
public void handle (HttpExchange req) {
52
try {
53
switch (count) {
54
case 0:
55
req.getResponseHeaders().set("Connection", "close");
56
req.getResponseHeaders().add("WWW-Authenticate", "Basic realm=\"foo\"");
57
req.sendResponseHeaders(401, -1);
58
break;
59
case 1:
60
case 3:
61
req.sendResponseHeaders(200, 0);
62
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
63
pw.print("Hello .");
64
}
65
break;
66
case 2:
67
req.getResponseHeaders().set("Connection", "close");
68
req.getResponseHeaders().add("Proxy-Authenticate", "Basic realm=\"foo\"");
69
req.sendResponseHeaders(407, -1);
70
break;
71
}
72
count ++;
73
} catch (IOException e) {
74
e.printStackTrace();
75
}
76
}
77
78
static void read (InputStream is) throws IOException {
79
int c;
80
System.out.println ("reading");
81
while ((c=is.read()) != -1) {
82
System.out.write (c);
83
}
84
System.out.println ("");
85
System.out.println ("finished reading");
86
}
87
88
89
static void client (String u) throws Exception {
90
URL url = new URL (u);
91
System.out.println ("client opening connection to: " + u);
92
URLConnection urlc = url.openConnection ();
93
InputStream is = urlc.getInputStream ();
94
read (is);
95
is.close();
96
}
97
98
static HttpServer server;
99
static URL urlsave;
100
101
public static void main (String[] args) throws Exception {
102
B4962064 b4962064 = new B4962064();
103
try {
104
InetAddress address = InetAddress.getLoopbackAddress();
105
InetAddress resolved = InetAddress.getByName(address.getHostName());
106
System.out.println("Lookup: " + address + " -> \""
107
+ address.getHostName() + "\" -> "
108
+ resolved);
109
server = HttpServer.create(new InetSocketAddress(address, 0), 10);
110
server.createContext("/", b4962064);
111
server.setExecutor(Executors.newSingleThreadExecutor());
112
server.start();
113
int port = server.getAddress().getPort();
114
String proxyHost = address.equals(resolved)
115
? address.getHostName()
116
: address.getHostAddress();
117
System.setProperty ("http.proxyHost", proxyHost);
118
System.setProperty ("http.proxyPort", Integer.toString (port));
119
MyAuthenticator auth = new MyAuthenticator ();
120
Authenticator.setDefault (auth);
121
System.out.println ("Server started: listening on port: " + port);
122
String s = new String ("http://foo.com/d1/d2/d3/foo.html");
123
urlsave = new URL (s);
124
client (s);
125
s = new String ("http://bar.com/dr/d3/foo.html");
126
urlsave = new URL (s);
127
client (s);
128
} catch (Exception e) {
129
if (server != null) {
130
server.stop(1);
131
}
132
throw e;
133
}
134
server.stop(1);
135
}
136
137
public static void except (String s) {
138
server.stop(1);
139
throw new RuntimeException (s);
140
}
141
142
static class MyAuthenticator extends Authenticator {
143
int count = 0;
144
MyAuthenticator () {
145
super ();
146
}
147
148
public PasswordAuthentication getPasswordAuthentication () {
149
URL url = getRequestingURL ();
150
if (!url.equals (urlsave)) {
151
except ("urls not equal");
152
}
153
Authenticator.RequestorType expected;
154
if (count == 0) {
155
expected = Authenticator.RequestorType.SERVER;
156
} else {
157
expected = Authenticator.RequestorType.PROXY;
158
}
159
if (getRequestorType() != expected) {
160
except ("wrong authtype");
161
}
162
count ++;
163
return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
164
}
165
166
}
167
168
}
169
170