Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/net/www/protocol/http/SetChunkedStreamingMode.java
66646 views
1
/*
2
* Copyright (c) 2004, 2021, 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 5049976
27
* @library /test/lib
28
* @run main/othervm SetChunkedStreamingMode
29
* @summary Unspecified NPE is thrown when streaming output mode is enabled
30
*/
31
32
import java.io.IOException;
33
import java.io.InputStream;
34
import java.io.PrintWriter;
35
import java.net.HttpURLConnection;
36
import java.net.InetAddress;
37
import java.net.InetSocketAddress;
38
import java.net.URL;
39
import java.util.concurrent.Executors;
40
41
import com.sun.net.httpserver.HttpExchange;
42
import com.sun.net.httpserver.HttpHandler;
43
import com.sun.net.httpserver.HttpServer;
44
import jdk.test.lib.net.URIBuilder;
45
46
public class SetChunkedStreamingMode implements HttpHandler {
47
48
void okReply (HttpExchange req) throws IOException {
49
req.sendResponseHeaders(200, 0);
50
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
51
pw.print("Hello .");
52
}
53
System.out.println ("Server: sent response");
54
}
55
56
@Override
57
public void handle(HttpExchange exchange) throws IOException {
58
okReply(exchange);
59
}
60
61
static HttpServer server;
62
63
public static void main (String[] args) throws Exception {
64
try {
65
InetAddress loopback = InetAddress.getLoopbackAddress();
66
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
67
server.createContext("/", new SetChunkedStreamingMode());
68
server.setExecutor(Executors.newSingleThreadExecutor());
69
server.start();
70
System.out.println ("Server: listening on port: " + server.getAddress().getPort());
71
URL url = URIBuilder.newBuilder()
72
.scheme("http")
73
.loopback()
74
.port(server.getAddress().getPort())
75
.path("/")
76
.toURL();
77
System.out.println ("Client: connecting to " + url);
78
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
79
urlc.setChunkedStreamingMode (0);
80
urlc.setRequestMethod("POST");
81
urlc.setDoOutput(true);
82
InputStream is = urlc.getInputStream();
83
} catch (Exception e) {
84
if (server != null) {
85
server.stop(1);
86
}
87
throw e;
88
}
89
server.stop(1);
90
}
91
92
public static void except (String s) {
93
server.stop(1);
94
throw new RuntimeException (s);
95
}
96
}
97
98