author | dfuchs |
Wed, 31 Jan 2018 16:18:41 +0000 | |
branch | http-client-branch |
changeset 56054 | 352e845ae744 |
parent 56041 | b4b5e09ef3cc |
child 56061 | 6d6290b1176f |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
package jdk.incubator.http; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
48083 | 29 |
import java.lang.System.Logger.Level; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.net.InetSocketAddress; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.nio.ByteBuffer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.nio.channels.SocketChannel; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.util.concurrent.CompletableFuture; |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
34 |
import java.util.function.Function; |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
35 |
|
48083 | 36 |
import jdk.incubator.http.internal.common.FlowTube; |
37 |
import jdk.incubator.http.internal.common.MinimalFuture; |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
38 |
|
48083 | 39 |
import static jdk.incubator.http.HttpResponse.BodyHandler.discard; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
* A plain text socket tunnel through a proxy. Uses "CONNECT" but does not |
46157 | 43 |
* encrypt. Used by WebSocket, as well as HTTP over SSL + Proxy. |
44 |
* Wrapped in SSLTunnelConnection or AsyncSSLTunnelConnection for encryption. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
*/ |
48083 | 46 |
final class PlainTunnelingConnection extends HttpConnection { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
final PlainHttpConnection delegate; |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
49 |
final HttpHeaders proxyHeaders; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
protected final InetSocketAddress proxyAddr; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
private volatile boolean connected; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
protected PlainTunnelingConnection(InetSocketAddress addr, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
InetSocketAddress proxy, |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
55 |
HttpClientImpl client, |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
56 |
HttpHeaders proxyHeaders) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
super(addr, client); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
this.proxyAddr = proxy; |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
59 |
this.proxyHeaders = proxyHeaders; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
delegate = new PlainHttpConnection(proxy, client); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
@Override |
48083 | 64 |
public CompletableFuture<Void> connectAsync() { |
65 |
debug.log(Level.DEBUG, "Connecting plain connection"); |
|
66 |
return delegate.connectAsync() |
|
67 |
.thenCompose((Void v) -> { |
|
68 |
debug.log(Level.DEBUG, "sending HTTP/1.1 CONNECT"); |
|
69 |
HttpClientImpl client = client(); |
|
70 |
assert client != null; |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
71 |
HttpRequestImpl req = new HttpRequestImpl("CONNECT", address, proxyHeaders); |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
72 |
MultiExchange<Void> mulEx = new MultiExchange<>(null, req, |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
73 |
client, discard(null), null, null); |
48083 | 74 |
Exchange<Void> connectExchange = new Exchange<>(req, mulEx); |
75 |
||
76 |
return connectExchange |
|
77 |
.responseAsyncImpl(delegate) |
|
78 |
.thenCompose((Response resp) -> { |
|
79 |
CompletableFuture<Void> cf = new MinimalFuture<>(); |
|
80 |
debug.log(Level.DEBUG, "got response: %d", resp.statusCode()); |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
81 |
if (resp.statusCode() == 407) { |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
82 |
return connectExchange.ignoreBody().handle((r,t) -> { |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
83 |
// close delegate after reading body: we won't |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
84 |
// be reusing that connection anyway. |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
85 |
delegate.close(); |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
86 |
ProxyAuthenticationRequired authenticationRequired = |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
87 |
new ProxyAuthenticationRequired(resp); |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
88 |
cf.completeExceptionally(authenticationRequired); |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
89 |
return cf; |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
90 |
}).thenCompose(Function.identity()); |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
91 |
} else if (resp.statusCode() != 200) { |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
92 |
delegate.close(); |
48083 | 93 |
cf.completeExceptionally(new IOException( |
94 |
"Tunnel failed, got: "+ resp.statusCode())); |
|
95 |
} else { |
|
96 |
// get the initial/remaining bytes |
|
97 |
ByteBuffer b = ((Http1Exchange<?>)connectExchange.exchImpl).drainLeftOverBytes(); |
|
98 |
int remaining = b.remaining(); |
|
99 |
assert remaining == 0: "Unexpected remaining: " + remaining; |
|
100 |
connected = true; |
|
101 |
cf.complete(null); |
|
102 |
} |
|
103 |
return cf; |
|
104 |
}); |
|
105 |
}); |
|
106 |
} |
|
107 |
||
108 |
@Override |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
109 |
boolean isTunnel() { return true; } |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
110 |
|
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56010
diff
changeset
|
111 |
@Override |
48083 | 112 |
HttpPublisher publisher() { return delegate.publisher(); } |
113 |
||
114 |
@Override |
|
115 |
boolean connected() { |
|
116 |
return connected; |
|
117 |
} |
|
118 |
||
119 |
@Override |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
SocketChannel channel() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
return delegate.channel(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
@Override |
48083 | 125 |
FlowTube getConnectionFlow() { |
126 |
return delegate.getConnectionFlow(); |
|
127 |
} |
|
128 |
||
129 |
@Override |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
ConnectionPool.CacheKey cacheKey() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
return new ConnectionPool.CacheKey(null, proxyAddr); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
public void close() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
delegate.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
connected = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
void shutdownInput() throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
delegate.shutdownInput(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
void shutdownOutput() throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
147 |
delegate.shutdownOutput(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
148 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
150 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
boolean isSecure() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
156 |
boolean isProxied() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
return true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
} |
46157 | 159 |
|
48083 | 160 |
// Support for WebSocket/RawChannelImpl which unfortunately |
161 |
// still depends on synchronous read/writes. |
|
162 |
// It should be removed when RawChannelImpl moves to using asynchronous APIs. |
|
46157 | 163 |
@Override |
48083 | 164 |
DetachedConnectionChannel detachChannel() { |
165 |
return delegate.detachChannel(); |
|
46157 | 166 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
} |