author | redestad |
Tue, 03 May 2016 15:50:54 +0200 | |
changeset 37781 | 71ed5645f17c |
parent 37593 | 824750ada3d6 |
child 42351 | 85ed90be0ae1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
34943 | 2 |
* Copyright (c) 1994, 2016, 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; |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
34943
diff
changeset
|
31 |
import java.util.Properties; |
2 | 32 |
import sun.net.NetworkClient; |
33 |
import sun.net.ProgressSource; |
|
34 |
import sun.net.www.MessageHeader; |
|
35 |
import sun.net.www.HeaderParser; |
|
36 |
import sun.net.www.MeteredStream; |
|
37 |
import sun.net.www.ParseUtil; |
|
38 |
import sun.net.www.protocol.http.HttpURLConnection; |
|
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3288
diff
changeset
|
39 |
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
|
40 |
import static sun.net.www.protocol.http.HttpURLConnection.TunnelState.*; |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
34943
diff
changeset
|
41 |
import sun.security.action.GetPropertyAction; |
2 | 42 |
|
43 |
/** |
|
44 |
* @author Herb Jellinek |
|
45 |
* @author Dave Brown |
|
46 |
*/ |
|
47 |
public class HttpClient extends NetworkClient { |
|
48 |
// whether this httpclient comes from the cache |
|
49 |
protected boolean cachedHttpClient = false; |
|
50 |
||
16499
88276d2da9c0
8009251: Add proxy handling and keep-alive fixes to jsse
robm
parents:
16477
diff
changeset
|
51 |
protected boolean inCache; |
2 | 52 |
|
53 |
// Http requests we send |
|
54 |
MessageHeader requests; |
|
55 |
||
56 |
// Http data we send with the headers |
|
57 |
PosterOutputStream poster = null; |
|
58 |
||
6673
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
59 |
// 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
|
60 |
boolean streaming; |
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
61 |
|
2 | 62 |
// if we've had one io error |
63 |
boolean failedOnce = false; |
|
64 |
||
65 |
/** Response code for CONTINUE */ |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
66 |
private boolean ignoreContinue = true; |
2 | 67 |
private static final int HTTP_CONTINUE = 100; |
68 |
||
69 |
/** Default port number for http daemons. REMIND: make these private */ |
|
70 |
static final int httpPortNumber = 80; |
|
71 |
||
72 |
/** return default port number (subclasses may override) */ |
|
73 |
protected int getDefaultPort () { return httpPortNumber; } |
|
74 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31061
diff
changeset
|
75 |
private static int getDefaultPort(String proto) { |
2 | 76 |
if ("http".equalsIgnoreCase(proto)) |
77 |
return 80; |
|
78 |
if ("https".equalsIgnoreCase(proto)) |
|
79 |
return 443; |
|
80 |
return -1; |
|
81 |
} |
|
82 |
||
83 |
/* All proxying (generic as well as instance-specific) may be |
|
84 |
* disabled through use of this flag |
|
85 |
*/ |
|
86 |
protected boolean proxyDisabled; |
|
87 |
||
88 |
// are we using proxy in this instance? |
|
89 |
public boolean usingProxy = false; |
|
90 |
// target host, port for the URL |
|
91 |
protected String host; |
|
92 |
protected int port; |
|
93 |
||
94 |
/* where we cache currently open, persistent connections */ |
|
95 |
protected static KeepAliveCache kac = new KeepAliveCache(); |
|
96 |
||
97 |
private static boolean keepAliveProp = true; |
|
98 |
||
99 |
// retryPostProp is true by default so as to preserve behavior |
|
100 |
// from previous releases. |
|
101 |
private static boolean retryPostProp = true; |
|
102 |
||
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
32649
diff
changeset
|
103 |
volatile boolean keepingAlive; /* this is a keep-alive connection */ |
2 | 104 |
int keepAliveConnections = -1; /* number of keep-alives left */ |
105 |
||
106 |
/**Idle timeout value, in milliseconds. Zero means infinity, |
|
107 |
* iff keepingAlive=true. |
|
108 |
* Unfortunately, we can't always believe this one. If I'm connected |
|
109 |
* through a Netscape proxy to a server that sent me a keep-alive |
|
110 |
* time of 15 sec, the proxy unilaterally terminates my connection |
|
111 |
* after 5 sec. So we have to hard code our effective timeout to |
|
112 |
* 4 sec for the case where we're using a proxy. *SIGH* |
|
113 |
*/ |
|
114 |
int keepAliveTimeout = 0; |
|
115 |
||
116 |
/** whether the response is to be cached */ |
|
117 |
private CacheRequest cacheRequest = null; |
|
118 |
||
119 |
/** Url being fetched. */ |
|
120 |
protected URL url; |
|
121 |
||
122 |
/* if set, the client will be reused and must not be put in cache */ |
|
123 |
public boolean reuse = false; |
|
124 |
||
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
125 |
// 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
|
126 |
private HttpCapture capture = null; |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
127 |
|
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
128 |
private static final PlatformLogger logger = HttpURLConnection.getHttpLogger(); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
129 |
private static void logFinest(String msg) { |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
16499
diff
changeset
|
130 |
if (logger.isLoggable(PlatformLogger.Level.FINEST)) { |
16477
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
131 |
logger.finest(msg); |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
132 |
} |
15ee12718efe
8009650: HttpClient available() check throws SocketException when connection has been closed
robm
parents:
14008
diff
changeset
|
133 |
} |
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
134 |
|
2 | 135 |
/** |
136 |
* A NOP method kept for backwards binary compatibility |
|
137 |
* @deprecated -- system properties are no longer cached. |
|
138 |
*/ |
|
139 |
@Deprecated |
|
140 |
public static synchronized void resetProperties() { |
|
141 |
} |
|
142 |
||
143 |
int getKeepAliveTimeout() { |
|
144 |
return keepAliveTimeout; |
|
145 |
} |
|
146 |
||
147 |
static { |
|
37781
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
148 |
Properties props = GetPropertyAction.privilegedGetProperties(); |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
34943
diff
changeset
|
149 |
String keepAlive = props.getProperty("http.keepAlive"); |
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
34943
diff
changeset
|
150 |
String retryPost = props.getProperty("sun.net.http.retryPost"); |
2 | 151 |
|
152 |
if (keepAlive != null) { |
|
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
34943
diff
changeset
|
153 |
keepAliveProp = Boolean.parseBoolean(keepAlive); |
2 | 154 |
} else { |
155 |
keepAliveProp = true; |
|
156 |
} |
|
157 |
||
158 |
if (retryPost != null) { |
|
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
34943
diff
changeset
|
159 |
retryPostProp = Boolean.parseBoolean(retryPost); |
2 | 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 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31061
diff
changeset
|
214 |
protected static Proxy newHttpProxy(String proxyHost, int proxyPort, |
2 | 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( |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
27717
diff
changeset
|
482 |
new java.security.PrivilegedExceptionAction<>() { |
51 | 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 |
31061 | 628 |
something like: {@literal "HTTP/1.0 <number> comment\r\n"}. */ |
2 | 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; |
27717
9d29bef916e1
8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents:
26720
diff
changeset
|
660 |
if (getRequestMethod().equals("CONNECT") |
9d29bef916e1
8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents:
26720
diff
changeset
|
661 |
|| streaming |
9d29bef916e1
8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents:
26720
diff
changeset
|
662 |
|| (httpuc.getRequestMethod().equals("POST") |
9d29bef916e1
8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents:
26720
diff
changeset
|
663 |
&& !retryPostProp)) { |
2 | 664 |
// do not retry the request |
665 |
} else { |
|
666 |
// try once more |
|
667 |
openServer(); |
|
668 |
if (needsTunneling()) { |
|
24045
31a23ee57590
8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
21278
diff
changeset
|
669 |
MessageHeader origRequests = requests; |
2 | 670 |
httpuc.doTunneling(); |
24045
31a23ee57590
8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
21278
diff
changeset
|
671 |
requests = origRequests; |
2 | 672 |
} |
673 |
afterConnect(); |
|
674 |
writeRequests(requests, poster); |
|
675 |
return parseHTTP(responses, pi, httpuc); |
|
676 |
} |
|
677 |
} |
|
678 |
throw e; |
|
679 |
} |
|
680 |
||
681 |
} |
|
682 |
||
683 |
private boolean parseHTTPHeader(MessageHeader responses, ProgressSource pi, HttpURLConnection httpuc) |
|
684 |
throws IOException { |
|
685 |
/* If "HTTP/*" is found in the beginning, return true. Let |
|
686 |
* HttpURLConnection parse the mime header itself. |
|
687 |
* |
|
688 |
* If this isn't valid HTTP, then we don't try to parse a header |
|
689 |
* out of the beginning of the response into the responses, |
|
690 |
* and instead just queue up the output stream to it's very beginning. |
|
691 |
* This seems most reasonable, and is what the NN browser does. |
|
692 |
*/ |
|
693 |
||
694 |
keepAliveConnections = -1; |
|
695 |
keepAliveTimeout = 0; |
|
696 |
||
697 |
boolean ret = false; |
|
698 |
byte[] b = new byte[8]; |
|
699 |
||
700 |
try { |
|
701 |
int nread = 0; |
|
702 |
serverInput.mark(10); |
|
703 |
while (nread < 8) { |
|
704 |
int r = serverInput.read(b, nread, 8 - nread); |
|
705 |
if (r < 0) { |
|
706 |
break; |
|
707 |
} |
|
708 |
nread += r; |
|
709 |
} |
|
710 |
String keep=null; |
|
711 |
ret = b[0] == 'H' && b[1] == 'T' |
|
712 |
&& b[2] == 'T' && b[3] == 'P' && b[4] == '/' && |
|
713 |
b[5] == '1' && b[6] == '.'; |
|
714 |
serverInput.reset(); |
|
715 |
if (ret) { // is valid HTTP - response started w/ "HTTP/1." |
|
716 |
responses.parseHeader(serverInput); |
|
717 |
||
718 |
// we've finished parsing http headers |
|
719 |
// 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
|
720 |
CookieHandler cookieHandler = httpuc.getCookieHandler(); |
2 | 721 |
if (cookieHandler != null) { |
722 |
URI uri = ParseUtil.toURI(url); |
|
723 |
// NOTE: That cast from Map shouldn't be necessary but |
|
724 |
// a bug in javac is triggered under certain circumstances |
|
725 |
// So we do put the cast in as a workaround until |
|
726 |
// it is resolved. |
|
727 |
if (uri != null) |
|
51 | 728 |
cookieHandler.put(uri, responses.getHeaders()); |
2 | 729 |
} |
730 |
||
731 |
/* decide if we're keeping alive: |
|
732 |
* This is a bit tricky. There's a spec, but most current |
|
733 |
* servers (10/1/96) that support this differ in dialects. |
|
734 |
* If the server/client misunderstand each other, the |
|
735 |
* protocol should fall back onto HTTP/1.0, no keep-alive. |
|
736 |
*/ |
|
737 |
if (usingProxy) { // not likely a proxy will return this |
|
738 |
keep = responses.findValue("Proxy-Connection"); |
|
739 |
} |
|
740 |
if (keep == null) { |
|
741 |
keep = responses.findValue("Connection"); |
|
742 |
} |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
743 |
if (keep != null && keep.toLowerCase(Locale.US).equals("keep-alive")) { |
2 | 744 |
/* some servers, notably Apache1.1, send something like: |
745 |
* "Keep-Alive: timeout=15, max=1" which we should respect. |
|
746 |
*/ |
|
747 |
HeaderParser p = new HeaderParser( |
|
748 |
responses.findValue("Keep-Alive")); |
|
749 |
if (p != null) { |
|
750 |
/* default should be larger in case of proxy */ |
|
751 |
keepAliveConnections = p.findInt("max", usingProxy?50:5); |
|
752 |
keepAliveTimeout = p.findInt("timeout", usingProxy?60:5); |
|
753 |
} |
|
754 |
} else if (b[7] != '0') { |
|
755 |
/* |
|
756 |
* We're talking 1.1 or later. Keep persistent until |
|
757 |
* the server says to close. |
|
758 |
*/ |
|
759 |
if (keep != null) { |
|
760 |
/* |
|
761 |
* The only Connection token we understand is close. |
|
762 |
* Paranoia: if there is any Connection header then |
|
763 |
* treat as non-persistent. |
|
764 |
*/ |
|
765 |
keepAliveConnections = 1; |
|
766 |
} else { |
|
767 |
keepAliveConnections = 5; |
|
768 |
} |
|
769 |
} |
|
770 |
} else if (nread != 8) { |
|
771 |
if (!failedOnce && requests != null) { |
|
6673
3674dbc66612
6672144: HttpURLConnection.getInputStream sends POST request after failed chunked
chegar
parents:
5506
diff
changeset
|
772 |
failedOnce = true; |
27717
9d29bef916e1
8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents:
26720
diff
changeset
|
773 |
if (getRequestMethod().equals("CONNECT") |
9d29bef916e1
8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents:
26720
diff
changeset
|
774 |
|| streaming |
9d29bef916e1
8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents:
26720
diff
changeset
|
775 |
|| (httpuc.getRequestMethod().equals("POST") |
9d29bef916e1
8050983: Misplaced parentheses in sun.net.www.http.HttpClient break HTTP PUT streaming
chegar
parents:
26720
diff
changeset
|
776 |
&& !retryPostProp)) { |
2 | 777 |
// do not retry the request |
778 |
} else { |
|
779 |
closeServer(); |
|
780 |
cachedHttpClient = false; |
|
781 |
openServer(); |
|
782 |
if (needsTunneling()) { |
|
24045
31a23ee57590
8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
21278
diff
changeset
|
783 |
MessageHeader origRequests = requests; |
2 | 784 |
httpuc.doTunneling(); |
24045
31a23ee57590
8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
21278
diff
changeset
|
785 |
requests = origRequests; |
2 | 786 |
} |
787 |
afterConnect(); |
|
788 |
writeRequests(requests, poster); |
|
789 |
return parseHTTP(responses, pi, httpuc); |
|
790 |
} |
|
791 |
} |
|
792 |
throw new SocketException("Unexpected end of file from server"); |
|
793 |
} else { |
|
794 |
// we can't vouche for what this is.... |
|
795 |
responses.set("Content-type", "unknown/unknown"); |
|
796 |
} |
|
797 |
} catch (IOException e) { |
|
798 |
throw e; |
|
799 |
} |
|
800 |
||
801 |
int code = -1; |
|
802 |
try { |
|
803 |
String resp; |
|
804 |
resp = responses.getValue(0); |
|
805 |
/* should have no leading/trailing LWS |
|
806 |
* expedite the typical case by assuming it has |
|
807 |
* form "HTTP/1.x <WS> 2XX <mumble>" |
|
808 |
*/ |
|
809 |
int ind; |
|
810 |
ind = resp.indexOf(' '); |
|
811 |
while(resp.charAt(ind) == ' ') |
|
812 |
ind++; |
|
26720
6b160d97c51d
8055032: Improve numerical parsing in java.net and sun.net
redestad
parents:
25859
diff
changeset
|
813 |
code = Integer.parseInt(resp, ind, ind + 3, 10); |
2 | 814 |
} catch (Exception e) {} |
815 |
||
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
816 |
if (code == HTTP_CONTINUE && ignoreContinue) { |
2 | 817 |
responses.reset(); |
818 |
return parseHTTPHeader(responses, pi, httpuc); |
|
819 |
} |
|
820 |
||
821 |
long cl = -1; |
|
822 |
||
823 |
/* |
|
824 |
* Set things up to parse the entity body of the reply. |
|
825 |
* We should be smarter about avoid pointless work when |
|
826 |
* the HTTP method and response code indicate there will be |
|
827 |
* no entity body to parse. |
|
828 |
*/ |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
829 |
String te = responses.findValue("Transfer-Encoding"); |
2 | 830 |
if (te != null && te.equalsIgnoreCase("chunked")) { |
831 |
serverInput = new ChunkedInputStream(serverInput, this, responses); |
|
832 |
||
833 |
/* |
|
834 |
* If keep alive not specified then close after the stream |
|
835 |
* has completed. |
|
836 |
*/ |
|
837 |
if (keepAliveConnections <= 1) { |
|
838 |
keepAliveConnections = 1; |
|
839 |
keepingAlive = false; |
|
840 |
} else { |
|
841 |
keepingAlive = true; |
|
842 |
} |
|
843 |
failedOnce = false; |
|
844 |
} else { |
|
845 |
||
846 |
/* |
|
847 |
* If it's a keep alive connection then we will keep |
|
848 |
* (alive if :- |
|
849 |
* 1. content-length is specified, or |
|
850 |
* 2. "Not-Modified" or "No-Content" responses - RFC 2616 states that |
|
851 |
* 204 or 304 response must not include a message body. |
|
852 |
*/ |
|
3059
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
853 |
String cls = responses.findValue("content-length"); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
854 |
if (cls != null) { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
855 |
try { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
856 |
cl = Long.parseLong(cls); |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
857 |
} catch (NumberFormatException e) { |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
858 |
cl = -1; |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
859 |
} |
2975ff687a6b
6811297: Add more logging to HTTP protocol handler
jccollet
parents:
2928
diff
changeset
|
860 |
} |
2 | 861 |
String requestLine = requests.getKey(0); |
862 |
||
863 |
if ((requestLine != null && |
|
864 |
(requestLine.startsWith("HEAD"))) || |
|
865 |
code == HttpURLConnection.HTTP_NOT_MODIFIED || |
|
866 |
code == HttpURLConnection.HTTP_NO_CONTENT) { |
|
867 |
cl = 0; |
|
868 |
} |
|
869 |
||
870 |
if (keepAliveConnections > 1 && |
|
871 |
(cl >= 0 || |
|
872 |
code == HttpURLConnection.HTTP_NOT_MODIFIED || |
|
873 |
code == HttpURLConnection.HTTP_NO_CONTENT)) { |
|
874 |
keepingAlive = true; |
|
875 |
failedOnce = false; |
|
876 |
} else if (keepingAlive) { |
|
877 |
/* Previously we were keeping alive, and now we're not. Remove |
|
878 |
* this from the cache (but only here, once) - otherwise we get |
|
879 |
* multiple removes and the cache count gets messed up. |
|
880 |
*/ |
|
881 |
keepingAlive=false; |
|
882 |
} |
|
883 |
} |
|
884 |
||
885 |
/* wrap a KeepAliveStream/MeteredStream around it if appropriate */ |
|
886 |
||
887 |
if (cl > 0) { |
|
888 |
// In this case, content length is well known, so it is okay |
|
889 |
// to wrap the input stream with KeepAliveStream/MeteredStream. |
|
890 |
||
891 |
if (pi != null) { |
|
892 |
// Progress monitor is enabled |
|
893 |
pi.setContentType(responses.findValue("content-type")); |
|
894 |
} |
|
895 |
||
896 |
if (isKeepingAlive()) { |
|
897 |
// 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
|
898 |
logFinest("KeepAlive stream used: " + url); |
2 | 899 |
serverInput = new KeepAliveStream(serverInput, pi, cl, this); |
900 |
failedOnce = false; |
|
901 |
} |
|
902 |
else { |
|
903 |
serverInput = new MeteredStream(serverInput, pi, cl); |
|
904 |
} |
|
905 |
} |
|
906 |
else if (cl == -1) { |
|
907 |
// In this case, content length is unknown - the input |
|
908 |
// stream would simply be a regular InputStream or |
|
909 |
// ChunkedInputStream. |
|
910 |
||
911 |
if (pi != null) { |
|
912 |
// Progress monitoring is enabled. |
|
913 |
||
914 |
pi.setContentType(responses.findValue("content-type")); |
|
915 |
||
916 |
// Wrap MeteredStream for tracking indeterministic |
|
917 |
// progress, even if the input stream is ChunkedInputStream. |
|
918 |
serverInput = new MeteredStream(serverInput, pi, cl); |
|
919 |
} |
|
920 |
else { |
|
921 |
// Progress monitoring is disabled, and there is no |
|
922 |
// need to wrap an unknown length input stream. |
|
923 |
||
924 |
// ** This is an no-op ** |
|
925 |
} |
|
926 |
} |
|
927 |
else { |
|
928 |
if (pi != null) |
|
929 |
pi.finishTracking(); |
|
930 |
} |
|
931 |
||
932 |
return ret; |
|
933 |
} |
|
934 |
||
935 |
public synchronized InputStream getInputStream() { |
|
936 |
return serverInput; |
|
937 |
} |
|
938 |
||
939 |
public OutputStream getOutputStream() { |
|
940 |
return serverOutput; |
|
941 |
} |
|
942 |
||
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
943 |
@Override |
2 | 944 |
public String toString() { |
945 |
return getClass().getName()+"("+url+")"; |
|
946 |
} |
|
947 |
||
948 |
public final boolean isKeepingAlive() { |
|
949 |
return getHttpKeepAliveSet() && keepingAlive; |
|
950 |
} |
|
951 |
||
952 |
public void setCacheRequest(CacheRequest cacheRequest) { |
|
953 |
this.cacheRequest = cacheRequest; |
|
954 |
} |
|
955 |
||
956 |
CacheRequest getCacheRequest() { |
|
957 |
return cacheRequest; |
|
958 |
} |
|
959 |
||
10136
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
960 |
String getRequestMethod() { |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
961 |
if (requests != null) { |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
962 |
String requestLine = requests.getKey(0); |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
963 |
if (requestLine != null) { |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
964 |
return requestLine.split("\\s+")[0]; |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
965 |
} |
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 |
return ""; |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
968 |
} |
af9631156b25
6670868: StackOverFlow with bad authenticated Proxy tunnels
chegar
parents:
9775
diff
changeset
|
969 |
|
2 | 970 |
public void setDoNotRetry(boolean value) { |
971 |
// failedOnce is used to determine if a request should be retried. |
|
972 |
failedOnce = value; |
|
973 |
} |
|
974 |
||
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
975 |
public void setIgnoreContinue(boolean value) { |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
976 |
ignoreContinue = value; |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
977 |
} |
2 | 978 |
|
979 |
/* Use only on connections in error. */ |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
715
diff
changeset
|
980 |
@Override |
2 | 981 |
public void closeServer() { |
982 |
try { |
|
983 |
keepingAlive = false; |
|
984 |
serverSocket.close(); |
|
985 |
} catch (Exception e) {} |
|
986 |
} |
|
987 |
||
988 |
/** |
|
989 |
* @return the proxy host being used for this client, or null |
|
990 |
* if we're not going through a proxy |
|
991 |
*/ |
|
992 |
public String getProxyHostUsed() { |
|
993 |
if (!usingProxy) { |
|
994 |
return null; |
|
995 |
} else { |
|
996 |
return ((InetSocketAddress)proxy.address()).getHostString(); |
|
997 |
} |
|
998 |
} |
|
999 |
||
1000 |
/** |
|
1001 |
* @return the proxy port being used for this client. Meaningless |
|
1002 |
* if getProxyHostUsed() gives null. |
|
1003 |
*/ |
|
1004 |
public int getProxyPortUsed() { |
|
1005 |
if (usingProxy) |
|
1006 |
return ((InetSocketAddress)proxy.address()).getPort(); |
|
1007 |
return -1; |
|
1008 |
} |
|
1009 |
} |