test/jdk/java/net/httpclient/EncodedCharsInURI.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 8199683
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 * @summary Tests that escaped characters in URI are correctly
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 *          handled (not re-escaped and not unescaped)
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51407
diff changeset
    29
 * @library /test/lib http2/server
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51407
diff changeset
    30
 * @build jdk.test.lib.net.SimpleSSLContext HttpServerAdapters EncodedCharsInURI
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
 * @modules java.base/sun.net.www.http
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
 *          java.net.http/jdk.internal.net.http.hpack
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
 * @run testng/othervm
51407
910f7b56592f 8207009: TLS 1.3 half-close and synchronization issues
xuelei
parents: 50681
diff changeset
    36
 *        -Djdk.tls.acknowledgeCloseNotify=true
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    37
 *        -Djdk.internal.httpclient.debug=true
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    38
 *        -Djdk.httpclient.HttpClient.log=headers,errors EncodedCharsInURI
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
//*        -Djdk.internal.httpclient.debug=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import com.sun.net.httpserver.HttpServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpsConfigurator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
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
    45
import jdk.test.lib.net.SimpleSSLContext;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import org.testng.annotations.AfterClass;
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.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
import java.net.ServerSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
import java.net.Socket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
import java.net.http.HttpRequest.BodyPublisher;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
import java.net.http.HttpRequest.BodyPublishers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
import java.net.http.HttpResponse.BodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
import java.net.http.HttpResponse.BodyHandlers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
import java.util.Locale;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
import java.util.StringTokenizer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
import java.util.concurrent.ConcurrentHashMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
import java.util.concurrent.ConcurrentLinkedQueue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
import java.util.concurrent.ConcurrentMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
import java.util.concurrent.Executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
import java.util.concurrent.Executors;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
import java.util.concurrent.atomic.AtomicLong;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
import static java.lang.String.format;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
import static java.lang.System.in;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
import static java.lang.System.out;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    83
import static java.nio.charset.StandardCharsets.US_ASCII;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
import static java.net.http.HttpClient.Builder.NO_PROXY;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
import static org.testng.Assert.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
public class EncodedCharsInURI implements HttpServerAdapters {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
    SSLContext sslContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
    HttpTestServer httpTestServer;    // HTTP/1.1    [ 4 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
    HttpTestServer httpsTestServer;   // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
    DummyServer    httpDummyServer;    // HTTP/1.1    [ 2 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
    DummyServer    httpsDummyServer;   // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
    String httpURI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
    String httpURI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
    String httpsURI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
    String httpsURI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
    String http2URI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
    String http2URI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
    String https2URI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
    String https2URI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
    String httpDummy;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
    String httpsDummy;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
    static final int ITERATION_COUNT = 1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
    // a shared executor helps reduce the amount of threads created by the test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
    static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
    static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
    static volatile boolean tasksFailed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
    static final AtomicLong serverCount = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
    static final AtomicLong clientCount = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
    static final long start = System.nanoTime();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
    public static String now() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
        long now = System.nanoTime() - start;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
        long secs = now / 1000_000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
        long mill = (now % 1000_000_000) / 1000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
        long nan = now % 1000_000;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
    private volatile HttpClient sharedClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
    static class TestExecutor implements Executor {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
        final AtomicLong tasks = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
        Executor executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
        TestExecutor(Executor executor) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
            this.executor = executor;
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
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
        public void execute(Runnable command) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
            long id = tasks.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
            executor.execute(() -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                    command.run();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
                } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
                    tasksFailed = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
                    System.out.printf(now() + "Task %s failed: %s%n", id, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
                    System.err.printf(now() + "Task %s failed: %s%n", id, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
                    FAILURES.putIfAbsent("Task " + id, t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
                    throw t;
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
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
    @AfterClass
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
    static final void printFailedTests() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
        out.println("\n=========================");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
            out.printf("%n%sCreated %d servers and %d clients%n",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
                    now(), serverCount.get(), clientCount.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
            if (FAILURES.isEmpty()) return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
            out.println("Failed tests: ");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
            FAILURES.entrySet().forEach((e) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
                out.printf("\t%s: %s%n", e.getKey(), e.getValue());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
                e.getValue().printStackTrace(out);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
            });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
            if (tasksFailed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
                System.out.println("WARNING: Some tasks failed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
        } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
            out.println("\n=========================\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
    private String[] uris() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
        return new String[] {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
                httpDummy,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
                httpsDummy,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
                httpURI_fixed,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
                httpURI_chunk,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
                httpsURI_fixed,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
                httpsURI_chunk,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
                http2URI_fixed,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
                http2URI_chunk,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
                https2URI_fixed,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
                https2URI_chunk,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
        };
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
    @DataProvider(name = "noThrows")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
    public Object[][] noThrows() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
        String[] uris = uris();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
        Object[][] result = new Object[uris.length * 2][];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
        //Object[][] result = new Object[uris.length][];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
        int i = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
        for (boolean sameClient : List.of(false, true)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
            //if (!sameClient) continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
            for (String uri: uris()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
                result[i++] = new Object[] {uri, sameClient};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
        assert i == uris.length * 2;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        // assert i == uris.length ;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
        return result;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
    private HttpClient makeNewClient() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
        clientCount.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
        return HttpClient.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
                .executor(executor)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
                .proxy(NO_PROXY)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
                .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
    HttpClient newHttpClient(boolean share) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
        if (!share) return makeNewClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
        HttpClient shared = sharedClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
        if (shared != null) return shared;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
        synchronized (this) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
            shared = sharedClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
            if (shared == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
                shared = sharedClient = makeNewClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
            return shared;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
    final String ENCODED = "/01%252F03/";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
    @Test(dataProvider = "noThrows")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
    public void testEncodedChars(String uri, boolean sameClient)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
            throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
        HttpClient client = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
        out.printf("%n%s testEncodedChars(%s, %b)%n", now(), uri, sameClient);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
        uri = uri + ENCODED;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
        for (int i=0; i< ITERATION_COUNT; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
            if (!sameClient || client == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
                client = newHttpClient(sameClient);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
            BodyPublisher bodyPublisher = BodyPublishers.ofString(uri);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
                    .POST(bodyPublisher)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
                    .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
            BodyHandler<String> handler = BodyHandlers.ofString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
            CompletableFuture<HttpResponse<String>> responseCF = client.sendAsync(req, handler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
            HttpResponse<String> response = responseCF.join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
            String body = response.body();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
            if (!uri.contains(body)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
                System.err.println("Test failed: " + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
                throw new RuntimeException(uri + " doesn't contain '" + body + "'");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
                System.out.println("Found expected " + body + " in " + uri);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
        }
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
    @BeforeTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
    public void setup() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
        sslContext = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
        if (sslContext == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
            throw new AssertionError("Unexpected null sslContext");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
        // HTTP/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
        HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
        HttpTestHandler h1_chunkHandler = new HTTP_ChunkedHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
        httpTestServer.addHandler(h1_fixedLengthHandler, "/http1/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
        httpTestServer.addHandler(h1_chunkHandler, "/http1/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
        httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
        httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
        httpsTestServer = HttpTestServer.of(httpsServer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
        httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
        httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
        httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
        httpsURI_chunk = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
        // HTTP/2
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
        HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
        HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   289
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
        // DummyServer
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
        httpDummyServer = DummyServer.create(sa);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
        httpsDummyServer = DummyServer.create(sa, sslContext);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
        httpDummy = "http://" + httpDummyServer.serverAuthority() + "/http1/dummy/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
        httpsDummy = "https://" + httpsDummyServer.serverAuthority() + "/https1/dummy/x";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
        serverCount.addAndGet(6);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
        httpTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
        httpsTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
        http2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
        https2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
        httpDummyServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
        httpsDummyServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
    public void teardown() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
        sharedClient = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
        httpTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
        httpsTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
        http2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
        https2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
        httpDummyServer.stopServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
        httpsDummyServer.stopServer();
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
    static class HTTP_FixedLengthHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
            out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
            byte[] req;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
            try (InputStream is = t.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
                req = is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
            String uri = new String(req, UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
            byte[] resp = t.getRequestURI().toString().getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
            if (!uri.contains(t.getRequestURI().toString())) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
                t.sendResponseHeaders(404, resp.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
                t.sendResponseHeaders(200, resp.length);  //fixed content length
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
            try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
                os.write(resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
    static class HTTP_ChunkedHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
            out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
            byte[] resp;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
            try (InputStream is = t.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
                resp = is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
            t.sendResponseHeaders(200, -1); // chunked/variable
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
            try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
                os.write(resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
            }
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
    static class DummyServer extends Thread {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
        final ServerSocket ss;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
        final boolean secure;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
        ConcurrentLinkedQueue<Socket> connections = new ConcurrentLinkedQueue<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
        volatile boolean stopped;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
        DummyServer(ServerSocket ss, boolean secure) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
            super("DummyServer[" + ss.getLocalPort()+"]");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
            this.secure = secure;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
            this.ss = ss;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
        // This is a bit shaky. It doesn't handle continuation
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
        // lines, but our client shouldn't send any.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
        // Read a line from the input stream, swallowing the final
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
        // \r\n sequence. Stops at the first \n, doesn't complain
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
        // if it wasn't preceded by '\r'.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
        //
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   375
        String readLine(InputStream r) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   376
            StringBuilder b = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   377
            int c;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
            while ((c = r.read()) != -1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   379
                if (c == '\n') break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   380
                b.appendCodePoint(c);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   381
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   382
            if (b.codePointAt(b.length() -1) == '\r') {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   383
                b.delete(b.length() -1, b.length());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   384
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   385
            return b.toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   386
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   387
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   388
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   389
        public void run() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   390
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   391
                while(!stopped) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   392
                    Socket clientConnection = ss.accept();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   393
                    connections.add(clientConnection);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   394
                    System.out.println(now() + getName() + ": Client accepted");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   395
                    StringBuilder headers = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   396
                    Socket targetConnection = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   397
                    InputStream  ccis = clientConnection.getInputStream();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   398
                    OutputStream ccos = clientConnection.getOutputStream();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   399
                    System.out.println(now() + getName() + ": Reading request line");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   400
                    String requestLine = readLine(ccis);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   401
                    System.out.println(now() + getName() + ": Request line: " + requestLine);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   402
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   403
                    StringTokenizer tokenizer = new StringTokenizer(requestLine);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   404
                    String method = tokenizer.nextToken();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   405
                    assert method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("GET");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   406
                    String path = tokenizer.nextToken();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   407
                    URI uri;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   408
                    try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   409
                        String hostport = serverAuthority();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   410
                        uri = new URI((secure ? "https" : "http") +"://" + hostport + path);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   411
                    } catch (Throwable x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   412
                        System.err.printf("Bad target address: \"%s\" in \"%s\"%n",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   413
                                path, requestLine);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   414
                        clientConnection.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   415
                        continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   416
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   417
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   418
                    // Read all headers until we find the empty line that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   419
                    // signals the end of all headers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   420
                    String line = requestLine;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   421
                    while (!line.equals("")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   422
                        System.out.println(now() + getName() + ": Reading header: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   423
                                + (line = readLine(ccis)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   424
                        headers.append(line).append("\r\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   425
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   426
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   427
                    StringBuilder response = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   428
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   429
                    int index = headers.toString()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   430
                            .toLowerCase(Locale.US)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   431
                            .indexOf("content-length: ");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   432
                    byte[] b = uri.toString().getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   433
                    if (index >= 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   434
                        index = index + "content-length: ".length();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   435
                        String cl = headers.toString().substring(index);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   436
                        StringTokenizer tk = new StringTokenizer(cl);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   437
                        int len = Integer.parseInt(tk.nextToken());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   438
                        assert len < b.length * 2;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   439
                        System.out.println(now() + getName()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   440
                                + ": received body: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   441
                                + new String(ccis.readNBytes(len), UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   442
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   443
                    System.out.println(now()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   444
                            + getName() + ": sending back " + uri);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   445
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   446
                    response.append("HTTP/1.1 200 OK\r\nContent-Length: ")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   447
                            .append(b.length)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   448
                            .append("\r\n\r\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   449
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   450
                    // Then send the 200 OK response to the client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   451
                    System.out.println(now() + getName() + ": Sending "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   452
                            + response);
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   453
                    ccos.write(response.toString().getBytes(UTF_8));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   454
                    ccos.flush();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   455
                    System.out.println(now() + getName() + ": sent response headers");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   456
                    ccos.write(b);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   457
                    ccos.flush();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   458
                    ccos.close();
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   459
                    System.out.println(now() + getName() + ": sent " + b.length + " body bytes");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   460
                    connections.remove(clientConnection);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   461
                    clientConnection.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   462
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   463
            } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   464
                if (!stopped) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   465
                    System.out.println(now() + getName() + ": failed: " + t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   466
                    t.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   467
                    try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   468
                        stopServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   469
                    } catch (Throwable e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   470
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   471
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   472
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   473
            } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   474
                System.out.println(now() + getName() + ": exiting");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   475
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   476
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   477
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   478
        void close(Socket s) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   479
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   480
                s.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   481
            } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   482
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   483
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   484
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   485
        public void stopServer() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   486
            stopped = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   487
            ss.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   488
            connections.forEach(this::close);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   489
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   490
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   491
        public String serverAuthority() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   492
            return InetAddress.getLoopbackAddress().getHostName() + ":"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   493
                    + ss.getLocalPort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   494
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   495
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   496
        static DummyServer create(InetSocketAddress sa) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   497
            ServerSocket ss = ServerSocketFactory.getDefault()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   498
                    .createServerSocket(sa.getPort(), -1, sa.getAddress());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   499
            return  new DummyServer(ss, false);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   500
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   501
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   502
        static DummyServer create(InetSocketAddress sa, SSLContext sslContext) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   503
            ServerSocket ss = sslContext.getServerSocketFactory()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   504
                    .createServerSocket(sa.getPort(), -1, sa.getAddress());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   505
            return new DummyServer(ss, true);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   506
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   507
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   508
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   509
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   510
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   511
}