1/*
2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 8012625
27 * @modules jdk.httpserver
28 * @run main B8012625
29 */
30
31import java.net.*;
32import java.io.*;
33
34import java.net.*;
35import java.io.*;
36import java.util.concurrent.*;
37import com.sun.net.httpserver.*;
38
39public class B8012625 implements HttpHandler {
40
41    public static void main (String[] args) throws Exception {
42        B8012625 test = new B8012625();
43        test.run();
44    }
45
46    public void run() throws Exception {
47        String u = "http://127.0.0.1:" + port + "/foo";
48        URL url = new URL(u);
49        HttpURLConnection uc = (HttpURLConnection)url.openConnection();
50        uc.setDoOutput(true);
51        uc.setRequestMethod("POST");
52        uc.addRequestProperty("Expect", "100-Continue");
53        //uc.setFixedLengthStreamingMode(256);
54        System.out.println ("Client: getting outputstream");
55        long before = System.currentTimeMillis();
56        OutputStream os = uc.getOutputStream();
57        long after = System.currentTimeMillis();
58        System.out.println ("Client: writing to outputstream");
59        byte[] buf = new byte[256];
60        os.write(buf);
61        System.out.println ("Client: done writing ");
62        int r = uc.getResponseCode();
63        System.out.println ("Client: received response code " + r);
64        server.stop(1);
65        ex.shutdownNow();
66        if (after - before >= 5000) {
67            throw new RuntimeException("Error: 5 second delay seen");
68        }
69    }
70
71    int port;
72    HttpServer server;
73    ExecutorService ex;
74
75    public B8012625 () throws Exception {
76        server = HttpServer.create(new InetSocketAddress(0), 10);
77        HttpContext ctx = server.createContext("/", this);
78        ex = Executors.newFixedThreadPool(5);
79        server.setExecutor(ex);
80        server.start();
81        port = server.getAddress().getPort();
82   }
83
84    public void handle(HttpExchange ex) throws IOException {
85        String s = ex.getRequestMethod();
86        if (!s.equals("POST")) {
87            ex.getResponseHeaders().set("Allow", "POST");
88            ex.sendResponseHeaders(500, -1);
89            ex.close();
90            return;
91        }
92        System.out.println ("Server: reading request body");
93        InputStream is = ex.getRequestBody();
94        // read request
95        byte[] buf = new byte [1024];
96        while (is.read(buf) != -1) ;
97        is.close();
98        ex.sendResponseHeaders(200, -1);
99        ex.close();
100   }
101}
102