49765
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, 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 8199943
|
|
27 |
* @summary Test for cookie handling when retrying after close
|
|
28 |
* @modules java.base/sun.net.www.http
|
|
29 |
* java.net.http/jdk.internal.net.http.common
|
|
30 |
* java.net.http/jdk.internal.net.http.frame
|
|
31 |
* java.net.http/jdk.internal.net.http.hpack
|
|
32 |
* java.logging
|
|
33 |
* jdk.httpserver
|
|
34 |
* @library /lib/testlibrary /test/lib http2/server
|
|
35 |
* @build Http2TestServer
|
|
36 |
* @build jdk.testlibrary.SimpleSSLContext ReferenceTracker
|
|
37 |
* @run testng/othervm
|
|
38 |
* -Djdk.httpclient.HttpClient.log=trace,headers,requests
|
|
39 |
* RetryWithCookie
|
|
40 |
*/
|
|
41 |
|
|
42 |
import com.sun.net.httpserver.HttpServer;
|
|
43 |
import com.sun.net.httpserver.HttpsConfigurator;
|
|
44 |
import com.sun.net.httpserver.HttpsServer;
|
|
45 |
import jdk.testlibrary.SimpleSSLContext;
|
|
46 |
import org.testng.annotations.AfterTest;
|
|
47 |
import org.testng.annotations.BeforeTest;
|
|
48 |
import org.testng.annotations.DataProvider;
|
|
49 |
import org.testng.annotations.Test;
|
|
50 |
|
|
51 |
import javax.net.ssl.SSLContext;
|
|
52 |
import java.io.IOException;
|
|
53 |
import java.io.InputStream;
|
|
54 |
import java.io.OutputStream;
|
|
55 |
import java.net.CookieManager;
|
|
56 |
import java.net.InetAddress;
|
|
57 |
import java.net.InetSocketAddress;
|
|
58 |
import java.net.URI;
|
|
59 |
import java.net.http.HttpClient;
|
|
60 |
import java.net.http.HttpClient.Redirect;
|
|
61 |
import java.net.http.HttpRequest;
|
|
62 |
import java.net.http.HttpResponse;
|
|
63 |
import java.net.http.HttpResponse.BodyHandlers;
|
|
64 |
import java.util.ArrayList;
|
|
65 |
import java.util.HashMap;
|
|
66 |
import java.util.List;
|
|
67 |
import java.util.Map;
|
|
68 |
import java.util.concurrent.ConcurrentHashMap;
|
|
69 |
import java.util.concurrent.atomic.AtomicLong;
|
|
70 |
|
|
71 |
import static java.lang.System.out;
|
49944
|
72 |
import static java.net.http.HttpClient.Builder.NO_PROXY;
|
49765
|
73 |
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
74 |
import static org.testng.Assert.assertEquals;
|
|
75 |
import static org.testng.Assert.assertTrue;
|
|
76 |
|
|
77 |
public class RetryWithCookie implements HttpServerAdapters {
|
|
78 |
|
|
79 |
SSLContext sslContext;
|
|
80 |
HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ]
|
|
81 |
HttpTestServer httpsTestServer; // HTTPS/1.1
|
|
82 |
HttpTestServer http2TestServer; // HTTP/2 ( h2c )
|
|
83 |
HttpTestServer https2TestServer; // HTTP/2 ( h2 )
|
|
84 |
String httpURI;
|
|
85 |
String httpsURI;
|
|
86 |
String http2URI;
|
|
87 |
String https2URI;
|
|
88 |
|
|
89 |
static final String MESSAGE = "BasicRedirectTest message body";
|
|
90 |
static final int ITERATIONS = 3;
|
|
91 |
|
|
92 |
@DataProvider(name = "positive")
|
|
93 |
public Object[][] positive() {
|
|
94 |
return new Object[][] {
|
|
95 |
{ httpURI, },
|
|
96 |
{ httpsURI, },
|
|
97 |
{ http2URI, },
|
|
98 |
{ https2URI, },
|
|
99 |
};
|
|
100 |
}
|
|
101 |
|
|
102 |
static final AtomicLong requestCounter = new AtomicLong();
|
|
103 |
final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
|
|
104 |
|
|
105 |
@Test(dataProvider = "positive")
|
|
106 |
void test(String uriString) throws Exception {
|
|
107 |
out.printf("%n---- starting (%s) ----%n", uriString);
|
|
108 |
CookieManager cookieManager = new CookieManager();
|
|
109 |
HttpClient client = HttpClient.newBuilder()
|
49944
|
110 |
.proxy(NO_PROXY)
|
49765
|
111 |
.followRedirects(Redirect.ALWAYS)
|
|
112 |
.cookieHandler(cookieManager)
|
|
113 |
.sslContext(sslContext)
|
|
114 |
.build();
|
|
115 |
TRACKER.track(client);
|
|
116 |
assert client.cookieHandler().isPresent();
|
|
117 |
|
|
118 |
URI uri = URI.create(uriString);
|
|
119 |
List<String> cookies = new ArrayList<>();
|
|
120 |
cookies.add("CUSTOMER=ARTHUR_DENT");
|
|
121 |
Map<String, List<String>> cookieHeaders = new HashMap<>();
|
|
122 |
cookieHeaders.put("Set-Cookie", cookies);
|
|
123 |
cookieManager.put(uri, cookieHeaders);
|
|
124 |
|
|
125 |
HttpRequest request = HttpRequest.newBuilder(uri)
|
|
126 |
.header("X-uuid", "uuid-" + requestCounter.incrementAndGet())
|
|
127 |
.build();
|
|
128 |
out.println("Initial request: " + request.uri());
|
|
129 |
|
|
130 |
for (int i=0; i< ITERATIONS; i++) {
|
|
131 |
out.println("iteration: " + i);
|
|
132 |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
|
|
133 |
|
|
134 |
out.println(" Got response: " + response);
|
|
135 |
out.println(" Got body Path: " + response.body());
|
|
136 |
|
|
137 |
assertEquals(response.statusCode(), 200);
|
|
138 |
assertEquals(response.body(), MESSAGE);
|
|
139 |
assertEquals(response.headers().allValues("X-Request-Cookie"), cookies);
|
|
140 |
request = HttpRequest.newBuilder(uri)
|
|
141 |
.header("X-uuid", "uuid-" + requestCounter.incrementAndGet())
|
|
142 |
.build();
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
// -- Infrastructure
|
|
147 |
|
|
148 |
@BeforeTest
|
|
149 |
public void setup() throws Exception {
|
|
150 |
sslContext = new SimpleSSLContext().get();
|
|
151 |
if (sslContext == null)
|
|
152 |
throw new AssertionError("Unexpected null sslContext");
|
|
153 |
|
|
154 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
|
|
155 |
|
|
156 |
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
|
|
157 |
httpTestServer.addHandler(new CookieRetryHandler(), "/http1/cookie/");
|
|
158 |
httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/cookie/retry";
|
|
159 |
HttpsServer httpsServer = HttpsServer.create(sa, 0);
|
|
160 |
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
|
161 |
httpsTestServer = HttpTestServer.of(httpsServer);
|
|
162 |
httpsTestServer.addHandler(new CookieRetryHandler(),"/https1/cookie/");
|
|
163 |
httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/cookie/retry";
|
|
164 |
|
|
165 |
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
|
|
166 |
http2TestServer.addHandler(new CookieRetryHandler(), "/http2/cookie/");
|
|
167 |
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/cookie/retry";
|
|
168 |
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
|
|
169 |
https2TestServer.addHandler(new CookieRetryHandler(), "/https2/cookie/");
|
|
170 |
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/cookie/retry";
|
|
171 |
|
|
172 |
httpTestServer.start();
|
|
173 |
httpsTestServer.start();
|
|
174 |
http2TestServer.start();
|
|
175 |
https2TestServer.start();
|
|
176 |
}
|
|
177 |
|
|
178 |
@AfterTest
|
|
179 |
public void teardown() throws Exception {
|
|
180 |
Thread.sleep(100);
|
|
181 |
AssertionError fail = TRACKER.check(500);
|
|
182 |
try {
|
|
183 |
httpTestServer.stop();
|
|
184 |
httpsTestServer.stop();
|
|
185 |
http2TestServer.stop();
|
|
186 |
https2TestServer.stop();
|
|
187 |
} finally {
|
|
188 |
if (fail != null) throw fail;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
static class CookieRetryHandler implements HttpTestHandler {
|
|
193 |
ConcurrentHashMap<String,String> closedRequests = new ConcurrentHashMap<>();
|
|
194 |
|
|
195 |
@Override
|
|
196 |
public void handle(HttpTestExchange t) throws IOException {
|
|
197 |
System.out.println("CookieRetryHandler for: " + t.getRequestURI());
|
|
198 |
|
|
199 |
List<String> uuids = t.getRequestHeaders().get("X-uuid");
|
|
200 |
if (uuids == null || uuids.size() != 1) {
|
|
201 |
readAllRequestData(t);
|
|
202 |
try (OutputStream os = t.getResponseBody()) {
|
|
203 |
String msg = "Incorrect uuid header values:[" + uuids + "]";
|
|
204 |
(new RuntimeException(msg)).printStackTrace();
|
|
205 |
t.sendResponseHeaders(500, -1);
|
|
206 |
os.write(msg.getBytes(UTF_8));
|
|
207 |
}
|
|
208 |
return;
|
|
209 |
}
|
|
210 |
|
|
211 |
String uuid = uuids.get(0);
|
|
212 |
// retrying
|
|
213 |
if (closedRequests.putIfAbsent(uuid, t.getRequestURI().toString()) == null) {
|
|
214 |
if (t.getExchangeVersion() == HttpClient.Version.HTTP_1_1) {
|
|
215 |
// Throwing an exception here only causes a retry
|
|
216 |
// with HTTP_1_1 - where it forces the server to close
|
|
217 |
// the connection.
|
|
218 |
// For HTTP/2 then throwing an IOE would cause the server
|
|
219 |
// to close the stream, and throwing anything else would
|
|
220 |
// cause it to close the connection, but neither would
|
|
221 |
// cause the client to retry.
|
|
222 |
// So we simply do not try to retry with HTTP/2 and just verify
|
|
223 |
// we have received the expected cookie
|
|
224 |
throw new IOException("Closing on first request");
|
|
225 |
}
|
|
226 |
}
|
|
227 |
|
|
228 |
// not retrying
|
|
229 |
readAllRequestData(t);
|
|
230 |
try (OutputStream os = t.getResponseBody()) {
|
|
231 |
List<String> cookie = t.getRequestHeaders().get("Cookie");
|
|
232 |
|
|
233 |
if (cookie == null || cookie.size() == 0) {
|
|
234 |
String msg = "No cookie header present";
|
|
235 |
(new RuntimeException(msg)).printStackTrace();
|
|
236 |
t.sendResponseHeaders(500, -1);
|
|
237 |
os.write(msg.getBytes(UTF_8));
|
|
238 |
} else if (!cookie.get(0).equals("CUSTOMER=ARTHUR_DENT")) {
|
|
239 |
String msg = "Incorrect cookie header value:[" + cookie.get(0) + "]";
|
|
240 |
(new RuntimeException(msg)).printStackTrace();
|
|
241 |
t.sendResponseHeaders(500, -1);
|
|
242 |
os.write(msg.getBytes(UTF_8));
|
|
243 |
} else if (cookie.size() > 1) {
|
|
244 |
String msg = "Incorrect cookie header values:[" + cookie + "]";
|
|
245 |
(new RuntimeException(msg)).printStackTrace();
|
|
246 |
t.sendResponseHeaders(500, -1);
|
|
247 |
os.write(msg.getBytes(UTF_8));
|
|
248 |
} else {
|
|
249 |
assert cookie.get(0).equals("CUSTOMER=ARTHUR_DENT");
|
|
250 |
byte[] bytes = MESSAGE.getBytes(UTF_8);
|
|
251 |
for (String value : cookie) {
|
|
252 |
t.getResponseHeaders().addHeader("X-Request-Cookie", value);
|
|
253 |
}
|
|
254 |
t.sendResponseHeaders(200, bytes.length);
|
|
255 |
os.write(bytes);
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
closedRequests.remove(uuid);
|
|
260 |
}
|
|
261 |
}
|
|
262 |
|
|
263 |
static void readAllRequestData(HttpTestExchange t) throws IOException {
|
|
264 |
try (InputStream is = t.getRequestBody()) {
|
|
265 |
is.readAllBytes();
|
|
266 |
}
|
|
267 |
}
|
|
268 |
}
|