author | jboes |
Fri, 08 Nov 2019 11:15:16 +0000 | |
changeset 59029 | 3786a0962570 |
parent 57838 | 78844dceede6 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
59029
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
49765 | 29 |
import java.net.MalformedURLException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.net.PasswordAuthentication; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.net.InetSocketAddress; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.net.URISyntaxException; |
49765 | 34 |
import java.net.URL; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.util.Base64; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.util.LinkedList; |
53374
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
37 |
import java.util.List; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.util.Objects; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.util.WeakHashMap; |
49765 | 40 |
import java.net.http.HttpHeaders; |
41 |
import jdk.internal.net.http.common.Log; |
|
42 |
import jdk.internal.net.http.common.Utils; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
import static java.net.Authenticator.RequestorType.PROXY; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import static java.net.Authenticator.RequestorType.SERVER; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
import static java.nio.charset.StandardCharsets.ISO_8859_1; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
46 |
import static java.nio.charset.StandardCharsets.UTF_8; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
* Implementation of Http Basic authentication. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
class AuthenticationFilter implements HeaderFilter { |
49765 | 52 |
volatile MultiExchange<?> exchange; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
private static final Base64.Encoder encoder = Base64.getEncoder(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
static final int DEFAULT_RETRY_LIMIT = 3; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
static final int retry_limit = Utils.getIntegerNetProperty( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
"jdk.httpclient.auth.retrylimit", DEFAULT_RETRY_LIMIT); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
static final int UNAUTHORIZED = 401; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
static final int PROXY_UNAUTHORIZED = 407; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
|
50681 | 63 |
private static final String BASIC_DUMMY = |
64 |
"Basic " + Base64.getEncoder() |
|
65 |
.encodeToString("o:o".getBytes(ISO_8859_1)); |
|
49765 | 66 |
|
48083 | 67 |
// A public no-arg constructor is required by FilterFactory |
68 |
public AuthenticationFilter() {} |
|
69 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
private PasswordAuthentication getCredentials(String header, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
boolean proxy, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
HttpRequestImpl req) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
throws IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
HttpClientImpl client = exchange.client(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
java.net.Authenticator auth = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
client.authenticator() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
.orElseThrow(() -> new IOException("No authenticator set")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
URI uri = req.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
HeaderParser parser = new HeaderParser(header); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
81 |
String authscheme = parser.findKey(0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
82 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
String realm = parser.findValue("realm"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
java.net.Authenticator.RequestorType rtype = proxy ? PROXY : SERVER; |
49765 | 85 |
URL url = toURL(uri, req.method(), proxy); |
86 |
String host; |
|
87 |
int port; |
|
88 |
String protocol; |
|
89 |
InetSocketAddress proxyAddress; |
|
90 |
if (proxy && (proxyAddress = req.proxy()) != null) { |
|
91 |
// request sent to server through proxy |
|
92 |
proxyAddress = req.proxy(); |
|
93 |
host = proxyAddress.getHostString(); |
|
94 |
port = proxyAddress.getPort(); |
|
95 |
protocol = "http"; // we don't support https connection to proxy |
|
96 |
} else { |
|
97 |
// direct connection to server or proxy |
|
98 |
host = uri.getHost(); |
|
99 |
port = uri.getPort(); |
|
100 |
protocol = uri.getScheme(); |
|
101 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
// needs to be instance method in Authenticator |
49765 | 104 |
return auth.requestPasswordAuthenticationInstance(host, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
105 |
null, |
49765 | 106 |
port, |
107 |
protocol, |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
108 |
realm, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
authscheme, |
49765 | 110 |
url, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
rtype |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
|
49765 | 115 |
private URL toURL(URI uri, String method, boolean proxy) |
116 |
throws MalformedURLException |
|
117 |
{ |
|
118 |
if (proxy && "CONNECT".equalsIgnoreCase(method) |
|
119 |
&& "socket".equalsIgnoreCase(uri.getScheme())) { |
|
120 |
return null; // proxy tunneling |
|
121 |
} |
|
122 |
return uri.toURL(); |
|
123 |
} |
|
124 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
private URI getProxyURI(HttpRequestImpl r) { |
48083 | 126 |
InetSocketAddress proxy = r.proxy(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
if (proxy == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
// our own private scheme for proxy URLs |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
50681
diff
changeset
|
132 |
// e.g. proxy.http://host:port/ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
String scheme = "proxy." + r.uri().getScheme(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
return new URI(scheme, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
proxy.getHostString(), |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
proxy.getPort(), |
49765 | 139 |
"/", |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
null); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
} catch (URISyntaxException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
throw new InternalError(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
147 |
@Override |
49765 | 148 |
public void request(HttpRequestImpl r, MultiExchange<?> e) throws IOException { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
// use preemptive authentication if an entry exists. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
150 |
Cache cache = getCache(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
this.exchange = e; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
// Proxy |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
if (exchange.proxyauth == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
URI proxyURI = getProxyURI(r); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
156 |
if (proxyURI != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
CacheEntry ca = cache.get(proxyURI, true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
if (ca != null) { |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
159 |
exchange.proxyauth = new AuthInfo(true, ca.scheme, null, ca, ca.isUTF8); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
160 |
addBasicCredentials(r, true, ca.value, ca.isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
161 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
162 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
163 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
165 |
// Server |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
166 |
if (exchange.serverauth == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
CacheEntry ca = cache.get(r.uri(), false); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
if (ca != null) { |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
169 |
exchange.serverauth = new AuthInfo(true, ca.scheme, null, ca, ca.isUTF8); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
170 |
addBasicCredentials(r, false, ca.value, ca.isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
// TODO: refactor into per auth scheme class |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
private static void addBasicCredentials(HttpRequestImpl r, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
boolean proxy, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
178 |
PasswordAuthentication pw, |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
179 |
boolean isUTF8) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
String hdrname = proxy ? "Proxy-Authorization" : "Authorization"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
StringBuilder sb = new StringBuilder(128); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
sb.append(pw.getUserName()).append(':').append(pw.getPassword()); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
183 |
var charset = isUTF8 ? UTF_8 : ISO_8859_1; |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
184 |
String s = encoder.encodeToString(sb.toString().getBytes(charset)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
String value = "Basic " + s; |
49765 | 186 |
if (proxy) { |
187 |
if (r.isConnect()) { |
|
50681 | 188 |
if (!Utils.PROXY_TUNNEL_FILTER.test(hdrname, value)) { |
49765 | 189 |
Log.logError("{0} disabled", hdrname); |
190 |
return; |
|
191 |
} |
|
192 |
} else if (r.proxy() != null) { |
|
50681 | 193 |
if (!Utils.PROXY_FILTER.test(hdrname, value)) { |
49765 | 194 |
Log.logError("{0} disabled", hdrname); |
195 |
return; |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
r.setSystemHeader(hdrname, value); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
201 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
202 |
// Information attached to a HttpRequestImpl relating to authentication |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
static class AuthInfo { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
final boolean fromcache; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
205 |
final String scheme; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
206 |
int retries; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
207 |
PasswordAuthentication credentials; // used in request |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
208 |
CacheEntry cacheEntry; // if used |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
209 |
final boolean isUTF8; // |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
210 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
211 |
AuthInfo(boolean fromcache, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
212 |
String scheme, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
213 |
PasswordAuthentication credentials, boolean isUTF8) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
214 |
this.fromcache = fromcache; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
this.scheme = scheme; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
this.credentials = credentials; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
this.retries = 1; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
218 |
this.isUTF8 = isUTF8; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
AuthInfo(boolean fromcache, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
String scheme, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
PasswordAuthentication credentials, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
224 |
CacheEntry ca, boolean isUTF8) { |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
225 |
this(fromcache, scheme, credentials, isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
assert credentials == null || (ca != null && ca.value == null); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
cacheEntry = ca; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
|
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
230 |
AuthInfo retryWithCredentials(PasswordAuthentication pw, boolean isUTF8) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
// If the info was already in the cache we need to create a new |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
// instance with fromCache==false so that it's put back in the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
233 |
// cache if authentication succeeds |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
234 |
AuthInfo res = fromcache ? new AuthInfo(false, scheme, pw, isUTF8) : this; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
res.credentials = Objects.requireNonNull(pw); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
res.retries = retries; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
return res; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
239 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
public HttpRequestImpl response(Response r) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
Cache cache = getCache(exchange); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
int status = r.statusCode(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
HttpHeaders hdrs = r.headers(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
HttpRequestImpl req = r.request(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
if (status != UNAUTHORIZED && status != PROXY_UNAUTHORIZED) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
// check if any authentication succeeded for first time |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
if (exchange.serverauth != null && !exchange.serverauth.fromcache) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
251 |
AuthInfo au = exchange.serverauth; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
252 |
cache.store(au.scheme, req.uri(), false, au.credentials, au.isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
253 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
254 |
if (exchange.proxyauth != null && !exchange.proxyauth.fromcache) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
AuthInfo au = exchange.proxyauth; |
49765 | 256 |
URI proxyURI = getProxyURI(req); |
257 |
if (proxyURI != null) { |
|
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
258 |
cache.store(au.scheme, proxyURI, true, au.credentials, au.isUTF8); |
49765 | 259 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
260 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
261 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
262 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
boolean proxy = status == PROXY_UNAUTHORIZED; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
String authname = proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; |
53374
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
266 |
List<String> authvals = hdrs.allValues(authname); |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
267 |
if (authvals.isEmpty() && exchange.client().authenticator().isPresent()) { |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
268 |
throw new IOException(authname + " header missing for response code " + status); |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
269 |
} |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
270 |
String authval = null; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
271 |
boolean isUTF8 = false; |
53374
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
272 |
for (String aval : authvals) { |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
273 |
HeaderParser parser = new HeaderParser(aval); |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
274 |
String scheme = parser.findKey(0); |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
275 |
if (scheme.equalsIgnoreCase("Basic")) { |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
276 |
authval = aval; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
277 |
var charset = parser.findValue("charset"); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
278 |
isUTF8 = (charset != null && charset.equalsIgnoreCase("UTF-8")); |
53374
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
279 |
break; |
50681 | 280 |
} |
281 |
} |
|
53374
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
282 |
if (authval == null) { |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
283 |
return null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
|
49765 | 286 |
if (proxy) { |
287 |
if (r.isConnectResponse) { |
|
288 |
if (!Utils.PROXY_TUNNEL_FILTER |
|
289 |
.test("Proxy-Authorization", BASIC_DUMMY)) { |
|
290 |
Log.logError("{0} disabled", "Proxy-Authorization"); |
|
291 |
return null; |
|
292 |
} |
|
293 |
} else if (req.proxy() != null) { |
|
294 |
if (!Utils.PROXY_FILTER |
|
295 |
.test("Proxy-Authorization", BASIC_DUMMY)) { |
|
296 |
Log.logError("{0} disabled", "Proxy-Authorization"); |
|
297 |
return null; |
|
298 |
} |
|
299 |
} |
|
300 |
} |
|
301 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
AuthInfo au = proxy ? exchange.proxyauth : exchange.serverauth; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
if (au == null) { |
49765 | 304 |
// if no authenticator, let the user deal with 407/401 |
305 |
if (!exchange.client().authenticator().isPresent()) return null; |
|
306 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
307 |
PasswordAuthentication pw = getCredentials(authval, proxy, req); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
if (pw == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
309 |
throw new IOException("No credentials provided"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
// No authentication in request. Get credentials from user |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
312 |
au = new AuthInfo(false, "Basic", pw, isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
if (proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
exchange.proxyauth = au; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
exchange.serverauth = au; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
317 |
} |
49765 | 318 |
req = HttpRequestImpl.newInstanceForAuthentication(req); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
319 |
addBasicCredentials(req, proxy, pw, isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
320 |
return req; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
321 |
} else if (au.retries > retry_limit) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
322 |
throw new IOException("too many authentication attempts. Limit: " + |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
323 |
Integer.toString(retry_limit)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
324 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
// we sent credentials, but they were rejected |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
if (au.fromcache) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
cache.remove(au.cacheEntry); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
} |
49765 | 329 |
|
330 |
// if no authenticator, let the user deal with 407/401 |
|
331 |
if (!exchange.client().authenticator().isPresent()) return null; |
|
332 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
333 |
// try again |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
334 |
PasswordAuthentication pw = getCredentials(authval, proxy, req); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
335 |
if (pw == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
336 |
throw new IOException("No credentials provided"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
} |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
338 |
au = au.retryWithCredentials(pw, isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
if (proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
exchange.proxyauth = au; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
exchange.serverauth = au; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
} |
49765 | 344 |
req = HttpRequestImpl.newInstanceForAuthentication(req); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
345 |
addBasicCredentials(req, proxy, au.credentials, isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
au.retries++; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
return req; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
// Use a WeakHashMap to make it possible for the HttpClient to |
49765 | 352 |
// be garbage collected when no longer referenced. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
static final WeakHashMap<HttpClientImpl,Cache> caches = new WeakHashMap<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
354 |
|
49765 | 355 |
static synchronized Cache getCache(MultiExchange<?> exchange) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
HttpClientImpl client = exchange.client(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
Cache c = caches.get(client); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
if (c == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
c = new Cache(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
caches.put(client, c); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
return c; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
// Note: Make sure that Cache and CacheEntry do not keep any strong |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
// reference to the HttpClient: it would prevent the client being |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
// GC'ed when no longer referenced. |
49765 | 368 |
static final class Cache { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
final LinkedList<CacheEntry> entries = new LinkedList<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
370 |
|
49765 | 371 |
Cache() {} |
372 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
373 |
synchronized CacheEntry get(URI uri, boolean proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
374 |
for (CacheEntry entry : entries) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
375 |
if (entry.equalsKey(uri, proxy)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
return entry; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
377 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
381 |
|
59029
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
382 |
private static boolean equalsIgnoreCase(String s1, String s2) { |
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
383 |
return s1 == s2 || (s1 != null && s1.equalsIgnoreCase(s2)); |
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
384 |
} |
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
385 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
synchronized void remove(String authscheme, URI domain, boolean proxy) { |
59029
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
387 |
var iterator = entries.iterator(); |
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
388 |
while (iterator.hasNext()) { |
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
389 |
var entry = iterator.next(); |
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
390 |
if (equalsIgnoreCase(entry.scheme, authscheme)) { |
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
391 |
if (entry.equalsKey(domain, proxy)) { |
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
392 |
iterator.remove(); |
3786a0962570
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException
jboes
parents:
57838
diff
changeset
|
393 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
394 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
395 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
397 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
398 |
synchronized void remove(CacheEntry entry) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
399 |
entries.remove(entry); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
401 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
synchronized void store(String authscheme, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
URI domain, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
404 |
boolean proxy, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
405 |
PasswordAuthentication value, boolean isUTF8) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
406 |
remove(authscheme, domain, proxy); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
407 |
entries.add(new CacheEntry(authscheme, domain, proxy, value, isUTF8)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
408 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
409 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
410 |
|
49765 | 411 |
static URI normalize(URI uri, boolean isPrimaryKey) { |
412 |
String path = uri.getPath(); |
|
413 |
if (path == null || path.isEmpty()) { |
|
414 |
// make sure the URI has a path, ignore query and fragment |
|
415 |
try { |
|
416 |
return new URI(uri.getScheme(), uri.getAuthority(), "/", null, null); |
|
417 |
} catch (URISyntaxException e) { |
|
418 |
throw new InternalError(e); |
|
419 |
} |
|
420 |
} else if (isPrimaryKey || !"/".equals(path)) { |
|
421 |
// remove extraneous components and normalize path |
|
422 |
return uri.resolve("."); |
|
423 |
} else { |
|
424 |
// path == "/" and the URI is not used to store |
|
425 |
// the primary key in the cache: nothing to do. |
|
426 |
return uri; |
|
427 |
} |
|
428 |
} |
|
429 |
||
430 |
static final class CacheEntry { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
431 |
final String root; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
432 |
final String scheme; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
433 |
final boolean proxy; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
434 |
final PasswordAuthentication value; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
435 |
final boolean isUTF8; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
436 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
437 |
CacheEntry(String authscheme, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
438 |
URI uri, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
439 |
boolean proxy, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
440 |
PasswordAuthentication value, boolean isUTF8) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
441 |
this.scheme = authscheme; |
49765 | 442 |
this.root = normalize(uri, true).toString(); // remove extraneous components |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
443 |
this.proxy = proxy; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
444 |
this.value = value; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
445 |
this.isUTF8 = isUTF8; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
446 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
447 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
448 |
public PasswordAuthentication value() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
449 |
return value; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
450 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
451 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
452 |
public boolean equalsKey(URI uri, boolean proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
453 |
if (this.proxy != proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
454 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
455 |
} |
49765 | 456 |
String other = String.valueOf(normalize(uri, false)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
457 |
return other.startsWith(root); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
458 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
459 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
460 |
} |