|
| 1 | +/* |
| 2 | + * ==================================================================== |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * ==================================================================== |
| 20 | + * |
| 21 | + * This software consists of voluntary contributions made by many |
| 22 | + * individuals on behalf of the Apache Software Foundation. For more |
| 23 | + * information on the Apache Software Foundation, please see |
| 24 | + * <http://www.apache.org/>. |
| 25 | + * |
| 26 | + */ |
| 27 | +package org.apache.hc.client5.testing.async; |
| 28 | + |
| 29 | +import java.io.Closeable; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.InputStream; |
| 32 | +import java.io.OutputStream; |
| 33 | +import java.net.InetAddress; |
| 34 | +import java.net.InetSocketAddress; |
| 35 | +import java.net.ServerSocket; |
| 36 | +import java.net.Socket; |
| 37 | +import java.nio.charset.StandardCharsets; |
| 38 | +import java.util.Locale; |
| 39 | +import java.util.concurrent.CompletableFuture; |
| 40 | +import java.util.concurrent.Future; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | +import java.util.stream.Stream; |
| 43 | + |
| 44 | +import javax.net.ssl.SSLContext; |
| 45 | +import javax.net.ssl.SSLSocket; |
| 46 | + |
| 47 | +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; |
| 48 | +import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder; |
| 49 | +import org.apache.hc.client5.http.config.TlsConfig; |
| 50 | +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; |
| 51 | +import org.apache.hc.client5.http.impl.async.HttpAsyncClients; |
| 52 | +import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager; |
| 53 | +import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder; |
| 54 | +import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy; |
| 55 | +import org.apache.hc.client5.testing.SSLTestContexts; |
| 56 | +import org.apache.hc.core5.http.HttpHost; |
| 57 | +import org.apache.hc.core5.http.ssl.TLS; |
| 58 | +import org.apache.hc.core5.http2.HttpVersionPolicy; |
| 59 | +import org.apache.hc.core5.util.Timeout; |
| 60 | +import org.junit.jupiter.api.Assertions; |
| 61 | +import org.junit.jupiter.params.ParameterizedTest; |
| 62 | +import org.junit.jupiter.params.provider.Arguments; |
| 63 | +import org.junit.jupiter.params.provider.MethodSource; |
| 64 | + |
| 65 | +class TestAsyncTlsConnectionClose { |
| 66 | + |
| 67 | + private static final Timeout TIMEOUT = Timeout.ofSeconds(5); |
| 68 | + |
| 69 | + enum CloseInitiator { |
| 70 | + // The origin sends Connection: close; HttpClient initiates TLS shutdown. |
| 71 | + CLIENT, |
| 72 | + // The origin sends close_notify after a reusable HTTP response. |
| 73 | + SERVER; |
| 74 | + |
| 75 | + @Override |
| 76 | + public String toString() { |
| 77 | + return name().toLowerCase(Locale.ROOT); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + static Stream<Arguments> tlsCloseScenarios() { |
| 82 | + return Stream.of(TLS.V_1_2, TLS.V_1_3) |
| 83 | + .flatMap(tlsVersion -> Stream.of(CloseInitiator.CLIENT, CloseInitiator.SERVER) |
| 84 | + .map(closeInitiator -> Arguments.of(tlsVersion, closeInitiator))); |
| 85 | + } |
| 86 | + |
| 87 | + @ParameterizedTest(name = "{0}, {1}-initiated TLS close") |
| 88 | + @MethodSource("tlsCloseScenarios") |
| 89 | + void testConnectionCloseCompletesTlsShutdownWithoutReset( |
| 90 | + final TLS tlsVersion, |
| 91 | + final CloseInitiator closeInitiator) throws Exception { |
| 92 | + try (TlsConnectionCloseServer server = new TlsConnectionCloseServer(tlsVersion, closeInitiator); |
| 93 | + PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create() |
| 94 | + .setTlsStrategy(new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext())) |
| 95 | + .setDefaultTlsConfig(TlsConfig.custom() |
| 96 | + .setSupportedProtocols(tlsVersion) |
| 97 | + .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_1) |
| 98 | + .build()) |
| 99 | + .build(); |
| 100 | + CloseableHttpAsyncClient client = HttpAsyncClients.custom() |
| 101 | + .setConnectionManager(connectionManager) |
| 102 | + .build()) { |
| 103 | + server.start(); |
| 104 | + client.start(); |
| 105 | + |
| 106 | + final HttpHost target = new HttpHost( |
| 107 | + "https", InetAddress.getByName("127.0.0.1"), "localhost", server.getPort()); |
| 108 | + final Future<SimpleHttpResponse> responseFuture = client.execute( |
| 109 | + SimpleRequestBuilder.get() |
| 110 | + .setHttpHost(target) |
| 111 | + .setPath("/") |
| 112 | + .build(), |
| 113 | + null); |
| 114 | + |
| 115 | + Assertions.assertEquals(200, responseFuture.get( |
| 116 | + TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()).getCode()); |
| 117 | + Assertions.assertTrue(server.awaitCleanClose(), |
| 118 | + () -> "the client reset the TCP connection during the " |
| 119 | + + tlsVersion.id + " " + closeInitiator |
| 120 | + + "-initiated TLS close_notify exchange"); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static final class TlsConnectionCloseServer implements Closeable { |
| 125 | + |
| 126 | + private final TLS tlsVersion; |
| 127 | + private final CloseInitiator closeInitiator; |
| 128 | + private final ServerSocket serverSocket; |
| 129 | + private final CompletableFuture<Boolean> cleanCloseFuture; |
| 130 | + private Thread worker; |
| 131 | + |
| 132 | + TlsConnectionCloseServer(final TLS tlsVersion, final CloseInitiator closeInitiator) throws IOException { |
| 133 | + this.tlsVersion = tlsVersion; |
| 134 | + this.closeInitiator = closeInitiator; |
| 135 | + this.serverSocket = new ServerSocket(); |
| 136 | + this.serverSocket.bind(new InetSocketAddress("127.0.0.1", 0)); |
| 137 | + this.cleanCloseFuture = new CompletableFuture<>(); |
| 138 | + } |
| 139 | + |
| 140 | + void start() { |
| 141 | + worker = new Thread(this::run, "tls-connection-close-server"); |
| 142 | + worker.setDaemon(true); |
| 143 | + worker.start(); |
| 144 | + } |
| 145 | + |
| 146 | + int getPort() { |
| 147 | + return serverSocket.getLocalPort(); |
| 148 | + } |
| 149 | + |
| 150 | + boolean awaitCleanClose() throws Exception { |
| 151 | + return cleanCloseFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()); |
| 152 | + } |
| 153 | + |
| 154 | + private void run() { |
| 155 | + try (Socket socket = serverSocket.accept()) { |
| 156 | + socket.setSoTimeout(TIMEOUT.toMillisecondsIntBound()); |
| 157 | + final SSLContext sslContext = SSLTestContexts.createServerSSLContext(); |
| 158 | + try (SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket( |
| 159 | + socket, "localhost", socket.getPort(), false)) { |
| 160 | + sslSocket.setUseClientMode(false); |
| 161 | + sslSocket.setEnabledProtocols(new String[] { tlsVersion.id }); |
| 162 | + sslSocket.startHandshake(); |
| 163 | + if (!tlsVersion.id.equals(sslSocket.getSession().getProtocol())) { |
| 164 | + throw new IOException("Unexpected negotiated TLS protocol " |
| 165 | + + sslSocket.getSession().getProtocol()); |
| 166 | + } |
| 167 | + |
| 168 | + readRequestHead(sslSocket.getInputStream()); |
| 169 | + final OutputStream outputStream = sslSocket.getOutputStream(); |
| 170 | + outputStream.write(("HTTP/1.1 200 OK\r\n" |
| 171 | + + "Content-Length: 0\r\n" |
| 172 | + + (closeInitiator == CloseInitiator.CLIENT ? "Connection: close\r\n" : "") |
| 173 | + + "\r\n").getBytes(StandardCharsets.US_ASCII)); |
| 174 | + outputStream.flush(); |
| 175 | + |
| 176 | + if (closeInitiator == CloseInitiator.CLIENT) { |
| 177 | + // Connection: close causes the client to initiate TLS |
| 178 | + // shutdown. Wait for its close_notify before returning ours. |
| 179 | + if (sslSocket.getInputStream().read() != -1) { |
| 180 | + throw new IOException("Unexpected application data after the request"); |
| 181 | + } |
| 182 | + } |
| 183 | + // Otherwise closing this SSLSocket sends the server's |
| 184 | + // close_notify first. |
| 185 | + } |
| 186 | + |
| 187 | + // The layered SSLSocket does not own the TCP socket. Check the |
| 188 | + // transport after TLS shutdown to distinguish FIN from RST. |
| 189 | + cleanCloseFuture.complete(socket.getInputStream().read() == -1); |
| 190 | + } catch (final IOException ex) { |
| 191 | + if (isConnectionReset(ex)) { |
| 192 | + cleanCloseFuture.complete(false); |
| 193 | + } else { |
| 194 | + cleanCloseFuture.completeExceptionally(ex); |
| 195 | + } |
| 196 | + } catch (final Exception ex) { |
| 197 | + cleanCloseFuture.completeExceptionally(ex); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private static boolean isConnectionReset(final Throwable cause) { |
| 202 | + for (Throwable current = cause; current != null; current = current.getCause()) { |
| 203 | + if (current.getMessage() != null |
| 204 | + && current.getMessage().toLowerCase(Locale.ROOT).contains("connection reset")) { |
| 205 | + return true; |
| 206 | + } |
| 207 | + } |
| 208 | + return false; |
| 209 | + } |
| 210 | + |
| 211 | + private static void readRequestHead(final InputStream inputStream) throws IOException { |
| 212 | + int matched = 0; |
| 213 | + while (matched < 4) { |
| 214 | + final int b = inputStream.read(); |
| 215 | + if (b == -1) { |
| 216 | + throw new IOException("Connection closed before the request head was complete"); |
| 217 | + } |
| 218 | + if (b == "\r\n\r\n".charAt(matched)) { |
| 219 | + matched++; |
| 220 | + } else { |
| 221 | + matched = b == '\r' ? 1 : 0; |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public void close() throws IOException { |
| 228 | + serverSocket.close(); |
| 229 | + if (worker != null) { |
| 230 | + try { |
| 231 | + worker.join(TimeUnit.SECONDS.toMillis(1)); |
| 232 | + } catch (final InterruptedException ex) { |
| 233 | + Thread.currentThread().interrupt(); |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments