test/jdk/java/net/httpclient/ServerCloseTest.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 52121 934969c63223
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
 * @summary Tests that our client deals correctly with servers that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 *          close the connection right after sending the last byte.
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51407
diff changeset
    28
 * @library /test/lib http2/server
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51407
diff changeset
    29
 * @build jdk.test.lib.net.SimpleSSLContext HttpServerAdapters EncodedCharsInURI
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
 * @modules java.base/sun.net.www.http
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
 *          java.net.http/jdk.internal.net.http.hpack
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 49765
diff changeset
    34
 * @run testng/othervm -Djdk.tls.acknowledgeCloseNotify=true ServerCloseTest
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
//*        -Djdk.internal.httpclient.debug=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51407
diff changeset
    38
import jdk.test.lib.net.SimpleSSLContext;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import org.testng.annotations.AfterClass;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import org.testng.annotations.BeforeTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import javax.net.ServerSocketFactory;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import javax.net.ssl.SSLContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import java.io.InputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import java.io.OutputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
import java.io.OutputStreamWriter;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
import java.io.PrintWriter;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
import java.io.Writer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
import java.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
import java.net.ServerSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
import java.net.Socket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
import java.net.http.HttpRequest.BodyPublisher;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
import java.net.http.HttpRequest.BodyPublishers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
import java.net.http.HttpResponse.BodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
import java.net.http.HttpResponse.BodyHandlers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
import java.util.Locale;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
import java.util.StringTokenizer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
import java.util.concurrent.ConcurrentHashMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
import java.util.concurrent.ConcurrentLinkedQueue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
import java.util.concurrent.ConcurrentMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
import java.util.concurrent.Executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
import java.util.concurrent.Executors;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
import java.util.concurrent.atomic.AtomicLong;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
import static java.lang.System.out;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
public class ServerCloseTest implements HttpServerAdapters {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    SSLContext sslContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
    DummyServer    httpDummyServer;    // HTTP/1.1    [ 2 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
    DummyServer    httpsDummyServer;   // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
    String httpDummy;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
    String httpsDummy;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
    static final int ITERATION_COUNT = 3;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
    // a shared executor helps reduce the amount of threads created by the test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
    static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
    static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
    static volatile boolean tasksFailed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
    static final AtomicLong serverCount = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
    static final AtomicLong clientCount = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
    static final long start = System.nanoTime();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
    public static String now() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
        long now = System.nanoTime() - start;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
        long secs = now / 1000_000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
        long mill = (now % 1000_000_000) / 1000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
        long nan = now % 1000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
    private volatile HttpClient sharedClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
    static class TestExecutor implements Executor {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
        final AtomicLong tasks = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
        Executor executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
        TestExecutor(Executor executor) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
            this.executor = executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
        public void execute(Runnable command) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
            long id = tasks.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
            executor.execute(() -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
                    command.run();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
                } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
                    tasksFailed = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
                    System.out.printf(now() + "Task %s failed: %s%n", id, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
                    System.err.printf(now() + "Task %s failed: %s%n", id, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
                    FAILURES.putIfAbsent("Task " + id, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
                    throw t;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
            });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
    @AfterClass
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
    static final void printFailedTests() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
        out.println("\n=========================");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
            out.printf("%n%sCreated %d servers and %d clients%n",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
                    now(), serverCount.get(), clientCount.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
            if (FAILURES.isEmpty()) return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
            out.println("Failed tests: ");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
            FAILURES.entrySet().forEach((e) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
                out.printf("\t%s: %s%n", e.getKey(), e.getValue());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                e.getValue().printStackTrace(out);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
            });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
            if (tasksFailed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
                System.out.println("WARNING: Some tasks failed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
        } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
            out.println("\n=========================\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
    private String[] uris() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
        return new String[] {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
                httpDummy,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
                httpsDummy,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
    @DataProvider(name = "servers")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
    public Object[][] noThrows() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
        String[] uris = uris();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
        Object[][] result = new Object[uris.length * 2][];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
        //Object[][] result = new Object[uris.length][];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
        int i = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
        for (boolean sameClient : List.of(false, true)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
            //if (!sameClient) continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
            for (String uri: uris()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
                result[i++] = new Object[] {uri, sameClient};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
        assert i == uris.length * 2;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
        // assert i == uris.length ;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
        return result;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
    private HttpClient makeNewClient() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
        clientCount.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
        return HttpClient.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
                .executor(executor)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
                .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
    HttpClient newHttpClient(boolean share) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
        if (!share) return makeNewClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
        HttpClient shared = sharedClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
        if (shared != null) return shared;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
        synchronized (this) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
            shared = sharedClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
            if (shared == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
                shared = sharedClient = makeNewClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
            return shared;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
    final String ENCODED = "/01%252F03/";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
    @Test(dataProvider = "servers")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
    public void testServerClose(String uri, boolean sameClient) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
        HttpClient client = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        out.printf("%n%s testServerClose(%s, %b)%n", now(), uri, sameClient);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
        uri = uri + ENCODED;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
        for (int i=0; i< ITERATION_COUNT; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
            if (!sameClient || client == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
                client = newHttpClient(sameClient);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
            BodyPublisher bodyPublisher = BodyPublishers.ofString(uri);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
                    .POST(bodyPublisher)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
                    .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
            BodyHandler<String> handler = BodyHandlers.ofString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
            HttpClient c = client;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
            CompletableFuture<HttpResponse<String>> responseCF = c.sendAsync(req, handler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
            // retry POST if needed   #### Replace with exceptionallyCompose
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
            responseCF = responseCF.handle((r,t) ->
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
                    t == null ? CompletableFuture.completedFuture(r)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
                            : c.sendAsync(req, handler)).thenCompose(x -> x);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
            HttpResponse<String> response = responseCF.join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
            String body = response.body();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
            if (!uri.contains(body)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
                System.err.println("Test failed: " + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
                throw new RuntimeException(uri + " doesn't contain '" + body + "'");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
                System.out.println("Found expected " + body + " in " + uri);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
    @BeforeTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
    public void setup() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
        sslContext = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
        if (sslContext == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
            throw new AssertionError("Unexpected null sslContext");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
        // DummyServer
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
        httpDummyServer = DummyServer.create(sa);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
        httpsDummyServer = DummyServer.create(sa, sslContext);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
        httpDummy = "http://" + httpDummyServer.serverAuthority() + "/http1/dummy/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
        httpsDummy = "https://" + httpsDummyServer.serverAuthority() + "/https1/dummy/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
        serverCount.addAndGet(2);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
        httpDummyServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
        httpsDummyServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
    public void teardown() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
        sharedClient = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
        httpDummyServer.stopServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
        httpsDummyServer.stopServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
    static class DummyServer extends Thread {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
        final ServerSocket ss;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
        final boolean secure;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
        ConcurrentLinkedQueue<Socket> connections = new ConcurrentLinkedQueue<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
        volatile boolean stopped;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
        DummyServer(ServerSocket ss, boolean secure) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
            super("DummyServer[" + ss.getLocalPort()+"]");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
            this.secure = secure;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
            this.ss = ss;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
        // This is a bit shaky. It doesn't handle continuation
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
        // lines, but our client shouldn't send any.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
        // Read a line from the input stream, swallowing the final
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
        // \r\n sequence. Stops at the first \n, doesn't complain
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
        // if it wasn't preceded by '\r'.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
        //
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
        String readLine(InputStream r) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
            StringBuilder b = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
            int c;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
            while ((c = r.read()) != -1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
                if (c == '\n') break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
                b.appendCodePoint(c);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
            if (b.codePointAt(b.length() -1) == '\r') {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
                b.delete(b.length() -1, b.length());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
            return b.toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
        public void run() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
                while(!stopped) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
                    Socket clientConnection = ss.accept();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
                    connections.add(clientConnection);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
                    System.out.println(now() + getName() + ": Client accepted");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
                    StringBuilder headers = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
                    Socket targetConnection = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
                    InputStream  ccis = clientConnection.getInputStream();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
                    OutputStream ccos = clientConnection.getOutputStream();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
                    Writer w = new OutputStreamWriter(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
                            clientConnection.getOutputStream(), "UTF-8");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
                    PrintWriter pw = new PrintWriter(w);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
                    System.out.println(now() + getName() + ": Reading request line");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
                    String requestLine = readLine(ccis);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
                    System.out.println(now() + getName() + ": Request line: " + requestLine);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
                    StringTokenizer tokenizer = new StringTokenizer(requestLine);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
                    String method = tokenizer.nextToken();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
                    assert method.equalsIgnoreCase("POST")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
                            || method.equalsIgnoreCase("GET");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
                    String path = tokenizer.nextToken();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
                    URI uri;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
                    try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
                        String hostport = serverAuthority();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
                        uri = new URI((secure ? "https" : "http") +"://" + hostport + path);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
                    } catch (Throwable x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
                        System.err.printf("Bad target address: \"%s\" in \"%s\"%n",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
                                path, requestLine);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
                        clientConnection.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
                        continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
                    // Read all headers until we find the empty line that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
                    // signals the end of all headers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
                    String line = requestLine;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
                    while (!line.equals("")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
                        System.out.println(now() + getName() + ": Reading header: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
                                + (line = readLine(ccis)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
                        headers.append(line).append("\r\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
                    StringBuilder response = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
                    int index = headers.toString()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
                            .toLowerCase(Locale.US)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
                            .indexOf("content-length: ");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
                    byte[] b = uri.toString().getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
                    if (index >= 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
                        index = index + "content-length: ".length();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
                        String cl = headers.toString().substring(index);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
                        StringTokenizer tk = new StringTokenizer(cl);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
                        int len = Integer.parseInt(tk.nextToken());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
                        assert len < b.length * 2;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
                        System.out.println(now() + getName()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
                                + ": received body: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
                                + new String(ccis.readNBytes(len), UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
                    System.out.println(now()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
                            + getName() + ": sending back " + uri);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
                    response.append("HTTP/1.1 200 OK\r\nContent-Length: ")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
                            .append(b.length)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
                            .append("\r\n\r\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
                    // Then send the 200 OK response to the client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
                    System.out.println(now() + getName() + ": Sending "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
                            + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
                    pw.print(response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   355
                    pw.flush();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   356
                    ccos.write(b);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   357
                    ccos.flush();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   358
                    ccos.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
                    connections.remove(clientConnection);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
                    clientConnection.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
            } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
                if (!stopped) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
                    System.out.println(now() + getName() + ": failed: " + t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
                    t.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
                    try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
                        stopServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
                    } catch (Throwable e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
            } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
                System.out.println(now() + getName() + ": exiting");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   375
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   376
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   377
        void close(Socket s) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   379
                s.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   380
            } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   381
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   382
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   383
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   384
        public void stopServer() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   385
            stopped = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   386
            ss.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   387
            connections.forEach(this::close);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   388
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   389
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   390
        public String serverAuthority() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   391
            return InetAddress.getLoopbackAddress().getHostName() + ":"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   392
                    + ss.getLocalPort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   393
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   394
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   395
        static DummyServer create(InetSocketAddress sa) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   396
            ServerSocket ss = ServerSocketFactory.getDefault()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   397
                    .createServerSocket(sa.getPort(), -1, sa.getAddress());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   398
            return  new DummyServer(ss, false);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   399
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   400
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   401
        static DummyServer create(InetSocketAddress sa, SSLContext sslContext) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   402
            ServerSocket ss = sslContext.getServerSocketFactory()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   403
                    .createServerSocket(sa.getPort(), -1, sa.getAddress());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   404
            return new DummyServer(ss, true);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   405
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   406
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   407
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   408
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   409
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   410
}