author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57994 | 197f36af94f3 |
permissions | -rw-r--r-- |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1 |
/* |
54086
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
52387
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
4 |
* |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
52387 | 7 |
* published by the Free Software Foundation. |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
8 |
* |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
13 |
* accompanied this code). |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
14 |
* |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
18 |
* |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
21 |
* questions. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
22 |
*/ |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
23 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
24 |
import com.sun.net.httpserver.BasicAuthenticator; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
25 |
import com.sun.net.httpserver.Filter; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
26 |
import com.sun.net.httpserver.Headers; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
27 |
import com.sun.net.httpserver.HttpContext; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
28 |
import com.sun.net.httpserver.HttpExchange; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
29 |
import com.sun.net.httpserver.HttpHandler; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
30 |
import com.sun.net.httpserver.HttpServer; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
31 |
import com.sun.net.httpserver.HttpsConfigurator; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
32 |
import com.sun.net.httpserver.HttpsParameters; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
33 |
import com.sun.net.httpserver.HttpsServer; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
34 |
import java.io.IOException; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
35 |
import java.io.InputStream; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
36 |
import java.io.OutputStream; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
37 |
import java.io.OutputStreamWriter; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
38 |
import java.io.PrintWriter; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
39 |
import java.io.Writer; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
40 |
import java.math.BigInteger; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
41 |
import java.net.HttpURLConnection; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
42 |
import java.net.InetAddress; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
43 |
import java.net.InetSocketAddress; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
44 |
import java.net.MalformedURLException; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
45 |
import java.net.ServerSocket; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
46 |
import java.net.Socket; |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
47 |
import java.net.SocketAddress; |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
48 |
import java.net.URL; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
49 |
import java.security.MessageDigest; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
50 |
import java.security.NoSuchAlgorithmException; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
51 |
import java.time.Instant; |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
52 |
import java.util.ArrayList; |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
53 |
import java.util.Arrays; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
54 |
import java.util.Base64; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
55 |
import java.util.List; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
56 |
import java.util.Objects; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
57 |
import java.util.Random; |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
58 |
import java.util.concurrent.CopyOnWriteArrayList; |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
59 |
import java.util.stream.Collectors; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
60 |
import javax.net.ssl.SSLContext; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
61 |
import sun.net.www.HeaderParser; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
62 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
63 |
/** |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
64 |
* A simple HTTP server that supports Digest authentication. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
65 |
* By default this server will echo back whatever is present |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
66 |
* in the request body. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
67 |
* @author danielfuchs |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
68 |
*/ |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
69 |
public class HTTPTestServer extends HTTPTest { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
70 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
71 |
final HttpServer serverImpl; // this server endpoint |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
72 |
final HTTPTestServer redirect; // the target server where to redirect 3xx |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
73 |
final HttpHandler delegate; // unused |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
74 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
75 |
private HTTPTestServer(HttpServer server, HTTPTestServer target, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
76 |
HttpHandler delegate) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
77 |
this.serverImpl = server; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
78 |
this.redirect = target; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
79 |
this.delegate = delegate; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
80 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
81 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
82 |
public static void main(String[] args) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
83 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
84 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
85 |
HTTPTestServer server = create(HTTPTest.DEFAULT_PROTOCOL_TYPE, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
86 |
HTTPTest.DEFAULT_HTTP_AUTH_TYPE, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
87 |
HTTPTest.AUTHENTICATOR, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
88 |
HTTPTest.DEFAULT_SCHEME_TYPE); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
89 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
90 |
System.out.println("Server created at " + server.getAddress()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
91 |
System.out.println("Strike <Return> to exit"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
92 |
System.in.read(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
93 |
} finally { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
94 |
System.out.println("stopping server"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
95 |
server.stop(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
96 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
97 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
98 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
99 |
private static String toString(Headers headers) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
100 |
return headers.entrySet().stream() |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
101 |
.map((e) -> e.getKey() + ": " + e.getValue()) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
102 |
.collect(Collectors.joining("\n")); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
103 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
104 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
105 |
public static HTTPTestServer create(HttpProtocolType protocol, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
106 |
HttpAuthType authType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
107 |
HttpTestAuthenticator auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
108 |
HttpSchemeType schemeType) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
109 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
110 |
return create(protocol, authType, auth, schemeType, null); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
111 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
112 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
113 |
public static HTTPTestServer create(HttpProtocolType protocol, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
114 |
HttpAuthType authType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
115 |
HttpTestAuthenticator auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
116 |
HttpSchemeType schemeType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
117 |
HttpHandler delegate) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
118 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
119 |
Objects.requireNonNull(authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
120 |
Objects.requireNonNull(auth); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
121 |
switch(authType) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
122 |
// A server that performs Server Digest authentication. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
123 |
case SERVER: return createServer(protocol, authType, auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
124 |
schemeType, delegate, "/"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
125 |
// A server that pretends to be a Proxy and performs |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
126 |
// Proxy Digest authentication. If protocol is HTTPS, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
127 |
// then this will create a HttpsProxyTunnel that will |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
128 |
// handle the CONNECT request for tunneling. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
129 |
case PROXY: return createProxy(protocol, authType, auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
130 |
schemeType, delegate, "/"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
131 |
// A server that sends 307 redirect to a server that performs |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
132 |
// Digest authentication. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
133 |
// Note: 301 doesn't work here because it transforms POST into GET. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
134 |
case SERVER307: return createServerAndRedirect(protocol, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
135 |
HttpAuthType.SERVER, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
136 |
auth, schemeType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
137 |
delegate, 307); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
138 |
// A server that sends 305 redirect to a proxy that performs |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
139 |
// Digest authentication. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
140 |
case PROXY305: return createServerAndRedirect(protocol, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
141 |
HttpAuthType.PROXY, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
142 |
auth, schemeType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
143 |
delegate, 305); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
144 |
default: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
145 |
throw new InternalError("Unknown server type: " + authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
146 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
147 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
148 |
|
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
149 |
/** |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
150 |
* The SocketBindableFactory ensures that the local port used by an HttpServer |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
151 |
* or a proxy ServerSocket previously created by the current test/VM will not |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
152 |
* get reused by a subsequent test in the same VM. This is to avoid having the |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
153 |
* AuthCache reuse credentials from previous tests - which would invalidate the |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
154 |
* assumptions made by the current test on when the default authenticator should |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
155 |
* be called. |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
156 |
*/ |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
157 |
private static abstract class SocketBindableFactory<B> { |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
158 |
private static final int MAX = 10; |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
159 |
private static final CopyOnWriteArrayList<String> addresses = |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
160 |
new CopyOnWriteArrayList<>(); |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
161 |
protected B createInternal() throws IOException { |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
162 |
final int max = addresses.size() + MAX; |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
163 |
final List<B> toClose = new ArrayList<>(); |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
164 |
try { |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
165 |
for (int i = 1; i <= max; i++) { |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
166 |
B bindable = createBindable(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
167 |
SocketAddress address = getAddress(bindable); |
57793
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
168 |
String key = toString(address); |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
169 |
if (addresses.addIfAbsent(key)) { |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
170 |
System.out.println("Socket bound to: " + key |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
171 |
+ " after " + i + " attempt(s)"); |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
172 |
return bindable; |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
173 |
} |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
174 |
System.out.println("warning: address " + key |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
175 |
+ " already used. Retrying bind."); |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
176 |
// keep the port bound until we get a port that we haven't |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
177 |
// used already |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
178 |
toClose.add(bindable); |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
179 |
} |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
180 |
} finally { |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
181 |
// if we had to retry, then close the socket we're not |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
182 |
// going to use. |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
183 |
for (B b : toClose) { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
184 |
try { close(b); } catch (Exception x) { /* ignore */ } |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
185 |
} |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
186 |
} |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
187 |
throw new IOException("Couldn't bind socket after " + max + " attempts: " |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
188 |
+ "addresses used before: " + addresses); |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
189 |
} |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
190 |
|
57793
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
191 |
private static String toString(SocketAddress address) { |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
192 |
// We don't rely on address.toString(): sometimes it can be |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
193 |
// "/127.0.0.1:port", sometimes it can be "localhost/127.0.0.1:port" |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
194 |
// Instead we compose our own string representation: |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
195 |
InetSocketAddress candidate = (InetSocketAddress) address; |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
196 |
String hostAddr = candidate.getAddress().getHostAddress(); |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
197 |
if (hostAddr.contains(":")) hostAddr = "[" + hostAddr + "]"; |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
198 |
return hostAddr + ":" + candidate.getPort(); |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
199 |
} |
d372747e8f08
8191169: java/net/Authenticator/B4769350.java failed intermittently
dfuchs
parents:
55522
diff
changeset
|
200 |
|
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
201 |
protected abstract B createBindable() throws IOException; |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
202 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
203 |
protected abstract SocketAddress getAddress(B bindable); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
204 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
205 |
protected abstract void close(B bindable) throws IOException; |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
206 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
207 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
208 |
/* |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
209 |
* Used to create ServerSocket for a proxy. |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
210 |
*/ |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
211 |
private static final class ServerSocketFactory |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
212 |
extends SocketBindableFactory<ServerSocket> { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
213 |
private static final ServerSocketFactory instance = new ServerSocketFactory(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
214 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
215 |
static ServerSocket create() throws IOException { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
216 |
return instance.createInternal(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
217 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
218 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
219 |
@Override |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
220 |
protected ServerSocket createBindable() throws IOException { |
54086
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
52387
diff
changeset
|
221 |
InetAddress address = InetAddress.getLoopbackAddress(); |
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
52387
diff
changeset
|
222 |
return new ServerSocket(0, 0, address); |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
223 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
224 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
225 |
@Override |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
226 |
protected SocketAddress getAddress(ServerSocket socket) { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
227 |
return socket.getLocalSocketAddress(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
228 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
229 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
230 |
@Override |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
231 |
protected void close(ServerSocket socket) throws IOException { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
232 |
socket.close(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
233 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
234 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
235 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
236 |
/* |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
237 |
* Used to create HttpServer for a NTLMTestServer. |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
238 |
*/ |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
239 |
private static abstract class WebServerFactory<S extends HttpServer> |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
240 |
extends SocketBindableFactory<S> { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
241 |
@Override |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
242 |
protected S createBindable() throws IOException { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
243 |
S server = newHttpServer(); |
54086
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
52387
diff
changeset
|
244 |
InetAddress address = InetAddress.getLoopbackAddress(); |
ccb4a50bee06
8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents:
52387
diff
changeset
|
245 |
server.bind(new InetSocketAddress(address, 0), 0); |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
246 |
return server; |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
247 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
248 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
249 |
@Override |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
250 |
protected SocketAddress getAddress(S server) { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
251 |
return server.getAddress(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
252 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
253 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
254 |
@Override |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
255 |
protected void close(S server) throws IOException { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
256 |
server.stop(1); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
257 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
258 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
259 |
/* |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
260 |
* Returns a HttpServer or a HttpsServer in different subclasses. |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
261 |
*/ |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
262 |
protected abstract S newHttpServer() throws IOException; |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
263 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
264 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
265 |
private static final class HttpServerFactory extends WebServerFactory<HttpServer> { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
266 |
private static final HttpServerFactory instance = new HttpServerFactory(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
267 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
268 |
static HttpServer create() throws IOException { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
269 |
return instance.createInternal(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
270 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
271 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
272 |
@Override |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
273 |
protected HttpServer newHttpServer() throws IOException { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
274 |
return HttpServer.create(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
275 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
276 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
277 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
278 |
private static final class HttpsServerFactory extends WebServerFactory<HttpsServer> { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
279 |
private static final HttpsServerFactory instance = new HttpsServerFactory(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
280 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
281 |
static HttpsServer create() throws IOException { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
282 |
return instance.createInternal(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
283 |
} |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
284 |
|
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
285 |
@Override |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
286 |
protected HttpsServer newHttpServer() throws IOException { |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
287 |
return HttpsServer.create(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
288 |
} |
43508
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
289 |
} |
6b8bca7f0cee
8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents:
42351
diff
changeset
|
290 |
|
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
291 |
static HttpServer createHttpServer(HttpProtocolType protocol) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
292 |
switch (protocol) { |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
293 |
case HTTP: return HttpServerFactory.create(); |
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
294 |
case HTTPS: return configure(HttpsServerFactory.create()); |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
295 |
default: throw new InternalError("Unsupported protocol " + protocol); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
296 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
297 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
298 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
299 |
static HttpsServer configure(HttpsServer server) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
300 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
301 |
SSLContext ctx = SSLContext.getDefault(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
302 |
server.setHttpsConfigurator(new Configurator(ctx)); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
303 |
} catch (NoSuchAlgorithmException ex) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
304 |
throw new IOException(ex); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
305 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
306 |
return server; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
307 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
308 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
309 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
310 |
static void setContextAuthenticator(HttpContext ctxt, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
311 |
HttpTestAuthenticator auth) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
312 |
final String realm = auth.getRealm(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
313 |
com.sun.net.httpserver.Authenticator authenticator = |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
314 |
new BasicAuthenticator(realm) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
315 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
316 |
public boolean checkCredentials(String username, String pwd) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
317 |
return auth.getUserName().equals(username) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
318 |
&& new String(auth.getPassword(username)).equals(pwd); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
319 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
320 |
}; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
321 |
ctxt.setAuthenticator(authenticator); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
322 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
323 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
324 |
public static HTTPTestServer createServer(HttpProtocolType protocol, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
325 |
HttpAuthType authType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
326 |
HttpTestAuthenticator auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
327 |
HttpSchemeType schemeType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
328 |
HttpHandler delegate, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
329 |
String path) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
330 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
331 |
Objects.requireNonNull(authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
332 |
Objects.requireNonNull(auth); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
333 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
334 |
HttpServer impl = createHttpServer(protocol); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
335 |
final HTTPTestServer server = new HTTPTestServer(impl, null, delegate); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
336 |
final HttpHandler hh = server.createHandler(schemeType, auth, authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
337 |
HttpContext ctxt = impl.createContext(path, hh); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
338 |
server.configureAuthentication(ctxt, schemeType, auth, authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
339 |
impl.start(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
340 |
return server; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
341 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
342 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
343 |
public static HTTPTestServer createProxy(HttpProtocolType protocol, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
344 |
HttpAuthType authType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
345 |
HttpTestAuthenticator auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
346 |
HttpSchemeType schemeType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
347 |
HttpHandler delegate, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
348 |
String path) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
349 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
350 |
Objects.requireNonNull(authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
351 |
Objects.requireNonNull(auth); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
352 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
353 |
HttpServer impl = createHttpServer(protocol); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
354 |
final HTTPTestServer server = protocol == HttpProtocolType.HTTPS |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
355 |
? new HttpsProxyTunnel(impl, null, delegate) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
356 |
: new HTTPTestServer(impl, null, delegate); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
357 |
final HttpHandler hh = server.createHandler(schemeType, auth, authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
358 |
HttpContext ctxt = impl.createContext(path, hh); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
359 |
server.configureAuthentication(ctxt, schemeType, auth, authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
360 |
impl.start(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
361 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
362 |
return server; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
363 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
364 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
365 |
public static HTTPTestServer createServerAndRedirect( |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
366 |
HttpProtocolType protocol, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
367 |
HttpAuthType targetAuthType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
368 |
HttpTestAuthenticator auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
369 |
HttpSchemeType schemeType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
370 |
HttpHandler targetDelegate, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
371 |
int code300) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
372 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
373 |
Objects.requireNonNull(targetAuthType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
374 |
Objects.requireNonNull(auth); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
375 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
376 |
// The connection between client and proxy can only |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
377 |
// be a plain connection: SSL connection to proxy |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
378 |
// is not supported by our client connection. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
379 |
HttpProtocolType targetProtocol = targetAuthType == HttpAuthType.PROXY |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
380 |
? HttpProtocolType.HTTP |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
381 |
: protocol; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
382 |
HTTPTestServer redirectTarget = |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
383 |
(targetAuthType == HttpAuthType.PROXY) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
384 |
? createProxy(protocol, targetAuthType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
385 |
auth, schemeType, targetDelegate, "/") |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
386 |
: createServer(targetProtocol, targetAuthType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
387 |
auth, schemeType, targetDelegate, "/"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
388 |
HttpServer impl = createHttpServer(protocol); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
389 |
final HTTPTestServer redirectingServer = |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
390 |
new HTTPTestServer(impl, redirectTarget, null); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
391 |
InetSocketAddress redirectAddr = redirectTarget.getAddress(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
392 |
URL locationURL = url(targetProtocol, redirectAddr, "/"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
393 |
final HttpHandler hh = redirectingServer.create300Handler(locationURL, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
394 |
HttpAuthType.SERVER, code300); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
395 |
impl.createContext("/", hh); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
396 |
impl.start(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
397 |
return redirectingServer; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
398 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
399 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
400 |
public InetSocketAddress getAddress() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
401 |
return serverImpl.getAddress(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
402 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
403 |
|
55522
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54086
diff
changeset
|
404 |
public InetSocketAddress getProxyAddress() { |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54086
diff
changeset
|
405 |
return serverImpl.getAddress(); |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54086
diff
changeset
|
406 |
} |
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54086
diff
changeset
|
407 |
|
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
408 |
public void stop() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
409 |
serverImpl.stop(0); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
410 |
if (redirect != null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
411 |
redirect.stop(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
412 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
413 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
414 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
415 |
protected void writeResponse(HttpExchange he) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
416 |
if (delegate == null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
417 |
he.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
418 |
he.getResponseBody().write(he.getRequestBody().readAllBytes()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
419 |
} else { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
420 |
delegate.handle(he); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
421 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
422 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
423 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
424 |
private HttpHandler createHandler(HttpSchemeType schemeType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
425 |
HttpTestAuthenticator auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
426 |
HttpAuthType authType) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
427 |
return new HttpNoAuthHandler(authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
428 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
429 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
430 |
private void configureAuthentication(HttpContext ctxt, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
431 |
HttpSchemeType schemeType, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
432 |
HttpTestAuthenticator auth, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
433 |
HttpAuthType authType) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
434 |
switch(schemeType) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
435 |
case DIGEST: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
436 |
// DIGEST authentication is handled by the handler. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
437 |
ctxt.getFilters().add(new HttpDigestFilter(auth, authType)); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
438 |
break; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
439 |
case BASIC: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
440 |
// BASIC authentication is handled by the filter. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
441 |
ctxt.getFilters().add(new HttpBasicFilter(auth, authType)); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
442 |
break; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
443 |
case BASICSERVER: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
444 |
switch(authType) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
445 |
case PROXY: case PROXY305: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
446 |
// HttpServer can't support Proxy-type authentication |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
447 |
// => we do as if BASIC had been specified, and we will |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
448 |
// handle authentication in the handler. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
449 |
ctxt.getFilters().add(new HttpBasicFilter(auth, authType)); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
450 |
break; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
451 |
case SERVER: case SERVER307: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
452 |
// Basic authentication is handled by HttpServer |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
453 |
// directly => the filter should not perform |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
454 |
// authentication again. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
455 |
setContextAuthenticator(ctxt, auth); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
456 |
ctxt.getFilters().add(new HttpNoAuthFilter(authType)); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
457 |
break; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
458 |
default: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
459 |
throw new InternalError("Invalid combination scheme=" |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
460 |
+ schemeType + " authType=" + authType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
461 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
462 |
case NONE: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
463 |
// No authentication at all. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
464 |
ctxt.getFilters().add(new HttpNoAuthFilter(authType)); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
465 |
break; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
466 |
default: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
467 |
throw new InternalError("No such scheme: " + schemeType); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
468 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
469 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
470 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
471 |
private HttpHandler create300Handler(URL proxyURL, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
472 |
HttpAuthType type, int code300) throws MalformedURLException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
473 |
return new Http3xxHandler(proxyURL, type, code300); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
474 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
475 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
476 |
// Abstract HTTP filter class. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
477 |
private abstract static class AbstractHttpFilter extends Filter { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
478 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
479 |
final HttpAuthType authType; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
480 |
final String type; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
481 |
public AbstractHttpFilter(HttpAuthType authType, String type) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
482 |
this.authType = authType; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
483 |
this.type = type; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
484 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
485 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
486 |
String getLocation() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
487 |
return "Location"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
488 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
489 |
String getAuthenticate() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
490 |
return authType == HttpAuthType.PROXY |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
491 |
? "Proxy-Authenticate" : "WWW-Authenticate"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
492 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
493 |
String getAuthorization() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
494 |
return authType == HttpAuthType.PROXY |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
495 |
? "Proxy-Authorization" : "Authorization"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
496 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
497 |
int getUnauthorizedCode() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
498 |
return authType == HttpAuthType.PROXY |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
499 |
? HttpURLConnection.HTTP_PROXY_AUTH |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
500 |
: HttpURLConnection.HTTP_UNAUTHORIZED; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
501 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
502 |
String getKeepAlive() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
503 |
return "keep-alive"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
504 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
505 |
String getConnection() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
506 |
return authType == HttpAuthType.PROXY |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
507 |
? "Proxy-Connection" : "Connection"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
508 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
509 |
protected abstract boolean isAuthentified(HttpExchange he) throws IOException; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
510 |
protected abstract void requestAuthentication(HttpExchange he) throws IOException; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
511 |
protected void accept(HttpExchange he, Chain chain) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
512 |
chain.doFilter(he); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
513 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
514 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
515 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
516 |
public String description() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
517 |
return "Filter for " + type; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
518 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
519 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
520 |
public void doFilter(HttpExchange he, Chain chain) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
521 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
522 |
System.out.println(type + ": Got " + he.getRequestMethod() |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
523 |
+ ": " + he.getRequestURI() |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
524 |
+ "\n" + HTTPTestServer.toString(he.getRequestHeaders())); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
525 |
if (!isAuthentified(he)) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
526 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
527 |
requestAuthentication(he); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
528 |
he.sendResponseHeaders(getUnauthorizedCode(), 0); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
529 |
System.out.println(type |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
530 |
+ ": Sent back " + getUnauthorizedCode()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
531 |
} finally { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
532 |
he.close(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
533 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
534 |
} else { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
535 |
accept(he, chain); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
536 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
537 |
} catch (RuntimeException | Error | IOException t) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
538 |
System.err.println(type |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
539 |
+ ": Unexpected exception while handling request: " + t); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
540 |
t.printStackTrace(System.err); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
541 |
he.close(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
542 |
throw t; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
543 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
544 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
545 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
546 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
547 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
548 |
private final static class DigestResponse { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
549 |
final String realm; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
550 |
final String username; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
551 |
final String nonce; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
552 |
final String cnonce; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
553 |
final String nc; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
554 |
final String uri; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
555 |
final String algorithm; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
556 |
final String response; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
557 |
final String qop; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
558 |
final String opaque; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
559 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
560 |
public DigestResponse(String realm, String username, String nonce, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
561 |
String cnonce, String nc, String uri, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
562 |
String algorithm, String qop, String opaque, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
563 |
String response) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
564 |
this.realm = realm; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
565 |
this.username = username; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
566 |
this.nonce = nonce; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
567 |
this.cnonce = cnonce; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
568 |
this.nc = nc; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
569 |
this.uri = uri; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
570 |
this.algorithm = algorithm; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
571 |
this.qop = qop; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
572 |
this.opaque = opaque; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
573 |
this.response = response; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
574 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
575 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
576 |
String getAlgorithm(String defval) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
577 |
return algorithm == null ? defval : algorithm; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
578 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
579 |
String getQoP(String defval) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
580 |
return qop == null ? defval : qop; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
581 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
582 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
583 |
// Code stolen from DigestAuthentication: |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
584 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
585 |
private static final char charArray[] = { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
586 |
'0', '1', '2', '3', '4', '5', '6', '7', |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
587 |
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
588 |
}; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
589 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
590 |
private static String encode(String src, char[] passwd, MessageDigest md) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
591 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
592 |
md.update(src.getBytes("ISO-8859-1")); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
593 |
} catch (java.io.UnsupportedEncodingException uee) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
594 |
assert false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
595 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
596 |
if (passwd != null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
597 |
byte[] passwdBytes = new byte[passwd.length]; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
598 |
for (int i=0; i<passwd.length; i++) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
599 |
passwdBytes[i] = (byte)passwd[i]; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
600 |
md.update(passwdBytes); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
601 |
Arrays.fill(passwdBytes, (byte)0x00); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
602 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
603 |
byte[] digest = md.digest(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
604 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
605 |
StringBuilder res = new StringBuilder(digest.length * 2); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
606 |
for (int i = 0; i < digest.length; i++) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
607 |
int hashchar = ((digest[i] >>> 4) & 0xf); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
608 |
res.append(charArray[hashchar]); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
609 |
hashchar = (digest[i] & 0xf); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
610 |
res.append(charArray[hashchar]); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
611 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
612 |
return res.toString(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
613 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
614 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
615 |
public static String computeDigest(boolean isRequest, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
616 |
String reqMethod, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
617 |
char[] password, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
618 |
DigestResponse params) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
619 |
throws NoSuchAlgorithmException |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
620 |
{ |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
621 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
622 |
String A1, HashA1; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
623 |
String algorithm = params.getAlgorithm("MD5"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
624 |
boolean md5sess = algorithm.equalsIgnoreCase ("MD5-sess"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
625 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
626 |
MessageDigest md = MessageDigest.getInstance(md5sess?"MD5":algorithm); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
627 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
628 |
if (params.username == null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
629 |
throw new IllegalArgumentException("missing username"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
630 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
631 |
if (params.realm == null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
632 |
throw new IllegalArgumentException("missing realm"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
633 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
634 |
if (params.uri == null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
635 |
throw new IllegalArgumentException("missing uri"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
636 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
637 |
if (params.nonce == null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
638 |
throw new IllegalArgumentException("missing nonce"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
639 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
640 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
641 |
A1 = params.username + ":" + params.realm + ":"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
642 |
HashA1 = encode(A1, password, md); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
643 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
644 |
String A2; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
645 |
if (isRequest) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
646 |
A2 = reqMethod + ":" + params.uri; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
647 |
} else { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
648 |
A2 = ":" + params.uri; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
649 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
650 |
String HashA2 = encode(A2, null, md); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
651 |
String combo, finalHash; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
652 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
653 |
if ("auth".equals(params.qop)) { /* RRC2617 when qop=auth */ |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
654 |
if (params.cnonce == null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
655 |
throw new IllegalArgumentException("missing nonce"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
656 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
657 |
if (params.nc == null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
658 |
throw new IllegalArgumentException("missing nonce"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
659 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
660 |
combo = HashA1+ ":" + params.nonce + ":" + params.nc + ":" + |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
661 |
params.cnonce + ":auth:" +HashA2; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
662 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
663 |
} else { /* for compatibility with RFC2069 */ |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
664 |
combo = HashA1 + ":" + |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
665 |
params.nonce + ":" + |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
666 |
HashA2; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
667 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
668 |
finalHash = encode(combo, null, md); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
669 |
return finalHash; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
670 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
671 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
672 |
public static DigestResponse create(String raw) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
673 |
String username, realm, nonce, nc, uri, response, cnonce, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
674 |
algorithm, qop, opaque; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
675 |
HeaderParser parser = new HeaderParser(raw); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
676 |
username = parser.findValue("username"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
677 |
realm = parser.findValue("realm"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
678 |
nonce = parser.findValue("nonce"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
679 |
nc = parser.findValue("nc"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
680 |
uri = parser.findValue("uri"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
681 |
cnonce = parser.findValue("cnonce"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
682 |
response = parser.findValue("response"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
683 |
algorithm = parser.findValue("algorithm"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
684 |
qop = parser.findValue("qop"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
685 |
opaque = parser.findValue("opaque"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
686 |
return new DigestResponse(realm, username, nonce, cnonce, nc, uri, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
687 |
algorithm, qop, opaque, response); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
688 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
689 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
690 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
691 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
692 |
private class HttpNoAuthFilter extends AbstractHttpFilter { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
693 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
694 |
public HttpNoAuthFilter(HttpAuthType authType) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
695 |
super(authType, authType == HttpAuthType.SERVER |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
696 |
? "NoAuth Server" : "NoAuth Proxy"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
697 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
698 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
699 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
700 |
protected boolean isAuthentified(HttpExchange he) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
701 |
return true; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
702 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
703 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
704 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
705 |
protected void requestAuthentication(HttpExchange he) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
706 |
throw new InternalError("Should not com here"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
707 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
708 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
709 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
710 |
public String description() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
711 |
return "Passthrough Filter"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
712 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
713 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
714 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
715 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
716 |
// An HTTP Filter that performs Basic authentication |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
717 |
private class HttpBasicFilter extends AbstractHttpFilter { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
718 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
719 |
private final HttpTestAuthenticator auth; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
720 |
public HttpBasicFilter(HttpTestAuthenticator auth, HttpAuthType authType) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
721 |
super(authType, authType == HttpAuthType.SERVER |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
722 |
? "Basic Server" : "Basic Proxy"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
723 |
this.auth = auth; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
724 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
725 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
726 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
727 |
protected void requestAuthentication(HttpExchange he) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
728 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
729 |
he.getResponseHeaders().add(getAuthenticate(), |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
730 |
"Basic realm=\"" + auth.getRealm() + "\""); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
731 |
System.out.println(type + ": Requesting Basic Authentication " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
732 |
+ he.getResponseHeaders().getFirst(getAuthenticate())); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
733 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
734 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
735 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
736 |
protected boolean isAuthentified(HttpExchange he) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
737 |
if (he.getRequestHeaders().containsKey(getAuthorization())) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
738 |
List<String> authorization = |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
739 |
he.getRequestHeaders().get(getAuthorization()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
740 |
for (String a : authorization) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
741 |
System.out.println(type + ": processing " + a); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
742 |
int sp = a.indexOf(' '); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
743 |
if (sp < 0) return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
744 |
String scheme = a.substring(0, sp); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
745 |
if (!"Basic".equalsIgnoreCase(scheme)) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
746 |
System.out.println(type + ": Unsupported scheme '" |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
747 |
+ scheme +"'"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
748 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
749 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
750 |
if (a.length() <= sp+1) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
751 |
System.out.println(type + ": value too short for '" |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
752 |
+ scheme +"'"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
753 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
754 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
755 |
a = a.substring(sp+1); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
756 |
return validate(a); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
757 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
758 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
759 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
760 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
761 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
762 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
763 |
boolean validate(String a) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
764 |
byte[] b = Base64.getDecoder().decode(a); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
765 |
String userpass = new String (b); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
766 |
int colon = userpass.indexOf (':'); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
767 |
String uname = userpass.substring (0, colon); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
768 |
String pass = userpass.substring (colon+1); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
769 |
return auth.getUserName().equals(uname) && |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
770 |
new String(auth.getPassword(uname)).equals(pass); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
771 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
772 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
773 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
774 |
public String description() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
775 |
return "Filter for " + type; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
776 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
777 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
778 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
779 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
780 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
781 |
// An HTTP Filter that performs Digest authentication |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
782 |
private class HttpDigestFilter extends AbstractHttpFilter { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
783 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
784 |
// This is a very basic DIGEST - used only for the purpose of testing |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
785 |
// the client implementation. Therefore we can get away with never |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
786 |
// updating the server nonce as it makes the implementation of the |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
787 |
// server side digest simpler. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
788 |
private final HttpTestAuthenticator auth; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
789 |
private final byte[] nonce; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
790 |
private final String ns; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
791 |
public HttpDigestFilter(HttpTestAuthenticator auth, HttpAuthType authType) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
792 |
super(authType, authType == HttpAuthType.SERVER |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
793 |
? "Digest Server" : "Digest Proxy"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
794 |
this.auth = auth; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
795 |
nonce = new byte[16]; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
796 |
new Random(Instant.now().toEpochMilli()).nextBytes(nonce); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
797 |
ns = new BigInteger(1, nonce).toString(16); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
798 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
799 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
800 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
801 |
protected void requestAuthentication(HttpExchange he) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
802 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
803 |
he.getResponseHeaders().add(getAuthenticate(), |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
804 |
"Digest realm=\"" + auth.getRealm() + "\"," |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
805 |
+ "\r\n qop=\"auth\"," |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
806 |
+ "\r\n nonce=\"" + ns +"\""); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
807 |
System.out.println(type + ": Requesting Digest Authentication " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
808 |
+ he.getResponseHeaders().getFirst(getAuthenticate())); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
809 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
810 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
811 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
812 |
protected boolean isAuthentified(HttpExchange he) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
813 |
if (he.getRequestHeaders().containsKey(getAuthorization())) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
814 |
List<String> authorization = he.getRequestHeaders().get(getAuthorization()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
815 |
for (String a : authorization) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
816 |
System.out.println(type + ": processing " + a); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
817 |
int sp = a.indexOf(' '); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
818 |
if (sp < 0) return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
819 |
String scheme = a.substring(0, sp); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
820 |
if (!"Digest".equalsIgnoreCase(scheme)) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
821 |
System.out.println(type + ": Unsupported scheme '" + scheme +"'"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
822 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
823 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
824 |
if (a.length() <= sp+1) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
825 |
System.out.println(type + ": value too short for '" + scheme +"'"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
826 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
827 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
828 |
a = a.substring(sp+1); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
829 |
DigestResponse dgr = DigestResponse.create(a); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
830 |
return validate(he.getRequestMethod(), dgr); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
831 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
832 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
833 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
834 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
835 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
836 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
837 |
boolean validate(String reqMethod, DigestResponse dg) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
838 |
if (!"MD5".equalsIgnoreCase(dg.getAlgorithm("MD5"))) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
839 |
System.out.println(type + ": Unsupported algorithm " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
840 |
+ dg.algorithm); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
841 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
842 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
843 |
if (!"auth".equalsIgnoreCase(dg.getQoP("auth"))) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
844 |
System.out.println(type + ": Unsupported qop " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
845 |
+ dg.qop); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
846 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
847 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
848 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
849 |
if (!dg.nonce.equals(ns)) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
850 |
System.out.println(type + ": bad nonce returned by client: " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
851 |
+ nonce + " expected " + ns); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
852 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
853 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
854 |
if (dg.response == null) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
855 |
System.out.println(type + ": missing digest response."); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
856 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
857 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
858 |
char[] pa = auth.getPassword(dg.username); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
859 |
return verify(reqMethod, dg, pa); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
860 |
} catch(IllegalArgumentException | SecurityException |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
861 |
| NoSuchAlgorithmException e) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
862 |
System.out.println(type + ": " + e.getMessage()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
863 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
864 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
865 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
866 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
867 |
boolean verify(String reqMethod, DigestResponse dg, char[] pw) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
868 |
throws NoSuchAlgorithmException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
869 |
String response = DigestResponse.computeDigest(true, reqMethod, pw, dg); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
870 |
if (!dg.response.equals(response)) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
871 |
System.out.println(type + ": bad response returned by client: " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
872 |
+ dg.response + " expected " + response); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
873 |
return false; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
874 |
} else { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
875 |
System.out.println(type + ": verified response " + response); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
876 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
877 |
return true; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
878 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
879 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
880 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
881 |
public String description() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
882 |
return "Filter for DIGEST authentication"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
883 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
884 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
885 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
886 |
// Abstract HTTP handler class. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
887 |
private abstract static class AbstractHttpHandler implements HttpHandler { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
888 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
889 |
final HttpAuthType authType; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
890 |
final String type; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
891 |
public AbstractHttpHandler(HttpAuthType authType, String type) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
892 |
this.authType = authType; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
893 |
this.type = type; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
894 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
895 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
896 |
String getLocation() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
897 |
return "Location"; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
898 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
899 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
900 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
901 |
public void handle(HttpExchange he) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
902 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
903 |
sendResponse(he); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
904 |
} catch (RuntimeException | Error | IOException t) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
905 |
System.err.println(type |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
906 |
+ ": Unexpected exception while handling request: " + t); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
907 |
t.printStackTrace(System.err); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
908 |
throw t; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
909 |
} finally { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
910 |
he.close(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
911 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
912 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
913 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
914 |
protected abstract void sendResponse(HttpExchange he) throws IOException; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
915 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
916 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
917 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
918 |
private class HttpNoAuthHandler extends AbstractHttpHandler { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
919 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
920 |
public HttpNoAuthHandler(HttpAuthType authType) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
921 |
super(authType, authType == HttpAuthType.SERVER |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
922 |
? "NoAuth Server" : "NoAuth Proxy"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
923 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
924 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
925 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
926 |
protected void sendResponse(HttpExchange he) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
927 |
HTTPTestServer.this.writeResponse(he); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
928 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
929 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
930 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
931 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
932 |
// A dummy HTTP Handler that redirects all incoming requests |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
933 |
// by sending a back 3xx response code (301, 305, 307 etc..) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
934 |
private class Http3xxHandler extends AbstractHttpHandler { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
935 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
936 |
private final URL redirectTargetURL; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
937 |
private final int code3XX; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
938 |
public Http3xxHandler(URL proxyURL, HttpAuthType authType, int code300) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
939 |
super(authType, "Server" + code300); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
940 |
this.redirectTargetURL = proxyURL; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
941 |
this.code3XX = code300; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
942 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
943 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
944 |
int get3XX() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
945 |
return code3XX; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
946 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
947 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
948 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
949 |
public void sendResponse(HttpExchange he) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
950 |
System.out.println(type + ": Got " + he.getRequestMethod() |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
951 |
+ ": " + he.getRequestURI() |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
952 |
+ "\n" + HTTPTestServer.toString(he.getRequestHeaders())); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
953 |
System.out.println(type + ": Redirecting to " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
954 |
+ (authType == HttpAuthType.PROXY305 |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
955 |
? "proxy" : "server")); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
956 |
he.getResponseHeaders().add(getLocation(), |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
957 |
redirectTargetURL.toExternalForm().toString()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
958 |
he.sendResponseHeaders(get3XX(), 0); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
959 |
System.out.println(type + ": Sent back " + get3XX() + " " |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
960 |
+ getLocation() + ": " + redirectTargetURL.toExternalForm().toString()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
961 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
962 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
963 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
964 |
static class Configurator extends HttpsConfigurator { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
965 |
public Configurator(SSLContext ctx) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
966 |
super(ctx); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
967 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
968 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
969 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
970 |
public void configure (HttpsParameters params) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
971 |
params.setSSLParameters (getSSLContext().getSupportedSSLParameters()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
972 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
973 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
974 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
975 |
// This is a bit hacky: HttpsProxyTunnel is an HTTPTestServer hidden |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
976 |
// behind a fake proxy that only understands CONNECT requests. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
977 |
// The fake proxy is just a server socket that intercept the |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
978 |
// CONNECT and then redirect streams to the real server. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
979 |
static class HttpsProxyTunnel extends HTTPTestServer |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
980 |
implements Runnable { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
981 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
982 |
final ServerSocket ss; |
57994
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
983 |
private volatile boolean stop; |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
984 |
|
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
985 |
public HttpsProxyTunnel(HttpServer server, HTTPTestServer target, |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
986 |
HttpHandler delegate) |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
987 |
throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
988 |
super(server, target, delegate); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
989 |
System.out.flush(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
990 |
System.err.println("WARNING: HttpsProxyTunnel is an experimental test class"); |
47322
d94b2c6e26a9
8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents:
47216
diff
changeset
|
991 |
ss = ServerSocketFactory.create(); |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
992 |
start(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
993 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
994 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
995 |
final void start() throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
996 |
Thread t = new Thread(this, "ProxyThread"); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
997 |
t.setDaemon(true); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
998 |
t.start(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
999 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1000 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1001 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1002 |
public void stop() { |
57994
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1003 |
try (var toClose = ss) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1004 |
stop = true; |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1005 |
System.out.println("Server " + ss + " stop requested"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1006 |
super.stop(); |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1007 |
} catch (IOException ex) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1008 |
if (DEBUG) ex.printStackTrace(System.out); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1009 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1010 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1011 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1012 |
// Pipe the input stream to the output stream. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1013 |
private synchronized Thread pipe(InputStream is, OutputStream os, char tag) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1014 |
return new Thread("TunnelPipe("+tag+")") { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1015 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1016 |
public void run() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1017 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1018 |
try { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1019 |
int c; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1020 |
while ((c = is.read()) != -1) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1021 |
os.write(c); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1022 |
os.flush(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1023 |
// if DEBUG prints a + or a - for each transferred |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1024 |
// character. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1025 |
if (DEBUG) System.out.print(tag); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1026 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1027 |
is.close(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1028 |
} finally { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1029 |
os.close(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1030 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1031 |
} catch (IOException ex) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1032 |
if (DEBUG) ex.printStackTrace(System.out); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1033 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1034 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1035 |
}; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1036 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1037 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1038 |
@Override |
55522
b2cbe2f674f0
8226514: Replace wildcard address with loopback or local host in tests - part 14
dfuchs
parents:
54086
diff
changeset
|
1039 |
public InetSocketAddress getProxyAddress() { |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1040 |
return new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1041 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1042 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1043 |
// This is a bit shaky. It doesn't handle continuation |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1044 |
// lines, but our client shouldn't send any. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1045 |
// Read a line from the input stream, swallowing the final |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1046 |
// \r\n sequence. Stops at the first \n, doesn't complain |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1047 |
// if it wasn't preceded by '\r'. |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1048 |
// |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1049 |
String readLine(InputStream r) throws IOException { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1050 |
StringBuilder b = new StringBuilder(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1051 |
int c; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1052 |
while ((c = r.read()) != -1) { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1053 |
if (c == '\n') break; |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1054 |
b.appendCodePoint(c); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1055 |
} |
57994
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1056 |
if (b.length() == 0) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1057 |
return ""; |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1058 |
} |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1059 |
if (b.codePointAt(b.length() -1) == '\r') { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1060 |
b.delete(b.length() -1, b.length()); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1061 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1062 |
return b.toString(); |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1063 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1064 |
|
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1065 |
@Override |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1066 |
public void run() { |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1067 |
Socket clientConnection = null; |
57994
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1068 |
while (!stop) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1069 |
System.out.println("Tunnel: Waiting for client at: " + ss); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1070 |
final Socket previous = clientConnection; |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1071 |
try { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1072 |
clientConnection = ss.accept(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1073 |
} catch (IOException io) { |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1074 |
try { |
57994
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1075 |
ss.close(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1076 |
} catch (IOException ex) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1077 |
if (DEBUG) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1078 |
ex.printStackTrace(System.out); |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1079 |
} |
57994
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1080 |
} |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1081 |
// log the reason that caused the server to stop accepting connections |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1082 |
if (!stop) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1083 |
System.err.println("Server will stop accepting connections due to an exception:"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1084 |
io.printStackTrace(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1085 |
} |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1086 |
break; |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1087 |
} finally { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1088 |
// close the previous connection |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1089 |
if (previous != null) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1090 |
try { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1091 |
previous.close(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1092 |
} catch (IOException e) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1093 |
// ignore |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1094 |
if (DEBUG) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1095 |
System.out.println("Ignoring exception that happened while closing " + |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1096 |
"an older connection:"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1097 |
e.printStackTrace(System.out); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1098 |
} |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1099 |
} |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1100 |
} |
57994
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1101 |
} |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1102 |
System.out.println("Tunnel: Client accepted"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1103 |
try { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1104 |
// We have only 1 client... process the current client |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1105 |
// request and wait until it has finished before |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1106 |
// accepting a new connection request. |
57994
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1107 |
processRequestAndWaitToComplete(clientConnection); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1108 |
} catch (IOException ioe) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1109 |
// close the client connection |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1110 |
try { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1111 |
clientConnection.close(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1112 |
} catch (IOException io) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1113 |
// ignore |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1114 |
if (DEBUG) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1115 |
System.out.println("Ignoring exception that happened during client" + |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1116 |
" connection close:"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1117 |
io.printStackTrace(System.out); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1118 |
} |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1119 |
} finally { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1120 |
clientConnection = null; |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1121 |
} |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1122 |
} catch (Throwable t) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1123 |
// don't close the client connection for non-IOExceptions, instead |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1124 |
// just log it and move on to accept next connection |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1125 |
if (!stop) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1126 |
t.printStackTrace(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1127 |
} |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1128 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1129 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1130 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1131 |
|
57994
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1132 |
private void processRequestAndWaitToComplete(final Socket clientConnection) |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1133 |
throws IOException, InterruptedException { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1134 |
final Socket targetConnection; |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1135 |
InputStream ccis = clientConnection.getInputStream(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1136 |
OutputStream ccos = clientConnection.getOutputStream(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1137 |
Writer w = new OutputStreamWriter( |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1138 |
clientConnection.getOutputStream(), "UTF-8"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1139 |
PrintWriter pw = new PrintWriter(w); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1140 |
System.out.println("Tunnel: Reading request line"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1141 |
String requestLine = readLine(ccis); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1142 |
System.out.println("Tunnel: Request line: " + requestLine); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1143 |
if (requestLine.startsWith("CONNECT ")) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1144 |
// We should probably check that the next word following |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1145 |
// CONNECT is the host:port of our HTTPS serverImpl. |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1146 |
// Some improvement for a followup! |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1147 |
|
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1148 |
// Read all headers until we find the empty line that |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1149 |
// signals the end of all headers. |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1150 |
while(!requestLine.equals("")) { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1151 |
System.out.println("Tunnel: Reading header: " |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1152 |
+ (requestLine = readLine(ccis))); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1153 |
} |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1154 |
|
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1155 |
targetConnection = new Socket( |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1156 |
serverImpl.getAddress().getAddress(), |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1157 |
serverImpl.getAddress().getPort()); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1158 |
|
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1159 |
// Then send the 200 OK response to the client |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1160 |
System.out.println("Tunnel: Sending " |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1161 |
+ "HTTP/1.1 200 OK\r\n\r\n"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1162 |
pw.print("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1163 |
pw.flush(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1164 |
} else { |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1165 |
// This should not happen. If it does then consider it a |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1166 |
// client error and throw an IOException |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1167 |
System.out.println("Tunnel: Throwing an IOException due to unexpected" + |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1168 |
" request line: " + requestLine); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1169 |
throw new IOException("Client request error - Unexpected request line"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1170 |
} |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1171 |
|
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1172 |
// Pipe the input stream of the client connection to the |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1173 |
// output stream of the target connection and conversely. |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1174 |
// Now the client and target will just talk to each other. |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1175 |
System.out.println("Tunnel: Starting tunnel pipes"); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1176 |
Thread t1 = pipe(ccis, targetConnection.getOutputStream(), '+'); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1177 |
Thread t2 = pipe(targetConnection.getInputStream(), ccos, '-'); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1178 |
t1.start(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1179 |
t2.start(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1180 |
// wait for the request to complete |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1181 |
t1.join(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1182 |
t2.join(); |
197f36af94f3
8223714: HTTPSetAuthenticatorTest could be made more resilient
jpai
parents:
57793
diff
changeset
|
1183 |
} |
42351
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1184 |
} |
85ed90be0ae1
8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff
changeset
|
1185 |
} |