Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/B8012625.java
38867 views
/*1* Copyright (c) 2013, 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 801262526* @run main B801262527*/2829import java.net.*;30import java.io.*;3132import java.net.*;33import java.io.*;34import java.util.concurrent.*;35import com.sun.net.httpserver.*;3637public class B8012625 implements HttpHandler {3839public static void main (String[] args) throws Exception {40B8012625 test = new B8012625();41test.run();42}4344public void run() throws Exception {45String u = "http://127.0.0.1:" + port + "/foo";46URL url = new URL(u);47HttpURLConnection uc = (HttpURLConnection)url.openConnection();48uc.setDoOutput(true);49uc.setRequestMethod("POST");50uc.addRequestProperty("Expect", "100-Continue");51//uc.setFixedLengthStreamingMode(256);52System.out.println ("Client: getting outputstream");53long before = System.currentTimeMillis();54OutputStream os = uc.getOutputStream();55long after = System.currentTimeMillis();56System.out.println ("Client: writing to outputstream");57byte[] buf = new byte[256];58os.write(buf);59System.out.println ("Client: done writing ");60int r = uc.getResponseCode();61System.out.println ("Client: received response code " + r);62server.stop(1);63ex.shutdownNow();64if (after - before >= 5000) {65throw new RuntimeException("Error: 5 second delay seen");66}67}6869int port;70HttpServer server;71ExecutorService ex;7273public B8012625 () throws Exception {74server = HttpServer.create(new InetSocketAddress(0), 10);75HttpContext ctx = server.createContext("/", this);76ex = Executors.newFixedThreadPool(5);77server.setExecutor(ex);78server.start();79port = server.getAddress().getPort();80}8182public void handle(HttpExchange ex) throws IOException {83String s = ex.getRequestMethod();84if (!s.equals("POST")) {85ex.getResponseHeaders().set("Allow", "POST");86ex.sendResponseHeaders(500, -1);87ex.close();88return;89}90System.out.println ("Server: reading request body");91InputStream is = ex.getRequestBody();92// read request93byte[] buf = new byte [1024];94while (is.read(buf) != -1) ;95is.close();96ex.sendResponseHeaders(200, -1);97ex.close();98}99}100101102