Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/UserAuth.java
38867 views
/*1* Copyright (c) 2006, 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 642112226* @run main/othervm UserAuth27* @summary Authorization header removed for preemptive authentication by user code28*/2930import java.net.*;31import com.sun.net.httpserver.*;32import java.util.*;33import java.io.*;34import java.util.concurrent.Executors;35import java.util.concurrent.ExecutorService;363738public class UserAuth39{40com.sun.net.httpserver.HttpServer httpServer;41ExecutorService executorService;4243public static void main(String[] args) {44new UserAuth();45}4647public UserAuth() {48try {49startHttpServer();50doClient();51} catch (IOException ioe) {52ioe.printStackTrace();53}54}5556void doClient() {57try {58InetSocketAddress address = httpServer.getAddress();5960// GET Request61URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/redirect/");62HttpURLConnection uc = (HttpURLConnection)url.openConnection();63uc.setRequestProperty("Authorization", "testString:ValueDoesNotMatter");64int resp = uc.getResponseCode();6566System.out.println("Response Code is " + resp);67if (resp != 200)68throw new RuntimeException("Failed: Authorization header was not retained after redirect");6970} catch (IOException e) {71e.printStackTrace();72} finally {73httpServer.stop(1);74executorService.shutdown();75}76}7778/**79* Http Server80*/81void startHttpServer() throws IOException {82httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);8384// create HttpServer context85HttpContext ctx = httpServer.createContext("/redirect/", new RedirectHandler());86HttpContext ctx1 = httpServer.createContext("/doStuff/", new HasAuthHandler());8788executorService = Executors.newCachedThreadPool();89httpServer.setExecutor(executorService);90httpServer.start();91}9293class RedirectHandler implements HttpHandler {94public void handle(HttpExchange t) throws IOException {95InetSocketAddress address = httpServer.getAddress();96String redirectUrl = "http://" + address.getHostName() + ":" + address.getPort() + "/doStuff/";9798Headers resHeaders = t.getResponseHeaders();99resHeaders.add("Location", redirectUrl);100101t.sendResponseHeaders(307, -1);102t.close();103}104}105106class HasAuthHandler implements HttpHandler {107public void handle(HttpExchange t) throws IOException {108Headers reqHeaders = t.getRequestHeaders();109110List<String> auth = reqHeaders.get("Authorization");111112if (auth == null || !auth.get(0).equals("testString:ValueDoesNotMatter"))113t.sendResponseHeaders(400, -1);114115t.sendResponseHeaders(200, -1);116t.close();117}118}119120121122}123124125