Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/FixedLengthInputStream.java
41154 views
1
/*
2
* Copyright (c) 2008, 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 6756771 6755625
27
* @summary com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
28
* @library /test/lib
29
* @run main FixedLengthInputStream
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true FixedLengthInputStream
31
*/
32
33
import java.io.InputStream;
34
import java.io.IOException;
35
import java.io.OutputStream;
36
import java.io.PrintStream;
37
import java.net.InetAddress;
38
import java.net.InetSocketAddress;
39
import java.net.HttpURLConnection;
40
import java.net.Proxy;
41
import java.net.URL;
42
import java.net.Socket;
43
import java.util.logging.*;
44
import com.sun.net.httpserver.HttpExchange;
45
import com.sun.net.httpserver.HttpHandler;
46
import com.sun.net.httpserver.HttpServer;
47
import jdk.test.lib.net.URIBuilder;
48
49
public class FixedLengthInputStream
50
{
51
static final long POST_SIZE = 4L * 1024L * 1024L * 1024L; // 4Gig
52
53
void test(String[] args) throws IOException {
54
HttpServer httpServer = startHttpServer();
55
int port = httpServer.getAddress().getPort();
56
try {
57
URL url = URIBuilder.newBuilder()
58
.scheme("http")
59
.loopback()
60
.port(port)
61
.path("/flis/")
62
.toURLUnchecked();
63
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
64
uc.setDoOutput(true);
65
uc.setRequestMethod("POST");
66
uc.setFixedLengthStreamingMode(POST_SIZE);
67
OutputStream os = uc.getOutputStream();
68
69
/* create a 32K byte array with data to POST */
70
int thirtyTwoK = 32 * 1024;
71
byte[] ba = new byte[thirtyTwoK];
72
for (int i =0; i<thirtyTwoK; i++)
73
ba[i] = (byte)i;
74
75
long times = POST_SIZE / thirtyTwoK;
76
for (int i=0; i<times; i++) {
77
os.write(ba);
78
}
79
80
os.close();
81
InputStream is = uc.getInputStream();
82
while(is.read(ba) != -1);
83
is.close();
84
85
pass();
86
} finally {
87
httpServer.stop(0);
88
}
89
}
90
91
/**
92
* Http Server
93
*/
94
HttpServer startHttpServer() throws IOException {
95
if (debug) {
96
Logger logger =
97
Logger.getLogger("com.sun.net.httpserver");
98
Handler outHandler = new StreamHandler(System.out,
99
new SimpleFormatter());
100
outHandler.setLevel(Level.FINEST);
101
logger.setLevel(Level.FINEST);
102
logger.addHandler(outHandler);
103
}
104
InetAddress loopback = InetAddress.getLoopbackAddress();
105
HttpServer httpServer = HttpServer.create(new InetSocketAddress(loopback, 0), 0);
106
httpServer.createContext("/flis/", new MyHandler(POST_SIZE));
107
httpServer.start();
108
return httpServer;
109
}
110
111
class MyHandler implements HttpHandler {
112
static final int BUFFER_SIZE = 32 * 1024;
113
long expected;
114
115
MyHandler(long expected){
116
this.expected = expected;
117
}
118
119
@Override
120
public void handle(HttpExchange t) throws IOException {
121
InputStream is = t.getRequestBody();
122
byte[] ba = new byte[BUFFER_SIZE];
123
int read;
124
long count = 0L;
125
while((read = is.read(ba)) != -1) {
126
count += read;
127
}
128
is.close();
129
130
check(count == expected, "Expected: " + expected + ", received "
131
+ count);
132
133
debug("Received " + count + " bytes");
134
135
t.sendResponseHeaders(200, -1);
136
t.close();
137
}
138
}
139
140
//--------------------- Infrastructure ---------------------------
141
boolean debug = true;
142
volatile int passed = 0, failed = 0;
143
void pass() {passed++;}
144
void fail() {failed++; Thread.dumpStack();}
145
void fail(String msg) {System.err.println(msg); fail();}
146
void unexpected(Throwable t) {failed++; t.printStackTrace();}
147
void check(boolean cond) {if (cond) pass(); else fail();}
148
void check(boolean cond, String failMessage) {if (cond) pass(); else fail(failMessage);}
149
void debug(String message) {if(debug) { System.out.println(message); } }
150
public static void main(String[] args) throws Throwable {
151
Class<?> k = new Object(){}.getClass().getEnclosingClass();
152
try {k.getMethod("instanceMain",String[].class)
153
.invoke( k.newInstance(), (Object) args);}
154
catch (Throwable e) {throw e.getCause();}}
155
public void instanceMain(String[] args) throws Throwable {
156
try {test(args);} catch (Throwable t) {unexpected(t);}
157
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
158
if (failed > 0) throw new AssertionError("Some tests failed");}
159
160
}
161
162