Path: blob/master/test/jdk/sun/net/www/protocol/http/StreamingOutputStream.java
41159 views
/*1* Copyright (c) 2006, 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 647225026* @modules jdk.httpserver27* @run main/othervm StreamingOutputStream28* @summary HttpURLConnection.getOutputStream streaming mode bug when called multiple times29*/3031import java.net.*;32import java.io.*;33import com.sun.net.httpserver.*;3435/*36* Calling HttpURLConnection.getOutputStream multiple times when streaming37* should not create a new OutputStream each time.38*/3940public class StreamingOutputStream41{42com.sun.net.httpserver.HttpServer httpServer;4344public static void main(String[] args) {45new StreamingOutputStream();46}4748public StreamingOutputStream() {49try {50startHttpServer();51doClient();52} catch (IOException ioe) {53ioe.printStackTrace();54}55}5657void doClient() {58try {59InetSocketAddress address = httpServer.getAddress();6061URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");62HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);6364uc.setDoOutput(true);65uc.setFixedLengthStreamingMode(1);66OutputStream os1 = uc.getOutputStream();67OutputStream os2 = uc.getOutputStream();6869if (os1 != os2)70throw new RuntimeException("Failed: OutputStreams should reference the same object");7172os2.write('b');73os2.close();7475int resp = uc.getResponseCode();76if (resp != 200)77throw new RuntimeException("Failed: Server should return 200 OK");7879} catch (IOException e) {80e.printStackTrace();81} finally {82httpServer.stop(1);83}84}85/**86* Http Server87*/88void startHttpServer() throws IOException {89InetAddress address = InetAddress.getLocalHost();90if (!InetAddress.getByName(address.getHostName()).equals(address)) {91// if this happens then we should possibly change the client92// side to use the address literal in its URL instead of93// the host name.94throw new IOException(address.getHostName()95+ " resolves to "96+ InetAddress.getByName(address.getHostName())97+ " not to "98+ address + ": check host configuration.");99}100httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0);101HttpContext ctx = httpServer.createContext("/test/", new MyHandler());102httpServer.start();103}104105class MyHandler implements HttpHandler {106public void handle(HttpExchange t) throws IOException {107InputStream is = t.getRequestBody();108while(is.read() != -1);109is.close();110111t.sendResponseHeaders(200, -1);112t.close();113}114}115}116117118