Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/HttpClient/RetryPost.java
41154 views
1
/*
2
* Copyright (c) 2006, 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 6427251 6382788
27
* @modules jdk.httpserver
28
* @library /test/lib
29
* @run main RetryPost
30
* @run main/othervm -Dsun.net.http.retryPost=false RetryPost noRetry
31
* @summary HttpURLConnection automatically retries non-idempotent method POST
32
*/
33
34
import com.sun.net.httpserver.HttpContext;
35
import com.sun.net.httpserver.HttpExchange;
36
import com.sun.net.httpserver.HttpHandler;
37
import java.io.IOException;
38
import java.io.OutputStream;
39
import java.net.HttpURLConnection;
40
import java.net.InetAddress;
41
import java.net.InetSocketAddress;
42
import java.net.Proxy;
43
import java.net.SocketException;
44
import java.net.URL;
45
import java.util.concurrent.Executors;
46
import java.util.concurrent.ExecutorService;
47
import jdk.test.lib.net.URIBuilder;
48
49
public class RetryPost
50
{
51
static boolean shouldRetry = true;
52
53
com.sun.net.httpserver.HttpServer httpServer;
54
MyHandler httpHandler;
55
ExecutorService executorService;
56
57
public static void main(String[] args) throws Exception {
58
if (args.length == 1 && args[0].equals("noRetry"))
59
shouldRetry = false;
60
61
new RetryPost();
62
}
63
64
public RetryPost() throws Exception {
65
startHttpServer(shouldRetry);
66
doClient();
67
}
68
69
void doClient() throws Exception {
70
try {
71
InetSocketAddress address = httpServer.getAddress();
72
URL url = URIBuilder.newBuilder()
73
.scheme("http")
74
.host(address.getAddress())
75
.port(address.getPort())
76
.path("/test/")
77
.toURLUnchecked();
78
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
79
uc.setDoOutput(true);
80
uc.setRequestMethod("POST");
81
uc.getResponseCode();
82
83
// if we reach here then we have failed
84
throw new RuntimeException("Failed: POST request being retried");
85
86
} catch (SocketException se) {
87
System.out.println("Got expected exception: " + se);
88
// this is what we expect to happen and is OK.
89
if (shouldRetry && httpHandler.getCallCount() != 2) {
90
se.printStackTrace(System.out);
91
throw new RuntimeException("Failed: Handler should have been called twice. " +
92
"It was called "+ httpHandler.getCallCount() + " times");
93
} else if (!shouldRetry && httpHandler.getCallCount() != 1) {
94
se.printStackTrace(System.out);
95
throw new RuntimeException("Failed: Handler should have only been called once" +
96
"It was called "+ httpHandler.getCallCount() + " times");
97
}
98
} finally {
99
httpServer.stop(1);
100
executorService.shutdown();
101
}
102
}
103
104
/**
105
* Http Server
106
*/
107
public void startHttpServer(boolean shouldRetry) throws IOException {
108
InetAddress loopback = InetAddress.getLoopbackAddress();
109
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(loopback, 0), 0);
110
httpHandler = new MyHandler(shouldRetry);
111
112
HttpContext ctx = httpServer.createContext("/test/", httpHandler);
113
114
executorService = Executors.newCachedThreadPool();
115
httpServer.setExecutor(executorService);
116
httpServer.start();
117
}
118
119
class MyHandler implements HttpHandler {
120
volatile int callCount = 0;
121
final boolean shouldRetry;
122
123
public MyHandler(boolean shouldRetry) {
124
this.shouldRetry = shouldRetry;
125
}
126
127
public void handle(HttpExchange t) throws IOException {
128
callCount++;
129
130
if (callCount > 1 && !shouldRetry) {
131
// if this bug has been fixed then this method will not be called twice
132
// when -Dhttp.retryPost=false
133
t.sendResponseHeaders(400, -1); // indicate failure by returning 400
134
} else {
135
// simply close out the stream without sending any data.
136
OutputStream os = t.getResponseBody();
137
os.close();
138
}
139
}
140
141
public int getCallCount() {
142
return callCount;
143
}
144
}
145
146
}
147
148