Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/UserAuth.java
38867 views
1
/*
2
* Copyright (c) 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 6421122
27
* @run main/othervm UserAuth
28
* @summary Authorization header removed for preemptive authentication by user code
29
*/
30
31
import java.net.*;
32
import com.sun.net.httpserver.*;
33
import java.util.*;
34
import java.io.*;
35
import java.util.concurrent.Executors;
36
import java.util.concurrent.ExecutorService;
37
38
39
public class UserAuth
40
{
41
com.sun.net.httpserver.HttpServer httpServer;
42
ExecutorService executorService;
43
44
public static void main(String[] args) {
45
new UserAuth();
46
}
47
48
public UserAuth() {
49
try {
50
startHttpServer();
51
doClient();
52
} catch (IOException ioe) {
53
ioe.printStackTrace();
54
}
55
}
56
57
void doClient() {
58
try {
59
InetSocketAddress address = httpServer.getAddress();
60
61
// GET Request
62
URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/redirect/");
63
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
64
uc.setRequestProperty("Authorization", "testString:ValueDoesNotMatter");
65
int resp = uc.getResponseCode();
66
67
System.out.println("Response Code is " + resp);
68
if (resp != 200)
69
throw new RuntimeException("Failed: Authorization header was not retained after redirect");
70
71
} catch (IOException e) {
72
e.printStackTrace();
73
} finally {
74
httpServer.stop(1);
75
executorService.shutdown();
76
}
77
}
78
79
/**
80
* Http Server
81
*/
82
void startHttpServer() throws IOException {
83
httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
84
85
// create HttpServer context
86
HttpContext ctx = httpServer.createContext("/redirect/", new RedirectHandler());
87
HttpContext ctx1 = httpServer.createContext("/doStuff/", new HasAuthHandler());
88
89
executorService = Executors.newCachedThreadPool();
90
httpServer.setExecutor(executorService);
91
httpServer.start();
92
}
93
94
class RedirectHandler implements HttpHandler {
95
public void handle(HttpExchange t) throws IOException {
96
InetSocketAddress address = httpServer.getAddress();
97
String redirectUrl = "http://" + address.getHostName() + ":" + address.getPort() + "/doStuff/";
98
99
Headers resHeaders = t.getResponseHeaders();
100
resHeaders.add("Location", redirectUrl);
101
102
t.sendResponseHeaders(307, -1);
103
t.close();
104
}
105
}
106
107
class HasAuthHandler implements HttpHandler {
108
public void handle(HttpExchange t) throws IOException {
109
Headers reqHeaders = t.getRequestHeaders();
110
111
List<String> auth = reqHeaders.get("Authorization");
112
113
if (auth == null || !auth.get(0).equals("testString:ValueDoesNotMatter"))
114
t.sendResponseHeaders(400, -1);
115
116
t.sendResponseHeaders(200, -1);
117
t.close();
118
}
119
}
120
121
122
123
}
124
125