author | jboes |
Fri, 08 Nov 2019 11:15:16 +0000 | |
changeset 59029 | 3786a0962570 |
parent 53350 | a47b8125b7cc |
permissions | -rw-r--r-- |
53350
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
1 |
/* |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
4 |
* |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
8 |
* |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
13 |
* accompanied this code). |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
14 |
* |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
18 |
* |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
21 |
* questions. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
22 |
*/ |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
23 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
24 |
/* |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
25 |
* @test |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
26 |
* @summary This test will timeout if the ALPN CF is not completed |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
27 |
* when a 'Connection reset by peer' exception is raised |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
28 |
* during the handshake. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
29 |
* @bug 8217094 |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
30 |
* @modules java.net.http |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
31 |
* java.logging |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
32 |
* @build ALPNFailureTest |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
33 |
* @run main/othervm -Djdk.internal.httpclient.debug=true ALPNFailureTest HTTP_1_1 |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
34 |
* @run main/othervm ALPNFailureTest HTTP_2 |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
35 |
*/ |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
36 |
import javax.net.ServerSocketFactory; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
37 |
import javax.net.ssl.SSLContext; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
38 |
import java.io.Closeable; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
39 |
import java.io.IOException; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
40 |
import java.io.InputStream; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
41 |
import java.io.OutputStream; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
42 |
import java.net.InetAddress; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
43 |
import java.net.ProxySelector; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
44 |
import java.net.ServerSocket; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
45 |
import java.net.Socket; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
46 |
import java.net.SocketTimeoutException; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
47 |
import java.net.StandardSocketOptions; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
48 |
import java.net.URI; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
49 |
import java.net.http.HttpClient; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
50 |
import java.net.http.HttpRequest; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
51 |
import java.net.http.HttpResponse; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
52 |
import java.net.http.HttpTimeoutException; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
53 |
import java.util.List; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
54 |
import java.util.concurrent.atomic.AtomicBoolean; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
55 |
import java.util.concurrent.atomic.AtomicReference; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
56 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
57 |
public class ALPNFailureTest { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
58 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
59 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
60 |
public static void main(String[] args) throws Exception{ |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
61 |
if (args == null || args.length == 0) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
62 |
args = new String[] {HttpClient.Version.HTTP_1_1.name()}; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
63 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
64 |
ServerSocket socket = ServerSocketFactory.getDefault() |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
65 |
.createServerSocket(0, 10, InetAddress.getLoopbackAddress()); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
66 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
67 |
test(socket, null, null, args); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
68 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
69 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
70 |
public static void test(ServerSocket socket, SSLContext context, |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
71 |
ProxySelector ps, String... args) |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
72 |
throws Exception |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
73 |
{ |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
74 |
System.out.println("Tests a race condition in SSLTube/SSLFlowDelegate"); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
75 |
System.out.println("This test will timeout if the ALPN CF is not completed" + |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
76 |
" when a 'Connection reset by peer' exception is raised" + |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
77 |
" during the handshake - see 8217094."); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
78 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
79 |
URI uri = new URI("https", null, |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
80 |
socket.getInetAddress().getHostAddress(), socket.getLocalPort(), |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
81 |
"/ReadOnlyServer/https_1_1/", null, null); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
82 |
HttpRequest request1 = HttpRequest.newBuilder(uri) |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
83 |
.GET().build(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
84 |
HttpRequest request2 = HttpRequest.newBuilder(uri) |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
85 |
.POST(HttpRequest.BodyPublishers.ofString("foo")).build(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
86 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
87 |
ReadOnlyServer server = new ReadOnlyServer(socket); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
88 |
Thread serverThread = new Thread(server, "ServerThread"); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
89 |
serverThread.start(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
90 |
try { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
91 |
for (var arg : args) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
92 |
var version = HttpClient.Version.valueOf(arg); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
93 |
HttpClient.Builder builder = HttpClient.newBuilder() |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
94 |
.version(version); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
95 |
if (ps != null) builder.proxy(ps); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
96 |
if (context != null) builder.sslContext(context); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
97 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
98 |
HttpClient client = builder.build(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
99 |
for (var request : List.of(request1, request2)) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
100 |
System.out.println("Server is " + socket.getLocalSocketAddress() |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
101 |
+ ", Version is " + version + ", Method is " + request.method() |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
102 |
+ (ps == null ? ", no proxy" |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
103 |
: (", Proxy is " + ps.select(request.uri())))); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
104 |
try { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
105 |
HttpResponse<String> resp = |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
106 |
client.send(request, HttpResponse.BodyHandlers.ofString()); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
107 |
throw new AssertionError( |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
108 |
"Client should not have received any response: " + resp); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
109 |
} catch (HttpTimeoutException x) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
110 |
System.out.println("Unexpected " + x); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
111 |
x.printStackTrace(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
112 |
throw new AssertionError("Unexpected exception " + x, x); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
113 |
} catch (Exception x) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
114 |
// We expect IOException("Connection reset by peer"), but |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
115 |
// any exception would do: we just don't want to linger |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
116 |
// forever. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
117 |
System.err.println("Client got expected exception: " + x); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
118 |
x.printStackTrace(System.out); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
119 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
120 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
121 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
122 |
} finally { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
123 |
server.close(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
124 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
125 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
126 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
127 |
public static class ReadOnlyServer implements Runnable, Closeable { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
128 |
final ServerSocket socket; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
129 |
final AtomicReference<Throwable> errorRef = new AtomicReference<>(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
130 |
final AtomicBoolean closing = new AtomicBoolean(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
131 |
ReadOnlyServer(ServerSocket socket) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
132 |
this.socket = socket; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
133 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
134 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
135 |
@Override |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
136 |
public void run() { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
137 |
int count = 0; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
138 |
int all = 0; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
139 |
try { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
140 |
System.out.println("Server starting"); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
141 |
while (!closing.get()) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
142 |
all += count; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
143 |
count = 0; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
144 |
try (Socket client = socket.accept()) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
145 |
client.setSoTimeout(1000); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
146 |
client.setOption(StandardSocketOptions.SO_LINGER, 0); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
147 |
InputStream is = client.getInputStream(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
148 |
OutputStream os = client.getOutputStream(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
149 |
boolean drain = true; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
150 |
int timeouts = 0; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
151 |
// now read some byte from the ClientHello |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
152 |
// and abruptly close the socket. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
153 |
while (drain) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
154 |
try { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
155 |
is.read(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
156 |
count++; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
157 |
if (count >= 50) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
158 |
drain = false; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
159 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
160 |
} catch (SocketTimeoutException so) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
161 |
// make sure we read something |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
162 |
if (count > 0) timeouts++; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
163 |
if (timeouts == 5) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
164 |
// presumably the client is |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
165 |
// waiting for us to answer... |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
166 |
// but we should not reach here. |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
167 |
drain = false; |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
168 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
169 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
170 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
171 |
System.out.println("Got " + count + " bytes"); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
172 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
173 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
174 |
} catch (Throwable t) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
175 |
if (!closing.get()) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
176 |
errorRef.set(t); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
177 |
t.printStackTrace(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
178 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
179 |
} finally { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
180 |
System.out.println("Server existing after reading " + (all + count) + " bytes"); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
181 |
close(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
182 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
183 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
184 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
185 |
|
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
186 |
@Override |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
187 |
public void close() { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
188 |
if (closing.getAndSet(true)) |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
189 |
return; // already closed |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
190 |
try { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
191 |
socket.close(); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
192 |
} catch (IOException x) { |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
193 |
System.out.println("Exception while closing: " + x); |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
194 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
195 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
196 |
} |
a47b8125b7cc
8217094: HttpClient SSL race if a socket IOException is raised before ALPN is available
dfuchs
parents:
diff
changeset
|
197 |
} |