Path: blob/master/test/jdk/sun/net/www/protocol/http/B8012625.java
41159 views
/*1* Copyright (c) 2013, 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 801262526* @library /test/lib27* @modules jdk.httpserver28* @run main B801262529*/3031import java.net.*;32import java.io.*;3334import java.net.*;35import java.io.*;36import java.util.concurrent.*;3738import jdk.test.lib.net.URIBuilder;3940import com.sun.net.httpserver.*;4142public class B8012625 implements HttpHandler {4344public static void main (String[] args) throws Exception {45B8012625 test = new B8012625();46test.run();47}4849public void run() throws Exception {50URL url = URIBuilder.newBuilder()51.scheme("http")52.loopback()53.port(port)54.path("/foo")55.toURL();56System.out.println("URL: " + url);57HttpURLConnection uc = (HttpURLConnection)url.openConnection();58uc.setDoOutput(true);59uc.setRequestMethod("POST");60uc.addRequestProperty("Expect", "100-Continue");61//uc.setFixedLengthStreamingMode(256);62System.out.println ("Client: getting outputstream");63long before = System.currentTimeMillis();64OutputStream os = uc.getOutputStream();65long after = System.currentTimeMillis();66System.out.println ("Client: writing to outputstream");67byte[] buf = new byte[256];68os.write(buf);69System.out.println ("Client: done writing ");70int r = uc.getResponseCode();71System.out.println ("Client: received response code " + r);72server.stop(1);73ex.shutdownNow();74if (after - before >= 5000) {75throw new RuntimeException("Error: 5 second delay seen");76}77}7879int port;80HttpServer server;81ExecutorService ex;8283public B8012625 () throws Exception {84InetAddress loopback = InetAddress.getLoopbackAddress();85server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);86HttpContext ctx = server.createContext("/", this);87ex = Executors.newFixedThreadPool(5);88server.setExecutor(ex);89server.start();90port = server.getAddress().getPort();91}9293public void handle(HttpExchange ex) throws IOException {94String s = ex.getRequestMethod();95if (!s.equals("POST")) {96ex.getResponseHeaders().set("Allow", "POST");97ex.sendResponseHeaders(500, -1);98ex.close();99return;100}101System.out.println ("Server: reading request body");102InputStream is = ex.getRequestBody();103// read request104byte[] buf = new byte [1024];105while (is.read(buf) != -1) ;106is.close();107ex.sendResponseHeaders(200, -1);108ex.close();109}110}111112113