author | arieber |
Sat, 22 Jun 2013 08:20:11 +0100 | |
changeset 18541 | a974731db859 |
parent 18178 | ee71c923891d |
child 18577 | f76979ce4bc4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16007
cf20f016fa94
8007315: HttpURLConnection.filterHeaderField method returns null where empty string is expected
coffeys
parents:
14766
diff
changeset
|
2 |
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.net.www.protocol.http; |
|
27 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
28 |
import java.util.Arrays; |
2 | 29 |
import java.net.URL; |
30 |
import java.net.URLConnection; |
|
31 |
import java.net.ProtocolException; |
|
32 |
import java.net.HttpRetryException; |
|
33 |
import java.net.PasswordAuthentication; |
|
34 |
import java.net.Authenticator; |
|
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
35 |
import java.net.HttpCookie; |
2 | 36 |
import java.net.InetAddress; |
37 |
import java.net.UnknownHostException; |
|
38 |
import java.net.SocketTimeoutException; |
|
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
39 |
import java.net.SocketPermission; |
2 | 40 |
import java.net.Proxy; |
41 |
import java.net.ProxySelector; |
|
42 |
import java.net.URI; |
|
43 |
import java.net.InetSocketAddress; |
|
44 |
import java.net.CookieHandler; |
|
45 |
import java.net.ResponseCache; |
|
46 |
import java.net.CacheResponse; |
|
47 |
import java.net.SecureCacheResponse; |
|
48 |
import java.net.CacheRequest; |
|
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
49 |
import java.net.HttpURLPermission; |
2 | 50 |
import java.net.Authenticator.RequestorType; |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
51 |
import java.security.AccessController; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
52 |
import java.security.PrivilegedExceptionAction; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
53 |
import java.security.PrivilegedActionException; |
2 | 54 |
import java.io.*; |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
55 |
import java.net.*; |
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
56 |
import java.util.ArrayList; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
57 |
import java.util.Collections; |
2 | 58 |
import java.util.Date; |
59 |
import java.util.Map; |
|
60 |
import java.util.List; |
|
61 |
import java.util.Locale; |
|
62 |
import java.util.StringTokenizer; |
|
63 |
import java.util.Iterator; |
|
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
64 |
import java.util.HashSet; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
65 |
import java.util.HashMap; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
66 |
import java.util.Set; |
2 | 67 |
import sun.net.*; |
68 |
import sun.net.www.*; |
|
69 |
import sun.net.www.http.HttpClient; |
|
70 |
import sun.net.www.http.PosterOutputStream; |
|
71 |
import sun.net.www.http.ChunkedInputStream; |
|
72 |
import sun.net.www.http.ChunkedOutputStream; |
|
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
73 |
import sun.util.logging.PlatformLogger; |
2 | 74 |
import java.text.SimpleDateFormat; |
75 |
import java.util.TimeZone; |
|
76 |
import java.net.MalformedURLException; |
|
77 |
import java.nio.ByteBuffer; |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
78 |
import static sun.net.www.protocol.http.AuthScheme.BASIC; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
79 |
import static sun.net.www.protocol.http.AuthScheme.DIGEST; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
80 |
import static sun.net.www.protocol.http.AuthScheme.NTLM; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
81 |
import static sun.net.www.protocol.http.AuthScheme.NEGOTIATE; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
82 |
import static sun.net.www.protocol.http.AuthScheme.KERBEROS; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
83 |
import static sun.net.www.protocol.http.AuthScheme.UNKNOWN; |
2 | 84 |
|
85 |
/** |
|
86 |
* A class to represent an HTTP connection to a remote object. |
|
87 |
*/ |
|
88 |
||
89 |
||
90 |
public class HttpURLConnection extends java.net.HttpURLConnection { |
|
91 |
||
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
92 |
static String HTTP_CONNECT = "CONNECT"; |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
93 |
|
2 | 94 |
static final String version; |
95 |
public static final String userAgent; |
|
96 |
||
97 |
/* max # of allowed re-directs */ |
|
98 |
static final int defaultmaxRedirects = 20; |
|
99 |
static final int maxRedirects; |
|
100 |
||
101 |
/* Not all servers support the (Proxy)-Authentication-Info headers. |
|
102 |
* By default, we don't require them to be sent |
|
103 |
*/ |
|
104 |
static final boolean validateProxy; |
|
105 |
static final boolean validateServer; |
|
106 |
||
107 |
private StreamingOutputStream strOutputStream; |
|
108 |
private final static String RETRY_MSG1 = |
|
109 |
"cannot retry due to proxy authentication, in streaming mode"; |
|
110 |
private final static String RETRY_MSG2 = |
|
111 |
"cannot retry due to server authentication, in streaming mode"; |
|
112 |
private final static String RETRY_MSG3 = |
|
113 |
"cannot retry due to redirection, in streaming mode"; |
|
114 |
||
115 |
/* |
|
116 |
* System properties related to error stream handling: |
|
117 |
* |
|
118 |
* sun.net.http.errorstream.enableBuffering = <boolean> |
|
119 |
* |
|
120 |
* With the above system property set to true (default is false), |
|
121 |
* when the response code is >=400, the HTTP handler will try to |
|
122 |
* buffer the response body (up to a certain amount and within a |
|
123 |
* time limit). Thus freeing up the underlying socket connection |
|
124 |
* for reuse. The rationale behind this is that usually when the |
|
125 |
* server responds with a >=400 error (client error or server |
|
126 |
* error, such as 404 file not found), the server will send a |
|
127 |
* small response body to explain who to contact and what to do to |
|
128 |
* recover. With this property set to true, even if the |
|
129 |
* application doesn't call getErrorStream(), read the response |
|
130 |
* body, and then call close(), the underlying socket connection |
|
131 |
* can still be kept-alive and reused. The following two system |
|
132 |
* properties provide further control to the error stream |
|
133 |
* buffering behaviour. |
|
134 |
* |
|
135 |
* sun.net.http.errorstream.timeout = <int> |
|
136 |
* the timeout (in millisec) waiting the error stream |
|
137 |
* to be buffered; default is 300 ms |
|
138 |
* |
|
139 |
* sun.net.http.errorstream.bufferSize = <int> |
|
140 |
* the size (in bytes) to use for the buffering the error stream; |
|
141 |
* default is 4k |
|
142 |
*/ |
|
143 |
||
144 |
||
145 |
/* Should we enable buffering of error streams? */ |
|
146 |
private static boolean enableESBuffer = false; |
|
147 |
||
148 |
/* timeout waiting for read for buffered error stream; |
|
149 |
*/ |
|
150 |
private static int timeout4ESBuffer = 0; |
|
151 |
||
152 |
/* buffer size for buffered error stream; |
|
153 |
*/ |
|
154 |
private static int bufSize4ES = 0; |
|
155 |
||
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
156 |
/* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
157 |
* Restrict setting of request headers through the public api |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
158 |
* consistent with JavaScript XMLHttpRequest2 with a few |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
159 |
* exceptions. Disallowed headers are silently ignored for |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
160 |
* backwards compatibility reasons rather than throwing a |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
161 |
* SecurityException. For example, some applets set the |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
162 |
* Host header since old JREs did not implement HTTP 1.1. |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
163 |
* Additionally, any header starting with Sec- is |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
164 |
* disallowed. |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
165 |
* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
166 |
* The following headers are allowed for historical reasons: |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
167 |
* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
168 |
* Accept-Charset, Accept-Encoding, Cookie, Cookie2, Date, |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
169 |
* Referer, TE, User-Agent, headers beginning with Proxy-. |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
170 |
* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
171 |
* The following headers are allowed in a limited form: |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
172 |
* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
173 |
* Connection: close |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
174 |
* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
175 |
* See http://www.w3.org/TR/XMLHttpRequest2. |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
176 |
*/ |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
177 |
private static final boolean allowRestrictedHeaders; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
178 |
private static final Set<String> restrictedHeaderSet; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
179 |
private static final String[] restrictedHeaders = { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
180 |
/* Restricted by XMLHttpRequest2 */ |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
181 |
//"Accept-Charset", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
182 |
//"Accept-Encoding", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
183 |
"Access-Control-Request-Headers", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
184 |
"Access-Control-Request-Method", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
185 |
"Connection", /* close is allowed */ |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
186 |
"Content-Length", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
187 |
//"Cookie", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
188 |
//"Cookie2", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
189 |
"Content-Transfer-Encoding", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
190 |
//"Date", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
191 |
//"Expect", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
192 |
"Host", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
193 |
"Keep-Alive", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
194 |
"Origin", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
195 |
// "Referer", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
196 |
// "TE", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
197 |
"Trailer", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
198 |
"Transfer-Encoding", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
199 |
"Upgrade", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
200 |
//"User-Agent", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
201 |
"Via" |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
202 |
}; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
203 |
|
2 | 204 |
static { |
205 |
maxRedirects = java.security.AccessController.doPrivileged( |
|
51 | 206 |
new sun.security.action.GetIntegerAction( |
207 |
"http.maxRedirects", defaultmaxRedirects)).intValue(); |
|
2 | 208 |
version = java.security.AccessController.doPrivileged( |
209 |
new sun.security.action.GetPropertyAction("java.version")); |
|
210 |
String agent = java.security.AccessController.doPrivileged( |
|
211 |
new sun.security.action.GetPropertyAction("http.agent")); |
|
212 |
if (agent == null) { |
|
213 |
agent = "Java/"+version; |
|
214 |
} else { |
|
215 |
agent = agent + " Java/"+version; |
|
216 |
} |
|
217 |
userAgent = agent; |
|
218 |
validateProxy = java.security.AccessController.doPrivileged( |
|
219 |
new sun.security.action.GetBooleanAction( |
|
220 |
"http.auth.digest.validateProxy")).booleanValue(); |
|
221 |
validateServer = java.security.AccessController.doPrivileged( |
|
222 |
new sun.security.action.GetBooleanAction( |
|
223 |
"http.auth.digest.validateServer")).booleanValue(); |
|
224 |
||
225 |
enableESBuffer = java.security.AccessController.doPrivileged( |
|
226 |
new sun.security.action.GetBooleanAction( |
|
227 |
"sun.net.http.errorstream.enableBuffering")).booleanValue(); |
|
228 |
timeout4ESBuffer = java.security.AccessController.doPrivileged( |
|
229 |
new sun.security.action.GetIntegerAction( |
|
230 |
"sun.net.http.errorstream.timeout", 300)).intValue(); |
|
231 |
if (timeout4ESBuffer <= 0) { |
|
232 |
timeout4ESBuffer = 300; // use the default |
|
233 |
} |
|
234 |
||
235 |
bufSize4ES = java.security.AccessController.doPrivileged( |
|
236 |
new sun.security.action.GetIntegerAction( |
|
237 |
"sun.net.http.errorstream.bufferSize", 4096)).intValue(); |
|
238 |
if (bufSize4ES <= 0) { |
|
239 |
bufSize4ES = 4096; // use the default |
|
240 |
} |
|
241 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
242 |
allowRestrictedHeaders = java.security.AccessController.doPrivileged( |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
243 |
new sun.security.action.GetBooleanAction( |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
244 |
"sun.net.http.allowRestrictedHeaders")).booleanValue(); |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
245 |
if (!allowRestrictedHeaders) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
246 |
restrictedHeaderSet = new HashSet<String>(restrictedHeaders.length); |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
247 |
for (int i=0; i < restrictedHeaders.length; i++) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
248 |
restrictedHeaderSet.add(restrictedHeaders[i].toLowerCase()); |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
249 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
250 |
} else { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
251 |
restrictedHeaderSet = null; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
252 |
} |
2 | 253 |
} |
254 |
||
255 |
static final String httpVersion = "HTTP/1.1"; |
|
256 |
static final String acceptString = |
|
257 |
"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"; |
|
258 |
||
259 |
// the following http request headers should NOT have their values |
|
260 |
// returned for security reasons. |
|
261 |
private static final String[] EXCLUDE_HEADERS = { |
|
262 |
"Proxy-Authorization", |
|
263 |
"Authorization" |
|
264 |
}; |
|
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
265 |
|
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
266 |
// also exclude system cookies when any might be set |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
267 |
private static final String[] EXCLUDE_HEADERS2= { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
268 |
"Proxy-Authorization", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
269 |
"Authorization", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
270 |
"Cookie", |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
271 |
"Cookie2" |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
272 |
}; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
273 |
|
2 | 274 |
protected HttpClient http; |
275 |
protected Handler handler; |
|
276 |
protected Proxy instProxy; |
|
277 |
||
278 |
private CookieHandler cookieHandler; |
|
11839
53065c5f535d
7133367: ResponseCache.put should not be called when setUseCaches(false)
chegar
parents:
11528
diff
changeset
|
279 |
private final ResponseCache cacheHandler; |
2 | 280 |
|
281 |
// the cached response, and cached response headers and body |
|
282 |
protected CacheResponse cachedResponse; |
|
283 |
private MessageHeader cachedHeaders; |
|
284 |
private InputStream cachedInputStream; |
|
285 |
||
286 |
/* output stream to server */ |
|
287 |
protected PrintStream ps = null; |
|
288 |
||
289 |
||
290 |
/* buffered error stream */ |
|
291 |
private InputStream errorStream = null; |
|
292 |
||
293 |
/* User set Cookies */ |
|
294 |
private boolean setUserCookies = true; |
|
295 |
private String userCookies = null; |
|
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
296 |
private String userCookies2 = null; |
2 | 297 |
|
298 |
/* We only have a single static authenticator for now. |
|
299 |
* REMIND: backwards compatibility with JDK 1.1. Should be |
|
300 |
* eliminated for JDK 2.0. |
|
301 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
302 |
@Deprecated |
2 | 303 |
private static HttpAuthenticator defaultAuth; |
304 |
||
305 |
/* all the headers we send |
|
306 |
* NOTE: do *NOT* dump out the content of 'requests' in the |
|
307 |
* output or stacktrace since it may contain security-sensitive |
|
308 |
* headers such as those defined in EXCLUDE_HEADERS. |
|
309 |
*/ |
|
310 |
private MessageHeader requests; |
|
311 |
||
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
312 |
/* The headers actually set by the user are recorded here also |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
313 |
*/ |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
314 |
private MessageHeader userHeaders; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
315 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
316 |
/* Headers and request method cannot be changed |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
317 |
* once this flag is set in :- |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
318 |
* - getOutputStream() |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
319 |
* - getInputStream()) |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
320 |
* - connect() |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
321 |
* Access synchronized on this. |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
322 |
*/ |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
323 |
private boolean connecting = false; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
324 |
|
2 | 325 |
/* The following two fields are only used with Digest Authentication */ |
326 |
String domain; /* The list of authentication domains */ |
|
327 |
DigestAuthentication.Parameters digestparams; |
|
328 |
||
329 |
/* Current credentials in use */ |
|
330 |
AuthenticationInfo currentProxyCredentials = null; |
|
331 |
AuthenticationInfo currentServerCredentials = null; |
|
332 |
boolean needToCheck = true; |
|
333 |
private boolean doingNTLM2ndStage = false; /* doing the 2nd stage of an NTLM server authentication */ |
|
334 |
private boolean doingNTLMp2ndStage = false; /* doing the 2nd stage of an NTLM proxy authentication */ |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
335 |
|
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
336 |
/* try auth without calling Authenticator. Used for transparent NTLM authentication */ |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
337 |
private boolean tryTransparentNTLMServer = true; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
338 |
private boolean tryTransparentNTLMProxy = true; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
339 |
|
3061 | 340 |
/* Used by Windows specific code */ |
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
341 |
private Object authObj; |
2 | 342 |
|
343 |
/* Set if the user is manually setting the Authorization or Proxy-Authorization headers */ |
|
344 |
boolean isUserServerAuth; |
|
345 |
boolean isUserProxyAuth; |
|
346 |
||
5199
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
347 |
String serverAuthKey, proxyAuthKey; |
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
348 |
|
2 | 349 |
/* Progress source */ |
350 |
protected ProgressSource pi; |
|
351 |
||
352 |
/* all the response headers we get back */ |
|
353 |
private MessageHeader responses; |
|
354 |
/* the stream _from_ the server */ |
|
355 |
private InputStream inputStream = null; |
|
356 |
/* post stream _to_ the server, if any */ |
|
357 |
private PosterOutputStream poster = null; |
|
358 |
||
359 |
/* Indicates if the std. request headers have been set in requests. */ |
|
360 |
private boolean setRequests=false; |
|
361 |
||
362 |
/* Indicates whether a request has already failed or not */ |
|
363 |
private boolean failedOnce=false; |
|
364 |
||
365 |
/* Remembered Exception, we will throw it again if somebody |
|
366 |
calls getInputStream after disconnect */ |
|
367 |
private Exception rememberedException = null; |
|
368 |
||
369 |
/* If we decide we want to reuse a client, we put it here */ |
|
370 |
private HttpClient reuseClient = null; |
|
371 |
||
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
372 |
/* Tunnel states */ |
14008
b572c1f3a7ad
7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents:
14007
diff
changeset
|
373 |
public enum TunnelState { |
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
374 |
/* No tunnel */ |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
375 |
NONE, |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
376 |
|
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
377 |
/* Setting up a tunnel */ |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
378 |
SETUP, |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
379 |
|
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
380 |
/* Tunnel has been successfully setup */ |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
381 |
TUNNELING |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
382 |
} |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
383 |
|
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
384 |
private TunnelState tunnelState = TunnelState.NONE; |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
385 |
|
7022
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
6879
diff
changeset
|
386 |
/* Redefine timeouts from java.net.URLConnection as we need -1 to mean |
2 | 387 |
* not set. This is to ensure backward compatibility. |
388 |
*/ |
|
7022
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
6879
diff
changeset
|
389 |
private int connectTimeout = NetworkClient.DEFAULT_CONNECT_TIMEOUT; |
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
6879
diff
changeset
|
390 |
private int readTimeout = NetworkClient.DEFAULT_READ_TIMEOUT; |
2 | 391 |
|
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
392 |
/* A permission converted from a HttpURLPermission */ |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
393 |
private SocketPermission socketPermission; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
394 |
|
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
395 |
/* Logging support */ |
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
396 |
private static final PlatformLogger logger = |
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
397 |
PlatformLogger.getLogger("sun.net.www.protocol.http.HttpURLConnection"); |
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
398 |
|
2 | 399 |
/* |
400 |
* privileged request password authentication |
|
401 |
* |
|
402 |
*/ |
|
403 |
private static PasswordAuthentication |
|
404 |
privilegedRequestPasswordAuthentication( |
|
405 |
final String host, |
|
406 |
final InetAddress addr, |
|
407 |
final int port, |
|
408 |
final String protocol, |
|
409 |
final String prompt, |
|
410 |
final String scheme, |
|
411 |
final URL url, |
|
412 |
final RequestorType authType) { |
|
51 | 413 |
return java.security.AccessController.doPrivileged( |
414 |
new java.security.PrivilegedAction<PasswordAuthentication>() { |
|
415 |
public PasswordAuthentication run() { |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
416 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
417 |
logger.finest("Requesting Authentication: host =" + host + " url = " + url); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
418 |
} |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
419 |
PasswordAuthentication pass = Authenticator.requestPasswordAuthentication( |
2 | 420 |
host, addr, port, protocol, |
421 |
prompt, scheme, url, authType); |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
422 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
423 |
logger.finest("Authentication returned: " + (pass != null ? pass.toString() : "null")); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
424 |
} |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
425 |
return pass; |
2 | 426 |
} |
427 |
}); |
|
428 |
} |
|
429 |
||
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
430 |
private boolean isRestrictedHeader(String key, String value) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
431 |
if (allowRestrictedHeaders) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
432 |
return false; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
433 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
434 |
|
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
435 |
key = key.toLowerCase(); |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
436 |
if (restrictedHeaderSet.contains(key)) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
437 |
/* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
438 |
* Exceptions to restricted headers: |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
439 |
* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
440 |
* Allow "Connection: close". |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
441 |
*/ |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
442 |
if (key.equals("connection") && value.equalsIgnoreCase("close")) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
443 |
return false; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
444 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
445 |
return true; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
446 |
} else if (key.startsWith("sec-")) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
447 |
return true; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
448 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
449 |
return false; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
450 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
451 |
|
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
452 |
/* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
453 |
* Checks the validity of http message header and whether the header |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
454 |
* is restricted and throws IllegalArgumentException if invalid or |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
455 |
* restricted. |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
456 |
*/ |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
457 |
private boolean isExternalMessageHeaderAllowed(String key, String value) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
458 |
checkMessageHeader(key, value); |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
459 |
if (!isRestrictedHeader(key, value)) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
460 |
return true; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
461 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
462 |
return false; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
463 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
464 |
|
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
465 |
/* Logging support */ |
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
466 |
public static PlatformLogger getHttpLogger() { |
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
467 |
return logger; |
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
468 |
} |
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
469 |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
470 |
/* Used for Windows NTLM implementation */ |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
471 |
public Object authObj() { |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
472 |
return authObj; |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
473 |
} |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
474 |
|
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
475 |
public void authObj(Object authObj) { |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
476 |
this.authObj = authObj; |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
477 |
} |
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
478 |
|
2 | 479 |
/* |
480 |
* checks the validity of http message header and throws |
|
481 |
* IllegalArgumentException if invalid. |
|
482 |
*/ |
|
483 |
private void checkMessageHeader(String key, String value) { |
|
484 |
char LF = '\n'; |
|
485 |
int index = key.indexOf(LF); |
|
486 |
if (index != -1) { |
|
487 |
throw new IllegalArgumentException( |
|
488 |
"Illegal character(s) in message header field: " + key); |
|
489 |
} |
|
490 |
else { |
|
491 |
if (value == null) { |
|
492 |
return; |
|
493 |
} |
|
494 |
||
495 |
index = value.indexOf(LF); |
|
496 |
while (index != -1) { |
|
497 |
index++; |
|
498 |
if (index < value.length()) { |
|
499 |
char c = value.charAt(index); |
|
500 |
if ((c==' ') || (c=='\t')) { |
|
501 |
// ok, check the next occurrence |
|
502 |
index = value.indexOf(LF, index); |
|
503 |
continue; |
|
504 |
} |
|
505 |
} |
|
506 |
throw new IllegalArgumentException( |
|
507 |
"Illegal character(s) in message header value: " + value); |
|
508 |
} |
|
509 |
} |
|
510 |
} |
|
511 |
||
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
512 |
public synchronized void setRequestMethod(String method) |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
513 |
throws ProtocolException { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
514 |
if (connecting) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
515 |
throw new IllegalStateException("connect in progress"); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
516 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
517 |
super.setRequestMethod(method); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
518 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
519 |
|
2 | 520 |
/* adds the standard key/val pairs to reqests if necessary & write to |
521 |
* given PrintStream |
|
522 |
*/ |
|
523 |
private void writeRequests() throws IOException { |
|
524 |
/* print all message headers in the MessageHeader |
|
525 |
* onto the wire - all the ones we've set and any |
|
526 |
* others that have been set |
|
527 |
*/ |
|
528 |
// send any pre-emptive authentication |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
529 |
if (http.usingProxy && tunnelState() != TunnelState.TUNNELING) { |
2 | 530 |
setPreemptiveProxyAuthentication(requests); |
531 |
} |
|
532 |
if (!setRequests) { |
|
533 |
||
534 |
/* We're very particular about the order in which we |
|
535 |
* set the request headers here. The order should not |
|
536 |
* matter, but some careless CGI programs have been |
|
537 |
* written to expect a very particular order of the |
|
538 |
* standard headers. To name names, the order in which |
|
539 |
* Navigator3.0 sends them. In particular, we make *sure* |
|
540 |
* to send Content-type: <> and Content-length:<> second |
|
541 |
* to last and last, respectively, in the case of a POST |
|
542 |
* request. |
|
543 |
*/ |
|
544 |
if (!failedOnce) |
|
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
545 |
requests.prepend(method + " " + getRequestURI()+" " + |
2 | 546 |
httpVersion, null); |
547 |
if (!getUseCaches()) { |
|
548 |
requests.setIfNotSet ("Cache-Control", "no-cache"); |
|
549 |
requests.setIfNotSet ("Pragma", "no-cache"); |
|
550 |
} |
|
551 |
requests.setIfNotSet("User-Agent", userAgent); |
|
552 |
int port = url.getPort(); |
|
553 |
String host = url.getHost(); |
|
554 |
if (port != -1 && port != url.getDefaultPort()) { |
|
555 |
host += ":" + String.valueOf(port); |
|
556 |
} |
|
557 |
requests.setIfNotSet("Host", host); |
|
558 |
requests.setIfNotSet("Accept", acceptString); |
|
559 |
||
560 |
/* |
|
561 |
* For HTTP/1.1 the default behavior is to keep connections alive. |
|
562 |
* However, we may be talking to a 1.0 server so we should set |
|
563 |
* keep-alive just in case, except if we have encountered an error |
|
564 |
* or if keep alive is disabled via a system property |
|
565 |
*/ |
|
566 |
||
567 |
// Try keep-alive only on first attempt |
|
568 |
if (!failedOnce && http.getHttpKeepAliveSet()) { |
|
4923
cd2829ef32e2
6365587: Proxy-Connection header sent through tunnel
chegar
parents:
4158
diff
changeset
|
569 |
if (http.usingProxy && tunnelState() != TunnelState.TUNNELING) { |
2 | 570 |
requests.setIfNotSet("Proxy-Connection", "keep-alive"); |
571 |
} else { |
|
572 |
requests.setIfNotSet("Connection", "keep-alive"); |
|
573 |
} |
|
574 |
} else { |
|
575 |
/* |
|
576 |
* RFC 2616 HTTP/1.1 section 14.10 says: |
|
577 |
* HTTP/1.1 applications that do not support persistent |
|
578 |
* connections MUST include the "close" connection option |
|
579 |
* in every message |
|
580 |
*/ |
|
581 |
requests.setIfNotSet("Connection", "close"); |
|
582 |
} |
|
583 |
// Set modified since if necessary |
|
584 |
long modTime = getIfModifiedSince(); |
|
585 |
if (modTime != 0 ) { |
|
586 |
Date date = new Date(modTime); |
|
587 |
//use the preferred date format according to RFC 2068(HTTP1.1), |
|
588 |
// RFC 822 and RFC 1123 |
|
589 |
SimpleDateFormat fo = |
|
590 |
new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US); |
|
591 |
fo.setTimeZone(TimeZone.getTimeZone("GMT")); |
|
592 |
requests.setIfNotSet("If-Modified-Since", fo.format(date)); |
|
593 |
} |
|
594 |
// check for preemptive authorization |
|
595 |
AuthenticationInfo sauth = AuthenticationInfo.getServerAuth(url); |
|
596 |
if (sauth != null && sauth.supportsPreemptiveAuthorization() ) { |
|
597 |
// Sets "Authorization" |
|
598 |
requests.setIfNotSet(sauth.getHeaderName(), sauth.getHeaderValue(url,method)); |
|
599 |
currentServerCredentials = sauth; |
|
600 |
} |
|
601 |
||
602 |
if (!method.equals("PUT") && (poster != null || streaming())) { |
|
603 |
requests.setIfNotSet ("Content-type", |
|
604 |
"application/x-www-form-urlencoded"); |
|
605 |
} |
|
606 |
||
6873
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
607 |
boolean chunked = false; |
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
608 |
|
2 | 609 |
if (streaming()) { |
610 |
if (chunkLength != -1) { |
|
611 |
requests.set ("Transfer-Encoding", "chunked"); |
|
6873
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
612 |
chunked = true; |
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
613 |
} else { /* fixed content length */ |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
614 |
if (fixedContentLengthLong != -1) { |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
615 |
requests.set ("Content-Length", |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
616 |
String.valueOf(fixedContentLengthLong)); |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
617 |
} else if (fixedContentLength != -1) { |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
618 |
requests.set ("Content-Length", |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
619 |
String.valueOf(fixedContentLength)); |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
620 |
} |
2 | 621 |
} |
622 |
} else if (poster != null) { |
|
623 |
/* add Content-Length & POST/PUT data */ |
|
624 |
synchronized (poster) { |
|
625 |
/* close it, so no more data can be added */ |
|
626 |
poster.close(); |
|
627 |
requests.set("Content-Length", |
|
628 |
String.valueOf(poster.size())); |
|
629 |
} |
|
630 |
} |
|
631 |
||
6873
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
632 |
if (!chunked) { |
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
633 |
if (requests.findValue("Transfer-Encoding") != null) { |
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
634 |
requests.remove("Transfer-Encoding"); |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
635 |
if (logger.isLoggable(PlatformLogger.Level.WARNING)) { |
6873
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
636 |
logger.warning( |
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
637 |
"use streaming mode for chunked encoding"); |
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
638 |
} |
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
639 |
} |
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
640 |
} |
7fe1f614001e
6952017: HttpURLConnection chunked encoding issue (Http request splitting)
michaelm
parents:
5979
diff
changeset
|
641 |
|
2 | 642 |
// get applicable cookies based on the uri and request headers |
643 |
// add them to the existing request headers |
|
644 |
setCookieHeader(); |
|
645 |
||
646 |
setRequests=true; |
|
647 |
} |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
648 |
if (logger.isLoggable(PlatformLogger.Level.FINE)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
649 |
logger.fine(requests.toString()); |
2 | 650 |
} |
6673
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
6292
diff
changeset
|
651 |
http.writeRequests(requests, poster, streaming()); |
2 | 652 |
if (ps.checkError()) { |
653 |
String proxyHost = http.getProxyHostUsed(); |
|
654 |
int proxyPort = http.getProxyPortUsed(); |
|
655 |
disconnectInternal(); |
|
656 |
if (failedOnce) { |
|
657 |
throw new IOException("Error writing to server"); |
|
658 |
} else { // try once more |
|
659 |
failedOnce=true; |
|
660 |
if (proxyHost != null) { |
|
661 |
setProxiedClient(url, proxyHost, proxyPort); |
|
662 |
} else { |
|
663 |
setNewClient (url); |
|
664 |
} |
|
665 |
ps = (PrintStream) http.getOutputStream(); |
|
666 |
connected=true; |
|
667 |
responses = new MessageHeader(); |
|
668 |
setRequests=false; |
|
669 |
writeRequests(); |
|
670 |
} |
|
671 |
} |
|
672 |
} |
|
673 |
||
674 |
||
675 |
/** |
|
676 |
* Create a new HttpClient object, bypassing the cache of |
|
677 |
* HTTP client objects/connections. |
|
678 |
* |
|
679 |
* @param url the URL being accessed |
|
680 |
*/ |
|
681 |
protected void setNewClient (URL url) |
|
682 |
throws IOException { |
|
683 |
setNewClient(url, false); |
|
684 |
} |
|
685 |
||
686 |
/** |
|
687 |
* Obtain a HttpsClient object. Use the cached copy if specified. |
|
688 |
* |
|
689 |
* @param url the URL being accessed |
|
690 |
* @param useCache whether the cached connection should be used |
|
691 |
* if present |
|
692 |
*/ |
|
693 |
protected void setNewClient (URL url, boolean useCache) |
|
694 |
throws IOException { |
|
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11839
diff
changeset
|
695 |
http = HttpClient.New(url, null, -1, useCache, connectTimeout, this); |
2 | 696 |
http.setReadTimeout(readTimeout); |
697 |
} |
|
698 |
||
699 |
||
700 |
/** |
|
701 |
* Create a new HttpClient object, set up so that it uses |
|
702 |
* per-instance proxying to the given HTTP proxy. This |
|
703 |
* bypasses the cache of HTTP client objects/connections. |
|
704 |
* |
|
705 |
* @param url the URL being accessed |
|
706 |
* @param proxyHost the proxy host to use |
|
707 |
* @param proxyPort the proxy port to use |
|
708 |
*/ |
|
709 |
protected void setProxiedClient (URL url, String proxyHost, int proxyPort) |
|
710 |
throws IOException { |
|
711 |
setProxiedClient(url, proxyHost, proxyPort, false); |
|
712 |
} |
|
713 |
||
714 |
/** |
|
715 |
* Obtain a HttpClient object, set up so that it uses per-instance |
|
716 |
* proxying to the given HTTP proxy. Use the cached copy of HTTP |
|
717 |
* client objects/connections if specified. |
|
718 |
* |
|
719 |
* @param url the URL being accessed |
|
720 |
* @param proxyHost the proxy host to use |
|
721 |
* @param proxyPort the proxy port to use |
|
722 |
* @param useCache whether the cached connection should be used |
|
723 |
* if present |
|
724 |
*/ |
|
725 |
protected void setProxiedClient (URL url, |
|
726 |
String proxyHost, int proxyPort, |
|
727 |
boolean useCache) |
|
728 |
throws IOException { |
|
729 |
proxiedConnect(url, proxyHost, proxyPort, useCache); |
|
730 |
} |
|
731 |
||
732 |
protected void proxiedConnect(URL url, |
|
733 |
String proxyHost, int proxyPort, |
|
734 |
boolean useCache) |
|
735 |
throws IOException { |
|
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11839
diff
changeset
|
736 |
http = HttpClient.New (url, proxyHost, proxyPort, useCache, |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11839
diff
changeset
|
737 |
connectTimeout, this); |
2 | 738 |
http.setReadTimeout(readTimeout); |
739 |
} |
|
740 |
||
741 |
protected HttpURLConnection(URL u, Handler handler) |
|
742 |
throws IOException { |
|
743 |
// we set proxy == null to distinguish this case with the case |
|
744 |
// when per connection proxy is set |
|
745 |
this(u, null, handler); |
|
746 |
} |
|
747 |
||
748 |
public HttpURLConnection(URL u, String host, int port) { |
|
749 |
this(u, new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(host, port))); |
|
750 |
} |
|
751 |
||
752 |
/** this constructor is used by other protocol handlers such as ftp |
|
753 |
that want to use http to fetch urls on their behalf.*/ |
|
754 |
public HttpURLConnection(URL u, Proxy p) { |
|
755 |
this(u, p, new Handler()); |
|
756 |
} |
|
757 |
||
758 |
protected HttpURLConnection(URL u, Proxy p, Handler handler) { |
|
759 |
super(u); |
|
760 |
requests = new MessageHeader(); |
|
761 |
responses = new MessageHeader(); |
|
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
762 |
userHeaders = new MessageHeader(); |
2 | 763 |
this.handler = handler; |
764 |
instProxy = p; |
|
3450
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
765 |
if (instProxy instanceof sun.net.ApplicationProxy) { |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
766 |
/* Application set Proxies should not have access to cookies |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
767 |
* in a secure environment unless explicitly allowed. */ |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
768 |
try { |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
769 |
cookieHandler = CookieHandler.getDefault(); |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
770 |
} catch (SecurityException se) { /* swallow exception */ } |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
771 |
} else { |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
772 |
cookieHandler = java.security.AccessController.doPrivileged( |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
773 |
new java.security.PrivilegedAction<CookieHandler>() { |
51 | 774 |
public CookieHandler run() { |
3450
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
775 |
return CookieHandler.getDefault(); |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
776 |
} |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
777 |
}); |
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
1576
diff
changeset
|
778 |
} |
51 | 779 |
cacheHandler = java.security.AccessController.doPrivileged( |
780 |
new java.security.PrivilegedAction<ResponseCache>() { |
|
781 |
public ResponseCache run() { |
|
2 | 782 |
return ResponseCache.getDefault(); |
783 |
} |
|
784 |
}); |
|
785 |
} |
|
786 |
||
787 |
/** |
|
788 |
* @deprecated. Use java.net.Authenticator.setDefault() instead. |
|
789 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
790 |
@Deprecated |
2 | 791 |
public static void setDefaultAuthenticator(HttpAuthenticator a) { |
792 |
defaultAuth = a; |
|
793 |
} |
|
794 |
||
795 |
/** |
|
796 |
* opens a stream allowing redirects only to the same host. |
|
797 |
*/ |
|
798 |
public static InputStream openConnectionCheckRedirects(URLConnection c) |
|
799 |
throws IOException |
|
800 |
{ |
|
801 |
boolean redir; |
|
802 |
int redirects = 0; |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
803 |
InputStream in; |
2 | 804 |
|
805 |
do { |
|
806 |
if (c instanceof HttpURLConnection) { |
|
807 |
((HttpURLConnection) c).setInstanceFollowRedirects(false); |
|
808 |
} |
|
809 |
||
810 |
// We want to open the input stream before |
|
811 |
// getting headers, because getHeaderField() |
|
812 |
// et al swallow IOExceptions. |
|
813 |
in = c.getInputStream(); |
|
814 |
redir = false; |
|
815 |
||
816 |
if (c instanceof HttpURLConnection) { |
|
817 |
HttpURLConnection http = (HttpURLConnection) c; |
|
818 |
int stat = http.getResponseCode(); |
|
819 |
if (stat >= 300 && stat <= 307 && stat != 306 && |
|
820 |
stat != HttpURLConnection.HTTP_NOT_MODIFIED) { |
|
821 |
URL base = http.getURL(); |
|
822 |
String loc = http.getHeaderField("Location"); |
|
823 |
URL target = null; |
|
824 |
if (loc != null) { |
|
825 |
target = new URL(base, loc); |
|
826 |
} |
|
827 |
http.disconnect(); |
|
828 |
if (target == null |
|
829 |
|| !base.getProtocol().equals(target.getProtocol()) |
|
830 |
|| base.getPort() != target.getPort() |
|
831 |
|| !hostsEqual(base, target) |
|
832 |
|| redirects >= 5) |
|
833 |
{ |
|
834 |
throw new SecurityException("illegal URL redirect"); |
|
835 |
} |
|
836 |
redir = true; |
|
837 |
c = target.openConnection(); |
|
838 |
redirects++; |
|
839 |
} |
|
840 |
} |
|
841 |
} while (redir); |
|
842 |
return in; |
|
843 |
} |
|
844 |
||
845 |
||
846 |
// |
|
847 |
// Same as java.net.URL.hostsEqual |
|
848 |
// |
|
849 |
private static boolean hostsEqual(URL u1, URL u2) { |
|
850 |
final String h1 = u1.getHost(); |
|
851 |
final String h2 = u2.getHost(); |
|
852 |
||
853 |
if (h1 == null) { |
|
854 |
return h2 == null; |
|
855 |
} else if (h2 == null) { |
|
856 |
return false; |
|
857 |
} else if (h1.equalsIgnoreCase(h2)) { |
|
858 |
return true; |
|
859 |
} |
|
860 |
// Have to resolve addresses before comparing, otherwise |
|
861 |
// names like tachyon and tachyon.eng would compare different |
|
862 |
final boolean result[] = {false}; |
|
863 |
||
864 |
java.security.AccessController.doPrivileged( |
|
51 | 865 |
new java.security.PrivilegedAction<Void>() { |
866 |
public Void run() { |
|
2 | 867 |
try { |
868 |
InetAddress a1 = InetAddress.getByName(h1); |
|
869 |
InetAddress a2 = InetAddress.getByName(h2); |
|
870 |
result[0] = a1.equals(a2); |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
871 |
} catch(UnknownHostException | SecurityException e) { |
2 | 872 |
} |
873 |
return null; |
|
874 |
} |
|
875 |
}); |
|
876 |
||
877 |
return result[0]; |
|
878 |
} |
|
879 |
||
880 |
// overridden in HTTPS subclass |
|
881 |
||
882 |
public void connect() throws IOException { |
|
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
883 |
synchronized (this) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
884 |
connecting = true; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
885 |
} |
2 | 886 |
plainConnect(); |
887 |
} |
|
888 |
||
889 |
private boolean checkReuseConnection () { |
|
890 |
if (connected) { |
|
891 |
return true; |
|
892 |
} |
|
893 |
if (reuseClient != null) { |
|
894 |
http = reuseClient; |
|
895 |
http.setReadTimeout(getReadTimeout()); |
|
896 |
http.reuse = false; |
|
897 |
reuseClient = null; |
|
898 |
connected = true; |
|
899 |
return true; |
|
900 |
} |
|
901 |
return false; |
|
902 |
} |
|
903 |
||
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
904 |
private String getHostAndPort(URL url) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
905 |
String host = url.getHost(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
906 |
int port = url.getPort(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
907 |
if (port == -1) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
908 |
String scheme = url.getProtocol(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
909 |
if ("http".equals(scheme)) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
910 |
return host + ":80"; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
911 |
} else { // scheme must be https |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
912 |
return host + ":443"; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
913 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
914 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
915 |
return host + ":" + Integer.toString(port); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
916 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
917 |
|
2 | 918 |
protected void plainConnect() throws IOException { |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
919 |
synchronized (this) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
920 |
if (connected) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
921 |
return; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
922 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
923 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
924 |
SocketPermission p = URLtoSocketPermission(this.url); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
925 |
if (p != null) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
926 |
try { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
927 |
AccessController.doPrivileged( |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
928 |
new PrivilegedExceptionAction<Void>() { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
929 |
public Void run() throws IOException { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
930 |
plainConnect0(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
931 |
return null; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
932 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
933 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
934 |
// }, null, p -- replace line above, when limited doPriv ready |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
935 |
); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
936 |
} catch (PrivilegedActionException e) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
937 |
throw (IOException) e.getException(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
938 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
939 |
} else { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
940 |
// run without additional permission |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
941 |
plainConnect0(); |
2 | 942 |
} |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
943 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
944 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
945 |
/** |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
946 |
* if the caller has a HttpURLPermission for connecting to the |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
947 |
* given URL, then return a SocketPermission which permits |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
948 |
* access to that destination. Return null otherwise. The permission |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
949 |
* is cached in a field (which can only be changed by redirects) |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
950 |
*/ |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
951 |
SocketPermission URLtoSocketPermission(URL url) throws IOException { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
952 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
953 |
if (socketPermission != null) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
954 |
return socketPermission; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
955 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
956 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
957 |
SecurityManager sm = System.getSecurityManager(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
958 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
959 |
if (sm == null) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
960 |
return null; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
961 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
962 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
963 |
// the permission, which we might grant |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
964 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
965 |
SocketPermission newPerm = new SocketPermission( |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
966 |
getHostAndPort(url), "connect" |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
967 |
); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
968 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
969 |
String actions = getRequestMethod()+":" + |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
970 |
getUserSetHeaders().getHeaderNamesInList(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
971 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
972 |
HttpURLPermission p = new HttpURLPermission(url.toString(), actions); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
973 |
try { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
974 |
sm.checkPermission(p); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
975 |
socketPermission = newPerm; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
976 |
return socketPermission; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
977 |
} catch (SecurityException e) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
978 |
// fall thru |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
979 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
980 |
return null; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
981 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
982 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
983 |
protected void plainConnect0() throws IOException { |
2 | 984 |
// try to see if request can be served from local cache |
985 |
if (cacheHandler != null && getUseCaches()) { |
|
986 |
try { |
|
987 |
URI uri = ParseUtil.toURI(url); |
|
988 |
if (uri != null) { |
|
989 |
cachedResponse = cacheHandler.get(uri, getRequestMethod(), requests.getHeaders(EXCLUDE_HEADERS)); |
|
990 |
if ("https".equalsIgnoreCase(uri.getScheme()) |
|
991 |
&& !(cachedResponse instanceof SecureCacheResponse)) { |
|
992 |
cachedResponse = null; |
|
993 |
} |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
994 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
995 |
logger.finest("Cache Request for " + uri + " / " + getRequestMethod()); |
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
996 |
logger.finest("From cache: " + (cachedResponse != null ? cachedResponse.toString() : "null")); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
997 |
} |
2 | 998 |
if (cachedResponse != null) { |
999 |
cachedHeaders = mapToMessageHeader(cachedResponse.getHeaders()); |
|
1000 |
cachedInputStream = cachedResponse.getBody(); |
|
1001 |
} |
|
1002 |
} |
|
1003 |
} catch (IOException ioex) { |
|
1004 |
// ignore and commence normal connection |
|
1005 |
} |
|
1006 |
if (cachedHeaders != null && cachedInputStream != null) { |
|
1007 |
connected = true; |
|
1008 |
return; |
|
1009 |
} else { |
|
1010 |
cachedResponse = null; |
|
1011 |
} |
|
1012 |
} |
|
1013 |
try { |
|
1014 |
/* Try to open connections using the following scheme, |
|
1015 |
* return on the first one that's successful: |
|
1016 |
* 1) if (instProxy != null) |
|
1017 |
* connect to instProxy; raise exception if failed |
|
1018 |
* 2) else use system default ProxySelector |
|
1019 |
* 3) is 2) fails, make direct connection |
|
1020 |
*/ |
|
1021 |
||
1022 |
if (instProxy == null) { // no instance Proxy is set |
|
1023 |
/** |
|
1024 |
* Do we have to use a proxy? |
|
1025 |
*/ |
|
51 | 1026 |
ProxySelector sel = |
2 | 1027 |
java.security.AccessController.doPrivileged( |
51 | 1028 |
new java.security.PrivilegedAction<ProxySelector>() { |
1029 |
public ProxySelector run() { |
|
2 | 1030 |
return ProxySelector.getDefault(); |
1031 |
} |
|
1032 |
}); |
|
1033 |
if (sel != null) { |
|
1034 |
URI uri = sun.net.www.ParseUtil.toURI(url); |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
1035 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
1036 |
logger.finest("ProxySelector Request for " + uri); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
1037 |
} |
2 | 1038 |
Iterator<Proxy> it = sel.select(uri).iterator(); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
1039 |
Proxy p; |
2 | 1040 |
while (it.hasNext()) { |
1041 |
p = it.next(); |
|
1042 |
try { |
|
1043 |
if (!failedOnce) { |
|
1044 |
http = getNewHttpClient(url, p, connectTimeout); |
|
1045 |
http.setReadTimeout(readTimeout); |
|
1046 |
} else { |
|
1047 |
// make sure to construct new connection if first |
|
1048 |
// attempt failed |
|
1049 |
http = getNewHttpClient(url, p, connectTimeout, false); |
|
1050 |
http.setReadTimeout(readTimeout); |
|
1051 |
} |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
1052 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
1053 |
if (p != null) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
1054 |
logger.finest("Proxy used: " + p.toString()); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
1055 |
} |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
1056 |
} |
2 | 1057 |
break; |
1058 |
} catch (IOException ioex) { |
|
1059 |
if (p != Proxy.NO_PROXY) { |
|
1060 |
sel.connectFailed(uri, p.address(), ioex); |
|
1061 |
if (!it.hasNext()) { |
|
1062 |
// fallback to direct connection |
|
1063 |
http = getNewHttpClient(url, null, connectTimeout, false); |
|
1064 |
http.setReadTimeout(readTimeout); |
|
1065 |
break; |
|
1066 |
} |
|
1067 |
} else { |
|
1068 |
throw ioex; |
|
1069 |
} |
|
1070 |
continue; |
|
1071 |
} |
|
1072 |
} |
|
1073 |
} else { |
|
1074 |
// No proxy selector, create http client with no proxy |
|
1075 |
if (!failedOnce) { |
|
1076 |
http = getNewHttpClient(url, null, connectTimeout); |
|
1077 |
http.setReadTimeout(readTimeout); |
|
1078 |
} else { |
|
1079 |
// make sure to construct new connection if first |
|
1080 |
// attempt failed |
|
1081 |
http = getNewHttpClient(url, null, connectTimeout, false); |
|
1082 |
http.setReadTimeout(readTimeout); |
|
1083 |
} |
|
1084 |
} |
|
1085 |
} else { |
|
1086 |
if (!failedOnce) { |
|
1087 |
http = getNewHttpClient(url, instProxy, connectTimeout); |
|
1088 |
http.setReadTimeout(readTimeout); |
|
1089 |
} else { |
|
1090 |
// make sure to construct new connection if first |
|
1091 |
// attempt failed |
|
1092 |
http = getNewHttpClient(url, instProxy, connectTimeout, false); |
|
1093 |
http.setReadTimeout(readTimeout); |
|
1094 |
} |
|
1095 |
} |
|
1096 |
||
1097 |
ps = (PrintStream)http.getOutputStream(); |
|
1098 |
} catch (IOException e) { |
|
1099 |
throw e; |
|
1100 |
} |
|
1101 |
// constructor to HTTP client calls openserver |
|
1102 |
connected = true; |
|
1103 |
} |
|
1104 |
||
1105 |
// subclass HttpsClient will overwrite & return an instance of HttpsClient |
|
1106 |
protected HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout) |
|
1107 |
throws IOException { |
|
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11839
diff
changeset
|
1108 |
return HttpClient.New(url, p, connectTimeout, this); |
2 | 1109 |
} |
1110 |
||
1111 |
// subclass HttpsClient will overwrite & return an instance of HttpsClient |
|
1112 |
protected HttpClient getNewHttpClient(URL url, Proxy p, |
|
1113 |
int connectTimeout, boolean useCache) |
|
1114 |
throws IOException { |
|
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11839
diff
changeset
|
1115 |
return HttpClient.New(url, p, connectTimeout, useCache, this); |
2 | 1116 |
} |
1117 |
||
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1118 |
private void expect100Continue() throws IOException { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1119 |
// Expect: 100-Continue was set, so check the return code for |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1120 |
// Acceptance |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1121 |
int oldTimeout = http.getReadTimeout(); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1122 |
boolean enforceTimeOut = false; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1123 |
boolean timedOut = false; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1124 |
if (oldTimeout <= 0) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1125 |
// 5s read timeout in case the server doesn't understand |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1126 |
// Expect: 100-Continue |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1127 |
http.setReadTimeout(5000); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1128 |
enforceTimeOut = true; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1129 |
} |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1130 |
|
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1131 |
try { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1132 |
http.parseHTTP(responses, pi, this); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1133 |
} catch (SocketTimeoutException se) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1134 |
if (!enforceTimeOut) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1135 |
throw se; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1136 |
} |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1137 |
timedOut = true; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1138 |
http.setIgnoreContinue(true); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1139 |
} |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1140 |
if (!timedOut) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1141 |
// Can't use getResponseCode() yet |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1142 |
String resp = responses.getValue(0); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1143 |
// Parse the response which is of the form: |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1144 |
// HTTP/1.1 417 Expectation Failed |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1145 |
// HTTP/1.1 100 Continue |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1146 |
if (resp != null && resp.startsWith("HTTP/")) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1147 |
String[] sa = resp.split("\\s+"); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1148 |
responseCode = -1; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1149 |
try { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1150 |
// Response code is 2nd token on the line |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1151 |
if (sa.length > 1) |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1152 |
responseCode = Integer.parseInt(sa[1]); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1153 |
} catch (NumberFormatException numberFormatException) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1154 |
} |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1155 |
} |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1156 |
if (responseCode != 100) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1157 |
throw new ProtocolException("Server rejected operation"); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1158 |
} |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1159 |
} |
7022
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
6879
diff
changeset
|
1160 |
|
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
6879
diff
changeset
|
1161 |
http.setReadTimeout(oldTimeout); |
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
6879
diff
changeset
|
1162 |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1163 |
responseCode = -1; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1164 |
responses.reset(); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1165 |
// Proceed |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1166 |
} |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1167 |
|
2 | 1168 |
/* |
1169 |
* Allowable input/output sequences: |
|
18541
a974731db859
7157360: HttpURLConnection: HTTP method DELETE doesn't support output
arieber
parents:
18178
diff
changeset
|
1170 |
* [interpreted as request entity] |
2 | 1171 |
* - get output, [write output,] get input, [read input] |
1172 |
* - get output, [write output] |
|
1173 |
* [interpreted as GET] |
|
1174 |
* - get input, [read input] |
|
1175 |
* Disallowed: |
|
1176 |
* - get input, [read input,] get output, [write output] |
|
1177 |
*/ |
|
1178 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
1179 |
@Override |
2 | 1180 |
public synchronized OutputStream getOutputStream() throws IOException { |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1181 |
connecting = true; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1182 |
SocketPermission p = URLtoSocketPermission(this.url); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1183 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1184 |
if (p != null) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1185 |
try { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1186 |
return AccessController.doPrivileged( |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1187 |
new PrivilegedExceptionAction<OutputStream>() { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1188 |
public OutputStream run() throws IOException { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1189 |
return getOutputStream0(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1190 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1191 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1192 |
// }, null, p -- replace line above, when limited doPriv ready |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1193 |
); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1194 |
} catch (PrivilegedActionException e) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1195 |
throw (IOException) e.getException(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1196 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1197 |
} else { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1198 |
return getOutputStream0(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1199 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1200 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1201 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1202 |
private synchronized OutputStream getOutputStream0() throws IOException { |
2 | 1203 |
try { |
1204 |
if (!doOutput) { |
|
1205 |
throw new ProtocolException("cannot write to a URLConnection" |
|
1206 |
+ " if doOutput=false - call setDoOutput(true)"); |
|
1207 |
} |
|
1208 |
||
1209 |
if (method.equals("GET")) { |
|
1210 |
method = "POST"; // Backward compatibility |
|
1211 |
} |
|
18541
a974731db859
7157360: HttpURLConnection: HTTP method DELETE doesn't support output
arieber
parents:
18178
diff
changeset
|
1212 |
if ("TRACE".equals(method) && "http".equals(url.getProtocol())) { |
a974731db859
7157360: HttpURLConnection: HTTP method DELETE doesn't support output
arieber
parents:
18178
diff
changeset
|
1213 |
throw new ProtocolException("HTTP method TRACE" + |
2 | 1214 |
" doesn't support output"); |
1215 |
} |
|
1216 |
||
1217 |
// if there's already an input stream open, throw an exception |
|
1218 |
if (inputStream != null) { |
|
1219 |
throw new ProtocolException("Cannot write output after reading input."); |
|
1220 |
} |
|
1221 |
||
1222 |
if (!checkReuseConnection()) |
|
1223 |
connect(); |
|
1224 |
||
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1225 |
boolean expectContinue = false; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1226 |
String expects = requests.findValue("Expect"); |
17485
feb8cd6a4c7d
8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
17473
diff
changeset
|
1227 |
if ("100-Continue".equalsIgnoreCase(expects) && streaming()) { |
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1228 |
http.setIgnoreContinue(false); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1229 |
expectContinue = true; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1230 |
} |
2 | 1231 |
|
1232 |
if (streaming() && strOutputStream == null) { |
|
1233 |
writeRequests(); |
|
1234 |
} |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1235 |
|
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1236 |
if (expectContinue) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1237 |
expect100Continue(); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1238 |
} |
2 | 1239 |
ps = (PrintStream)http.getOutputStream(); |
1240 |
if (streaming()) { |
|
1241 |
if (strOutputStream == null) { |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1242 |
if (chunkLength != -1) { /* chunked */ |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1243 |
strOutputStream = new StreamingOutputStream( |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1244 |
new ChunkedOutputStream(ps, chunkLength), -1L); |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1245 |
} else { /* must be fixed content length */ |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1246 |
long length = 0L; |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1247 |
if (fixedContentLengthLong != -1) { |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1248 |
length = fixedContentLengthLong; |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1249 |
} else if (fixedContentLength != -1) { |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1250 |
length = fixedContentLength; |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1251 |
} |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1252 |
strOutputStream = new StreamingOutputStream(ps, length); |
2 | 1253 |
} |
1254 |
} |
|
1255 |
return strOutputStream; |
|
1256 |
} else { |
|
1257 |
if (poster == null) { |
|
1258 |
poster = new PosterOutputStream(); |
|
1259 |
} |
|
1260 |
return poster; |
|
1261 |
} |
|
1262 |
} catch (RuntimeException e) { |
|
1263 |
disconnectInternal(); |
|
1264 |
throw e; |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1265 |
} catch (ProtocolException e) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1266 |
// Save the response code which may have been set while enforcing |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1267 |
// the 100-continue. disconnectInternal() forces it to -1 |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1268 |
int i = responseCode; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1269 |
disconnectInternal(); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1270 |
responseCode = i; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
1271 |
throw e; |
2 | 1272 |
} catch (IOException e) { |
1273 |
disconnectInternal(); |
|
1274 |
throw e; |
|
1275 |
} |
|
1276 |
} |
|
1277 |
||
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11839
diff
changeset
|
1278 |
public boolean streaming () { |
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1279 |
return (fixedContentLength != -1) || (fixedContentLengthLong != -1) || |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
1280 |
(chunkLength != -1); |
2 | 1281 |
} |
1282 |
||
1283 |
/* |
|
1284 |
* get applicable cookies based on the uri and request headers |
|
1285 |
* add them to the existing request headers |
|
1286 |
*/ |
|
1287 |
private void setCookieHeader() throws IOException { |
|
1288 |
if (cookieHandler != null) { |
|
1289 |
// we only want to capture the user defined Cookies once, as |
|
1290 |
// they cannot be changed by user code after we are connected, |
|
1291 |
// only internally. |
|
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1292 |
synchronized (this) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1293 |
if (setUserCookies) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1294 |
int k = requests.getKey("Cookie"); |
6879 | 1295 |
if (k != -1) |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1296 |
userCookies = requests.getValue(k); |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1297 |
k = requests.getKey("Cookie2"); |
6879 | 1298 |
if (k != -1) |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1299 |
userCookies2 = requests.getValue(k); |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1300 |
setUserCookies = false; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1301 |
} |
2 | 1302 |
} |
1303 |
||
1304 |
// remove old Cookie header before setting new one. |
|
1305 |
requests.remove("Cookie"); |
|
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1306 |
requests.remove("Cookie2"); |
2 | 1307 |
|
1308 |
URI uri = ParseUtil.toURI(url); |
|
1309 |
if (uri != null) { |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
1310 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
1311 |
logger.finest("CookieHandler request for " + uri); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
1312 |
} |
51 | 1313 |
Map<String, List<String>> cookies |
1314 |
= cookieHandler.get( |
|
1315 |
uri, requests.getHeaders(EXCLUDE_HEADERS)); |
|
2 | 1316 |
if (!cookies.isEmpty()) { |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
1317 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
1318 |
logger.finest("Cookies retrieved: " + cookies.toString()); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
1319 |
} |
51 | 1320 |
for (Map.Entry<String, List<String>> entry : |
1321 |
cookies.entrySet()) { |
|
1322 |
String key = entry.getKey(); |
|
2 | 1323 |
// ignore all entries that don't have "Cookie" |
1324 |
// or "Cookie2" as keys |
|
1325 |
if (!"Cookie".equalsIgnoreCase(key) && |
|
1326 |
!"Cookie2".equalsIgnoreCase(key)) { |
|
1327 |
continue; |
|
1328 |
} |
|
51 | 1329 |
List<String> l = entry.getValue(); |
2 | 1330 |
if (l != null && !l.isEmpty()) { |
1331 |
StringBuilder cookieValue = new StringBuilder(); |
|
51 | 1332 |
for (String value : l) { |
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
1333 |
cookieValue.append(value).append("; "); |
2 | 1334 |
} |
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
1335 |
// strip off the trailing '; ' |
2 | 1336 |
try { |
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
1337 |
requests.add(key, cookieValue.substring(0, cookieValue.length() - 2)); |
2 | 1338 |
} catch (StringIndexOutOfBoundsException ignored) { |
1339 |
// no-op |
|
1340 |
} |
|
1341 |
} |
|
1342 |
} |
|
1343 |
} |
|
1344 |
} |
|
1345 |
if (userCookies != null) { |
|
1346 |
int k; |
|
1347 |
if ((k = requests.getKey("Cookie")) != -1) |
|
1348 |
requests.set("Cookie", requests.getValue(k) + ";" + userCookies); |
|
1349 |
else |
|
1350 |
requests.set("Cookie", userCookies); |
|
1351 |
} |
|
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1352 |
if (userCookies2 != null) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1353 |
int k; |
6877 | 1354 |
if ((k = requests.getKey("Cookie2")) != -1) |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1355 |
requests.set("Cookie2", requests.getValue(k) + ";" + userCookies2); |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1356 |
else |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1357 |
requests.set("Cookie2", userCookies2); |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
1358 |
} |
2 | 1359 |
|
1360 |
} // end of getting cookies |
|
1361 |
} |
|
1362 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
1363 |
@Override |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1364 |
public synchronized InputStream getInputStream() throws IOException { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1365 |
connecting = true; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1366 |
SocketPermission p = URLtoSocketPermission(this.url); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1367 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1368 |
if (p != null) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1369 |
try { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1370 |
return AccessController.doPrivileged( |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1371 |
new PrivilegedExceptionAction<InputStream>() { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1372 |
public InputStream run() throws IOException { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1373 |
return getInputStream0(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1374 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1375 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1376 |
// }, null, p -- replace line above, when limited doPriv ready |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1377 |
); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1378 |
} catch (PrivilegedActionException e) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1379 |
throw (IOException) e.getException(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1380 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1381 |
} else { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1382 |
return getInputStream0(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1383 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1384 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1385 |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
1386 |
@SuppressWarnings("empty-statement") |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
1387 |
private synchronized InputStream getInputStream0() throws IOException { |
2 | 1388 |
|
1389 |
if (!doInput) { |
|
1390 |
throw new ProtocolException("Cannot read from URLConnection" |
|
1391 |
+ " if doInput=false (call setDoInput(true))"); |
|
1392 |
} |
|
1393 |
||
1394 |
if (rememberedException != null) { |
|
1395 |
if (rememberedException instanceof RuntimeException) |
|
1396 |
throw new RuntimeException(rememberedException); |
|
1397 |
else { |
|
1398 |
throw getChainedException((IOException)rememberedException); |
|
1399 |
} |
|
1400 |
} |
|
1401 |
||
1402 |
if (inputStream != null) { |
|
1403 |
return inputStream; |
|
1404 |
} |
|
1405 |
||
1406 |
if (streaming() ) { |
|
1407 |
if (strOutputStream == null) { |
|
1408 |
getOutputStream(); |
|
1409 |
} |
|
1410 |
/* make sure stream is closed */ |
|
1411 |
strOutputStream.close (); |
|
1412 |
if (!strOutputStream.writtenOK()) { |
|
1413 |
throw new IOException ("Incomplete output stream"); |
|
1414 |
} |
|
1415 |
} |
|
1416 |
||
1417 |
int redirects = 0; |
|
1418 |
int respCode = 0; |
|
1419 |
long cl = -1; |
|
1420 |
AuthenticationInfo serverAuthentication = null; |
|
1421 |
AuthenticationInfo proxyAuthentication = null; |
|
1422 |
AuthenticationHeader srvHdr = null; |
|
1423 |
||
1424 |
/** |
|
1425 |
* Failed Negotiate |
|
1426 |
* |
|
1427 |
* In some cases, the Negotiate auth is supported for the |
|
1428 |
* remote host but the negotiate process still fails (For |
|
1429 |
* example, if the web page is located on a backend server |
|
1430 |
* and delegation is needed but fails). The authentication |
|
1431 |
* process will start again, and we need to detect this |
|
1432 |
* kind of failure and do proper fallback (say, to NTLM). |
|
1433 |
* |
|
1434 |
* In order to achieve this, the inNegotiate flag is set |
|
1435 |
* when the first negotiate challenge is met (and reset |
|
1436 |
* if authentication is finished). If a fresh new negotiate |
|
1437 |
* challenge (no parameter) is found while inNegotiate is |
|
1438 |
* set, we know there's a failed auth attempt recently. |
|
1439 |
* Here we'll ignore the header line so that fallback |
|
1440 |
* can be practiced. |
|
1441 |
* |
|
1442 |
* inNegotiateProxy is for proxy authentication. |
|
1443 |
*/ |
|
1444 |
boolean inNegotiate = false; |
|
1445 |
boolean inNegotiateProxy = false; |
|
1446 |
||
1447 |
// If the user has set either of these headers then do not remove them |
|
1448 |
isUserServerAuth = requests.getKey("Authorization") != -1; |
|
1449 |
isUserProxyAuth = requests.getKey("Proxy-Authorization") != -1; |
|
1450 |
||
1451 |
try { |
|
1452 |
do { |
|
1453 |
if (!checkReuseConnection()) |
|
1454 |
connect(); |
|
1455 |
||
1456 |
if (cachedInputStream != null) { |
|
1457 |
return cachedInputStream; |
|
1458 |
} |
|
1459 |
||
1460 |
// Check if URL should be metered |
|
1461 |
boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, method); |
|
1462 |
||
1463 |
if (meteredInput) { |
|
1464 |
pi = new ProgressSource(url, method); |
|
1465 |
pi.beginTracking(); |
|
1466 |
} |
|
1467 |
||
1468 |
/* REMIND: This exists to fix the HttpsURLConnection subclass. |
|
1469 |
* Hotjava needs to run on JDK1.1FCS. Do proper fix once a |
|
1470 |
* proper solution for SSL can be found. |
|
1471 |
*/ |
|
1472 |
ps = (PrintStream)http.getOutputStream(); |
|
1473 |
||
1474 |
if (!streaming()) { |
|
1475 |
writeRequests(); |
|
1476 |
} |
|
1477 |
http.parseHTTP(responses, pi, this); |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
1478 |
if (logger.isLoggable(PlatformLogger.Level.FINE)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
1479 |
logger.fine(responses.toString()); |
2 | 1480 |
} |
14766 | 1481 |
|
1482 |
boolean b1 = responses.filterNTLMResponses("WWW-Authenticate"); |
|
1483 |
boolean b2 = responses.filterNTLMResponses("Proxy-Authenticate"); |
|
1484 |
if (b1 || b2) { |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
1485 |
if (logger.isLoggable(PlatformLogger.Level.FINE)) { |
14766 | 1486 |
logger.fine(">>>> Headers are filtered"); |
1487 |
logger.fine(responses.toString()); |
|
1488 |
} |
|
1489 |
} |
|
1490 |
||
2 | 1491 |
inputStream = http.getInputStream(); |
1492 |
||
1493 |
respCode = getResponseCode(); |
|
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
3952
diff
changeset
|
1494 |
if (respCode == -1) { |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
3952
diff
changeset
|
1495 |
disconnectInternal(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
3952
diff
changeset
|
1496 |
throw new IOException ("Invalid Http response"); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
3952
diff
changeset
|
1497 |
} |
2 | 1498 |
if (respCode == HTTP_PROXY_AUTH) { |
1499 |
if (streaming()) { |
|
1500 |
disconnectInternal(); |
|
1501 |
throw new HttpRetryException ( |
|
1502 |
RETRY_MSG1, HTTP_PROXY_AUTH); |
|
1503 |
} |
|
1504 |
||
1505 |
// Read comments labeled "Failed Negotiate" for details. |
|
1506 |
boolean dontUseNegotiate = false; |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
1507 |
Iterator<String> iter = responses.multiValueIterator("Proxy-Authenticate"); |
2 | 1508 |
while (iter.hasNext()) { |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
1509 |
String value = iter.next().trim(); |
2 | 1510 |
if (value.equalsIgnoreCase("Negotiate") || |
1511 |
value.equalsIgnoreCase("Kerberos")) { |
|
1512 |
if (!inNegotiateProxy) { |
|
1513 |
inNegotiateProxy = true; |
|
1514 |
} else { |
|
1515 |
dontUseNegotiate = true; |
|
1516 |
doingNTLMp2ndStage = false; |
|
1517 |
proxyAuthentication = null; |
|
1518 |
} |
|
1519 |
break; |
|
1520 |
} |
|
1521 |
} |
|
1522 |
||
1523 |
// changes: add a 3rd parameter to the constructor of |
|
1524 |
// AuthenticationHeader, so that NegotiateAuthentication. |
|
1525 |
// isSupported can be tested. |
|
1526 |
// The other 2 appearances of "new AuthenticationHeader" is |
|
1527 |
// altered in similar ways. |
|
1528 |
||
1529 |
AuthenticationHeader authhdr = new AuthenticationHeader ( |
|
1530 |
"Proxy-Authenticate", responses, |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
1531 |
new HttpCallerInfo(url, http.getProxyHostUsed(), |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
1532 |
http.getProxyPortUsed()), |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
1533 |
dontUseNegotiate |
2 | 1534 |
); |
1535 |
||
1536 |
if (!doingNTLMp2ndStage) { |
|
1537 |
proxyAuthentication = |
|
1538 |
resetProxyAuthentication(proxyAuthentication, authhdr); |
|
1539 |
if (proxyAuthentication != null) { |
|
1540 |
redirects++; |
|
1541 |
disconnectInternal(); |
|
1542 |
continue; |
|
1543 |
} |
|
1544 |
} else { |
|
1545 |
/* in this case, only one header field will be present */ |
|
1546 |
String raw = responses.findValue ("Proxy-Authenticate"); |
|
1547 |
reset (); |
|
1548 |
if (!proxyAuthentication.setHeaders(this, |
|
1549 |
authhdr.headerParser(), raw)) { |
|
1550 |
disconnectInternal(); |
|
1551 |
throw new IOException ("Authentication failure"); |
|
1552 |
} |
|
1553 |
if (serverAuthentication != null && srvHdr != null && |
|
1554 |
!serverAuthentication.setHeaders(this, |
|
1555 |
srvHdr.headerParser(), raw)) { |
|
1556 |
disconnectInternal (); |
|
1557 |
throw new IOException ("Authentication failure"); |
|
1558 |
} |
|
3061 | 1559 |
authObj = null; |
2 | 1560 |
doingNTLMp2ndStage = false; |
1561 |
continue; |
|
1562 |
} |
|
5161 | 1563 |
} else { |
1564 |
inNegotiateProxy = false; |
|
1565 |
doingNTLMp2ndStage = false; |
|
1566 |
if (!isUserProxyAuth) |
|
1567 |
requests.remove("Proxy-Authorization"); |
|
2 | 1568 |
} |
1569 |
||
1570 |
// cache proxy authentication info |
|
1571 |
if (proxyAuthentication != null) { |
|
1572 |
// cache auth info on success, domain header not relevant. |
|
1573 |
proxyAuthentication.addToCache(); |
|
1574 |
} |
|
1575 |
||
1576 |
if (respCode == HTTP_UNAUTHORIZED) { |
|
1577 |
if (streaming()) { |
|
1578 |
disconnectInternal(); |
|
1579 |
throw new HttpRetryException ( |
|
1580 |
RETRY_MSG2, HTTP_UNAUTHORIZED); |
|
1581 |
} |
|
1582 |
||
1583 |
// Read comments labeled "Failed Negotiate" for details. |
|
1584 |
boolean dontUseNegotiate = false; |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
1585 |
Iterator<String> iter = responses.multiValueIterator("WWW-Authenticate"); |
2 | 1586 |
while (iter.hasNext()) { |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
1587 |
String value = iter.next().trim(); |
2 | 1588 |
if (value.equalsIgnoreCase("Negotiate") || |
1589 |
value.equalsIgnoreCase("Kerberos")) { |
|
1590 |
if (!inNegotiate) { |
|
1591 |
inNegotiate = true; |
|
1592 |
} else { |
|
1593 |
dontUseNegotiate = true; |
|
1594 |
doingNTLM2ndStage = false; |
|
1595 |
serverAuthentication = null; |
|
1596 |
} |
|
1597 |
break; |
|
1598 |
} |
|
1599 |
} |
|
1600 |
||
1601 |
srvHdr = new AuthenticationHeader ( |
|
1602 |
"WWW-Authenticate", responses, |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
1603 |
new HttpCallerInfo(url), |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
1604 |
dontUseNegotiate |
2 | 1605 |
); |
1606 |
||
1607 |
String raw = srvHdr.raw(); |
|
1608 |
if (!doingNTLM2ndStage) { |
|
1609 |
if ((serverAuthentication != null)&& |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
1610 |
serverAuthentication.getAuthScheme() != NTLM) { |
2 | 1611 |
if (serverAuthentication.isAuthorizationStale (raw)) { |
1612 |
/* we can retry with the current credentials */ |
|
5161 | 1613 |
disconnectWeb(); |
2 | 1614 |
redirects++; |
1615 |
requests.set(serverAuthentication.getHeaderName(), |
|
1616 |
serverAuthentication.getHeaderValue(url, method)); |
|
1617 |
currentServerCredentials = serverAuthentication; |
|
1618 |
setCookieHeader(); |
|
1619 |
continue; |
|
1620 |
} else { |
|
1621 |
serverAuthentication.removeFromCache(); |
|
1622 |
} |
|
1623 |
} |
|
1624 |
serverAuthentication = getServerAuthentication(srvHdr); |
|
1625 |
currentServerCredentials = serverAuthentication; |
|
1626 |
||
1627 |
if (serverAuthentication != null) { |
|
5161 | 1628 |
disconnectWeb(); |
2 | 1629 |
redirects++; // don't let things loop ad nauseum |
1630 |
setCookieHeader(); |
|
1631 |
continue; |
|
1632 |
} |
|
1633 |
} else { |
|
1634 |
reset (); |
|
1635 |
/* header not used for ntlm */ |
|
1636 |
if (!serverAuthentication.setHeaders(this, null, raw)) { |
|
5161 | 1637 |
disconnectWeb(); |
2 | 1638 |
throw new IOException ("Authentication failure"); |
1639 |
} |
|
1640 |
doingNTLM2ndStage = false; |
|
3061 | 1641 |
authObj = null; |
2 | 1642 |
setCookieHeader(); |
1643 |
continue; |
|
1644 |
} |
|
1645 |
} |
|
1646 |
// cache server authentication info |
|
1647 |
if (serverAuthentication != null) { |
|
1648 |
// cache auth info on success |
|
1649 |
if (!(serverAuthentication instanceof DigestAuthentication) || |
|
1650 |
(domain == null)) { |
|
1651 |
if (serverAuthentication instanceof BasicAuthentication) { |
|
1652 |
// check if the path is shorter than the existing entry |
|
1653 |
String npath = AuthenticationInfo.reducePath (url.getPath()); |
|
1654 |
String opath = serverAuthentication.path; |
|
1655 |
if (!opath.startsWith (npath) || npath.length() >= opath.length()) { |
|
1656 |
/* npath is longer, there must be a common root */ |
|
1657 |
npath = BasicAuthentication.getRootPath (opath, npath); |
|
1658 |
} |
|
1659 |
// remove the entry and create a new one |
|
1660 |
BasicAuthentication a = |
|
1661 |
(BasicAuthentication) serverAuthentication.clone(); |
|
1662 |
serverAuthentication.removeFromCache(); |
|
1663 |
a.path = npath; |
|
1664 |
serverAuthentication = a; |
|
1665 |
} |
|
1666 |
serverAuthentication.addToCache(); |
|
1667 |
} else { |
|
1668 |
// what we cache is based on the domain list in the request |
|
1669 |
DigestAuthentication srv = (DigestAuthentication) |
|
1670 |
serverAuthentication; |
|
1671 |
StringTokenizer tok = new StringTokenizer (domain," "); |
|
1672 |
String realm = srv.realm; |
|
1673 |
PasswordAuthentication pw = srv.pw; |
|
1674 |
digestparams = srv.params; |
|
1675 |
while (tok.hasMoreTokens()) { |
|
1676 |
String path = tok.nextToken(); |
|
1677 |
try { |
|
1678 |
/* path could be an abs_path or a complete URI */ |
|
1679 |
URL u = new URL (url, path); |
|
1680 |
DigestAuthentication d = new DigestAuthentication ( |
|
1681 |
false, u, realm, "Digest", pw, digestparams); |
|
1682 |
d.addToCache (); |
|
1683 |
} catch (Exception e) {} |
|
1684 |
} |
|
1685 |
} |
|
1686 |
} |
|
1687 |
||
1688 |
// some flags should be reset to its initialized form so that |
|
1689 |
// even after a redirect the necessary checks can still be |
|
1690 |
// preformed. |
|
1691 |
inNegotiate = false; |
|
1692 |
inNegotiateProxy = false; |
|
1693 |
||
1694 |
//serverAuthentication = null; |
|
1695 |
doingNTLMp2ndStage = false; |
|
1696 |
doingNTLM2ndStage = false; |
|
1697 |
if (!isUserServerAuth) |
|
1698 |
requests.remove("Authorization"); |
|
1699 |
if (!isUserProxyAuth) |
|
1700 |
requests.remove("Proxy-Authorization"); |
|
1701 |
||
1702 |
if (respCode == HTTP_OK) { |
|
1703 |
checkResponseCredentials (false); |
|
1704 |
} else { |
|
1705 |
needToCheck = false; |
|
1706 |
} |
|
1707 |
||
1708 |
// a flag need to clean |
|
1709 |
needToCheck = true; |
|
1710 |
||
1711 |
if (followRedirect()) { |
|
1712 |
/* if we should follow a redirect, then the followRedirects() |
|
1713 |
* method will disconnect() and re-connect us to the new |
|
1714 |
* location |
|
1715 |
*/ |
|
1716 |
redirects++; |
|
1717 |
||
1718 |
// redirecting HTTP response may have set cookie, so |
|
1719 |
// need to re-generate request header |
|
1720 |
setCookieHeader(); |
|
1721 |
||
1722 |
continue; |
|
1723 |
} |
|
1724 |
||
1725 |
try { |
|
1726 |
cl = Long.parseLong(responses.findValue("content-length")); |
|
1727 |
} catch (Exception exc) { }; |
|
1728 |
||
1729 |
if (method.equals("HEAD") || cl == 0 || |
|
1730 |
respCode == HTTP_NOT_MODIFIED || |
|
1731 |
respCode == HTTP_NO_CONTENT) { |
|
1732 |
||
1733 |
if (pi != null) { |
|
1734 |
pi.finishTracking(); |
|
1735 |
pi = null; |
|
1736 |
} |
|
1737 |
http.finished(); |
|
1738 |
http = null; |
|
1739 |
inputStream = new EmptyInputStream(); |
|
1740 |
connected = false; |
|
1741 |
} |
|
1742 |
||
1743 |
if (respCode == 200 || respCode == 203 || respCode == 206 || |
|
1744 |
respCode == 300 || respCode == 301 || respCode == 410) { |
|
11839
53065c5f535d
7133367: ResponseCache.put should not be called when setUseCaches(false)
chegar
parents:
11528
diff
changeset
|
1745 |
if (cacheHandler != null && getUseCaches()) { |
2 | 1746 |
// give cache a chance to save response in cache |
1747 |
URI uri = ParseUtil.toURI(url); |
|
1748 |
if (uri != null) { |
|
1749 |
URLConnection uconn = this; |
|
1750 |
if ("https".equalsIgnoreCase(uri.getScheme())) { |
|
1751 |
try { |
|
1752 |
// use reflection to get to the public |
|
1753 |
// HttpsURLConnection instance saved in |
|
1754 |
// DelegateHttpsURLConnection |
|
1755 |
uconn = (URLConnection)this.getClass().getField("httpsURLConnection").get(this); |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
1756 |
} catch (IllegalAccessException | |
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
1757 |
NoSuchFieldException e) { |
2 | 1758 |
// ignored; use 'this' |
1759 |
} |
|
1760 |
} |
|
1761 |
CacheRequest cacheRequest = |
|
1762 |
cacheHandler.put(uri, uconn); |
|
1763 |
if (cacheRequest != null && http != null) { |
|
1764 |
http.setCacheRequest(cacheRequest); |
|
1765 |
inputStream = new HttpInputStream(inputStream, cacheRequest); |
|
1766 |
} |
|
1767 |
} |
|
1768 |
} |
|
1769 |
} |
|
1770 |
||
1771 |
if (!(inputStream instanceof HttpInputStream)) { |
|
1772 |
inputStream = new HttpInputStream(inputStream); |
|
1773 |
} |
|
1774 |
||
1775 |
if (respCode >= 400) { |
|
1776 |
if (respCode == 404 || respCode == 410) { |
|
1777 |
throw new FileNotFoundException(url.toString()); |
|
1778 |
} else { |
|
1779 |
throw new java.io.IOException("Server returned HTTP" + |
|
1780 |
" response code: " + respCode + " for URL: " + |
|
1781 |
url.toString()); |
|
1782 |
} |
|
1783 |
} |
|
1784 |
poster = null; |
|
1785 |
strOutputStream = null; |
|
1786 |
return inputStream; |
|
1787 |
} while (redirects < maxRedirects); |
|
1788 |
||
1789 |
throw new ProtocolException("Server redirected too many " + |
|
1790 |
" times ("+ redirects + ")"); |
|
1791 |
} catch (RuntimeException e) { |
|
1792 |
disconnectInternal(); |
|
1793 |
rememberedException = e; |
|
1794 |
throw e; |
|
1795 |
} catch (IOException e) { |
|
1796 |
rememberedException = e; |
|
1797 |
||
1798 |
// buffer the error stream if bytes < 4k |
|
1799 |
// and it can be buffered within 1 second |
|
1800 |
String te = responses.findValue("Transfer-Encoding"); |
|
1801 |
if (http != null && http.isKeepingAlive() && enableESBuffer && |
|
1802 |
(cl > 0 || (te != null && te.equalsIgnoreCase("chunked")))) { |
|
1803 |
errorStream = ErrorStream.getErrorStream(inputStream, cl, http); |
|
1804 |
} |
|
1805 |
throw e; |
|
1806 |
} finally { |
|
5199
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
1807 |
if (proxyAuthKey != null) { |
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
1808 |
AuthenticationInfo.endAuthRequest(proxyAuthKey); |
2 | 1809 |
} |
5199
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
1810 |
if (serverAuthKey != null) { |
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
1811 |
AuthenticationInfo.endAuthRequest(serverAuthKey); |
2 | 1812 |
} |
1813 |
} |
|
1814 |
} |
|
1815 |
||
1816 |
/* |
|
1817 |
* Creates a chained exception that has the same type as |
|
1818 |
* original exception and with the same message. Right now, |
|
1819 |
* there is no convenient APIs for doing so. |
|
1820 |
*/ |
|
51 | 1821 |
private IOException getChainedException(final IOException rememberedException) { |
2 | 1822 |
try { |
51 | 1823 |
final Object[] args = { rememberedException.getMessage() }; |
1824 |
IOException chainedException = |
|
1825 |
java.security.AccessController.doPrivileged( |
|
1826 |
new java.security.PrivilegedExceptionAction<IOException>() { |
|
1827 |
public IOException run() throws Exception { |
|
1828 |
return (IOException) |
|
1829 |
rememberedException.getClass() |
|
10701
2c8dde454979
7090499: missing rawtypes warnings in anonymous inner class
chegar
parents:
10596
diff
changeset
|
1830 |
.getConstructor(new Class<?>[] { String.class }) |
51 | 1831 |
.newInstance(args); |
2 | 1832 |
} |
1833 |
}); |
|
51 | 1834 |
chainedException.initCause(rememberedException); |
2 | 1835 |
return chainedException; |
1836 |
} catch (Exception ignored) { |
|
1837 |
return rememberedException; |
|
1838 |
} |
|
1839 |
} |
|
1840 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
1841 |
@Override |
2 | 1842 |
public InputStream getErrorStream() { |
1843 |
if (connected && responseCode >= 400) { |
|
1844 |
// Client Error 4xx and Server Error 5xx |
|
1845 |
if (errorStream != null) { |
|
1846 |
return errorStream; |
|
1847 |
} else if (inputStream != null) { |
|
1848 |
return inputStream; |
|
1849 |
} |
|
1850 |
} |
|
1851 |
return null; |
|
1852 |
} |
|
1853 |
||
1854 |
/** |
|
1855 |
* set or reset proxy authentication info in request headers |
|
1856 |
* after receiving a 407 error. In the case of NTLM however, |
|
1857 |
* receiving a 407 is normal and we just skip the stale check |
|
1858 |
* because ntlm does not support this feature. |
|
1859 |
*/ |
|
1860 |
private AuthenticationInfo |
|
4150
74dfe4a968c6
6890349: Fix #6870935 in jdk7/pit/b74 caused HttpClinet's check for "proxy capture" attack by-passed.
michaelm
parents:
4047
diff
changeset
|
1861 |
resetProxyAuthentication(AuthenticationInfo proxyAuthentication, AuthenticationHeader auth) throws IOException { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
1862 |
if ((proxyAuthentication != null )&& |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
1863 |
proxyAuthentication.getAuthScheme() != NTLM) { |
2 | 1864 |
String raw = auth.raw(); |
1865 |
if (proxyAuthentication.isAuthorizationStale (raw)) { |
|
1866 |
/* we can retry with the current credentials */ |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1867 |
String value; |
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
1868 |
if (proxyAuthentication instanceof DigestAuthentication) { |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
1869 |
DigestAuthentication digestProxy = (DigestAuthentication) |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
1870 |
proxyAuthentication; |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
1871 |
if (tunnelState() == TunnelState.SETUP) { |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
1872 |
value = digestProxy.getHeaderValue(connectRequestURI(url), HTTP_CONNECT); |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
1873 |
} else { |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
1874 |
value = digestProxy.getHeaderValue(getRequestURI(), method); |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
1875 |
} |
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1876 |
} else { |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1877 |
value = proxyAuthentication.getHeaderValue(url, method); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1878 |
} |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1879 |
requests.set(proxyAuthentication.getHeaderName(), value); |
2 | 1880 |
currentProxyCredentials = proxyAuthentication; |
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1881 |
return proxyAuthentication; |
2 | 1882 |
} else { |
1883 |
proxyAuthentication.removeFromCache(); |
|
1884 |
} |
|
1885 |
} |
|
1886 |
proxyAuthentication = getHttpProxyAuthentication(auth); |
|
1887 |
currentProxyCredentials = proxyAuthentication; |
|
1888 |
return proxyAuthentication; |
|
1889 |
} |
|
1890 |
||
1891 |
/** |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1892 |
* Returns the tunnel state. |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1893 |
* |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1894 |
* @return the state |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1895 |
*/ |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1896 |
TunnelState tunnelState() { |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1897 |
return tunnelState; |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1898 |
} |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1899 |
|
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1900 |
/** |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1901 |
* Set the tunneling status. |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1902 |
* |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1903 |
* @param the state |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1904 |
*/ |
14008
b572c1f3a7ad
7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents:
14007
diff
changeset
|
1905 |
public void setTunnelState(TunnelState tunnelState) { |
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1906 |
this.tunnelState = tunnelState; |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1907 |
} |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1908 |
|
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1909 |
/** |
2 | 1910 |
* establish a tunnel through proxy server |
1911 |
*/ |
|
1912 |
public synchronized void doTunneling() throws IOException { |
|
1913 |
int retryTunnel = 0; |
|
1914 |
String statusLine = ""; |
|
1915 |
int respCode = 0; |
|
1916 |
AuthenticationInfo proxyAuthentication = null; |
|
1917 |
String proxyHost = null; |
|
1918 |
int proxyPort = -1; |
|
1919 |
||
1920 |
// save current requests so that they can be restored after tunnel is setup. |
|
1921 |
MessageHeader savedRequests = requests; |
|
1922 |
requests = new MessageHeader(); |
|
1923 |
||
1924 |
// Read comments labeled "Failed Negotiate" for details. |
|
1925 |
boolean inNegotiateProxy = false; |
|
1926 |
||
1927 |
try { |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1928 |
/* Actively setting up a tunnel */ |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1929 |
setTunnelState(TunnelState.SETUP); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
1930 |
|
2 | 1931 |
do { |
1932 |
if (!checkReuseConnection()) { |
|
1933 |
proxiedConnect(url, proxyHost, proxyPort, false); |
|
1934 |
} |
|
1935 |
// send the "CONNECT" request to establish a tunnel |
|
1936 |
// through proxy server |
|
1937 |
sendCONNECTRequest(); |
|
1938 |
responses.reset(); |
|
1939 |
||
1940 |
// There is no need to track progress in HTTP Tunneling, |
|
1941 |
// so ProgressSource is null. |
|
1942 |
http.parseHTTP(responses, null, this); |
|
1943 |
||
478
5adf3e3f1ff3
6659779: HttpURLConnections logger should log tunnel requests
chegar
parents:
94
diff
changeset
|
1944 |
/* Log the response to the CONNECT */ |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
1945 |
if (logger.isLoggable(PlatformLogger.Level.FINE)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
1946 |
logger.fine(responses.toString()); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
1947 |
} |
478
5adf3e3f1ff3
6659779: HttpURLConnections logger should log tunnel requests
chegar
parents:
94
diff
changeset
|
1948 |
|
14766 | 1949 |
if (responses.filterNTLMResponses("Proxy-Authenticate")) { |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
1950 |
if (logger.isLoggable(PlatformLogger.Level.FINE)) { |
14766 | 1951 |
logger.fine(">>>> Headers are filtered"); |
1952 |
logger.fine(responses.toString()); |
|
1953 |
} |
|
1954 |
} |
|
1955 |
||
2 | 1956 |
statusLine = responses.getValue(0); |
1957 |
StringTokenizer st = new StringTokenizer(statusLine); |
|
1958 |
st.nextToken(); |
|
1959 |
respCode = Integer.parseInt(st.nextToken().trim()); |
|
1960 |
if (respCode == HTTP_PROXY_AUTH) { |
|
1961 |
// Read comments labeled "Failed Negotiate" for details. |
|
1962 |
boolean dontUseNegotiate = false; |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
1963 |
Iterator<String> iter = responses.multiValueIterator("Proxy-Authenticate"); |
2 | 1964 |
while (iter.hasNext()) { |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
1965 |
String value = iter.next().trim(); |
2 | 1966 |
if (value.equalsIgnoreCase("Negotiate") || |
1967 |
value.equalsIgnoreCase("Kerberos")) { |
|
1968 |
if (!inNegotiateProxy) { |
|
1969 |
inNegotiateProxy = true; |
|
1970 |
} else { |
|
1971 |
dontUseNegotiate = true; |
|
1972 |
doingNTLMp2ndStage = false; |
|
1973 |
proxyAuthentication = null; |
|
1974 |
} |
|
1975 |
break; |
|
1976 |
} |
|
1977 |
} |
|
1978 |
||
1979 |
AuthenticationHeader authhdr = new AuthenticationHeader ( |
|
1980 |
"Proxy-Authenticate", responses, |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
1981 |
new HttpCallerInfo(url, http.getProxyHostUsed(), |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
1982 |
http.getProxyPortUsed()), |
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
1983 |
dontUseNegotiate |
2 | 1984 |
); |
1985 |
if (!doingNTLMp2ndStage) { |
|
1986 |
proxyAuthentication = |
|
1987 |
resetProxyAuthentication(proxyAuthentication, authhdr); |
|
1988 |
if (proxyAuthentication != null) { |
|
1989 |
proxyHost = http.getProxyHostUsed(); |
|
1990 |
proxyPort = http.getProxyPortUsed(); |
|
1991 |
disconnectInternal(); |
|
1992 |
retryTunnel++; |
|
1993 |
continue; |
|
1994 |
} |
|
1995 |
} else { |
|
1996 |
String raw = responses.findValue ("Proxy-Authenticate"); |
|
1997 |
reset (); |
|
1998 |
if (!proxyAuthentication.setHeaders(this, |
|
1999 |
authhdr.headerParser(), raw)) { |
|
2000 |
disconnectInternal(); |
|
2001 |
throw new IOException ("Authentication failure"); |
|
2002 |
} |
|
3061 | 2003 |
authObj = null; |
2 | 2004 |
doingNTLMp2ndStage = false; |
2005 |
continue; |
|
2006 |
} |
|
2007 |
} |
|
2008 |
// cache proxy authentication info |
|
2009 |
if (proxyAuthentication != null) { |
|
2010 |
// cache auth info on success, domain header not relevant. |
|
2011 |
proxyAuthentication.addToCache(); |
|
2012 |
} |
|
2013 |
||
2014 |
if (respCode == HTTP_OK) { |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2015 |
setTunnelState(TunnelState.TUNNELING); |
2 | 2016 |
break; |
2017 |
} |
|
2018 |
// we don't know how to deal with other response code |
|
2019 |
// so disconnect and report error |
|
2020 |
disconnectInternal(); |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2021 |
setTunnelState(TunnelState.NONE); |
2 | 2022 |
break; |
2023 |
} while (retryTunnel < maxRedirects); |
|
2024 |
||
2025 |
if (retryTunnel >= maxRedirects || (respCode != HTTP_OK)) { |
|
2026 |
throw new IOException("Unable to tunnel through proxy."+ |
|
2027 |
" Proxy returns \"" + |
|
2028 |
statusLine + "\""); |
|
2029 |
} |
|
2030 |
} finally { |
|
5199
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
2031 |
if (proxyAuthKey != null) { |
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
2032 |
AuthenticationInfo.endAuthRequest(proxyAuthKey); |
2 | 2033 |
} |
2034 |
} |
|
2035 |
||
2036 |
// restore original request headers |
|
2037 |
requests = savedRequests; |
|
2038 |
||
2039 |
// reset responses |
|
2040 |
responses.reset(); |
|
2041 |
} |
|
2042 |
||
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2043 |
static String connectRequestURI(URL url) { |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2044 |
String host = url.getHost(); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2045 |
int port = url.getPort(); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2046 |
port = port != -1 ? port : url.getDefaultPort(); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2047 |
|
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2048 |
return host + ":" + port; |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2049 |
} |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2050 |
|
2 | 2051 |
/** |
2052 |
* send a CONNECT request for establishing a tunnel to proxy server |
|
2053 |
*/ |
|
2054 |
private void sendCONNECTRequest() throws IOException { |
|
2055 |
int port = url.getPort(); |
|
2056 |
||
10136
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9035
diff
changeset
|
2057 |
requests.set(0, HTTP_CONNECT + " " + connectRequestURI(url) |
2 | 2058 |
+ " " + httpVersion, null); |
2059 |
requests.setIfNotSet("User-Agent", userAgent); |
|
2060 |
||
2061 |
String host = url.getHost(); |
|
2062 |
if (port != -1 && port != url.getDefaultPort()) { |
|
2063 |
host += ":" + String.valueOf(port); |
|
2064 |
} |
|
2065 |
requests.setIfNotSet("Host", host); |
|
2066 |
||
2067 |
// Not really necessary for a tunnel, but can't hurt |
|
2068 |
requests.setIfNotSet("Accept", acceptString); |
|
2069 |
||
6292
9da761dff0c7
6973030: NTLM proxy authentication fails with https
chegar
parents:
5979
diff
changeset
|
2070 |
if (http.getHttpKeepAliveSet()) { |
9da761dff0c7
6973030: NTLM proxy authentication fails with https
chegar
parents:
5979
diff
changeset
|
2071 |
requests.setIfNotSet("Proxy-Connection", "keep-alive"); |
9da761dff0c7
6973030: NTLM proxy authentication fails with https
chegar
parents:
5979
diff
changeset
|
2072 |
} |
9da761dff0c7
6973030: NTLM proxy authentication fails with https
chegar
parents:
5979
diff
changeset
|
2073 |
|
2 | 2074 |
setPreemptiveProxyAuthentication(requests); |
478
5adf3e3f1ff3
6659779: HttpURLConnections logger should log tunnel requests
chegar
parents:
94
diff
changeset
|
2075 |
|
5adf3e3f1ff3
6659779: HttpURLConnections logger should log tunnel requests
chegar
parents:
94
diff
changeset
|
2076 |
/* Log the CONNECT request */ |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
2077 |
if (logger.isLoggable(PlatformLogger.Level.FINE)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
2078 |
logger.fine(requests.toString()); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2079 |
} |
478
5adf3e3f1ff3
6659779: HttpURLConnections logger should log tunnel requests
chegar
parents:
94
diff
changeset
|
2080 |
|
2 | 2081 |
http.writeRequests(requests, null); |
2082 |
} |
|
2083 |
||
2084 |
/** |
|
2085 |
* Sets pre-emptive proxy authentication in header |
|
2086 |
*/ |
|
4150
74dfe4a968c6
6890349: Fix #6870935 in jdk7/pit/b74 caused HttpClinet's check for "proxy capture" attack by-passed.
michaelm
parents:
4047
diff
changeset
|
2087 |
private void setPreemptiveProxyAuthentication(MessageHeader requests) throws IOException { |
2 | 2088 |
AuthenticationInfo pauth |
2089 |
= AuthenticationInfo.getProxyAuth(http.getProxyHostUsed(), |
|
2090 |
http.getProxyPortUsed()); |
|
2091 |
if (pauth != null && pauth.supportsPreemptiveAuthorization()) { |
|
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2092 |
String value; |
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2093 |
if (pauth instanceof DigestAuthentication) { |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2094 |
DigestAuthentication digestProxy = (DigestAuthentication) pauth; |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2095 |
if (tunnelState() == TunnelState.SETUP) { |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2096 |
value = digestProxy |
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2097 |
.getHeaderValue(connectRequestURI(url), HTTP_CONNECT); |
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2098 |
} else { |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2099 |
value = digestProxy.getHeaderValue(getRequestURI(), method); |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2100 |
} |
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2101 |
} else { |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2102 |
value = pauth.getHeaderValue(url, method); |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2103 |
} |
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2104 |
|
2 | 2105 |
// Sets "Proxy-authorization" |
479
c6ddcfc7ff4d
6687282: URLConnection for HTTPS connection through Proxy w/ Digest Authentication gives 400 Bad Request
chegar
parents:
478
diff
changeset
|
2106 |
requests.set(pauth.getHeaderName(), value); |
2 | 2107 |
currentProxyCredentials = pauth; |
2108 |
} |
|
2109 |
} |
|
2110 |
||
2111 |
/** |
|
2112 |
* Gets the authentication for an HTTP proxy, and applies it to |
|
2113 |
* the connection. |
|
2114 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
2115 |
@SuppressWarnings("fallthrough") |
2 | 2116 |
private AuthenticationInfo getHttpProxyAuthentication (AuthenticationHeader authhdr) { |
2117 |
/* get authorization from authenticator */ |
|
2118 |
AuthenticationInfo ret = null; |
|
2119 |
String raw = authhdr.raw(); |
|
2120 |
String host = http.getProxyHostUsed(); |
|
2121 |
int port = http.getProxyPortUsed(); |
|
2122 |
if (host != null && authhdr.isPresent()) { |
|
2123 |
HeaderParser p = authhdr.headerParser(); |
|
2124 |
String realm = p.findValue("realm"); |
|
2125 |
String scheme = authhdr.scheme(); |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2126 |
AuthScheme authScheme = UNKNOWN; |
2 | 2127 |
if ("basic".equalsIgnoreCase(scheme)) { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2128 |
authScheme = BASIC; |
2 | 2129 |
} else if ("digest".equalsIgnoreCase(scheme)) { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2130 |
authScheme = DIGEST; |
2 | 2131 |
} else if ("ntlm".equalsIgnoreCase(scheme)) { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2132 |
authScheme = NTLM; |
2 | 2133 |
doingNTLMp2ndStage = true; |
2134 |
} else if ("Kerberos".equalsIgnoreCase(scheme)) { |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2135 |
authScheme = KERBEROS; |
2 | 2136 |
doingNTLMp2ndStage = true; |
2137 |
} else if ("Negotiate".equalsIgnoreCase(scheme)) { |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2138 |
authScheme = NEGOTIATE; |
2 | 2139 |
doingNTLMp2ndStage = true; |
2140 |
} |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2141 |
|
2 | 2142 |
if (realm == null) |
2143 |
realm = ""; |
|
5199
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
2144 |
proxyAuthKey = AuthenticationInfo.getProxyAuthKey(host, port, realm, authScheme); |
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
2145 |
ret = AuthenticationInfo.getProxyAuth(proxyAuthKey); |
2 | 2146 |
if (ret == null) { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2147 |
switch (authScheme) { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2148 |
case BASIC: |
2 | 2149 |
InetAddress addr = null; |
2150 |
try { |
|
2151 |
final String finalHost = host; |
|
51 | 2152 |
addr = java.security.AccessController.doPrivileged( |
2153 |
new java.security.PrivilegedExceptionAction<InetAddress>() { |
|
2154 |
public InetAddress run() |
|
2 | 2155 |
throws java.net.UnknownHostException { |
2156 |
return InetAddress.getByName(finalHost); |
|
2157 |
} |
|
2158 |
}); |
|
2159 |
} catch (java.security.PrivilegedActionException ignored) { |
|
2160 |
// User will have an unknown host. |
|
2161 |
} |
|
2162 |
PasswordAuthentication a = |
|
2163 |
privilegedRequestPasswordAuthentication( |
|
2164 |
host, addr, port, "http", |
|
2165 |
realm, scheme, url, RequestorType.PROXY); |
|
2166 |
if (a != null) { |
|
2167 |
ret = new BasicAuthentication(true, host, port, realm, a); |
|
2168 |
} |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2169 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2170 |
case DIGEST: |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2171 |
a = privilegedRequestPasswordAuthentication( |
2 | 2172 |
host, null, port, url.getProtocol(), |
2173 |
realm, scheme, url, RequestorType.PROXY); |
|
2174 |
if (a != null) { |
|
2175 |
DigestAuthentication.Parameters params = |
|
2176 |
new DigestAuthentication.Parameters(); |
|
2177 |
ret = new DigestAuthentication(true, host, port, realm, |
|
2178 |
scheme, a, params); |
|
2179 |
} |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2180 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2181 |
case NTLM: |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
2182 |
if (NTLMAuthenticationProxy.supported) { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2183 |
/* tryTransparentNTLMProxy will always be true the first |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2184 |
* time around, but verify that the platform supports it |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2185 |
* otherwise don't try. */ |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2186 |
if (tryTransparentNTLMProxy) { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2187 |
tryTransparentNTLMProxy = |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
2188 |
NTLMAuthenticationProxy.supportsTransparentAuth; |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2189 |
} |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2190 |
a = null; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2191 |
if (tryTransparentNTLMProxy) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
2192 |
logger.finest("Trying Transparent NTLM authentication"); |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2193 |
} else { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2194 |
a = privilegedRequestPasswordAuthentication( |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2195 |
host, null, port, url.getProtocol(), |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2196 |
"", scheme, url, RequestorType.PROXY); |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2197 |
} |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2198 |
/* If we are not trying transparent authentication then |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2199 |
* we need to have a PasswordAuthentication instance. For |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2200 |
* transparent authentication (Windows only) the username |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2201 |
* and password will be picked up from the current logged |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2202 |
* on users credentials. |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2203 |
*/ |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2204 |
if (tryTransparentNTLMProxy || |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2205 |
(!tryTransparentNTLMProxy && a != null)) { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2206 |
ret = NTLMAuthenticationProxy.proxy.create(true, host, port, a); |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2207 |
} |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2208 |
|
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2209 |
/* set to false so that we do not try again */ |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2210 |
tryTransparentNTLMProxy = false; |
2 | 2211 |
} |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2212 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2213 |
case NEGOTIATE: |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
2214 |
ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Negotiate")); |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2215 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2216 |
case KERBEROS: |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
2217 |
ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Kerberos")); |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2218 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2219 |
case UNKNOWN: |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
2220 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
16839
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
16007
diff
changeset
|
2221 |
logger.finest("Unknown/Unsupported authentication scheme: " + scheme); |
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
16007
diff
changeset
|
2222 |
} |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
2223 |
/*fall through*/ |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2224 |
default: |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2225 |
throw new AssertionError("should not reach here"); |
2 | 2226 |
} |
2227 |
} |
|
2228 |
// For backwards compatibility, we also try defaultAuth |
|
2229 |
// REMIND: Get rid of this for JDK2.0. |
|
2230 |
||
2231 |
if (ret == null && defaultAuth != null |
|
2232 |
&& defaultAuth.schemeSupported(scheme)) { |
|
2233 |
try { |
|
2234 |
URL u = new URL("http", host, port, "/"); |
|
2235 |
String a = defaultAuth.authString(u, scheme, realm); |
|
2236 |
if (a != null) { |
|
2237 |
ret = new BasicAuthentication (true, host, port, realm, a); |
|
2238 |
// not in cache by default - cache on success |
|
2239 |
} |
|
2240 |
} catch (java.net.MalformedURLException ignored) { |
|
2241 |
} |
|
2242 |
} |
|
2243 |
if (ret != null) { |
|
2244 |
if (!ret.setHeaders(this, p, raw)) { |
|
2245 |
ret = null; |
|
2246 |
} |
|
2247 |
} |
|
2248 |
} |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
2249 |
if (logger.isLoggable(PlatformLogger.Level.FINER)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
2250 |
logger.finer("Proxy Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null")); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2251 |
} |
2 | 2252 |
return ret; |
2253 |
} |
|
2254 |
||
2255 |
/** |
|
2256 |
* Gets the authentication for an HTTP server, and applies it to |
|
2257 |
* the connection. |
|
2258 |
* @param authHdr the AuthenticationHeader which tells what auth scheme is |
|
2259 |
* prefered. |
|
2260 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
2261 |
@SuppressWarnings("fallthrough") |
2 | 2262 |
private AuthenticationInfo getServerAuthentication (AuthenticationHeader authhdr) { |
2263 |
/* get authorization from authenticator */ |
|
2264 |
AuthenticationInfo ret = null; |
|
2265 |
String raw = authhdr.raw(); |
|
2266 |
/* When we get an NTLM auth from cache, don't set any special headers */ |
|
2267 |
if (authhdr.isPresent()) { |
|
2268 |
HeaderParser p = authhdr.headerParser(); |
|
2269 |
String realm = p.findValue("realm"); |
|
2270 |
String scheme = authhdr.scheme(); |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2271 |
AuthScheme authScheme = UNKNOWN; |
2 | 2272 |
if ("basic".equalsIgnoreCase(scheme)) { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2273 |
authScheme = BASIC; |
2 | 2274 |
} else if ("digest".equalsIgnoreCase(scheme)) { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2275 |
authScheme = DIGEST; |
2 | 2276 |
} else if ("ntlm".equalsIgnoreCase(scheme)) { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2277 |
authScheme = NTLM; |
2 | 2278 |
doingNTLM2ndStage = true; |
2279 |
} else if ("Kerberos".equalsIgnoreCase(scheme)) { |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2280 |
authScheme = KERBEROS; |
2 | 2281 |
doingNTLM2ndStage = true; |
2282 |
} else if ("Negotiate".equalsIgnoreCase(scheme)) { |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2283 |
authScheme = NEGOTIATE; |
2 | 2284 |
doingNTLM2ndStage = true; |
2285 |
} |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2286 |
|
2 | 2287 |
domain = p.findValue ("domain"); |
2288 |
if (realm == null) |
|
2289 |
realm = ""; |
|
5199
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
2290 |
serverAuthKey = AuthenticationInfo.getServerAuthKey(url, realm, authScheme); |
98b1778f0fd1
6648001: Cancelling HTTP authentication causes subsequent deadlocks
chegar
parents:
5161
diff
changeset
|
2291 |
ret = AuthenticationInfo.getServerAuth(serverAuthKey); |
2 | 2292 |
InetAddress addr = null; |
2293 |
if (ret == null) { |
|
2294 |
try { |
|
2295 |
addr = InetAddress.getByName(url.getHost()); |
|
2296 |
} catch (java.net.UnknownHostException ignored) { |
|
2297 |
// User will have addr = null |
|
2298 |
} |
|
2299 |
} |
|
2300 |
// replacing -1 with default port for a protocol |
|
2301 |
int port = url.getPort(); |
|
2302 |
if (port == -1) { |
|
2303 |
port = url.getDefaultPort(); |
|
2304 |
} |
|
2305 |
if (ret == null) { |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2306 |
switch(authScheme) { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2307 |
case KERBEROS: |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
2308 |
ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Kerberos")); |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2309 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2310 |
case NEGOTIATE: |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2928
diff
changeset
|
2311 |
ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Negotiate")); |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2312 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2313 |
case BASIC: |
2 | 2314 |
PasswordAuthentication a = |
2315 |
privilegedRequestPasswordAuthentication( |
|
2316 |
url.getHost(), addr, port, url.getProtocol(), |
|
2317 |
realm, scheme, url, RequestorType.SERVER); |
|
2318 |
if (a != null) { |
|
2319 |
ret = new BasicAuthentication(false, url, realm, a); |
|
2320 |
} |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2321 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2322 |
case DIGEST: |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2323 |
a = privilegedRequestPasswordAuthentication( |
2 | 2324 |
url.getHost(), addr, port, url.getProtocol(), |
2325 |
realm, scheme, url, RequestorType.SERVER); |
|
2326 |
if (a != null) { |
|
2327 |
digestparams = new DigestAuthentication.Parameters(); |
|
2328 |
ret = new DigestAuthentication(false, url, realm, scheme, a, digestparams); |
|
2329 |
} |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2330 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2331 |
case NTLM: |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
2332 |
if (NTLMAuthenticationProxy.supported) { |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2333 |
URL url1; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2334 |
try { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2335 |
url1 = new URL (url, "/"); /* truncate the path */ |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2336 |
} catch (Exception e) { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2337 |
url1 = url; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2338 |
} |
2 | 2339 |
|
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2340 |
/* tryTransparentNTLMServer will always be true the first |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2341 |
* time around, but verify that the platform supports it |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2342 |
* otherwise don't try. */ |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2343 |
if (tryTransparentNTLMServer) { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2344 |
tryTransparentNTLMServer = |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
2345 |
NTLMAuthenticationProxy.supportsTransparentAuth; |
8815
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
7022
diff
changeset
|
2346 |
/* If the platform supports transparent authentication |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
7022
diff
changeset
|
2347 |
* then check if we are in a secure environment |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
7022
diff
changeset
|
2348 |
* whether, or not, we should try transparent authentication.*/ |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
7022
diff
changeset
|
2349 |
if (tryTransparentNTLMServer) { |
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
7022
diff
changeset
|
2350 |
tryTransparentNTLMServer = |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
2351 |
NTLMAuthenticationProxy.isTrustedSite(url); |
8815
9a3f042d307e
6997851: Create NTLM AuthenticationCallBack class to avoid NTLM info leakage on client side
chegar
parents:
7022
diff
changeset
|
2352 |
} |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2353 |
} |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2354 |
a = null; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2355 |
if (tryTransparentNTLMServer) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
2356 |
logger.finest("Trying Transparent NTLM authentication"); |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2357 |
} else { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2358 |
a = privilegedRequestPasswordAuthentication( |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2359 |
url.getHost(), addr, port, url.getProtocol(), |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2360 |
"", scheme, url, RequestorType.SERVER); |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2361 |
} |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2362 |
|
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2363 |
/* If we are not trying transparent authentication then |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2364 |
* we need to have a PasswordAuthentication instance. For |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2365 |
* transparent authentication (Windows only) the username |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2366 |
* and password will be picked up from the current logged |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2367 |
* on users credentials. |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2368 |
*/ |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2369 |
if (tryTransparentNTLMServer || |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2370 |
(!tryTransparentNTLMServer && a != null)) { |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2371 |
ret = NTLMAuthenticationProxy.proxy.create(false, url1, a); |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2372 |
} |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2373 |
|
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2374 |
/* set to false so that we do not try again */ |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2375 |
tryTransparentNTLMServer = false; |
2 | 2376 |
} |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2377 |
break; |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2378 |
case UNKNOWN: |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
2379 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
16839
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
16007
diff
changeset
|
2380 |
logger.finest("Unknown/Unsupported authentication scheme: " + scheme); |
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
16007
diff
changeset
|
2381 |
} |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
2382 |
/*fall through*/ |
3859
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2383 |
default: |
8b82336dedb3
6882594: Remove static dependancy on NTLM authentication
chegar
parents:
3465
diff
changeset
|
2384 |
throw new AssertionError("should not reach here"); |
2 | 2385 |
} |
2386 |
} |
|
2387 |
||
2388 |
// For backwards compatibility, we also try defaultAuth |
|
2389 |
// REMIND: Get rid of this for JDK2.0. |
|
2390 |
||
2391 |
if (ret == null && defaultAuth != null |
|
2392 |
&& defaultAuth.schemeSupported(scheme)) { |
|
2393 |
String a = defaultAuth.authString(url, scheme, realm); |
|
2394 |
if (a != null) { |
|
2395 |
ret = new BasicAuthentication (false, url, realm, a); |
|
2396 |
// not in cache by default - cache on success |
|
2397 |
} |
|
2398 |
} |
|
2399 |
||
2400 |
if (ret != null ) { |
|
2401 |
if (!ret.setHeaders(this, p, raw)) { |
|
2402 |
ret = null; |
|
2403 |
} |
|
2404 |
} |
|
2405 |
} |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
2406 |
if (logger.isLoggable(PlatformLogger.Level.FINER)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
2407 |
logger.finer("Server Authentication for " + authhdr.toString() +" returned " + (ret != null ? ret.toString() : "null")); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2408 |
} |
2 | 2409 |
return ret; |
2410 |
} |
|
2411 |
||
2412 |
/* inclose will be true if called from close(), in which case we |
|
2413 |
* force the call to check because this is the last chance to do so. |
|
2414 |
* If not in close(), then the authentication info could arrive in a trailer |
|
2415 |
* field, which we have not read yet. |
|
2416 |
*/ |
|
2417 |
private void checkResponseCredentials (boolean inClose) throws IOException { |
|
2418 |
try { |
|
2419 |
if (!needToCheck) |
|
2420 |
return; |
|
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2421 |
if ((validateProxy && currentProxyCredentials != null) && |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2422 |
(currentProxyCredentials instanceof DigestAuthentication)) { |
2 | 2423 |
String raw = responses.findValue ("Proxy-Authentication-Info"); |
2424 |
if (inClose || (raw != null)) { |
|
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2425 |
DigestAuthentication da = (DigestAuthentication) |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2426 |
currentProxyCredentials; |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2427 |
da.checkResponse (raw, method, getRequestURI()); |
2 | 2428 |
currentProxyCredentials = null; |
2429 |
} |
|
2430 |
} |
|
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2431 |
if ((validateServer && currentServerCredentials != null) && |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2432 |
(currentServerCredentials instanceof DigestAuthentication)) { |
2 | 2433 |
String raw = responses.findValue ("Authentication-Info"); |
2434 |
if (inClose || (raw != null)) { |
|
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2435 |
DigestAuthentication da = (DigestAuthentication) |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2436 |
currentServerCredentials; |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2437 |
da.checkResponse (raw, method, url); |
2 | 2438 |
currentServerCredentials = null; |
2439 |
} |
|
2440 |
} |
|
2441 |
if ((currentServerCredentials==null) && (currentProxyCredentials == null)) { |
|
2442 |
needToCheck = false; |
|
2443 |
} |
|
2444 |
} catch (IOException e) { |
|
2445 |
disconnectInternal(); |
|
2446 |
connected = false; |
|
2447 |
throw e; |
|
2448 |
} |
|
2449 |
} |
|
2450 |
||
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2451 |
/* The request URI used in the request line for this request. |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2452 |
* Also, needed for digest authentication |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2453 |
*/ |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2454 |
|
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2455 |
String requestURI = null; |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2456 |
|
4150
74dfe4a968c6
6890349: Fix #6870935 in jdk7/pit/b74 caused HttpClinet's check for "proxy capture" attack by-passed.
michaelm
parents:
4047
diff
changeset
|
2457 |
String getRequestURI() throws IOException { |
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2458 |
if (requestURI == null) { |
4150
74dfe4a968c6
6890349: Fix #6870935 in jdk7/pit/b74 caused HttpClinet's check for "proxy capture" attack by-passed.
michaelm
parents:
4047
diff
changeset
|
2459 |
requestURI = http.getURLFile(); |
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2460 |
} |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2461 |
return requestURI; |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2462 |
} |
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2463 |
|
2 | 2464 |
/* Tells us whether to follow a redirect. If so, it |
2465 |
* closes the connection (break any keep-alive) and |
|
2466 |
* resets the url, re-connects, and resets the request |
|
2467 |
* property. |
|
2468 |
*/ |
|
2469 |
private boolean followRedirect() throws IOException { |
|
2470 |
if (!getInstanceFollowRedirects()) { |
|
2471 |
return false; |
|
2472 |
} |
|
2473 |
||
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2474 |
final int stat = getResponseCode(); |
2 | 2475 |
if (stat < 300 || stat > 307 || stat == 306 |
2476 |
|| stat == HTTP_NOT_MODIFIED) { |
|
2477 |
return false; |
|
2478 |
} |
|
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2479 |
final String loc = getHeaderField("Location"); |
2 | 2480 |
if (loc == null) { |
2481 |
/* this should be present - if not, we have no choice |
|
2482 |
* but to go forward w/ the response we got |
|
2483 |
*/ |
|
2484 |
return false; |
|
2485 |
} |
|
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2486 |
|
2 | 2487 |
URL locUrl; |
2488 |
try { |
|
2489 |
locUrl = new URL(loc); |
|
2490 |
if (!url.getProtocol().equalsIgnoreCase(locUrl.getProtocol())) { |
|
2491 |
return false; |
|
2492 |
} |
|
2493 |
||
2494 |
} catch (MalformedURLException mue) { |
|
2495 |
// treat loc as a relative URI to conform to popular browsers |
|
2496 |
locUrl = new URL(url, loc); |
|
2497 |
} |
|
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2498 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2499 |
final URL locUrl0 = locUrl; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2500 |
socketPermission = null; // force recalculation |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2501 |
SocketPermission p = URLtoSocketPermission(locUrl); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2502 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2503 |
if (p != null) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2504 |
try { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2505 |
return AccessController.doPrivileged( |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2506 |
new PrivilegedExceptionAction<Boolean>() { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2507 |
public Boolean run() throws IOException { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2508 |
return followRedirect0(loc, stat, locUrl0); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2509 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2510 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2511 |
// }, null, p -- replace line above, when limited doPriv ready |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2512 |
); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2513 |
} catch (PrivilegedActionException e) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2514 |
throw (IOException) e.getException(); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2515 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2516 |
} else { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2517 |
// run without additional permission |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2518 |
return followRedirect0(loc, stat, locUrl); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2519 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2520 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2521 |
|
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2522 |
/* Tells us whether to follow a redirect. If so, it |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2523 |
* closes the connection (break any keep-alive) and |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2524 |
* resets the url, re-connects, and resets the request |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2525 |
* property. |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2526 |
*/ |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2527 |
private boolean followRedirect0(String loc, int stat, URL locUrl) |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2528 |
throws IOException |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2529 |
{ |
2 | 2530 |
disconnectInternal(); |
2531 |
if (streaming()) { |
|
2532 |
throw new HttpRetryException (RETRY_MSG3, stat, loc); |
|
2533 |
} |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
17952
diff
changeset
|
2534 |
if (logger.isLoggable(PlatformLogger.Level.FINE)) { |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3859
diff
changeset
|
2535 |
logger.fine("Redirected from " + url + " to " + locUrl); |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2536 |
} |
2 | 2537 |
|
2538 |
// clear out old response headers!!!! |
|
2539 |
responses = new MessageHeader(); |
|
2540 |
if (stat == HTTP_USE_PROXY) { |
|
2541 |
/* This means we must re-request the resource through the |
|
2542 |
* proxy denoted in the "Location:" field of the response. |
|
2543 |
* Judging by the spec, the string in the Location header |
|
2544 |
* _should_ denote a URL - let's hope for "http://my.proxy.org" |
|
2545 |
* Make a new HttpClient to the proxy, using HttpClient's |
|
2546 |
* Instance-specific proxy fields, but note we're still fetching |
|
2547 |
* the same URL. |
|
2548 |
*/ |
|
2549 |
String proxyHost = locUrl.getHost(); |
|
2550 |
int proxyPort = locUrl.getPort(); |
|
2551 |
||
2552 |
SecurityManager security = System.getSecurityManager(); |
|
2553 |
if (security != null) { |
|
2554 |
security.checkConnect(proxyHost, proxyPort); |
|
2555 |
} |
|
2556 |
||
2557 |
setProxiedClient (url, proxyHost, proxyPort); |
|
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2558 |
requests.set(0, method + " " + getRequestURI()+" " + |
2 | 2559 |
httpVersion, null); |
2560 |
connected = true; |
|
2561 |
} else { |
|
2562 |
// maintain previous headers, just change the name |
|
2563 |
// of the file we're getting |
|
2564 |
url = locUrl; |
|
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2565 |
requestURI = null; // force it to be recalculated |
2 | 2566 |
if (method.equals("POST") && !Boolean.getBoolean("http.strictPostRedirect") && (stat!=307)) { |
2567 |
/* The HTTP/1.1 spec says that a redirect from a POST |
|
2568 |
* *should not* be immediately turned into a GET, and |
|
2569 |
* that some HTTP/1.0 clients incorrectly did this. |
|
2570 |
* Correct behavior redirects a POST to another POST. |
|
2571 |
* Unfortunately, since most browsers have this incorrect |
|
2572 |
* behavior, the web works this way now. Typical usage |
|
2573 |
* seems to be: |
|
2574 |
* POST a login code or passwd to a web page. |
|
2575 |
* after validation, the server redirects to another |
|
2576 |
* (welcome) page |
|
2577 |
* The second request is (erroneously) expected to be GET |
|
2578 |
* |
|
2579 |
* We will do the incorrect thing (POST-->GET) by default. |
|
2580 |
* We will provide the capability to do the "right" thing |
|
2581 |
* (POST-->POST) by a system property, "http.strictPostRedirect=true" |
|
2582 |
*/ |
|
2583 |
||
2584 |
requests = new MessageHeader(); |
|
2585 |
setRequests = false; |
|
2586 |
setRequestMethod("GET"); |
|
2587 |
poster = null; |
|
2588 |
if (!checkReuseConnection()) |
|
2589 |
connect(); |
|
2590 |
} else { |
|
2591 |
if (!checkReuseConnection()) |
|
2592 |
connect(); |
|
2593 |
/* Even after a connect() call, http variable still can be |
|
2594 |
* null, if a ResponseCache has been installed and it returns |
|
2595 |
* a non-null CacheResponse instance. So check nullity before using it. |
|
2596 |
* |
|
2597 |
* And further, if http is null, there's no need to do anything |
|
2598 |
* about request headers because successive http session will use |
|
2599 |
* cachedInputStream/cachedHeaders anyway, which is returned by |
|
2600 |
* CacheResponse. |
|
2601 |
*/ |
|
2602 |
if (http != null) { |
|
3952
dc329398de30
6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
3866
diff
changeset
|
2603 |
requests.set(0, method + " " + getRequestURI()+" " + |
2 | 2604 |
httpVersion, null); |
2605 |
int port = url.getPort(); |
|
2606 |
String host = url.getHost(); |
|
2607 |
if (port != -1 && port != url.getDefaultPort()) { |
|
2608 |
host += ":" + String.valueOf(port); |
|
2609 |
} |
|
2610 |
requests.set("Host", host); |
|
2611 |
} |
|
2612 |
} |
|
2613 |
} |
|
2614 |
return true; |
|
2615 |
} |
|
2616 |
||
2617 |
/* dummy byte buffer for reading off socket prior to closing */ |
|
2618 |
byte[] cdata = new byte [128]; |
|
2619 |
||
2620 |
/** |
|
2621 |
* Reset (without disconnecting the TCP conn) in order to do another transaction with this instance |
|
2622 |
*/ |
|
2623 |
private void reset() throws IOException { |
|
2624 |
http.reuse = true; |
|
2625 |
/* must save before calling close */ |
|
2626 |
reuseClient = http; |
|
2627 |
InputStream is = http.getInputStream(); |
|
2628 |
if (!method.equals("HEAD")) { |
|
2629 |
try { |
|
2630 |
/* we want to read the rest of the response without using the |
|
2631 |
* hurry mechanism, because that would close the connection |
|
2632 |
* if everything is not available immediately |
|
2633 |
*/ |
|
2634 |
if ((is instanceof ChunkedInputStream) || |
|
2635 |
(is instanceof MeteredStream)) { |
|
2636 |
/* reading until eof will not block */ |
|
2637 |
while (is.read (cdata) > 0) {} |
|
2638 |
} else { |
|
2639 |
/* raw stream, which will block on read, so only read |
|
2640 |
* the expected number of bytes, probably 0 |
|
2641 |
*/ |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2642 |
long cl = 0; |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2643 |
int n = 0; |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2644 |
String cls = responses.findValue ("Content-Length"); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2645 |
if (cls != null) { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2646 |
try { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2647 |
cl = Long.parseLong (cls); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2648 |
} catch (NumberFormatException e) { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2649 |
cl = 0; |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2650 |
} |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2651 |
} |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2942
diff
changeset
|
2652 |
for (long i=0; i<cl; ) { |
2 | 2653 |
if ((n = is.read (cdata)) == -1) { |
2654 |
break; |
|
2655 |
} else { |
|
2656 |
i+= n; |
|
2657 |
} |
|
2658 |
} |
|
2659 |
} |
|
2660 |
} catch (IOException e) { |
|
2661 |
http.reuse = false; |
|
2662 |
reuseClient = null; |
|
2663 |
disconnectInternal(); |
|
2664 |
return; |
|
2665 |
} |
|
2666 |
try { |
|
2667 |
if (is instanceof MeteredStream) { |
|
2668 |
is.close(); |
|
2669 |
} |
|
2670 |
} catch (IOException e) { } |
|
2671 |
} |
|
2672 |
responseCode = -1; |
|
2673 |
responses = new MessageHeader(); |
|
2674 |
connected = false; |
|
2675 |
} |
|
2676 |
||
2677 |
/** |
|
5161 | 2678 |
* Disconnect from the web server at the first 401 error. Do not |
2679 |
* disconnect when using a proxy, a good proxy should have already |
|
2680 |
* closed the connection to the web server. |
|
2681 |
*/ |
|
2682 |
private void disconnectWeb() throws IOException { |
|
5979
26b9b2b1b37b
6954525: Testcase failure java/net/Authenticator/B4769350.java
chegar
parents:
5506
diff
changeset
|
2683 |
if (usingProxy() && http.isKeepingAlive()) { |
5161 | 2684 |
responseCode = -1; |
2685 |
// clean up, particularly, skip the content part |
|
2686 |
// of a 401 error response |
|
2687 |
reset(); |
|
2688 |
} else { |
|
2689 |
disconnectInternal(); |
|
2690 |
} |
|
2691 |
} |
|
2692 |
||
2693 |
/** |
|
2 | 2694 |
* Disconnect from the server (for internal use) |
2695 |
*/ |
|
2696 |
private void disconnectInternal() { |
|
2697 |
responseCode = -1; |
|
78
65353d0d0e31
6660405: HttpURLConnection returns the wrong InputStream
jccollet
parents:
74
diff
changeset
|
2698 |
inputStream = null; |
2 | 2699 |
if (pi != null) { |
2700 |
pi.finishTracking(); |
|
2701 |
pi = null; |
|
2702 |
} |
|
2703 |
if (http != null) { |
|
2704 |
http.closeServer(); |
|
2705 |
http = null; |
|
2706 |
connected = false; |
|
2707 |
} |
|
2708 |
} |
|
2709 |
||
2710 |
/** |
|
2711 |
* Disconnect from the server (public API) |
|
2712 |
*/ |
|
2713 |
public void disconnect() { |
|
2714 |
||
2715 |
responseCode = -1; |
|
2716 |
if (pi != null) { |
|
2717 |
pi.finishTracking(); |
|
2718 |
pi = null; |
|
2719 |
} |
|
2720 |
||
2721 |
if (http != null) { |
|
2722 |
/* |
|
2723 |
* If we have an input stream this means we received a response |
|
2724 |
* from the server. That stream may have been read to EOF and |
|
2725 |
* dependening on the stream type may already be closed or the |
|
2726 |
* the http client may be returned to the keep-alive cache. |
|
2727 |
* If the http client has been returned to the keep-alive cache |
|
2728 |
* it may be closed (idle timeout) or may be allocated to |
|
2729 |
* another request. |
|
2730 |
* |
|
2731 |
* In other to avoid timing issues we close the input stream |
|
2732 |
* which will either close the underlying connection or return |
|
2733 |
* the client to the cache. If there's a possibility that the |
|
2734 |
* client has been returned to the cache (ie: stream is a keep |
|
2735 |
* alive stream or a chunked input stream) then we remove an |
|
2736 |
* idle connection to the server. Note that this approach |
|
2737 |
* can be considered an approximation in that we may close a |
|
2738 |
* different idle connection to that used by the request. |
|
2739 |
* Additionally it's possible that we close two connections |
|
2740 |
* - the first becuase it wasn't an EOF (and couldn't be |
|
2741 |
* hurried) - the second, another idle connection to the |
|
2742 |
* same server. The is okay because "disconnect" is an |
|
2743 |
* indication that the application doesn't intend to access |
|
2744 |
* this http server for a while. |
|
2745 |
*/ |
|
2746 |
||
2747 |
if (inputStream != null) { |
|
2748 |
HttpClient hc = http; |
|
2749 |
||
2750 |
// un-synchronized |
|
2751 |
boolean ka = hc.isKeepingAlive(); |
|
2752 |
||
2753 |
try { |
|
2754 |
inputStream.close(); |
|
2755 |
} catch (IOException ioe) { } |
|
2756 |
||
2757 |
// if the connection is persistent it may have been closed |
|
2758 |
// or returned to the keep-alive cache. If it's been returned |
|
2759 |
// to the keep-alive cache then we would like to close it |
|
2760 |
// but it may have been allocated |
|
2761 |
||
2762 |
if (ka) { |
|
2763 |
hc.closeIdleConnection(); |
|
2764 |
} |
|
2765 |
||
2766 |
||
2767 |
} else { |
|
2768 |
// We are deliberatly being disconnected so HttpClient |
|
2769 |
// should not try to resend the request no matter what stage |
|
2770 |
// of the connection we are in. |
|
2771 |
http.setDoNotRetry(true); |
|
2772 |
||
2773 |
http.closeServer(); |
|
2774 |
} |
|
2775 |
||
2776 |
// poster = null; |
|
2777 |
http = null; |
|
2778 |
connected = false; |
|
2779 |
} |
|
2780 |
cachedInputStream = null; |
|
2781 |
if (cachedHeaders != null) { |
|
2782 |
cachedHeaders.reset(); |
|
2783 |
} |
|
2784 |
} |
|
2785 |
||
2786 |
public boolean usingProxy() { |
|
2787 |
if (http != null) { |
|
2788 |
return (http.getProxyHostUsed() != null); |
|
2789 |
} |
|
2790 |
return false; |
|
2791 |
} |
|
2792 |
||
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2793 |
// constant strings represent set-cookie header names |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2794 |
private final static String SET_COOKIE = "set-cookie"; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2795 |
private final static String SET_COOKIE2 = "set-cookie2"; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2796 |
|
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2797 |
/** |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2798 |
* Returns a filtered version of the given headers value. |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2799 |
* |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2800 |
* Note: The implementation currently only filters out HttpOnly cookies |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2801 |
* from Set-Cookie and Set-Cookie2 headers. |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2802 |
*/ |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2803 |
private String filterHeaderField(String name, String value) { |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2804 |
if (value == null) |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2805 |
return null; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2806 |
|
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2807 |
if (SET_COOKIE.equalsIgnoreCase(name) || |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2808 |
SET_COOKIE2.equalsIgnoreCase(name)) { |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2809 |
// Filtering only if there is a cookie handler. [Assumption: the |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2810 |
// cookie handler will store/retrieve the HttpOnly cookies] |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2811 |
if (cookieHandler == null) |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2812 |
return value; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2813 |
|
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2814 |
sun.misc.JavaNetHttpCookieAccess access = |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2815 |
sun.misc.SharedSecrets.getJavaNetHttpCookieAccess(); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2816 |
StringBuilder retValue = new StringBuilder(); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2817 |
List<HttpCookie> cookies = access.parse(value); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2818 |
boolean multipleCookies = false; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2819 |
for (HttpCookie cookie : cookies) { |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2820 |
// skip HttpOnly cookies |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2821 |
if (cookie.isHttpOnly()) |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2822 |
continue; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2823 |
if (multipleCookies) |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2824 |
retValue.append(','); // RFC 2965, comma separated |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2825 |
retValue.append(access.header(cookie)); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2826 |
multipleCookies = true; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2827 |
} |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2828 |
|
16007
cf20f016fa94
8007315: HttpURLConnection.filterHeaderField method returns null where empty string is expected
coffeys
parents:
14766
diff
changeset
|
2829 |
return retValue.length() == 0 ? "" : retValue.toString(); |
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2830 |
} |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2831 |
|
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2832 |
return value; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2833 |
} |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2834 |
|
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2835 |
// Cache the filtered response headers so that they don't need |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2836 |
// to be generated for every getHeaderFields() call. |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2837 |
private Map<String, List<String>> filteredHeaders; // null |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2838 |
|
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2839 |
private Map<String, List<String>> getFilteredHeaderFields() { |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2840 |
if (filteredHeaders != null) |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2841 |
return filteredHeaders; |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2842 |
|
11518
7e720dd82e9e
7128648: HttpURLConnection.getHeaderFields should return an unmodifiable Map
chegar
parents:
11284
diff
changeset
|
2843 |
Map<String, List<String>> headers, tmpMap = new HashMap<>(); |
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2844 |
|
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2845 |
if (cachedHeaders != null) |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2846 |
headers = cachedHeaders.getHeaders(); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2847 |
else |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2848 |
headers = responses.getHeaders(); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2849 |
|
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2850 |
for (Map.Entry<String, List<String>> e: headers.entrySet()) { |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2851 |
String key = e.getKey(); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2852 |
List<String> values = e.getValue(), filteredVals = new ArrayList<>(); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2853 |
for (String value : values) { |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2854 |
String fVal = filterHeaderField(key, value); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2855 |
if (fVal != null) |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2856 |
filteredVals.add(fVal); |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2857 |
} |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2858 |
if (!filteredVals.isEmpty()) |
11518
7e720dd82e9e
7128648: HttpURLConnection.getHeaderFields should return an unmodifiable Map
chegar
parents:
11284
diff
changeset
|
2859 |
tmpMap.put(key, Collections.unmodifiableList(filteredVals)); |
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2860 |
} |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2861 |
|
11518
7e720dd82e9e
7128648: HttpURLConnection.getHeaderFields should return an unmodifiable Map
chegar
parents:
11284
diff
changeset
|
2862 |
return filteredHeaders = Collections.unmodifiableMap(tmpMap); |
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2863 |
} |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2864 |
|
2 | 2865 |
/** |
2866 |
* Gets a header field by name. Returns null if not known. |
|
2867 |
* @param name the name of the header field |
|
2868 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
2869 |
@Override |
2 | 2870 |
public String getHeaderField(String name) { |
2871 |
try { |
|
2872 |
getInputStream(); |
|
2873 |
} catch (IOException e) {} |
|
2874 |
||
2875 |
if (cachedHeaders != null) { |
|
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2876 |
return filterHeaderField(name, cachedHeaders.findValue(name)); |
2 | 2877 |
} |
2878 |
||
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2879 |
return filterHeaderField(name, responses.findValue(name)); |
2 | 2880 |
} |
2881 |
||
2882 |
/** |
|
2883 |
* Returns an unmodifiable Map of the header fields. |
|
2884 |
* The Map keys are Strings that represent the |
|
2885 |
* response-header field names. Each Map value is an |
|
2886 |
* unmodifiable List of Strings that represents |
|
2887 |
* the corresponding field values. |
|
2888 |
* |
|
2889 |
* @return a Map of header fields |
|
2890 |
* @since 1.4 |
|
2891 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
2892 |
@Override |
51 | 2893 |
public Map<String, List<String>> getHeaderFields() { |
2 | 2894 |
try { |
2895 |
getInputStream(); |
|
2896 |
} catch (IOException e) {} |
|
2897 |
||
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2898 |
return getFilteredHeaderFields(); |
2 | 2899 |
} |
2900 |
||
2901 |
/** |
|
2902 |
* Gets a header field by index. Returns null if not known. |
|
2903 |
* @param n the index of the header field |
|
2904 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
2905 |
@Override |
2 | 2906 |
public String getHeaderField(int n) { |
2907 |
try { |
|
2908 |
getInputStream(); |
|
2909 |
} catch (IOException e) {} |
|
2910 |
||
2911 |
if (cachedHeaders != null) { |
|
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2912 |
return filterHeaderField(cachedHeaders.getKey(n), |
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2913 |
cachedHeaders.getValue(n)); |
2 | 2914 |
} |
11284
2750cfd2352c
7095980: Ensure HttpURLConnection (and supporting APIs) don't expose HttpOnly cookies
chegar
parents:
10701
diff
changeset
|
2915 |
return filterHeaderField(responses.getKey(n), responses.getValue(n)); |
2 | 2916 |
} |
2917 |
||
2918 |
/** |
|
2919 |
* Gets a header field by index. Returns null if not known. |
|
2920 |
* @param n the index of the header field |
|
2921 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
2922 |
@Override |
2 | 2923 |
public String getHeaderFieldKey(int n) { |
2924 |
try { |
|
2925 |
getInputStream(); |
|
2926 |
} catch (IOException e) {} |
|
2927 |
||
2928 |
if (cachedHeaders != null) { |
|
2929 |
return cachedHeaders.getKey(n); |
|
2930 |
} |
|
2931 |
||
2932 |
return responses.getKey(n); |
|
2933 |
} |
|
2934 |
||
2935 |
/** |
|
2936 |
* Sets request property. If a property with the key already |
|
2937 |
* exists, overwrite its value with the new value. |
|
2938 |
* @param value the value to be set |
|
2939 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
2940 |
@Override |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2941 |
public synchronized void setRequestProperty(String key, String value) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2942 |
if (connected || connecting) |
2 | 2943 |
throw new IllegalStateException("Already connected"); |
2944 |
if (key == null) |
|
2945 |
throw new NullPointerException ("key is null"); |
|
2946 |
||
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2947 |
if (isExternalMessageHeaderAllowed(key, value)) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2948 |
requests.set(key, value); |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2949 |
if (!key.equalsIgnoreCase("Content-Type")) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2950 |
userHeaders.set(key, value); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2951 |
} |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2952 |
} |
2 | 2953 |
} |
2954 |
||
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2955 |
MessageHeader getUserSetHeaders() { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2956 |
return userHeaders; |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2957 |
} |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2958 |
|
2 | 2959 |
/** |
2960 |
* Adds a general request property specified by a |
|
2961 |
* key-value pair. This method will not overwrite |
|
2962 |
* existing values associated with the same key. |
|
2963 |
* |
|
2964 |
* @param key the keyword by which the request is known |
|
2965 |
* (e.g., "<code>accept</code>"). |
|
2966 |
* @param value the value associated with it. |
|
2967 |
* @see #getRequestProperties(java.lang.String) |
|
2968 |
* @since 1.4 |
|
2969 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
2970 |
@Override |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2971 |
public synchronized void addRequestProperty(String key, String value) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2972 |
if (connected || connecting) |
2 | 2973 |
throw new IllegalStateException("Already connected"); |
2974 |
if (key == null) |
|
2975 |
throw new NullPointerException ("key is null"); |
|
2976 |
||
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2977 |
if (isExternalMessageHeaderAllowed(key, value)) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2978 |
requests.add(key, value); |
17473
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2979 |
if (!key.equalsIgnoreCase("Content-Type")) { |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2980 |
userHeaders.add(key, value); |
35cd9b3a98ff
8010464: Evolve java networking same origin policy
michaelm
parents:
16839
diff
changeset
|
2981 |
} |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2982 |
} |
2 | 2983 |
} |
2984 |
||
2985 |
// |
|
2986 |
// Set a property for authentication. This can safely disregard |
|
2987 |
// the connected test. |
|
2988 |
// |
|
4157
558590fb3b49
6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents:
4047
diff
changeset
|
2989 |
public void setAuthenticationProperty(String key, String value) { |
2 | 2990 |
checkMessageHeader(key, value); |
2991 |
requests.set(key, value); |
|
2992 |
} |
|
2993 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
2994 |
@Override |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2995 |
public synchronized String getRequestProperty (String key) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2996 |
if (key == null) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2997 |
return null; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2998 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
2999 |
|
2 | 3000 |
// don't return headers containing security sensitive information |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3001 |
for (int i=0; i < EXCLUDE_HEADERS.length; i++) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3002 |
if (key.equalsIgnoreCase(EXCLUDE_HEADERS[i])) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3003 |
return null; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3004 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3005 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3006 |
if (!setUserCookies) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3007 |
if (key.equalsIgnoreCase("Cookie")) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3008 |
return userCookies; |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3009 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3010 |
if (key.equalsIgnoreCase("Cookie2")) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3011 |
return userCookies2; |
2 | 3012 |
} |
3013 |
} |
|
3014 |
return requests.findValue(key); |
|
3015 |
} |
|
3016 |
||
3017 |
/** |
|
3018 |
* Returns an unmodifiable Map of general request |
|
3019 |
* properties for this connection. The Map keys |
|
3020 |
* are Strings that represent the request-header |
|
3021 |
* field names. Each Map value is a unmodifiable List |
|
3022 |
* of Strings that represents the corresponding |
|
3023 |
* field values. |
|
3024 |
* |
|
3025 |
* @return a Map of the general request properties for this connection. |
|
3026 |
* @throws IllegalStateException if already connected |
|
3027 |
* @since 1.4 |
|
3028 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3029 |
@Override |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3030 |
public synchronized Map<String, List<String>> getRequestProperties() { |
2 | 3031 |
if (connected) |
3032 |
throw new IllegalStateException("Already connected"); |
|
3033 |
||
3034 |
// exclude headers containing security-sensitive info |
|
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3035 |
if (setUserCookies) { |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3036 |
return requests.getHeaders(EXCLUDE_HEADERS); |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3037 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3038 |
/* |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3039 |
* The cookies in the requests message headers may have |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3040 |
* been modified. Use the saved user cookies instead. |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3041 |
*/ |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
3042 |
Map<String, List<String>> userCookiesMap = null; |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3043 |
if (userCookies != null || userCookies2 != null) { |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
3044 |
userCookiesMap = new HashMap<>(); |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3045 |
if (userCookies != null) { |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
3046 |
userCookiesMap.put("Cookie", Arrays.asList(userCookies)); |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3047 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3048 |
if (userCookies2 != null) { |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10136
diff
changeset
|
3049 |
userCookiesMap.put("Cookie2", Arrays.asList(userCookies2)); |
6876
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3050 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3051 |
} |
13fdbd146659
6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents:
6873
diff
changeset
|
3052 |
return requests.filterAndAddHeaders(EXCLUDE_HEADERS2, userCookiesMap); |
2 | 3053 |
} |
3054 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3055 |
@Override |
2 | 3056 |
public void setConnectTimeout(int timeout) { |
3057 |
if (timeout < 0) |
|
3058 |
throw new IllegalArgumentException("timeouts can't be negative"); |
|
3059 |
connectTimeout = timeout; |
|
3060 |
} |
|
3061 |
||
3062 |
||
3063 |
/** |
|
3064 |
* Returns setting for connect timeout. |
|
3065 |
* <p> |
|
3066 |
* 0 return implies that the option is disabled |
|
3067 |
* (i.e., timeout of infinity). |
|
3068 |
* |
|
3069 |
* @return an <code>int</code> that indicates the connect timeout |
|
3070 |
* value in milliseconds |
|
3071 |
* @see java.net.URLConnection#setConnectTimeout(int) |
|
3072 |
* @see java.net.URLConnection#connect() |
|
3073 |
* @since 1.5 |
|
3074 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3075 |
@Override |
2 | 3076 |
public int getConnectTimeout() { |
3077 |
return (connectTimeout < 0 ? 0 : connectTimeout); |
|
3078 |
} |
|
3079 |
||
3080 |
/** |
|
3081 |
* Sets the read timeout to a specified timeout, in |
|
3082 |
* milliseconds. A non-zero value specifies the timeout when |
|
3083 |
* reading from Input stream when a connection is established to a |
|
3084 |
* resource. If the timeout expires before there is data available |
|
3085 |
* for read, a java.net.SocketTimeoutException is raised. A |
|
3086 |
* timeout of zero is interpreted as an infinite timeout. |
|
3087 |
* |
|
3088 |
* <p> Some non-standard implementation of this method ignores the |
|
3089 |
* specified timeout. To see the read timeout set, please call |
|
3090 |
* getReadTimeout(). |
|
3091 |
* |
|
3092 |
* @param timeout an <code>int</code> that specifies the timeout |
|
3093 |
* value to be used in milliseconds |
|
3094 |
* @throws IllegalArgumentException if the timeout parameter is negative |
|
3095 |
* |
|
3096 |
* @see java.net.URLConnectiongetReadTimeout() |
|
3097 |
* @see java.io.InputStream#read() |
|
3098 |
* @since 1.5 |
|
3099 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3100 |
@Override |
2 | 3101 |
public void setReadTimeout(int timeout) { |
3102 |
if (timeout < 0) |
|
3103 |
throw new IllegalArgumentException("timeouts can't be negative"); |
|
3104 |
readTimeout = timeout; |
|
3105 |
} |
|
3106 |
||
3107 |
/** |
|
3108 |
* Returns setting for read timeout. 0 return implies that the |
|
3109 |
* option is disabled (i.e., timeout of infinity). |
|
3110 |
* |
|
3111 |
* @return an <code>int</code> that indicates the read timeout |
|
3112 |
* value in milliseconds |
|
3113 |
* |
|
3114 |
* @see java.net.URLConnection#setReadTimeout(int) |
|
3115 |
* @see java.io.InputStream#read() |
|
3116 |
* @since 1.5 |
|
3117 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3118 |
@Override |
2 | 3119 |
public int getReadTimeout() { |
3120 |
return readTimeout < 0 ? 0 : readTimeout; |
|
3121 |
} |
|
3122 |
||
11528
638ca25aec87
7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents:
11518
diff
changeset
|
3123 |
public CookieHandler getCookieHandler() { |
638ca25aec87
7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents:
11518
diff
changeset
|
3124 |
return cookieHandler; |
638ca25aec87
7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents:
11518
diff
changeset
|
3125 |
} |
638ca25aec87
7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents:
11518
diff
changeset
|
3126 |
|
2 | 3127 |
String getMethod() { |
3128 |
return method; |
|
3129 |
} |
|
3130 |
||
51 | 3131 |
private MessageHeader mapToMessageHeader(Map<String, List<String>> map) { |
2 | 3132 |
MessageHeader headers = new MessageHeader(); |
3133 |
if (map == null || map.isEmpty()) { |
|
3134 |
return headers; |
|
3135 |
} |
|
51 | 3136 |
for (Map.Entry<String, List<String>> entry : map.entrySet()) { |
3137 |
String key = entry.getKey(); |
|
3138 |
List<String> values = entry.getValue(); |
|
3139 |
for (String value : values) { |
|
2 | 3140 |
if (key == null) { |
3141 |
headers.prepend(key, value); |
|
3142 |
} else { |
|
3143 |
headers.add(key, value); |
|
3144 |
} |
|
3145 |
} |
|
3146 |
} |
|
3147 |
return headers; |
|
3148 |
} |
|
3149 |
||
3150 |
/* The purpose of this wrapper is just to capture the close() call |
|
3151 |
* so we can check authentication information that may have |
|
3152 |
* arrived in a Trailer field |
|
3153 |
*/ |
|
3154 |
class HttpInputStream extends FilterInputStream { |
|
3155 |
private CacheRequest cacheRequest; |
|
3156 |
private OutputStream outputStream; |
|
3157 |
private boolean marked = false; |
|
3158 |
private int inCache = 0; |
|
3159 |
private int markCount = 0; |
|
17952
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3160 |
private boolean closed; // false |
2 | 3161 |
|
3162 |
public HttpInputStream (InputStream is) { |
|
3163 |
super (is); |
|
3164 |
this.cacheRequest = null; |
|
3165 |
this.outputStream = null; |
|
3166 |
} |
|
3167 |
||
3168 |
public HttpInputStream (InputStream is, CacheRequest cacheRequest) { |
|
3169 |
super (is); |
|
3170 |
this.cacheRequest = cacheRequest; |
|
3171 |
try { |
|
3172 |
this.outputStream = cacheRequest.getBody(); |
|
3173 |
} catch (IOException ioex) { |
|
3174 |
this.cacheRequest.abort(); |
|
3175 |
this.cacheRequest = null; |
|
3176 |
this.outputStream = null; |
|
3177 |
} |
|
3178 |
} |
|
3179 |
||
3180 |
/** |
|
3181 |
* Marks the current position in this input stream. A subsequent |
|
3182 |
* call to the <code>reset</code> method repositions this stream at |
|
3183 |
* the last marked position so that subsequent reads re-read the same |
|
3184 |
* bytes. |
|
3185 |
* <p> |
|
3186 |
* The <code>readlimit</code> argument tells this input stream to |
|
3187 |
* allow that many bytes to be read before the mark position gets |
|
3188 |
* invalidated. |
|
3189 |
* <p> |
|
3190 |
* This method simply performs <code>in.mark(readlimit)</code>. |
|
3191 |
* |
|
3192 |
* @param readlimit the maximum limit of bytes that can be read before |
|
3193 |
* the mark position becomes invalid. |
|
3194 |
* @see java.io.FilterInputStream#in |
|
3195 |
* @see java.io.FilterInputStream#reset() |
|
3196 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3197 |
@Override |
2 | 3198 |
public synchronized void mark(int readlimit) { |
3199 |
super.mark(readlimit); |
|
3200 |
if (cacheRequest != null) { |
|
3201 |
marked = true; |
|
3202 |
markCount = 0; |
|
3203 |
} |
|
3204 |
} |
|
3205 |
||
3206 |
/** |
|
3207 |
* Repositions this stream to the position at the time the |
|
3208 |
* <code>mark</code> method was last called on this input stream. |
|
3209 |
* <p> |
|
3210 |
* This method |
|
3211 |
* simply performs <code>in.reset()</code>. |
|
3212 |
* <p> |
|
3213 |
* Stream marks are intended to be used in |
|
3214 |
* situations where you need to read ahead a little to see what's in |
|
3215 |
* the stream. Often this is most easily done by invoking some |
|
3216 |
* general parser. If the stream is of the type handled by the |
|
3217 |
* parse, it just chugs along happily. If the stream is not of |
|
3218 |
* that type, the parser should toss an exception when it fails. |
|
3219 |
* If this happens within readlimit bytes, it allows the outer |
|
3220 |
* code to reset the stream and try another parser. |
|
3221 |
* |
|
3222 |
* @exception IOException if the stream has not been marked or if the |
|
3223 |
* mark has been invalidated. |
|
3224 |
* @see java.io.FilterInputStream#in |
|
3225 |
* @see java.io.FilterInputStream#mark(int) |
|
3226 |
*/ |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3227 |
@Override |
2 | 3228 |
public synchronized void reset() throws IOException { |
3229 |
super.reset(); |
|
3230 |
if (cacheRequest != null) { |
|
3231 |
marked = false; |
|
3232 |
inCache += markCount; |
|
3233 |
} |
|
3234 |
} |
|
3235 |
||
17952
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3236 |
private void ensureOpen() throws IOException { |
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3237 |
if (closed) |
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3238 |
throw new IOException("stream is closed"); |
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3239 |
} |
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3240 |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3241 |
@Override |
2 | 3242 |
public int read() throws IOException { |
17952
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3243 |
ensureOpen(); |
2 | 3244 |
try { |
3245 |
byte[] b = new byte[1]; |
|
3246 |
int ret = read(b); |
|
3247 |
return (ret == -1? ret : (b[0] & 0x00FF)); |
|
3248 |
} catch (IOException ioex) { |
|
3249 |
if (cacheRequest != null) { |
|
3250 |
cacheRequest.abort(); |
|
3251 |
} |
|
3252 |
throw ioex; |
|
3253 |
} |
|
3254 |
} |
|
3255 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3256 |
@Override |
2 | 3257 |
public int read(byte[] b) throws IOException { |
3258 |
return read(b, 0, b.length); |
|
3259 |
} |
|
3260 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3261 |
@Override |
2 | 3262 |
public int read(byte[] b, int off, int len) throws IOException { |
17952
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3263 |
ensureOpen(); |
2 | 3264 |
try { |
3265 |
int newLen = super.read(b, off, len); |
|
3266 |
int nWrite; |
|
3267 |
// write to cache |
|
3268 |
if (inCache > 0) { |
|
3269 |
if (inCache >= newLen) { |
|
3270 |
inCache -= newLen; |
|
3271 |
nWrite = 0; |
|
3272 |
} else { |
|
3273 |
nWrite = newLen - inCache; |
|
3274 |
inCache = 0; |
|
3275 |
} |
|
3276 |
} else { |
|
3277 |
nWrite = newLen; |
|
3278 |
} |
|
3279 |
if (nWrite > 0 && outputStream != null) |
|
3280 |
outputStream.write(b, off + (newLen-nWrite), nWrite); |
|
3281 |
if (marked) { |
|
3282 |
markCount += newLen; |
|
3283 |
} |
|
3284 |
return newLen; |
|
3285 |
} catch (IOException ioex) { |
|
3286 |
if (cacheRequest != null) { |
|
3287 |
cacheRequest.abort(); |
|
3288 |
} |
|
3289 |
throw ioex; |
|
3290 |
} |
|
3291 |
} |
|
3292 |
||
6685
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3293 |
/* skip() calls read() in order to ensure that entire response gets |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3294 |
* cached. same implementation as InputStream.skip */ |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3295 |
|
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3296 |
private byte[] skipBuffer; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3297 |
private static final int SKIP_BUFFER_SIZE = 8096; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3298 |
|
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3299 |
@Override |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3300 |
public long skip (long n) throws IOException { |
17952
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3301 |
ensureOpen(); |
6685
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3302 |
long remaining = n; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3303 |
int nr; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3304 |
if (skipBuffer == null) |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3305 |
skipBuffer = new byte[SKIP_BUFFER_SIZE]; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3306 |
|
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3307 |
byte[] localSkipBuffer = skipBuffer; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3308 |
|
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3309 |
if (n <= 0) { |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3310 |
return 0; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3311 |
} |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3312 |
|
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3313 |
while (remaining > 0) { |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3314 |
nr = read(localSkipBuffer, 0, |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3315 |
(int) Math.min(SKIP_BUFFER_SIZE, remaining)); |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3316 |
if (nr < 0) { |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3317 |
break; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3318 |
} |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3319 |
remaining -= nr; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3320 |
} |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3321 |
|
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3322 |
return n - remaining; |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3323 |
} |
36f070045e17
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
6673
diff
changeset
|
3324 |
|
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3325 |
@Override |
2 | 3326 |
public void close () throws IOException { |
17952
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3327 |
if (closed) |
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3328 |
return; |
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3329 |
|
2 | 3330 |
try { |
3331 |
if (outputStream != null) { |
|
3332 |
if (read() != -1) { |
|
3333 |
cacheRequest.abort(); |
|
3334 |
} else { |
|
3335 |
outputStream.close(); |
|
3336 |
} |
|
3337 |
} |
|
3338 |
super.close (); |
|
3339 |
} catch (IOException ioex) { |
|
3340 |
if (cacheRequest != null) { |
|
3341 |
cacheRequest.abort(); |
|
3342 |
} |
|
3343 |
throw ioex; |
|
3344 |
} finally { |
|
17952
a5f2fb0e7457
8011719: Properties.loadFromXML fails with a chunked HTTP connection
chegar
parents:
17485
diff
changeset
|
3345 |
closed = true; |
2 | 3346 |
HttpURLConnection.this.http = null; |
3347 |
checkResponseCredentials (true); |
|
3348 |
} |
|
3349 |
} |
|
3350 |
} |
|
3351 |
||
3352 |
class StreamingOutputStream extends FilterOutputStream { |
|
3353 |
||
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
3354 |
long expected; |
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
3355 |
long written; |
2 | 3356 |
boolean closed; |
3357 |
boolean error; |
|
3358 |
IOException errorExcp; |
|
3359 |
||
3360 |
/** |
|
3361 |
* expectedLength == -1 if the stream is chunked |
|
3362 |
* expectedLength > 0 if the stream is fixed content-length |
|
3363 |
* In the 2nd case, we make sure the expected number of |
|
3364 |
* of bytes are actually written |
|
3365 |
*/ |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
3366 |
StreamingOutputStream (OutputStream os, long expectedLength) { |
2 | 3367 |
super (os); |
3368 |
expected = expectedLength; |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
3369 |
written = 0L; |
2 | 3370 |
closed = false; |
3371 |
error = false; |
|
3372 |
} |
|
3373 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3374 |
@Override |
2 | 3375 |
public void write (int b) throws IOException { |
3376 |
checkError(); |
|
3377 |
written ++; |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
3378 |
if (expected != -1L && written > expected) { |
2 | 3379 |
throw new IOException ("too many bytes written"); |
3380 |
} |
|
3381 |
out.write (b); |
|
3382 |
} |
|
3383 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3384 |
@Override |
2 | 3385 |
public void write (byte[] b) throws IOException { |
3386 |
write (b, 0, b.length); |
|
3387 |
} |
|
3388 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3389 |
@Override |
2 | 3390 |
public void write (byte[] b, int off, int len) throws IOException { |
3391 |
checkError(); |
|
3392 |
written += len; |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
3393 |
if (expected != -1L && written > expected) { |
2 | 3394 |
out.close (); |
3395 |
throw new IOException ("too many bytes written"); |
|
3396 |
} |
|
3397 |
out.write (b, off, len); |
|
3398 |
} |
|
3399 |
||
3400 |
void checkError () throws IOException { |
|
3401 |
if (closed) { |
|
3402 |
throw new IOException ("Stream is closed"); |
|
3403 |
} |
|
3404 |
if (error) { |
|
3405 |
throw errorExcp; |
|
3406 |
} |
|
3407 |
if (((PrintStream)out).checkError()) { |
|
3408 |
throw new IOException("Error writing request body to server"); |
|
3409 |
} |
|
3410 |
} |
|
3411 |
||
3412 |
/* this is called to check that all the bytes |
|
3413 |
* that were supposed to be written were written |
|
3414 |
* and that the stream is now closed(). |
|
3415 |
*/ |
|
3416 |
boolean writtenOK () { |
|
3417 |
return closed && ! error; |
|
3418 |
} |
|
3419 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3420 |
@Override |
2 | 3421 |
public void close () throws IOException { |
3422 |
if (closed) { |
|
3423 |
return; |
|
3424 |
} |
|
3425 |
closed = true; |
|
1576
b697b141012d
6755625: Add HttpURLConnection.setFixedLengthStreamingMode(long)
chegar
parents:
715
diff
changeset
|
3426 |
if (expected != -1L) { |
2 | 3427 |
/* not chunked */ |
3428 |
if (written != expected) { |
|
3429 |
error = true; |
|
3430 |
errorExcp = new IOException ("insufficient data written"); |
|
3431 |
out.close (); |
|
3432 |
throw errorExcp; |
|
3433 |
} |
|
3434 |
super.flush(); /* can't close the socket */ |
|
3435 |
} else { |
|
3436 |
/* chunked */ |
|
3437 |
super.close (); /* force final chunk to be written */ |
|
3438 |
/* trailing \r\n */ |
|
3439 |
OutputStream o = http.getOutputStream(); |
|
3440 |
o.write ('\r'); |
|
3441 |
o.write ('\n'); |
|
3442 |
o.flush(); |
|
3443 |
} |
|
3444 |
} |
|
3445 |
} |
|
3446 |
||
3447 |
||
3448 |
static class ErrorStream extends InputStream { |
|
3449 |
ByteBuffer buffer; |
|
3450 |
InputStream is; |
|
3451 |
||
3452 |
private ErrorStream(ByteBuffer buf) { |
|
3453 |
buffer = buf; |
|
3454 |
is = null; |
|
3455 |
} |
|
3456 |
||
3457 |
private ErrorStream(ByteBuffer buf, InputStream is) { |
|
3458 |
buffer = buf; |
|
3459 |
this.is = is; |
|
3460 |
} |
|
3461 |
||
3462 |
// when this method is called, it's either the case that cl > 0, or |
|
3463 |
// if chunk-encoded, cl = -1; in other words, cl can't be 0 |
|
3464 |
public static InputStream getErrorStream(InputStream is, long cl, HttpClient http) { |
|
3465 |
||
3466 |
// cl can't be 0; this following is here for extra precaution |
|
3467 |
if (cl == 0) { |
|
3468 |
return null; |
|
3469 |
} |
|
3470 |
||
3471 |
try { |
|
3472 |
// set SO_TIMEOUT to 1/5th of the total timeout |
|
3473 |
// remember the old timeout value so that we can restore it |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
3474 |
int oldTimeout = http.getReadTimeout(); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
3475 |
http.setReadTimeout(timeout4ESBuffer/5); |
2 | 3476 |
|
3477 |
long expected = 0; |
|
3478 |
boolean isChunked = false; |
|
3479 |
// the chunked case |
|
3480 |
if (cl < 0) { |
|
3481 |
expected = bufSize4ES; |
|
3482 |
isChunked = true; |
|
3483 |
} else { |
|
3484 |
expected = cl; |
|
3485 |
} |
|
3486 |
if (expected <= bufSize4ES) { |
|
3487 |
int exp = (int) expected; |
|
3488 |
byte[] buffer = new byte[exp]; |
|
3489 |
int count = 0, time = 0, len = 0; |
|
3490 |
do { |
|
3491 |
try { |
|
3492 |
len = is.read(buffer, count, |
|
3493 |
buffer.length - count); |
|
3494 |
if (len < 0) { |
|
3495 |
if (isChunked) { |
|
3496 |
// chunked ended |
|
3497 |
// if chunked ended prematurely, |
|
3498 |
// an IOException would be thrown |
|
3499 |
break; |
|
3500 |
} |
|
3501 |
// the server sends less than cl bytes of data |
|
3502 |
throw new IOException("the server closes"+ |
|
3503 |
" before sending "+cl+ |
|
3504 |
" bytes of data"); |
|
3505 |
} |
|
3506 |
count += len; |
|
3507 |
} catch (SocketTimeoutException ex) { |
|
3508 |
time += timeout4ESBuffer/5; |
|
3509 |
} |
|
3510 |
} while (count < exp && time < timeout4ESBuffer); |
|
3511 |
||
3512 |
// reset SO_TIMEOUT to old value |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1576
diff
changeset
|
3513 |
http.setReadTimeout(oldTimeout); |
2 | 3514 |
|
3515 |
// if count < cl at this point, we will not try to reuse |
|
3516 |
// the connection |
|
3517 |
if (count == 0) { |
|
3518 |
// since we haven't read anything, |
|
3519 |
// we will return the underlying |
|
3520 |
// inputstream back to the application |
|
3521 |
return null; |
|
3522 |
} else if ((count == expected && !(isChunked)) || (isChunked && len <0)) { |
|
3523 |
// put the connection into keep-alive cache |
|
3524 |
// the inputstream will try to do the right thing |
|
3525 |
is.close(); |
|
3526 |
return new ErrorStream(ByteBuffer.wrap(buffer, 0, count)); |
|
3527 |
} else { |
|
3528 |
// we read part of the response body |
|
3529 |
return new ErrorStream( |
|
3530 |
ByteBuffer.wrap(buffer, 0, count), is); |
|
3531 |
} |
|
3532 |
} |
|
3533 |
return null; |
|
3534 |
} catch (IOException ioex) { |
|
3535 |
// ioex.printStackTrace(); |
|
3536 |
return null; |
|
3537 |
} |
|
3538 |
} |
|
3539 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3540 |
@Override |
2 | 3541 |
public int available() throws IOException { |
3542 |
if (is == null) { |
|
3543 |
return buffer.remaining(); |
|
3544 |
} else { |
|
3545 |
return buffer.remaining()+is.available(); |
|
3546 |
} |
|
3547 |
} |
|
3548 |
||
3549 |
public int read() throws IOException { |
|
3550 |
byte[] b = new byte[1]; |
|
3551 |
int ret = read(b); |
|
3552 |
return (ret == -1? ret : (b[0] & 0x00FF)); |
|
3553 |
} |
|
3554 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3555 |
@Override |
2 | 3556 |
public int read(byte[] b) throws IOException { |
3557 |
return read(b, 0, b.length); |
|
3558 |
} |
|
3559 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3560 |
@Override |
2 | 3561 |
public int read(byte[] b, int off, int len) throws IOException { |
3562 |
int rem = buffer.remaining(); |
|
3563 |
if (rem > 0) { |
|
3564 |
int ret = rem < len? rem : len; |
|
3565 |
buffer.get(b, off, ret); |
|
3566 |
return ret; |
|
3567 |
} else { |
|
3568 |
if (is == null) { |
|
3569 |
return -1; |
|
3570 |
} else { |
|
3571 |
return is.read(b, off, len); |
|
3572 |
} |
|
3573 |
} |
|
3574 |
} |
|
3575 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3576 |
@Override |
2 | 3577 |
public void close() throws IOException { |
3578 |
buffer = null; |
|
3579 |
if (is != null) { |
|
3580 |
is.close(); |
|
3581 |
} |
|
3582 |
} |
|
3583 |
} |
|
3584 |
} |
|
3585 |
||
3586 |
/** An input stream that just returns EOF. This is for |
|
3587 |
* HTTP URLConnections that are KeepAlive && use the |
|
3588 |
* HEAD method - i.e., stream not dead, but nothing to be read. |
|
3589 |
*/ |
|
3590 |
||
3591 |
class EmptyInputStream extends InputStream { |
|
3592 |
||
74
068494063b1b
6641309: Wrong Cookie separator used in HttpURLConnection
jccollet
parents:
2
diff
changeset
|
3593 |
@Override |
2 | 3594 |
public int available() { |
3595 |
return 0; |
|
3596 |
} |
|
3597 |
||
3598 |
public int read() { |
|
3599 |
return -1; |
|
3600 |
} |
|
3601 |
} |