Path: blob/master/test/jdk/sun/net/www/protocol/http/SetChunkedStreamingMode.java
66646 views
/*1* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 504997626* @library /test/lib27* @run main/othervm SetChunkedStreamingMode28* @summary Unspecified NPE is thrown when streaming output mode is enabled29*/3031import java.io.IOException;32import java.io.InputStream;33import java.io.PrintWriter;34import java.net.HttpURLConnection;35import java.net.InetAddress;36import java.net.InetSocketAddress;37import java.net.URL;38import java.util.concurrent.Executors;3940import com.sun.net.httpserver.HttpExchange;41import com.sun.net.httpserver.HttpHandler;42import com.sun.net.httpserver.HttpServer;43import jdk.test.lib.net.URIBuilder;4445public class SetChunkedStreamingMode implements HttpHandler {4647void okReply (HttpExchange req) throws IOException {48req.sendResponseHeaders(200, 0);49try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {50pw.print("Hello .");51}52System.out.println ("Server: sent response");53}5455@Override56public void handle(HttpExchange exchange) throws IOException {57okReply(exchange);58}5960static HttpServer server;6162public static void main (String[] args) throws Exception {63try {64InetAddress loopback = InetAddress.getLoopbackAddress();65server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);66server.createContext("/", new SetChunkedStreamingMode());67server.setExecutor(Executors.newSingleThreadExecutor());68server.start();69System.out.println ("Server: listening on port: " + server.getAddress().getPort());70URL url = URIBuilder.newBuilder()71.scheme("http")72.loopback()73.port(server.getAddress().getPort())74.path("/")75.toURL();76System.out.println ("Client: connecting to " + url);77HttpURLConnection urlc = (HttpURLConnection)url.openConnection();78urlc.setChunkedStreamingMode (0);79urlc.setRequestMethod("POST");80urlc.setDoOutput(true);81InputStream is = urlc.getInputStream();82} catch (Exception e) {83if (server != null) {84server.stop(1);85}86throw e;87}88server.stop(1);89}9091public static void except (String s) {92server.stop(1);93throw new RuntimeException (s);94}95}969798