1/*
2 * Copyright (c) 2007, 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 6526158
27 * @summary  HttpExchange.getRequestBody().close() throws Exception
28 */
29
30import com.sun.net.httpserver.*;
31
32import java.util.*;
33import java.util.concurrent.*;
34import java.io.*;
35import java.net.*;
36import java.security.*;
37import java.security.cert.*;
38import javax.net.ssl.*;
39
40public class B6526158 {
41
42    /* keep under 64 k */
43    final static int SIZE = 60 * 1024;
44
45    public static void main (String[] args) throws Exception {
46        Handler handler = new Handler();
47        InetSocketAddress addr = new InetSocketAddress (0);
48        HttpServer server = HttpServer.create (addr, 0);
49        HttpContext ctx = server.createContext ("/test", handler);
50
51        ExecutorService executor = Executors.newCachedThreadPool();
52        server.setExecutor (executor);
53        server.start ();
54
55        URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
56        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
57        urlc.setDoOutput (true);
58        try {
59            OutputStream os = new BufferedOutputStream (urlc.getOutputStream());
60            for (int i=0; i< SIZE; i++) {
61                os.write (i);
62            }
63            os.close();
64            InputStream is = urlc.getInputStream();
65            int c = 0;
66            while (is.read()!= -1) {
67                c ++;
68            }
69            is.close();
70        } finally {
71            server.stop(2);
72            executor.shutdown();
73        }
74        if (error) {
75            throw new RuntimeException ("Test failed");
76        }
77    }
78
79    public static boolean error = false;
80
81    static class Handler implements HttpHandler {
82        int invocation = 1;
83        public void handle (HttpExchange t)
84            throws IOException
85        {
86            InputStream is = t.getRequestBody();
87            try {
88                is.close();
89            } catch (IOException e) {
90                e.printStackTrace();
91                error = true;
92            }
93            t.sendResponseHeaders (200, -1);
94            t.close();
95        }
96    }
97}
98