Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/B5017051.java
41159 views
1
/*
2
* Copyright (c) 2005, 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
/*
25
* @test
26
* @bug 5017051 6360774
27
* @modules jdk.httpserver
28
* @library /test/lib
29
* @run main/othervm B5017051
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true B5017051
31
* @summary Tests CR 5017051 & 6360774
32
*/
33
34
import java.net.*;
35
import java.util.*;
36
import java.io.*;
37
import com.sun.net.httpserver.*;
38
import java.util.concurrent.Executors;
39
import java.util.concurrent.ExecutorService;
40
import jdk.test.lib.net.URIBuilder;
41
42
/*
43
* Part 1:
44
* First request sent to the http server will not have an "Authorization" header set and
45
* the server will respond with a 401, but not until it has set a cookie in the response
46
* headers. The subsequent request ( comes from HttpURLConnection's authentication retry )
47
* will have the appropriate Authorization header and the servers context handler will be
48
* invoked. The test passes only if the client (HttpURLConnection) has sent the cookie
49
* in its second request that had been set via the first response from the server.
50
*
51
* Part 2:
52
* Preload the CookieManager with a cookie. Make a http request that requires authentication
53
* The cookie will be sent in the first request (without the Authorization header), the
54
* server will respond with a 401 (from MyBasicAuthFilter) and the client will add the
55
* appropriate Authorization header. This tests ensures that there is only one Cookie header
56
* in the request that actually makes it to the Http servers context handler.
57
*/
58
59
public class B5017051
60
{
61
HttpServer httpServer;
62
ExecutorService executorService;
63
64
public static void main(String[] args) throws Exception {
65
new B5017051();
66
}
67
68
public B5017051() throws Exception {
69
startHttpServer();
70
doClient();
71
}
72
73
void doClient() throws Exception {
74
java.net.Authenticator.setDefault(new MyAuthenticator());
75
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
76
ProxySelector.setDefault(ProxySelector.of(null));
77
78
try {
79
InetSocketAddress address = httpServer.getAddress();
80
81
// Part 1
82
URL url = URIBuilder.newBuilder()
83
.scheme("http")
84
.host(address.getAddress())
85
.port(address.getPort())
86
.path("/test/")
87
.toURL();
88
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
89
int resp = uc.getResponseCode();
90
if (resp != 200)
91
throw new RuntimeException("Failed: Part 1, Response code is not 200: " + resp);
92
93
System.out.println("Response code from Part 1 = 200 OK");
94
95
// Part 2
96
URL url2 = URIBuilder.newBuilder()
97
.scheme("http")
98
.host(address.getAddress())
99
.port(address.getPort())
100
.path("/test2/")
101
.toURL();
102
103
// can use the global CookieHandler used for the first test as the URL's are different
104
CookieHandler ch = CookieHandler.getDefault();
105
Map<String,List<String>> header = new HashMap<String,List<String>>();
106
List<String> values = new LinkedList<String>();
107
values.add("Test2Cookie=\"TEST2\"; path=\"/test2/\"");
108
header.put("Set-Cookie2", values);
109
110
// preload the CookieHandler with a cookie for our URL
111
// so that it will be sent during the first request
112
ch.put(url2.toURI(), header);
113
114
uc = (HttpURLConnection)url2.openConnection();
115
resp = uc.getResponseCode();
116
if (resp != 200)
117
throw new RuntimeException("Failed: Part 2, Response code is not 200: " + resp);
118
119
System.out.println("Response code from Part 2 = 200 OK");
120
121
} finally {
122
httpServer.stop(1);
123
executorService.shutdown();
124
}
125
}
126
127
/**
128
* Http Server
129
*/
130
public void startHttpServer() throws IOException {
131
InetAddress loopback = InetAddress.getLoopbackAddress();
132
httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
133
134
// create HttpServer context for Part 1.
135
HttpContext ctx = httpServer.createContext("/test/", new MyHandler());
136
ctx.setAuthenticator( new MyBasicAuthenticator("foo"));
137
// CookieFilter needs to be executed before Authenticator.
138
ctx.getFilters().add(0, new CookieFilter());
139
140
// create HttpServer context for Part 2.
141
HttpContext ctx2 = httpServer.createContext("/test2/", new MyHandler2());
142
ctx2.setAuthenticator( new MyBasicAuthenticator("foobar"));
143
144
executorService = Executors.newCachedThreadPool();
145
httpServer.setExecutor(executorService);
146
httpServer.start();
147
}
148
149
class MyHandler implements HttpHandler {
150
public void handle(HttpExchange t) throws IOException {
151
InputStream is = t.getRequestBody();
152
Headers reqHeaders = t.getRequestHeaders();
153
Headers resHeaders = t.getResponseHeaders();
154
while (is.read () != -1) ;
155
is.close();
156
157
if (!reqHeaders.containsKey("Authorization"))
158
t.sendResponseHeaders(400, -1);
159
160
List<String> cookies = reqHeaders.get("Cookie");
161
if (cookies != null) {
162
for (String str : cookies) {
163
if (str.equals("Customer=WILE_E_COYOTE"))
164
t.sendResponseHeaders(200, -1);
165
}
166
}
167
t.sendResponseHeaders(400, -1);
168
}
169
}
170
171
class MyHandler2 implements HttpHandler {
172
public void handle(HttpExchange t) throws IOException {
173
InputStream is = t.getRequestBody();
174
Headers reqHeaders = t.getRequestHeaders();
175
Headers resHeaders = t.getResponseHeaders();
176
while (is.read () != -1) ;
177
is.close();
178
179
if (!reqHeaders.containsKey("Authorization"))
180
t.sendResponseHeaders(400, -1);
181
182
List<String> cookies = reqHeaders.get("Cookie");
183
184
// there should only be one Cookie header
185
if (cookies != null && (cookies.size() == 1)) {
186
t.sendResponseHeaders(200, -1);
187
}
188
t.sendResponseHeaders(400, -1);
189
}
190
}
191
192
class MyAuthenticator extends java.net.Authenticator {
193
public PasswordAuthentication getPasswordAuthentication () {
194
return new PasswordAuthentication("tester", "passwd".toCharArray());
195
}
196
}
197
198
class MyBasicAuthenticator extends BasicAuthenticator
199
{
200
public MyBasicAuthenticator(String realm) {
201
super(realm);
202
}
203
204
public boolean checkCredentials (String username, String password) {
205
return username.equals("tester") && password.equals("passwd");
206
}
207
}
208
209
class CookieFilter extends Filter
210
{
211
public void doFilter(HttpExchange t, Chain chain) throws IOException
212
{
213
Headers resHeaders = t.getResponseHeaders();
214
Headers reqHeaders = t.getRequestHeaders();
215
216
if (!reqHeaders.containsKey("Authorization"))
217
resHeaders.set("Set-Cookie2", "Customer=\"WILE_E_COYOTE\"; path=\"/test/\"");
218
219
chain.doFilter(t);
220
}
221
222
public void destroy(HttpContext c) { }
223
224
public void init(HttpContext c) { }
225
226
public String description() {
227
return new String("Filter for setting a cookie for requests without an \"Authorization\" header.");
228
}
229
}
230
}
231
232