test/jdk/java/net/httpclient/CookieHeaderTest.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
 * @bug 8199851
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 * @summary Test for multiple vs single cookie header for HTTP/2 vs HTTP/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 * @modules java.base/sun.net.www.http
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.hpack
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
 *          java.logging
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
 *          jdk.httpserver
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51407
diff changeset
    34
 * @library /test/lib http2/server
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
 * @build Http2TestServer
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51407
diff changeset
    36
 * @build jdk.test.lib.net.SimpleSSLContext
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
 * @run testng/othervm
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50681
diff changeset
    38
 *       -Djdk.tls.acknowledgeCloseNotify=true
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
 *       -Djdk.httpclient.HttpClient.log=trace,headers,requests
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
 *       CookieHeaderTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import com.sun.net.httpserver.HttpsConfigurator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import com.sun.net.httpserver.HttpsServer;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51407
diff changeset
    46
import jdk.test.lib.net.SimpleSSLContext;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import org.testng.annotations.BeforeTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
import javax.net.ServerSocketFactory;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
import javax.net.ssl.SSLContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
import java.io.InputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
import java.io.OutputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
import java.io.OutputStreamWriter;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
import java.io.PrintWriter;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
import java.io.Writer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
import java.net.CookieHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
import java.net.CookieManager;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
import java.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
import java.net.ServerSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
import java.net.Socket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
import java.net.http.HttpClient.Redirect;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
import java.net.http.HttpResponse.BodyHandlers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
import java.util.ArrayList;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
import java.util.Arrays;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
import java.util.Collections;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
import java.util.HashMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
import java.util.Locale;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
import java.util.Map;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
import java.util.StringTokenizer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
import java.util.concurrent.ConcurrentHashMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
import java.util.concurrent.ConcurrentLinkedQueue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
import java.util.concurrent.atomic.AtomicLong;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
import java.util.stream.Collectors;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
import java.util.stream.Stream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
import static java.lang.System.out;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
import static org.testng.Assert.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
public class CookieHeaderTest implements HttpServerAdapters {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
    SSLContext sslContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
    HttpTestServer httpTestServer;        // HTTP/1.1    [ 6 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
    HttpTestServer httpsTestServer;       // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
    HttpTestServer http2TestServer;       // HTTP/2 ( h2c )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
    HttpTestServer https2TestServer;      // HTTP/2 ( h2  )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
    DummyServer httpDummyServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
    DummyServer httpsDummyServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
    String httpURI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
    String httpsURI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
    String http2URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
    String https2URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
    String httpDummy;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
    String httpsDummy;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
    static final String MESSAGE = "Basic CookieHeaderTest message body";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
    static final int ITERATIONS = 3;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
    static final long start = System.nanoTime();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
    public static String now() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
        long now = System.nanoTime() - start;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
        long secs = now / 1000_000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
        long mill = (now % 1000_000_000) / 1000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
        long nan = now % 1000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
    @DataProvider(name = "positive")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
    public Object[][] positive() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
        return new Object[][] {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
                { httpURI, HttpClient.Version.HTTP_1_1  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
                { httpsURI, HttpClient.Version.HTTP_1_1  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
                { httpDummy, HttpClient.Version.HTTP_1_1 },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
                { httpsDummy, HttpClient.Version.HTTP_1_1 },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
                { httpURI, HttpClient.Version.HTTP_2  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
                { httpsURI, HttpClient.Version.HTTP_2  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
                { httpDummy, HttpClient.Version.HTTP_2 },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
                { httpsDummy, HttpClient.Version.HTTP_2 },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
                { http2URI, null  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
                { https2URI, null },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
    static final AtomicLong requestCounter = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
    @Test(dataProvider = "positive")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
    void test(String uriString, HttpClient.Version version) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
        out.printf("%n---- starting (%s) ----%n", uriString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
        ConcurrentHashMap<String, List<String>> cookieHeaders
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
                = new ConcurrentHashMap<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
        CookieHandler cookieManager = new TestCookieHandler(cookieHeaders);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
        HttpClient client = HttpClient.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
                .followRedirects(Redirect.ALWAYS)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
                .cookieHandler(cookieManager)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
                .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
        assert client.cookieHandler().isPresent();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
        URI uri = URI.create(uriString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
        List<String> cookies = new ArrayList<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
        cookies.add("CUSTOMER=ARTHUR_DENT");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
        cookies.add("LOCATION=TR\u0100IN_STATION");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
        cookies.add("LOC\u0100TION=TRAIN_STATION");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
        cookies.add("ORDER=BISCUITS");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
        cookieHeaders.put("Cookie", cookies);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
                .header("X-uuid", "uuid-" + requestCounter.incrementAndGet());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
        if (version != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
            requestBuilder.version(version);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
        HttpRequest request = requestBuilder.build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
        out.println("Initial request: " + request.uri());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
        for (int i=0; i< ITERATIONS; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
            out.println("iteration: " + i);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
            out.println("  Got response: " + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
            out.println("  Got body Path: " + response.body());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
            assertEquals(response.statusCode(), 200);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
            assertEquals(response.body(), MESSAGE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
            assertEquals(response.headers().allValues("X-Request-Cookie"),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
                    cookies.stream()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
                            .filter(s -> !s.startsWith("LOC"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
                            .collect(Collectors.toList()));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
            requestBuilder = HttpRequest.newBuilder(uri)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
                    .header("X-uuid", "uuid-" + requestCounter.incrementAndGet());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
            if (version != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
                requestBuilder.version(version);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
            request = requestBuilder.build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
    // -- Infrastructure
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
    @BeforeTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
    public void setup() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
        sslContext = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
        if (sslContext == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
            throw new AssertionError("Unexpected null sslContext");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
        httpTestServer.addHandler(new CookieValidationHandler(), "/http1/cookie/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/cookie/retry";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
        httpsTestServer = HttpTestServer.of(httpsServer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
        httpsTestServer.addHandler(new CookieValidationHandler(),"/https1/cookie/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
        httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/cookie/retry";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
        http2TestServer.addHandler(new CookieValidationHandler(), "/http2/cookie/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
        http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/cookie/retry";
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   209
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
        https2TestServer.addHandler(new CookieValidationHandler(), "/https2/cookie/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
        https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/cookie/retry";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
        // DummyServer
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
        httpDummyServer = DummyServer.create(sa);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
        httpsDummyServer = DummyServer.create(sa, sslContext);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
        httpDummy = "http://" + httpDummyServer.serverAuthority() + "/http1/dummy/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
        httpsDummy = "https://" + httpsDummyServer.serverAuthority() + "/https1/dummy/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
        httpTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
        httpsTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
        http2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
        https2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
        httpDummyServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
        httpsDummyServer.start();
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
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
    public void teardown() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
        httpTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
        httpsTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
        http2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
        https2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
        httpsDummyServer.stopServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
        httpsDummyServer.stopServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
    static class TestCookieHandler extends CookieHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
        final ConcurrentHashMap<String, List<String>> cookies;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
        TestCookieHandler(ConcurrentHashMap<String, List<String>> map) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
            this.cookies = map;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
                throws IOException
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
        {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
            return cookies;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
        public void put(URI uri, Map<String, List<String>> responseHeaders)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
                throws IOException
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
        {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
            // do nothing
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
    static class CookieValidationHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
        ConcurrentHashMap<String,String> closedRequests = new ConcurrentHashMap<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
            System.out.println("CookieValidationHandler for: " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
            List<String> uuids = t.getRequestHeaders().get("X-uuid");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
            if (uuids == null || uuids.size() != 1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
                readAllRequestData(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
                try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
                    String msg = "Incorrect uuid header values:[" + uuids + "]";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
                    (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
                    t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
                    os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
                return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
            String uuid = uuids.get(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
            // retrying
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
            if (closedRequests.putIfAbsent(uuid, t.getRequestURI().toString()) == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
                if (t.getExchangeVersion() == HttpClient.Version.HTTP_1_1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
                    // Throwing an exception here only causes a retry
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
                    // with HTTP_1_1 - where it forces the server to close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
                    // the connection.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
                    // For HTTP/2 then throwing an IOE would cause the server
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
                    // to close the stream, and throwing anything else would
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
                    // cause it to close the connection, but neither would
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
                    // cause the client to retry.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
                    // So we simply do not try to retry with HTTP/2 and just verify
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
                    // we have received the expected cookie
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
                    throw new IOException("Closing on first request");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
            // Check whether this request was upgraded.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
            // An upgraded request will have a version of HTTP_2 and
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
            // an Upgrade: h2c header
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
            HttpClient.Version version = t.getExchangeVersion();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
            List<String> upgrade = t.getRequestHeaders().get("Upgrade");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
            if (upgrade == null) upgrade = List.of();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
            boolean upgraded = version == HttpClient.Version.HTTP_2
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
                    && upgrade.stream().anyMatch("h2c"::equalsIgnoreCase);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
            // not retrying
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
            readAllRequestData(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
            try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
                List<String> cookie = t.getRequestHeaders().get("Cookie");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
                if (cookie != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
                    if (version == HttpClient.Version.HTTP_1_1 || upgraded) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
                        if (cookie.size() == 1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
                            cookie = List.of(cookie.get(0).split("; "));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
                        } else if (cookie.size() > 1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
                            String msg = "Found multiple 'Cookie:' lines for version=%s (upgraded=%s): %s";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
                            msg = String.format(msg, version, upgraded, cookie);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
                            (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
                            t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
                            os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
                            return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
                    Collections.sort(cookie = new ArrayList<String>(cookie));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
                if (cookie == null || cookie.size() == 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
                    String msg = "No cookie header present";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
                    (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
                    t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
                    os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
                } else if (!cookie.get(0).equals("CUSTOMER=ARTHUR_DENT")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
                    String msg = "Incorrect cookie header value:[" + cookie.get(0) + "]";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
                    (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
                    t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
                    os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
                } else if (cookie.size() == 2 && !cookie.get(1).equals("ORDER=BISCUITS")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
                    String msg = "Incorrect cookie header value:[" + cookie.get(0) + "]";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
                    (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
                    t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
                    os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
                } else if (cookie.size() != 2) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
                    String msg = "Incorrect cookie header values:[" + cookie + "]";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
                    (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
                    t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
                    os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
                } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
                    assert cookie.get(0).equals("CUSTOMER=ARTHUR_DENT");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
                    byte[] bytes = MESSAGE.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
                    for (String value : cookie) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
                        t.getResponseHeaders().addHeader("X-Request-Cookie", value);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
                    t.sendResponseHeaders(200, bytes.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
                    os.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
            } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
                closedRequests.remove(uuid);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   355
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   356
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   357
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   358
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
    static void readAllRequestData(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
        try (InputStream is = t.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
            is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
    static class DummyServer extends Thread {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
        final ServerSocket ss;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
        final boolean secure;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
        ConcurrentLinkedQueue<Socket> connections = new ConcurrentLinkedQueue<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
        volatile boolean stopped;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
        DummyServer(ServerSocket ss, boolean secure) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
            super("DummyServer[" + ss.getLocalPort()+"]");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
            this.secure = secure;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
            this.ss = ss;
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
        // This is a bit shaky. It doesn't handle continuation
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
        // lines, but our client shouldn't send any.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   379
        // Read a line from the input stream, swallowing the final
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   380
        // \r\n sequence. Stops at the first \n, doesn't complain
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   381
        // if it wasn't preceded by '\r'.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   382
        //
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   383
        String readLine(InputStream r) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   384
            StringBuilder b = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   385
            int c;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   386
            while ((c = r.read()) != -1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   387
                if (c == '\n') break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   388
                b.appendCodePoint(c);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   389
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   390
            if (b.codePointAt(b.length() -1) == '\r') {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   391
                b.delete(b.length() -1, b.length());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   392
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   393
            return b.toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   394
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   395
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   396
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   397
        public void run() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   398
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   399
                while(!stopped) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   400
                    Socket clientConnection = ss.accept();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   401
                    connections.add(clientConnection);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   402
                    System.out.println(now() + getName() + ": Client accepted");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   403
                    StringBuilder headers = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   404
                    Socket targetConnection = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   405
                    InputStream  ccis = clientConnection.getInputStream();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   406
                    OutputStream ccos = clientConnection.getOutputStream();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   407
                    Writer w = new OutputStreamWriter(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   408
                            clientConnection.getOutputStream(), "UTF-8");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   409
                    PrintWriter pw = new PrintWriter(w);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   410
                    System.out.println(now() + getName() + ": Reading request line");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   411
                    String requestLine = readLine(ccis);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   412
                    System.out.println(now() + getName() + ": Request line: " + requestLine);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   413
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   414
                    StringTokenizer tokenizer = new StringTokenizer(requestLine);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   415
                    String method = tokenizer.nextToken();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   416
                    assert method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("GET");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   417
                    String path = tokenizer.nextToken();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   418
                    URI uri;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   419
                    try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   420
                        String hostport = serverAuthority();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   421
                        uri = new URI((secure ? "https" : "http") +"://" + hostport + path);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   422
                    } catch (Throwable x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   423
                        System.err.printf("Bad target address: \"%s\" in \"%s\"%n",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   424
                                path, requestLine);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   425
                        clientConnection.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   426
                        continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   427
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   428
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   429
                    // Read all headers until we find the empty line that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   430
                    // signals the end of all headers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   431
                    String line = requestLine;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   432
                    String cookies = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   433
                    while (!line.equals("")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   434
                        System.out.println(now() + getName() + ": Reading header: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   435
                                + (line = readLine(ccis)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   436
                        if (line.startsWith("Cookie:")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   437
                            if (cookies == null) cookies = line;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   438
                            else cookies = cookies + "\n" + line;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   439
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   440
                        headers.append(line).append("\r\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   441
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   442
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   443
                    StringBuilder response = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   444
                    StringBuilder xheaders = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   445
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   446
                    int index = headers.toString()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   447
                            .toLowerCase(Locale.US)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   448
                            .indexOf("content-length: ");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   449
                    if (index >= 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   450
                        index = index + "content-length: ".length();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   451
                        String cl = headers.toString().substring(index);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   452
                        StringTokenizer tk = new StringTokenizer(cl);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   453
                        int len = Integer.parseInt(tk.nextToken());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   454
                        System.out.println(now() + getName()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   455
                                + ": received body: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   456
                                + new String(ccis.readNBytes(len), UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   457
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   458
                    String resp = MESSAGE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   459
                    String status = "200 OK";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   460
                    if (cookies == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   461
                        resp = "No cookies found in headers";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   462
                        status = "500 Internal Server Error";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   463
                    } else if (cookies.contains("\n")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   464
                        resp = "More than one 'Cookie:' line found: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   465
                                + Arrays.asList(cookies.split("\n"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   466
                        status = "500 Internal Server Error";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   467
                    } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   468
                        List<String> values =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   469
                                Stream.of(cookies.substring("Cookie:".length()).trim().split("; "))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   470
                                        .map(String::trim)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   471
                                        .collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   472
                        Collections.sort(values);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   473
                        if (values.size() != 2) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   474
                            resp = "Bad cookie list: " + values;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   475
                            status = "500 Internal Server Error";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   476
                        } else if (!values.get(0).equals("CUSTOMER=ARTHUR_DENT")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   477
                            resp = "Unexpected cookie: " + values.get(0) + " in " + values;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   478
                            status = "500 Internal Server Error";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   479
                        } else if (!values.get(1).equals("ORDER=BISCUITS")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   480
                            resp = "Unexpected cookie: " + values.get(1) + " in " + values;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   481
                            status = "500 Internal Server Error";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   482
                        } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   483
                            for (String cookie : values) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   484
                                xheaders.append("X-Request-Cookie: ")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   485
                                        .append(cookie)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   486
                                        .append("\r\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   487
                            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   488
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   489
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   490
                    byte[] b = resp.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   491
                    System.out.println(now()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   492
                            + getName() + ": sending back " + uri);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   493
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   494
                    response.append("HTTP/1.1 ")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   495
                            .append(status)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   496
                            .append("\r\nContent-Length: ")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   497
                            .append(b.length)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   498
                            .append("\r\n")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   499
                            .append(xheaders)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   500
                            .append("\r\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   501
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   502
                    // Then send the 200 OK response to the client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   503
                    System.out.println(now() + getName() + ": Sending "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   504
                            + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   505
                    pw.print(response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   506
                    pw.flush();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   507
                    ccos.write(b);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   508
                    ccos.flush();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   509
                    ccos.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   510
                    connections.remove(clientConnection);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   511
                    clientConnection.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   512
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   513
            } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   514
                if (!stopped) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   515
                    System.out.println(now() + getName() + ": failed: " + t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   516
                    t.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   517
                    try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   518
                        stopServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   519
                    } catch (Throwable e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   520
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   521
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   522
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   523
            } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   524
                System.out.println(now() + getName() + ": exiting");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   525
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   526
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   527
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   528
        void close(Socket s) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   529
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   530
                s.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   531
            } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   532
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   533
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   534
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   535
        public void stopServer() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   536
            stopped = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   537
            ss.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   538
            connections.forEach(this::close);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   539
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   540
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   541
        public String serverAuthority() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   542
            return InetAddress.getLoopbackAddress().getHostName() + ":"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   543
                    + ss.getLocalPort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   544
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   545
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   546
        static DummyServer create(InetSocketAddress sa) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   547
            ServerSocket ss = ServerSocketFactory.getDefault()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   548
                    .createServerSocket(sa.getPort(), -1, sa.getAddress());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   549
            return  new DummyServer(ss, false);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   550
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   551
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   552
        static DummyServer create(InetSocketAddress sa, SSLContext sslContext) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   553
            ServerSocket ss = sslContext.getServerSocketFactory()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   554
                    .createServerSocket(sa.getPort(), -1, sa.getAddress());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   555
            return new DummyServer(ss, true);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   556
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   557
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   558
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   559
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   560
}