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