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