1 /* |
|
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
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 * |
|
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. |
|
24 */ |
|
25 |
|
26 package jdk.incubator.http; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.lang.System.Logger.Level; |
|
30 import java.net.InetSocketAddress; |
|
31 import java.nio.ByteBuffer; |
|
32 import java.nio.channels.SocketChannel; |
|
33 import java.util.concurrent.CompletableFuture; |
|
34 import java.util.function.Function; |
|
35 |
|
36 import jdk.incubator.http.internal.common.FlowTube; |
|
37 import jdk.incubator.http.internal.common.MinimalFuture; |
|
38 |
|
39 import static jdk.incubator.http.HttpResponse.BodyHandler.discard; |
|
40 |
|
41 /** |
|
42 * A plain text socket tunnel through a proxy. Uses "CONNECT" but does not |
|
43 * encrypt. Used by WebSocket, as well as HTTP over SSL + Proxy. |
|
44 * Wrapped in SSLTunnelConnection or AsyncSSLTunnelConnection for encryption. |
|
45 */ |
|
46 final class PlainTunnelingConnection extends HttpConnection { |
|
47 |
|
48 final PlainHttpConnection delegate; |
|
49 final HttpHeaders proxyHeaders; |
|
50 final InetSocketAddress proxyAddr; |
|
51 private volatile boolean connected; |
|
52 |
|
53 protected PlainTunnelingConnection(InetSocketAddress addr, |
|
54 InetSocketAddress proxy, |
|
55 HttpClientImpl client, |
|
56 HttpHeaders proxyHeaders) { |
|
57 super(addr, client); |
|
58 this.proxyAddr = proxy; |
|
59 this.proxyHeaders = proxyHeaders; |
|
60 delegate = new PlainHttpConnection(proxy, client); |
|
61 } |
|
62 |
|
63 @Override |
|
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; |
|
71 HttpRequestImpl req = new HttpRequestImpl("CONNECT", address, proxyHeaders); |
|
72 MultiExchange<Void> mulEx = new MultiExchange<>(null, req, |
|
73 client, discard(null), null, null); |
|
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()); |
|
81 if (resp.statusCode() == 407) { |
|
82 return connectExchange.ignoreBody().handle((r,t) -> { |
|
83 // close delegate after reading body: we won't |
|
84 // be reusing that connection anyway. |
|
85 delegate.close(); |
|
86 ProxyAuthenticationRequired authenticationRequired = |
|
87 new ProxyAuthenticationRequired(resp); |
|
88 cf.completeExceptionally(authenticationRequired); |
|
89 return cf; |
|
90 }).thenCompose(Function.identity()); |
|
91 } else if (resp.statusCode() != 200) { |
|
92 delegate.close(); |
|
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 |
|
109 boolean isTunnel() { return true; } |
|
110 |
|
111 @Override |
|
112 HttpPublisher publisher() { return delegate.publisher(); } |
|
113 |
|
114 @Override |
|
115 boolean connected() { |
|
116 return connected; |
|
117 } |
|
118 |
|
119 @Override |
|
120 SocketChannel channel() { |
|
121 return delegate.channel(); |
|
122 } |
|
123 |
|
124 @Override |
|
125 FlowTube getConnectionFlow() { |
|
126 return delegate.getConnectionFlow(); |
|
127 } |
|
128 |
|
129 @Override |
|
130 ConnectionPool.CacheKey cacheKey() { |
|
131 return new ConnectionPool.CacheKey(null, proxyAddr); |
|
132 } |
|
133 |
|
134 @Override |
|
135 public void close() { |
|
136 delegate.close(); |
|
137 connected = false; |
|
138 } |
|
139 |
|
140 @Override |
|
141 void shutdownInput() throws IOException { |
|
142 delegate.shutdownInput(); |
|
143 } |
|
144 |
|
145 @Override |
|
146 void shutdownOutput() throws IOException { |
|
147 delegate.shutdownOutput(); |
|
148 } |
|
149 |
|
150 @Override |
|
151 boolean isSecure() { |
|
152 return false; |
|
153 } |
|
154 |
|
155 @Override |
|
156 boolean isProxied() { |
|
157 return true; |
|
158 } |
|
159 |
|
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. |
|
163 @Override |
|
164 DetachedConnectionChannel detachChannel() { |
|
165 return delegate.detachChannel(); |
|
166 } |
|
167 } |
|