Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6341616.java
41154 views
/*1* Copyright (c) 2005, 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 634161626* @library /test/lib27* @run main B634161628* @run main/othervm -Djava.net.preferIPv6Addresses=true B634161629* @summary Server doesnt send response if there is a RuntimeException in validate of BasicAuthFilter30*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.io.*;37import java.net.*;38import java.security.*;39import java.security.cert.*;40import javax.net.ssl.*;41import jdk.test.lib.net.URIBuilder;4243public class B6341616 {4445public static void main (String[] args) throws Exception {46Handler handler = new Handler();47InetAddress loopback = InetAddress.getLoopbackAddress();48InetSocketAddress addr = new InetSocketAddress (loopback, 0);49HttpServer server = HttpServer.create (addr, 0);50HttpContext ctx = server.createContext ("/test", handler);51BasicAuthenticator filter = new BasicAuthenticator ("[email protected]") {52public boolean checkCredentials (String username, String pw) {53throw new RuntimeException ("");54}55};5657ctx.setAuthenticator (filter);58ExecutorService executor = Executors.newCachedThreadPool();59server.setExecutor (executor);60server.start ();61java.net.Authenticator.setDefault (new MyAuthenticator());6263URL url = URIBuilder.newBuilder()64.scheme("http")65.loopback()66.port(server.getAddress().getPort())67.path("/test/foo.html")68.toURL();69HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);70try {71InputStream is = urlc.getInputStream();72int c = 0;73while (is.read()!= -1) {74c ++;75}76} catch (IOException e) {77server.stop(2);78executor.shutdown();79System.out.println ("OK");80}81}8283public static boolean error = false;8485static class MyAuthenticator extends java.net.Authenticator {86public PasswordAuthentication getPasswordAuthentication () {87PasswordAuthentication pw;88if (!getRequestingPrompt().equals ("[email protected]")) {89B6341616.error = true;90}91pw = new PasswordAuthentication ("fred", "xyz".toCharArray());92return pw;93}94}9596static class Handler implements HttpHandler {97int invocation = 1;98public void handle (HttpExchange t)99throws IOException100{101InputStream is = t.getRequestBody();102Headers map = t.getRequestHeaders();103Headers rmap = t.getResponseHeaders();104while (is.read () != -1) ;105is.close();106t.sendResponseHeaders (200, -1);107t.close();108}109}110}111112113