Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/HttpClient/StreamingRetry.java
41154 views
1
/*
2
* Copyright (c) 2010, 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 6672144 8050983
27
* @summary Do not retry failed request with a streaming body.
28
* @library /test/lib
29
* @run main StreamingRetry
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true StreamingRetry
31
*/
32
33
import java.net.HttpURLConnection;
34
import java.net.InetAddress;
35
import java.net.InetSocketAddress;
36
import java.net.Proxy;
37
import java.net.ServerSocket;
38
import java.net.URL;
39
import java.io.IOException;
40
import java.io.InputStream;
41
import java.io.OutputStream;
42
import static java.lang.System.out;
43
44
import jdk.test.lib.net.URIBuilder;
45
46
public class StreamingRetry implements Runnable {
47
static final int ACCEPT_TIMEOUT = 20 * 1000; // 20 seconds
48
volatile ServerSocket ss;
49
50
public static void main(String[] args) throws Exception {
51
(new StreamingRetry()).instanceMain();
52
}
53
54
void instanceMain() throws Exception {
55
out.println("Test with default method");
56
test(null);
57
out.println("Test with POST method");
58
test("POST");
59
out.println("Test with PUT method");
60
test("PUT");
61
62
if (failed > 0) throw new RuntimeException("Some tests failed");
63
}
64
65
void test(String method) throws Exception {
66
ss = new ServerSocket();
67
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
68
ss.setSoTimeout(ACCEPT_TIMEOUT);
69
int port = ss.getLocalPort();
70
71
Thread otherThread = new Thread(this);
72
otherThread.start();
73
74
try {
75
URL url = URIBuilder.newBuilder()
76
.scheme("http")
77
.host(ss.getInetAddress())
78
.port(port)
79
.path("/")
80
.toURL();
81
HttpURLConnection uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
82
uc.setDoOutput(true);
83
if (method != null)
84
uc.setRequestMethod(method);
85
uc.setChunkedStreamingMode(4096);
86
OutputStream os = uc.getOutputStream();
87
os.write("Hello there".getBytes());
88
89
InputStream is = uc.getInputStream();
90
is.close();
91
} catch (IOException expected) {
92
//expected.printStackTrace();
93
} finally {
94
ss.close();
95
otherThread.join();
96
}
97
}
98
99
// Server
100
public void run() {
101
try {
102
(ss.accept()).close();
103
(ss.accept()).close();
104
ss.close();
105
fail("The server shouldn't accept a second connection");
106
} catch (IOException e) {
107
//OK, the client will close the server socket if successful
108
}
109
}
110
111
volatile int failed = 0;
112
void fail() {failed++; Thread.dumpStack();}
113
void fail(String msg) {System.err.println(msg); fail();}
114
}
115
116