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