Path: blob/master/test/jdk/sun/net/www/protocol/http/SetChunkedStreamingMode.java
41159 views
/*1* Copyright (c) 2004, 2019, 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* @modules java.base/sun.net.www27* @library ../../httptest/28* @library /test/lib29* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction30* @run main SetChunkedStreamingMode31* @summary Unspecified NPE is thrown when streaming output mode is enabled32*/3334import java.io.*;35import java.net.*;36import jdk.test.lib.net.URIBuilder;3738public class SetChunkedStreamingMode implements HttpCallback {3940void okReply (HttpTransaction req) throws IOException {41req.setResponseEntityBody ("Hello .");42req.sendResponse (200, "Ok");43System.out.println ("Server: sent response");44req.orderlyClose();45}4647public void request (HttpTransaction req) {48try {49okReply (req);50} catch (IOException e) {51e.printStackTrace();52}53}5455static void read (InputStream is) throws IOException {56int c;57System.out.println ("reading");58while ((c=is.read()) != -1) {59System.out.write (c);60}61System.out.println ("");62System.out.println ("finished reading");63}6465static TestHttpServer server;6667public static void main (String[] args) throws Exception {68try {69server = new TestHttpServer(new SetChunkedStreamingMode(), 1, 10,70InetAddress.getLoopbackAddress(), 0);71System.out.println ("Server: listening on port: " + server.getLocalPort());72URL url = URIBuilder.newBuilder()73.scheme("http")74.loopback()75.port(server.getLocalPort())76.path("/")77.toURL();78System.out.println ("Client: connecting to " + url);79HttpURLConnection urlc = (HttpURLConnection)url.openConnection();80urlc.setChunkedStreamingMode (0);81urlc.setRequestMethod("POST");82urlc.setDoOutput(true);83InputStream is = urlc.getInputStream();84} catch (Exception e) {85if (server != null) {86server.terminate();87}88throw e;89}90server.terminate();91}9293public static void except (String s) {94server.terminate();95throw new RuntimeException (s);96}97}9899100