Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6401598.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
* @library /test/lib
27
* @bug 6401598
28
* @summary new HttpServer cannot serve binary stream data
29
* @run main B6401598
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6401598
31
*/
32
33
import java.io.*;
34
import java.net.HttpURLConnection;
35
import java.net.MalformedURLException;
36
import java.net.Proxy;
37
import java.net.URL;
38
import java.net.InetAddress;
39
import java.net.InetSocketAddress;
40
import java.util.concurrent.*;
41
42
import jdk.test.lib.net.URIBuilder;
43
44
import com.sun.net.httpserver.HttpExchange;
45
import com.sun.net.httpserver.HttpHandler;
46
import com.sun.net.httpserver.HttpServer;
47
48
public class B6401598 {
49
50
static class MyHandler implements HttpHandler {
51
52
public MyHandler() {
53
54
}
55
56
public void handle(HttpExchange arg0) throws IOException {
57
try {
58
InputStream is = arg0.getRequestBody();
59
OutputStream os = arg0.getResponseBody();
60
61
DataInputStream dis = new DataInputStream(is);
62
63
short input = dis.readShort();
64
while (dis.read() != -1) ;
65
dis.close();
66
67
DataOutputStream dos = new DataOutputStream(os);
68
69
arg0.sendResponseHeaders(200, 0);
70
71
dos.writeShort(input);
72
73
dos.flush();
74
dos.close();
75
} catch (IOException e) {
76
e.printStackTrace();
77
error = true;
78
}
79
}
80
81
}
82
83
static int port;
84
static boolean error = false;
85
static ExecutorService exec;
86
static HttpServer server;
87
88
public static void main(String[] args) {
89
try {
90
InetAddress loopback = InetAddress.getLoopbackAddress();
91
server = HttpServer.create(new InetSocketAddress(loopback, 0), 400);
92
server.createContext("/server/", new MyHandler());
93
exec = Executors.newFixedThreadPool(3);
94
server.setExecutor(exec);
95
port = server.getAddress().getPort();
96
server.start();
97
98
short counter;
99
100
for (counter = 0; counter < 1000; counter++) {
101
URL url = URIBuilder.newBuilder()
102
.scheme("http")
103
.loopback()
104
.port(port)
105
.path("/server/")
106
.toURLUnchecked();
107
System.out.println("URL: " + url);
108
HttpURLConnection connection = getHttpURLConnection(url, 10000);
109
110
OutputStream os = connection.getOutputStream();
111
112
DataOutputStream dos = new DataOutputStream(os);
113
114
dos.writeShort(counter);
115
116
dos.flush();
117
dos.close();
118
119
counter++;
120
121
InputStream is = connection.getInputStream();
122
123
DataInputStream dis = new DataInputStream(is);
124
125
short ret = dis.readShort();
126
127
dis.close();
128
}
129
System.out.println ("Stopping");
130
} catch (Exception e) {
131
throw new AssertionError("Unexpected exception: " + e, e);
132
} finally {
133
server.stop (1);
134
exec.shutdown();
135
}
136
}
137
138
139
140
static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {
141
142
HttpURLConnection httpURLConnection =
143
(HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
144
145
httpURLConnection.setConnectTimeout(40000);
146
httpURLConnection.setReadTimeout(timeout);
147
httpURLConnection.setDoOutput(true);
148
httpURLConnection.setDoInput(true);
149
httpURLConnection.setUseCaches(false);
150
httpURLConnection.setAllowUserInteraction(false);
151
httpURLConnection.setRequestMethod("POST");
152
153
// HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);
154
155
return httpURLConnection;
156
}
157
}
158
159