Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/TruncatedRequestBody.java
41155 views
1
/*
2
* Copyright (c) 2017, 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 8190793
27
* @summary Httpserver does not detect truncated request body
28
*/
29
30
import com.sun.net.httpserver.HttpContext;
31
import com.sun.net.httpserver.HttpExchange;
32
import com.sun.net.httpserver.HttpHandler;
33
import com.sun.net.httpserver.HttpServer;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.OutputStream;
37
import java.net.InetAddress;
38
import java.net.InetSocketAddress;
39
import java.net.Socket;
40
import java.nio.charset.StandardCharsets;
41
import java.util.concurrent.CountDownLatch;
42
import java.util.concurrent.ExecutorService;
43
import java.util.concurrent.Executors;
44
import java.util.logging.ConsoleHandler;
45
import java.util.logging.Level;
46
import java.util.logging.Logger;
47
48
/*
49
* Send two POST requests to the server which are both trucated
50
* and socket closed. Server needs to detect this and throw an IOException
51
* in getRequestBody().read(). Two variants for fixed length and chunked.
52
*/
53
public class TruncatedRequestBody {
54
static volatile boolean error = false;
55
56
static CountDownLatch latch = new CountDownLatch(2);
57
58
static class Handler implements HttpHandler {
59
60
@Override
61
public void handle(HttpExchange exch) throws IOException {
62
InputStream is = exch.getRequestBody();
63
int c, count = 0;
64
byte[] buf = new byte[128];
65
try {
66
while ((c=is.read(buf)) > 0)
67
count += c;
68
} catch (IOException e) {
69
System.out.println("Exception caught");
70
latch.countDown();
71
throw e;
72
}
73
// shouldn't get to here
74
error = true;
75
latch.countDown();
76
System.out.println("Read " + count + " bytes");
77
is.close();
78
exch.sendResponseHeaders(200, -1);
79
}
80
81
}
82
83
/**
84
* @param args the command line arguments
85
*/
86
public static void main(String[] args) throws IOException, InterruptedException {
87
Logger logger = Logger.getLogger("com.sun.net.httpserver");
88
ConsoleHandler h = new ConsoleHandler();
89
h.setLevel(Level.ALL);
90
logger.setLevel(Level.ALL);
91
logger.addHandler(h);
92
93
InetAddress loopback = InetAddress.getLoopbackAddress();
94
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
95
HttpServer server = HttpServer.create(addr, 10);
96
HttpContext ct = server.createContext("/", new Handler());
97
ExecutorService ex = Executors.newCachedThreadPool();
98
server.setExecutor(ex);
99
server.start();
100
101
int port = server.getAddress().getPort();
102
103
// Test 1: fixed length
104
105
Socket sock = new Socket(loopback, port);
106
String s1 = "POST /foo HTTP/1.1\r\nContent-length: 200000\r\n"
107
+ "\r\nfoo bar99";
108
109
OutputStream os = sock.getOutputStream();
110
os.write(s1.getBytes(StandardCharsets.ISO_8859_1));
111
Thread.sleep(500);
112
113
sock.close();
114
115
// Test 2: chunked
116
117
String s2 = "POST /foo HTTP/1.1\r\nTransfer-encoding: chunked\r\n\r\n" +
118
"100\r\nFoo bar";
119
sock = new Socket(loopback, port);
120
os = sock.getOutputStream();
121
os.write(s2.getBytes(StandardCharsets.ISO_8859_1));
122
Thread.sleep(500);
123
sock.close();
124
latch.await();
125
server.stop(0);
126
ex.shutdownNow();
127
if (error)
128
throw new RuntimeException("Test failed");
129
}
130
}
131
132