Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/HttpClient/B6726695.java
41154 views
1
/*
2
* Copyright (c) 2009, 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 6726695 6993490
27
* @summary HttpURLConnection should support 'Expect: 100-contimue' headers for PUT
28
* @library /test/lib
29
* @run main B6726695
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6726695
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
36
import jdk.test.lib.net.URIBuilder;
37
38
public class B6726695 extends Thread {
39
private ServerSocket server = null;
40
private int port = 0;
41
private byte[] data = new byte[512];
42
private String boundary = "----------------7774563516523621";
43
44
public static void main(String[] args) throws Exception {
45
B6726695 test = new B6726695();
46
// Exit even if server is still running
47
test.setDaemon(true);
48
// start server
49
test.start();
50
// run test
51
test.test();
52
}
53
54
public B6726695() {
55
try {
56
server = new ServerSocket();
57
server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
58
port = server.getLocalPort();
59
} catch (IOException e) {
60
e.printStackTrace();
61
}
62
}
63
64
public void test() throws Exception {
65
/**
66
* This is a hardcoded test. The server side expects 3 requests with a
67
* Expect: 100-continue header. It will reject the 1st one and accept
68
* the second one. Thus allowing us to test both scenarios.
69
* The 3rd case is the simulation of a server that just plains ignore
70
* the Expect: 100-Continue header. So the POST should proceed after
71
* a timeout.
72
*/
73
URL url = URIBuilder.newBuilder()
74
.scheme("http")
75
.loopback()
76
.port(port)
77
.path("/foo")
78
.toURL();
79
80
// 1st Connection. Should be rejected. I.E. get a ProtocolException
81
URLConnection con = url.openConnection(Proxy.NO_PROXY);
82
HttpURLConnection http = (HttpURLConnection) con;
83
http.setRequestMethod("POST");
84
http.setRequestProperty("Expect", "100-Continue");
85
http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
86
http.setDoOutput(true);
87
http.setFixedLengthStreamingMode(512);
88
OutputStream out = null;
89
int errorCode = -1;
90
try {
91
out = http.getOutputStream();
92
} catch (ProtocolException e) {
93
errorCode = http.getResponseCode();
94
}
95
if (errorCode != 417) {
96
throw new RuntimeException("Didn't get the ProtocolException");
97
}
98
99
// 2nd connection. Should be accepted by server.
100
http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
101
http.setRequestMethod("POST");
102
http.setRequestProperty("Expect", "100-Continue");
103
http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
104
http.setDoOutput(true);
105
http.setFixedLengthStreamingMode(data.length);
106
out = null;
107
try {
108
out = http.getOutputStream();
109
} catch (ProtocolException e) {
110
}
111
if (out == null) {
112
throw new RuntimeException("Didn't get an OutputStream");
113
}
114
out.write(data);
115
out.flush();
116
errorCode = http.getResponseCode();
117
if (errorCode != 200) {
118
throw new RuntimeException("Response code is " + errorCode);
119
}
120
out.close();
121
122
// 3rd connection. Simulate a server that doesn't implement 100-continue
123
http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
124
http.setRequestMethod("POST");
125
http.setRequestProperty("Expect", "100-Continue");
126
http.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
127
http.setDoOutput(true);
128
http.setFixedLengthStreamingMode(data.length);
129
out = null;
130
try {
131
out = http.getOutputStream();
132
} catch (ProtocolException e) {
133
}
134
if (out == null) {
135
throw new RuntimeException("Didn't get an OutputStream");
136
}
137
out.write(data);
138
out.flush();
139
out.close();
140
errorCode = http.getResponseCode();
141
if (errorCode != 200) {
142
throw new RuntimeException("Response code is " + errorCode);
143
}
144
}
145
146
147
@Override
148
public void run() {
149
try {
150
// Fist connection: don't accetpt the request
151
Socket s = server.accept();
152
serverReject(s);
153
// Second connection: accept the request (send 100-continue)
154
s = server.accept();
155
serverAccept(s);
156
// 3rd connection: just ignore the 'Expect:' header
157
s = server.accept();
158
serverIgnore(s);
159
} catch (IOException e) {
160
e.printStackTrace();
161
} finally {
162
try { server.close(); } catch (IOException unused) {}
163
}
164
}
165
166
public void serverReject(Socket s) throws IOException {
167
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
168
PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
169
String line = null;
170
do {
171
line = in.readLine();
172
} while (line != null && line.length() != 0);
173
174
out.print("HTTP/1.1 417 Expectation Failed\r\n");
175
out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
176
out.print("Connection: close\r\n");
177
out.print("Content-Length: 0\r\n");
178
out.print("\r\n");
179
out.flush();
180
out.close();
181
in.close();
182
}
183
184
public void serverAccept(Socket s) throws IOException {
185
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
186
PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
187
String line = null;
188
do {
189
line = in.readLine();
190
} while (line != null && line.length() != 0);
191
192
// Send 100-Continue
193
out.print("HTTP/1.1 100 Continue\r\n");
194
out.print("\r\n");
195
out.flush();
196
// Then read the body
197
char[] cbuf = new char[512];
198
in.read(cbuf);
199
200
/* Force the server to not respond for more that the expect 100-Continue
201
* timeout set by the HTTP handler (5000 millis). This ensures the
202
* timeout is correctly resets the default read timeout, infinity.
203
* See 6993490. */
204
System.out.println("server sleeping...");
205
try {Thread.sleep(6000); } catch (InterruptedException e) {}
206
207
// finally send the 200 OK
208
out.print("HTTP/1.1 200 OK");
209
out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
210
out.print("Connection: close\r\n");
211
out.print("Content-Length: 0\r\n");
212
out.print("\r\n");
213
out.flush();
214
out.close();
215
in.close();
216
}
217
218
public void serverIgnore(Socket s) throws IOException {
219
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
220
PrintStream out = new PrintStream(new BufferedOutputStream(s.getOutputStream()));
221
String line = null;
222
do {
223
line = in.readLine();
224
} while (line != null && line.length() != 0);
225
226
// Then read the body
227
char[] cbuf = new char[512];
228
int l = in.read(cbuf);
229
// finally send the 200 OK
230
out.print("HTTP/1.1 200 OK");
231
out.print("Server: Sun-Java-System-Web-Server/7.0\r\n");
232
out.print("Content-Length: 0\r\n");
233
out.print("Connection: close\r\n");
234
out.print("\r\n");
235
out.flush();
236
out.close();
237
in.close();
238
}
239
}
240
241