Path: blob/master/test/jdk/sun/net/www/protocol/http/UserAuth.java
41159 views
/*1* Copyright (c) 2006, 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 642112226* @modules jdk.httpserver27* @run main/othervm UserAuth28* @summary Authorization header removed for preemptive authentication by user code29*/3031import java.net.*;32import com.sun.net.httpserver.*;33import java.util.*;34import java.io.*;35import java.util.concurrent.Executors;36import java.util.concurrent.ExecutorService;37import static java.net.Proxy.NO_PROXY;3839public class UserAuth40{41com.sun.net.httpserver.HttpServer httpServer;42ExecutorService executorService;4344public static void main(String[] args) {45new UserAuth();46}4748public UserAuth() {49try {50startHttpServer();51doClient();52} catch (IOException ioe) {53ioe.printStackTrace();54}55}5657void doClient() {58try {59InetSocketAddress address = httpServer.getAddress();6061// GET Request62URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/redirect/");63HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);64uc.setRequestProperty("Authorization", "testString:ValueDoesNotMatter");65int resp = uc.getResponseCode();6667System.out.println("Response Code is " + resp);68if (resp != 200)69throw new RuntimeException("Failed: Authorization header was not retained after redirect");7071} catch (IOException e) {72e.printStackTrace();73} finally {74httpServer.stop(1);75executorService.shutdown();76}77}7879/**80* Http Server81*/82void startHttpServer() throws IOException {83InetAddress address = InetAddress.getLocalHost();84if (!InetAddress.getByName(address.getHostName()).equals(address)) {85// if this happens then we should possibly change the client86// side to use the address literal in its URL instead of87// the host name.88throw new IOException(address.getHostName()89+ " resolves to "90+ InetAddress.getByName(address.getHostName())91+ " not to "92+ address + ": check host configuration.");93}9495httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(address, 0), 0);9697// create HttpServer context98HttpContext ctx = httpServer.createContext("/redirect/", new RedirectHandler());99HttpContext ctx1 = httpServer.createContext("/doStuff/", new HasAuthHandler());100101executorService = Executors.newCachedThreadPool();102httpServer.setExecutor(executorService);103httpServer.start();104}105106class RedirectHandler implements HttpHandler {107public void handle(HttpExchange t) throws IOException {108InetSocketAddress address = httpServer.getAddress();109String redirectUrl = "http://" + address.getHostName() + ":" + address.getPort() + "/doStuff/";110111Headers resHeaders = t.getResponseHeaders();112resHeaders.add("Location", redirectUrl);113114t.sendResponseHeaders(307, -1);115t.close();116}117}118119class HasAuthHandler implements HttpHandler {120public void handle(HttpExchange t) throws IOException {121Headers reqHeaders = t.getRequestHeaders();122123List<String> auth = reqHeaders.get("Authorization");124125if (auth == null || !auth.get(0).equals("testString:ValueDoesNotMatter"))126t.sendResponseHeaders(400, -1);127128t.sendResponseHeaders(200, -1);129t.close();130}131}132133134135}136137138