Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/8199849/ParamTest.java
41161 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
import java.io.*;
25
import java.net.*;
26
import java.net.http.HttpClient;
27
import java.net.http.HttpRequest;
28
import java.net.http.HttpResponse;
29
import java.util.*;
30
import java.nio.charset.StandardCharsets;
31
import jdk.test.lib.net.URIBuilder;
32
33
/**
34
* @test
35
* @bug 8199849 8235976
36
* @summary
37
* @library /test/lib
38
* @run main/othervm ParamTest
39
* @run main/othervm -Djava.net.preferIPv6Addresses=true ParamTest
40
*/
41
42
public class ParamTest {
43
44
static final String[] variants = {
45
" ,charset=utf-8",
46
" ,charset=UtF-8",
47
" ,charset=\"utF-8\"",
48
" ,charset=\"UtF-8\""
49
};
50
51
static final int LOOPS = variants.length;
52
53
volatile static boolean error = false;
54
55
static class BasicServer extends Thread {
56
57
final ServerSocket server;
58
59
Socket s;
60
InputStream is;
61
OutputStream os;
62
63
static final String realm = "wallyworld";
64
65
String reply1 = "HTTP/1.1 401 Unauthorized\r\n"+
66
"WWW-Authenticate: Basic realm=\""+realm+"\"\r\n";
67
68
String reply2 = "HTTP/1.1 200 OK\r\n"+
69
"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
70
"Server: Apache/1.3.14 (Unix)\r\n" +
71
"Connection: close\r\n" +
72
"Content-Type: text/html; charset=iso-8859-1\r\n" +
73
"Content-Length: 10\r\n\r\n";
74
75
BasicServer(ServerSocket s) {
76
server = s;
77
}
78
79
String readHeaders(Socket sock) throws IOException {
80
InputStream is = sock.getInputStream();
81
String s = "";
82
byte[] buf = new byte[1024];
83
while (!s.endsWith("\r\n\r\n")) {
84
int c = is.read(buf);
85
if (c == -1)
86
return s;
87
String f = new String(buf, 0, c, StandardCharsets.ISO_8859_1);
88
s = s + f;
89
}
90
return s;
91
}
92
93
void check(String s, int iteration) {
94
if (s.indexOf(encodedAuthString) == -1) {
95
System.err.printf("On iteration %d, wrong auth string received %s\n", iteration, s);
96
error = true;
97
} else {
98
System.err.println("check: correct auth string received");
99
}
100
}
101
102
public void run() {
103
try {
104
for (int j = 0; j < 2; j++)
105
for (int i = 0; i < LOOPS; i++) {
106
System.out.println("Server 1: accept");
107
s = server.accept();
108
readHeaders(s);
109
System.out.println("accepted");
110
os = s.getOutputStream();
111
String str = reply1 + variants[i] + "\r\n\r\n";
112
os.write(str.getBytes());
113
114
System.out.println("Server 2: accept");
115
Socket s1 = server.accept();
116
String request = readHeaders(s1);
117
check(request, i);
118
System.out.println("accepted");
119
os = s1.getOutputStream();
120
os.write((reply2 + "HelloWorld").getBytes());
121
os.flush();
122
s.close();
123
s1.close();
124
finished();
125
}
126
} catch (Exception e) {
127
System.out.println(e);
128
error = true;
129
}
130
}
131
132
public synchronized void finished() {
133
notifyAll();
134
}
135
136
}
137
138
static final String password = "Selam D\u00fcnya.";
139
140
// "user : <password above>" encoded in UTF-8 and converted to Base 64
141
142
static final String encodedAuthString = "dXNlcjpTZWxhbSBEw7xueWEu";
143
144
static class MyAuthenticator extends Authenticator {
145
MyAuthenticator() {
146
super();
147
}
148
149
public PasswordAuthentication getPasswordAuthentication()
150
{
151
System.out.println("Auth called");
152
return (new PasswordAuthentication ("user", password.toCharArray()));
153
}
154
}
155
156
157
static void read(InputStream is) throws IOException {
158
int c;
159
System.out.println("reading");
160
while ((c=is.read()) != -1) {
161
System.out.write(c);
162
}
163
System.out.println("");
164
System.out.println("finished reading");
165
}
166
167
public static void main(String args[]) throws Exception {
168
MyAuthenticator auth = new MyAuthenticator();
169
Authenticator.setDefault(auth);
170
InetAddress loopback = InetAddress.getLoopbackAddress();
171
ServerSocket ss = new ServerSocket();
172
ss.bind(new InetSocketAddress(loopback, 0));
173
int port = ss.getLocalPort();
174
BasicServer server = new BasicServer(ss);
175
synchronized (server) {
176
server.start();
177
System.out.println("client 1");
178
String base = URIBuilder.newBuilder()
179
.scheme("http")
180
.loopback()
181
.port(port)
182
.path("/")
183
.build()
184
.toString();
185
URL url = new URL(base + "d1/d2/d3/foo.html");
186
187
for (int i = 0; i < LOOPS; i++) {
188
URLConnection urlc = url.openConnection(Proxy.NO_PROXY);
189
InputStream is = urlc.getInputStream();
190
read(is);
191
System.out.println("Client: waiting for notify");
192
server.wait();
193
System.out.println("Client: continue");
194
// check if authenticator was called once (ok) or twice (not)
195
if (error) {
196
System.err.println("Error old client iteration " + i);
197
}
198
}
199
200
URI uri = url.toURI();
201
HttpClient client = HttpClient.newBuilder()
202
.authenticator(auth)
203
.proxy(ProxySelector.of(null))
204
.build();
205
206
HttpRequest request = HttpRequest
207
.newBuilder(uri)
208
.GET()
209
.build();
210
211
for (int i = 0; i < LOOPS; i++) {
212
HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
213
int status = response.statusCode();
214
if (status != 200) {
215
System.err.printf("Error new client (%d) iteration ",
216
status, i);
217
error = true;
218
} else
219
System.err.println("New client ok iteration " + i);
220
System.out.println("New Client: waiting for notify");
221
server.wait();
222
System.out.println("New Client: continue");
223
}
224
225
if (error) {
226
throw new RuntimeException("Test failed");
227
}
228
}
229
}
230
}
231
232