1/*
2 * Copyright (c) 2005, 2008, 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 6744329
27 * @summary  Exception in light weight Http server
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 B6744329 {
41
42    public static void main (String[] args) throws Exception {
43        Handler handler = new Handler();
44        InetSocketAddress addr = new InetSocketAddress (0);
45        HttpServer server = HttpServer.create (addr, 0);
46        HttpContext ctx = server.createContext ("/test", handler);
47        ExecutorService executor = Executors.newCachedThreadPool();
48        server.setExecutor (executor);
49        server.start ();
50
51        URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
52        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
53        try {
54            InputStream is = urlc.getInputStream();
55            int c = 0;
56            while (is.read()!= -1) {
57                c ++;
58            }
59            System.out.println ("OK");
60        } catch (IOException e) {
61            System.out.println ("exception");
62            error = true;
63        }
64        server.stop(2);
65        executor.shutdown();
66        if (error) {
67            throw new RuntimeException ("Test failed");
68        }
69    }
70
71    public static boolean error = false;
72
73    /* this must be the same size as in ChunkedOutputStream.java
74     */
75    final static int CHUNK_SIZE = 4096;
76
77    static class Handler implements HttpHandler {
78        int invocation = 1;
79        public void handle (HttpExchange t)
80            throws IOException
81        {
82            InputStream is = t.getRequestBody();
83            Headers map = t.getRequestHeaders();
84            Headers rmap = t.getResponseHeaders();
85            while (is.read () != -1) ;
86            is.close();
87            /* chunked response */
88            t.sendResponseHeaders (200, 0);
89            OutputStream os = t.getResponseBody();
90            byte[] first = new byte [CHUNK_SIZE * 2];
91            byte[] second = new byte [2];
92            os.write (first);
93            os.write ('x');
94            os.write ('x');
95            /* An index out of bounds exception will be thrown
96             * below, which is caught by server, and connection
97             * will be closed. resulting in IOException to client
98             * - if bug present
99             */
100            os.write ('x');
101            os.write ('x');
102            os.write ('x');
103            t.close();
104        }
105    }
106}
107