author | chegar |
Sun, 17 Aug 2014 15:54:13 +0100 | |
changeset 25859 | 3317bb8137f4 |
parent 24969 | jdk/src/share/classes/sun/net/www/http/HttpClient.java@afa6934dd8e8 |
child 26720 | 6b160d97c51d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
2 |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.net.www.http; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.net.*; |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
30 |
import java.util.Locale; |
2 | 31 |
import sun.net.NetworkClient; |
32 |
import sun.net.ProgressSource; |
|
33 |
import sun.net.www.MessageHeader; |
|
34 |
import sun.net.www.HeaderParser; |
|
35 |
import sun.net.www.MeteredStream; |
|
36 |
import sun.net.www.ParseUtil; |
|
37 |
import sun.net.www.protocol.http.HttpURLConnection; |
|
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3288
diff
changeset
|
38 |
import sun.util.logging.PlatformLogger; |
14008
b572c1f3a7ad
7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents:
14007
diff
changeset
|
39 |
import static sun.net.www.protocol.http.HttpURLConnection.TunnelState.*; |
2 | 40 |
|
41 |
/** |
|
42 |
* @author Herb Jellinek |
|
43 |
* @author Dave Brown |
|
44 |
*/ |
|
45 |
public class HttpClient extends NetworkClient { |
|
46 |
// whether this httpclient comes from the cache |
|
47 |
protected boolean cachedHttpClient = false; |
|
48 |
||
16499
88276d2da9c0
8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents:
16477
diff
changeset
|
49 |
protected boolean inCache; |
2 | 50 |
|
51 |
// Http requests we send |
|
52 |
MessageHeader requests; |
|
53 |
||
54 |
// Http data we send with the headers |
|
55 |
PosterOutputStream poster = null; |
|
56 |
||
6673
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
57 |
// true if we are in streaming mode (fixed length or chunked) |
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
58 |
boolean streaming; |
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
59 |
|
2 | 60 |
// if we've had one io error |
61 |
boolean failedOnce = false; |
|
62 |
||
63 |
/** Response code for CONTINUE */ |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
64 |
private boolean ignoreContinue = true; |
2 | 65 |
private static final int HTTP_CONTINUE = 100; |
66 |
||
67 |
/** Default port number for http daemons. REMIND: make these private */ |
|
68 |
static final int httpPortNumber = 80; |
|
69 |
||
70 |
/** return default port number (subclasses may override) */ |
|
71 |
protected int getDefaultPort () { return httpPortNumber; } |
|
72 |
||
73 |
static private int getDefaultPort(String proto) { |
|
74 |
if ("http".equalsIgnoreCase(proto)) |
|
75 |
return 80; |
|
76 |
if ("https".equalsIgnoreCase(proto)) |
|
77 |
return 443; |
|
78 |
return -1; |
|
79 |
} |
|
80 |
||
81 |
/* All proxying (generic as well as instance-specific) may be |
|
82 |
* disabled through use of this flag |
|
83 |
*/ |
|
84 |
protected boolean proxyDisabled; |
|
85 |
||
86 |
// are we using proxy in this instance? |
|
87 |
public boolean usingProxy = false; |
|
88 |
// target host, port for the URL |
|
89 |
protected String host; |
|
90 |
protected int port; |
|
91 |
||
92 |
/* where we cache currently open, persistent connections */ |
|
93 |
protected static KeepAliveCache kac = new KeepAliveCache(); |
|
94 |
||
95 |
private static boolean keepAliveProp = true; |
|
96 |
||
97 |
// retryPostProp is true by default so as to preserve behavior |
|
98 |
// from previous releases. |
|
99 |
private static boolean retryPostProp = true; |
|
100 |
||
101 |
volatile boolean keepingAlive = false; /* this is a keep-alive connection */ |
|
102 |
int keepAliveConnections = -1; /* number of keep-alives left */ |
|
103 |
||
104 |
/**Idle timeout value, in milliseconds. Zero means infinity, |
|
105 |
* iff keepingAlive=true. |
|
106 |
* Unfortunately, we can't always believe this one. If I'm connected |
|
107 |
* through a Netscape proxy to a server that sent me a keep-alive |
|
108 |
* time of 15 sec, the proxy unilaterally terminates my connection |
|
109 |
* after 5 sec. So we have to hard code our effective timeout to |
|
110 |
* 4 sec for the case where we're using a proxy. *SIGH* |
|
111 |
*/ |
|
112 |
int keepAliveTimeout = 0; |
|
113 |
||
114 |
/** whether the response is to be cached */ |
|
115 |
private CacheRequest cacheRequest = null; |
|
116 |
||
117 |
/** Url being fetched. */ |
|
118 |
protected URL url; |
|
119 |
||
120 |
/* if set, the client will be reused and must not be put in cache */ |
|
121 |
public boolean reuse = false; |
|
122 |
||
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
123 |
// Traffic capture tool, if configured. See HttpCapture class for info |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
124 |
private HttpCapture capture = null; |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
125 |
|
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
126 |
private static final PlatformLogger logger = HttpURLConnection.getHttpLogger(); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
127 |
private static void logFinest(String msg) { |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
16499
diff
changeset
|
128 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
129 |
logger.finest(msg); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
130 |
} |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
131 |
} |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
132 |
|
2 | 133 |
/** |
134 |
* A NOP method kept for backwards binary compatibility |
|
135 |
* @deprecated -- system properties are no longer cached. |
|
136 |
*/ |
|
137 |
@Deprecated |
|
138 |
public static synchronized void resetProperties() { |
|
139 |
} |
|
140 |
||
141 |
int getKeepAliveTimeout() { |
|
142 |
return keepAliveTimeout; |
|
143 |
} |
|
144 |
||
145 |
static { |
|
146 |
String keepAlive = java.security.AccessController.doPrivileged( |
|
147 |
new sun.security.action.GetPropertyAction("http.keepAlive")); |
|
148 |
||
149 |
String retryPost = java.security.AccessController.doPrivileged( |
|
150 |
new sun.security.action.GetPropertyAction("sun.net.http.retryPost")); |
|
151 |
||
152 |
if (keepAlive != null) { |
|
153 |
keepAliveProp = Boolean.valueOf(keepAlive).booleanValue(); |
|
154 |
} else { |
|
155 |
keepAliveProp = true; |
|
156 |
} |
|
157 |
||
158 |
if (retryPost != null) { |
|
159 |
retryPostProp = Boolean.valueOf(retryPost).booleanValue(); |
|
160 |
} else |
|
161 |
retryPostProp = true; |
|
162 |
||
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* @return true iff http keep alive is set (i.e. enabled). Defaults |
|
167 |
* to true if the system property http.keepAlive isn't set. |
|
168 |
*/ |
|
169 |
public boolean getHttpKeepAliveSet() { |
|
170 |
return keepAliveProp; |
|
171 |
} |
|
172 |
||
173 |
||
174 |
protected HttpClient() { |
|
175 |
} |
|
176 |
||
177 |
private HttpClient(URL url) |
|
178 |
throws IOException { |
|
179 |
this(url, (String)null, -1, false); |
|
180 |
} |
|
181 |
||
182 |
protected HttpClient(URL url, |
|
183 |
boolean proxyDisabled) throws IOException { |
|
184 |
this(url, null, -1, proxyDisabled); |
|
185 |
} |
|
186 |
||
187 |
/* This package-only CTOR should only be used for FTP piggy-backed on HTTP |
|
188 |
* HTTP URL's that use this won't take advantage of keep-alive. |
|
189 |
* Additionally, this constructor may be used as a last resort when the |
|
190 |
* first HttpClient gotten through New() failed (probably b/c of a |
|
191 |
* Keep-Alive mismatch). |
|
192 |
* |
|
193 |
* XXX That documentation is wrong ... it's not package-private any more |
|
194 |
*/ |
|
195 |
public HttpClient(URL url, String proxyHost, int proxyPort) |
|
196 |
throws IOException { |
|
197 |
this(url, proxyHost, proxyPort, false); |
|
198 |
} |
|
199 |
||
200 |
protected HttpClient(URL url, Proxy p, int to) throws IOException { |
|
201 |
proxy = (p == null) ? Proxy.NO_PROXY : p; |
|
202 |
this.host = url.getHost(); |
|
203 |
this.url = url; |
|
204 |
port = url.getPort(); |
|
205 |
if (port == -1) { |
|
206 |
port = getDefaultPort(); |
|
207 |
} |
|
208 |
setConnectTimeout(to); |
|
209 |
||
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
210 |
capture = HttpCapture.getCapture(url); |
2 | 211 |
openServer(); |
212 |
} |
|
213 |
||
214 |
static protected Proxy newHttpProxy(String proxyHost, int proxyPort, |
|
215 |
String proto) { |
|
216 |
if (proxyHost == null || proto == null) |
|
217 |
return Proxy.NO_PROXY; |
|
218 |
int pport = proxyPort < 0 ? getDefaultPort(proto) : proxyPort; |
|
219 |
InetSocketAddress saddr = InetSocketAddress.createUnresolved(proxyHost, pport); |
|
220 |
return new Proxy(Proxy.Type.HTTP, saddr); |
|
221 |
} |
|
222 |
||
223 |
/* |
|
224 |
* This constructor gives "ultimate" flexibility, including the ability |
|
225 |
* to bypass implicit proxying. Sometimes we need to be using tunneling |
|
226 |
* (transport or network level) instead of proxying (application level), |
|
227 |
* for example when we don't want the application level data to become |
|
228 |
* visible to third parties. |
|
229 |
* |
|
230 |
* @param url the URL to which we're connecting |
|
231 |
* @param proxy proxy to use for this URL (e.g. forwarding) |
|
232 |
* @param proxyPort proxy port to use for this URL |
|
233 |
* @param proxyDisabled true to disable default proxying |
|
234 |
*/ |
|
235 |
private HttpClient(URL url, String proxyHost, int proxyPort, |
|
236 |
boolean proxyDisabled) |
|
237 |
throws IOException { |
|
238 |
this(url, proxyDisabled ? Proxy.NO_PROXY : |
|
239 |
newHttpProxy(proxyHost, proxyPort, "http"), -1); |
|
240 |
} |
|
241 |
||
242 |
public HttpClient(URL url, String proxyHost, int proxyPort, |
|
243 |
boolean proxyDisabled, int to) |
|
244 |
throws IOException { |
|
245 |
this(url, proxyDisabled ? Proxy.NO_PROXY : |
|
246 |
newHttpProxy(proxyHost, proxyPort, "http"), to); |
|
247 |
} |
|
248 |
||
249 |
/* This class has no public constructor for HTTP. This method is used to |
|
21278 | 250 |
* get an HttpClient to the specified URL. If there's currently an |
2 | 251 |
* active HttpClient to that server/port, you'll get that one. |
252 |
*/ |
|
253 |
public static HttpClient New(URL url) |
|
254 |
throws IOException { |
|
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
255 |
return HttpClient.New(url, Proxy.NO_PROXY, -1, true, null); |
2 | 256 |
} |
257 |
||
258 |
public static HttpClient New(URL url, boolean useCache) |
|
259 |
throws IOException { |
|
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
260 |
return HttpClient.New(url, Proxy.NO_PROXY, -1, useCache, null); |
2 | 261 |
} |
262 |
||
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
263 |
public static HttpClient New(URL url, Proxy p, int to, boolean useCache, |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
264 |
HttpURLConnection httpuc) throws IOException |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
265 |
{ |
2 | 266 |
if (p == null) { |
267 |
p = Proxy.NO_PROXY; |
|
268 |
} |
|
269 |
HttpClient ret = null; |
|
270 |
/* see if one's already around */ |
|
271 |
if (useCache) { |
|
51 | 272 |
ret = kac.get(url, null); |
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
273 |
if (ret != null && httpuc != null && |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
274 |
httpuc.streaming() && |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
275 |
httpuc.getRequestMethod() == "POST") { |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
276 |
if (!ret.available()) { |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
277 |
ret.inCache = false; |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
278 |
ret.closeServer(); |
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
279 |
ret = null; |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
280 |
} |
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
281 |
} |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
282 |
|
2 | 283 |
if (ret != null) { |
284 |
if ((ret.proxy != null && ret.proxy.equals(p)) || |
|
285 |
(ret.proxy == null && p == null)) { |
|
286 |
synchronized (ret) { |
|
287 |
ret.cachedHttpClient = true; |
|
288 |
assert ret.inCache; |
|
289 |
ret.inCache = false; |
|
14008
b572c1f3a7ad
7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents:
14007
diff
changeset
|
290 |
if (httpuc != null && ret.needsTunneling()) |
b572c1f3a7ad
7199219: Proxy-Connection headers set incorrectly when a HttpClient is retrieved from the Keep Alive Cache
robm
parents:
14007
diff
changeset
|
291 |
httpuc.setTunnelState(TUNNELING); |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
292 |
logFinest("KeepAlive stream retrieved from the cache, " + ret); |
2 | 293 |
} |
294 |
} else { |
|
295 |
// We cannot return this connection to the cache as it's |
|
296 |
// KeepAliveTimeout will get reset. We simply close the connection. |
|
297 |
// This should be fine as it is very rare that a connection |
|
298 |
// to the same host will not use the same proxy. |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
299 |
synchronized(ret) { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
300 |
ret.inCache = false; |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
301 |
ret.closeServer(); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
302 |
} |
2 | 303 |
ret = null; |
304 |
} |
|
305 |
} |
|
306 |
} |
|
307 |
if (ret == null) { |
|
308 |
ret = new HttpClient(url, p, to); |
|
309 |
} else { |
|
310 |
SecurityManager security = System.getSecurityManager(); |
|
311 |
if (security != null) { |
|
9775 | 312 |
if (ret.proxy == Proxy.NO_PROXY || ret.proxy == null) { |
313 |
security.checkConnect(InetAddress.getByName(url.getHost()).getHostAddress(), url.getPort()); |
|
314 |
} else { |
|
315 |
security.checkConnect(url.getHost(), url.getPort()); |
|
316 |
} |
|
2 | 317 |
} |
318 |
ret.url = url; |
|
319 |
} |
|
320 |
return ret; |
|
321 |
} |
|
322 |
||
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
323 |
public static HttpClient New(URL url, Proxy p, int to, |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
324 |
HttpURLConnection httpuc) throws IOException |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
325 |
{ |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
326 |
return New(url, p, to, true, httpuc); |
2 | 327 |
} |
328 |
||
329 |
public static HttpClient New(URL url, String proxyHost, int proxyPort, |
|
330 |
boolean useCache) |
|
331 |
throws IOException { |
|
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
332 |
return New(url, newHttpProxy(proxyHost, proxyPort, "http"), |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
333 |
-1, useCache, null); |
2 | 334 |
} |
335 |
||
336 |
public static HttpClient New(URL url, String proxyHost, int proxyPort, |
|
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
337 |
boolean useCache, int to, |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
338 |
HttpURLConnection httpuc) |
2 | 339 |
throws IOException { |
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
340 |
return New(url, newHttpProxy(proxyHost, proxyPort, "http"), |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
341 |
to, useCache, httpuc); |
2 | 342 |
} |
343 |
||
344 |
/* return it to the cache as still usable, if: |
|
345 |
* 1) It's keeping alive, AND |
|
346 |
* 2) It still has some connections left, AND |
|
347 |
* 3) It hasn't had a error (PrintStream.checkError()) |
|
348 |
* 4) It hasn't timed out |
|
349 |
* |
|
350 |
* If this client is not keepingAlive, it should have been |
|
351 |
* removed from the cache in the parseHeaders() method. |
|
352 |
*/ |
|
353 |
||
354 |
public void finished() { |
|
355 |
if (reuse) /* will be reused */ |
|
356 |
return; |
|
357 |
keepAliveConnections--; |
|
358 |
poster = null; |
|
359 |
if (keepAliveConnections > 0 && isKeepingAlive() && |
|
360 |
!(serverOutput.checkError())) { |
|
361 |
/* This connection is keepingAlive && still valid. |
|
362 |
* Return it to the cache. |
|
363 |
*/ |
|
364 |
putInKeepAliveCache(); |
|
365 |
} else { |
|
366 |
closeServer(); |
|
367 |
} |
|
368 |
} |
|
369 |
||
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
370 |
protected synchronized boolean available() { |
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
371 |
boolean available = true; |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
372 |
int old = -1; |
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
373 |
|
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
374 |
try { |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
375 |
try { |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
376 |
old = serverSocket.getSoTimeout(); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
377 |
serverSocket.setSoTimeout(1); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
378 |
BufferedInputStream tmpbuf = |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
379 |
new BufferedInputStream(serverSocket.getInputStream()); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
380 |
int r = tmpbuf.read(); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
381 |
if (r == -1) { |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
382 |
logFinest("HttpClient.available(): " + |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
383 |
"read returned -1: not available"); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
384 |
available = false; |
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
385 |
} |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
386 |
} catch (SocketTimeoutException e) { |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
387 |
logFinest("HttpClient.available(): " + |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
388 |
"SocketTimeout: its available"); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
389 |
} finally { |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
390 |
if (old != -1) |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
391 |
serverSocket.setSoTimeout(old); |
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
392 |
} |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
393 |
} catch (IOException e) { |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
394 |
logFinest("HttpClient.available(): " + |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
395 |
"SocketException: not available"); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
396 |
available = false; |
14007
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
397 |
} |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
398 |
return available; |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
399 |
} |
2fde9d6d874a
7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
robm
parents:
11528
diff
changeset
|
400 |
|
2 | 401 |
protected synchronized void putInKeepAliveCache() { |
402 |
if (inCache) { |
|
403 |
assert false : "Duplicate put to keep alive cache"; |
|
404 |
return; |
|
405 |
} |
|
406 |
inCache = true; |
|
407 |
kac.put(url, null, this); |
|
408 |
} |
|
409 |
||
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
410 |
protected synchronized boolean isInKeepAliveCache() { |
2 | 411 |
return inCache; |
412 |
} |
|
413 |
||
414 |
/* |
|
415 |
* Close an idle connection to this URL (if it exists in the |
|
416 |
* cache). |
|
417 |
*/ |
|
418 |
public void closeIdleConnection() { |
|
51 | 419 |
HttpClient http = kac.get(url, null); |
2 | 420 |
if (http != null) { |
421 |
http.closeServer(); |
|
422 |
} |
|
423 |
} |
|
424 |
||
425 |
/* We're very particular here about what our InputStream to the server |
|
426 |
* looks like for reasons that are apparent if you can decipher the |
|
427 |
* method parseHTTP(). That's why this method is overidden from the |
|
428 |
* superclass. |
|
429 |
*/ |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
430 |
@Override |
2 | 431 |
public void openServer(String server, int port) throws IOException { |
432 |
serverSocket = doConnect(server, port); |
|
433 |
try { |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
434 |
OutputStream out = serverSocket.getOutputStream(); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
435 |
if (capture != null) { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
436 |
out = new HttpCaptureOutputStream(out, capture); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
437 |
} |
2 | 438 |
serverOutput = new PrintStream( |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
439 |
new BufferedOutputStream(out), |
2 | 440 |
false, encoding); |
441 |
} catch (UnsupportedEncodingException e) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10136
diff
changeset
|
442 |
throw new InternalError(encoding+" encoding not found", e); |
2 | 443 |
} |
444 |
serverSocket.setTcpNoDelay(true); |
|
445 |
} |
|
446 |
||
447 |
/* |
|
448 |
* Returns true if the http request should be tunneled through proxy. |
|
449 |
* An example where this is the case is Https. |
|
450 |
*/ |
|
451 |
public boolean needsTunneling() { |
|
452 |
return false; |
|
453 |
} |
|
454 |
||
455 |
/* |
|
456 |
* Returns true if this httpclient is from cache |
|
457 |
*/ |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
458 |
public synchronized boolean isCachedConnection() { |
2 | 459 |
return cachedHttpClient; |
460 |
} |
|
461 |
||
462 |
/* |
|
463 |
* Finish any work left after the socket connection is |
|
464 |
* established. In the normal http case, it's a NO-OP. Subclass |
|
465 |
* may need to override this. An example is Https, where for |
|
466 |
* direct connection to the origin server, ssl handshake needs to |
|
467 |
* be done; for proxy tunneling, the socket needs to be converted |
|
468 |
* into an SSL socket before ssl handshake can take place. |
|
469 |
*/ |
|
470 |
public void afterConnect() throws IOException, UnknownHostException { |
|
471 |
// NO-OP. Needs to be overwritten by HttpsClient |
|
472 |
} |
|
473 |
||
474 |
/* |
|
475 |
* call openServer in a privileged block |
|
476 |
*/ |
|
477 |
private synchronized void privilegedOpenServer(final InetSocketAddress server) |
|
478 |
throws IOException |
|
479 |
{ |
|
480 |
try { |
|
481 |
java.security.AccessController.doPrivileged( |
|
51 | 482 |
new java.security.PrivilegedExceptionAction<Void>() { |
483 |
public Void run() throws IOException { |
|
2 | 484 |
openServer(server.getHostString(), server.getPort()); |
485 |
return null; |
|
486 |
} |
|
487 |
}); |
|
488 |
} catch (java.security.PrivilegedActionException pae) { |
|
489 |
throw (IOException) pae.getException(); |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
/* |
|
494 |
* call super.openServer |
|
495 |
*/ |
|
496 |
private void superOpenServer(final String proxyHost, |
|
497 |
final int proxyPort) |
|
498 |
throws IOException, UnknownHostException |
|
499 |
{ |
|
500 |
super.openServer(proxyHost, proxyPort); |
|
501 |
} |
|
502 |
||
503 |
/* |
|
504 |
*/ |
|
505 |
protected synchronized void openServer() throws IOException { |
|
506 |
||
507 |
SecurityManager security = System.getSecurityManager(); |
|
9775 | 508 |
|
9550
c3a275ce56d3
7041044: InetAddress.getByName(String,InetAddress) added in error
michaelm
parents:
9544
diff
changeset
|
509 |
if (security != null) { |
c3a275ce56d3
7041044: InetAddress.getByName(String,InetAddress) added in error
michaelm
parents:
9544
diff
changeset
|
510 |
security.checkConnect(host, port); |
c3a275ce56d3
7041044: InetAddress.getByName(String,InetAddress) added in error
michaelm
parents:
9544
diff
changeset
|
511 |
} |
2 | 512 |
|
513 |
if (keepingAlive) { // already opened |
|
514 |
return; |
|
515 |
} |
|
516 |
||
517 |
if (url.getProtocol().equals("http") || |
|
518 |
url.getProtocol().equals("https") ) { |
|
519 |
||
520 |
if ((proxy != null) && (proxy.type() == Proxy.Type.HTTP)) { |
|
9775 | 521 |
sun.net.www.URLConnection.setProxiedHost(host); |
2 | 522 |
privilegedOpenServer((InetSocketAddress) proxy.address()); |
523 |
usingProxy = true; |
|
524 |
return; |
|
525 |
} else { |
|
526 |
// make direct connection |
|
527 |
openServer(host, port); |
|
528 |
usingProxy = false; |
|
529 |
return; |
|
530 |
} |
|
531 |
||
532 |
} else { |
|
533 |
/* we're opening some other kind of url, most likely an |
|
534 |
* ftp url. |
|
535 |
*/ |
|
536 |
if ((proxy != null) && (proxy.type() == Proxy.Type.HTTP)) { |
|
9775 | 537 |
sun.net.www.URLConnection.setProxiedHost(host); |
2 | 538 |
privilegedOpenServer((InetSocketAddress) proxy.address()); |
539 |
usingProxy = true; |
|
540 |
return; |
|
541 |
} else { |
|
542 |
// make direct connection |
|
543 |
super.openServer(host, port); |
|
544 |
usingProxy = false; |
|
545 |
return; |
|
546 |
} |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
public String getURLFile() throws IOException { |
|
551 |
||
18294
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
552 |
String fileName; |
2 | 553 |
|
554 |
/** |
|
555 |
* proxyDisabled is set by subclass HttpsClient! |
|
556 |
*/ |
|
557 |
if (usingProxy && !proxyDisabled) { |
|
558 |
// Do not use URLStreamHandler.toExternalForm as the fragment |
|
559 |
// should not be part of the RequestURI. It should be an |
|
560 |
// absolute URI which does not have a fragment part. |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
24045
diff
changeset
|
561 |
StringBuilder result = new StringBuilder(128); |
2 | 562 |
result.append(url.getProtocol()); |
563 |
result.append(":"); |
|
564 |
if (url.getAuthority() != null && url.getAuthority().length() > 0) { |
|
565 |
result.append("//"); |
|
566 |
result.append(url.getAuthority()); |
|
567 |
} |
|
568 |
if (url.getPath() != null) { |
|
569 |
result.append(url.getPath()); |
|
570 |
} |
|
571 |
if (url.getQuery() != null) { |
|
572 |
result.append('?'); |
|
573 |
result.append(url.getQuery()); |
|
574 |
} |
|
575 |
||
18294
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
576 |
fileName = result.toString(); |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
577 |
} else { |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
578 |
fileName = url.getFile(); |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
579 |
|
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
580 |
if ((fileName == null) || (fileName.length() == 0)) { |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
581 |
fileName = "/"; |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
582 |
} else if (fileName.charAt(0) == '?') { |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
583 |
/* HTTP/1.1 spec says in 5.1.2. about Request-URI: |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
584 |
* "Note that the absolute path cannot be empty; if |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
585 |
* none is present in the original URI, it MUST be |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
586 |
* given as "/" (the server root)." So if the file |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
587 |
* name here has only a query string, the path is |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
588 |
* empty and we also have to add a "/". |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
589 |
*/ |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
590 |
fileName = "/" + fileName; |
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
591 |
} |
2 | 592 |
} |
18294
364814dc7391
7025238: HttpURLConnection does not handle URLs with an empty path component.
arieber
parents:
18178
diff
changeset
|
593 |
|
2 | 594 |
if (fileName.indexOf('\n') == -1) |
595 |
return fileName; |
|
596 |
else |
|
597 |
throw new java.net.MalformedURLException("Illegal character in URL"); |
|
598 |
} |
|
599 |
||
600 |
/** |
|
601 |
* @deprecated |
|
602 |
*/ |
|
603 |
@Deprecated |
|
604 |
public void writeRequests(MessageHeader head) { |
|
605 |
requests = head; |
|
606 |
requests.print(serverOutput); |
|
607 |
serverOutput.flush(); |
|
608 |
} |
|
609 |
||
610 |
public void writeRequests(MessageHeader head, |
|
611 |
PosterOutputStream pos) throws IOException { |
|
612 |
requests = head; |
|
613 |
requests.print(serverOutput); |
|
614 |
poster = pos; |
|
615 |
if (poster != null) |
|
616 |
poster.writeTo(serverOutput); |
|
617 |
serverOutput.flush(); |
|
618 |
} |
|
619 |
||
6673
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
620 |
public void writeRequests(MessageHeader head, |
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
621 |
PosterOutputStream pos, |
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
622 |
boolean streaming) throws IOException { |
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
623 |
this.streaming = streaming; |
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
624 |
writeRequests(head, pos); |
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
625 |
} |
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
626 |
|
2 | 627 |
/** Parse the first line of the HTTP request. It usually looks |
628 |
something like: "HTTP/1.0 <number> comment\r\n". */ |
|
629 |
||
630 |
public boolean parseHTTP(MessageHeader responses, ProgressSource pi, HttpURLConnection httpuc) |
|
631 |
throws IOException { |
|
632 |
/* If "HTTP/*" is found in the beginning, return true. Let |
|
633 |
* HttpURLConnection parse the mime header itself. |
|
634 |
* |
|
635 |
* If this isn't valid HTTP, then we don't try to parse a header |
|
636 |
* out of the beginning of the response into the responses, |
|
637 |
* and instead just queue up the output stream to it's very beginning. |
|
638 |
* This seems most reasonable, and is what the NN browser does. |
|
639 |
*/ |
|
640 |
||
641 |
try { |
|
642 |
serverInput = serverSocket.getInputStream(); |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
643 |
if (capture != null) { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
644 |
serverInput = new HttpCaptureInputStream(serverInput, capture); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
645 |
} |
2 | 646 |
serverInput = new BufferedInputStream(serverInput); |
647 |
return (parseHTTPHeader(responses, pi, httpuc)); |
|
648 |
} catch (SocketTimeoutException stex) { |
|
649 |
// We don't want to retry the request when the app. sets a timeout |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
650 |
// but don't close the server if timeout while waiting for 100-continue |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
651 |
if (ignoreContinue) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
652 |
closeServer(); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
653 |
} |
2 | 654 |
throw stex; |
655 |
} catch (IOException e) { |
|
656 |
closeServer(); |
|
657 |
cachedHttpClient = false; |
|
658 |
if (!failedOnce && requests != null) { |
|
6673
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
659 |
failedOnce = true; |
10136
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
660 |
if (getRequestMethod().equals("CONNECT") || |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
661 |
(httpuc.getRequestMethod().equals("POST") && |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
662 |
(!retryPostProp || streaming))) { |
2 | 663 |
// do not retry the request |
664 |
} else { |
|
665 |
// try once more |
|
666 |
openServer(); |
|
667 |
if (needsTunneling()) { |
|
24045
31a23ee57590
8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
21278
diff
changeset
|
668 |
MessageHeader origRequests = requests; |
2 | 669 |
httpuc.doTunneling(); |
24045
31a23ee57590
8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
21278
diff
changeset
|
670 |
requests = origRequests; |
2 | 671 |
} |
672 |
afterConnect(); |
|
673 |
writeRequests(requests, poster); |
|
674 |
return parseHTTP(responses, pi, httpuc); |
|
675 |
} |
|
676 |
} |
|
677 |
throw e; |
|
678 |
} |
|
679 |
||
680 |
} |
|
681 |
||
682 |
private boolean parseHTTPHeader(MessageHeader responses, ProgressSource pi, HttpURLConnection httpuc) |
|
683 |
throws IOException { |
|
684 |
/* If "HTTP/*" is found in the beginning, return true. Let |
|
685 |
* HttpURLConnection parse the mime header itself. |
|
686 |
* |
|
687 |
* If this isn't valid HTTP, then we don't try to parse a header |
|
688 |
* out of the beginning of the response into the responses, |
|
689 |
* and instead just queue up the output stream to it's very beginning. |
|
690 |
* This seems most reasonable, and is what the NN browser does. |
|
691 |
*/ |
|
692 |
||
693 |
keepAliveConnections = -1; |
|
694 |
keepAliveTimeout = 0; |
|
695 |
||
696 |
boolean ret = false; |
|
697 |
byte[] b = new byte[8]; |
|
698 |
||
699 |
try { |
|
700 |
int nread = 0; |
|
701 |
serverInput.mark(10); |
|
702 |
while (nread < 8) { |
|
703 |
int r = serverInput.read(b, nread, 8 - nread); |
|
704 |
if (r < 0) { |
|
705 |
break; |
|
706 |
} |
|
707 |
nread += r; |
|
708 |
} |
|
709 |
String keep=null; |
|
710 |
ret = b[0] == 'H' && b[1] == 'T' |
|
711 |
&& b[2] == 'T' && b[3] == 'P' && b[4] == '/' && |
|
712 |
b[5] == '1' && b[6] == '.'; |
|
713 |
serverInput.reset(); |
|
714 |
if (ret) { // is valid HTTP - response started w/ "HTTP/1." |
|
715 |
responses.parseHeader(serverInput); |
|
716 |
||
717 |
// we've finished parsing http headers |
|
718 |
// check if there are any applicable cookies to set (in cache) |
|
11528
638ca25aec87
7129083: CookieManager does not store cookies if url is read before setting cookie manager
chegar
parents:
10419
diff
changeset
|
719 |
CookieHandler cookieHandler = httpuc.getCookieHandler(); |
2 | 720 |
if (cookieHandler != null) { |
721 |
URI uri = ParseUtil.toURI(url); |
|
722 |
// NOTE: That cast from Map shouldn't be necessary but |
|
723 |
// a bug in javac is triggered under certain circumstances |
|
724 |
// So we do put the cast in as a workaround until |
|
725 |
// it is resolved. |
|
726 |
if (uri != null) |
|
51 | 727 |
cookieHandler.put(uri, responses.getHeaders()); |
2 | 728 |
} |
729 |
||
730 |
/* decide if we're keeping alive: |
|
731 |
* This is a bit tricky. There's a spec, but most current |
|
732 |
* servers (10/1/96) that support this differ in dialects. |
|
733 |
* If the server/client misunderstand each other, the |
|
734 |
* protocol should fall back onto HTTP/1.0, no keep-alive. |
|
735 |
*/ |
|
736 |
if (usingProxy) { // not likely a proxy will return this |
|
737 |
keep = responses.findValue("Proxy-Connection"); |
|
738 |
} |
|
739 |
if (keep == null) { |
|
740 |
keep = responses.findValue("Connection"); |
|
741 |
} |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
742 |
if (keep != null && keep.toLowerCase(Locale.US).equals("keep-alive")) { |
2 | 743 |
/* some servers, notably Apache1.1, send something like: |
744 |
* "Keep-Alive: timeout=15, max=1" which we should respect. |
|
745 |
*/ |
|
746 |
HeaderParser p = new HeaderParser( |
|
747 |
responses.findValue("Keep-Alive")); |
|
748 |
if (p != null) { |
|
749 |
/* default should be larger in case of proxy */ |
|
750 |
keepAliveConnections = p.findInt("max", usingProxy?50:5); |
|
751 |
keepAliveTimeout = p.findInt("timeout", usingProxy?60:5); |
|
752 |
} |
|
753 |
} else if (b[7] != '0') { |
|
754 |
/* |
|
755 |
* We're talking 1.1 or later. Keep persistent until |
|
756 |
* the server says to close. |
|
757 |
*/ |
|
758 |
if (keep != null) { |
|
759 |
/* |
|
760 |
* The only Connection token we understand is close. |
|
761 |
* Paranoia: if there is any Connection header then |
|
762 |
* treat as non-persistent. |
|
763 |
*/ |
|
764 |
keepAliveConnections = 1; |
|
765 |
} else { |
|
766 |
keepAliveConnections = 5; |
|
767 |
} |
|
768 |
} |
|
769 |
} else if (nread != 8) { |
|
770 |
if (!failedOnce && requests != null) { |
|
6673
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
771 |
failedOnce = true; |
10136
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
772 |
if (getRequestMethod().equals("CONNECT") || |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
773 |
(httpuc.getRequestMethod().equals("POST") && |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
774 |
(!retryPostProp || streaming))) { |
2 | 775 |
// do not retry the request |
776 |
} else { |
|
777 |
closeServer(); |
|
778 |
cachedHttpClient = false; |
|
779 |
openServer(); |
|
780 |
if (needsTunneling()) { |
|
24045
31a23ee57590
8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
21278
diff
changeset
|
781 |
MessageHeader origRequests = requests; |
2 | 782 |
httpuc.doTunneling(); |
24045
31a23ee57590
8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
21278
diff
changeset
|
783 |
requests = origRequests; |
2 | 784 |
} |
785 |
afterConnect(); |
|
786 |
writeRequests(requests, poster); |
|
787 |
return parseHTTP(responses, pi, httpuc); |
|
788 |
} |
|
789 |
} |
|
790 |
throw new SocketException("Unexpected end of file from server"); |
|
791 |
} else { |
|
792 |
// we can't vouche for what this is.... |
|
793 |
responses.set("Content-type", "unknown/unknown"); |
|
794 |
} |
|
795 |
} catch (IOException e) { |
|
796 |
throw e; |
|
797 |
} |
|
798 |
||
799 |
int code = -1; |
|
800 |
try { |
|
801 |
String resp; |
|
802 |
resp = responses.getValue(0); |
|
803 |
/* should have no leading/trailing LWS |
|
804 |
* expedite the typical case by assuming it has |
|
805 |
* form "HTTP/1.x <WS> 2XX <mumble>" |
|
806 |
*/ |
|
807 |
int ind; |
|
808 |
ind = resp.indexOf(' '); |
|
809 |
while(resp.charAt(ind) == ' ') |
|
810 |
ind++; |
|
811 |
code = Integer.parseInt(resp.substring(ind, ind + 3)); |
|
812 |
} catch (Exception e) {} |
|
813 |
||
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
814 |
if (code == HTTP_CONTINUE && ignoreContinue) { |
2 | 815 |
responses.reset(); |
816 |
return parseHTTPHeader(responses, pi, httpuc); |
|
817 |
} |
|
818 |
||
819 |
long cl = -1; |
|
820 |
||
821 |
/* |
|
822 |
* Set things up to parse the entity body of the reply. |
|
823 |
* We should be smarter about avoid pointless work when |
|
824 |
* the HTTP method and response code indicate there will be |
|
825 |
* no entity body to parse. |
|
826 |
*/ |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
827 |
String te = responses.findValue("Transfer-Encoding"); |
2 | 828 |
if (te != null && te.equalsIgnoreCase("chunked")) { |
829 |
serverInput = new ChunkedInputStream(serverInput, this, responses); |
|
830 |
||
831 |
/* |
|
832 |
* If keep alive not specified then close after the stream |
|
833 |
* has completed. |
|
834 |
*/ |
|
835 |
if (keepAliveConnections <= 1) { |
|
836 |
keepAliveConnections = 1; |
|
837 |
keepingAlive = false; |
|
838 |
} else { |
|
839 |
keepingAlive = true; |
|
840 |
} |
|
841 |
failedOnce = false; |
|
842 |
} else { |
|
843 |
||
844 |
/* |
|
845 |
* If it's a keep alive connection then we will keep |
|
846 |
* (alive if :- |
|
847 |
* 1. content-length is specified, or |
|
848 |
* 2. "Not-Modified" or "No-Content" responses - RFC 2616 states that |
|
849 |
* 204 or 304 response must not include a message body. |
|
850 |
*/ |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
851 |
String cls = responses.findValue("content-length"); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
852 |
if (cls != null) { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
853 |
try { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
854 |
cl = Long.parseLong(cls); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
855 |
} catch (NumberFormatException e) { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
856 |
cl = -1; |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
857 |
} |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
858 |
} |
2 | 859 |
String requestLine = requests.getKey(0); |
860 |
||
861 |
if ((requestLine != null && |
|
862 |
(requestLine.startsWith("HEAD"))) || |
|
863 |
code == HttpURLConnection.HTTP_NOT_MODIFIED || |
|
864 |
code == HttpURLConnection.HTTP_NO_CONTENT) { |
|
865 |
cl = 0; |
|
866 |
} |
|
867 |
||
868 |
if (keepAliveConnections > 1 && |
|
869 |
(cl >= 0 || |
|
870 |
code == HttpURLConnection.HTTP_NOT_MODIFIED || |
|
871 |
code == HttpURLConnection.HTTP_NO_CONTENT)) { |
|
872 |
keepingAlive = true; |
|
873 |
failedOnce = false; |
|
874 |
} else if (keepingAlive) { |
|
875 |
/* Previously we were keeping alive, and now we're not. Remove |
|
876 |
* this from the cache (but only here, once) - otherwise we get |
|
877 |
* multiple removes and the cache count gets messed up. |
|
878 |
*/ |
|
879 |
keepingAlive=false; |
|
880 |
} |
|
881 |
} |
|
882 |
||
883 |
/* wrap a KeepAliveStream/MeteredStream around it if appropriate */ |
|
884 |
||
885 |
if (cl > 0) { |
|
886 |
// In this case, content length is well known, so it is okay |
|
887 |
// to wrap the input stream with KeepAliveStream/MeteredStream. |
|
888 |
||
889 |
if (pi != null) { |
|
890 |
// Progress monitor is enabled |
|
891 |
pi.setContentType(responses.findValue("content-type")); |
|
892 |
} |
|
893 |
||
894 |
if (isKeepingAlive()) { |
|
895 |
// Wrap KeepAliveStream if keep alive is enabled. |
|
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
896 |
logFinest("KeepAlive stream used: " + url); |
2 | 897 |
serverInput = new KeepAliveStream(serverInput, pi, cl, this); |
898 |
failedOnce = false; |
|
899 |
} |
|
900 |
else { |
|
901 |
serverInput = new MeteredStream(serverInput, pi, cl); |
|
902 |
} |
|
903 |
} |
|
904 |
else if (cl == -1) { |
|
905 |
// In this case, content length is unknown - the input |
|
906 |
// stream would simply be a regular InputStream or |
|
907 |
// ChunkedInputStream. |
|
908 |
||
909 |
if (pi != null) { |
|
910 |
// Progress monitoring is enabled. |
|
911 |
||
912 |
pi.setContentType(responses.findValue("content-type")); |
|
913 |
||
914 |
// Wrap MeteredStream for tracking indeterministic |
|
915 |
// progress, even if the input stream is ChunkedInputStream. |
|
916 |
serverInput = new MeteredStream(serverInput, pi, cl); |
|
917 |
} |
|
918 |
else { |
|
919 |
// Progress monitoring is disabled, and there is no |
|
920 |
// need to wrap an unknown length input stream. |
|
921 |
||
922 |
// ** This is an no-op ** |
|
923 |
} |
|
924 |
} |
|
925 |
else { |
|
926 |
if (pi != null) |
|
927 |
pi.finishTracking(); |
|
928 |
} |
|
929 |
||
930 |
return ret; |
|
931 |
} |
|
932 |
||
933 |
public synchronized InputStream getInputStream() { |
|
934 |
return serverInput; |
|
935 |
} |
|
936 |
||
937 |
public OutputStream getOutputStream() { |
|
938 |
return serverOutput; |
|
939 |
} |
|
940 |
||
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
941 |
@Override |
2 | 942 |
public String toString() { |
943 |
return getClass().getName()+"("+url+")"; |
|
944 |
} |
|
945 |
||
946 |
public final boolean isKeepingAlive() { |
|
947 |
return getHttpKeepAliveSet() && keepingAlive; |
|
948 |
} |
|
949 |
||
950 |
public void setCacheRequest(CacheRequest cacheRequest) { |
|
951 |
this.cacheRequest = cacheRequest; |
|
952 |
} |
|
953 |
||
954 |
CacheRequest getCacheRequest() { |
|
955 |
return cacheRequest; |
|
956 |
} |
|
957 |
||
10136
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
958 |
String getRequestMethod() { |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
959 |
if (requests != null) { |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
960 |
String requestLine = requests.getKey(0); |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
961 |
if (requestLine != null) { |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
962 |
return requestLine.split("\\s+")[0]; |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
963 |
} |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
964 |
} |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
965 |
return ""; |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
966 |
} |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
967 |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
968 |
@Override |
2 | 969 |
protected void finalize() throws Throwable { |
970 |
// This should do nothing. The stream finalizer will |
|
971 |
// close the fd. |
|
972 |
} |
|
973 |
||
974 |
public void setDoNotRetry(boolean value) { |
|
975 |
// failedOnce is used to determine if a request should be retried. |
|
976 |
failedOnce = value; |
|
977 |
} |
|
978 |
||
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
979 |
public void setIgnoreContinue(boolean value) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
980 |
ignoreContinue = value; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
981 |
} |
2 | 982 |
|
983 |
/* Use only on connections in error. */ |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
984 |
@Override |
2 | 985 |
public void closeServer() { |
986 |
try { |
|
987 |
keepingAlive = false; |
|
988 |
serverSocket.close(); |
|
989 |
} catch (Exception e) {} |
|
990 |
} |
|
991 |
||
992 |
/** |
|
993 |
* @return the proxy host being used for this client, or null |
|
994 |
* if we're not going through a proxy |
|
995 |
*/ |
|
996 |
public String getProxyHostUsed() { |
|
997 |
if (!usingProxy) { |
|
998 |
return null; |
|
999 |
} else { |
|
1000 |
return ((InetSocketAddress)proxy.address()).getHostString(); |
|
1001 |
} |
|
1002 |
} |
|
1003 |
||
1004 |
/** |
|
1005 |
* @return the proxy port being used for this client. Meaningless |
|
1006 |
* if getProxyHostUsed() gives null. |
|
1007 |
*/ |
|
1008 |
public int getProxyPortUsed() { |
|
1009 |
if (usingProxy) |
|
1010 |
return ((InetSocketAddress)proxy.address()).getPort(); |
|
1011 |
return -1; |
|
1012 |
} |
|
1013 |
} |