Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLConnection/ResendPostBody.java
41149 views
1
/*
2
* Copyright (c) 2001, 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 4361492
27
* @summary HTTPUrlConnection does not receive binary data correctly
28
* @library /test/lib
29
* @run main/timeout=20 ResendPostBody
30
*/
31
32
import java.io.*;
33
import java.net.*;
34
35
import jdk.test.lib.net.URIBuilder;
36
37
/*
38
* This test does the following:
39
* 1. client opens HTTP connection to server
40
* 2. client sends POST with a body containing "ZZZ"
41
* 3. server waits for POST and closes connection without replying
42
* 4. client should re-open the connection and re-send the POST
43
* 5. <bug>The client forgets to re-send the body with the POST
44
* The server hangs waiting for the body</bug>
45
*
46
* <bugfixed>The client sends the body. The server reads it and the
47
* test terminates normally </bugfixed>
48
*/
49
50
public class ResendPostBody {
51
52
static class Server extends Thread {
53
54
private InputStream in;
55
private OutputStream out;
56
private Socket sock;
57
private StringBuffer response;
58
private ServerSocket server;
59
60
Server(ServerSocket s) throws IOException {
61
server = s;
62
}
63
64
void waitFor(String s) throws IOException {
65
byte[] w = s.getBytes();
66
for (int c = 0; c < w.length; c++) {
67
byte expected = w[c];
68
int b = in.read();
69
if (b == -1) {
70
acceptConn();
71
}
72
if ((byte) b != expected) {
73
c = 0;
74
}
75
}
76
}
77
78
private boolean done = false;
79
80
public synchronized boolean finished() {
81
return done;
82
}
83
84
public synchronized void setFinished(boolean b) throws IOException {
85
done = b;
86
this.closeConn();
87
server.close();
88
}
89
90
void acceptConn() throws IOException {
91
sock = server.accept();
92
in = sock.getInputStream();
93
out = sock.getOutputStream();
94
}
95
96
void closeConn() throws IOException {
97
in.close();
98
out.close();
99
sock.close();
100
}
101
102
public void run() {
103
try {
104
response = new StringBuffer(1024);
105
acceptConn();
106
waitFor("POST");
107
waitFor("ZZZ");
108
Thread.sleep(500);
109
sock.close();
110
acceptConn();
111
waitFor("POST");
112
waitFor("ZZZ");
113
response.append("HTTP/1.1 200 OK\r\n");
114
response.append("Server: Microsoft-IIS/5.0");
115
response.append("Date: Wed, 26 Jul 2000 14:17:04 GMT\r\n\r\n");
116
out.write(response.toString().getBytes());
117
out.flush();
118
while (!finished()) {
119
Thread.sleep(1000);
120
}
121
} catch (Exception e) {
122
if (!done) {
123
System.err.println("Server Exception: " + e);
124
}
125
} finally {
126
try {
127
closeConn();
128
} catch (IOException ioe) {
129
if (!done) {
130
ioe.printStackTrace();
131
}
132
}
133
}
134
}
135
}
136
137
private ServerSocket ss;
138
private Server server;
139
140
public static void main(String[] args) throws Exception {
141
if (args.length == 1 && args[0].equals("-i")) {
142
System.out.println("Press return when ready");
143
System.in.read();
144
System.out.println("Done");
145
}
146
ResendPostBody t = new ResendPostBody();
147
t.execute();
148
}
149
150
public void execute() throws Exception {
151
byte b[] = "X=ABCDEFGHZZZ".getBytes();
152
153
ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
154
server = new Server(ss);
155
server.start();
156
157
/* Get the URL */
158
URL url = URIBuilder.newBuilder()
159
.scheme("http")
160
.loopback()
161
.port(ss.getLocalPort())
162
.path("/test")
163
.toURL();
164
HttpURLConnection conURL = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
165
166
conURL.setDoOutput(true);
167
conURL.setDoInput(true);
168
conURL.setAllowUserInteraction(false);
169
conURL.setUseCaches(false);
170
conURL.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
171
conURL.setRequestProperty("Content-Length", "" + b.length);
172
conURL.setRequestProperty("Connection", "Close");
173
174
/* POST some data */
175
DataOutputStream OutStream = new DataOutputStream(conURL.getOutputStream());
176
OutStream.write(b, 0, b.length);
177
OutStream.flush();
178
OutStream.close();
179
180
/* Read the response */
181
int resp = conURL.getResponseCode();
182
183
server.setFinished(true); // Set finished and close ServerSocket
184
server.join(); // Join server thread
185
186
if (resp != 200)
187
throw new RuntimeException("Response code was not 200: " + resp);
188
}
189
}
190
191