Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Authenticator/BasicTest4.java
41149 views
1
/*
2
* Copyright (c) 2002, 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
import java.io.*;
25
import java.net.*;
26
import java.util.*;
27
import jdk.test.lib.net.URIBuilder;
28
29
/**
30
* @test
31
* @bug 4623722
32
* @summary performance hit for Basic Authentication
33
* @library /test/lib
34
* @run main/othervm BasicTest4
35
* @run main/othervm -Djava.net.preferIPv6Addresses=true BasicTest4
36
*/
37
38
public class BasicTest4 {
39
40
static class BasicServer extends Thread {
41
42
ServerSocket server;
43
44
Socket s;
45
InputStream is;
46
OutputStream os;
47
48
static final String realm = "wallyworld";
49
50
String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
51
"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n\r\n";
52
53
String reply2 = "HTTP/1.1 200 OK\r\n"+
54
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
55
"Server: Apache/1.3.14 (Unix)\r\n" +
56
"Connection: close\r\n" +
57
"Content-Type: text/html; charset=iso-8859-1\r\n" +
58
"Content-Length: 10\r\n\r\n";
59
60
BasicServer (ServerSocket s) {
61
server = s;
62
}
63
64
static boolean checkFor (InputStream in, char[] seq) throws IOException {
65
System.out.println ("checkfor");
66
StringBuilder message = new StringBuilder();
67
try {
68
int i=0, count=0;
69
while (true) {
70
int c = in.read();
71
if (c == -1) {
72
System.out.println(new String(seq) + " not found in \n<<"
73
+ message + ">>");
74
return false;
75
}
76
message.append((char)c);
77
count++;
78
if (c == seq[i]) {
79
i++;
80
if (i == seq.length)
81
return true;
82
continue;
83
} else {
84
i = 0;
85
}
86
}
87
}
88
catch (SocketTimeoutException e) {
89
System.out.println("checkFor: " + e);
90
return false;
91
}
92
}
93
94
boolean success = false;
95
96
void readAll (Socket s) throws IOException {
97
byte[] buf = new byte [128];
98
InputStream is = s.getInputStream ();
99
s.setSoTimeout(1000);
100
try {
101
while (is.read(buf) > 0) ;
102
} catch (SocketTimeoutException x) { }
103
}
104
105
public void run () {
106
try {
107
System.out.println ("Server 1: accept");
108
s = server.accept ();
109
readAll (s);
110
System.out.println ("accepted");
111
os = s.getOutputStream();
112
os.write (reply1.getBytes());
113
s.close ();
114
115
System.out.println ("Server 2: accept");
116
s = server.accept ();
117
readAll (s);
118
System.out.println ("accepted");
119
os = s.getOutputStream();
120
os.write ((reply2+"HelloWorld").getBytes());
121
s.close ();
122
123
/* Second request now */
124
125
System.out.println ("Server 3: accept");
126
s = server.accept ();
127
readAll (s);
128
System.out.println ("accepted");
129
os = s.getOutputStream();
130
os.write (reply1.getBytes());
131
s.close ();
132
133
System.out.println ("Server 4: accept");
134
s = server.accept ();
135
readAll (s);
136
System.out.println ("accepted");
137
os = s.getOutputStream();
138
os.write ((reply2+"HelloAgain").getBytes());
139
s.close ();
140
141
/* Third request now */
142
143
/* This should include pre-emptive authorization */
144
145
System.out.println ("Server 5: accept");
146
s = server.accept ();
147
s.setSoTimeout (1000);
148
System.out.println ("accepted");
149
InputStream is = s.getInputStream ();
150
success = checkFor (is, "Authorization".toCharArray());
151
System.out.println ("checkfor returned " + success);
152
readAll (s);
153
os = s.getOutputStream();
154
os.write (reply2.getBytes());
155
s.close ();
156
157
if (success)
158
return;
159
160
System.out.println ("Server 6: accept");
161
s = server.accept ();
162
System.out.println ("accepted");
163
os = s.getOutputStream();
164
readAll (s);
165
os.write ((reply2+"HelloAgain").getBytes());
166
s.close ();
167
}
168
catch (Exception e) {
169
System.out.println (e);
170
}
171
finished ();
172
}
173
174
public synchronized void finished () {
175
notifyAll();
176
}
177
178
}
179
180
static class MyAuthenticator extends Authenticator {
181
MyAuthenticator () {
182
super ();
183
}
184
185
public PasswordAuthentication getPasswordAuthentication ()
186
{
187
System.out.println ("Auth called");
188
return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
189
}
190
191
}
192
193
194
static void read (InputStream is) throws IOException {
195
int c;
196
System.out.println ("reading");
197
while ((c=is.read()) != -1) {
198
System.out.write (c);
199
}
200
System.out.println ("");
201
System.out.println ("finished reading");
202
}
203
204
public static void main (String args[]) throws Exception {
205
MyAuthenticator auth = new MyAuthenticator ();
206
Authenticator.setDefault (auth);
207
InetAddress loopback = InetAddress.getLoopbackAddress();
208
ServerSocket ss = new ServerSocket();
209
ss.bind(new InetSocketAddress(loopback, 0));
210
int port = ss.getLocalPort ();
211
BasicServer server = new BasicServer (ss);
212
synchronized (server) {
213
server.start();
214
System.out.println ("client 1");
215
String base = URIBuilder.newBuilder()
216
.scheme("http")
217
.loopback()
218
.port(port)
219
.path("/d1/")
220
.build()
221
.toString();
222
System.out.println("Base URL: " + base);
223
URL url = new URL (base + "d3/foo.html");
224
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
225
InputStream is = urlc.getInputStream ();
226
read (is);
227
System.out.println ("client 2");
228
url = new URL (base + "d2/bar.html");
229
urlc = url.openConnection(Proxy.NO_PROXY);
230
is = urlc.getInputStream ();
231
System.out.println ("client 3");
232
url = new URL (base + "d4/foobar.html");
233
urlc = url.openConnection(Proxy.NO_PROXY);
234
is = urlc.getInputStream ();
235
read (is);
236
server.wait ();
237
if (!server.success) {
238
throw new RuntimeException ("3rd request did not use pre-emptive authorization");
239
}
240
}
241
}
242
}
243
244