author | michaelm |
Thu, 22 Aug 2019 14:36:10 +0100 | |
changeset 57838 | 78844dceede6 |
parent 53374 | 7c0d1f696dbc |
child 59029 | 3786a0962570 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, 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; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
35 |
import java.nio.charset.Charset; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.util.Base64; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.util.LinkedList; |
53374
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
38 |
import java.util.List; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.util.Objects; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.util.WeakHashMap; |
49765 | 41 |
import java.net.http.HttpHeaders; |
42 |
import jdk.internal.net.http.common.Log; |
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
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
|
47 |
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
|
48 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
* Implementation of Http Basic authentication. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
class AuthenticationFilter implements HeaderFilter { |
49765 | 53 |
volatile MultiExchange<?> exchange; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
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
|
55 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
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
|
57 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
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
|
59 |
"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
|
60 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
static final int UNAUTHORIZED = 401; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
static final int PROXY_UNAUTHORIZED = 407; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
|
50681 | 64 |
private static final String BASIC_DUMMY = |
65 |
"Basic " + Base64.getEncoder() |
|
66 |
.encodeToString("o:o".getBytes(ISO_8859_1)); |
|
49765 | 67 |
|
48083 | 68 |
// A public no-arg constructor is required by FilterFactory |
69 |
public AuthenticationFilter() {} |
|
70 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
private PasswordAuthentication getCredentials(String header, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
boolean proxy, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
HttpRequestImpl req) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
throws IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
HttpClientImpl client = exchange.client(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
java.net.Authenticator auth = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
client.authenticator() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
.orElseThrow(() -> new IOException("No authenticator set")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
URI uri = req.uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
81 |
HeaderParser parser = new HeaderParser(header); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
82 |
String authscheme = parser.findKey(0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
String realm = parser.findValue("realm"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
85 |
java.net.Authenticator.RequestorType rtype = proxy ? PROXY : SERVER; |
49765 | 86 |
URL url = toURL(uri, req.method(), proxy); |
87 |
String host; |
|
88 |
int port; |
|
89 |
String protocol; |
|
90 |
InetSocketAddress proxyAddress; |
|
91 |
if (proxy && (proxyAddress = req.proxy()) != null) { |
|
92 |
// request sent to server through proxy |
|
93 |
proxyAddress = req.proxy(); |
|
94 |
host = proxyAddress.getHostString(); |
|
95 |
port = proxyAddress.getPort(); |
|
96 |
protocol = "http"; // we don't support https connection to proxy |
|
97 |
} else { |
|
98 |
// direct connection to server or proxy |
|
99 |
host = uri.getHost(); |
|
100 |
port = uri.getPort(); |
|
101 |
protocol = uri.getScheme(); |
|
102 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
// needs to be instance method in Authenticator |
49765 | 105 |
return auth.requestPasswordAuthenticationInstance(host, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
106 |
null, |
49765 | 107 |
port, |
108 |
protocol, |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
realm, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
authscheme, |
49765 | 111 |
url, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
rtype |
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
|
49765 | 116 |
private URL toURL(URI uri, String method, boolean proxy) |
117 |
throws MalformedURLException |
|
118 |
{ |
|
119 |
if (proxy && "CONNECT".equalsIgnoreCase(method) |
|
120 |
&& "socket".equalsIgnoreCase(uri.getScheme())) { |
|
121 |
return null; // proxy tunneling |
|
122 |
} |
|
123 |
return uri.toURL(); |
|
124 |
} |
|
125 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
private URI getProxyURI(HttpRequestImpl r) { |
48083 | 127 |
InetSocketAddress proxy = r.proxy(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
if (proxy == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
return null; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
// our own private scheme for proxy URLs |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
50681
diff
changeset
|
133 |
// 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
|
134 |
String scheme = "proxy." + r.uri().getScheme(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
return new URI(scheme, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
null, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
proxy.getHostString(), |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
proxy.getPort(), |
49765 | 140 |
"/", |
42460
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 |
null); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
} catch (URISyntaxException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
throw new InternalError(e); |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
148 |
@Override |
49765 | 149 |
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
|
150 |
// 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
|
151 |
Cache cache = getCache(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
this.exchange = e; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
// Proxy |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
if (exchange.proxyauth == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
156 |
URI proxyURI = getProxyURI(r); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
if (proxyURI != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
CacheEntry ca = cache.get(proxyURI, true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
if (ca != null) { |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
160 |
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
|
161 |
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
|
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
166 |
// Server |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
if (exchange.serverauth == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
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
|
169 |
if (ca != null) { |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
170 |
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
|
171 |
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
|
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
// 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
|
177 |
private static void addBasicCredentials(HttpRequestImpl r, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
boolean proxy, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
179 |
PasswordAuthentication pw, |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
180 |
boolean isUTF8) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
String hdrname = proxy ? "Proxy-Authorization" : "Authorization"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
StringBuilder sb = new StringBuilder(128); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
String value = "Basic " + s; |
49765 | 187 |
if (proxy) { |
188 |
if (r.isConnect()) { |
|
50681 | 189 |
if (!Utils.PROXY_TUNNEL_FILTER.test(hdrname, value)) { |
49765 | 190 |
Log.logError("{0} disabled", hdrname); |
191 |
return; |
|
192 |
} |
|
193 |
} else if (r.proxy() != null) { |
|
50681 | 194 |
if (!Utils.PROXY_FILTER.test(hdrname, value)) { |
49765 | 195 |
Log.logError("{0} disabled", hdrname); |
196 |
return; |
|
197 |
} |
|
198 |
} |
|
199 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
r.setSystemHeader(hdrname, value); |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
// 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
|
204 |
static class AuthInfo { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
205 |
final boolean fromcache; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
206 |
final String scheme; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
207 |
int retries; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
208 |
PasswordAuthentication credentials; // used in request |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
209 |
CacheEntry cacheEntry; // if used |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
210 |
final boolean isUTF8; // |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
211 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
212 |
AuthInfo(boolean fromcache, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
213 |
String scheme, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
214 |
PasswordAuthentication credentials, boolean isUTF8) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
this.fromcache = fromcache; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
this.scheme = scheme; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
this.credentials = credentials; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
this.retries = 1; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
219 |
this.isUTF8 = isUTF8; |
42460
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
AuthInfo(boolean fromcache, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
String scheme, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
PasswordAuthentication credentials, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
225 |
CacheEntry ca, boolean isUTF8) { |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
226 |
this(fromcache, scheme, credentials, isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
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
|
228 |
cacheEntry = ca; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
|
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
231 |
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
|
232 |
// 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
|
233 |
// 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
|
234 |
// cache if authentication succeeds |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
235 |
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
|
236 |
res.credentials = Objects.requireNonNull(pw); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
res.retries = retries; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
return res; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
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
|
244 |
Cache cache = getCache(exchange); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
int status = r.statusCode(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
HttpHeaders hdrs = r.headers(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
HttpRequestImpl req = r.request(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
if (status != UNAUTHORIZED && status != PROXY_UNAUTHORIZED) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
// 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
|
251 |
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
|
252 |
AuthInfo au = exchange.serverauth; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
253 |
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
|
254 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
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
|
256 |
AuthInfo au = exchange.proxyauth; |
49765 | 257 |
URI proxyURI = getProxyURI(req); |
258 |
if (proxyURI != null) { |
|
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
259 |
cache.store(au.scheme, proxyURI, true, au.credentials, au.isUTF8); |
49765 | 260 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
261 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
262 |
return null; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
boolean proxy = status == PROXY_UNAUTHORIZED; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
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
|
267 |
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
|
268 |
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
|
269 |
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
|
270 |
} |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
271 |
String authval = null; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
272 |
boolean isUTF8 = false; |
53374
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
273 |
for (String aval : authvals) { |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
274 |
HeaderParser parser = new HeaderParser(aval); |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
275 |
String scheme = parser.findKey(0); |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
276 |
if (scheme.equalsIgnoreCase("Basic")) { |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
277 |
authval = aval; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
278 |
var charset = parser.findValue("charset"); |
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
279 |
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
|
280 |
break; |
50681 | 281 |
} |
282 |
} |
|
53374
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
283 |
if (authval == null) { |
7c0d1f696dbc
8217237: HttpClient does not deal well with multi-valued WWW-Authenticate challenge headers
michaelm
parents:
52499
diff
changeset
|
284 |
return null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
|
49765 | 287 |
if (proxy) { |
288 |
if (r.isConnectResponse) { |
|
289 |
if (!Utils.PROXY_TUNNEL_FILTER |
|
290 |
.test("Proxy-Authorization", BASIC_DUMMY)) { |
|
291 |
Log.logError("{0} disabled", "Proxy-Authorization"); |
|
292 |
return null; |
|
293 |
} |
|
294 |
} else if (req.proxy() != null) { |
|
295 |
if (!Utils.PROXY_FILTER |
|
296 |
.test("Proxy-Authorization", BASIC_DUMMY)) { |
|
297 |
Log.logError("{0} disabled", "Proxy-Authorization"); |
|
298 |
return null; |
|
299 |
} |
|
300 |
} |
|
301 |
} |
|
302 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
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
|
304 |
if (au == null) { |
49765 | 305 |
// if no authenticator, let the user deal with 407/401 |
306 |
if (!exchange.client().authenticator().isPresent()) return null; |
|
307 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
PasswordAuthentication pw = getCredentials(authval, proxy, req); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
309 |
if (pw == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
throw new IOException("No credentials provided"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
// 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
|
313 |
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
|
314 |
if (proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
exchange.proxyauth = au; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
317 |
exchange.serverauth = au; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
318 |
} |
49765 | 319 |
req = HttpRequestImpl.newInstanceForAuthentication(req); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
320 |
addBasicCredentials(req, proxy, pw, isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
321 |
return req; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
322 |
} else if (au.retries > retry_limit) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
323 |
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
|
324 |
Integer.toString(retry_limit)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
// 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
|
327 |
if (au.fromcache) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
cache.remove(au.cacheEntry); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
} |
49765 | 330 |
|
331 |
// if no authenticator, let the user deal with 407/401 |
|
332 |
if (!exchange.client().authenticator().isPresent()) return null; |
|
333 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
334 |
// try again |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
335 |
PasswordAuthentication pw = getCredentials(authval, proxy, req); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
336 |
if (pw == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
throw new IOException("No credentials provided"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
338 |
} |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
339 |
au = au.retryWithCredentials(pw, isUTF8); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
if (proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
exchange.proxyauth = au; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
exchange.serverauth = au; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
} |
49765 | 345 |
req = HttpRequestImpl.newInstanceForAuthentication(req); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
346 |
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
|
347 |
au.retries++; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
return req; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
// Use a WeakHashMap to make it possible for the HttpClient to |
49765 | 353 |
// 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
|
354 |
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
|
355 |
|
49765 | 356 |
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
|
357 |
HttpClientImpl client = exchange.client(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
Cache c = caches.get(client); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
if (c == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
c = new Cache(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
caches.put(client, c); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
return c; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
// 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
|
367 |
// 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
|
368 |
// GC'ed when no longer referenced. |
49765 | 369 |
static final class Cache { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
370 |
final LinkedList<CacheEntry> entries = new LinkedList<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
371 |
|
49765 | 372 |
Cache() {} |
373 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
374 |
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
|
375 |
for (CacheEntry entry : entries) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
if (entry.equalsKey(uri, proxy)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
377 |
return entry; |
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
381 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
382 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
383 |
synchronized void remove(String authscheme, URI domain, boolean proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
384 |
for (CacheEntry entry : entries) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
385 |
if (entry.equalsKey(domain, proxy)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
entries.remove(entry); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
387 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
388 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
389 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
390 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
synchronized void remove(CacheEntry entry) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
392 |
entries.remove(entry); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
393 |
} |
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 |
synchronized void store(String authscheme, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
URI domain, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
397 |
boolean proxy, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
398 |
PasswordAuthentication value, boolean isUTF8) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
399 |
remove(authscheme, domain, proxy); |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
400 |
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
|
401 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
|
49765 | 404 |
static URI normalize(URI uri, boolean isPrimaryKey) { |
405 |
String path = uri.getPath(); |
|
406 |
if (path == null || path.isEmpty()) { |
|
407 |
// make sure the URI has a path, ignore query and fragment |
|
408 |
try { |
|
409 |
return new URI(uri.getScheme(), uri.getAuthority(), "/", null, null); |
|
410 |
} catch (URISyntaxException e) { |
|
411 |
throw new InternalError(e); |
|
412 |
} |
|
413 |
} else if (isPrimaryKey || !"/".equals(path)) { |
|
414 |
// remove extraneous components and normalize path |
|
415 |
return uri.resolve("."); |
|
416 |
} else { |
|
417 |
// path == "/" and the URI is not used to store |
|
418 |
// the primary key in the cache: nothing to do. |
|
419 |
return uri; |
|
420 |
} |
|
421 |
} |
|
422 |
||
423 |
static final class CacheEntry { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
424 |
final String root; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
425 |
final String scheme; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
426 |
final boolean proxy; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
427 |
final PasswordAuthentication value; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
428 |
final boolean isUTF8; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
429 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
430 |
CacheEntry(String authscheme, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
431 |
URI uri, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
432 |
boolean proxy, |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
433 |
PasswordAuthentication value, boolean isUTF8) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
434 |
this.scheme = authscheme; |
49765 | 435 |
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
|
436 |
this.proxy = proxy; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
437 |
this.value = value; |
57838
78844dceede6
8199849: Add support for UTF-8 encoded credentials in HTTP Basic Authentication
michaelm
parents:
53374
diff
changeset
|
438 |
this.isUTF8 = isUTF8; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
439 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
440 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
441 |
public PasswordAuthentication value() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
442 |
return value; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
443 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
444 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
445 |
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
|
446 |
if (this.proxy != proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
447 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
448 |
} |
49765 | 449 |
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
|
450 |
return other.startsWith(root); |
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
453 |
} |