1/*
2 * Copyright (c) 2005, 2006, 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 6341616
27 * @summary  Server doesnt send response if there is a RuntimeException in validate of BasicAuthFilter
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 B6341616 {
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        BasicAuthenticator filter = new BasicAuthenticator ("foobar@test.realm") {
48            public boolean checkCredentials (String username, String pw) {
49                throw new RuntimeException ("");
50            }
51        };
52
53        ctx.setAuthenticator (filter);
54        ExecutorService executor = Executors.newCachedThreadPool();
55        server.setExecutor (executor);
56        server.start ();
57        java.net.Authenticator.setDefault (new MyAuthenticator());
58
59        URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
60        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
61        try {
62            InputStream is = urlc.getInputStream();
63            int c = 0;
64            while (is.read()!= -1) {
65                c ++;
66            }
67        } catch (IOException e) {
68            server.stop(2);
69            executor.shutdown();
70            System.out.println ("OK");
71        }
72    }
73
74    public static boolean error = false;
75
76    static class MyAuthenticator extends java.net.Authenticator {
77        public PasswordAuthentication getPasswordAuthentication () {
78            PasswordAuthentication pw;
79            if (!getRequestingPrompt().equals ("foobar@test.realm")) {
80                B6341616.error = true;
81            }
82            pw = new PasswordAuthentication ("fred", "xyz".toCharArray());
83            return pw;
84        }
85    }
86
87    static class Handler implements HttpHandler {
88        int invocation = 1;
89        public void handle (HttpExchange t)
90            throws IOException
91        {
92            InputStream is = t.getRequestBody();
93            Headers map = t.getRequestHeaders();
94            Headers rmap = t.getResponseHeaders();
95            while (is.read () != -1) ;
96            is.close();
97            t.sendResponseHeaders (200, -1);
98            t.close();
99        }
100    }
101}
102